test-var-expand.c revision e539ff946adcbc68793adb6d9d84621dce3bf46b
1008N/A/* Copyright (c) 2009-2016 Dovecot authors, see the included COPYING file */
1008N/A
1008N/A#include "test-lib.h"
1008N/A#include "str.h"
1008N/A#include "env-util.h"
1008N/A#include "hostpid.h"
1008N/A#include "var-expand.h"
1008N/A
1008N/Astruct var_expand_test {
1008N/A const char *in;
1008N/A const char *out;
1008N/A};
1008N/A
1008N/Astruct var_get_key_range_test {
1008N/A const char *in;
1008N/A unsigned int idx, size;
1008N/A};
1008N/A
1008N/Astatic void test_var_expand_ranges(void)
1008N/A{
1008N/A static struct var_expand_test tests[] = {
1008N/A { "%v", "value1234" },
1008N/A { "%3v", "val" },
1008N/A { "%3.2v", "ue" },
4129N/A { "%3.-2v", "ue12" },
1008N/A { "%-3.2v", "23" },
1008N/A { "%0.-1v", "value123" },
1008N/A { "%-4.-1v", "123" }
1008N/A };
1008N/A static struct var_expand_table table[] = {
1008N/A { 'v', "value1234", NULL },
1008N/A { '\0', NULL, NULL }
1008N/A };
1008N/A string_t *str = t_str_new(128);
1008N/A unsigned int i;
1008N/A
1008N/A test_begin("var_expand - ranges");
1255N/A for (i = 0; i < N_ELEMENTS(tests); i++) {
1255N/A str_truncate(str, 0);
1255N/A var_expand(str, tests[i].in, table);
1008N/A test_assert(strcmp(tests[i].out, str_c(str)) == 0);
1008N/A }
1008N/A test_end();
1008N/A}
1008N/A
1008N/Astatic void test_var_expand_builtin(void)
4129N/A{
1008N/A static struct var_expand_test tests[] = {
1008N/A { "%{hostname}", NULL },
1008N/A { "%{pid}", NULL },
1008N/A { "a%{env:FOO}b", "abaRb" },
1008N/A { "%50Hv", "1f" },
1008N/A { "%50Hw", "2e" },
1008N/A { "%50Nv", "25" },
1008N/A { "%50Nw", "e" },
1008N/A
1008N/A { "%{nonexistent}", "UNSUPPORTED_VARIABLE_nonexistent" },
1389N/A { "%{nonexistent:default}", "UNSUPPORTED_VARIABLE_nonexistent" },
1389N/A };
1008N/A static struct var_expand_table table[] = {
1008N/A { 'v', "value", NULL },
1008N/A { 'w', "value2", NULL },
1389N/A { '\0', NULL, NULL }
1389N/A };
1389N/A string_t *str = t_str_new(128);
1389N/A unsigned int i;
1389N/A
1008N/A tests[0].out = my_hostname;
1929N/A tests[1].out = my_pid;
1929N/A env_put("FOO=baR");
1389N/A
1008N/A test_begin("var_expand - builtin");
1008N/A for (i = 0; i < N_ELEMENTS(tests); i++) {
1008N/A str_truncate(str, 0);
1008N/A var_expand(str, tests[i].in, table);
1008N/A test_assert_idx(strcmp(tests[i].out, str_c(str)) == 0, i);
1008N/A }
1008N/A test_end();
1389N/A}
1389N/A
1008N/Astatic void test_var_get_key_range(void)
1008N/A{
1008N/A static struct var_get_key_range_test tests[] = {
1008N/A { "", 0, 0 },
1008N/A { "{", 1, 0 },
1389N/A { "k", 0, 1 },
1389N/A { "{key}", 1, 3 },
1389N/A { "5.5Rk", 4, 1 },
1389N/A { "5.5R{key}", 5, 3 },
1389N/A { "{key", 1, 3 }
1008N/A };
1929N/A unsigned int i, idx, size;
1389N/A
1389N/A test_begin("var_get_key_range");
1008N/A for (i = 0; i < N_ELEMENTS(tests); i++) {
1008N/A var_get_key_range(tests[i].in, &idx, &size);
1008N/A test_assert_idx(tests[i].idx == idx, i);
1008N/A test_assert_idx(tests[i].size == size, i);
1008N/A
4129N/A if (tests[i].size == 1)
4129N/A test_assert_idx(tests[i].in[idx] == var_get_key(tests[i].in), i);
4129N/A }
4129N/A test_end();
4129N/A}
4129N/A
4129N/Astatic const char *test_var_expand_func1(const char *data, void *context)
4129N/A{
4129N/A test_assert(*(int *)context == 0xabcdef);
4129N/A return t_strdup_printf("<%s>", data);
4129N/A}
4129N/A
4129N/Astatic const char *test_var_expand_func2(const char *data ATTR_UNUSED,
4129N/A void *context ATTR_UNUSED)
4129N/A{
4129N/A return "";
4129N/A}
4129N/A
4129N/Astatic const char *test_var_expand_func3(const char *data ATTR_UNUSED,
4129N/A void *context ATTR_UNUSED)
4129N/A{
4129N/A return NULL;
4129N/A}
4129N/A
1008N/Astatic void test_var_expand_with_funcs(void)
1008N/A{
1008N/A static struct var_expand_test tests[] = {
1008N/A { "%{func1}", "<>" },
1008N/A { "%{func1:foo}", "<foo>" },
1008N/A { "%{func2}", "" },
1008N/A { "%{func3}", "" }
1008N/A };
1008N/A static struct var_expand_table table[] = {
1008N/A { '\0', NULL, NULL }
1389N/A };
1389N/A static const struct var_expand_func_table func_table[] = {
1389N/A { "func1", test_var_expand_func1 },
1389N/A { "func2", test_var_expand_func2 },
1008N/A { "func3", test_var_expand_func3 },
1389N/A { NULL, NULL }
1008N/A };
1389N/A string_t *str = t_str_new(128);
1008N/A unsigned int i;
1008N/A int ctx = 0xabcdef;
1008N/A
1008N/A test_begin("var_expand_with_funcs");
1008N/A for (i = 0; i < N_ELEMENTS(tests); i++) {
1008N/A str_truncate(str, 0);
1008N/A var_expand_with_funcs(str, tests[i].in, table, func_table, &ctx);
1255N/A test_assert_idx(strcmp(tests[i].out, str_c(str)) == 0, i);
1255N/A }
1255N/A test_end();
1255N/A}
1008N/A
4129N/Astatic void test_var_get_key(void)
1008N/A{
1008N/A static struct {
4129N/A const char *str;
1008N/A char key;
1008N/A } tests[] = {
1008N/A { "x", 'x' },
1008N/A { "2.5Mx", 'x' },
1008N/A { "200MDx", 'x' },
1008N/A { "200MD{foo}", '{' },
1008N/A { "{foo}", '{' },
1008N/A { "", '\0' },
1008N/A };
1008N/A
1008N/A test_begin("var_get_key");
1008N/A for (unsigned int i = 0; i < N_ELEMENTS(tests); i++)
1008N/A test_assert_idx(var_get_key(tests[i].str) == tests[i].key, i);
1008N/A test_end();
1008N/A}
1008N/A
1008N/Astatic void test_var_has_key(void)
1008N/A{
1008N/A static struct {
1008N/A const char *str;
1008N/A char key;
1008N/A const char *long_key;
1008N/A bool result;
1008N/A } tests[] = {
1008N/A { "%x%y", 'x', NULL, TRUE },
1008N/A { "%x%y", 'y', NULL, TRUE },
1008N/A { "%x%y", 'z', NULL, FALSE },
1008N/A { "%{foo}", 'f', NULL, FALSE },
1008N/A { "%{foo}", 'o', NULL, FALSE },
1008N/A { "%{foo}", '\0', "foo", TRUE },
1008N/A { "%{foo}", 'o', "foo", TRUE },
1008N/A { "%2.5Mx%y", 'x', NULL, TRUE },
1008N/A { "%2.5M{foo}", '\0', "foo", TRUE },
1255N/A };
1255N/A
1255N/A test_begin("var_has_key");
1255N/A for (unsigned int i = 0; i < N_ELEMENTS(tests); i++)
1008N/A test_assert_idx(var_has_key(tests[i].str, tests[i].key, tests[i].long_key) == tests[i].result, i);
4129N/A test_end();
1008N/A}
1008N/A
1008N/Avoid test_var_expand(void)
1008N/A{
1008N/A test_var_expand_ranges();
1008N/A test_var_expand_builtin();
1008N/A test_var_get_key_range();
1255N/A test_var_expand_with_funcs();
1255N/A test_var_get_key();
1255N/A test_var_has_key();
1255N/A}
1008N/A