decodenetnum.c revision 1.1 1 #include "config.h"
2 #include "ntp_stdlib.h"
3 #include "ntp_calendar.h"
4 #include "unity.h"
5
6 #include "sockaddrtest.h"
7
8
9 void test_IPv4AddressOnly(void) {
10 const char *str = "192.0.2.1";
11 sockaddr_u actual;
12
13 sockaddr_u expected;
14 expected.sa4.sin_family = AF_INET;
15 expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
16 SET_PORT(&expected, NTP_PORT);
17
18 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
19 TEST_ASSERT_TRUE(IsEqual(expected, actual));
20 }
21
22 void test_IPv4AddressWithPort(void) {
23 const char *str = "192.0.2.2:2000";
24 sockaddr_u actual;
25
26 sockaddr_u expected;
27 expected.sa4.sin_family = AF_INET;
28 expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
29 SET_PORT(&expected, 2000);
30
31 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
32 TEST_ASSERT_TRUE(IsEqual(expected, actual));
33 }
34
35 void test_IPv6AddressOnly(void) {
36 const struct in6_addr address = {
37 0x20, 0x01, 0x0d, 0xb8,
38 0x85, 0xa3, 0x08, 0xd3,
39 0x13, 0x19, 0x8a, 0x2e,
40 0x03, 0x70, 0x73, 0x34
41 };
42
43 const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
44 sockaddr_u actual;
45
46 sockaddr_u expected;
47 expected.sa6.sin6_family = AF_INET6;
48 expected.sa6.sin6_addr = address;
49 SET_PORT(&expected, NTP_PORT);
50
51 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
52 TEST_ASSERT_TRUE(IsEqual(expected, actual));
53 }
54
55 void test_IPv6AddressWithPort(void) {
56 const struct in6_addr address = {
57 0x20, 0x01, 0x0d, 0xb8,
58 0x85, 0xa3, 0x08, 0xd3,
59 0x13, 0x19, 0x8a, 0x2e,
60 0x03, 0x70, 0x73, 0x34
61 };
62
63 const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
64 sockaddr_u actual;
65
66 sockaddr_u expected;
67 expected.sa6.sin6_family = AF_INET6;
68 expected.sa6.sin6_addr = address;
69 SET_PORT(&expected, 3000);
70
71 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
72 TEST_ASSERT_TRUE(IsEqual(expected, actual));
73 }
74
75 void test_IllegalAddress(void) {
76 const char *str = "192.0.2.270:2000";
77 sockaddr_u actual;
78
79 TEST_ASSERT_FALSE(decodenetnum(str, &actual));
80 }
81
82 void test_IllegalCharInPort(void) {
83 /* An illegal port does not make the decodenetnum fail, but instead
84 * makes it use the standard port.
85 */
86 const char *str = "192.0.2.1:a700";
87 sockaddr_u actual;
88
89 sockaddr_u expected;
90 expected.sa4.sin_family = AF_INET;
91 expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
92 SET_PORT(&expected, NTP_PORT);
93
94 TEST_ASSERT_TRUE(decodenetnum(str, &actual));
95 TEST_ASSERT_TRUE(IsEqual(expected, actual));
96 }
97