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