test_gosub.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
  23. *
  24. * \ingroup tests
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <support_level>extended</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. int res = AST_TEST_PASS, i;
  40. struct ast_channel *chan;
  41. struct ast_str *str;
  42. struct ast_context *con;
  43. struct testplan {
  44. const char *app;
  45. const char *args;
  46. const char *expected_value;
  47. } testplan[] = {
  48. { "Gosub", "tests_test_gosub_virtual_context,s,1" },
  49. { NULL, "${EXTEN}", "s" },
  50. { "Gosub", "test,dne,1", (const char *) -1 }, /* This is the only invocation that should fail. */
  51. { NULL, "${EXTEN}", "s" },
  52. { "Gosub", "tests_test_gosub_virtual_context,s,1(5,5,5,5,5)" },
  53. { NULL, "$[0${ARG1} + 0${ARG5}]", "10" },
  54. { "Gosub", "tests_test_gosub_virtual_context,s,1(4,4,4,4)" },
  55. { NULL, "$[0${ARG1} + 0${ARG5}]", "4" },
  56. { NULL, "$[0${ARG1} + 0${ARG4}]", "8" },
  57. { "Gosub", "tests_test_gosub_virtual_context,s,1(3,3,3)" },
  58. { NULL, "$[0${ARG1} + 0${ARG4}]", "3" },
  59. { NULL, "$[0${ARG1} + 0${ARG3}]", "6" },
  60. { "Gosub", "tests_test_gosub_virtual_context,s,1(2,2)" },
  61. { NULL, "$[0${ARG1} + 0${ARG3}]", "2" },
  62. { NULL, "$[0${ARG1} + 0${ARG2}]", "4" },
  63. { "Gosub", "tests_test_gosub_virtual_context,s,1(1)" },
  64. { NULL, "$[0${ARG1} + 0${ARG2}]", "1" },
  65. { NULL, "$[0${ARG1} + 0${ARG1}]", "2" },
  66. { "Gosub", "tests_test_gosub_virtual_context,s,1" },
  67. { NULL, "$[0${ARG1} + 0${ARG1}]", "0" }, /* All arguments are correctly masked */
  68. { "Set", "LOCAL(foo)=5" },
  69. { NULL, "${foo}", "5" }, /* LOCAL() set a variable correctly */
  70. { NULL, "${LOCAL_PEEK(0,ARG1)}", "" }, /* LOCAL_PEEK() arguments work correctly */
  71. { NULL, "${LOCAL_PEEK(4,ARG1)}", "4" }, /* LOCAL_PEEK() arguments work correctly */
  72. { NULL, "$[0${LOCAL_PEEK(3,ARG1)} + 0${LOCAL_PEEK(5,ARG1)}]", "8" },
  73. { "StackPop", "" },
  74. { NULL, "${foo}", "" }, /* StackPop removed the variable set with LOCAL() */
  75. { "Return", "7" },
  76. { NULL, "${GOSUB_RETVAL}", "7" }, /* Return sets a return value correctly */
  77. { NULL, "$[0${GOSUB_RETVAL} + 0${ARG1}]", "9" }, /* Two frames less means ARG1 should have 2 */
  78. };
  79. switch (cmd) {
  80. case TEST_INIT:
  81. info->name = "gosub application";
  82. info->category = "/apps/app_gosub/";
  83. info->summary = "Verify functionality of gosub application";
  84. info->description =
  85. "Verify functionality of gosub application";
  86. return AST_TEST_NOT_RUN;
  87. case TEST_EXECUTE:
  88. break;
  89. }
  90. if (!(chan = ast_dummy_channel_alloc())) {
  91. ast_test_status_update(test, "Unable to allocate dummy channel\n");
  92. return AST_TEST_FAIL;
  93. }
  94. if (!(str = ast_str_create(16))) {
  95. ast_test_status_update(test, "Unable to allocate dynamic string buffer\n");
  96. ast_channel_release(chan);
  97. return AST_TEST_FAIL;
  98. }
  99. /* Create our test dialplan */
  100. if (!(con = ast_context_find_or_create(NULL, NULL, "tests_test_gosub_virtual_context", "test_gosub"))) {
  101. ast_test_status_update(test, "Unable to create test dialplan context");
  102. ast_free(str);
  103. ast_channel_release(chan);
  104. return AST_TEST_FAIL;
  105. }
  106. ast_add_extension2(con, 1, "s", 1, NULL, NULL, "NoOp", ast_strdup(""), ast_free_ptr, "test_gosub");
  107. for (i = 0; i < ARRAY_LEN(testplan); i++) {
  108. if (testplan[i].app == NULL) {
  109. /* Evaluation */
  110. ast_str_substitute_variables(&str, 0, chan, testplan[i].args);
  111. if (strcmp(ast_str_buffer(str), testplan[i].expected_value)) {
  112. ast_test_status_update(test, "Evaluation of '%s' returned '%s' instead of the expected value '%s'\n",
  113. testplan[i].args, ast_str_buffer(str), testplan[i].expected_value);
  114. res = AST_TEST_FAIL;
  115. }
  116. } else {
  117. /* Run application */
  118. intptr_t exec_res;
  119. struct ast_app *app = pbx_findapp(testplan[i].app);
  120. if (!app) {
  121. ast_test_status_update(test, "Could not find '%s' in application listing!\n", testplan[i].app);
  122. res = AST_TEST_FAIL;
  123. break;
  124. }
  125. if ((exec_res = pbx_exec(chan, app, testplan[i].args)) && ((const char *) exec_res != testplan[i].expected_value)) {
  126. ast_test_status_update(test, "Application '%s' exited abnormally (with code %d)\n", testplan[i].app, (int) exec_res);
  127. res = AST_TEST_FAIL;
  128. break;
  129. }
  130. }
  131. }
  132. ast_free(str);
  133. ast_channel_release(chan);
  134. ast_context_remove_extension2(con, "s", 1, NULL, 0);
  135. ast_context_destroy(con, "test_gosub");
  136. return res;
  137. }
  138. static int unload_module(void)
  139. {
  140. AST_TEST_UNREGISTER(test_gosub);
  141. return 0;
  142. }
  143. static int load_module(void)
  144. {
  145. AST_TEST_REGISTER(test_gosub);
  146. return AST_MODULE_LOAD_SUCCESS;
  147. }
  148. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gosub Tests");