asterisk/tests/test_netsock2.c

199 lines
6.5 KiB
C
Raw Normal View History

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2011, Digium, Inc.
*
* Terry Wilson <twilson@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
* \brief Netsock2 Unit Tests
*
* \author Terry Wilson <twilson@digium.com>
*
*/
/*** MODULEINFO
<depend>TEST_FRAMEWORK</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
git migration: Refactor the ASTERISK_FILE_VERSION macro Git does not support the ability to replace a token with a version string during check-in. While it does have support for replacing a token on clone, this is somewhat sub-optimal: the token is replaced with the object hash, which is not particularly easy for human consumption. What's more, in practice, the source file version was often not terribly useful. Generally, when triaging bugs, the overall version of Asterisk is far more useful than an individual SVN version of a file. As a result, this patch removes Asterisk's support for showing source file versions. Specifically, it does the following: * Rename ASTERISK_FILE_VERSION macro to ASTERISK_REGISTER_FILE, and remove passing the version in with the macro. Other facilities than 'core show file version' make use of the file names, such as setting a debug level only on a specific file. As such, the act of registering source files with the Asterisk core still has use. The macro rename now reflects the new macro purpose. * main/asterisk: - Refactor the file_version structure to reflect that it no longer tracks a version field. - Remove the "core show file version" CLI command. Without the file version, it is no longer useful. - Remove the ast_file_version_find function. The file version is no longer tracked. - Rename ast_register_file_version/ast_unregister_file_version to ast_register_file/ast_unregister_file, respectively. * main/manager: Remove value from the Version key of the ModuleCheck Action. The actual key itself has not been removed, as doing so would absolutely constitute a backwards incompatible change. However, since the file version is no longer tracked, there is no need to attempt to include it in the Version key. * UPGRADE: Add notes for: - Modification to the ModuleCheck AMI Action - Removal of the "core show file version" CLI command Change-Id: I6cf0ff280e1668bf4957dc21f32a5ff43444a40e
2015-04-12 02:38:22 +00:00
ASTERISK_REGISTER_FILE()
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/netsock2.h"
#include "asterisk/logger.h"
#include "asterisk/config.h" /* PARSE_PORT_* */
struct parse_test {
const char *address;
int expected_result;
};
AST_TEST_DEFINE(parsing)
{
int res = AST_TEST_PASS;
struct parse_test test_vals[] = {
{ "192.168.1.0", 1 },
{ "10.255.255.254", 1 },
{ "172.18.5.4", 1 },
{ "8.8.4.4", 1 },
{ "0.0.0.0", 1 },
{ "127.0.0.1", 1 },
{ "1.256.3.4", 0 },
{ "256.0.0.1", 0 },
{ "1.2.3.4:5060", 1 },
{ "::ffff:5.6.7.8", 1 },
{ "fdf8:f53b:82e4::53", 1 },
{ "fe80::200:5aee:feaa:20a2", 1 },
{ "2001::1", 1 },
{ "2001:0000:4136:e378:8000:63bf:3fff:fdd2", 1 },
{ "2001:0002:6c::430", 1 },
{ "2001:10:240:ab::a", 1 },
{ "2002:cb0a:3cdd:1::1", 1 },
{ "2001:db8:8:4::2", 1 }, /* Documentation only, should never be used */
{ "ff01:0:0:0:0:0:0:2", 1 }, /* Multicast */
{ "[fdf8:f53b:82e4::53]", 1 },
{ "[fe80::200:5aee:feaa:20a2]", 1 },
{ "[2001::1]", 1 },
{ "[2001:0000:4136:e378:8000:63bf:3fff:fdd2]:5060", 1 },
{ "2001:0000:4136:e378:8000:63bf:3fff:fdd2:5060", 0 }, /* port, but no brackets */
{ "fe80::200::abcd", 0 }, /* multiple zero expansions */
};
size_t x;
struct ast_sockaddr addr = { { 0, 0, } };
int parse_result;
switch (cmd) {
case TEST_INIT:
info->name = "parsing";
info->category = "/main/netsock2/";
info->summary = "netsock2 parsing unit test";
info->description =
"Test parsing of IPv4 and IPv6 network addresses";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
for (x = 0; x < ARRAY_LEN(test_vals); x++) {
if ((parse_result = ast_sockaddr_parse(&addr, test_vals[x].address, 0)) != test_vals[x].expected_result) {
ast_test_status_update(test, "On '%s' expected %d but got %d\n", test_vals[x].address, test_vals[x].expected_result, parse_result);
res = AST_TEST_FAIL;
}
if (parse_result) {
struct ast_sockaddr tmp_addr = { { 0, 0, } };
const char *tmp;
tmp = ast_sockaddr_stringify(&addr);
ast_sockaddr_parse(&tmp_addr, tmp, 0);
if (ast_sockaddr_cmp_addr(&addr, &tmp_addr)) {
char buf[64];
ast_copy_string(buf, ast_sockaddr_stringify(&addr), sizeof(buf));
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));
res = AST_TEST_FAIL;
}
}
}
return res;
}
AST_TEST_DEFINE(split_hostport)
{
int res = AST_TEST_PASS;
char *host, *port, buf[128];
switch (cmd) {
case TEST_INIT:
info->name = "split_hostport";
info->category = "/main/netsock2/";
info->summary = "netsock2 ast_sockaddr_split_hostport() unit test";
info->description =
"Test splitting of IPv4 and IPv6 host:port strings";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
/* Assumes res, host, and port variables */
#define TEST_SPLIT_HOSTPORT(str, flags, expected_host, expected_port, expected_result) { \
int __res; \
ast_copy_string(buf, str, sizeof(buf)); \
if ((__res = ast_sockaddr_split_hostport(buf, &host, &port, flags)) != expected_result || ( \
expected_result && ( \
strcmp(host, expected_host) || ( \
ast_strlen_zero(expected_port) ? !ast_strlen_zero(port) : (!ast_strlen_zero(port) && strcmp(port, expected_port)) \
) \
) \
) \
) { \
res = AST_TEST_FAIL; \
if (__res != expected_result) { \
ast_test_status_update(test, "Expected %d, got %d\n", expected_result, __res); \
} else { \
ast_test_status_update(test, "Failed parsing '%s' into expected host '%s' (got '%s') and port '%s' (got '%s')\n", \
str, S_OR(expected_host, ""), host, expected_port, S_OR(port, "")); \
} \
} \
}
/* Test various situations with flags = 0 */
TEST_SPLIT_HOSTPORT("192.168.1.1", 0, "192.168.1.1", "", 1);
TEST_SPLIT_HOSTPORT("192.168.1.1:5060", 0, "192.168.1.1", "5060", 1);
TEST_SPLIT_HOSTPORT("::ffff:5.6.7.8", 0, "::ffff:5.6.7.8", "", 1);
TEST_SPLIT_HOSTPORT("[::ffff:5.6.7.8]:5060", 0, "::ffff:5.6.7.8", "5060", 1);
TEST_SPLIT_HOSTPORT("fdf8:f53b:82e4::53", 0, "fdf8:f53b:82e4::53", "", 1);
TEST_SPLIT_HOSTPORT("fe80::200:5aee:feaa:20a2", 0, "fe80::200:5aee:feaa:20a2", "", 1);
TEST_SPLIT_HOSTPORT("[fdf8:f53b:82e4::53]", 0, "fdf8:f53b:82e4::53", "", 1);
TEST_SPLIT_HOSTPORT("[fe80::200:5aee:feaa:20a2]:5060", 0, "fe80::200:5aee:feaa:20a2", "5060", 1);
TEST_SPLIT_HOSTPORT("host:port", 0, "host", "port", 1);
TEST_SPLIT_HOSTPORT("host", 0, "host", "", 1);
/* Make sure that flag conditions work when they should */
TEST_SPLIT_HOSTPORT("192.168.1.1:5060", PARSE_PORT_IGNORE, "192.168.1.1", "", 1);
TEST_SPLIT_HOSTPORT("192.168.1.1:5060", PARSE_PORT_REQUIRE, "192.168.1.1", "5060", 1);
TEST_SPLIT_HOSTPORT("192.168.1.1", PARSE_PORT_FORBID, "192.168.1.1", "", 1);
TEST_SPLIT_HOSTPORT("[::ffff:5.6.7.8]:5060", PARSE_PORT_IGNORE, "::ffff:5.6.7.8", "", 1);
TEST_SPLIT_HOSTPORT("[::ffff:5.6.7.8]:5060", PARSE_PORT_REQUIRE, "::ffff:5.6.7.8", "5060", 1);
TEST_SPLIT_HOSTPORT("::ffff:5.6.7.8", PARSE_PORT_FORBID, "::ffff:5.6.7.8", "", 1);
/* Make sure it fails when flag requirements are not met */
TEST_SPLIT_HOSTPORT("192.168.1.1", PARSE_PORT_REQUIRE, "<undefined>", "<undefined>", 0);
TEST_SPLIT_HOSTPORT("192.168.1.1:5060", PARSE_PORT_FORBID, "<undefined>", "<undefined>", 0);
TEST_SPLIT_HOSTPORT("::ffff:5.6.7.8", PARSE_PORT_REQUIRE, "<undefined>", "<undefined>", 0);
TEST_SPLIT_HOSTPORT("[::ffff:5.6.7.8]:5060", PARSE_PORT_FORBID, "<undefined>", "<undefined>", 0);
return res;
}
static int unload_module(void)
{
AST_TEST_UNREGISTER(parsing);
AST_TEST_UNREGISTER(split_hostport);
return 0;
}
static int load_module(void)
{
AST_TEST_REGISTER(parsing);
AST_TEST_REGISTER(split_hostport);
return AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Netsock2 test module");