1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/child/blink_platform_impl.h"
7 #include "base/run_loop.h"
8 #include "base/time/time.h"
9 #include "net/base/ip_address_number.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "url/origin.h"
17 TEST(BlinkPlatformTest
, IsReservedIPAddress
) {
18 BlinkPlatformImpl platform_impl
;
20 // Unreserved IPv4 addresses (in various forms).
21 EXPECT_FALSE(platform_impl
.isReservedIPAddress("8.8.8.8"));
22 EXPECT_FALSE(platform_impl
.isReservedIPAddress("99.64.0.0"));
23 EXPECT_FALSE(platform_impl
.isReservedIPAddress("212.15.0.0"));
24 EXPECT_FALSE(platform_impl
.isReservedIPAddress("212.15"));
25 EXPECT_FALSE(platform_impl
.isReservedIPAddress("212.15.0"));
26 EXPECT_FALSE(platform_impl
.isReservedIPAddress("3557752832"));
28 // Reserved IPv4 addresses (in various forms).
29 EXPECT_TRUE(platform_impl
.isReservedIPAddress("192.168.0.0"));
30 EXPECT_TRUE(platform_impl
.isReservedIPAddress("192.168.0.6"));
31 EXPECT_TRUE(platform_impl
.isReservedIPAddress("10.0.0.5"));
32 EXPECT_TRUE(platform_impl
.isReservedIPAddress("10.0.0"));
33 EXPECT_TRUE(platform_impl
.isReservedIPAddress("10.0"));
34 EXPECT_TRUE(platform_impl
.isReservedIPAddress("3232235526"));
36 // Unreserved IPv6 addresses.
37 EXPECT_FALSE(platform_impl
.isReservedIPAddress(
38 "[FFC0:ba98:7654:3210:FEDC:BA98:7654:3210]"));
39 EXPECT_FALSE(platform_impl
.isReservedIPAddress(
40 "[2000:ba98:7654:2301:EFCD:BA98:7654:3210]"));
42 // Reserved IPv6 addresses.
43 EXPECT_TRUE(platform_impl
.isReservedIPAddress("[::1]"));
44 EXPECT_TRUE(platform_impl
.isReservedIPAddress("[::192.9.5.5]"));
45 EXPECT_TRUE(platform_impl
.isReservedIPAddress("[FEED::BEEF]"));
46 EXPECT_TRUE(platform_impl
.isReservedIPAddress(
47 "[FEC0:ba98:7654:3210:FEDC:BA98:7654:3210]"));
49 // Not IP addresses at all.
50 EXPECT_FALSE(platform_impl
.isReservedIPAddress("example.com"));
51 EXPECT_FALSE(platform_impl
.isReservedIPAddress("127.0.0.1.example.com"));
54 uint8 address
[4] = {0, 0, 0, 1};
55 for (int i
= 0; i
< 256; i
++) {
57 std::string addressString
=
58 net::IPAddressToString(address
, sizeof(address
));
59 if (i
== 0 || i
== 10 || i
== 127 || i
> 223) {
60 EXPECT_TRUE(platform_impl
.isReservedIPAddress(
61 blink::WebString::fromUTF8(addressString
)));
63 EXPECT_FALSE(platform_impl
.isReservedIPAddress(
64 blink::WebString::fromUTF8(addressString
)));
69 TEST(BlinkPlatformTest
, portAllowed
) {
70 BlinkPlatformImpl platform_impl
;
71 EXPECT_TRUE(platform_impl
.portAllowed(GURL("http://example.com")));
72 EXPECT_TRUE(platform_impl
.portAllowed(GURL("file://example.com")));
73 EXPECT_TRUE(platform_impl
.portAllowed(GURL("file://example.com:87")));
74 EXPECT_TRUE(platform_impl
.portAllowed(GURL("ftp://example.com:21")));
75 EXPECT_FALSE(platform_impl
.portAllowed(GURL("ftp://example.com:87")));
76 EXPECT_FALSE(platform_impl
.portAllowed(GURL("ws://example.com:21")));
77 EXPECT_TRUE(platform_impl
.portAllowed(GURL("http://example.com:80")));
78 EXPECT_TRUE(platform_impl
.portAllowed(GURL("http://example.com:8889")));
81 TEST(BlinkPlatformTest
, castWebSecurityOrigin
) {
88 {"http://example.com", "http", "example.com", 80},
89 {"http://example.com:80", "http", "example.com", 80},
90 {"http://example.com:81", "http", "example.com", 81},
91 {"https://example.com", "https", "example.com", 443},
92 {"https://example.com:443", "https", "example.com", 443},
93 {"https://example.com:444", "https", "example.com", 444},
96 for (const auto& test
: cases
) {
97 blink::WebSecurityOrigin web_origin
=
98 blink::WebSecurityOrigin::createFromString(
99 blink::WebString::fromUTF8(test
.origin
));
100 EXPECT_EQ(test
.scheme
, web_origin
.protocol().utf8());
101 EXPECT_EQ(test
.host
, web_origin
.host().utf8());
102 EXPECT_EQ(test
.port
, web_origin
.effectivePort());
104 url::Origin url_origin
= web_origin
;
105 EXPECT_EQ(test
.scheme
, url_origin
.scheme());
106 EXPECT_EQ(test
.host
, url_origin
.host());
107 EXPECT_EQ(test
.port
, url_origin
.port());
109 web_origin
= url::Origin(GURL(test
.origin
));
110 EXPECT_EQ(test
.scheme
, web_origin
.protocol().utf8());
111 EXPECT_EQ(test
.host
, web_origin
.host().utf8());
112 EXPECT_EQ(test
.port
, web_origin
.effectivePort());
115 blink::WebSecurityOrigin web_origin
=
116 blink::WebSecurityOrigin::createUnique();
117 EXPECT_TRUE(web_origin
.isUnique());
119 url::Origin url_origin
= web_origin
;
120 EXPECT_TRUE(url_origin
.unique());
122 web_origin
= url::Origin(GURL(""));
123 EXPECT_TRUE(web_origin
.isUnique());
126 } // namespace content