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