test_substitution.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Digium, Inc.
  5. *
  6. * Tilghman Lesher <tlesher AT digium DOT com>
  7. *
  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.
  13. *
  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.
  17. */
  18. /*! \file
  19. *
  20. * \brief Substitution Test
  21. *
  22. * \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
  23. *
  24. * \ingroup tests
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <depend>func_curl</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/file.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/module.h"
  37. #include "asterisk/lock.h"
  38. #include "asterisk/app.h"
  39. #include "asterisk/strings.h"
  40. #include "asterisk/stringfields.h"
  41. #include "asterisk/threadstorage.h"
  42. #include "asterisk/test.h"
  43. #include "asterisk/vector.h"
  44. static enum ast_test_result_state test_chan_integer(struct ast_test *test,
  45. struct ast_channel *c, int *ifield, const char *expression)
  46. {
  47. int i, okay = 1, value1 = -1, value2 = -1;
  48. char workspace[4096];
  49. struct ast_str *str = ast_str_create(16);
  50. for (i = 0; i < 256; i++) {
  51. *ifield = i;
  52. ast_str_substitute_variables(&str, 0, c, expression);
  53. pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
  54. if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) {
  55. ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i);
  56. okay = 0;
  57. }
  58. }
  59. ast_test_status_update(test, "Tested '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
  60. ast_free(str);
  61. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  62. }
  63. static enum ast_test_result_state test_chan_integer_accessor(struct ast_test *test,
  64. struct ast_channel *c, void (*setter)(struct ast_channel *, int),const char *expression)
  65. {
  66. int i, okay = 1, value1 = -1, value2 = -1;
  67. char workspace[4096];
  68. struct ast_str *str = ast_str_create(16);
  69. for (i = 0; i < 256; i++) {
  70. setter(c, i);
  71. ast_str_substitute_variables(&str, 0, c, expression);
  72. pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
  73. if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) {
  74. ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i);
  75. okay = 0;
  76. }
  77. }
  78. ast_test_status_update(test, "Tested '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
  79. ast_free(str);
  80. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  81. }
  82. static enum ast_test_result_state test_chan_string(struct ast_test *test,
  83. struct ast_channel *c, void (*setter)(struct ast_channel *, const char *),
  84. const char *(*getter)(const struct ast_channel *), const char *expression)
  85. {
  86. const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
  87. int i, okay = 1;
  88. char workspace[4096];
  89. struct ast_str *str = ast_str_create(16);
  90. for (i = 0; i < ARRAY_LEN(values); i++) {
  91. setter(c, values[i]);
  92. ast_str_substitute_variables(&str, 0, c, expression);
  93. pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
  94. if (strcmp(getter(c), ast_str_buffer(str)) != 0 || strcmp(getter(c), workspace) != 0) {
  95. ast_test_status_update(test, "%s != %s != %s\n", getter(c), ast_str_buffer(str), workspace);
  96. okay = 0;
  97. }
  98. }
  99. ast_test_status_update(test, "Tested '%s' . . . . . %s\n",
  100. expression, okay ? "passed" : "FAILED");
  101. ast_free(str);
  102. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  103. }
  104. static enum ast_test_result_state test_chan_variable(struct ast_test *test,
  105. struct ast_channel *c, const char *varname)
  106. {
  107. const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
  108. int i, okay = 1;
  109. char workspace[4096];
  110. struct ast_str *str = ast_str_create(16);
  111. struct ast_str *var = ast_str_create(16);
  112. ast_str_set(&var, 0, "${%s}", varname);
  113. for (i = 0; i < ARRAY_LEN(values); i++) {
  114. pbx_builtin_setvar_helper(c, varname, values[i]);
  115. ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var));
  116. pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace));
  117. if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) {
  118. ast_test_status_update(test, "%s != %s != %s\n",
  119. values[i], ast_str_buffer(str), workspace);
  120. okay = 0;
  121. }
  122. }
  123. ast_test_status_update(test, "Tested '%s' . . . . . %s\n",
  124. ast_str_buffer(var), okay ? "passed" : "FAILED");
  125. ast_free(str);
  126. ast_free(var);
  127. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  128. }
  129. static enum ast_test_result_state test_chan_function(struct ast_test *test,
  130. struct ast_channel *c, const char *expression)
  131. {
  132. int okay = 1;
  133. char workspace[4096];
  134. struct ast_str *str = ast_str_create(16);
  135. ast_str_substitute_variables(&str, 0, c, expression);
  136. pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
  137. if (strcmp(workspace, ast_str_buffer(str)) != 0) {
  138. ast_test_status_update(test, "expr: '%s' ... %s != %s\n",
  139. expression, ast_str_buffer(str), workspace);
  140. okay = 0;
  141. }
  142. ast_test_status_update(test, "Tested '%s' . . . . . %s\n",
  143. expression, okay ? "passed" : "FAILED");
  144. ast_free(str);
  145. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  146. }
  147. static enum ast_test_result_state test_2way_function(struct ast_test *test,
  148. struct ast_channel *c, const char *encode1, const char *encode2,
  149. const char *decode1, const char *decode2)
  150. {
  151. struct ast_str *str = ast_str_create(16), *expression = ast_str_alloca(120);
  152. int okay;
  153. ast_str_set(&expression, 0, "%s%s%s", encode1, "foobarbaz", encode2);
  154. ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression));
  155. ast_str_set(&expression, 0, "%s%s%s", decode1, ast_str_buffer(str), decode2);
  156. ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression));
  157. okay = !strcmp(ast_str_buffer(str), "foobarbaz");
  158. if (!okay) {
  159. ast_test_status_update(test, "'%s' != 'foobarbaz'\n",
  160. ast_str_buffer(str));
  161. }
  162. ast_test_status_update(test, "Tested '%s%s' and '%s%s' . . . . . %s\n",
  163. encode1, encode2, decode1, decode2,
  164. okay ? "passed" : "FAILED");
  165. ast_free(str);
  166. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  167. }
  168. static enum ast_test_result_state test_expected_result(struct ast_test *test,
  169. struct ast_channel *c, const char *expression, const char *result)
  170. {
  171. struct ast_str *str = ast_str_create(16);
  172. int okay;
  173. ast_str_substitute_variables(&str, 0, c, expression);
  174. okay = !strcmp(ast_str_buffer(str), result);
  175. if (!okay) {
  176. ast_test_status_update(test, "'%s' != '%s'\n",
  177. ast_str_buffer(str), result);
  178. }
  179. ast_test_status_update(test, "Tested '%s' ('%s') == '%s' . . . . . %s\n",
  180. ast_str_buffer(str), expression, result,
  181. okay ? "passed" : "FAILED");
  182. ast_free(str);
  183. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  184. }
  185. AST_TEST_DEFINE(test_substitution)
  186. {
  187. struct ast_channel *c;
  188. int i;
  189. enum ast_test_result_state res = AST_TEST_PASS;
  190. struct ast_vector_string *funcs;
  191. switch (cmd) {
  192. case TEST_INIT:
  193. info->name = "test_substitution";
  194. info->category = "/main/pbx/";
  195. info->summary = "Test variable and function substitution";
  196. info->description =
  197. "This test executes a variety of variable and function substitutions "
  198. "and ensures that the expected results are received.";
  199. return AST_TEST_NOT_RUN;
  200. case TEST_EXECUTE:
  201. break;
  202. }
  203. ast_test_status_update(test, "Testing variable substitution ...\n");
  204. c = ast_channel_alloc(0, 0, "", "", "", "", "", NULL, NULL, 0, "Test/substitution");
  205. ast_channel_unlock(c);
  206. #define TEST(t) if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; }
  207. #if 0
  208. /*
  209. * We can no longer test the CALLINGPRES value this way because it is now
  210. * a calculated value from the name and number presentation information to
  211. * get a combined presentation value.
  212. */
  213. TEST(test_chan_integer(test, c, &c->caller.id.number.presentation, "${CALLINGPRES}"));
  214. #endif
  215. TEST(test_chan_integer(test, c, &ast_channel_caller(c)->ani2, "${CALLINGANI2}"));
  216. TEST(test_chan_integer(test, c, &ast_channel_caller(c)->id.number.plan, "${CALLINGTON}"));
  217. TEST(test_chan_integer(test, c, &ast_channel_dialed(c)->transit_network_select, "${CALLINGTNS}"));
  218. TEST(test_chan_integer_accessor(test, c, ast_channel_hangupcause_set, "${HANGUPCAUSE}"));
  219. TEST(test_chan_integer_accessor(test, c, ast_channel_priority_set, "${PRIORITY}"));
  220. TEST(test_chan_string(test, c, ast_channel_context_set, ast_channel_context, "${CONTEXT}"));
  221. TEST(test_chan_string(test, c, ast_channel_exten_set, ast_channel_exten, "${EXTEN}"));
  222. TEST(test_chan_variable(test, c, "CHANNEL(language)"));
  223. TEST(test_chan_variable(test, c, "CHANNEL(musicclass)"));
  224. TEST(test_chan_variable(test, c, "CHANNEL(parkinglot)"));
  225. TEST(test_chan_variable(test, c, "CALLERID(name)"));
  226. TEST(test_chan_variable(test, c, "CURLOPT(proxyuserpwd)"));
  227. TEST(test_chan_variable(test, c, "CDR(foo)"));
  228. TEST(test_chan_variable(test, c, "ENV(foo)"));
  229. TEST(test_chan_variable(test, c, "GLOBAL(foo)"));
  230. TEST(test_chan_variable(test, c, "GROUP()"));
  231. TEST(test_2way_function(test, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}"));
  232. TEST(test_2way_function(test, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}"));
  233. pbx_builtin_setvar_helper(c, "foo", "123");
  234. pbx_builtin_setvar_helper(c, "bar", "foo");
  235. pbx_builtin_setvar_helper(c, "baz", "fo");
  236. TEST(test_expected_result(test, c, "${foo}${foo}", "123123"));
  237. TEST(test_expected_result(test, c, "A${foo}A${foo}A", "A123A123A"));
  238. TEST(test_expected_result(test, c, "A${${bar}}A", "A123A"));
  239. TEST(test_expected_result(test, c, "A${${baz}o}A", "A123A"));
  240. TEST(test_expected_result(test, c, "A${${baz}o:1}A", "A23A"));
  241. TEST(test_expected_result(test, c, "A${${baz}o:1:1}A", "A2A"));
  242. TEST(test_expected_result(test, c, "A${${baz}o:1:-1}A", "A2A"));
  243. TEST(test_expected_result(test, c, "A${${baz}o:-1:1}A", "A3A"));
  244. TEST(test_expected_result(test, c, "A${${baz}o:-2:1}A", "A2A"));
  245. TEST(test_expected_result(test, c, "A${${baz}o:-2:-1}A", "A2A"));
  246. pbx_builtin_setvar_helper(c, "list1", "ab&cd&ef");
  247. TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,ab)}", "cd&ef"));
  248. TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,cd)}", "ab&ef"));
  249. TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,ef)}", "ab&cd"));
  250. TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,gh)}", "ab&cd&ef"));
  251. TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,c)}", "ab&cd&ef"));
  252. TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,d)}", "ab&cd&ef"));
  253. pbx_builtin_setvar_helper(c, "list2", "ab");
  254. TEST(test_expected_result(test, c, "${LISTFILTER(list2,&,ab)}", ""));
  255. pbx_builtin_setvar_helper(c, "list_empty", "");
  256. TEST(test_expected_result(test, c, "${LISTFILTER(list_empty,&,ab)}", ""));
  257. TEST(test_expected_result(test, c, "${SHELL(printf '%d' 123)},${SHELL(printf '%d' 456)}", "123,456"));
  258. TEST(test_expected_result(test, c, "${foo},${CDR(answer)},${SHELL(printf '%d' 456)}", "123,,456"));
  259. TEST(test_expected_result(test, c, "${foo},${CDR(answer,u)},${SHELL(printf '%d' 456)}", "123,0.000000,456"));
  260. TEST(test_expected_result(test, c, "${foo},${this_does_not_exist},${THIS_DOES_NOT_EXIST(either)}", "123,,"));
  261. #undef TEST
  262. /* For testing dialplan functions */
  263. funcs = ast_cli_completion_vector("core show function", "");
  264. /* Skip "best match" element 0 */
  265. for (i = 1; funcs && i < AST_VECTOR_SIZE(funcs); i++) {
  266. char *cmd = AST_VECTOR_GET(funcs, i);
  267. if (strcmp(cmd, "CHANNEL") && strcmp(cmd, "CALLERID") && strncmp(cmd, "CURL", 4) &&
  268. strncmp(cmd, "AES", 3) && strncmp(cmd, "BASE64", 6) &&
  269. strcmp(cmd, "CDR") && strcmp(cmd, "ENV") && strcmp(cmd, "GLOBAL") &&
  270. strcmp(cmd, "GROUP") && strcmp(cmd, "CUT") && strcmp(cmd, "LISTFILTER") &&
  271. strcmp(cmd, "PP_EACH_EXTENSION") && strcmp(cmd, "SET")) {
  272. struct ast_custom_function *acf = ast_custom_function_find(cmd);
  273. if (acf->read && acf->read2) {
  274. char expression[80];
  275. snprintf(expression, sizeof(expression), "${%s(foo)}", cmd);
  276. if (AST_TEST_FAIL == test_chan_function(test, c, expression)) {
  277. res = AST_TEST_FAIL;
  278. }
  279. }
  280. }
  281. }
  282. if (funcs) {
  283. AST_VECTOR_CALLBACK_VOID(funcs, ast_free);
  284. AST_VECTOR_PTR_FREE(funcs);
  285. }
  286. ast_hangup(c);
  287. return res;
  288. }
  289. static int unload_module(void)
  290. {
  291. AST_TEST_UNREGISTER(test_substitution);
  292. return 0;
  293. }
  294. static int load_module(void)
  295. {
  296. AST_TEST_REGISTER(test_substitution);
  297. return AST_MODULE_LOAD_SUCCESS;
  298. }
  299. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Substitution tests");