blob: 7b35e6663e7aa6b5f967b1983a7dd0fe6629a373 [file] [log] [blame]
[email protected]ee9bdfce2012-02-09 00:04:091// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]87f36372011-11-11 20:49:012// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/tests/test_net_address_private.h"
6
[email protected]ee9bdfce2012-02-09 00:04:097#include <string.h>
8
[email protected]87f36372011-11-11 20:49:019#include "ppapi/cpp/private/net_address_private.h"
10#include "ppapi/c/private/ppb_net_address_private.h"
[email protected]2059f3d2011-12-01 20:12:3711#include "ppapi/tests/test_utils.h"
[email protected]87f36372011-11-11 20:49:0112#include "ppapi/tests/testing_instance.h"
13
[email protected]87f36372011-11-11 20:49:0114using pp::NetAddressPrivate;
15
16namespace {
17
[email protected]b13deef82012-03-22 22:41:0618PP_NetAddress_Private MakeIPv4NetAddress(const uint8_t host[4], int port) {
19 PP_NetAddress_Private addr;
20 NetAddressPrivate::CreateFromIPv4Address(host, port, &addr);
[email protected]87f36372011-11-11 20:49:0121 return addr;
22}
23
[email protected]b13deef82012-03-22 22:41:0624PP_NetAddress_Private MakeIPv6NetAddress(const uint16_t host[8], uint16_t port,
[email protected]ba359352011-11-22 00:24:1325 uint32_t scope_id) {
26 PP_NetAddress_Private addr = PP_NetAddress_Private();
[email protected]b13deef82012-03-22 22:41:0627 uint8_t ip[16];
28 for(int i = 0; i < 8; ++i) {
29 ip[i * 2] = host[i] >> 8;
30 ip[i * 2 + 1] = host[i] & 0xff;
31 }
32 NetAddressPrivate::CreateFromIPv6Address(ip, scope_id, port, &addr);
[email protected]ba359352011-11-22 00:24:1333 return addr;
34}
[email protected]87f36372011-11-11 20:49:0135
36} // namespace
37
38REGISTER_TEST_CASE(NetAddressPrivate);
39
40TestNetAddressPrivate::TestNetAddressPrivate(TestingInstance* instance)
41 : TestCase(instance) {
42}
43
44bool TestNetAddressPrivate::Init() {
45 return NetAddressPrivate::IsAvailable();
46}
47
[email protected]2622d6b2011-11-16 04:28:0248void TestNetAddressPrivate::RunTests(const std::string& filter) {
49 RUN_TEST(AreEqual, filter);
50 RUN_TEST(AreHostsEqual, filter);
51 RUN_TEST(Describe, filter);
52 RUN_TEST(ReplacePort, filter);
53 RUN_TEST(GetAnyAddress, filter);
[email protected]ba359352011-11-22 00:24:1354 RUN_TEST(DescribeIPv6, filter);
[email protected]ee9bdfce2012-02-09 00:04:0955 RUN_TEST(GetFamily, filter);
56 RUN_TEST(GetPort, filter);
57 RUN_TEST(GetAddress, filter);
[email protected]b13deef82012-03-22 22:41:0658 RUN_TEST(GetScopeID, filter);
[email protected]87f36372011-11-11 20:49:0159}
60
61std::string TestNetAddressPrivate::TestAreEqual() {
62 // No comparisons should ever be done with invalid addresses.
63 PP_NetAddress_Private invalid = PP_NetAddress_Private();
64 ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid));
65
[email protected]b13deef82012-03-22 22:41:0666 uint8_t localhost_ip[4] = { 127, 0, 0, 1 };
67 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80);
[email protected]87f36372011-11-11 20:49:0168 ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80));
69 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid));
70
[email protected]b13deef82012-03-22 22:41:0671 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress(localhost_ip, 1234);
[email protected]87f36372011-11-11 20:49:0172 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, localhost_1234));
73
[email protected]b13deef82012-03-22 22:41:0674 uint8_t other_ip[4] = { 192, 168, 0, 1 };
75 PP_NetAddress_Private other_80 = MakeIPv4NetAddress(other_ip, 80);
[email protected]87f36372011-11-11 20:49:0176 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, other_80));
77
78 PASS();
79}
80
81std::string TestNetAddressPrivate::TestAreHostsEqual() {
82 // No comparisons should ever be done with invalid addresses.
83 PP_NetAddress_Private invalid = PP_NetAddress_Private();
84 ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(invalid, invalid));
85
[email protected]b13deef82012-03-22 22:41:0686 uint8_t localhost_ip[4] = { 127, 0, 0, 1 };
87 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80);
[email protected]87f36372011-11-11 20:49:0188 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_80));
89 ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, invalid));
90
[email protected]b13deef82012-03-22 22:41:0691 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress(localhost_ip, 1234);
[email protected]87f36372011-11-11 20:49:0192 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_1234));
93
[email protected]b13deef82012-03-22 22:41:0694 uint8_t other_ip[4] = { 192, 168, 0, 1 };
95 PP_NetAddress_Private other_80 = MakeIPv4NetAddress(other_ip, 80);
[email protected]87f36372011-11-11 20:49:0196 ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, other_80));
97
98 PASS();
99}
100
101std::string TestNetAddressPrivate::TestDescribe() {
102 PP_NetAddress_Private invalid = PP_NetAddress_Private();
103 ASSERT_EQ("", NetAddressPrivate::Describe(invalid, false));
104 ASSERT_EQ("", NetAddressPrivate::Describe(invalid, true));
105
[email protected]b13deef82012-03-22 22:41:06106 uint8_t localhost_ip[4] = { 127, 0, 0, 1 };
107 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80);
[email protected]87f36372011-11-11 20:49:01108 ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_80, false));
109 ASSERT_EQ("127.0.0.1:80", NetAddressPrivate::Describe(localhost_80, true));
110
[email protected]b13deef82012-03-22 22:41:06111 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress(localhost_ip, 1234);
[email protected]87f36372011-11-11 20:49:01112 ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_1234, false));
113 ASSERT_EQ("127.0.0.1:1234", NetAddressPrivate::Describe(localhost_1234,
114 true));
115
[email protected]b13deef82012-03-22 22:41:06116 uint8_t other_ip[4] = { 192, 168, 0, 1 };
117 PP_NetAddress_Private other_80 = MakeIPv4NetAddress(other_ip, 80);
[email protected]87f36372011-11-11 20:49:01118 ASSERT_EQ("192.168.0.1", NetAddressPrivate::Describe(other_80, false));
119 ASSERT_EQ("192.168.0.1:80", NetAddressPrivate::Describe(other_80, true));
120
121 PASS();
122}
123
124std::string TestNetAddressPrivate::TestReplacePort() {
125 // Assume that |AreEqual()| works correctly.
126 PP_NetAddress_Private result = PP_NetAddress_Private();
127
128 PP_NetAddress_Private invalid = PP_NetAddress_Private();
129 ASSERT_FALSE(NetAddressPrivate::ReplacePort(invalid, 1234, &result));
130
[email protected]b13deef82012-03-22 22:41:06131 uint8_t localhost_ip[4] = { 127, 0, 0, 1 };
132 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80);
[email protected]87f36372011-11-11 20:49:01133 ASSERT_TRUE(NetAddressPrivate::ReplacePort(localhost_80, 1234, &result));
[email protected]b13deef82012-03-22 22:41:06134 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress(localhost_ip, 1234);
[email protected]87f36372011-11-11 20:49:01135 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, localhost_1234));
136
137 // Test that having the out param being the same as the in param works
138 // properly.
139 ASSERT_TRUE(NetAddressPrivate::ReplacePort(result, 80, &result));
140 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, localhost_80));
141
142 PASS();
143}
144
145std::string TestNetAddressPrivate::TestGetAnyAddress() {
146 // Just make sure it doesn't crash and such.
147 PP_NetAddress_Private result = PP_NetAddress_Private();
148
149 NetAddressPrivate::GetAnyAddress(false, &result);
150 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
151
152 NetAddressPrivate::GetAnyAddress(true, &result);
153 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
154
155 PASS();
156}
[email protected]ba359352011-11-22 00:24:13157
158// TODO(viettrungluu): More IPv6 tests needed.
159
160std::string TestNetAddressPrivate::TestDescribeIPv6() {
161 static const struct {
162 uint16_t address[8];
163 uint16_t port;
164 uint32_t scope;
165 const char* expected_without_port;
166 const char* expected_with_port;
167 } test_cases[] = {
168 { // Generic test case (unique longest run of zeros to collapse).
169 { 0x12, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 12, 0,
170 "12:abcd:0:1::cdef", "[12:abcd:0:1::cdef]:12"
171 },
172 { // Non-zero scope.
173 { 0x1234, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 1234, 789,
174 "1234:abcd:0:1::cdef%789", "[1234:abcd:0:1::cdef%789]:1234"
175 },
176 { // Ignore the first (non-longest) run of zeros.
177 { 0, 0, 0, 0x0123, 0, 0, 0, 0 }, 123, 0,
178 "0:0:0:123::", "[0:0:0:123::]:123"
179 },
180 { // Collapse the first (equally-longest) run of zeros.
181 { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }, 123, 0,
182 "1234:abcd::ff:0:0:cdef", "[1234:abcd::ff:0:0:cdef]:123"
183 },
184 { // Don't collapse "runs" of zeros of length 1.
185 { 0, 0xa, 1, 2, 3, 0, 5, 0 }, 123, 0,
186 "0:a:1:2:3:0:5:0", "[0:a:1:2:3:0:5:0]:123"
187 },
188 { // Collapse a run of zeros at the beginning.
189 { 0, 0, 0, 2, 3, 0, 0, 0 }, 123, 0,
190 "::2:3:0:0:0", "[::2:3:0:0:0]:123"
191 },
192 { // Collapse a run of zeros at the end.
193 { 0, 0xa, 1, 2, 3, 0, 0, 0 }, 123, 0,
194 "0:a:1:2:3::", "[0:a:1:2:3::]:123"
195 },
196 { // IPv4 192.168.1.2 embedded in IPv6 in the deprecated way.
197 { 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, 0,
198 "::192.168.1.2", "[::192.168.1.2]:123"
199 },
200 { // ... with non-zero scope.
201 { 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, 789,
202 "::192.168.1.2%789", "[::192.168.1.2%789]:123"
203 },
204 { // IPv4 192.168.1.2 embedded in IPv6.
205 { 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, 0,
206 "::ffff:192.168.1.2", "[::ffff:192.168.1.2]:123"
207 },
208 { // ... with non-zero scope.
209 { 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, 789,
210 "::ffff:192.168.1.2%789", "[::ffff:192.168.1.2%789]:123"
211 },
212 { // *Not* IPv4 embedded in IPv6.
213 { 0, 0, 0, 0, 0, 0x1234, 0xc0a8, 0x102 }, 123, 0,
214 "::1234:c0a8:102", "[::1234:c0a8:102]:123"
215 }
216 };
217
[email protected]2059f3d2011-12-01 20:12:37218 for (size_t i = 0; i < sizeof(test_cases)/sizeof(test_cases[0]); i++) {
[email protected]ba359352011-11-22 00:24:13219 PP_NetAddress_Private addr = MakeIPv6NetAddress(test_cases[i].address,
220 test_cases[i].port,
221 test_cases[i].scope);
222 ASSERT_EQ(test_cases[i].expected_without_port,
223 NetAddressPrivate::Describe(addr, false));
224 ASSERT_EQ(test_cases[i].expected_with_port,
225 NetAddressPrivate::Describe(addr, true));
226 }
227
228 PASS();
229}
[email protected]ee9bdfce2012-02-09 00:04:09230
231std::string TestNetAddressPrivate::TestGetFamily() {
[email protected]b13deef82012-03-22 22:41:06232 uint8_t localhost_ip[4] = { 127, 0, 0, 1 };
233 PP_NetAddress_Private ipv4 = MakeIPv4NetAddress(localhost_ip, 80);
[email protected]7be69ab2012-02-16 03:09:45234 ASSERT_EQ(NetAddressPrivate::GetFamily(ipv4), PP_NETADDRESSFAMILY_IPV4);
[email protected]ee9bdfce2012-02-09 00:04:09235
236 uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
[email protected]b13deef82012-03-22 22:41:06237 PP_NetAddress_Private ipv6 = MakeIPv6NetAddress(ipv6_address, 123, 0);
[email protected]7be69ab2012-02-16 03:09:45238 ASSERT_EQ(NetAddressPrivate::GetFamily(ipv6), PP_NETADDRESSFAMILY_IPV6);
[email protected]ee9bdfce2012-02-09 00:04:09239
240 PASS();
241}
242
243std::string TestNetAddressPrivate::TestGetPort() {
[email protected]b13deef82012-03-22 22:41:06244 uint8_t localhost_ip[4] = { 127, 0, 0, 1 };
245 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(localhost_ip, 80);
[email protected]ee9bdfce2012-02-09 00:04:09246 ASSERT_EQ(NetAddressPrivate::GetPort(localhost_80), 80);
247
248 uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
249
250 PP_NetAddress_Private port_123 = MakeIPv6NetAddress(ipv6_address, 123, 0);
251 ASSERT_EQ(NetAddressPrivate::GetPort(port_123), 123);
252
253 PP_NetAddress_Private port_FFFF = MakeIPv6NetAddress(ipv6_address,
254 0xFFFF,
255 0);
256 ASSERT_EQ(NetAddressPrivate::GetPort(port_FFFF), 0xFFFF);
257
258 PASS();
259}
260
261std::string TestNetAddressPrivate::TestGetAddress() {
262 const int addr_storage_len = 16;
263 unsigned char addr_storage[addr_storage_len];
264
[email protected]b13deef82012-03-22 22:41:06265 const uint8_t ipv4_addr[4] = { 127, 0, 0, 1 };
[email protected]ee9bdfce2012-02-09 00:04:09266 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(ipv4_addr, 80);
267 memset(addr_storage, 0, addr_storage_len);
268 ASSERT_TRUE(NetAddressPrivate::GetAddress(localhost_80,
269 addr_storage,
270 addr_storage_len));
[email protected]b13deef82012-03-22 22:41:06271 ASSERT_EQ(memcmp(addr_storage, &ipv4_addr, 4), 0);
[email protected]ee9bdfce2012-02-09 00:04:09272
273 // Insufficient storage for address.
274 ASSERT_FALSE(NetAddressPrivate::GetAddress(localhost_80,
275 addr_storage,
276 1));
277
278 uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
279 PP_NetAddress_Private ipv6_addr = MakeIPv6NetAddress(ipv6_address,
280 123,
281 0);
282
283 // Ensure the ipv6 address is transformed properly into network order.
[email protected]b13deef82012-03-22 22:41:06284 uint8_t ipv6_bytes[16];
285 for(int i = 0; i < 8; ++i) {
286 ipv6_bytes[i * 2] = ipv6_address[i] >> 8;
287 ipv6_bytes[i * 2 + 1] = ipv6_address[i] & 0xFF;
288 }
[email protected]ee9bdfce2012-02-09 00:04:09289
290 memset(addr_storage, 0, addr_storage_len);
291 ASSERT_TRUE(NetAddressPrivate::GetAddress(ipv6_addr,
292 addr_storage,
293 addr_storage_len));
[email protected]b13deef82012-03-22 22:41:06294 ASSERT_EQ(memcmp(addr_storage, ipv6_bytes, 16), 0);
[email protected]ee9bdfce2012-02-09 00:04:09295
296 // Insufficient storage for address.
297 ASSERT_FALSE(NetAddressPrivate::GetAddress(ipv6_addr,
298 addr_storage,
299 1));
300
301 PASS();
302}
[email protected]b13deef82012-03-22 22:41:06303
304std::string TestNetAddressPrivate::TestGetScopeID() {
305 uint8_t localhost_ip[4] = { 127, 0, 0, 1 };
306 PP_NetAddress_Private ipv4 = MakeIPv4NetAddress(localhost_ip, 80);
307 ASSERT_EQ(NetAddressPrivate::GetScopeID(ipv4), 0);
308
309 uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
310
311 PP_NetAddress_Private ipv6_123 = MakeIPv6NetAddress(ipv6_address, 0, 123);
312 ASSERT_EQ(NetAddressPrivate::GetScopeID(ipv6_123), 123);
313
314 PP_NetAddress_Private ipv6_max =
315 MakeIPv6NetAddress(ipv6_address, 0, 0xFFFFFFFF);
316 ASSERT_EQ(NetAddressPrivate::GetScopeID(ipv6_max), 0xFFFFFFFF);
317
318 PASS();
319}