2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2011, Digium, Inc.
6 * Terry Wilson <twilson@digium.com>
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.
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.
21 * \brief Netsock2 Unit Tests
23 * \author Terry Wilson <twilson@digium.com>
28 <depend>TEST_FRAMEWORK</depend>
29 <support_level>core</support_level>
34 ASTERISK_FILE_VERSION(__FILE__, "")
36 #include "asterisk/test.h"
37 #include "asterisk/module.h"
38 #include "asterisk/netsock2.h"
39 #include "asterisk/logger.h"
45 AST_TEST_DEFINE(parsing)
47 int res = AST_TEST_PASS;
48 struct parse_test test_vals[] = {
50 { "10.255.255.254", 1 },
57 { "1.2.3.4:5060", 1 },
58 { "::ffff:5.6.7.8", 1 },
59 { "fdf8:f53b:82e4::53", 1 },
60 { "fe80::200:5aee:feaa:20a2", 1 },
62 { "2001:0000:4136:e378:8000:63bf:3fff:fdd2", 1 },
63 { "2001:0002:6c::430", 1 },
64 { "2001:10:240:ab::a", 1 },
65 { "2002:cb0a:3cdd:1::1", 1 },
66 { "2001:db8:8:4::2", 1 }, /* Documentation only, should never be used */
67 { "ff01:0:0:0:0:0:0:2", 1 }, /* Multicast */
68 { "[fdf8:f53b:82e4::53]", 1 },
69 { "[fe80::200:5aee:feaa:20a2]", 1 },
71 { "[2001:0000:4136:e378:8000:63bf:3fff:fdd2]:5060", 1 },
72 { "2001:0000:4136:e378:8000:63bf:3fff:fdd2:5060", 0 }, /* port, but no brackets */
73 { "fe80::200::abcd", 0 }, /* multiple zero expansions */
77 struct ast_sockaddr addr = { { 0, 0, } };
82 info->name = "parsing";
83 info->category = "/main/netsock2/";
84 info->summary = "netsock2 parsing unit test";
86 "Test parsing of IPv4 and IPv6 network addresses";
87 return AST_TEST_NOT_RUN;
92 for (x = 0; x < ARRAY_LEN(test_vals); x++) {
93 if ((parse_result = ast_sockaddr_parse(&addr, test_vals[x].address, 0)) != test_vals[x].expected_result) {
94 ast_test_status_update(test, "On '%s' expected %d but got %d\n", test_vals[x].address, test_vals[x].expected_result, parse_result);
98 struct ast_sockaddr tmp_addr = { { 0, 0, } };
101 tmp = ast_sockaddr_stringify(&addr);
102 ast_sockaddr_parse(&tmp_addr, tmp, 0);
103 if (ast_sockaddr_cmp_addr(&addr, &tmp_addr)) {
105 ast_copy_string(buf, ast_sockaddr_stringify(&addr), sizeof(buf));
106 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));
115 static int unload_module(void)
117 AST_TEST_UNREGISTER(parsing);
121 static int load_module(void)
123 AST_TEST_REGISTER(parsing);
124 return AST_MODULE_LOAD_SUCCESS;
127 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Netsock2 test module");