Merged revisions 324557 via svnmerge from
[asterisk/asterisk.git] / tests / test_netsock2.c
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 /*!
20  * \file
21  * \brief Netsock2 Unit Tests
22  *
23  * \author Terry Wilson <twilson@digium.com>
24  *
25  */
26
27 /*** MODULEINFO
28         <depend>TEST_FRAMEWORK</depend>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "")
34
35 #include "asterisk/test.h"
36 #include "asterisk/module.h"
37 #include "asterisk/netsock2.h"
38 #include "asterisk/logger.h"
39 struct parse_test {
40         const char *address;
41         int expected_result;
42 };
43
44 AST_TEST_DEFINE(parsing)
45 {
46         int res = AST_TEST_PASS;
47         struct parse_test test_vals[] = {
48                 { "192.168.1.0", 1 },
49                 { "10.255.255.254", 1 },
50                 { "172.18.5.4", 1 },
51                 { "8.8.4.4", 1 },
52                 { "0.0.0.0", 1 },
53                 { "127.0.0.1", 1 },
54                 { "1.256.3.4", 0 },
55                 { "256.0.0.1", 0 },
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 },
60                 { "2001::1", 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 },
69                 { "[2001::1]", 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 */
74         };
75
76         size_t x;
77         struct ast_sockaddr addr = { { 0, 0, } };
78         int parse_result;
79
80         switch (cmd) {
81         case TEST_INIT:
82                 info->name = "parsing";
83                 info->category = "/main/netsock2/";
84                 info->summary = "netsock2 parsing unit test";
85                 info->description =
86                         "Test parsing of IPv4 and IPv6 network addresses";
87                 return AST_TEST_NOT_RUN;
88         case TEST_EXECUTE:
89                 break;
90         }
91
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);
95                         res = AST_TEST_FAIL;
96                 }
97                 if (parse_result) {
98                         struct ast_sockaddr tmp_addr = { { 0, 0, } };
99                         const char *tmp;
100
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));
105                                 res = AST_TEST_FAIL;
106                         }
107                 }
108         }
109
110         return res;
111 }
112
113 static int unload_module(void)
114 {
115         AST_TEST_UNREGISTER(parsing);
116         return 0;
117 }
118
119 static int load_module(void)
120 {
121         AST_TEST_REGISTER(parsing);
122         return AST_MODULE_LOAD_SUCCESS;
123 }
124
125 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Netsock2 test module");