Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: ppapi/tests/test_net_address_private.cc

Issue 9235035: Add getter methods for sockaddr. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Fixing errors found by review system. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/tests/test_net_address_private.h" 5 #include "ppapi/tests/test_net_address_private.h"
6 6
7 #include "ppapi/cpp/private/net_address_private.h" 7 #include "ppapi/cpp/private/net_address_private.h"
8 #include "ppapi/c/private/ppb_net_address_private.h" 8 #include "ppapi/c/private/ppb_net_address_private.h"
9 #include "ppapi/tests/test_utils.h" 9 #include "ppapi/tests/test_utils.h"
10 #include "ppapi/tests/testing_instance.h" 10 #include "ppapi/tests/testing_instance.h"
11 11
12 // Other than |GetAnyAddress()|, there's no way to actually get 12 // Other than |GetAnyAddress()|, there's no way to actually get
13 // |PP_NetAddress_Private| structs from just this interface. We'll cheat and 13 // |PP_NetAddress_Private| structs from just this interface. We'll cheat and
14 // synthesize some. 14 // synthesize some.
15 15
16 #if defined(PPAPI_POSIX) 16 #if defined(PPAPI_POSIX)
17 #include <arpa/inet.h> 17 #include <arpa/inet.h>
18 #include <netdb.h> 18 #include <netdb.h>
19 #include <netinet/in.h> 19 #include <netinet/in.h>
20 #include <sys/socket.h> 20 #include <sys/socket.h>
21 #include <string.h> // for memset/memcmp
21 #endif 22 #endif
22 23
23 #if defined(PPAPI_OS_MACOSX) 24 #if defined(PPAPI_OS_MACOSX)
24 // This is a bit evil, but it's standard operating procedure for |s6_addr|.... 25 // This is a bit evil, but it's standard operating procedure for |s6_addr|....
25 #define s6_addr16 __u6_addr.__u6_addr16 26 #define s6_addr16 __u6_addr.__u6_addr16
26 #endif 27 #endif
27 28
28 #if defined(PPAPI_OS_WIN) 29 #if defined(PPAPI_OS_WIN)
29 #include <ws2tcpip.h> 30 #include <ws2tcpip.h>
30 31
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 return NetAddressPrivate::IsAvailable(); 74 return NetAddressPrivate::IsAvailable();
74 } 75 }
75 76
76 void TestNetAddressPrivate::RunTests(const std::string& filter) { 77 void TestNetAddressPrivate::RunTests(const std::string& filter) {
77 RUN_TEST(AreEqual, filter); 78 RUN_TEST(AreEqual, filter);
78 RUN_TEST(AreHostsEqual, filter); 79 RUN_TEST(AreHostsEqual, filter);
79 RUN_TEST(Describe, filter); 80 RUN_TEST(Describe, filter);
80 RUN_TEST(ReplacePort, filter); 81 RUN_TEST(ReplacePort, filter);
81 RUN_TEST(GetAnyAddress, filter); 82 RUN_TEST(GetAnyAddress, filter);
82 RUN_TEST(DescribeIPv6, filter); 83 RUN_TEST(DescribeIPv6, filter);
84 RUN_TEST(GetFamily, filter);
85 RUN_TEST(GetPort, filter);
86 RUN_TEST(GetAddress, filter);
83 } 87 }
84 88
85 std::string TestNetAddressPrivate::TestAreEqual() { 89 std::string TestNetAddressPrivate::TestAreEqual() {
86 // No comparisons should ever be done with invalid addresses. 90 // No comparisons should ever be done with invalid addresses.
87 PP_NetAddress_Private invalid = PP_NetAddress_Private(); 91 PP_NetAddress_Private invalid = PP_NetAddress_Private();
88 ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid)); 92 ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid));
89 93
90 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80); 94 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
91 ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80)); 95 ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80));
92 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid)); 96 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid));
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 test_cases[i].port, 241 test_cases[i].port,
238 test_cases[i].scope); 242 test_cases[i].scope);
239 ASSERT_EQ(test_cases[i].expected_without_port, 243 ASSERT_EQ(test_cases[i].expected_without_port,
240 NetAddressPrivate::Describe(addr, false)); 244 NetAddressPrivate::Describe(addr, false));
241 ASSERT_EQ(test_cases[i].expected_with_port, 245 ASSERT_EQ(test_cases[i].expected_with_port,
242 NetAddressPrivate::Describe(addr, true)); 246 NetAddressPrivate::Describe(addr, true));
243 } 247 }
244 248
245 PASS(); 249 PASS();
246 } 250 }
251
252 std::string TestNetAddressPrivate::TestGetFamily() {
253 PP_NetAddress_Private ipv4 = MakeIPv4NetAddress("127.0.0.1", 80);
254 ASSERT_EQ(NetAddressPrivate::GetFamily(&ipv4), AF_INET);
255
256 uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
257 PP_NetAddress_Private ipv6 = MakeIPv6NetAddress(ipv6_address,
258 123,
259 0);
260 ASSERT_EQ(NetAddressPrivate::GetFamily(&ipv6), AF_INET6);
261
262 PASS();
263 }
264
265 std::string TestNetAddressPrivate::TestGetPort() {
266 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
267 ASSERT_EQ(NetAddressPrivate::GetPort(&localhost_80), 80);
268
269 uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
270
271 PP_NetAddress_Private port_123 = MakeIPv6NetAddress(ipv6_address, 123, 0);
272 ASSERT_EQ(NetAddressPrivate::GetPort(&port_123), 123);
273
274 PP_NetAddress_Private port_FFFF = MakeIPv6NetAddress(ipv6_address,
275 0xFFFF,
276 0);
277 ASSERT_EQ(NetAddressPrivate::GetPort(&port_FFFF), 0xFFFF);
278
279 PASS();
280 }
281
282 std::string TestNetAddressPrivate::TestGetAddress() {
283 const int addr_storage_len = 16;
284 unsigned char addr_storage[addr_storage_len];
285
286 const char* ipv4_addr = "127.0.0.1";
287 uint32_t ipv4_addr_long = inet_addr(ipv4_addr);
288 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress(ipv4_addr, 80);
289 memset(addr_storage, 0, addr_storage_len);
290 ASSERT_TRUE(NetAddressPrivate::GetAddress(&localhost_80,
291 addr_storage,
292 addr_storage_len));
293 ASSERT_EQ(memcmp(addr_storage, &ipv4_addr_long, 4), 0);
294
295 // Insufficient storage for address.
296 ASSERT_FALSE(NetAddressPrivate::GetAddress(&localhost_80,
297 addr_storage,
298 1));
299
300 uint16_t ipv6_address[8] = { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef };
301 PP_NetAddress_Private ipv6_addr = MakeIPv6NetAddress(ipv6_address,
302 123,
303 0);
304
305 // Ensure the ipv6 address is transformed properly into network order.
306 for (int i = 0; i < 8; i++)
307 ipv6_address[i] = htons(ipv6_address[i]);
308
309 memset(addr_storage, 0, addr_storage_len);
310 ASSERT_TRUE(NetAddressPrivate::GetAddress(&ipv6_addr,
311 addr_storage,
312 addr_storage_len));
313 ASSERT_EQ(memcmp(addr_storage, ipv6_address, 16), 0);
314
315 // Insufficient storage for address.
316 ASSERT_FALSE(NetAddressPrivate::GetAddress(&ipv6_addr,
317 addr_storage,
318 1));
319
320 PASS();
321 }
322
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698