OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "content/child/blink_platform_impl.h" | 5 #include "content/child/blink_platform_impl.h" |
6 | 6 |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "net/base/ip_address_number.h" | 9 #include "net/base/ip_address_number.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "third_party/WebKit/public/platform/WebString.h" | 11 #include "third_party/WebKit/public/platform/WebString.h" |
12 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 12 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 // Derives BlinkPlatformImpl for testing shared timers. | |
17 class TestBlinkPlatformImpl : public BlinkPlatformImpl { | |
18 public: | |
19 TestBlinkPlatformImpl() : mock_monotonically_increasing_time_(0) {} | |
20 | |
21 // Returns mock time when enabled. | |
22 double monotonicallyIncreasingTime() override { | |
23 if (mock_monotonically_increasing_time_ > 0.0) | |
24 return mock_monotonically_increasing_time_; | |
25 return BlinkPlatformImpl::monotonicallyIncreasingTime(); | |
26 } | |
27 | |
28 void OnStartSharedTimer(base::TimeDelta delay) override { | |
29 shared_timer_delay_ = delay; | |
30 } | |
31 | |
32 base::TimeDelta shared_timer_delay() { return shared_timer_delay_; } | |
33 | |
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 | |
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 | |
71 TEST(BlinkPlatformTest, IsReservedIPAddress) { | 16 TEST(BlinkPlatformTest, IsReservedIPAddress) { |
72 TestBlinkPlatformImpl platform_impl; | 17 BlinkPlatformImpl platform_impl; |
73 | 18 |
74 // Unreserved IPv4 addresses (in various forms). | 19 // Unreserved IPv4 addresses (in various forms). |
75 EXPECT_FALSE(platform_impl.isReservedIPAddress("8.8.8.8")); | 20 EXPECT_FALSE(platform_impl.isReservedIPAddress("8.8.8.8")); |
76 EXPECT_FALSE(platform_impl.isReservedIPAddress("99.64.0.0")); | 21 EXPECT_FALSE(platform_impl.isReservedIPAddress("99.64.0.0")); |
77 EXPECT_FALSE(platform_impl.isReservedIPAddress("212.15.0.0")); | 22 EXPECT_FALSE(platform_impl.isReservedIPAddress("212.15.0.0")); |
78 EXPECT_FALSE(platform_impl.isReservedIPAddress("212.15")); | 23 EXPECT_FALSE(platform_impl.isReservedIPAddress("212.15")); |
79 EXPECT_FALSE(platform_impl.isReservedIPAddress("212.15.0")); | 24 EXPECT_FALSE(platform_impl.isReservedIPAddress("212.15.0")); |
80 EXPECT_FALSE(platform_impl.isReservedIPAddress("3557752832")); | 25 EXPECT_FALSE(platform_impl.isReservedIPAddress("3557752832")); |
81 | 26 |
82 // Reserved IPv4 addresses (in various forms). | 27 // Reserved IPv4 addresses (in various forms). |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 EXPECT_TRUE(platform_impl.isReservedIPAddress( | 59 EXPECT_TRUE(platform_impl.isReservedIPAddress( |
115 blink::WebString::fromUTF8(addressString))); | 60 blink::WebString::fromUTF8(addressString))); |
116 } else { | 61 } else { |
117 EXPECT_FALSE(platform_impl.isReservedIPAddress( | 62 EXPECT_FALSE(platform_impl.isReservedIPAddress( |
118 blink::WebString::fromUTF8(addressString))); | 63 blink::WebString::fromUTF8(addressString))); |
119 } | 64 } |
120 } | 65 } |
121 } | 66 } |
122 | 67 |
123 TEST(BlinkPlatformTest, portAllowed) { | 68 TEST(BlinkPlatformTest, portAllowed) { |
124 TestBlinkPlatformImpl platform_impl; | 69 BlinkPlatformImpl platform_impl; |
125 EXPECT_TRUE(platform_impl.portAllowed(GURL("http://example.com"))); | 70 EXPECT_TRUE(platform_impl.portAllowed(GURL("http://example.com"))); |
126 EXPECT_TRUE(platform_impl.portAllowed(GURL("file://example.com"))); | 71 EXPECT_TRUE(platform_impl.portAllowed(GURL("file://example.com"))); |
127 EXPECT_TRUE(platform_impl.portAllowed(GURL("file://example.com:87"))); | 72 EXPECT_TRUE(platform_impl.portAllowed(GURL("file://example.com:87"))); |
128 EXPECT_TRUE(platform_impl.portAllowed(GURL("ftp://example.com:21"))); | 73 EXPECT_TRUE(platform_impl.portAllowed(GURL("ftp://example.com:21"))); |
129 EXPECT_FALSE(platform_impl.portAllowed(GURL("ftp://example.com:87"))); | 74 EXPECT_FALSE(platform_impl.portAllowed(GURL("ftp://example.com:87"))); |
130 EXPECT_FALSE(platform_impl.portAllowed(GURL("ws://example.com:21"))); | 75 EXPECT_FALSE(platform_impl.portAllowed(GURL("ws://example.com:21"))); |
131 EXPECT_TRUE(platform_impl.portAllowed(GURL("http://example.com:80"))); | 76 EXPECT_TRUE(platform_impl.portAllowed(GURL("http://example.com:80"))); |
132 EXPECT_TRUE(platform_impl.portAllowed(GURL("http://example.com:8889"))); | 77 EXPECT_TRUE(platform_impl.portAllowed(GURL("http://example.com:8889"))); |
133 } | 78 } |
134 | 79 |
135 } // namespace content | 80 } // namespace content |
OLD | NEW |