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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ppapi/tests/test_net_address_private.h"
#include <string.h>
#include "ppapi/cpp/private/net_address_private.h"
#include "ppapi/c/private/ppb_net_address_private.h"
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"
// Other than |GetAnyAddress()|, there's no way to actually get
// |PP_NetAddress_Private| structs from just this interface. We'll cheat and
// synthesize some.
#if defined(PPAPI_POSIX)
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#if defined(PPAPI_OS_MACOSX)
// This is a bit evil, but it's standard operating procedure for |s6_addr|....
#define s6_addr16 __u6_addr.__u6_addr16
#endif
#if defined(PPAPI_OS_WIN)
#include <ws2tcpip.h>
#define s6_addr16 u.Word
#endif
using pp::NetAddressPrivate;
namespace {
// |host| should be an IP address represented as text, e.g., "192.168.0.1".
PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) {
PP_NetAddress_Private addr = PP_NetAddress_Private();
addr.size = sizeof(sockaddr_in);
sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr.data);
a->sin_family = AF_INET;
a->sin_port = htons(port);
a->sin_addr.s_addr = inet_addr(host);
return addr;
}
// |host| should be an array of eight 16-bit numbers.
PP_NetAddress_Private MakeIPv6NetAddress(const uint16_t host[], uint16_t port,
uint32_t scope_id) {
PP_NetAddress_Private addr = PP_NetAddress_Private();
addr.size = sizeof(sockaddr_in6);
sockaddr_in6* a = reinterpret_cast<sockaddr_in6*>(addr.data);
a->sin6_family = AF_INET6;
a->sin6_port = htons(port);
a->sin6_flowinfo = 0;
for (int i = 0; i < 8; i++)
a->sin6_addr.s6_addr16[i] = htons(host[i]);
a->sin6_scope_id = scope_id;
return addr;
}
} // namespace
REGISTER_TEST_CASE(NetAddressPrivate);
TestNetAddressPrivate::TestNetAddressPrivate(TestingInstance* instance)
: TestCase(instance) {
}
bool TestNetAddressPrivate::Init() {
return NetAddressPrivate::IsAvailable();
}
void TestNetAddressPrivate::RunTests(const std::string& filter) {
RUN_TEST(AreEqual, filter);
RUN_TEST(AreHostsEqual, filter);
RUN_TEST(Describe, filter);
RUN_TEST(ReplacePort, filter);
RUN_TEST(GetAnyAddress, filter);
RUN_TEST(DescribeIPv6, filter);
RUN_TEST(GetFamily, filter);
RUN_TEST(GetPort, filter);
RUN_TEST(GetAddress, filter);
}
std::string TestNetAddressPrivate::TestAreEqual() {
// No comparisons should ever be done with invalid addresses.
PP_NetAddress_Private invalid = PP_NetAddress_Private();
ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid));
PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80));
ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid));
PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, localhost_1234));
PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80);
ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, other_80));
PASS();
}
std::string TestNetAddressPrivate::TestAreHostsEqual() {
// No comparisons should ever be done with invalid addresses.
PP_NetAddress_Private invalid = PP_NetAddress_Private();
ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(invalid, invalid));
PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_80));
ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, invalid));
PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_1234));
PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80);
ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, other_80));
PASS();
}
std::string TestNetAddressPrivate::TestDescribe() {
PP_NetAddress_Private invalid = PP_NetAddress_Private();
ASSERT_EQ("", NetAddressPrivate::Describe(invalid, false));
ASSERT_EQ("", NetAddressPrivate::Describe(invalid, true));
PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_80, false));
ASSERT_EQ("127.0.0.1:80", NetAddressPrivate::Describe(localhost_80, true));
PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_1234, false));
ASSERT_EQ("127.0.0.1:1234", NetAddressPrivate::Describe(localhost_1234,
true));
PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80);
ASSERT_EQ("192.168.0.1", NetAddressPrivate::Describe(other_80, false));
ASSERT_EQ("192.168.0.1:80", NetAddressPrivate::Describe(other_80, true));
PASS();
}
std::string TestNetAddressPrivate::TestReplacePort() {
// Assume that |AreEqual()| works correctly.
PP_NetAddress_Private result = PP_NetAddress_Private();
PP_NetAddress_Private invalid = PP_NetAddress_Private();
ASSERT_FALSE(NetAddressPrivate::ReplacePort(invalid, 1234, &result));
PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
ASSERT_TRUE(NetAddressPrivate::ReplacePort(localhost_80, 1234, &result));
PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
ASSERT_TRUE(NetAddressPrivate::AreEqual(result, localhost_1234));
// Test that having the out param being the same as the in param works
// properly.
ASSERT_TRUE(NetAddressPrivate::ReplacePort(result, 80, &result));
ASSERT_TRUE(NetAddressPrivate::AreEqual(result, localhost_80));
PASS();
}
std::string TestNetAddressPrivate::TestGetAnyAddress() {
// Just make sure it doesn't crash and such.
PP_NetAddress_Private result = PP_NetAddress_Private();
NetAddressPrivate::GetAnyAddress(false, &result);
ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
NetAddressPrivate::GetAnyAddress(true, &result);
ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
PASS();
}
// TODO(viettrungluu): More IPv6 tests needed.
std::string TestNetAddressPrivate::TestDescribeIPv6() {
static const struct {
uint16_t address[8];
uint16_t port;
uint32_t scope;
const char* expected_without_port;
const char* expected_with_port;
} test_cases[] = {
{ // Generic test case (unique longest run of zeros to collapse).
{ 0x12, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 12, 0,
"12:abcd:0:1::cdef", "[12:abcd:0:1::cdef]:12"
},
{ // Non-zero scope.
{ 0x1234, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 1234, 789,
"1234:abcd:0:1::cdef%789", "[1234:abcd:0:1::cdef%789]:1234"
},
{ // Ignore the first (non-longest) run of zeros.
{ 0, 0, 0, 0x0123, 0, 0, 0, 0 }, 123, 0,
"0:0:0:123::", "[0:0:0:123::]:123"
},
{ // Collapse the first (equally-longest) run of zeros.
{ 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }, 123, 0,
"1234:abcd::ff:0:0:cdef", "[1234:abcd::ff:0:0:cdef]:123"
},
{ // Don't collapse "runs" of zeros of length 1.
{ 0, 0xa, 1, 2, 3, 0, 5, 0 }, 123, 0,
"0:a:1:2:3:0:5:0", "[0:a:1:2:3:0:5:0]:123"
},
{ // Collapse a run of zeros at the beginning.
{ 0, 0, 0, 2, 3, 0, 0, 0 }, 123, 0,
"::2:3:0:0:0", "[::2:3:0:0:0]:123"
},
{ // Collapse a run of zeros at the end.
{ 0, 0xa, 1, 2, 3, 0, 0, 0 }, 123, 0,
"0:a:1:2:3::", "[0:a:1:2:3::]:123"
},
{ // IPv4 192.168.1.2 embedded in IPv6 in the deprecated way.
{ 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, 0,
"::192.168.1.2", "[::192.168.1.2]:123"
},
{ // ... with non-zero scope.
{ 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, 789,
"::192.168.1.2%789", "[::192.168.1.2%789]:123"
},
{ // IPv4 192.168.1.2 embedded in IPv6.
{ 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, 0,
"::ffff:192.168.1.2", "[::ffff:192.168.1.2]:123"
},
{ // ... with non-zero scope.
{ 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, 789,
"::ffff:192.168.1.2%789", "[::ffff:192.168.1.2%789]:123"
},
{ // *Not* IPv4 embedded in IPv6.
{ 0, 0, 0, 0, 0, 0x1234, 0xc0a8, 0x102 }, 123, 0,
"::1234:c0a8:102", "[::1234:c0a8:102]:123"
}
};
for (size_t i = 0; i < sizeof(test_cases)/sizeof(test_cases[0]); i++) {
PP_NetAddress_Private addr = MakeIPv6NetAddress(test_cases[i].address,
test_cases[i].port,
test_cases[i].scope);
ASSERT_EQ(test_cases[i].expected_without_port,
NetAddressPrivate::Describe(addr, false));
ASSERT_EQ(test_cases[i].expected_with_port,
NetAddressPrivate::Describe(addr, true));
}
PASS();
}
std::string TestNetAddressPrivate::TestGetFamily() {
PP_NetAddress_Private ipv4 = MakeIPv4NetAddress("127.0.0.1", 80);
ASSERT_EQ(NetAddressPrivate::GetFamily(ipv4), AF_INET);
uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
PP_NetAddress_Private ipv6 = MakeIPv6NetAddress(ipv6_address,
123,
0);
ASSERT_EQ(NetAddressPrivate::GetFamily(ipv6), AF_INET6);
PASS();
}
std::string TestNetAddressPrivate::TestGetPort() {
PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
ASSERT_EQ(NetAddressPrivate::GetPort(localhost_80), 80);
uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
PP_NetAddress_Private port_123 = MakeIPv6NetAddress(ipv6_address, 123, 0);
ASSERT_EQ(NetAddressPrivate::GetPort(port_123), 123);
PP_NetAddress_Private port_FFFF = MakeIPv6NetAddress(ipv6_address,
0xFFFF,
0);
ASSERT_EQ(NetAddressPrivate::GetPort(port_FFFF), 0xFFFF);
PASS();
}
std::string TestNetAddressPrivate::TestGetAddress() {
const int addr_storage_len = 16;
unsigned char addr_storage[addr_storage_len];
const char* ipv4_addr = "127.0.0.1";
uint32_t ipv4_addr_long = inet_addr(ipv4_addr);
PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(ipv4_addr, 80);
memset(addr_storage, 0, addr_storage_len);
ASSERT_TRUE(NetAddressPrivate::GetAddress(localhost_80,
addr_storage,
addr_storage_len));
ASSERT_EQ(memcmp(addr_storage, &ipv4_addr_long, 4), 0);
// Insufficient storage for address.
ASSERT_FALSE(NetAddressPrivate::GetAddress(localhost_80,
addr_storage,
1));
uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
PP_NetAddress_Private ipv6_addr = MakeIPv6NetAddress(ipv6_address,
123,
0);
// Ensure the ipv6 address is transformed properly into network order.
for (int i = 0; i < 8; i++)
ipv6_address[i] = htons(ipv6_address[i]);
memset(addr_storage, 0, addr_storage_len);
ASSERT_TRUE(NetAddressPrivate::GetAddress(ipv6_addr,
addr_storage,
addr_storage_len));
ASSERT_EQ(memcmp(addr_storage, ipv6_address, 16), 0);
// Insufficient storage for address.
ASSERT_FALSE(NetAddressPrivate::GetAddress(ipv6_addr,
addr_storage,
1));
PASS();
}
|