Asterisk - The Open Source Telephony Project GIT-master-f36a736
Data Structures | Macros | Functions | Variables
test_netsock2.c File Reference

Netsock2 Unit Tests. More...

#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/netsock2.h"
#include "asterisk/logger.h"
#include "asterisk/config.h"
Include dependency graph for test_netsock2.c:

Go to the source code of this file.

Data Structures

struct  parse_test
 

Macros

#define TEST_SPLIT_HOSTPORT(str, flags, expected_host, expected_port, expected_result)
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (parsing)
 
 AST_TEST_DEFINE (split_hostport)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Netsock2 test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Netsock2 Unit Tests.

Author
Terry Wilson twils.nosp@m.on@d.nosp@m.igium.nosp@m..com

Definition in file test_netsock2.c.

Macro Definition Documentation

◆ TEST_SPLIT_HOSTPORT

#define TEST_SPLIT_HOSTPORT (   str,
  flags,
  expected_host,
  expected_port,
  expected_result 
)

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 198 of file test_netsock2.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 198 of file test_netsock2.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 198 of file test_netsock2.c.

◆ AST_TEST_DEFINE() [1/2]

AST_TEST_DEFINE ( parsing  )

Definition at line 44 of file test_netsock2.c.

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::abcd", 0 }, /* multiple zero expansions */
73 };
74
75 size_t x;
76 struct ast_sockaddr addr;
77 int parse_result;
78
79 switch (cmd) {
80 case TEST_INIT:
81 info->name = "parsing";
82 info->category = "/main/netsock2/";
83 info->summary = "netsock2 parsing unit test";
84 info->description =
85 "Test parsing of IPv4 and IPv6 network addresses";
86 return AST_TEST_NOT_RUN;
87 case TEST_EXECUTE:
88 break;
89 }
90
91 for (x = 0; x < ARRAY_LEN(test_vals); x++) {
92 memset(&addr, 0, sizeof(addr));
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;
99 const char *tmp;
100
102 memset(&tmp_addr, 0, sizeof(tmp_addr));
103 ast_sockaddr_parse(&tmp_addr, tmp, 0);
104 if (ast_sockaddr_cmp_addr(&addr, &tmp_addr)) {
105 char buf[64];
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}
static int tmp()
Definition: bt_open.c:389
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
char * address
Definition: f2c.h:59
def info(msg)
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:256
int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares the addresses of two ast_sockaddr structures.
Definition: netsock2.c:413
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Socket address structure.
Definition: netsock2.h:97
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_copy_string(), ast_sockaddr_cmp_addr(), ast_sockaddr_parse(), ast_sockaddr_stringify(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, buf, sip_to_pjsip::info(), TEST_EXECUTE, TEST_INIT, and tmp().

◆ AST_TEST_DEFINE() [2/2]

AST_TEST_DEFINE ( split_hostport  )

Definition at line 116 of file test_netsock2.c.

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}
@ PARSE_PORT_REQUIRE
#define TEST_SPLIT_HOSTPORT(str, flags, expected_host, expected_port, expected_result)

References AST_TEST_NOT_RUN, AST_TEST_PASS, buf, sip_to_pjsip::info(), PARSE_PORT_FORBID, PARSE_PORT_IGNORE, PARSE_PORT_REQUIRE, TEST_EXECUTE, TEST_INIT, and TEST_SPLIT_HOSTPORT.

◆ load_module()

static int load_module ( void  )
static

Definition at line 191 of file test_netsock2.c.

192{
196}
static void parsing(int size, unsigned char *buf, struct unistimsession *pte, struct sockaddr_in *addr_from)
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
def split_hostport(addr)
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

References AST_MODULE_LOAD_SUCCESS, AST_TEST_REGISTER, parsing(), and sip_to_pjsip::split_hostport().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 184 of file test_netsock2.c.

185{
188 return 0;
189}
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

References AST_TEST_UNREGISTER, parsing(), and sip_to_pjsip::split_hostport().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Netsock2 test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 198 of file test_netsock2.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 198 of file test_netsock2.c.