socktoa.c revision 1.1.1.3
1/* $NetBSD: socktoa.c,v 1.1.1.3 2015/10/23 17:47:45 christos Exp $ */ 2 3#include "config.h" 4 5#include "ntp_stdlib.h" 6#include "ntp_calendar.h" 7 8#include "unity.h" 9#include "sockaddrtest.h" 10 11 12void test_IPv4AddressWithPort(void); 13//#ifdef ISC_PLATFORM_HAVEIPV6 14void test_IPv6AddressWithPort(void); 15void test_IgnoreIPv6Fields(void); 16//#endif /* ISC_PLATFORM_HAVEIPV6 */ 17void test_ScopedIPv6AddressWithPort(void); 18void test_HashEqual(void); 19void test_HashNotEqual(void); 20 21void 22test_IPv4AddressWithPort(void) { 23 sockaddr_u input = CreateSockaddr4("192.0.2.10", 123); 24 25 TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input)); 26 TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input)); 27} 28 29 30void 31test_IPv6AddressWithPort(void) { 32 33#ifdef ISC_PLATFORM_WANTIPV6 34 35 const struct in6_addr address = { 36 0x20, 0x01, 0x0d, 0xb8, 37 0x85, 0xa3, 0x08, 0xd3, 38 0x13, 0x19, 0x8a, 0x2e, 39 0x03, 0x70, 0x73, 0x34 40 }; 41 42 const char* expected = 43 "2001:db8:85a3:8d3:1319:8a2e:370:7334"; 44 const char* expected_port = 45 "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123"; 46 47 sockaddr_u input; 48 memset(&input, 0, sizeof(input)); 49 AF(&input) = AF_INET6; 50 SET_ADDR6N(&input, address); 51 SET_PORT(&input, 123); 52 53 TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input)); 54 TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input)); 55 56#else 57 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping."); 58 59#endif /* ISC_PLATFORM_HAVEIPV6 */ 60 61} 62 63 64void 65test_ScopedIPv6AddressWithPort(void) { 66#ifdef ISC_PLATFORM_HAVESCOPEID 67 const struct in6_addr address = { 68 0xfe, 0x80, 0x00, 0x00, 69 0x00, 0x00, 0x00, 0x00, 70 0x02, 0x12, 0x3f, 0xff, 71 0xfe, 0x29, 0xff, 0xfa 72 }; 73 74 const char* expected = 75 "fe80::212:3fff:fe29:fffa%5"; 76 const char* expected_port = 77 "[fe80::212:3fff:fe29:fffa%5]:123"; 78 79 sockaddr_u input; 80 memset(&input, 0, sizeof(input)); 81 AF(&input) = AF_INET6; 82 SET_ADDR6N(&input, address); 83 SET_PORT(&input, 123); 84 SCOPE_VAR(&input) = 5; 85 86 TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input)); 87 TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input)); 88#else 89 TEST_IGNORE_MESSAGE("Skipping because ISC_PLATFORM does not have Scope ID"); 90#endif 91} 92 93void 94test_HashEqual(void) { 95 sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123); 96 sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123); 97 98 TEST_ASSERT_TRUE(IsEqual(input1, input2)); 99 TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2)); 100} 101 102void 103test_HashNotEqual(void) { 104 /* These two addresses should not generate the same hash. */ 105 sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123); 106 sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123); 107 108 TEST_ASSERT_FALSE(IsEqual(input1, input2)); 109 TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2)); 110} 111 112 113void 114test_IgnoreIPv6Fields(void) { 115 116#ifdef ISC_PLATFORM_WANTIPV6 117 118 const struct in6_addr address = { 119 0x20, 0x01, 0x0d, 0xb8, 120 0x85, 0xa3, 0x08, 0xd3, 121 0x13, 0x19, 0x8a, 0x2e, 122 0x03, 0x70, 0x73, 0x34 123 }; 124 125 sockaddr_u input1, input2; 126 127 input1.sa6.sin6_family = AF_INET6; 128 input1.sa6.sin6_addr = address; 129 input1.sa6.sin6_flowinfo = 30L; // This value differs from input2. 130 SET_PORT(&input1, NTP_PORT); 131 132 input2.sa6.sin6_family = AF_INET6; 133 input2.sa6.sin6_addr = address; 134 input2.sa6.sin6_flowinfo = 10L; // This value differs from input1. 135 SET_PORT(&input2, NTP_PORT); 136 137 TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2)); 138 139#else 140 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping."); 141#endif /* ISC_PLATFORM_HAVEIPV6 */ 142} 143 144