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