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