decodenetnum.c revision 294905
1#include "config.h" 2#include "ntp_stdlib.h" 3#include "sockaddrtest.h" 4 5#include "unity.h" 6 7void setUp(void); 8extern void test_IPv4AddressOnly(void); 9extern void test_IPv4AddressWithPort(void); 10extern void test_IPv6AddressOnly(void); 11extern void test_IPv6AddressWithPort(void); 12extern void test_IllegalAddress(void); 13extern void test_IllegalCharInPort(void); 14 15/* 16 * NOTE: The IPv6 specific tests are reduced to stubs when IPv6 is 17 * disabled. 18 * 19 * ISC_PLATFORM_HAVEIPV6 checks if system has IPV6 capabilies. WANTIPV6 20 * ISC_PLATFORM_WANTIPV6 can be changed with build --disable-ipv6. 21 * 22 * If we want IPv6 but don't have it, the tests should fail, I think. 23 */ 24void 25setUp(void) 26{ 27 init_lib(); 28} 29 30 31void 32test_IPv4AddressOnly(void) 33{ 34 const char *str = "192.0.2.1"; 35 sockaddr_u actual; 36 37 sockaddr_u expected; 38 expected.sa4.sin_family = AF_INET; 39 expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); 40 SET_PORT(&expected, NTP_PORT); 41 42 TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 43 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 44} 45 46void 47test_IPv4AddressWithPort(void) 48{ 49 const char *str = "192.0.2.2:2000"; 50 sockaddr_u actual; 51 52 sockaddr_u expected; 53 expected.sa4.sin_family = AF_INET; 54 expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2"); 55 SET_PORT(&expected, 2000); 56 57 TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 58 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 59} 60 61 62void 63test_IPv6AddressOnly(void) 64{ 65#ifdef ISC_PLATFORM_WANTIPV6 66 67 const struct in6_addr address = { 68 0x20, 0x01, 0x0d, 0xb8, 69 0x85, 0xa3, 0x08, 0xd3, 70 0x13, 0x19, 0x8a, 0x2e, 71 0x03, 0x70, 0x73, 0x34 72 }; 73 74 const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334"; 75 sockaddr_u actual; 76 77 sockaddr_u expected; 78 expected.sa6.sin6_family = AF_INET6; 79 expected.sa6.sin6_addr = address; 80 SET_PORT(&expected, NTP_PORT); 81 82 TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 83 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 84 85#else 86 87 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping."); 88 89#endif /* ISC_PLATFORM_HAVEIPV6 */ 90} 91 92 93void 94test_IPv6AddressWithPort(void) 95{ 96#ifdef ISC_PLATFORM_WANTIPV6 97 98 const struct in6_addr address = { 99 0x20, 0x01, 0x0d, 0xb8, 100 0x85, 0xa3, 0x08, 0xd3, 101 0x13, 0x19, 0x8a, 0x2e, 102 0x03, 0x70, 0x73, 0x34 103 }; 104 105 const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000"; 106 sockaddr_u actual; 107 108 sockaddr_u expected; 109 expected.sa6.sin6_family = AF_INET6; 110 expected.sa6.sin6_addr = address; 111 SET_PORT(&expected, 3000); 112 113 TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 114 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 115 116#else 117 118 TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping."); 119 120#endif /* ISC_PLATFORM_HAVEIPV6 */ 121} 122 123 124void 125test_IllegalAddress(void) 126{ 127 const char *str = "192.0.2.270:2000"; 128 sockaddr_u actual; 129 130 TEST_ASSERT_FALSE(decodenetnum(str, &actual)); 131} 132 133 134void 135test_IllegalCharInPort(void) 136{ 137 /* An illegal port does not make the decodenetnum fail, but instead 138 * makes it use the standard port. 139 */ 140 const char *str = "192.0.2.1:a700"; 141 sockaddr_u actual; 142 143 sockaddr_u expected; 144 expected.sa4.sin_family = AF_INET; 145 expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); 146 SET_PORT(&expected, NTP_PORT); 147 148 TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 149 TEST_ASSERT_TRUE(IsEqual(expected, actual)); 150} 151