test_netsock2.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011, Digium, Inc.
  5. *
  6. * Terry Wilson <twilson@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 Netsock2 Unit Tests
  21. *
  22. * \author Terry Wilson <twilson@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__, "")
  31. #include "asterisk/test.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/netsock2.h"
  34. #include "asterisk/logger.h"
  35. #include "asterisk/config.h" /* PARSE_PORT_* */
  36. struct parse_test {
  37. const char *address;
  38. int expected_result;
  39. };
  40. AST_TEST_DEFINE(parsing)
  41. {
  42. int res = AST_TEST_PASS;
  43. struct parse_test test_vals[] = {
  44. { "192.168.1.0", 1 },
  45. { "10.255.255.254", 1 },
  46. { "172.18.5.4", 1 },
  47. { "8.8.4.4", 1 },
  48. { "0.0.0.0", 1 },
  49. { "127.0.0.1", 1 },
  50. { "1.256.3.4", 0 },
  51. { "256.0.0.1", 0 },
  52. { "1.2.3.4:5060", 1 },
  53. { "::ffff:5.6.7.8", 1 },
  54. { "fdf8:f53b:82e4::53", 1 },
  55. { "fe80::200:5aee:feaa:20a2", 1 },
  56. { "2001::1", 1 },
  57. { "2001:0000:4136:e378:8000:63bf:3fff:fdd2", 1 },
  58. { "2001:0002:6c::430", 1 },
  59. { "2001:10:240:ab::a", 1 },
  60. { "2002:cb0a:3cdd:1::1", 1 },
  61. { "2001:db8:8:4::2", 1 }, /* Documentation only, should never be used */
  62. { "ff01:0:0:0:0:0:0:2", 1 }, /* Multicast */
  63. { "[fdf8:f53b:82e4::53]", 1 },
  64. { "[fe80::200:5aee:feaa:20a2]", 1 },
  65. { "[2001::1]", 1 },
  66. { "[2001:0000:4136:e378:8000:63bf:3fff:fdd2]:5060", 1 },
  67. { "2001:0000:4136:e378:8000:63bf:3fff:fdd2:5060", 0 }, /* port, but no brackets */
  68. { "fe80::200::abcd", 0 }, /* multiple zero expansions */
  69. };
  70. size_t x;
  71. struct ast_sockaddr addr;
  72. int parse_result;
  73. switch (cmd) {
  74. case TEST_INIT:
  75. info->name = "parsing";
  76. info->category = "/main/netsock2/";
  77. info->summary = "netsock2 parsing unit test";
  78. info->description =
  79. "Test parsing of IPv4 and IPv6 network addresses";
  80. return AST_TEST_NOT_RUN;
  81. case TEST_EXECUTE:
  82. break;
  83. }
  84. for (x = 0; x < ARRAY_LEN(test_vals); x++) {
  85. memset(&addr, 0, sizeof(addr));
  86. if ((parse_result = ast_sockaddr_parse(&addr, test_vals[x].address, 0)) != test_vals[x].expected_result) {
  87. ast_test_status_update(test, "On '%s' expected %d but got %d\n", test_vals[x].address, test_vals[x].expected_result, parse_result);
  88. res = AST_TEST_FAIL;
  89. }
  90. if (parse_result) {
  91. struct ast_sockaddr tmp_addr;
  92. const char *tmp;
  93. tmp = ast_sockaddr_stringify(&addr);
  94. memset(&tmp_addr, 0, sizeof(tmp_addr));
  95. ast_sockaddr_parse(&tmp_addr, tmp, 0);
  96. if (ast_sockaddr_cmp_addr(&addr, &tmp_addr)) {
  97. char buf[64];
  98. ast_copy_string(buf, ast_sockaddr_stringify(&addr), sizeof(buf));
  99. ast_test_status_update(test, "Re-parsed stringification of '%s' did not match: '%s' vs '%s'\n", test_vals[x].address, buf, ast_sockaddr_stringify(&tmp_addr));
  100. res = AST_TEST_FAIL;
  101. }
  102. }
  103. }
  104. return res;
  105. }
  106. AST_TEST_DEFINE(split_hostport)
  107. {
  108. int res = AST_TEST_PASS;
  109. char *host, *port, buf[128];
  110. switch (cmd) {
  111. case TEST_INIT:
  112. info->name = "split_hostport";
  113. info->category = "/main/netsock2/";
  114. info->summary = "netsock2 ast_sockaddr_split_hostport() unit test";
  115. info->description =
  116. "Test splitting of IPv4 and IPv6 host:port strings";
  117. return AST_TEST_NOT_RUN;
  118. case TEST_EXECUTE:
  119. break;
  120. }
  121. /* Assumes res, host, and port variables */
  122. #define TEST_SPLIT_HOSTPORT(str, flags, expected_host, expected_port, expected_result) { \
  123. int __res; \
  124. ast_copy_string(buf, str, sizeof(buf)); \
  125. if ((__res = ast_sockaddr_split_hostport(buf, &host, &port, flags)) != expected_result || ( \
  126. expected_result && ( \
  127. strcmp(host, expected_host) || ( \
  128. ast_strlen_zero(expected_port) ? !ast_strlen_zero(port) : (!ast_strlen_zero(port) && strcmp(port, expected_port)) \
  129. ) \
  130. ) \
  131. ) \
  132. ) { \
  133. res = AST_TEST_FAIL; \
  134. if (__res != expected_result) { \
  135. ast_test_status_update(test, "Expected %d, got %d\n", expected_result, __res); \
  136. } else { \
  137. ast_test_status_update(test, "Failed parsing '%s' into expected host '%s' (got '%s') and port '%s' (got '%s')\n", \
  138. str, S_OR(expected_host, ""), host, expected_port, S_OR(port, "")); \
  139. } \
  140. } \
  141. }
  142. /* Test various situations with flags = 0 */
  143. TEST_SPLIT_HOSTPORT("192.168.1.1", 0, "192.168.1.1", "", 1);
  144. TEST_SPLIT_HOSTPORT("192.168.1.1:5060", 0, "192.168.1.1", "5060", 1);
  145. TEST_SPLIT_HOSTPORT("::ffff:5.6.7.8", 0, "::ffff:5.6.7.8", "", 1);
  146. TEST_SPLIT_HOSTPORT("[::ffff:5.6.7.8]:5060", 0, "::ffff:5.6.7.8", "5060", 1);
  147. TEST_SPLIT_HOSTPORT("fdf8:f53b:82e4::53", 0, "fdf8:f53b:82e4::53", "", 1);
  148. TEST_SPLIT_HOSTPORT("fe80::200:5aee:feaa:20a2", 0, "fe80::200:5aee:feaa:20a2", "", 1);
  149. TEST_SPLIT_HOSTPORT("[fdf8:f53b:82e4::53]", 0, "fdf8:f53b:82e4::53", "", 1);
  150. TEST_SPLIT_HOSTPORT("[fe80::200:5aee:feaa:20a2]:5060", 0, "fe80::200:5aee:feaa:20a2", "5060", 1);
  151. TEST_SPLIT_HOSTPORT("host:port", 0, "host", "port", 1);
  152. TEST_SPLIT_HOSTPORT("host", 0, "host", "", 1);
  153. /* Make sure that flag conditions work when they should */
  154. TEST_SPLIT_HOSTPORT("192.168.1.1:5060", PARSE_PORT_IGNORE, "192.168.1.1", "", 1);
  155. TEST_SPLIT_HOSTPORT("192.168.1.1:5060", PARSE_PORT_REQUIRE, "192.168.1.1", "5060", 1);
  156. TEST_SPLIT_HOSTPORT("192.168.1.1", PARSE_PORT_FORBID, "192.168.1.1", "", 1);
  157. TEST_SPLIT_HOSTPORT("[::ffff:5.6.7.8]:5060", PARSE_PORT_IGNORE, "::ffff:5.6.7.8", "", 1);
  158. TEST_SPLIT_HOSTPORT("[::ffff:5.6.7.8]:5060", PARSE_PORT_REQUIRE, "::ffff:5.6.7.8", "5060", 1);
  159. TEST_SPLIT_HOSTPORT("::ffff:5.6.7.8", PARSE_PORT_FORBID, "::ffff:5.6.7.8", "", 1);
  160. /* Make sure it fails when flag requirements are not met */
  161. TEST_SPLIT_HOSTPORT("192.168.1.1", PARSE_PORT_REQUIRE, "<undefined>", "<undefined>", 0);
  162. TEST_SPLIT_HOSTPORT("192.168.1.1:5060", PARSE_PORT_FORBID, "<undefined>", "<undefined>", 0);
  163. TEST_SPLIT_HOSTPORT("::ffff:5.6.7.8", PARSE_PORT_REQUIRE, "<undefined>", "<undefined>", 0);
  164. TEST_SPLIT_HOSTPORT("[::ffff:5.6.7.8]:5060", PARSE_PORT_FORBID, "<undefined>", "<undefined>", 0);
  165. return res;
  166. }
  167. static int unload_module(void)
  168. {
  169. AST_TEST_UNREGISTER(parsing);
  170. AST_TEST_UNREGISTER(split_hostport);
  171. return 0;
  172. }
  173. static int load_module(void)
  174. {
  175. AST_TEST_REGISTER(parsing);
  176. AST_TEST_REGISTER(split_hostport);
  177. return AST_MODULE_LOAD_SUCCESS;
  178. }
  179. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Netsock2 test module");