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

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

Issue 8590006: Pepper: Implement PPB_NetAddress_Private Describe() for IPv6 addresses on Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: implement embedded IPv4 addresses, switch to our code on Mac Created 9 years, 1 month 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 "base/basictypes.h"
8 #include "build/build_config.h"
7 #include "ppapi/cpp/private/net_address_private.h" 9 #include "ppapi/cpp/private/net_address_private.h"
8 #include "ppapi/c/private/ppb_net_address_private.h" 10 #include "ppapi/c/private/ppb_net_address_private.h"
9 #include "ppapi/tests/testing_instance.h" 11 #include "ppapi/tests/testing_instance.h"
10 12
11 // Other than |GetAnyAddress()|, there's no way to actually get 13 // Other than |GetAnyAddress()|, there's no way to actually get
12 // |PP_NetAddress_Private| structs from just this interface. We'll cheat and 14 // |PP_NetAddress_Private| structs from just this interface. We'll cheat and
13 // synthesize some. 15 // synthesize some.
14 // TODO(viettrungluu): This is very fragile and implementation-dependent. :(
15 #if defined(_WIN32)
16 #define OS_WIN
17 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
18 defined(__OpenBSD__) || defined(__sun) || defined(__native_client__)
19 #define OS_POSIX
20 #else
21 #error "Unsupported platform."
22 #endif
23 16
24 #if defined(OS_WIN) 17 #if defined(OS_POSIX)
25 #include <ws2tcpip.h>
26 #elif defined(OS_POSIX)
27 #include <arpa/inet.h> 18 #include <arpa/inet.h>
28 #include <netdb.h> 19 #include <netdb.h>
29 #include <netinet/in.h> 20 #include <netinet/in.h>
30 #include <sys/socket.h> 21 #include <sys/socket.h>
31 #endif 22 #endif
32 23
24 #if defined(OS_MACOSX)
25 // This is a bit evil, but it's standard operating procedure for |s6_addr|....
26 #define s6_addr16 __u6_addr.__u6_addr16
27 #endif
28
29 #if defined(OS_WIN)
30 #include <ws2tcpip.h>
31
32 #define s6_addr16 u.Word
33 #endif
34
33 using pp::NetAddressPrivate; 35 using pp::NetAddressPrivate;
34 36
35 namespace { 37 namespace {
36 38
39 // |host| should be an IP address represented as text, e.g., "192.168.0.1".
37 PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) { 40 PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) {
38 PP_NetAddress_Private addr = PP_NetAddress_Private(); 41 PP_NetAddress_Private addr = PP_NetAddress_Private();
39 addr.size = sizeof(sockaddr_in); 42 addr.size = sizeof(sockaddr_in);
40 sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr.data); 43 sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr.data);
41 a->sin_family = AF_INET; 44 a->sin_family = AF_INET;
42 a->sin_port = htons(port); 45 a->sin_port = htons(port);
43 a->sin_addr.s_addr = inet_addr(host); 46 a->sin_addr.s_addr = inet_addr(host);
44 return addr; 47 return addr;
45 } 48 }
46 49
47 // TODO(viettrungluu): Also add IPv6 tests. 50 // |host| should be an array of eight 16-bit numbers.
51 PP_NetAddress_Private MakeIPv6NetAddress(const uint16_t host[], uint16_t port,
52 uint32_t scope_id) {
53 PP_NetAddress_Private addr = PP_NetAddress_Private();
54 addr.size = sizeof(sockaddr_in6);
55 sockaddr_in6* a = reinterpret_cast<sockaddr_in6*>(addr.data);
56 a->sin6_family = AF_INET6;
57 a->sin6_port = htons(port);
58 a->sin6_flowinfo = 0;
59 for (int i = 0; i < 8; i++)
60 a->sin6_addr.s6_addr16[i] = htons(host[i]);
61 a->sin6_scope_id = scope_id;
62 return addr;
63 }
48 64
49 } // namespace 65 } // namespace
50 66
51 REGISTER_TEST_CASE(NetAddressPrivate); 67 REGISTER_TEST_CASE(NetAddressPrivate);
52 68
53 TestNetAddressPrivate::TestNetAddressPrivate(TestingInstance* instance) 69 TestNetAddressPrivate::TestNetAddressPrivate(TestingInstance* instance)
54 : TestCase(instance) { 70 : TestCase(instance) {
55 } 71 }
56 72
57 bool TestNetAddressPrivate::Init() { 73 bool TestNetAddressPrivate::Init() {
58 return NetAddressPrivate::IsAvailable(); 74 return NetAddressPrivate::IsAvailable();
59 } 75 }
60 76
61 void TestNetAddressPrivate::RunTests(const std::string& filter) { 77 void TestNetAddressPrivate::RunTests(const std::string& filter) {
62 RUN_TEST(AreEqual, filter); 78 RUN_TEST(AreEqual, filter);
63 RUN_TEST(AreHostsEqual, filter); 79 RUN_TEST(AreHostsEqual, filter);
64 RUN_TEST(Describe, filter); 80 RUN_TEST(Describe, filter);
65 RUN_TEST(ReplacePort, filter); 81 RUN_TEST(ReplacePort, filter);
66 RUN_TEST(GetAnyAddress, filter); 82 RUN_TEST(GetAnyAddress, filter);
83 RUN_TEST(DescribeIPv6, filter);
67 } 84 }
68 85
69 std::string TestNetAddressPrivate::TestAreEqual() { 86 std::string TestNetAddressPrivate::TestAreEqual() {
70 // No comparisons should ever be done with invalid addresses. 87 // No comparisons should ever be done with invalid addresses.
71 PP_NetAddress_Private invalid = PP_NetAddress_Private(); 88 PP_NetAddress_Private invalid = PP_NetAddress_Private();
72 ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid)); 89 ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid));
73 90
74 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80); 91 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
75 ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80)); 92 ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80));
76 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid)); 93 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid));
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 PP_NetAddress_Private result = PP_NetAddress_Private(); 165 PP_NetAddress_Private result = PP_NetAddress_Private();
149 166
150 NetAddressPrivate::GetAnyAddress(false, &result); 167 NetAddressPrivate::GetAnyAddress(false, &result);
151 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result)); 168 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
152 169
153 NetAddressPrivate::GetAnyAddress(true, &result); 170 NetAddressPrivate::GetAnyAddress(true, &result);
154 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result)); 171 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
155 172
156 PASS(); 173 PASS();
157 } 174 }
175
176 // TODO(viettrungluu): More IPv6 tests needed.
177
178 std::string TestNetAddressPrivate::TestDescribeIPv6() {
179 static const struct {
180 uint16_t address[8];
181 uint16_t port;
182 uint32_t scope;
183 const char* expected_without_port;
184 const char* expected_with_port;
185 } test_cases[] = {
186 { // Generic test case (unique longest run of zeros to collapse).
187 { 0x12, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 12, 0,
188 "12:abcd:0:1::cdef", "[12:abcd:0:1::cdef]:12"
189 },
190 { // Non-zero scope.
191 { 0x1234, 0xabcd, 0, 0x0001, 0, 0, 0, 0xcdef }, 1234, 789,
192 "1234:abcd:0:1::cdef%789", "[1234:abcd:0:1::cdef%789]:1234"
193 },
194 { // Ignore the first (non-longest) run of zeros.
195 { 0, 0, 0, 0x0123, 0, 0, 0, 0 }, 123, 0,
196 "0:0:0:123::", "[0:0:0:123::]:123"
197 },
198 { // Collapse the first (equally-longest) run of zeros.
199 { 0x1234, 0xabcd, 0, 0, 0xff, 0, 0, 0xcdef }, 123, 0,
200 "1234:abcd::ff:0:0:cdef", "[1234:abcd::ff:0:0:cdef]:123"
201 },
202 { // Don't collapse "runs" of zeros of length 1.
203 { 0, 0xa, 1, 2, 3, 0, 5, 0 }, 123, 0,
204 "0:a:1:2:3:0:5:0", "[0:a:1:2:3:0:5:0]:123"
205 },
206 { // Collapse a run of zeros at the beginning.
207 { 0, 0, 0, 2, 3, 0, 0, 0 }, 123, 0,
208 "::2:3:0:0:0", "[::2:3:0:0:0]:123"
209 },
210 { // Collapse a run of zeros at the end.
211 { 0, 0xa, 1, 2, 3, 0, 0, 0 }, 123, 0,
212 "0:a:1:2:3::", "[0:a:1:2:3::]:123"
213 },
214 { // IPv4 192.168.1.2 embedded in IPv6 in the deprecated way.
215 { 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, 0,
216 "::192.168.1.2", "[::192.168.1.2]:123"
217 },
218 { // ... with non-zero scope.
219 { 0, 0, 0, 0, 0, 0, 0xc0a8, 0x102 }, 123, 789,
220 "::192.168.1.2%789", "[::192.168.1.2%789]:123"
221 },
222 { // IPv4 192.168.1.2 embedded in IPv6.
223 { 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, 0,
224 "::ffff:192.168.1.2", "[::ffff:192.168.1.2]:123"
225 },
226 { // ... with non-zero scope.
227 { 0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x102 }, 123, 789,
228 "::ffff:192.168.1.2%789", "[::ffff:192.168.1.2%789]:123"
229 },
230 { // *Not* IPv4 embedded in IPv6.
231 { 0, 0, 0, 0, 0, 0x1234, 0xc0a8, 0x102 }, 123, 0,
232 "::1234:c0a8:102", "[::1234:c0a8:102]:123"
233 }
234 };
235
236 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); i++) {
237 PP_NetAddress_Private addr = MakeIPv6NetAddress(test_cases[i].address,
238 test_cases[i].port,
239 test_cases[i].scope);
240 ASSERT_EQ(test_cases[i].expected_without_port,
241 NetAddressPrivate::Describe(addr, false));
242 ASSERT_EQ(test_cases[i].expected_with_port,
243 NetAddressPrivate::Describe(addr, true));
dmichael (off chromium) 2011/11/21 21:20:46 The downside with this approach is if one of these
viettrungluu 2011/11/21 21:33:48 I'll defer this, since what I'd really like is a m
244 }
245
246 PASS();
247 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698