summaryrefslogtreecommitdiffstats
path: root/src/lib/test-var-expand.c
blob: 558ef76b869d1ea958b5a5000e3efcf0c70912cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */

#include "test-lib.h"
#include "str.h"
#include "env-util.h"
#include "hostpid.h"
#include "var-expand.h"
#include "var-expand-private.h"

struct var_expand_test {
	const char *in;
	const char *out;
	int ret;
};

struct var_get_key_range_test {
	const char *in;
	unsigned int idx, size;
};

static void test_var_expand_ranges(void)
{
	static const struct var_expand_test tests[] = {
		{ "%v", "value1234", 1 },
		{ "%3v", "val", 1 },
		{ "%3.2v", "ue", 1 },
		{ "%3.-2v", "ue12", 1 },
		{ "%-3.2v", "23", 1 },
		{ "%0.-1v", "value123", 1 },
		{ "%-4.-1v", "123", 1 }
	};
	static const struct var_expand_table table[] = {
		{ 'v', "value1234", NULL },
		{ '\0', NULL, NULL }
	};
	string_t *str = t_str_new(128);
	const char *error;
	unsigned int i;

	test_begin("var_expand - ranges");
	for (i = 0; i < N_ELEMENTS(tests); i++) {
		str_truncate(str, 0);
		test_assert(var_expand(str, tests[i].in, table, &error) == tests[i].ret);
		test_assert(strcmp(tests[i].out, str_c(str)) == 0);
	}
	test_end();
}

static void test_var_expand_builtin(void)
{
	static struct var_expand_test tests[] = {
		{ "%{hostname}", NULL, 1 },
		{ "%{pid}", NULL, 1 },
		{ "a%{env:FOO}b", "abaRb", 1 },
		{ "%50Hv", "1f", 1 },
		{ "%50Hw", "2e", 1 },
		{ "%50Nv", "25", 1 },
		{ "%50Nw", "e", 1 },

		{ "%{nonexistent}", "UNSUPPORTED_VARIABLE_nonexistent", 0 },
		{ "%1.2M{nonexistent:default}", "UNSUPPORTED_VARIABLE_nonexistent", 0 },
		{ "%x", "UNSUPPORTED_VARIABLE_x", 0 },
		{ "%5Mm", "UNSUPPORTED_VARIABLE_m", 0 },
	};
	static const struct var_expand_table table[] = {
		{ 'v', "value", NULL },
		{ 'w', "value2", NULL },
		{ '\0', NULL, NULL }
	};
	string_t *str = t_str_new(128);
	const char *error;
	unsigned int i;

	tests[0].out = my_hostname;
	tests[1].out = my_pid;
	env_put("FOO", "baR");

	test_begin("var_expand - builtin");
	for (i = 0; i < N_ELEMENTS(tests); i++) {
		str_truncate(str, 0);
		test_assert_idx(var_expand(str, tests[i].in, table, &error) == tests[i].ret, i);
		test_assert_idx(strcmp(tests[i].out, str_c(str)) == 0, i);
	}
	test_end();
}

static void test_var_get_key_range(void)
{
	static const struct var_get_key_range_test tests[] = {
		{ "", 0, 0 },
		{ "{", 1, 0 },
		{ "k", 0, 1 },
		{ "{key}", 1, 3 },
		{ "5.5Rk", 4, 1 },
		{ "5.5R{key}", 5, 3 },
		{ "{key", 1, 3 },
		{ "{if;%{if;%{value};eq;value;t;f};eq;t;t;f}", 1, 39 },
	};
	unsigned int i, idx, size;

	test_begin("var_get_key_range");
	for (i = 0; i < N_ELEMENTS(tests); i++) {
		var_get_key_range(tests[i].in, &idx, &size);
		test_assert_idx(tests[i].idx == idx, i);
		test_assert_idx(tests[i].size == size, i);

		if (tests[i].size == 1)
			test_assert_idx(tests[i].in[idx] == var_get_key(tests[i].in), i);
	}
	test_end();
}

