test_app.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Jeff Peeler <jpeeler@digium.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 App unit test
  21. *
  22. * \author Jeff Peeler <jpeeler@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/utils.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/test.h"
  34. #include "asterisk/app.h"
  35. #include "asterisk/channel.h"
  36. #define BASE_GROUP "a group"
  37. AST_TEST_DEFINE(options_parsing)
  38. {
  39. enum test_option_flags {
  40. OPT_SIMPLE,
  41. OPT_WITHQUOTES,
  42. OPT_WITHBACKSLASH,
  43. };
  44. enum test_option_args {
  45. OPT_ARG_SIMPLE,
  46. OPT_ARG_WITHQUOTES,
  47. OPT_ARG_WITHBACKSLASH,
  48. OPT_ARG_ARRAY_SIZE,
  49. };
  50. AST_APP_OPTIONS(test_options, {
  51. AST_APP_OPTION_ARG('a', OPT_SIMPLE, OPT_ARG_SIMPLE),
  52. AST_APP_OPTION_ARG('b', OPT_WITHQUOTES, OPT_ARG_WITHQUOTES),
  53. AST_APP_OPTION_ARG('c', OPT_WITHBACKSLASH, OPT_ARG_WITHBACKSLASH),
  54. });
  55. struct ast_flags opts = { 0, };
  56. struct ast_flags64 opts64 = { 0, };
  57. char *opt_args[OPT_ARG_ARRAY_SIZE];
  58. struct {
  59. const char *string;
  60. const char *parse[3];
  61. } options[] = {
  62. { "a(simple)b(\"quoted\")c(back\\slash)", { "simple", "quoted", "backslash", }, },
  63. { "b(\"((())))\")a(simple)c(back\\)slash)", { "simple", "((())))", "back)slash", }, },
  64. { "b(\"((\\\"\\)\\(\")a(simple)c(back\\\"\\)\\(\\\"slash)", { "simple", "((\"\\)\\(", "back\")(\"slash", }, },
  65. };
  66. int i, j, res = AST_TEST_PASS;
  67. char buffer[256];
  68. switch (cmd) {
  69. case TEST_INIT:
  70. info->name = "options_parsing";
  71. info->category = "/main/app/";
  72. info->summary = "App options unit test";
  73. info->description =
  74. "This tests the options parsing code to ensure that it behaves as expected";
  75. return AST_TEST_NOT_RUN;
  76. case TEST_EXECUTE:
  77. break;
  78. }
  79. for (i = 0; i < ARRAY_LEN(options); i++) {
  80. ast_copy_string(buffer, options[i].string, sizeof(buffer));
  81. if (ast_app_parse_options(test_options, &opts, opt_args, buffer)) {
  82. ast_test_status_update(test, "ast_app_parse_options() of '%s' failed\n", options[i].string);
  83. res = AST_TEST_FAIL;
  84. } else {
  85. /* Check arguments for success */
  86. for (j = 0; j < 3; j++) {
  87. if (strcmp(opt_args[j], options[i].parse[j])) {
  88. ast_test_status_update(test, "Parse of option %c from '%s' produced '%s', "
  89. "but it should have produced '%s'\n",
  90. 'a' + j, options[i].string, opt_args[j], options[i].parse[j]);
  91. res = AST_TEST_FAIL;
  92. }
  93. }
  94. }
  95. ast_copy_string(buffer, options[i].string, sizeof(buffer));
  96. if (ast_app_parse_options64(test_options, &opts64, opt_args, buffer)) {
  97. ast_test_status_update(test, "ast_app_parse_options64() of '%s' failed\n", options[i].string);
  98. res = AST_TEST_FAIL;
  99. } else {
  100. /* Check arguments for success */
  101. for (j = 0; j < 3; j++) {
  102. if (strcmp(opt_args[j], options[i].parse[j])) {
  103. ast_test_status_update(test, "Parse of option %c from '%s' produced '%s', "
  104. "but it should have produced '%s'\n",
  105. 'a' + j, options[i].string, opt_args[j], options[i].parse[j]);
  106. res = AST_TEST_FAIL;
  107. }
  108. }
  109. }
  110. }
  111. return res;
  112. }
  113. AST_TEST_DEFINE(app_group)
  114. {
  115. struct ast_channel *test_channel1 = NULL;
  116. struct ast_channel *test_channel2 = NULL;
  117. struct ast_channel *test_channel3 = NULL;
  118. struct ast_channel *test_channel4 = NULL;
  119. static const char group1_full[] = BASE_GROUP "groupgroup";
  120. static const char group2_full[] = BASE_GROUP "Groupgroup";
  121. static const char regex1[] = "gr"; /* matches everything */
  122. static const char regex2[] = "(group){2}$"; /* matches only group1_full */
  123. static const char regex3[] = "[:ascii:]"; /* matches everything */
  124. static const char regex4[] = "^(NOMATCH)"; /* matches nothing */
  125. static const char category1_full[] = BASE_GROUP "@a_category"; /* categories shouldn't have spaces */
  126. static const char category2_full[] = BASE_GROUP "@another!Category";
  127. static const char regex5[] = "(gory)$"; /* matches both categories */
  128. static const char regex6[] = "[A-Z]+"; /* matches only category2_full */
  129. static const char regex7[] = "[["; /* not valid syntax, yes an expected warning will be displayed */
  130. static enum ast_test_result_state res = AST_TEST_PASS;
  131. static const struct group_test_params {
  132. const char *groupmatch;
  133. const char *category;
  134. int expected;
  135. } subtests[] = {
  136. { regex1, "", 4 },
  137. { regex2, "", 1 },
  138. { regex3, "", 4 },
  139. { regex4, "", 0 },
  140. { BASE_GROUP, regex5, 2 },
  141. { BASE_GROUP, regex6, 1 },
  142. /* this test is expected to generate a warning message from the invalid regex */
  143. { BASE_GROUP, regex7, 0 }
  144. };
  145. int i;
  146. int returned_count;
  147. switch (cmd) {
  148. case TEST_INIT:
  149. info->name = "app_group";
  150. info->category = "/main/app/";
  151. info->summary = "App group unit test";
  152. info->description =
  153. "This tests various app group functionality";
  154. return AST_TEST_NOT_RUN;
  155. case TEST_EXECUTE:
  156. break;
  157. }
  158. ast_test_status_update(test, "Creating test channels with the following groups:\n"
  159. "'%s', '%s', '%s', '%s'\n", group1_full, group2_full, category1_full, category2_full);
  160. if (!(test_channel1 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
  161. NULL, NULL, NULL, NULL, 0, "TestChannel1"))) {
  162. goto exit_group_test;
  163. }
  164. ast_channel_unlock(test_channel1);
  165. if (!(test_channel2 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
  166. NULL, NULL, NULL, NULL, 0, "TestChannel2"))) {
  167. goto exit_group_test;
  168. }
  169. ast_channel_unlock(test_channel2);
  170. if (!(test_channel3 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
  171. NULL, NULL, NULL, NULL, 0, "TestChannel3"))) {
  172. goto exit_group_test;
  173. }
  174. ast_channel_unlock(test_channel3);
  175. if (!(test_channel4 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
  176. NULL, NULL, NULL, NULL, 0, "TestChannel4"))) {
  177. goto exit_group_test;
  178. }
  179. ast_channel_unlock(test_channel4);
  180. ast_app_group_set_channel(test_channel1, group1_full);
  181. ast_app_group_set_channel(test_channel2, group2_full);
  182. ast_app_group_set_channel(test_channel3, category1_full);
  183. ast_app_group_set_channel(test_channel4, category2_full);
  184. for (i = 0; i < ARRAY_LEN(subtests); i++) {
  185. ast_assert(subtests[i].groupmatch != NULL || subtests[i].category != NULL);
  186. returned_count = ast_app_group_match_get_count(subtests[i].groupmatch, subtests[i].category);
  187. if (subtests[i].expected != returned_count) {
  188. ast_test_status_update(test, "(Subtest %d) Expected %d matches but found %d when examining group:'%s' category:'%s'\n",
  189. i + 1, subtests[i].expected, returned_count, subtests[i].groupmatch, subtests[i].category);
  190. res = AST_TEST_FAIL;
  191. goto exit_group_test;
  192. } else {
  193. ast_test_status_update(test, "(Subtest %d) Found %d matches as expected when examining group:'%s' category:'%s'\n",
  194. i + 1, subtests[i].expected, subtests[i].groupmatch, subtests[i].category);
  195. }
  196. }
  197. exit_group_test:
  198. ast_hangup(test_channel1);
  199. ast_hangup(test_channel2);
  200. ast_hangup(test_channel3);
  201. ast_hangup(test_channel4);
  202. return res;
  203. }
  204. static int unload_module(void)
  205. {
  206. AST_TEST_UNREGISTER(app_group);
  207. AST_TEST_UNREGISTER(options_parsing);
  208. return 0;
  209. }
  210. static int load_module(void)
  211. {
  212. AST_TEST_REGISTER(app_group);
  213. AST_TEST_REGISTER(options_parsing);
  214. return AST_MODULE_LOAD_SUCCESS;
  215. }
  216. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "App unit tests");