void test_IPv6AddressWithPort(void) { #ifdef ISC_PLATFORM_WANTIPV6 const struct in6_addr address = { 0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x08, 0xd3, 0x13, 0x19, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34 }; const char* expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334"; const char* expected_port = "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123"; sockaddr_u input; memset(&input, 0, sizeof(input)); AF(&input) = AF_INET6; SET_ADDR6N(&input, address); SET_PORT(&input, 123); TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input)); TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input)); #else TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping."); #endif /* ISC_PLATFORM_HAVEIPV6 */ }
void test_ScopedIPv6AddressWithPort(void) { #ifdef ISC_PLATFORM_HAVESCOPEID const struct in6_addr address = { { { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x12, 0x3f, 0xff, 0xfe, 0x29, 0xff, 0xfa } } }; const char* expected = "fe80::212:3fff:fe29:fffa%5"; const char* expected_port = "[fe80::212:3fff:fe29:fffa%5]:123"; sockaddr_u input; memset(&input, 0, sizeof(input)); AF(&input) = AF_INET6; SET_ADDR6N(&input, address); SET_PORT(&input, 123); SCOPE_VAR(&input) = 5; TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input)); TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input)); #else TEST_IGNORE_MESSAGE("Skipping because ISC_PLATFORM does not have Scope ID"); #endif }
const char * refnumtoa( sockaddr_u *num ) { u_int32 netnum; char *buf; const char *rclock; if (!ISREFCLOCKADR(num)) return socktoa(num); LIB_GETBUF(buf); netnum = SRCADR(num); rclock = clockname((int)((u_long)netnum >> 8) & 0xff); if (rclock != NULL) snprintf(buf, LIB_BUFLENGTH, "%s(%lu)", rclock, (u_long)netnum & 0xff); else snprintf(buf, LIB_BUFLENGTH, "REFCLK(%lu,%lu)", ((u_long)netnum >> 8) & 0xff, (u_long)netnum & 0xff); return buf; }
void test_IPv4AddressWithPort(void) { sockaddr_u input = CreateSockaddr4("192.0.2.10", 123); TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input)); TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input)); }
const char * sockporttoa( const sockaddr_u *sock ) { int saved_errno; const char * atext; char * buf; saved_errno = errno; atext = socktoa(sock); LIB_GETBUF(buf); snprintf(buf, LIB_BUFLENGTH, (IS_IPV6(sock)) ? "[%s]:%hu" : "%s:%hu", atext, SRCPORT(sock)); errno = saved_errno; return buf; }
TEST(socktoa, IPv6AddressWithPort) { const struct in6_addr address = {{{ 0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x08, 0xd3, 0x13, 0x19, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34 }}}; const char* expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334"; const char* expected_port = "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123"; sockaddr_u input; memset(&input, 0, sizeof(input)); AF(&input) = AF_INET6; SET_ADDR6N(&input, address); SET_PORT(&input, 123); TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input)); TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input)); }
TEST(socktoa, ScopedIPv6AddressWithPort) { const struct in6_addr address = {{{ 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x12, 0x3f, 0xff, 0xfe, 0x29, 0xff, 0xfa }}}; const char* expected = "fe80::212:3fff:fe29:fffa%5"; const char* expected_port = "[fe80::212:3fff:fe29:fffa%5]:123"; sockaddr_u input; memset(&input, 0, sizeof(input)); AF(&input) = AF_INET6; SET_ADDR6N(&input, address); SET_PORT(&input, 123); SCOPE_VAR(&input) = 5; TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input)); TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input)); }
int IsEqual(const sockaddr_u expected, const sockaddr_u actual) { struct in_addr in; struct in6_addr in6; if (expected.sa.sa_family != actual.sa.sa_family) { printf("Expected sa_family: %d but got: %d", expected.sa.sa_family, actual.sa.sa_family); return FALSE; } if (actual.sa.sa_family == AF_INET) { // IPv4 if (expected.sa4.sin_port == actual.sa4.sin_port && memcmp(&expected.sa4.sin_addr, &actual.sa4.sin_addr, sizeof( in )) == 0) { return TRUE; } else { printf("IPv4 comparision failed, expected: %s(%s) but was: %s(%s)",inet_ntoa(expected.sa4.sin_addr), socktoa(&expected), inet_ntoa(actual.sa4.sin_addr),socktoa(&actual)); return FALSE; } } else if (actual.sa.sa_family == AF_INET6) { //IPv6 if (expected.sa6.sin6_port == actual.sa6.sin6_port && memcmp(&expected.sa6.sin6_addr, &actual.sa6.sin6_addr, sizeof(in6)) == 0) { return TRUE; } else { printf("IPv6 comparision failed"); return FALSE; } } else { // Unknown family printf("Unknown sa_family: %d",actual.sa.sa_family); return FALSE; } }
TEST_F(socktoaTest, IPv4AddressWithPort) { sockaddr_u input = CreateSockaddr4("192.0.2.10", 123); EXPECT_STREQ("192.0.2.10", socktoa(&input)); EXPECT_STREQ("192.0.2.10:123", sockporttoa(&input)); }