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 for (i = 0; i < 256; i++) {
58 ast_str_substitute_variables(&str, 0, c, expression);
59 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);
66 ast_test_status_update(test, "Tested '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
70 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
73 static enum ast_test_result_state test_chan_integer_accessor(struct ast_test *test,
74 struct ast_channel *c, void (*setter)(struct ast_channel *, int),const char *expression)
76 int i, okay = 1, value1 = -1, value2 = -1;
78 struct ast_str *str = ast_str_create(16);
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));
85 if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) {
86 ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i);
90 ast_test_status_update(test, "Tested '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
94 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
97 static enum ast_test_result_state test_chan_string(struct ast_test *test,
98 struct ast_channel *c, void (*setter)(struct ast_channel *, const char *),
99 const char *(*getter)(const struct ast_channel *), const char *expression)
101 const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
103 char workspace[4096];
104 struct ast_str *str = ast_str_create(16);
106 for (i = 0; i < ARRAY_LEN(values); i++) {
107 setter(c, values[i]);
108 ast_str_substitute_variables(&str, 0, c, expression);
109 pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
111 if (strcmp(getter(c), ast_str_buffer(str)) != 0 || strcmp(getter(c), workspace) != 0) {
112 ast_test_status_update(test, "%s != %s != %s\n", getter(c), ast_str_buffer(str), workspace);
116 ast_test_status_update(test, "Tested '%s' . . . . . %s\n",
117 expression, okay ? "passed" : "FAILED");
121 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
124 static enum ast_test_result_state test_chan_variable(struct ast_test *test,
125 struct ast_channel *c, const char *varname)
127 const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
129 char workspace[4096];
130 struct ast_str *str = ast_str_create(16);
131 struct ast_str *var = ast_str_create(16);
133 ast_str_set(&var, 0, "${%s}", varname);
134 for (i = 0; i < ARRAY_LEN(values); i++) {
135 pbx_builtin_setvar_helper(c, varname, values[i]);
136 ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var));
137 pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace));
139 if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) {
140 ast_test_status_update(test, "%s != %s != %s\n",
141 values[i], ast_str_buffer(str), workspace);
145 ast_test_status_update(test, "Tested '%s' . . . . . %s\n",
146 ast_str_buffer(var), okay ? "passed" : "FAILED");
151 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
154 static enum ast_test_result_state test_chan_function(struct ast_test *test,
155 struct ast_channel *c, const char *expression)
158 char workspace[4096];
159 struct ast_str *str = ast_str_create(16);
161 ast_str_substitute_variables(&str, 0, c, expression);
162 pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
164 if (strcmp(workspace, ast_str_buffer(str)) != 0) {
165 ast_test_status_update(test, "expr: '%s' ... %s != %s\n",
166 expression, ast_str_buffer(str), workspace);
169 ast_test_status_update(test, "Tested '%s' . . . . . %s\n",
170 expression, okay ? "passed" : "FAILED");
174 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
177 static enum ast_test_result_state test_2way_function(struct ast_test *test,
178 struct ast_channel *c, const char *encode1, const char *encode2,
179 const char *decode1, const char *decode2)
181 struct ast_str *str = ast_str_create(16), *expression = ast_str_alloca(120);
184 ast_str_set(&expression, 0, "%s%s%s", encode1, "foobarbaz", encode2);
185 ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression));
186 ast_str_set(&expression, 0, "%s%s%s", decode1, ast_str_buffer(str), decode2);
187 ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression));
189 okay = !strcmp(ast_str_buffer(str), "foobarbaz");
191 ast_test_status_update(test, "'%s' != 'foobarbaz'\n",
192 ast_str_buffer(str));
194 ast_test_status_update(test, "Tested '%s%s' and '%s%s' . . . . . %s\n",
195 encode1, encode2, decode1, decode2,
196 okay ? "passed" : "FAILED");
200 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
203 static enum ast_test_result_state test_expected_result(struct ast_test *test,
204 struct ast_channel *c, const char *expression, const char *result)
206 struct ast_str *str = ast_str_create(16);
209 ast_str_substitute_variables(&str, 0, c, expression);
211 okay = !strcmp(ast_str_buffer(str), result);
213 ast_test_status_update(test, "'%s' != '%s'\n",
214 ast_str_buffer(str), result);
216 ast_test_status_update(test, "Tested '%s' ('%s') == '%s' . . . . . %s\n",
217 ast_str_buffer(str), expression, result,
218 okay ? "passed" : "FAILED");
222 return okay ? AST_TEST_PASS : AST_TEST_FAIL;
225 AST_TEST_DEFINE(test_substitution)
227 struct ast_channel *c;
229 enum ast_test_result_state res = AST_TEST_PASS;
233 info->name = "test_substitution";
234 info->category = "/main/pbx/";
235 info->summary = "Test variable and function substitution";
237 "This test executes a variety of variable and function substitutions "
238 "and ensures that the expected results are received.";
239 return AST_TEST_NOT_RUN;
244 ast_test_status_update(test, "Testing variable substitution ...\n");
246 c = ast_channel_alloc(0, 0, "", "", "", "", "", "", 0, "Test/substitution");
248 #define TEST(t) if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; }
251 * We can no longer test the CALLINGPRES value this way because it is now
252 * a calculated value from the name and number presentation information to
253 * get a combined presentation value.
255 TEST(test_chan_integer(test, c, &c->caller.id.number.presentation, "${CALLINGPRES}"));
257 TEST(test_chan_integer(test, c, &ast_channel_caller(c)->ani2, "${CALLINGANI2}"));
258 TEST(test_chan_integer(test, c, &ast_channel_caller(c)->id.number.plan, "${CALLINGTON}"));
259 TEST(test_chan_integer(test, c, &ast_channel_dialed(c)->transit_network_select, "${CALLINGTNS}"));
260 TEST(test_chan_integer_accessor(test, c, ast_channel_hangupcause_set, "${HANGUPCAUSE}"));
261 TEST(test_chan_integer_accessor(test, c, ast_channel_priority_set, "${PRIORITY}"));
262 TEST(test_chan_string(test, c, ast_channel_context_set, ast_channel_context, "${CONTEXT}"));
263 TEST(test_chan_string(test, c, ast_channel_exten_set, ast_channel_exten, "${EXTEN}"));
264 TEST(test_chan_variable(test, c, "CHANNEL(language)"));
265 TEST(test_chan_variable(test, c, "CHANNEL(musicclass)"));
266 TEST(test_chan_variable(test, c, "CHANNEL(parkinglot)"));
267 TEST(test_chan_variable(test, c, "CALLERID(name)"));
268 TEST(test_chan_variable(test, c, "CURLOPT(proxyuserpwd)"));
269 TEST(test_chan_variable(test, c, "CDR(foo)"));
270 TEST(test_chan_variable(test, c, "ENV(foo)"));
271 TEST(test_chan_variable(test, c, "GLOBAL(foo)"));
272 TEST(test_chan_variable(test, c, "GROUP()"));
273 TEST(test_2way_function(test, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}"));
274 TEST(test_2way_function(test, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}"));
275 pbx_builtin_setvar_helper(c, "foo", "123");
276 pbx_builtin_setvar_helper(c, "bar", "foo");
277 pbx_builtin_setvar_helper(c, "baz", "fo");
278 TEST(test_expected_result(test, c, "${foo}${foo}", "123123"));
279 TEST(test_expected_result(test, c, "A${foo}A${foo}A", "A123A123A"));
280 TEST(test_expected_result(test, c, "A${${bar}}A", "A123A"));
281 TEST(test_expected_result(test, c, "A${${baz}o}A", "A123A"));
282 TEST(test_expected_result(test, c, "A${${baz}o:1}A", "A23A"));
283 TEST(test_expected_result(test, c, "A${${baz}o:1:1}A", "A2A"));
284 TEST(test_expected_result(test, c, "A${${baz}o:1:-1}A", "A2A"));
285 TEST(test_expected_result(test, c, "A${${baz}o:-1:1}A", "A3A"));
286 TEST(test_expected_result(test, c, "A${${baz}o:-2:1}A", "A2A"));
287 TEST(test_expected_result(test, c, "A${${baz}o:-2:-1}A", "A2A"));
288 pbx_builtin_setvar_helper(c, "list1", "ab&cd&ef");
289 TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,cd)}", "ab&ef"));
290 TEST(test_expected_result(test, c, "${SHELL(printf '%d' 123)},${SHELL(printf '%d' 456)}", "123,456"));
291 TEST(test_expected_result(test, c, "${foo},${CDR(answer)},${SHELL(printf '%d' 456)}", "123,0.000000,456"));
292 TEST(test_expected_result(test, c, "${foo},${CDR(answer,u)},${SHELL(printf '%d' 456)}", "123,0.000000,456"));
293 TEST(test_expected_result(test, c, "${foo},${this_does_not_exist},${THIS_DOES_NOT_EXIST(either)}", "123,,"));
296 /* For testing dialplan functions */
298 char *cmd = ast_cli_generator("core show function", "", i);
302 if (strcmp(cmd, "CHANNEL") && strcmp(cmd, "CALLERID") && strncmp(cmd, "CURL", 4) &&
303 strncmp(cmd, "AES", 3) && strncmp(cmd, "BASE64", 6) &&
304 strcmp(cmd, "CDR") && strcmp(cmd, "ENV") && strcmp(cmd, "GLOBAL") &&
305 strcmp(cmd, "GROUP") && strcmp(cmd, "CUT") && strcmp(cmd, "LISTFILTER") &&
306 strcmp(cmd, "PP_EACH_EXTENSION") && strcmp(cmd, "SET")) {
307 struct ast_custom_function *acf = ast_custom_function_find(cmd);
308 if (acf->read && acf->read2) {
310 snprintf(expression, sizeof(expression), "${%s(foo)}", cmd);
311 if (AST_TEST_FAIL == test_chan_function(test, c, expression)) {
323 static int unload_module(void)
325 AST_TEST_UNREGISTER(test_substitution);
329 static int load_module(void)
331 AST_TEST_REGISTER(test_substitution);
332 return AST_MODULE_LOAD_SUCCESS;
335 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Substitution tests");