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