static int test_var_expand_func1(const char *data, void *context,
				 const char **value_r,
				 const char **error_r ATTR_UNUSED)
{
	test_assert(*(int *)context == 0xabcdef);
	*value_r = t_strdup_printf("<%s>", data);
	return 1;
}

static int test_var_expand_func2(const char *data ATTR_UNUSED,
				 void *context ATTR_UNUSED,
				 const char **value_r,
				 const char **error_r ATTR_UNUSED)
{
	*value_r = "";
	return 1;
}

static int test_var_expand_func3(const char *data ATTR_UNUSED,
				 void *context ATTR_UNUSED,
				 const char **value_r,
				 const char **error_r ATTR_UNUSED)
{
	*value_r = NULL;
	return 1;
}

static int test_var_expand_func4(const char *data,
				 void *context ATTR_UNUSED,
				 const char **value_r ATTR_UNUSED,
				 const char **error_r)
{
	*error_r = t_strdup_printf("Unknown data %s", data == NULL ? "" : data);
	return 0;
}

static int test_var_expand_func5(const char *data ATTR_UNUSED,
				 void *context ATTR_UNUSED,
				 const char **value_r ATTR_UNUSED,
				 const char **error_r)
{
	*error_r = "Internal error";
	return -1;
}

static void test_var_expand_with_funcs(void)
{
	static const struct var_expand_test tests[] = {
		{ "%{func1}", "<>", 1 },
		{ "%{func1:foo}", "<foo>", 1 },
		{ "%{func2}", "", 1 },
		{ "%{func3}", "", 1 },
		{ "%{func4}", "", 0 },
		{ "%{func5}", "", -1 },
		{ "%{func4}%{func5}", "", -1 },
		{ "%{func5}%{func4}%{func3}", "", -1 },
	};
	static const struct var_expand_table table[] = {
		{ '\0', NULL, NULL }
	};
	static const struct var_expand_func_table func_table[] = {
		{ "func1", test_var_expand_func1 },
		{ "func2", test_var_expand_func2 },
		{ "func3", test_var_expand_func3 },
		{ "func4", test_var_expand_func4 },
		{ "func5", test_var_expand_func5 },
		{ NULL, NULL }
	};
	string_t *str = t_str_new(128);
	const char *error;
	unsigned int i;
	int ctx = 0xabcdef;

	test_begin("var_expand_with_funcs");
	for (i = 0; i < N_ELEMENTS(tests); i++) {
		str_truncate(str, 0);
		test_assert_idx(var_expand_with_funcs(str, tests[i].in, table, func_table, &ctx, &error) == tests[i].ret, i);
		test_assert_idx(strcmp(tests[i].out, str_c(str)) == 0, i);
	}
	test_end();
}

static void test_var_get_key(void)
{
	static const struct {
		const char *str;
		char key;
	} tests[] = {
		{ "x", 'x' },
		{ "2.5Mx", 'x' },
		{ "200MDx", 'x' },
		{ "200MD{foo}", '{' },
		{ "{foo}", '{' },
		{ "", '\0' },
	};

	test_begin("var_get_key");
	for (unsigned int i = 0; i < N_ELEMENTS(tests); i++)
		test_assert_idx(var_get_key(tests[i].str) == tests[i].key, i);
	test_end();
}

static void test_var_has_key(void)
{
	static const struct {
		const char *str;
		char key;
		const char *long_key;
		bool result;
	} tests[] = {
		{ "%x%y", 'x', NULL, TRUE },
		{ "%x%y", 'y', NULL, TRUE },
		{ "%x%y", 'z', NULL, FALSE },
		{ "%{foo}", 'f', NULL, FALSE },
		{ "%{foo}", 'o', NULL, FALSE },
		{ "%{foo}", '\0', "foo", TRUE },
		{ "%{foo}", 'o', "foo", TRUE },
		{ "%2.5Mx%y", 'x', NULL, TRUE },
		{ "%2.5M{foo}", '\0', "foo", TRUE },
	};

	test_begin("var_has_key");
	for (unsigned int i = 0; i < N_ELEMENTS(tests); i++)
		test_assert_idx(var_has_key(tests[i].str, tests[i].key, tests[i].long_key) == tests[i].result, i);
	test_end();
}

