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>
33 ASTERISK_FILE_VERSION(__FILE__, "")
35 #include "asterisk/test.h"
36 #include "asterisk/module.h"
37 #include "asterisk/netsock2.h"
38 #include "asterisk/logger.h"
44 AST_TEST_DEFINE(parsing)
46 int res = AST_TEST_PASS;
47 struct parse_test test_vals[] = {
49 { "10.255.255.254", 1 },
56 { "1.2.3.4:5060", 1 },
57 { "::ffff:5.6.7.8", 1 },
58 { "fdf8:f53b:82e4::53", 1 },
59 { "fe80::200:5aee:feaa:20a2", 1 },
61 { "2001:0000:4136:e378:8000:63bf:3fff:fdd2", 1 },
62 { "2001:0002:6c::430", 1 },
63 { "2001:10:240:ab::a", 1 },
64 { "2002:cb0a:3cdd:1::1", 1 },
65 { "2001:db8:8:4::2", 1 }, /* Documentation only, should never be used */
66 { "ff01:0:0:0:0:0:0:2", 1 }, /* Multicast */
67 { "[fdf8:f53b:82e4::53]", 1 },
68 { "[fe80::200:5aee:feaa:20a2]", 1 },
70 { "[2001:0000:4136:e378:8000:63bf:3fff:fdd2]:5060", 1 },
71 { "2001:0000:4136:e378:8000:63bf:3fff:fdd2:5060", 0 }, /* port, but no brackets */
72 { "[fe80::200:5aee:feaa:20a2%eth0]", 1 }, /* link-local with scope id */
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)) {
104 ast_test_status_update(test, "Re-parsed stringification did not match: '%s' vs '%s'\n", ast_sockaddr_stringify(&addr), ast_sockaddr_stringify(&tmp_addr));
113 static int unload_module(void)
115 AST_TEST_UNREGISTER(parsing);
119 static int load_module(void)
121 AST_TEST_REGISTER(parsing);
122 return AST_MODULE_LOAD_SUCCESS;
125 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Netsock2 test module");