test_substitution.c 12 KB

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