3 #include "ntp_stdlib.h"
4 #include "ntp_calendar.h"
8 #include "sockaddrtest.h"
11 void test_ClassBAddress(void);
12 void test_ClassCAddress(void);
13 void test_ClassAAddress(void);
14 void test_IPv6Address(void);
18 void test_ClassBAddress(void) {
19 sockaddr_u input = CreateSockaddr4("172.16.2.1", NTP_PORT);
20 sockaddr_u expected = CreateSockaddr4("172.16.0.0", NTP_PORT);
22 sockaddr_u* actual = netof(&input);
24 TEST_ASSERT_TRUE(actual != NULL);
25 TEST_ASSERT_TRUE(IsEqual(expected, *actual));
28 void test_ClassCAddress(void) {
29 sockaddr_u input = CreateSockaddr4("192.0.2.255", NTP_PORT);
30 sockaddr_u expected = CreateSockaddr4("192.0.2.0", NTP_PORT);
32 sockaddr_u* actual = netof(&input);
34 TEST_ASSERT_TRUE(actual != NULL);
35 TEST_ASSERT_TRUE(IsEqual(expected, *actual));
38 void test_ClassAAddress(void) {
39 /* Class A addresses are assumed to be classless,
40 * thus the same address should be returned.
42 sockaddr_u input = CreateSockaddr4("10.20.30.40", NTP_PORT);
43 sockaddr_u expected = CreateSockaddr4("10.20.30.40", NTP_PORT);
45 sockaddr_u* actual = netof(&input);
47 TEST_ASSERT_TRUE(actual != NULL);
48 TEST_ASSERT_TRUE(IsEqual(expected, *actual));
51 void test_IPv6Address(void) {
52 /* IPv6 addresses are assumed to have 64-bit host- and 64-bit network parts. */
53 const struct in6_addr input_address = {
54 0x20, 0x01, 0x0d, 0xb8,
55 0x85, 0xa3, 0x08, 0xd3,
56 0x13, 0x19, 0x8a, 0x2e,
57 0x03, 0x70, 0x73, 0x34
58 }; // 2001:0db8:85a3:08d3:1319:8a2e:0370:7334
60 const struct in6_addr expected_address = {
61 0x20, 0x01, 0x0d, 0xb8,
62 0x85, 0xa3, 0x08, 0xd3,
63 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00
65 }; // 2001:0db8:85a3:08d3:0000:0000:0000:0000
68 input.sa6.sin6_family = AF_INET6;
69 input.sa6.sin6_addr = input_address;
70 SET_PORT(&input, 3000);
73 expected.sa6.sin6_family = AF_INET6;
74 expected.sa6.sin6_addr = expected_address;
75 SET_PORT(&expected, 3000);
77 sockaddr_u* actual = netof(&input);
79 TEST_ASSERT_TRUE(actual != NULL);
80 TEST_ASSERT_TRUE(IsEqual(expected, *actual));