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