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