test: simplify test-ip6-addr.c

Drop the callback-driven approach in favor of a simple loop. Reduces
the line count and makes the flow of execution a little easier to
follow.
This commit is contained in:
Ben Noordhuis 2013-08-19 21:20:20 +02:00
parent 12bad62add
commit dfce87c94f

View File

@ -30,72 +30,68 @@
# include <net/if.h>
#endif
typedef void (*iface_info_cb)(const char* ip6_addr, const char* device_name,
unsigned iface_index);
void call_iface_info_cb(iface_info_cb iface_cb,
char const* iface_name,
struct sockaddr_in6 const* address) {
TEST_IMPL(ip6_addr_link_local) {
#ifdef UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS
char string_address[INET6_ADDRSTRLEN];
ASSERT(0 == uv_inet_ntop(AF_INET6,
&address->sin6_addr,
string_address,
INET6_ADDRSTRLEN));
iface_cb(string_address, iface_name, address->sin6_scope_id);
}
void foreach_ip6_interface(iface_info_cb iface_cb) {
int count, ix;
uv_interface_address_t* addresses;
uv_interface_address_t* address;
struct sockaddr_in6 addr;
unsigned int iface_index;
const char* device_name;
/* 40 bytes address, 16 bytes device name, plus reserve. */
char scoped_addr[128];
int count;
int ix;
ASSERT(0 == uv_interface_addresses(&addresses, &count));
for (ix = 0; ix < count; ix++) {
if (addresses[ix].address.address4.sin_family != AF_INET6)
address = addresses + ix;
if (address->address.address6.sin6_family != AF_INET6)
continue;
call_iface_info_cb(iface_cb,
addresses[ix].name,
&addresses[ix].address.address6);
ASSERT(0 == uv_inet_ntop(AF_INET6,
&address->address.address6.sin6_addr,
string_address,
sizeof(string_address)));
/* Skip addresses that are not link-local. */
if (strncmp(string_address, "fe80::", 6) != 0)
continue;
iface_index = address->address.address6.sin6_scope_id;
device_name = address->name;
#ifdef _WIN32
snprintf(scoped_addr,
sizeof(scoped_addr),
"%s%%%d",
string_address,
iface_index);
#else
snprintf(scoped_addr,
sizeof(scoped_addr),
"%s%%%s",
string_address,
device_name);
#endif
LOGF("Testing link-local address %s "
"(iface_index: 0x%02x, device_name: %s)\n",
scoped_addr,
iface_index,
device_name);
addr = uv_ip6_addr(scoped_addr, TEST_PORT);
LOGF("Got scope_id 0x%02x\n", addr.sin6_scope_id);
ASSERT(iface_index == addr.sin6_scope_id);
}
uv_free_interface_addresses(addresses, count);
}
void test_ip6_addr_scope(const char* ip6_addr,
const char* device_name,
unsigned iface_index) {
/* 40 bytes address, 16 bytes device name, plus reserve */
char scoped_addr[128];
struct sockaddr_in6 addr;
/* skip addresses that are not link-local */
if (strncmp(ip6_addr, "fe80::", 6) != 0) return;
#ifdef _WIN32
snprintf(scoped_addr, sizeof(scoped_addr), "%s%%%d", ip6_addr, iface_index);
#else
snprintf(scoped_addr, sizeof(scoped_addr), "%s%%%s", ip6_addr, device_name);
#endif
LOGF("Testing link-local address %s (iface_index: 0x%02x, device_name: %s)\n",
scoped_addr,
iface_index,
device_name);
addr = uv_ip6_addr(scoped_addr, TEST_PORT);
LOGF("Got scope_id 0x%02x\n", addr.sin6_scope_id);
ASSERT(iface_index == addr.sin6_scope_id);
}
TEST_IMPL(ip6_addr_link_local) {
#ifdef UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS
foreach_ip6_interface(&test_ip6_addr_scope);
MAKE_VALGRIND_HAPPY();
return 0;
#else
RETURN_SKIP("Qualified link-local addresses are not supported.");