static int test_var_expand_hashing_func1(const char *data,
					 void *context ATTR_UNUSED,
					 const char **value_r,
					 const char **error_r ATTR_UNUSED)
{
	*value_r = data;
	return 1;
}

static int test_var_expand_bad_func(struct var_expand_context *ctx ATTR_UNUSED,
				    const char *key,
				    const char *field ATTR_UNUSED,
				    const char **result_r ATTR_UNUSED,
				    const char **error_r)
{
	if (strcmp(key, "notfound") == 0) return 0;
	*error_r = "Bad parameters";
	return -1;
}

static const struct var_expand_extension_func_table test_extension_funcs[] = {
	{ "notfound", test_var_expand_bad_func },
	{ "badparam", test_var_expand_bad_func },
	{ NULL, NULL }
};

static void test_var_expand_extensions(void)
{
	const char *error;
	test_begin("var_expand_extensions");

	var_expand_register_func_array(test_extension_funcs);

	static const struct var_expand_table table[] = {
		{'\0', "example", "value" },
		{'\0', "other-example", "other-value" },
		{'\0', NULL, NULL}
	};

	static const struct {
		const char *in;
		const char *out;
	} tests[] = {
		{ "md5: %M{value} %{md5:value}", "md5: 1a79a4d60de6718e8e5b326e338ae533 1a79a4d60de6718e8e5b326e338ae533" },
		{ "sha1: %{sha1:value}", "sha1: c3499c2729730a7f807efb8676a92dcb6f8a3f8f" },
		{ "sha1: %{sha1:func1:example}", "sha1: c3499c2729730a7f807efb8676a92dcb6f8a3f8f" },
		{ "truncate: %{sha1;truncate=12:value}", "truncate: 0c34" },
		{ "truncate: %{sha1;truncate=16:value}", "truncate: c349" },
		{ "rounds,salt: %{sha1;rounds=1000,salt=seawater:value}", "rounds,salt: b515c85884f6b82dc7588279f3643a73e55d2289" },
		{ "rounds,salt,expand: %{sha1;rounds=1000,salt=%{other-value}:value} %{other-value}", "rounds,salt,expand: 49a598ee110af615e175f2e4511cc5d7ccff96ab other-example" },
		{ "format: %4.8{sha1:value}", "format: 9c272973" },
		{ "base64: %{sha1;format=base64:value}", "base64: w0mcJylzCn+AfvuGdqkty2+KP48=" },
	};

	static const struct var_expand_func_table func_table[] = {
		{ "func1", test_var_expand_hashing_func1 },
		{ NULL, NULL }
	};

	string_t *str = t_str_new(128);

	for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
		str_truncate(str, 0);
		error = NULL;
		test_assert(var_expand_with_funcs(str, tests[i].in, table,
			    func_table, NULL, &error) == 1);
		test_assert_idx(strcmp(str_c(str), tests[i].out) == 0, i);
		if (error != NULL) {
			i_debug("Error: %s", error);
		}
	}

	test_assert(var_expand_with_funcs(str, "notfound: %{notfound:field}",
		    table, func_table, NULL, &error) == 0);
	error = NULL;
	test_assert(var_expand_with_funcs(str, "notfound: %{badparam:field}",
		    table, func_table, NULL, &error) == -1);
	test_assert(error != NULL);

	var_expand_unregister_func_array(test_extension_funcs);

	test_end();
}

