Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "base/bind.h" | |
| 6 #include "base/run_loop.h" | |
| 7 #include "components/web_restrictions/browser/mock_web_restrictions_client.h" | |
| 8 #include "components/web_restrictions/browser/web_restrictions_client.h" | |
| 9 #include "components/web_restrictions/browser/web_restrictions_resource_throttle .h" | |
| 10 #include "content/public/browser/resource_controller.h" | |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "net/url_request/redirect_info.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace web_restrictions { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class TestResourceController : public content::ResourceController { | |
| 21 public: | |
| 22 TestResourceController(const base::Closure& quit_closure) | |
| 23 : resume_called_(false), | |
| 24 cancel_with_error_called_(false), | |
| 25 error_code_(0), | |
| 26 quit_closure_(quit_closure) {} | |
| 27 | |
| 28 void Cancel() override {} | |
| 29 void CancelAndIgnore() override {} | |
| 30 void CancelWithError(int error_code) override { | |
| 31 cancel_with_error_called_ = true; | |
| 32 error_code_ = error_code; | |
| 33 quit_closure_.Run(); | |
| 34 } | |
| 35 void Resume() override { | |
| 36 resume_called_ = true; | |
| 37 quit_closure_.Run(); | |
| 38 } | |
| 39 | |
| 40 bool CancelWithErrorCalled() const { return cancel_with_error_called_; } | |
| 41 | |
| 42 int GetErrorCode() const { return error_code_; } | |
| 43 | |
| 44 bool ResumeCalled() const { return resume_called_; } | |
| 45 | |
| 46 private: | |
| 47 bool resume_called_; | |
| 48 bool cancel_with_error_called_; | |
| 49 int error_code_; | |
| 50 base::Closure quit_closure_; | |
| 51 }; | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 class WebRestrictionsResourceThrottleTest : public testing::Test { | |
| 56 protected: | |
| 57 // Mock the Java WebRestrictionsClient. The real version | |
|
Bernhard Bauer
2016/02/23 14:29:13
Move members after methods please.
aberent
2016/02/23 16:26:51
Done.
| |
| 58 // would need a content provider to do anything. | |
| 59 web_restrictions::MockWebRestrictionsClient mock_; | |
| 60 content::TestBrowserThreadBundle thread_bundle_; | |
| 61 WebRestrictionsClient provider_; | |
| 62 WebRestrictionsResourceThrottle throttle_; | |
| 63 base::RunLoop run_loop_; | |
| 64 TestResourceController controller_; | |
| 65 | |
| 66 WebRestrictionsResourceThrottleTest() | |
| 67 : throttle_(&provider_, GURL("http://example.com"), true), | |
| 68 controller_(run_loop_.QuitClosure()) { | |
| 69 throttle_.set_controller_for_testing(&controller_); | |
| 70 } | |
| 71 | |
| 72 void StartProvider() { | |
| 73 provider_.SetAuthority("Good"); | |
| 74 bool defer; | |
| 75 throttle_.WillStartRequest(&defer); | |
| 76 run_loop_.Run(); | |
| 77 } | |
| 78 }; | |
| 79 | |
| 80 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_NoAuthority) { | |
| 81 WebRestrictionsResourceThrottle throttle(&provider_, | |
| 82 GURL("http://example.com"), true); | |
| 83 bool defer; | |
| 84 throttle.WillStartRequest(&defer); | |
| 85 // If there is no authority the request won't be deferred. | |
| 86 EXPECT_FALSE(defer); | |
| 87 } | |
| 88 | |
| 89 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredAllow) { | |
| 90 // Test deferring with a resource provider, and that the correct results | |
| 91 // are received. | |
| 92 provider_.SetAuthority("Good"); | |
| 93 bool defer; | |
| 94 throttle_.WillStartRequest(&defer); | |
| 95 EXPECT_TRUE(defer); | |
| 96 run_loop_.Run(); | |
| 97 EXPECT_TRUE(controller_.ResumeCalled()); | |
| 98 EXPECT_FALSE(controller_.CancelWithErrorCalled()); | |
| 99 } | |
| 100 | |
| 101 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredForbid) { | |
| 102 provider_.SetAuthority("Bad"); | |
| 103 bool defer; | |
| 104 throttle_.WillStartRequest(&defer); | |
| 105 EXPECT_TRUE(defer); | |
| 106 run_loop_.Run(); | |
| 107 EXPECT_FALSE(controller_.ResumeCalled()); | |
| 108 EXPECT_TRUE(controller_.CancelWithErrorCalled()); | |
| 109 EXPECT_EQ(net::ERR_BLOCKED_BY_ADMINISTRATOR, controller_.GetErrorCode()); | |
| 110 } | |
| 111 | |
| 112 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_Subresource) { | |
| 113 // Only the main frame should be deferred. | |
| 114 // Initialization of the controller is asynchronous, and this will only work | |
| 115 // correctly if the provider is initialized. Run a main frame through this | |
| 116 // first to ensure that everything is initialized. | |
| 117 StartProvider(); | |
| 118 // Now the real test. | |
| 119 WebRestrictionsResourceThrottle throttle( | |
| 120 &provider_, GURL("http://example.com/sub"), false); | |
| 121 base::RunLoop test_run_loop; | |
| 122 TestResourceController test_controller(test_run_loop.QuitClosure()); | |
| 123 throttle.set_controller_for_testing(&test_controller); | |
| 124 bool defer; | |
| 125 throttle.WillStartRequest(&defer); | |
| 126 EXPECT_FALSE(defer); | |
| 127 } | |
| 128 | |
| 129 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_KnownUrl) { | |
| 130 // Set up a cached url. | |
| 131 StartProvider(); | |
| 132 // Using the same URL should not be deferred | |
| 133 net::RedirectInfo redirect; | |
| 134 redirect.new_url = GURL("http://example.com"); | |
| 135 bool defer; | |
| 136 throttle_.WillRedirectRequest(redirect, &defer); | |
| 137 EXPECT_FALSE(defer); | |
| 138 } | |
| 139 | |
| 140 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_NewUrl) { | |
| 141 // Set up a cached url. | |
| 142 StartProvider(); | |
| 143 // Using a different URL should be deferred | |
| 144 net::RedirectInfo redirect; | |
| 145 redirect.new_url = GURL("http://example.com/2"); | |
| 146 base::RunLoop test_run_loop; | |
| 147 TestResourceController test_controller(test_run_loop.QuitClosure()); | |
| 148 throttle_.set_controller_for_testing(&test_controller); | |
| 149 bool defer; | |
| 150 throttle_.WillRedirectRequest(redirect, &defer); | |
| 151 EXPECT_TRUE(defer); | |
| 152 // If we don't wait for the callback it may happen after the exit, which | |
| 153 // results in accesses the redirect_url after the stack frame is freed. | |
| 154 test_run_loop.Run(); | |
| 155 } | |
| 156 | |
| 157 } // namespace web_restrictions | |
| OLD | NEW |