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