summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/tests/libntp/socktoa.c
blob: e9be1829fc6cb28ccedae26c9204d2651e5afd84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "config.h"

#include "ntp_stdlib.h"
#include "ntp_calendar.h"

#include "unity.h"
#include "sockaddrtest.h"


void setUp(void);
void test_IPv4AddressWithPort(void);
void test_IPv6AddressWithPort(void);
void test_IgnoreIPv6Fields(void);
void test_ScopedIPv6AddressWithPort(void);
void test_HashEqual(void);
void test_HashNotEqual(void);


void
setUp(void)
{
	init_lib();
}


void 
test_IPv4AddressWithPort(void)
{
	sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);

	TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input));
	TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input));
}


void 
test_IPv6AddressWithPort(void)
{
#ifdef ISC_PLATFORM_WANTIPV6

	const struct in6_addr address = {
		0x20, 0x01, 0x0d, 0xb8,
		0x85, 0xa3, 0x08, 0xd3, 
		0x13, 0x19, 0x8a, 0x2e,
		0x03, 0x70, 0x73, 0x34
	};

	const char* expected =
		"2001:db8:85a3:8d3:1319:8a2e:370:7334";
	const char* expected_port = 
		"[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";

	sockaddr_u input;
	memset(&input, 0, sizeof(input));
	AF(&input) = AF_INET6;
	SET_ADDR6N(&input, address);
	SET_PORT(&input, 123);

	TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
	TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));

#else

	TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");

#endif /* ISC_PLATFORM_HAVEIPV6 */
}


void 
test_ScopedIPv6AddressWithPort(void)
{
#ifdef ISC_PLATFORM_HAVESCOPEID
    
	const struct in6_addr address = { { {
		0xfe, 0x80, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00,
		0x02, 0x12, 0x3f, 0xff, 
		0xfe, 0x29, 0xff, 0xfa
	} } };

	const char* expected =
		"fe80::212:3fff:fe29:fffa%5";
	const char* expected_port = 
		"[fe80::212:3fff:fe29:fffa%5]:123";

	sockaddr_u input;
	memset(&input, 0, sizeof(input));
	AF(&input) = AF_INET6;
	SET_ADDR6N(&input, address);
	SET_PORT(&input, 123);
	SCOPE_VAR(&input) = 5;

	TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
	TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
#else
	
	TEST_IGNORE_MESSAGE("Skipping because ISC_PLATFORM does not have Scope ID");
	
#endif
}


void 
test_HashEqual(void)
{
	sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
	sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);

	TEST_ASSERT_TRUE(IsEqual(input1, input2));
	TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
}


void 
test_HashNotEqual(void)
{
	/* These two addresses should not generate the same hash. */
	sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
	sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);

	TEST_ASSERT_FALSE(IsEqual(input1, input2));
	TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2)); 
}


void 
test_IgnoreIPv6Fields(void)
{
#ifdef ISC_PLATFORM_WANTIPV6

	const struct in6_addr address = {
		0x20, 0x01, 0x0d, 0xb8,
		0x85, 0xa3, 0x08, 0xd3, 
		0x13, 0x19, 0x8a, 0x2e,
		0x03, 0x70, 0x73, 0x34
	};

	sockaddr_u input1, input2;

	input1.sa6.sin6_family = AF_INET6;
	input1.sa6.sin6_addr = address;
	input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
	SET_PORT(&input1, NTP_PORT);

	input2.sa6.sin6_family = AF_INET6;
	input2.sa6.sin6_addr = address;
	input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
	SET_PORT(&input2, NTP_PORT);

	TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));

#else

	TEST_IGNORE_MESSAGE("IPV6 disabled in build, skipping.");

#endif /* ISC_PLATFORM_HAVEIPV6 */
}
OpenPOWER on IntegriCloud