test_callerid.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Kinsey Moore
  5. *
  6. * Kinsey Moore <kmoore@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 Callerid Tests
  21. *
  22. * \author\verbatim Kinsey Moore <kmoore@digium.com> \endverbatim
  23. *
  24. * This is an Asterisk test module for callerid functionality
  25. * \ingroup tests
  26. */
  27. /*** MODULEINFO
  28. <depend>TEST_FRAMEWORK</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. #include "asterisk/callerid.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/utils.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/test.h"
  37. struct cid_set {
  38. char *cid;
  39. char *name;
  40. char *number;
  41. };
  42. AST_TEST_DEFINE(parse_nominal)
  43. {
  44. static const struct cid_set cid_sets[] = {
  45. {"\"name\" <number>", "name", "number"},
  46. {"\" name \" <number>", " name ", "number"},
  47. {"name <number>", "name", "number"},
  48. {" name <number>", "name", "number"},
  49. {"\"\" <number>", NULL, "number"},
  50. {"<number>", NULL, "number"},
  51. {"name", "name", NULL},
  52. {" name", "name", NULL},
  53. {"\"name\"", "name", NULL},
  54. {"\"*10\"", "*10", NULL},
  55. {" \"*10\"", "*10", NULL},
  56. {"\"name\" <>", "name", NULL},
  57. {"name <>", "name", NULL},
  58. {"1234", NULL, "1234"},
  59. {" 1234", NULL, "1234"},
  60. {"\"na\\\"me\" <number>", "na\"me", "number"},
  61. };
  62. char *name;
  63. char *number;
  64. int i;
  65. switch (cmd) {
  66. case TEST_INIT:
  67. info->name = "parse_nominal";
  68. info->category = "/main/callerid/";
  69. info->summary = "Callerid nominal parse unit test";
  70. info->description =
  71. "This tests parsing of nominal callerid strings.";
  72. return AST_TEST_NOT_RUN;
  73. case TEST_EXECUTE:
  74. break;
  75. }
  76. for (i = 0; i < ARRAY_LEN(cid_sets); i++) {
  77. RAII_VAR(char *, callerid, ast_strdup(cid_sets[i].cid), ast_free);
  78. ast_callerid_parse(callerid, &name, &number);
  79. if (!cid_sets[i].name == !ast_strlen_zero(name) || (cid_sets[i].name && strcmp(name, cid_sets[i].name))) {
  80. ast_test_status_update(test,
  81. "Expected callerid name '%s' instead of '%s'\n",
  82. cid_sets[i].name, name);
  83. return AST_TEST_FAIL;
  84. }
  85. if (!cid_sets[i].number == !ast_strlen_zero(number) || (cid_sets[i].number && strcmp(number, cid_sets[i].number))) {
  86. ast_test_status_update(test,
  87. "Expected callerid number '%s' instead of '%s'\n",
  88. cid_sets[i].number, number);
  89. return AST_TEST_FAIL;
  90. }
  91. }
  92. return AST_TEST_PASS;
  93. }
  94. AST_TEST_DEFINE(parse_off_nominal)
  95. {
  96. static const struct cid_set cid_sets[] = {
  97. {"na\\\"me <number>", "na\"me", "number"},
  98. {"\"na\"me\" <number>", "na\"me", "number"},
  99. {"na\"me <number>", "na\"me", "number"},
  100. {"\"name <number>", "\"name", "number"},
  101. {"name <number", "name", "number"},
  102. {"\"name <number>\"", "name", "number"},
  103. };
  104. char *name;
  105. char *number;
  106. int i;
  107. switch (cmd) {
  108. case TEST_INIT:
  109. info->name = "parse_off_nominal";
  110. info->category = "/main/callerid/";
  111. info->summary = "Callerid off-nominal parse unit test";
  112. info->description =
  113. "This tests parsing of off-nominal callerid strings.";
  114. return AST_TEST_NOT_RUN;
  115. case TEST_EXECUTE:
  116. break;
  117. }
  118. for (i = 0; i < ARRAY_LEN(cid_sets); i++) {
  119. RAII_VAR(char *, callerid, ast_strdup(cid_sets[i].cid), ast_free);
  120. ast_callerid_parse(callerid, &name, &number);
  121. if (!cid_sets[i].name == !ast_strlen_zero(name) || (cid_sets[i].name && strcmp(name, cid_sets[i].name))) {
  122. ast_test_status_update(test,
  123. "Expected callerid name '%s' instead of '%s'\n",
  124. cid_sets[i].name, name);
  125. return AST_TEST_FAIL;
  126. }
  127. if (!cid_sets[i].number == !ast_strlen_zero(number) || (cid_sets[i].number && strcmp(number, cid_sets[i].number))) {
  128. ast_test_status_update(test,
  129. "Expected callerid number '%s' instead of '%s'\n",
  130. cid_sets[i].number, number);
  131. return AST_TEST_FAIL;
  132. }
  133. }
  134. return AST_TEST_PASS;
  135. }
  136. static int unload_module(void)
  137. {
  138. AST_TEST_UNREGISTER(parse_nominal);
  139. AST_TEST_UNREGISTER(parse_off_nominal);
  140. return 0;
  141. }
  142. static int load_module(void)
  143. {
  144. AST_TEST_REGISTER(parse_nominal);
  145. AST_TEST_REGISTER(parse_off_nominal);
  146. return AST_MODULE_LOAD_SUCCESS;
  147. }
  148. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Callerid Parse Tests");