decodenetnum.c revision 289999
1284990Scy#include "config.h" 2284990Scy#include "ntp_stdlib.h" 3289999Sglebius#include "sockaddrtest.h" 4289999Sglebius 5284990Scy#include "unity.h" 6284990Scy 7289999Sglebiusextern void test_IPv4AddressOnly(void); 8289999Sglebiusextern void test_IPv4AddressWithPort(void); 9289999Sglebius//#ifdef ISC_PLATFORM_HAVEIPV6 10289999Sglebiusextern void test_IPv6AddressOnly(void); 11289999Sglebiusextern void test_IPv6AddressWithPort(void); 12289999Sglebius//#endif /* ISC_PLATFORM_HAVEIPV6 */ 13289999Sglebiusextern void test_IllegalAddress(void); 14289999Sglebiusextern void test_IllegalCharInPort(void); 15284990Scy 16284990Scy 17289999Sglebiusvoid 18289999Sglebiustest_IPv4AddressOnly(void) { 19284990Scy const char *str = "192.0.2.1"; 20284990Scy sockaddr_u actual; 21284990Scy 22284990Scy sockaddr_u expected; 23284990Scy expected.sa4.sin_family = AF_INET; 24284990Scy expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); 25284990Scy SET_PORT(&expected, NTP_PORT); 26284990Scy 27284990Scy TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 28284990Scy TEST_ASSERT_TRUE(IsEqual(expected, actual)); 29284990Scy} 30284990Scy 31289999Sglebiusvoid 32289999Sglebiustest_IPv4AddressWithPort(void) { 33284990Scy const char *str = "192.0.2.2:2000"; 34284990Scy sockaddr_u actual; 35284990Scy 36284990Scy sockaddr_u expected; 37284990Scy expected.sa4.sin_family = AF_INET; 38284990Scy expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2"); 39284990Scy SET_PORT(&expected, 2000); 40284990Scy 41284990Scy TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 42284990Scy TEST_ASSERT_TRUE(IsEqual(expected, actual)); 43284990Scy} 44284990Scy 45289999Sglebius 46289999Sglebiusvoid 47289999Sglebiustest_IPv6AddressOnly(void) { 48289999Sglebius 49289999Sglebius//#ifdef ISC_PLATFORM_HAVEIPV6 //looks like HAVEIPV6 checks if system has IPV6 capabilies. WANTIPV6 can be changed with build --disable-ipv6 50289999Sglebius#ifdef ISC_PLATFORM_WANTIPV6 51284990Scy const struct in6_addr address = { 52284990Scy 0x20, 0x01, 0x0d, 0xb8, 53289999Sglebius 0x85, 0xa3, 0x08, 0xd3, 54284990Scy 0x13, 0x19, 0x8a, 0x2e, 55284990Scy 0x03, 0x70, 0x73, 0x34 56284990Scy }; 57284990Scy 58284990Scy const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334"; 59284990Scy sockaddr_u actual; 60284990Scy 61284990Scy sockaddr_u expected; 62284990Scy expected.sa6.sin6_family = AF_INET6; 63284990Scy expected.sa6.sin6_addr = address; 64284990Scy SET_PORT(&expected, NTP_PORT); 65284990Scy 66284990Scy TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 67284990Scy TEST_ASSERT_TRUE(IsEqual(expected, actual)); 68289999Sglebius 69289999Sglebius#else 70289999Sglebius TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping."); 71289999Sglebius#endif /* ISC_PLATFORM_HAVEIPV6 */ 72289999Sglebius 73289999Sglebius 74284990Scy} 75284990Scy 76289999Sglebius 77289999Sglebius 78289999Sglebiusvoid 79289999Sglebiustest_IPv6AddressWithPort(void) { 80289999Sglebius 81289999Sglebius#ifdef ISC_PLATFORM_WANTIPV6 82289999Sglebius 83284990Scy const struct in6_addr address = { 84284990Scy 0x20, 0x01, 0x0d, 0xb8, 85289999Sglebius 0x85, 0xa3, 0x08, 0xd3, 86284990Scy 0x13, 0x19, 0x8a, 0x2e, 87284990Scy 0x03, 0x70, 0x73, 0x34 88284990Scy }; 89284990Scy 90284990Scy const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000"; 91284990Scy sockaddr_u actual; 92284990Scy 93284990Scy sockaddr_u expected; 94284990Scy expected.sa6.sin6_family = AF_INET6; 95284990Scy expected.sa6.sin6_addr = address; 96284990Scy SET_PORT(&expected, 3000); 97284990Scy 98284990Scy TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 99284990Scy TEST_ASSERT_TRUE(IsEqual(expected, actual)); 100289999Sglebius 101289999Sglebius#else 102289999Sglebius TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping."); 103289999Sglebius#endif /* ISC_PLATFORM_HAVEIPV6 */ 104284990Scy} 105284990Scy 106289999Sglebius 107289999Sglebiusvoid 108289999Sglebiustest_IllegalAddress(void) { 109284990Scy const char *str = "192.0.2.270:2000"; 110284990Scy sockaddr_u actual; 111284990Scy 112284990Scy TEST_ASSERT_FALSE(decodenetnum(str, &actual)); 113284990Scy} 114284990Scy 115289999Sglebiusvoid 116289999Sglebiustest_IllegalCharInPort(void) { 117284990Scy /* An illegal port does not make the decodenetnum fail, but instead 118284990Scy * makes it use the standard port. 119284990Scy */ 120284990Scy const char *str = "192.0.2.1:a700"; 121284990Scy sockaddr_u actual; 122284990Scy 123284990Scy sockaddr_u expected; 124284990Scy expected.sa4.sin_family = AF_INET; 125284990Scy expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); 126284990Scy SET_PORT(&expected, NTP_PORT); 127284990Scy 128284990Scy TEST_ASSERT_TRUE(decodenetnum(str, &actual)); 129284990Scy TEST_ASSERT_TRUE(IsEqual(expected, actual)); 130284990Scy} 131