test_gosub.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, 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. /*!
  19. * \file
  20. * \brief Gosub tests
  21. *
  22. * \author Tilghman Lesher \verbatim <tlesher AT digium DOT com> \endverbatim
  23. *
  24. * \ingroup tests
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/utils.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/test.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/channel.h"
  37. AST_TEST_DEFINE(test_gosub)
  38. {
  39. #define CONTEXT_NAME "tests_test_gosub_virtual_context"
  40. int res = AST_TEST_PASS, i;
  41. struct ast_channel *chan;
  42. struct ast_str *str;
  43. struct testplan {
  44. const char *app;
  45. const char *args;
  46. const char *expected_value;
  47. } testplan[] = {
  48. { NULL, "${STACK_PEEK(1,e,1)}", "" }, /* Stack is empty */
  49. { "Gosub", "tests_test_gosub_virtual_context,s,1" },
  50. { NULL, "${PRIORITY}", "1" },
  51. { NULL, "${EXTEN}", "s" },
  52. { NULL, "${STACK_PEEK(1,e,1)}", "" }, /* No extension originally */
  53. { "Gosub", "test,dne,1", (const char *) -1 }, /* This is the only invocation that should fail. */
  54. { NULL, "${PRIORITY}", "1" },
  55. { NULL, "${EXTEN}", "s" },
  56. { "Gosub", "tests_test_gosub_virtual_context,s,1(5,5,5,5,5)" },
  57. { NULL, "${PRIORITY}", "1" },
  58. { NULL, "$[0${ARG1} + 0${ARG5}]", "10" },
  59. { NULL, "${STACK_PEEK(1,e)}", "s" },
  60. { NULL, "${STACK_PEEK(1,c)}", "tests_test_gosub_virtual_context" },
  61. { NULL, "${STACK_PEEK(1,p)}", "1" },
  62. { NULL, "${STACK_PEEK(1,l)}", "tests_test_gosub_virtual_context,s,1" },
  63. { "StackPop", "" },
  64. { NULL, "${STACK_PEEK(1,e,1)}", "" }, /* Only 1 frame deep, my caller is top-level */
  65. { "Gosub", "tests_test_gosub_virtual_context,s,1(5,5,5,5,5)" },
  66. { "Gosub", "tests_test_gosub_virtual_context,s,1(4,4,4,4)" },
  67. { NULL, "$[0${ARG1} + 0${ARG5}]", "4" },
  68. { NULL, "$[0${ARG1} + 0${ARG4}]", "8" },
  69. { "Gosub", "tests_test_gosub_virtual_context,s,1(3,3,3)" },
  70. { NULL, "$[0${ARG1} + 0${ARG4}]", "3" },
  71. { NULL, "$[0${ARG1} + 0${ARG3}]", "6" },
  72. { "Gosub", "tests_test_gosub_virtual_context,s,1(2,2)" },
  73. { NULL, "$[0${ARG1} + 0${ARG3}]", "2" },
  74. { NULL, "$[0${ARG1} + 0${ARG2}]", "4" },
  75. { "Gosub", "tests_test_gosub_virtual_context,s,1(1)" },
  76. { NULL, "$[0${ARG1} + 0${ARG2}]", "1" },
  77. { NULL, "$[0${ARG1} + 0${ARG1}]", "2" },
  78. { "Gosub", "tests_test_gosub_virtual_context,s,1" },
  79. { NULL, "$[0${ARG1} + 0${ARG1}]", "0" }, /* All arguments are correctly masked */
  80. { "Set", "LOCAL(foo)=5" },
  81. { NULL, "${foo}", "5" }, /* LOCAL() set a variable correctly */
  82. { NULL, "${LOCAL_PEEK(0,ARG1)}", "" }, /* LOCAL_PEEK() arguments work correctly */
  83. { NULL, "${LOCAL_PEEK(4,ARG1)}", "4" }, /* LOCAL_PEEK() arguments work correctly */
  84. { NULL, "$[0${LOCAL_PEEK(3,ARG1)} + 0${LOCAL_PEEK(5,ARG1)}]", "8" },
  85. { "StackPop", "" },
  86. { NULL, "${foo}", "" }, /* StackPop removed the variable set with LOCAL() */
  87. { "Return", "7" },
  88. { NULL, "${GOSUB_RETVAL}", "7" }, /* Return sets a return value correctly */
  89. { NULL, "$[0${GOSUB_RETVAL} + 0${ARG1}]", "9" }, /* Two frames less means ARG1 should have 2 */
  90. };
  91. switch (cmd) {
  92. case TEST_INIT:
  93. info->name = "gosub application";
  94. info->category = "/apps/app_gosub/";
  95. info->summary = "Verify functionality of gosub application";
  96. info->description =
  97. "Verify functionality of gosub application";
  98. return AST_TEST_NOT_RUN;
  99. case TEST_EXECUTE:
  100. break;
  101. }
  102. if (!(chan = ast_dummy_channel_alloc())) {
  103. ast_test_status_update(test, "Unable to allocate dummy channel\n");
  104. return AST_TEST_FAIL;
  105. }
  106. if (!(str = ast_str_create(16))) {
  107. ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
  108. ast_channel_unref(chan);
  109. return AST_TEST_FAIL;
  110. }
  111. /* Create our test dialplan */
  112. if (!ast_context_find_or_create(NULL, NULL, CONTEXT_NAME, "test_gosub")) {
  113. ast_test_status_update(test, "Unable to create test dialplan context");
  114. ast_free(str);
  115. ast_channel_unref(chan);
  116. return AST_TEST_FAIL;
  117. }
  118. ast_add_extension(CONTEXT_NAME, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "test_gosub");
  119. for (i = 0; i < ARRAY_LEN(testplan); i++) {
  120. if (testplan[i].app == NULL) {
  121. /* Evaluation */
  122. ast_str_substitute_variables(&str, 0, chan, testplan[i].args);
  123. if (strcmp(ast_str_buffer(str), testplan[i].expected_value)) {
  124. ast_test_status_update(test, "Evaluation of '%s' returned '%s' instead of the expected value '%s'\n",
  125. testplan[i].args, ast_str_buffer(str), testplan[i].expected_value);
  126. res = AST_TEST_FAIL;
  127. }
  128. } else {
  129. /* Run application */
  130. intptr_t exec_res;
  131. struct ast_app *app = pbx_findapp(testplan[i].app);
  132. if (!app) {
  133. ast_test_status_update(test, "Could not find '%s' in application listing!\n", testplan[i].app);
  134. res = AST_TEST_FAIL;
  135. break;
  136. }
  137. if ((exec_res = pbx_exec(chan, app, testplan[i].args)) && ((const char *) exec_res != testplan[i].expected_value)) {
  138. ast_test_status_update(test, "Application '%s' exited abnormally (with code %d)\n", testplan[i].app, (int) exec_res);
  139. res = AST_TEST_FAIL;
  140. break;
  141. }
  142. }
  143. }
  144. ast_free(str);
  145. ast_channel_unref(chan);
  146. ast_context_remove_extension(CONTEXT_NAME, "s", 1, NULL);
  147. ast_context_destroy(NULL, "test_gosub");
  148. return res;
  149. }
  150. static int unload_module(void)
  151. {
  152. AST_TEST_UNREGISTER(test_gosub);
  153. return 0;
  154. }
  155. static int load_module(void)
  156. {
  157. AST_TEST_REGISTER(test_gosub);
  158. return AST_MODULE_LOAD_SUCCESS;
  159. }
  160. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gosub Tests");