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