static void test_var_expand_if(void)
{
	static const struct var_expand_table table[] = {
		{ 'a', "alpha", "alpha" },
		{ 'b', "beta", "beta" },
		{ 'o', "1", "one" },
		{ 't', "2", "two" },
		{ '\0', ";:", "evil1" },
		{ '\0', ";test;", "evil2" },
		{ '\0', NULL, NULL }
	};
	const char *error;
	string_t *dest = t_str_new(64);
	test_begin("var_expand_if");

	static const struct var_expand_test tests[] = {
		/* basic numeric operand test */
		{ "%{if;1;==;1;yes;no}", "yes", 1 },
		{ "%{if;1;==;2;yes;no}", "no", 1 },
		{ "%{if;1;<;1;yes;no}", "no", 1 },
		{ "%{if;1;<;2;yes;no}", "yes", 1 },
		{ "%{if;1;<=;1;yes;no}", "yes", 1 },
		{ "%{if;1;<=;2;yes;no}", "yes", 1 },
		{ "%{if;1;>;1;yes;no}", "no", 1 },
		{ "%{if;1;>;2;yes;no}", "no", 1 },
		{ "%{if;1;>=;1;yes;no}", "yes", 1 },
		{ "%{if;1;>=;2;yes;no}", "no", 1 },
		{ "%{if;1;!=;1;yes;no}", "no", 1 },
		{ "%{if;1;!=;2;yes;no}", "yes", 1 },
		/* basic string operand test */
		{ "%{if;a;eq;a;yes;no}", "yes", 1 },
		{ "%{if;a;eq;b;yes;no}", "no", 1 },
		{ "%{if;a;lt;a;yes;no}", "no", 1 },
		{ "%{if;a;lt;b;yes;no}", "yes", 1 },
		{ "%{if;a;le;a;yes;no}", "yes", 1 },
		{ "%{if;a;le;b;yes;no}", "yes", 1 },
		{ "%{if;a;gt;a;yes;no}", "no", 1 },
		{ "%{if;a;gt;b;yes;no}", "no", 1 },
		{ "%{if;a;ge;a;yes;no}", "yes", 1 },
		{ "%{if;a;ge;b;yes;no}", "no", 1 },
		{ "%{if;a;ne;a;yes;no}", "no", 1 },
		{ "%{if;a;ne;b;yes;no}", "yes", 1 },
		{ "%{if;a;*;a;yes;no}", "yes", 1 },
		{ "%{if;a;*;b;yes;no}", "no", 1 },
		{ "%{if;a;*;*a*;yes;no}", "yes", 1 },
		{ "%{if;a;*;*b*;yes;no}", "no", 1 },
		{ "%{if;a;*;*;yes;no}", "yes", 1 },
		{ "%{if;a;!*;a;yes;no}", "no", 1 },
		{ "%{if;a;!*;b;yes;no}", "yes", 1 },
		{ "%{if;a;!*;*a*;yes;no}", "no", 1 },
		{ "%{if;a;!*;*b*;yes;no}", "yes", 1 },
		{ "%{if;a;!*;*;yes;no}", "no", 1 },
		{ "%{if;a;~;a;yes;no}", "yes", 1 },
		{ "%{if;a;~;b;yes;no}", "no", 1 },
		{ "%{if;a;~;.*a.*;yes;no}", "yes", 1 },
		{ "%{if;a;~;.*b.*;yes;no}", "no", 1 },
		{ "%{if;a;~;.*;yes;no}", "yes", 1 },
		{ "%{if;a;!~;a;yes;no}", "no", 1 },
		{ "%{if;a;!~;b;yes;no}", "yes", 1 },
		{ "%{if;a;!~;.*a.*;yes;no}", "no", 1 },
		{ "%{if;a;!~;.*b.*;yes;no}", "yes", 1 },
		{ "%{if;a;!~;.*;yes;no}", "no", 1 },
		{ "%{if;this is test;~;^test;yes;no}", "no", 1 },
		{ "%{if;this is test;~;.*test;yes;no}", "yes", 1 },
		/* variable expansion */
		{ "%{if;%a;eq;%a;yes;no}", "yes", 1 },
		{ "%{if;%a;eq;%b;yes;no}", "no", 1 },
		{ "%{if;%{alpha};eq;%{alpha};yes;no}", "yes", 1 },
		{ "%{if;%{alpha};eq;%{beta};yes;no}", "no", 1 },
		{ "%{if;%o;eq;%o;yes;no}", "yes", 1 },
		{ "%{if;%o;eq;%t;yes;no}", "no", 1 },
		{ "%{if;%{one};eq;%{one};yes;no}", "yes", 1 },
		{ "%{if;%{one};eq;%{two};yes;no}", "no", 1 },
		{ "%{if;%{one};eq;%{one};%{one};%{two}}", "1", 1 },
		{ "%{if;%{one};gt;%{two};%{one};%{two}}", "2", 1 },
		{ "%{if;%{evil1};eq;\\;\\:;%{evil2};no}", ";test;", 1 },
		/* inner if */
		{ "%{if;%{if;%{one};eq;1;1;0};eq;%{if;%{two};eq;2;2;3};yes;no}", "no", 1 },
		/* no false */
		{ "%{if;1;==;1;yes}", "yes", 1 },
		{ "%{if;1;==;2;yes}", "", 1 },
		/* invalid input */
		{ "%{if;}", "", -1 },
		{ "%{if;1;}", "", -1 },
		{ "%{if;1;==;}", "", -1 },
		{ "%{if;1;==;2;}", "", -1 },
		{ "%{if;1;fu;2;yes;no}", "", -1 },
		/* missing variables */
		{ "%{if;%{missing1};==;%{missing2};yes;no}", "", 0 },
	};

	for(size_t i = 0; i < N_ELEMENTS(tests); i++) {
		int ret;
		error = NULL;
		str_truncate(dest, 0);
		ret = var_expand(dest, tests[i].in, table, &error);
		test_assert_idx(tests[i].ret == ret, i);
		test_assert_idx(strcmp(tests[i].out, str_c(dest)) == 0, i);
	}

	test_end();
}

