test_substitution.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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>extended</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. static enum ast_test_result_state test_chan_integer(struct ast_test *test,
  44. struct ast_channel *c, int *ifield, const char *expression)
  45. {
  46. int i, okay = 1, value1 = -1, value2 = -1;
  47. char workspace[4096];
  48. struct ast_str *str = ast_str_create(16);
  49. ast_test_status_update(test, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED");
  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_free(str);
  60. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  61. }
  62. static enum ast_test_result_state test_chan_string(struct ast_test *test,
  63. struct ast_channel *c, char *cfield, size_t cfieldsize,
  64. const char *expression)
  65. {
  66. const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
  67. int i, okay = 1;
  68. char workspace[4096];
  69. struct ast_str *str = ast_str_create(16);
  70. for (i = 0; i < ARRAY_LEN(values); i++) {
  71. ast_copy_string(cfield, values[i], cfieldsize);
  72. ast_str_substitute_variables(&str, 0, c, expression);
  73. pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
  74. ast_test_status_update(test, "Testing '%s' . . . . . %s\n",
  75. expression, okay ? "passed" : "FAILED");
  76. if (strcmp(cfield, ast_str_buffer(str)) != 0 || strcmp(cfield, workspace) != 0) {
  77. ast_test_status_update(test, "%s != %s != %s\n", cfield, ast_str_buffer(str), workspace);
  78. okay = 0;
  79. }
  80. }
  81. ast_free(str);
  82. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  83. }
  84. static enum ast_test_result_state test_chan_variable(struct ast_test *test,
  85. struct ast_channel *c, const char *varname)
  86. {
  87. const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" };
  88. int i, okay = 1;
  89. char workspace[4096];
  90. struct ast_str *str = ast_str_create(16);
  91. struct ast_str *var = ast_str_create(16);
  92. ast_str_set(&var, 0, "${%s}", varname);
  93. for (i = 0; i < ARRAY_LEN(values); i++) {
  94. pbx_builtin_setvar_helper(c, varname, values[i]);
  95. ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var));
  96. pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace));
  97. ast_test_status_update(test, "Testing '%s' . . . . . %s\n",
  98. ast_str_buffer(var), okay ? "passed" : "FAILED");
  99. if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) {
  100. ast_test_status_update(test, "%s != %s != %s\n",
  101. values[i], ast_str_buffer(str), workspace);
  102. okay = 0;
  103. }
  104. }
  105. ast_free(str);
  106. ast_free(var);
  107. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  108. }
  109. static enum ast_test_result_state test_chan_function(struct ast_test *test,
  110. struct ast_channel *c, const char *expression)
  111. {
  112. int okay = 1;
  113. char workspace[4096];
  114. struct ast_str *str = ast_str_create(16);
  115. ast_str_substitute_variables(&str, 0, c, expression);
  116. pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace));
  117. ast_test_status_update(test, "Testing '%s' . . . . . %s\n",
  118. expression, okay ? "passed" : "FAILED");
  119. if (strcmp(workspace, ast_str_buffer(str)) != 0) {
  120. ast_test_status_update(test, "test_chan_function, expr: '%s' ... %s != %s\n",
  121. expression, ast_str_buffer(str), workspace);
  122. okay = 0;
  123. }
  124. ast_free(str);
  125. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  126. }
  127. static enum ast_test_result_state test_2way_function(struct ast_test *test,
  128. struct ast_channel *c, const char *encode1, const char *encode2,
  129. const char *decode1, const char *decode2)
  130. {
  131. struct ast_str *str = ast_str_create(16), *expression = ast_str_alloca(120);
  132. int okay;
  133. ast_str_set(&expression, 0, "%s%s%s", encode1, "foobarbaz", encode2);
  134. ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression));
  135. ast_str_set(&expression, 0, "%s%s%s", decode1, ast_str_buffer(str), decode2);
  136. ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression));
  137. okay = !strcmp(ast_str_buffer(str), "foobarbaz");
  138. ast_test_status_update(test, "Testing '%s%s' and '%s%s' . . . . . %s\n",
  139. encode1, encode2, decode1, decode2,
  140. okay ? "passed" : "FAILED");
  141. if (!okay) {
  142. ast_test_status_update(test, " '%s' != 'foobarbaz'\n",
  143. ast_str_buffer(str));
  144. }
  145. ast_free(str);
  146. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  147. }
  148. static enum ast_test_result_state test_expected_result(struct ast_test *test,
  149. struct ast_channel *c, const char *expression, const char *result)
  150. {
  151. struct ast_str *str = ast_str_create(16);
  152. int okay;
  153. ast_str_substitute_variables(&str, 0, c, expression);
  154. okay = !strcmp(ast_str_buffer(str), result);
  155. ast_test_status_update(test, "Testing '%s' ('%s') == '%s' . . . . . %s\n",
  156. ast_str_buffer(str), expression, result,
  157. okay ? "passed" : "FAILED");
  158. if (!okay) {
  159. ast_test_status_update(test, "test_expected_result: '%s' != '%s'\n",
  160. ast_str_buffer(str), result);
  161. }
  162. ast_free(str);
  163. return okay ? AST_TEST_PASS : AST_TEST_FAIL;
  164. }
  165. AST_TEST_DEFINE(test_substitution)
  166. {
  167. struct ast_channel *c;
  168. int i;
  169. enum ast_test_result_state res = AST_TEST_PASS;
  170. switch (cmd) {
  171. case TEST_INIT:
  172. info->name = "test_substitution";
  173. info->category = "/main/pbx/";
  174. info->summary = "Test variable and function substitution";
  175. info->description =
  176. "This test executes a variety of variable and function substitutions "
  177. "and ensures that the expected results are received.";
  178. return AST_TEST_NOT_RUN;
  179. case TEST_EXECUTE:
  180. break;
  181. }
  182. ast_test_status_update(test, "Testing variable substitution ...\n");
  183. c = ast_channel_alloc(0, 0, "", "", "", "", "", "", 0, "Test/substitution");
  184. #define TEST(t) if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; }
  185. #if 0
  186. /*
  187. * We can no longer test the CALLINGPRES value this way because it is now
  188. * a calculated value from the name and number presentation information to
  189. * get a combined presentation value.
  190. */
  191. TEST(test_chan_integer(test, c, &c->caller.id.number.presentation, "${CALLINGPRES}"));
  192. #endif
  193. TEST(test_chan_integer(test, c, &c->caller.ani2, "${CALLINGANI2}"));
  194. TEST(test_chan_integer(test, c, &c->caller.id.number.plan, "${CALLINGTON}"));
  195. TEST(test_chan_integer(test, c, &c->dialed.transit_network_select, "${CALLINGTNS}"));
  196. TEST(test_chan_integer(test, c, &c->hangupcause, "${HANGUPCAUSE}"));
  197. TEST(test_chan_integer(test, c, &c->priority, "${PRIORITY}"));
  198. TEST(test_chan_string(test, c, c->context, sizeof(c->context), "${CONTEXT}"));
  199. TEST(test_chan_string(test, c, c->exten, sizeof(c->exten), "${EXTEN}"));
  200. TEST(test_chan_variable(test, c, "CHANNEL(language)"));
  201. TEST(test_chan_variable(test, c, "CHANNEL(musicclass)"));
  202. TEST(test_chan_variable(test, c, "CHANNEL(parkinglot)"));
  203. TEST(test_chan_variable(test, c, "CALLERID(name)"));
  204. TEST(test_chan_variable(test, c, "CURLOPT(proxyuserpwd)"));
  205. TEST(test_chan_variable(test, c, "CDR(foo)"));
  206. TEST(test_chan_variable(test, c, "ENV(foo)"));
  207. TEST(test_chan_variable(test, c, "GLOBAL(foo)"));
  208. TEST(test_chan_variable(test, c, "GROUP()"));
  209. TEST(test_2way_function(test, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}"));
  210. TEST(test_2way_function(test, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}"));
  211. pbx_builtin_setvar_helper(c, "foo", "123");
  212. pbx_builtin_setvar_helper(c, "bar", "foo");
  213. pbx_builtin_setvar_helper(c, "baz", "fo");
  214. TEST(test_expected_result(test, c, "${foo}${foo}", "123123"));
  215. TEST(test_expected_result(test, c, "A${foo}A${foo}A", "A123A123A"));
  216. TEST(test_expected_result(test, c, "A${${bar}}A", "A123A"));
  217. TEST(test_expected_result(test, c, "A${${baz}o}A", "A123A"));
  218. TEST(test_expected_result(test, c, "A${${baz}o:1}A", "A23A"));
  219. TEST(test_expected_result(test, c, "A${${baz}o:1:1}A", "A2A"));
  220. TEST(test_expected_result(test, c, "A${${baz}o:1:-1}A", "A2A"));
  221. TEST(test_expected_result(test, c, "A${${baz}o:-1:1}A", "A3A"));
  222. TEST(test_expected_result(test, c, "A${${baz}o:-2:1}A", "A2A"));
  223. TEST(test_expected_result(test, c, "A${${baz}o:-2:-1}A", "A2A"));
  224. pbx_builtin_setvar_helper(c, "list1", "ab&cd&ef");
  225. TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,cd)}", "ab&ef"));
  226. TEST(test_expected_result(test, c, "${SHELL(printf '%d' 123)},${SHELL(printf '%d' 456)}", "123,456"));
  227. TEST(test_expected_result(test, c, "${foo},${CDR(answer)},${SHELL(printf '%d' 456)}", "123,,456"));
  228. TEST(test_expected_result(test, c, "${foo},${this_does_not_exist},${THIS_DOES_NOT_EXIST(either)}", "123,,"));
  229. #undef TEST
  230. /* For testing dialplan functions */
  231. for (i = 0; ; i++) {
  232. char *cmd = ast_cli_generator("core show function", "", i);
  233. if (cmd == NULL) {
  234. break;
  235. }
  236. if (strcmp(cmd, "CHANNEL") && strcmp(cmd, "CALLERID") && strncmp(cmd, "CURL", 4) &&
  237. strncmp(cmd, "AES", 3) && strncmp(cmd, "BASE64", 6) &&
  238. strcmp(cmd, "CDR") && strcmp(cmd, "ENV") && strcmp(cmd, "GLOBAL") &&
  239. strcmp(cmd, "GROUP") && strcmp(cmd, "CUT") && strcmp(cmd, "LISTFILTER") &&
  240. strcmp(cmd, "PP_EACH_EXTENSION") && strcmp(cmd, "SET")) {
  241. struct ast_custom_function *acf = ast_custom_function_find(cmd);
  242. if (acf->read && acf->read2) {
  243. char expression[80];
  244. snprintf(expression, sizeof(expression), "${%s(foo)}", cmd);
  245. if (AST_TEST_FAIL == test_chan_function(test, c, expression)) {
  246. res = AST_TEST_FAIL;
  247. }
  248. }
  249. }
  250. ast_free(cmd);
  251. }
  252. ast_hangup(c);
  253. return res;
  254. }
  255. static int unload_module(void)
  256. {
  257. AST_TEST_UNREGISTER(test_substitution);
  258. return 0;
  259. }
  260. static int load_module(void)
  261. {
  262. AST_TEST_REGISTER(test_substitution);
  263. return AST_MODULE_LOAD_SUCCESS;
  264. }
  265. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Substitution tests");