test_netsock2.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "")
  30. #include "asterisk/test.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/netsock2.h"
  33. #include "asterisk/logger.h"
  34. struct parse_test {
  35. const char *address;
  36. int expected_result;
  37. };
  38. AST_TEST_DEFINE(parsing)
  39. {
  40. int res = AST_TEST_PASS;
  41. struct parse_test test_vals[] = {
  42. { "192.168.1.0", 1 },
  43. { "10.255.255.254", 1 },
  44. { "172.18.5.4", 1 },
  45. { "8.8.4.4", 1 },
  46. { "0.0.0.0", 1 },
  47. { "127.0.0.1", 1 },
  48. { "1.256.3.4", 0 },
  49. { "256.0.0.1", 0 },
  50. { "1.2.3.4:5060", 1 },
  51. { "::ffff:5.6.7.8", 1 },
  52. { "fdf8:f53b:82e4::53", 1 },
  53. { "fe80::200:5aee:feaa:20a2", 1 },
  54. { "2001::1", 1 },
  55. { "2001:0000:4136:e378:8000:63bf:3fff:fdd2", 1 },
  56. { "2001:0002:6c::430", 1 },
  57. { "2001:10:240:ab::a", 1 },
  58. { "2002:cb0a:3cdd:1::1", 1 },
  59. { "2001:db8:8:4::2", 1 }, /* Documentation only, should never be used */
  60. { "ff01:0:0:0:0:0:0:2", 1 }, /* Multicast */
  61. { "[fdf8:f53b:82e4::53]", 1 },
  62. { "[fe80::200:5aee:feaa:20a2]", 1 },
  63. { "[2001::1]", 1 },
  64. { "[2001:0000:4136:e378:8000:63bf:3fff:fdd2]:5060", 1 },
  65. { "2001:0000:4136:e378:8000:63bf:3fff:fdd2:5060", 0 }, /* port, but no brackets */
  66. { "[fe80::200::abcd", 0 }, /* multiple zero expansions */
  67. };
  68. size_t x;
  69. struct ast_sockaddr addr = { { 0, 0, } };
  70. int parse_result;
  71. switch (cmd) {
  72. case TEST_INIT:
  73. info->name = "parsing";
  74. info->category = "/main/netsock2/";
  75. info->summary = "netsock2 parsing unit test";
  76. info->description =
  77. "Test parsing of IPv4 and IPv6 network addresses";
  78. return AST_TEST_NOT_RUN;
  79. case TEST_EXECUTE:
  80. break;
  81. }
  82. for (x = 0; x < ARRAY_LEN(test_vals); x++) {
  83. if ((parse_result = ast_sockaddr_parse(&addr, test_vals[x].address, 0)) != test_vals[x].expected_result) {
  84. ast_test_status_update(test, "On '%s' expected %d but got %d\n", test_vals[x].address, test_vals[x].expected_result, parse_result);
  85. res = AST_TEST_FAIL;
  86. }
  87. if (parse_result) {
  88. struct ast_sockaddr tmp_addr = { { 0, 0, } };
  89. const char *tmp;
  90. tmp = ast_sockaddr_stringify(&addr);
  91. ast_sockaddr_parse(&tmp_addr, tmp, 0);
  92. if (ast_sockaddr_cmp_addr(&addr, &tmp_addr)) {
  93. char buf[64];
  94. ast_copy_string(buf, ast_sockaddr_stringify(&addr), sizeof(buf));
  95. 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));
  96. res = AST_TEST_FAIL;
  97. }
  98. }
  99. }
  100. return res;
  101. }
  102. static int unload_module(void)
  103. {
  104. AST_TEST_UNREGISTER(parsing);
  105. return 0;
  106. }
  107. static int load_module(void)
  108. {
  109. AST_TEST_REGISTER(parsing);
  110. return AST_MODULE_LOAD_SUCCESS;
  111. }
  112. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Netsock2 test module");