• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "content/child/blink_platform_impl.h"
6 
7 #include "base/run_loop.h"
8 #include "base/time/time.h"
9 #include "net/base/net_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
13 
14 namespace content {
15 
16 // Derives BlinkPlatformImpl for testing shared timers.
17 class TestBlinkPlatformImpl : public BlinkPlatformImpl {
18  public:
TestBlinkPlatformImpl()19   TestBlinkPlatformImpl() : mock_monotonically_increasing_time_(0) {}
20 
21   // Returns mock time when enabled.
monotonicallyIncreasingTime()22   virtual double monotonicallyIncreasingTime() OVERRIDE {
23     if (mock_monotonically_increasing_time_ > 0.0)
24       return mock_monotonically_increasing_time_;
25     return BlinkPlatformImpl::monotonicallyIncreasingTime();
26   }
27 
OnStartSharedTimer(base::TimeDelta delay)28   virtual void OnStartSharedTimer(base::TimeDelta delay) OVERRIDE {
29     shared_timer_delay_ = delay;
30   }
31 
shared_timer_delay()32   base::TimeDelta shared_timer_delay() { return shared_timer_delay_; }
33 
set_mock_monotonically_increasing_time(double mock_time)34   void set_mock_monotonically_increasing_time(double mock_time) {
35     mock_monotonically_increasing_time_ = mock_time;
36   }
37 
38  private:
39   base::TimeDelta shared_timer_delay_;
40   double mock_monotonically_increasing_time_;
41 
42   DISALLOW_COPY_AND_ASSIGN(TestBlinkPlatformImpl);
43 };
44 
TEST(BlinkPlatformTest,SuspendResumeSharedTimer)45 TEST(BlinkPlatformTest, SuspendResumeSharedTimer) {
46   base::MessageLoop message_loop;
47 
48   TestBlinkPlatformImpl platform_impl;
49 
50   // Set a timer to fire as soon as possible.
51   platform_impl.setSharedTimerFireInterval(0);
52 
53   // Suspend timers immediately so the above timer wouldn't be fired.
54   platform_impl.SuspendSharedTimer();
55 
56   // The above timer would have posted a task which can be processed out of the
57   // message loop.
58   base::RunLoop().RunUntilIdle();
59 
60   // Set a mock time after 1 second to simulate timers suspended for 1 second.
61   double new_time = base::Time::Now().ToDoubleT() + 1;
62   platform_impl.set_mock_monotonically_increasing_time(new_time);
63 
64   // Resume timers so that the timer set above will be set again to fire
65   // immediately.
66   platform_impl.ResumeSharedTimer();
67 
68   EXPECT_TRUE(base::TimeDelta() == platform_impl.shared_timer_delay());
69 }
70 
TEST(BlinkPlatformTest,IsReservedIPAddress_WebURL)71 TEST(BlinkPlatformTest, IsReservedIPAddress_WebURL) {
72   TestBlinkPlatformImpl platform_impl;
73 
74   // Unreserved IPv4 addresses (in various forms).
75   EXPECT_FALSE(platform_impl.isReservedIPAddress(GURL("http://8.8.8.8/")));
76   EXPECT_FALSE(platform_impl.isReservedIPAddress(GURL("http://99.64.0.0/")));
77   EXPECT_FALSE(platform_impl.isReservedIPAddress(GURL("http://212.15.0.0/")));
78   EXPECT_FALSE(platform_impl.isReservedIPAddress(GURL("http://212.15/")));
79   EXPECT_FALSE(platform_impl.isReservedIPAddress(GURL("http://212.15.0/")));
80   EXPECT_FALSE(platform_impl.isReservedIPAddress(GURL("http://3557752832/")));
81 
82   // Reserved IPv4 addresses (in various forms).
83   EXPECT_TRUE(platform_impl.isReservedIPAddress(GURL("http://192.168.0.0/")));
84   EXPECT_TRUE(platform_impl.isReservedIPAddress(GURL("http://192.168.0.6/")));
85   EXPECT_TRUE(platform_impl.isReservedIPAddress(GURL("http://10.0.0.5/")));
86   EXPECT_TRUE(platform_impl.isReservedIPAddress(GURL("http://10.0.0/")));
87   EXPECT_TRUE(platform_impl.isReservedIPAddress(GURL("http://10.0/")));
88   EXPECT_TRUE(platform_impl.isReservedIPAddress(GURL("http://3232235526/")));
89 
90   // Unreserved IPv6 addresses.
91   EXPECT_FALSE(platform_impl.isReservedIPAddress(
92       GURL("http://[FFC0:ba98:7654:3210:FEDC:BA98:7654:3210]/")));
93   EXPECT_FALSE(platform_impl.isReservedIPAddress(
94       GURL("http://[2000:ba98:7654:2301:EFCD:BA98:7654:3210]/")));
95 
96   // Reserved IPv6 addresses.
97   EXPECT_TRUE(platform_impl.isReservedIPAddress(GURL("http://[::1]/")));
98   EXPECT_TRUE(platform_impl.isReservedIPAddress(GURL("http://[::192.9.5.5]/")));
99   EXPECT_TRUE(platform_impl.isReservedIPAddress(GURL("http://[FEED::BEEF]/")));
100   EXPECT_TRUE(platform_impl.isReservedIPAddress(
101       GURL("http://[FEC0:ba98:7654:3210:FEDC:BA98:7654:3210]/")));
102 
103   // Not IP addresses at all.
104   EXPECT_FALSE(platform_impl.isReservedIPAddress(GURL("http://example.com/")));
105   EXPECT_FALSE(
106       platform_impl.isReservedIPAddress(GURL("http://127.0.0.1.example.com/")));
107 
108   // Moar IPv4
109   uint8 address[4] = {0, 0, 0, 1};
110   for (int i = 0; i < 256; i++) {
111     address[0] = i;
112     std::string addressString =
113         net::IPAddressToString(address, sizeof(address));
114     if (i == 0 || i == 10 || i == 127 || i > 223) {
115       EXPECT_TRUE(
116           platform_impl.isReservedIPAddress(GURL("http://" + addressString)));
117     } else {
118       EXPECT_FALSE(
119           platform_impl.isReservedIPAddress(GURL("http://" + addressString)));
120     }
121   }
122 }
123 
TEST(BlinkPlatformTest,IsReservedIPAddress_WebSecurityOrigin)124 TEST(BlinkPlatformTest, IsReservedIPAddress_WebSecurityOrigin) {
125   TestBlinkPlatformImpl platform_impl;
126 
127   // Unreserved IPv4 addresses (in various forms).
128   EXPECT_FALSE(platform_impl.isReservedIPAddress(
129       blink::WebSecurityOrigin::createFromString("http://8.8.8.8/")));
130   EXPECT_FALSE(platform_impl.isReservedIPAddress(
131       blink::WebSecurityOrigin::createFromString("http://99.64.0.0/")));
132   EXPECT_FALSE(platform_impl.isReservedIPAddress(
133       blink::WebSecurityOrigin::createFromString("http://212.15.0.0/")));
134   EXPECT_FALSE(platform_impl.isReservedIPAddress(
135       blink::WebSecurityOrigin::createFromString("http://212.15/")));
136   EXPECT_FALSE(platform_impl.isReservedIPAddress(
137       blink::WebSecurityOrigin::createFromString("http://212.15.0/")));
138   EXPECT_FALSE(platform_impl.isReservedIPAddress(
139       blink::WebSecurityOrigin::createFromString("http://3557752832/")));
140 
141   // Reserved IPv4 addresses (in various forms).
142   EXPECT_TRUE(platform_impl.isReservedIPAddress(
143       blink::WebSecurityOrigin::createFromString("http://192.168.0.0/")));
144   EXPECT_TRUE(platform_impl.isReservedIPAddress(
145       blink::WebSecurityOrigin::createFromString("http://192.168.0.6/")));
146   EXPECT_TRUE(platform_impl.isReservedIPAddress(
147       blink::WebSecurityOrigin::createFromString("http://10.0.0.5/")));
148   EXPECT_TRUE(platform_impl.isReservedIPAddress(
149       blink::WebSecurityOrigin::createFromString("http://10.0.0/")));
150   EXPECT_TRUE(platform_impl.isReservedIPAddress(
151       blink::WebSecurityOrigin::createFromString("http://10.0/")));
152   EXPECT_TRUE(platform_impl.isReservedIPAddress(
153       blink::WebSecurityOrigin::createFromString("http://3232235526/")));
154 
155   // Unreserved IPv6 addresses.
156   EXPECT_FALSE(platform_impl.isReservedIPAddress(
157       blink::WebSecurityOrigin::createFromString(
158           "http://[FFC0:ba98:7654:3210:FEDC:BA98:7654:3210]/")));
159   EXPECT_FALSE(platform_impl.isReservedIPAddress(
160       blink::WebSecurityOrigin::createFromString(
161           "http://[2000:ba98:7654:2301:EFCD:BA98:7654:3210]/")));
162 
163   // Reserved IPv6 addresses.
164   EXPECT_TRUE(platform_impl.isReservedIPAddress(
165       blink::WebSecurityOrigin::createFromString("http://[::1]/")));
166   EXPECT_TRUE(platform_impl.isReservedIPAddress(
167       blink::WebSecurityOrigin::createFromString("http://[::192.9.5.5]/")));
168   EXPECT_TRUE(platform_impl.isReservedIPAddress(
169       blink::WebSecurityOrigin::createFromString("http://[FEED::BEEF]/")));
170   EXPECT_TRUE(platform_impl.isReservedIPAddress(
171       blink::WebSecurityOrigin::createFromString(
172           "http://[FEC0:ba98:7654:3210:FEDC:BA98:7654:3210]/")));
173 
174   // Not IP addresses at all.
175   EXPECT_FALSE(platform_impl.isReservedIPAddress(
176       blink::WebSecurityOrigin::createFromString("http://example.com/")));
177   EXPECT_FALSE(platform_impl.isReservedIPAddress(
178       blink::WebSecurityOrigin::createFromString(
179           "http://127.0.0.1.example.com/")));
180 
181   // Moar IPv4
182   uint8 address[4] = {0, 0, 0, 1};
183   for (int i = 0; i < 256; i++) {
184     address[0] = i;
185     blink::WebString addressString = blink::WebString::fromUTF8(
186         "http://" + net::IPAddressToString(address, sizeof(address)) + "/");
187     if (i == 0 || i == 10 || i == 127 || i > 223) {
188       EXPECT_TRUE(platform_impl.isReservedIPAddress(
189           blink::WebSecurityOrigin::createFromString(addressString)));
190     } else {
191       EXPECT_FALSE(platform_impl.isReservedIPAddress(
192           blink::WebSecurityOrigin::createFromString(addressString)));
193     }
194   }
195 }
196 
197 }  // namespace content
198