static void test_var_expand_merge_tables(void)
{
	const struct var_expand_table one[] = {
		{ 'a', "1", "alpha" },
		{ '\0', "2", "beta" },
		{ '\0', NULL, NULL }
	},
	two[] = {
		{ 't', "3", "theta" },
		{ '\0', "4", "phi" },
		{ '\0', NULL, NULL }
	},
	*merged = NULL;


	test_begin("var_expand_merge_tables");

	merged = t_var_expand_merge_tables(one, two);

	test_assert(var_expand_table_size(merged) == 4);
	for(unsigned int i = 0; i < var_expand_table_size(merged); i++) {
		if (i < 2) {
			test_assert_idx(merged[i].key == one[i].key, i);
			test_assert_idx(merged[i].value == one[i].value || strcmp(merged[i].value, one[i].value) == 0, i);
			test_assert_idx(merged[i].long_key == one[i].long_key || strcmp(merged[i].long_key, one[i].long_key) == 0, i);
		} else if (i < 4) {
			test_assert_idx(merged[i].key == two[i-2].key, i);
			test_assert_idx(merged[i].value == two[i-2].value || strcmp(merged[i].value, two[i-2].value) == 0, i);
			test_assert_idx(merged[i].long_key == two[i-2].long_key || strcmp(merged[i].long_key, two[i-2].long_key) == 0, i);
		} else {
			break;
		}
	}
	test_end();
}

void test_var_expand(void)
{
	test_var_expand_ranges();
	test_var_expand_builtin();
	test_var_get_key_range();
	test_var_expand_with_funcs();
	test_var_get_key();
	test_var_has_key();
	test_var_expand_extensions();
	test_var_expand_if();
	test_var_expand_merge_tables();
}