bridges/bridge_t38: Add a bridging module for managing T.38 state
[asterisk/asterisk.git] / tests / test_substitution.c
index 6fdcfa1..c22f5e6 100644 (file)
 
 /*** MODULEINFO
        <depend>TEST_FRAMEWORK</depend>
+       <depend>func_curl</depend>
+       <support_level>core</support_level>
  ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_REGISTER_FILE()
 
 #include "asterisk/file.h"
 #include "asterisk/channel.h"
@@ -44,35 +46,57 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/threadstorage.h"
 #include "asterisk/test.h"
 
-static enum ast_test_result_state test_chan_integer(void *status, struct ast_str **err,
+static enum ast_test_result_state test_chan_integer(struct ast_test *test,
                struct ast_channel *c, int *ifield, const char *expression)
 {
        int i, okay = 1, value1 = -1, value2 = -1;
        char workspace[4096];
        struct ast_str *str = ast_str_create(16);
 
-       ast_test_status_update(status, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
        for (i = 0; i < 256; i++) {
                *ifield = i;
                ast_str_substitute_variables(&str, 0, c, expression);
                pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
+
                if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) {
-                       ast_test_status_update(status, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i);
-                       ast_str_set(err, 0, "%s: '%s' != '%s' and/or %d != %d != %d\n",
-                                       __PRETTY_FUNCTION__, ast_str_buffer(str),
-                                       workspace, value1, value2, i);
+                       ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i);
                        okay = 0;
                }
        }
+       ast_test_status_update(test, "Tested '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
 
        ast_free(str);
 
        return okay ? AST_TEST_PASS : AST_TEST_FAIL;
 }
 
-static enum ast_test_result_state test_chan_string(void *status, struct ast_str **err,
-               struct ast_channel *c, char *cfield, size_t cfieldsize,
-               const char *expression)
+static enum ast_test_result_state test_chan_integer_accessor(struct ast_test *test,
+               struct ast_channel *c, void (*setter)(struct ast_channel *, int),const char *expression)
+{
+       int i, okay = 1, value1 = -1, value2 = -1;
+       char workspace[4096];
+       struct ast_str *str = ast_str_create(16);
+
+       for (i = 0; i < 256; i++) {
+               setter(c, i);
+               ast_str_substitute_variables(&str, 0, c, expression);
+               pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
+
+               if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) {
+                       ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i);
+                       okay = 0;
+               }
+       }
+       ast_test_status_update(test, "Tested '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
+
+       ast_free(str);
+
+       return okay ? AST_TEST_PASS : AST_TEST_FAIL;
+}
+
+static enum ast_test_result_state test_chan_string(struct ast_test *test,
+               struct ast_channel *c, void (*setter)(struct ast_channel *, const char *),
+               const char *(*getter)(const struct ast_channel *), const char *expression)
 {
        const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
        int i, okay = 1;
@@ -80,24 +104,24 @@ static enum ast_test_result_state test_chan_string(void *status, struct ast_str
        struct ast_str *str = ast_str_create(16);
 
        for (i = 0; i < ARRAY_LEN(values); i++) {
-               ast_copy_string(cfield, values[i], cfieldsize);
+               setter(c, values[i]);
                ast_str_substitute_variables(&str, 0, c, expression);
                pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
-               ast_test_status_update(status, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
-               if (strcmp(cfield, ast_str_buffer(str)) != 0 || strcmp(cfield, workspace) != 0) {
-                       ast_test_status_update(status, "%s != %s != %s\n", cfield, ast_str_buffer(str), workspace);
-                       ast_str_set(err, 0, "%s: '%s' != '%s' != '%s'\n",
-                                       __PRETTY_FUNCTION__, cfield, ast_str_buffer(str), workspace);
+
+               if (strcmp(getter(c), ast_str_buffer(str)) != 0 || strcmp(getter(c), workspace) != 0) {
+                       ast_test_status_update(test, "%s != %s != %s\n", getter(c), ast_str_buffer(str), workspace);
                        okay = 0;
                }
        }
+       ast_test_status_update(test, "Tested '%s' . . . . . %s\n",
+               expression, okay ? "passed" : "FAILED");
 
        ast_free(str);
 
        return okay ? AST_TEST_PASS : AST_TEST_FAIL;
 }
 
-static enum ast_test_result_state test_chan_variable(void *status, struct ast_str **err,
+static enum ast_test_result_state test_chan_variable(struct ast_test *test,
                struct ast_channel *c, const char *varname)
 {
        const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
@@ -111,14 +135,15 @@ static enum ast_test_result_state test_chan_variable(void *status, struct ast_st
                pbx_builtin_setvar_helper(c, varname, values[i]);
                ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var));
                pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace));
-               ast_test_status_update(status, "Testing '%s' . . . . . %s\n", ast_str_buffer(var), okay ? "passed" : "FAILED");
+
                if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) {
-                       ast_test_status_update(status, "%s != %s != %s\n", values[i], ast_str_buffer(str), workspace);
-                       ast_str_set(err, 0, "%s: Variable: '%s' ... '%s' != '%s' != '%s'\n", varname,
-                                       __PRETTY_FUNCTION__, values[i], ast_str_buffer(str), workspace);
+                       ast_test_status_update(test, "%s != %s != %s\n",
+                                       values[i], ast_str_buffer(str), workspace);
                        okay = 0;
                }
        }
+       ast_test_status_update(test, "Tested '%s' . . . . . %s\n",
+               ast_str_buffer(var), okay ? "passed" : "FAILED");
 
        ast_free(str);
        ast_free(var);
@@ -126,7 +151,7 @@ static enum ast_test_result_state test_chan_variable(void *status, struct ast_st
        return okay ? AST_TEST_PASS : AST_TEST_FAIL;
 }
 
-static enum ast_test_result_state test_chan_function(void *status, struct ast_str **err,
+static enum ast_test_result_state test_chan_function(struct ast_test *test,
                struct ast_channel *c, const char *expression)
 {
        int okay = 1;
@@ -135,21 +160,21 @@ static enum ast_test_result_state test_chan_function(void *status, struct ast_st
 
        ast_str_substitute_variables(&str, 0, c, expression);
        pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
-       ast_test_status_update(status, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
+
        if (strcmp(workspace, ast_str_buffer(str)) != 0) {
-               ast_test_status_update(status, "test_chan_function, expr: '%s' ... %s != %s\n",
+               ast_test_status_update(test, "expr: '%s' ... %s != %s\n",
                                expression, ast_str_buffer(str), workspace);
-               ast_str_set(err, 0, "%s: expr: '%s' ... '%s' != '%s'\n",
-                               __PRETTY_FUNCTION__, expression, ast_str_buffer(str), workspace);
                okay = 0;
        }
+       ast_test_status_update(test, "Tested '%s' . . . . . %s\n",
+               expression, okay ? "passed" : "FAILED");
 
        ast_free(str);
 
        return okay ? AST_TEST_PASS : AST_TEST_FAIL;
 }
 
-static enum ast_test_result_state test_2way_function(void *status, struct ast_str **err,
+static enum ast_test_result_state test_2way_function(struct ast_test *test,
                struct ast_channel *c, const char *encode1, const char *encode2,
                const char *decode1, const char *decode2)
 {
@@ -162,41 +187,35 @@ static enum ast_test_result_state test_2way_function(void *status, struct ast_st
        ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression));
 
        okay = !strcmp(ast_str_buffer(str), "foobarbaz");
-
-       ast_test_status_update(status, "Testing '%s%s' and '%s%s' . . . . . %s\n",
-                       encode1, encode2, decode1, decode2,
-                       okay ? "passed" : "FAILED");
-
        if (!okay) {
-               ast_test_status_update(status, "  '%s' != 'foobarbaz'\n", ast_str_buffer(str));
-               ast_str_set(err, 0, "%s: '%s' != 'foobarbaz'\n",
-                               __PRETTY_FUNCTION__, ast_str_buffer(str));
+               ast_test_status_update(test, "'%s' != 'foobarbaz'\n",
+                               ast_str_buffer(str));
        }
+       ast_test_status_update(test, "Tested '%s%s' and '%s%s' . . . . . %s\n",
+               encode1, encode2, decode1, decode2,
+               okay ? "passed" : "FAILED");
 
        ast_free(str);
 
        return okay ? AST_TEST_PASS : AST_TEST_FAIL;
 }
 
-static enum ast_test_result_state test_expected_result(void *status, struct ast_str **err,
+static enum ast_test_result_state test_expected_result(struct ast_test *test,
                struct ast_channel *c, const char *expression, const char *result)
 {
        struct ast_str *str = ast_str_create(16);
        int okay;
 
        ast_str_substitute_variables(&str, 0, c, expression);
-       okay = !strcmp(ast_str_buffer(str), result);
-
-       ast_test_status_update(status, "Testing '%s' ('%s') == '%s' . . . . . %s\n",
-                       ast_str_buffer(str), expression, result,
-                       okay ? "passed" : "FAILED");
 
+       okay = !strcmp(ast_str_buffer(str), result);
        if (!okay) {
-               ast_test_status_update(status, "test_expected_result: '%s' != '%s'\n",
-                               ast_str_buffer(str), result);
-               ast_str_set(err, 0, "%s: '%s' != '%s'\n", __PRETTY_FUNCTION__,
+               ast_test_status_update(test, "'%s' != '%s'\n",
                                ast_str_buffer(str), result);
        }
+       ast_test_status_update(test, "Tested '%s' ('%s') == '%s' . . . . . %s\n",
+               ast_str_buffer(str), expression, result,
+               okay ? "passed" : "FAILED");
 
        ast_free(str);
 
@@ -212,7 +231,7 @@ AST_TEST_DEFINE(test_substitution)
        switch (cmd) {
        case TEST_INIT:
                info->name = "test_substitution";
-               info->category = "main/pbx/";
+               info->category = "/main/pbx/";
                info->summary = "Test variable and function substitution";
                info->description =
                        "This test executes a variety of variable and function substitutions "
@@ -222,43 +241,57 @@ AST_TEST_DEFINE(test_substitution)
                break;
        }
 
-       ast_test_status_update(&args->status_update, "Testing variable substitution ...\n");
+       ast_test_status_update(test, "Testing variable substitution ...\n");
 
-       c = ast_channel_alloc(0, 0, "", "", "", "", "", "", 0, "Test/substitution");
+       c = ast_channel_alloc(0, 0, "", "", "", "", "", NULL, NULL, 0, "Test/substitution");
+       ast_channel_unlock(c);
 
 #define TEST(t) if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; }
-       TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_pres, "${CALLINGPRES}"));
-       TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_ani2, "${CALLINGANI2}"));
-       TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_ton, "${CALLINGTON}"));
-       TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_tns, "${CALLINGTNS}"));
-       TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->hangupcause, "${HANGUPCAUSE}"));
-       TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->priority, "${PRIORITY}"));
-       TEST(test_chan_string(&args->status_update, &args->ast_test_error_str, c, c->context, sizeof(c->context), "${CONTEXT}"));
-       TEST(test_chan_string(&args->status_update, &args->ast_test_error_str, c, c->exten, sizeof(c->exten), "${EXTEN}"));
-       TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CHANNEL(language)"));
-       TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CHANNEL(musicclass)"));
-       TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CHANNEL(parkinglot)"));
-       TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CALLERID(name)"));
-       TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CURLOPT(proxyuserpwd)"));
-       TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CDR(foo)"));
-       TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "ENV(foo)"));
-       TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "GLOBAL(foo)"));
-       TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "GROUP()"));
-       TEST(test_2way_function(&args->status_update, &args->ast_test_error_str, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}"));
-       TEST(test_2way_function(&args->status_update, &args->ast_test_error_str, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}"));
+#if 0
+       /*
+        * We can no longer test the CALLINGPRES value this way because it is now
+        * a calculated value from the name and number presentation information to
+        * get a combined presentation value.
+        */
+       TEST(test_chan_integer(test, c, &c->caller.id.number.presentation, "${CALLINGPRES}"));
+#endif
+       TEST(test_chan_integer(test, c, &ast_channel_caller(c)->ani2, "${CALLINGANI2}"));
+       TEST(test_chan_integer(test, c, &ast_channel_caller(c)->id.number.plan, "${CALLINGTON}"));
+       TEST(test_chan_integer(test, c, &ast_channel_dialed(c)->transit_network_select, "${CALLINGTNS}"));
+       TEST(test_chan_integer_accessor(test, c, ast_channel_hangupcause_set, "${HANGUPCAUSE}"));
+       TEST(test_chan_integer_accessor(test, c, ast_channel_priority_set, "${PRIORITY}"));
+       TEST(test_chan_string(test, c, ast_channel_context_set, ast_channel_context, "${CONTEXT}"));
+       TEST(test_chan_string(test, c, ast_channel_exten_set, ast_channel_exten, "${EXTEN}"));
+       TEST(test_chan_variable(test, c, "CHANNEL(language)"));
+       TEST(test_chan_variable(test, c, "CHANNEL(musicclass)"));
+       TEST(test_chan_variable(test, c, "CHANNEL(parkinglot)"));
+       TEST(test_chan_variable(test, c, "CALLERID(name)"));
+       TEST(test_chan_variable(test, c, "CURLOPT(proxyuserpwd)"));
+       TEST(test_chan_variable(test, c, "CDR(foo)"));
+       TEST(test_chan_variable(test, c, "ENV(foo)"));
+       TEST(test_chan_variable(test, c, "GLOBAL(foo)"));
+       TEST(test_chan_variable(test, c, "GROUP()"));
+       TEST(test_2way_function(test, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}"));
+       TEST(test_2way_function(test, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}"));
        pbx_builtin_setvar_helper(c, "foo", "123");
        pbx_builtin_setvar_helper(c, "bar", "foo");
        pbx_builtin_setvar_helper(c, "baz", "fo");
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "${foo}${foo}", "123123"));
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${foo}A${foo}A", "A123A123A"));
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${bar}}A", "A123A"));
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o}A", "A123A"));
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:1}A", "A23A"));
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:1:1}A", "A2A"));
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:1:-1}A", "A2A"));
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:-1:1}A", "A3A"));
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:-2:1}A", "A2A"));
-       TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:-2:-1}A", "A2A"));
+       TEST(test_expected_result(test, c, "${foo}${foo}", "123123"));
+       TEST(test_expected_result(test, c, "A${foo}A${foo}A", "A123A123A"));
+       TEST(test_expected_result(test, c, "A${${bar}}A", "A123A"));
+       TEST(test_expected_result(test, c, "A${${baz}o}A", "A123A"));
+       TEST(test_expected_result(test, c, "A${${baz}o:1}A", "A23A"));
+       TEST(test_expected_result(test, c, "A${${baz}o:1:1}A", "A2A"));
+       TEST(test_expected_result(test, c, "A${${baz}o:1:-1}A", "A2A"));
+       TEST(test_expected_result(test, c, "A${${baz}o:-1:1}A", "A3A"));
+       TEST(test_expected_result(test, c, "A${${baz}o:-2:1}A", "A2A"));
+       TEST(test_expected_result(test, c, "A${${baz}o:-2:-1}A", "A2A"));
+       pbx_builtin_setvar_helper(c, "list1", "ab&cd&ef");
+       TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,cd)}", "ab&ef"));
+       TEST(test_expected_result(test, c, "${SHELL(printf '%d' 123)},${SHELL(printf '%d' 456)}", "123,456"));
+       TEST(test_expected_result(test, c, "${foo},${CDR(answer)},${SHELL(printf '%d' 456)}", "123,,456"));
+       TEST(test_expected_result(test, c, "${foo},${CDR(answer,u)},${SHELL(printf '%d' 456)}", "123,0.000000,456"));
+       TEST(test_expected_result(test, c, "${foo},${this_does_not_exist},${THIS_DOES_NOT_EXIST(either)}", "123,,"));
 #undef TEST
 
        /* For testing dialplan functions */
@@ -276,7 +309,7 @@ AST_TEST_DEFINE(test_substitution)
                        if (acf->read && acf->read2) {
                                char expression[80];
                                snprintf(expression, sizeof(expression), "${%s(foo)}", cmd);
-                               if (AST_TEST_FAIL == test_chan_function(&args->status_update, &args->ast_test_error_str,c, expression)) {
+                               if (AST_TEST_FAIL == test_chan_function(test, c, expression)) {
                                        res = AST_TEST_FAIL;
                                }
                        }