]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/socktoa.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / socktoa.c
1 #include "config.h"
2
3 #include "ntp_stdlib.h"
4 #include "ntp_calendar.h"
5
6 #include "unity.h"
7
8 #include "sockaddrtest.h"
9
10
11 void test_IPv4AddressWithPort(void) {
12         sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
13
14         TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input));
15         TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input));
16 }
17
18 void test_IPv6AddressWithPort(void) {
19         const struct in6_addr address = {
20                 0x20, 0x01, 0x0d, 0xb8,
21                 0x85, 0xa3, 0x08, 0xd3, 
22                 0x13, 0x19, 0x8a, 0x2e,
23                 0x03, 0x70, 0x73, 0x34
24         };
25
26         const char* expected =
27                 "2001:db8:85a3:8d3:1319:8a2e:370:7334";
28         const char* expected_port = 
29                 "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
30
31         sockaddr_u input;
32         memset(&input, 0, sizeof(input));
33         AF(&input) = AF_INET6;
34         SET_ADDR6N(&input, address);
35         SET_PORT(&input, 123);
36
37         TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
38         TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
39 }
40
41 #ifdef ISC_PLATFORM_HAVESCOPEID
42 void test_ScopedIPv6AddressWithPort(void) {
43         const struct in6_addr address = {
44                 0xfe, 0x80, 0x00, 0x00,
45                 0x00, 0x00, 0x00, 0x00,
46                 0x02, 0x12, 0x3f, 0xff, 
47                 0xfe, 0x29, 0xff, 0xfa
48         };
49
50         const char* expected =
51                 "fe80::212:3fff:fe29:fffa%5";
52         const char* expected_port = 
53                 "[fe80::212:3fff:fe29:fffa%5]:123";
54
55         sockaddr_u input;
56         memset(&input, 0, sizeof(input));
57         AF(&input) = AF_INET6;
58         SET_ADDR6N(&input, address);
59         SET_PORT(&input, 123);
60         SCOPE_VAR(&input) = 5;
61
62         TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
63         TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
64 }
65 #endif  /* ISC_PLATFORM_HAVESCOPEID */
66
67 void test_HashEqual(void) {
68         sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
69         sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
70
71         TEST_ASSERT_TRUE(IsEqual(input1, input2));
72         TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
73 }
74
75 void test_HashNotEqual(void) {
76         /* These two addresses should not generate the same hash. */
77         sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
78         sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
79
80         TEST_ASSERT_FALSE(IsEqual(input1, input2));
81         //TODO : EXPECT_NE(sock_hash(&input1), sock_hash(&input2));
82         //Damir's suggestion below:
83         TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2)); 
84         //NOTE: sock_hash returns u_short, so you can compare it with ==
85         //for complex structures you have to write an additional function like bool compare(a,b) 
86 }
87
88 void test_IgnoreIPv6Fields(void) {
89         const struct in6_addr address = {
90                 0x20, 0x01, 0x0d, 0xb8,
91         0x85, 0xa3, 0x08, 0xd3, 
92         0x13, 0x19, 0x8a, 0x2e,
93         0x03, 0x70, 0x73, 0x34
94         };
95
96         sockaddr_u input1, input2;
97
98         input1.sa6.sin6_family = AF_INET6;
99         input1.sa6.sin6_addr = address;
100         input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
101         SET_PORT(&input1, NTP_PORT);
102
103         input2.sa6.sin6_family = AF_INET6;
104         input2.sa6.sin6_addr = address;
105         input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
106         SET_PORT(&input2, NTP_PORT);
107
108         TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
109 }