test_app.c 7.5 KB

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