bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct var_expand_test tests[] = {
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct var_expand_table table[] = {
1bc529e36d36f9a525c6af023ae014ebc6cdb440Timo Sirainen unsigned int i;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen test_assert(var_expand(str, tests[i].in, table, &error) == tests[i].ret);
1bc529e36d36f9a525c6af023ae014ebc6cdb440Timo Sirainen test_assert(strcmp(tests[i].out, str_c(str)) == 0);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen { "%{nonexistent}", "UNSUPPORTED_VARIABLE_nonexistent", 0 },
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen { "%{nonexistent:default}", "UNSUPPORTED_VARIABLE_nonexistent", 0 },
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct var_expand_table table[] = {
927d3977d5598f12ae18d4fa3f22b9e913f7dd46Timo Sirainen unsigned int i;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen test_assert_idx(var_expand(str, tests[i].in, table, &error) == tests[i].ret, i);
552a9b01e99cb9201de5ea5591df293d3b7495e0Phil Carmody test_assert_idx(strcmp(tests[i].out, str_c(str)) == 0, i);
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct var_get_key_range_test tests[] = {
b58cb4b59ccd78ee1c3e0df0bc13c300d1bec380Timo Sirainen { "", 0, 0 },
66bdf53dbb272c55bd2c5c4d3980f34296f1a95fAki Tuomi { "{if;%{if;%{value};eq;value;t;f};eq;t;t;f}", 1, 39 },
552a9b01e99cb9201de5ea5591df293d3b7495e0Phil Carmody test_assert_idx(tests[i].in[idx] == var_get_key(tests[i].in), i);
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainenstatic int test_var_expand_func1(const char *data, void *context,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen const char **value_r,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainenstatic int test_var_expand_func2(const char *data ATTR_UNUSED,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen const char **value_r,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainenstatic int test_var_expand_func3(const char *data ATTR_UNUSED,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen const char **value_r,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainenstatic int test_var_expand_func4(const char *data,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen const char **error_r)
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen *error_r = t_strdup_printf("Unknown data %s", data == NULL ? "" : data);
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainenstatic int test_var_expand_func5(const char *data ATTR_UNUSED,
bcf1cf2afb9692b0db555e6ecf662a2fbd19793dTimo Sirainen const char **error_r)
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct var_expand_test tests[] = {
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct var_expand_table table[] = {
654b46078129456bda90c5eb18014fb2858c302eTimo Sirainen static const struct var_expand_func_table func_table[] = {
654b46078129456bda90c5eb18014fb2858c302eTimo Sirainen unsigned int i;
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen test_assert_idx(var_expand_with_funcs(str, tests[i].in, table, func_table, &ctx, &error) == tests[i].ret, i);
654b46078129456bda90c5eb18014fb2858c302eTimo Sirainen test_assert_idx(strcmp(tests[i].out, str_c(str)) == 0, i);
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
7a556ac46f1f51ee29bc8aa92b5ffe05b004a8b8Timo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++)
7a556ac46f1f51ee29bc8aa92b5ffe05b004a8b8Timo Sirainen test_assert_idx(var_get_key(tests[i].str) == tests[i].key, i);
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
e539ff946adcbc68793adb6d9d84621dce3bf46bTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++)
e539ff946adcbc68793adb6d9d84621dce3bf46bTimo Sirainen test_assert_idx(var_has_key(tests[i].str, tests[i].key, tests[i].long_key) == tests[i].result, i);
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomistatic int test_var_expand_hashing_func1(const char *data,
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi const char **value_r,
b8596c89dd8b6628e0fa52e52f2a95e869968706Aki Tuomistatic int test_var_expand_bad_func(struct var_expand_context *ctx ATTR_UNUSED,
b8596c89dd8b6628e0fa52e52f2a95e869968706Aki Tuomi const char *key,
b8596c89dd8b6628e0fa52e52f2a95e869968706Aki Tuomi const char **error_r)
b8596c89dd8b6628e0fa52e52f2a95e869968706Aki Tuomistatic const struct var_expand_extension_func_table test_extension_funcs[] = {
b8596c89dd8b6628e0fa52e52f2a95e869968706Aki Tuomi var_expand_register_func_array(test_extension_funcs);
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct var_expand_table table[] = {
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainen static const struct {
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi { "md5: %M{value} %{md5:value}", "md5: 1a79a4d60de6718e8e5b326e338ae533 1a79a4d60de6718e8e5b326e338ae533" },
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi { "sha1: %{sha1:value}", "sha1: c3499c2729730a7f807efb8676a92dcb6f8a3f8f" },
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi { "sha1: %{sha1:func1:example}", "sha1: c3499c2729730a7f807efb8676a92dcb6f8a3f8f" },
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi { "truncate: %{sha1;truncate=12:value}", "truncate: 0c34" },
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi { "truncate: %{sha1;truncate=16:value}", "truncate: c349" },
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi { "rounds,salt: %{sha1;rounds=1000,salt=seawater:value}", "rounds,salt: b515c85884f6b82dc7588279f3643a73e55d2289" },
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi { "rounds,salt,expand: %{sha1;rounds=1000,salt=%{other-value}:value} %{other-value}", "rounds,salt,expand: 49a598ee110af615e175f2e4511cc5d7ccff96ab other-example" },
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi { "format: %4.8{sha1:value}", "format: 9c272973" },
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi { "base64: %{sha1;format=base64:value}", "base64: w0mcJylzCn+AfvuGdqkty2+KP48=" },
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi static const struct var_expand_func_table func_table[] = {
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi test_assert(var_expand_with_funcs(str, tests[i].in, table,
14c903cfef0227c750e66298df56c3bf8ef258a1Aki Tuomi test_assert_idx(strcmp(str_c(str), tests[i].out) == 0, i);
b8596c89dd8b6628e0fa52e52f2a95e869968706Aki Tuomi test_assert(var_expand_with_funcs(str, "notfound: %{notfound:field}",
b8596c89dd8b6628e0fa52e52f2a95e869968706Aki Tuomi test_assert(var_expand_with_funcs(str, "notfound: %{badparam:field}",
b8596c89dd8b6628e0fa52e52f2a95e869968706Aki Tuomi var_expand_unregister_func_array(test_extension_funcs);
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi /* basic numeric operand test */
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi /* basic string operand test */
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi { "%{if;this is test;~;.*test;yes;no}", "yes", 1 },
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi /* variable expansion */
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi { "%{if;%{alpha};eq;%{alpha};yes;no}", "yes", 1 },
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi { "%{if;%{one};eq;%{one};%{one};%{two}}", "1", 1 },
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi { "%{if;%{one};gt;%{two};%{one};%{two}}", "2", 1 },
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi { "%{if;%{evil1};eq;\\;\\:;%{evil2};no}", ";test;", 1 },
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi /* inner if */
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi { "%{if;%{if;%{one};eq;1;1;0};eq;%{if;%{two};eq;2;2;3};yes;no}", "no", 1 },
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi /* no false */
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi /* invalid input */
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi /* missing variables */
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi { "%{if;%{missing1};==;%{missing2};yes;no}", "", 0 },
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi ret = var_expand(dest, tests[i].in, table, &error);
57479bd6600f51e0c4850969f6587cae40f56782Aki Tuomi test_assert_idx(strcmp(tests[i].out, str_c(dest)) == 0, i);
857a9e1f1cf3a2241403bfb201421bb1a66038b1Aki Tuomi for(unsigned int i = 0; i < var_expand_table_size(merged); i++) {
857a9e1f1cf3a2241403bfb201421bb1a66038b1Aki Tuomi if (i < 2) {
857a9e1f1cf3a2241403bfb201421bb1a66038b1Aki Tuomi test_assert_idx(merged[i].value == one[i].value || strcmp(merged[i].value, one[i].value) == 0, i);
857a9e1f1cf3a2241403bfb201421bb1a66038b1Aki Tuomi test_assert_idx(merged[i].long_key == one[i].long_key || strcmp(merged[i].long_key, one[i].long_key) == 0, i);
857a9e1f1cf3a2241403bfb201421bb1a66038b1Aki Tuomi } else if (i < 4) {
857a9e1f1cf3a2241403bfb201421bb1a66038b1Aki Tuomi test_assert_idx(merged[i].key == two[i-2].key, i);
857a9e1f1cf3a2241403bfb201421bb1a66038b1Aki Tuomi test_assert_idx(merged[i].value == two[i-2].value || strcmp(merged[i].value, two[i-2].value) == 0, i);