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