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