2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2009, Digium, Inc.
6 * Tilghman Lesher <tlesher AT digium DOT com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Substitution Test
23 * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
29 <depend>TEST_FRAMEWORK</depend>
30 <depend>func_curl</depend>
31 <support_level>core</support_level>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/file.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/app.h"
44 #include "asterisk/strings.h"
45 #include "asterisk/stringfields.h"
46 #include "asterisk/threadstorage.h"
47 #include "asterisk/test.h"
49 static enum ast_test_result_state test_chan_integer(struct ast_test *test,
50 struct ast_channel *c, int *ifield, const char *expression)
52 int i, okay = 1, value1 = -1, value2 = -1;
54 struct ast_str *str = ast_str_create(16);
56 ast_test_status_update(test, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
57 for (i = 0; i < 256; i++) {
59 ast_str_substitute_variables(&str, 0, c, expression);
60 pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
61 if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) {
62 ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i);
69 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
72 static enum ast_test_result_state test_chan_integer_accessor(struct ast_test *test,
73 struct ast_channel *c, void (*setter)(struct ast_channel *, int),const char *expression)
75 int i, okay = 1, value1 = -1, value2 = -1;
77 struct ast_str *str = ast_str_create(16);
79 ast_test_status_update(test, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
80 for (i = 0; i < 256; i++) {
82 ast_str_substitute_variables(&str, 0, c, expression);
83 pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
84 if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) {
85 ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i);
92 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
95 static enum ast_test_result_state test_chan_string(struct ast_test *test,
96 struct ast_channel *c, void (*setter)(struct ast_channel *, const char *),
97 const char *(*getter)(const struct ast_channel *), const char *expression)
99 const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
101 char workspace[4096];
102 struct ast_str *str = ast_str_create(16);
104 for (i = 0; i < ARRAY_LEN(values); i++) {
105 setter(c, values[i]);
106 ast_str_substitute_variables(&str, 0, c, expression);
107 pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
108 ast_test_status_update(test, "Testing '%s' . . . . . %s\n",
109 expression, okay ? "passed" : "FAILED");
110 if (strcmp(getter(c), ast_str_buffer(str)) != 0 || strcmp(getter(c), workspace) != 0) {
111 ast_test_status_update(test, "%s != %s != %s\n", getter(c), ast_str_buffer(str), workspace);
118 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
121 static enum ast_test_result_state test_chan_variable(struct ast_test *test,
122 struct ast_channel *c, const char *varname)
124 const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
126 char workspace[4096];
127 struct ast_str *str = ast_str_create(16);
128 struct ast_str *var = ast_str_create(16);
130 ast_str_set(&var, 0, "${%s}", varname);
131 for (i = 0; i < ARRAY_LEN(values); i++) {
132 pbx_builtin_setvar_helper(c, varname, values[i]);
133 ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var));
134 pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace));
135 ast_test_status_update(test, "Testing '%s' . . . . . %s\n",
136 ast_str_buffer(var), okay ? "passed" : "FAILED");
137 if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) {
138 ast_test_status_update(test, "%s != %s != %s\n",
139 values[i], ast_str_buffer(str), workspace);
147 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
150 static enum ast_test_result_state test_chan_function(struct ast_test *test,
151 struct ast_channel *c, const char *expression)
154 char workspace[4096];
155 struct ast_str *str = ast_str_create(16);
157 ast_str_substitute_variables(&str, 0, c, expression);
158 pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
159 ast_test_status_update(test, "Testing '%s' . . . . . %s\n",
160 expression, okay ? "passed" : "FAILED");
161 if (strcmp(workspace, ast_str_buffer(str)) != 0) {
162 ast_test_status_update(test, "test_chan_function, expr: '%s' ... %s != %s\n",
163 expression, ast_str_buffer(str), workspace);
169 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
172 static enum ast_test_result_state test_2way_function(struct ast_test *test,
173 struct ast_channel *c, const char *encode1, const char *encode2,
174 const char *decode1, const char *decode2)
176 struct ast_str *str = ast_str_create(16), *expression = ast_str_alloca(120);
179 ast_str_set(&expression, 0, "%s%s%s", encode1, "foobarbaz", encode2);
180 ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression));
181 ast_str_set(&expression, 0, "%s%s%s", decode1, ast_str_buffer(str), decode2);
182 ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression));
184 okay = !strcmp(ast_str_buffer(str), "foobarbaz");
186 ast_test_status_update(test, "Testing '%s%s' and '%s%s' . . . . . %s\n",
187 encode1, encode2, decode1, decode2,
188 okay ? "passed" : "FAILED");
191 ast_test_status_update(test, " '%s' != 'foobarbaz'\n",
192 ast_str_buffer(str));
197 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
200 static enum ast_test_result_state test_expected_result(struct ast_test *test,
201 struct ast_channel *c, const char *expression, const char *result)
203 struct ast_str *str = ast_str_create(16);
206 ast_str_substitute_variables(&str, 0, c, expression);
207 okay = !strcmp(ast_str_buffer(str), result);
209 ast_test_status_update(test, "Testing '%s' ('%s') == '%s' . . . . . %s\n",
210 ast_str_buffer(str), expression, result,
211 okay ? "passed" : "FAILED");
214 ast_test_status_update(test, "test_expected_result: '%s' != '%s'\n",
215 ast_str_buffer(str), result);
220 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
223 AST_TEST_DEFINE(test_substitution)
225 struct ast_channel *c;
227 enum ast_test_result_state res = AST_TEST_PASS;
231 info->name = "test_substitution";
232 info->category = "/main/pbx/";
233 info->summary = "Test variable and function substitution";
235 "This test executes a variety of variable and function substitutions "
236 "and ensures that the expected results are received.";
237 return AST_TEST_NOT_RUN;
242 ast_test_status_update(test, "Testing variable substitution ...\n");
244 c = ast_channel_alloc(0, 0, "", "", "", "", "", "", 0, "Test/substitution");
246 #define TEST(t) if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; }
249 * We can no longer test the CALLINGPRES value this way because it is now
250 * a calculated value from the name and number presentation information to
251 * get a combined presentation value.
253 TEST(test_chan_integer(test, c, &c->caller.id.number.presentation, "${CALLINGPRES}"));
255 TEST(test_chan_integer(test, c, &ast_channel_caller(c)->ani2, "${CALLINGANI2}"));
256 TEST(test_chan_integer(test, c, &ast_channel_caller(c)->id.number.plan, "${CALLINGTON}"));
257 TEST(test_chan_integer(test, c, &ast_channel_dialed(c)->transit_network_select, "${CALLINGTNS}"));
258 TEST(test_chan_integer_accessor(test, c, ast_channel_hangupcause_set, "${HANGUPCAUSE}"));
259 TEST(test_chan_integer_accessor(test, c, ast_channel_priority_set, "${PRIORITY}"));
260 TEST(test_chan_string(test, c, ast_channel_context_set, ast_channel_context, "${CONTEXT}"));
261 TEST(test_chan_string(test, c, ast_channel_exten_set, ast_channel_exten, "${EXTEN}"));
262 TEST(test_chan_variable(test, c, "CHANNEL(language)"));
263 TEST(test_chan_variable(test, c, "CHANNEL(musicclass)"));
264 TEST(test_chan_variable(test, c, "CHANNEL(parkinglot)"));
265 TEST(test_chan_variable(test, c, "CALLERID(name)"));
266 TEST(test_chan_variable(test, c, "CURLOPT(proxyuserpwd)"));
267 TEST(test_chan_variable(test, c, "CDR(foo)"));
268 TEST(test_chan_variable(test, c, "ENV(foo)"));
269 TEST(test_chan_variable(test, c, "GLOBAL(foo)"));
270 TEST(test_chan_variable(test, c, "GROUP()"));
271 TEST(test_2way_function(test, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}"));
272 TEST(test_2way_function(test, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}"));
273 pbx_builtin_setvar_helper(c, "foo", "123");
274 pbx_builtin_setvar_helper(c, "bar", "foo");
275 pbx_builtin_setvar_helper(c, "baz", "fo");
276 TEST(test_expected_result(test, c, "${foo}${foo}", "123123"));
277 TEST(test_expected_result(test, c, "A${foo}A${foo}A", "A123A123A"));
278 TEST(test_expected_result(test, c, "A${${bar}}A", "A123A"));
279 TEST(test_expected_result(test, c, "A${${baz}o}A", "A123A"));
280 TEST(test_expected_result(test, c, "A${${baz}o:1}A", "A23A"));
281 TEST(test_expected_result(test, c, "A${${baz}o:1:1}A", "A2A"));
282 TEST(test_expected_result(test, c, "A${${baz}o:1:-1}A", "A2A"));
283 TEST(test_expected_result(test, c, "A${${baz}o:-1:1}A", "A3A"));
284 TEST(test_expected_result(test, c, "A${${baz}o:-2:1}A", "A2A"));
285 TEST(test_expected_result(test, c, "A${${baz}o:-2:-1}A", "A2A"));
286 pbx_builtin_setvar_helper(c, "list1", "ab&cd&ef");
287 TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,cd)}", "ab&ef"));
288 TEST(test_expected_result(test, c, "${SHELL(printf '%d' 123)},${SHELL(printf '%d' 456)}", "123,456"));
289 TEST(test_expected_result(test, c, "${foo},${CDR(answer)},${SHELL(printf '%d' 456)}", "123,0.000000,456"));
290 TEST(test_expected_result(test, c, "${foo},${this_does_not_exist},${THIS_DOES_NOT_EXIST(either)}", "123,,"));
293 /* For testing dialplan functions */
295 char *cmd = ast_cli_generator("core show function", "", i);
299 if (strcmp(cmd, "CHANNEL") && strcmp(cmd, "CALLERID") && strncmp(cmd, "CURL", 4) &&
300 strncmp(cmd, "AES", 3) && strncmp(cmd, "BASE64", 6) &&
301 strcmp(cmd, "CDR") && strcmp(cmd, "ENV") && strcmp(cmd, "GLOBAL") &&
302 strcmp(cmd, "GROUP") && strcmp(cmd, "CUT") && strcmp(cmd, "LISTFILTER") &&
303 strcmp(cmd, "PP_EACH_EXTENSION") && strcmp(cmd, "SET")) {
304 struct ast_custom_function *acf = ast_custom_function_find(cmd);
305 if (acf->read && acf->read2) {
307 snprintf(expression, sizeof(expression), "${%s(foo)}", cmd);
308 if (AST_TEST_FAIL == test_chan_function(test, c, expression)) {
320 static int unload_module(void)
322 AST_TEST_UNREGISTER(test_substitution);
326 static int load_module(void)
328 AST_TEST_REGISTER(test_substitution);
329 return AST_MODULE_LOAD_SUCCESS;
332 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Substitution tests");