clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 1 | // Copyright 2015 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 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 5 | #include "content/browser/renderer_host/navigation_request.h" |
Sebastien Marchand | f8cbfab | 2019-01-25 16:02:30 | [diff] [blame] | 6 | #include "base/bind.h" |
Charles Harrison | 860f7ef | 2017-06-28 15:31:41 | [diff] [blame] | 7 | #include "base/macros.h" |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 8 | #include "base/optional.h" |
Becky Zhou | 9898cb6e | 2019-06-05 00:35:30 | [diff] [blame] | 9 | #include "build/build_config.h" |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 10 | #include "content/public/browser/navigation_throttle.h" |
jam | 6d47c345 | 2016-09-09 18:51:01 | [diff] [blame] | 11 | #include "content/public/browser/ssl_status.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 12 | #include "content/public/common/content_client.h" |
Charles Harrison | 860f7ef | 2017-06-28 15:31:41 | [diff] [blame] | 13 | #include "content/public/common/url_constants.h" |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 14 | #include "content/public/test/test_navigation_throttle.h" |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 15 | #include "content/test/navigation_simulator_impl.h" |
Charles Harrison | 860f7ef | 2017-06-28 15:31:41 | [diff] [blame] | 16 | #include "content/test/test_content_browser_client.h" |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 17 | #include "content/test/test_render_frame_host.h" |
scottmg | 276753cf | 2016-10-27 18:25:22 | [diff] [blame] | 18 | #include "content/test/test_web_contents.h" |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 19 | #include "net/ssl/ssl_connection_status_flags.h" |
Richard Li | e689995 | 2018-11-30 08:42:00 | [diff] [blame] | 20 | #include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom.h" |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 21 | |
| 22 | namespace content { |
| 23 | |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 24 | // Test version of a NavigationThrottle that will execute a callback when |
| 25 | // called. |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 26 | class DeletingNavigationThrottle : public NavigationThrottle { |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 27 | public: |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 28 | DeletingNavigationThrottle(NavigationHandle* handle, |
| 29 | const base::RepeatingClosure& deletion_callback) |
| 30 | : NavigationThrottle(handle), deletion_callback_(deletion_callback) {} |
| 31 | ~DeletingNavigationThrottle() override {} |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 32 | |
| 33 | NavigationThrottle::ThrottleCheckResult WillStartRequest() override { |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 34 | deletion_callback_.Run(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 35 | return NavigationThrottle::PROCEED; |
| 36 | } |
| 37 | |
| 38 | NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override { |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 39 | deletion_callback_.Run(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 40 | return NavigationThrottle::PROCEED; |
| 41 | } |
| 42 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 43 | NavigationThrottle::ThrottleCheckResult WillFailRequest() override { |
| 44 | deletion_callback_.Run(); |
| 45 | return NavigationThrottle::PROCEED; |
| 46 | } |
| 47 | |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 48 | NavigationThrottle::ThrottleCheckResult WillProcessResponse() override { |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 49 | deletion_callback_.Run(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 50 | return NavigationThrottle::PROCEED; |
| 51 | } |
| 52 | |
| 53 | const char* GetNameForLogging() override { |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 54 | return "DeletingNavigationThrottle"; |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 58 | base::RepeatingClosure deletion_callback_; |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 59 | }; |
| 60 | |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 61 | class NavigationRequestTest : public RenderViewHostImplTestHarness { |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 62 | public: |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 63 | NavigationRequestTest() |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 64 | : was_callback_called_(false), |
| 65 | callback_result_(NavigationThrottle::DEFER) {} |
| 66 | |
| 67 | void SetUp() override { |
| 68 | RenderViewHostImplTestHarness::SetUp(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 69 | CreateNavigationHandle(); |
scottmg | 276753cf | 2016-10-27 18:25:22 | [diff] [blame] | 70 | contents()->GetMainFrame()->InitializeRenderFrameIfNeeded(); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void TearDown() override { |
Mohamed Abdelhalim | b7cabb1 | 2019-03-26 13:31:25 | [diff] [blame] | 74 | // Release the |request_| before destroying the WebContents, to match |
Avi Drissman | 650a39d1 | 2020-07-14 17:18:29 | [diff] [blame] | 75 | // the WebContentsObserverConsistencyChecker expectations. |
Mohamed Abdelhalim | b7cabb1 | 2019-03-26 13:31:25 | [diff] [blame] | 76 | request_.reset(); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 77 | RenderViewHostImplTestHarness::TearDown(); |
| 78 | } |
| 79 | |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 80 | void CancelDeferredNavigation( |
| 81 | NavigationThrottle::ThrottleCheckResult result) { |
Mohamed Abdelhalim | 9ef43fc | 2019-04-05 13:09:43 | [diff] [blame] | 82 | request_->CancelDeferredNavigationInternal(result); |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 83 | } |
| 84 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 85 | // Helper function to call WillStartRequest on |handle|. If this function |
| 86 | // returns DEFER, |callback_result_| will be set to the actual result of |
| 87 | // the throttle checks when they are finished. |
| 88 | void SimulateWillStartRequest() { |
| 89 | was_callback_called_ = false; |
| 90 | callback_result_ = NavigationThrottle::DEFER; |
| 91 | |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 92 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 93 | // the NavigationRequestTest. |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 94 | request_->set_complete_callback_for_testing( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 95 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 96 | base::Unretained(this))); |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 97 | |
| 98 | request_->WillStartRequest(); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Helper function to call WillRedirectRequest on |handle|. If this function |
| 102 | // returns DEFER, |callback_result_| will be set to the actual result of the |
| 103 | // throttle checks when they are finished. |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 104 | // TODO(clamy): this should also simulate that WillStartRequest was called if |
| 105 | // it has not been called before. |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 106 | void SimulateWillRedirectRequest() { |
| 107 | was_callback_called_ = false; |
| 108 | callback_result_ = NavigationThrottle::DEFER; |
| 109 | |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 110 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 111 | // the NavigationRequestTest. |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 112 | request_->set_complete_callback_for_testing( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 113 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 114 | base::Unretained(this))); |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 115 | |
Aaron Colwell | dc50263c | 2020-09-18 01:54:22 | [diff] [blame] | 116 | request_->WillRedirectRequest( |
Arthur Hemery | 92037961 | 2020-10-07 11:46:41 | [diff] [blame] | 117 | GURL(), CoopCoepCrossOriginIsolatedInfo::CreateNonIsolated(), |
Aaron Colwell | dc50263c | 2020-09-18 01:54:22 | [diff] [blame] | 118 | nullptr /* post_redirect_process */); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 119 | } |
| 120 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 121 | // Helper function to call WillFailRequest on |handle|. If this function |
| 122 | // returns DEFER, |callback_result_| will be set to the actual result of the |
| 123 | // throttle checks when they are finished. |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 124 | void SimulateWillFailRequest( |
| 125 | net::Error net_error_code, |
| 126 | const base::Optional<net::SSLInfo> ssl_info = base::nullopt) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 127 | was_callback_called_ = false; |
| 128 | callback_result_ = NavigationThrottle::DEFER; |
Mohamed Abdelhalim | b4db22a | 2019-06-18 10:46:52 | [diff] [blame] | 129 | request_->set_net_error(net_error_code); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 130 | |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 131 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 132 | // the NavigationRequestTest. |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 133 | request_->set_complete_callback_for_testing( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 134 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 135 | base::Unretained(this))); |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 136 | |
| 137 | request_->WillFailRequest(); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 138 | } |
| 139 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 140 | // Whether the callback was called. |
| 141 | bool was_callback_called() const { return was_callback_called_; } |
| 142 | |
| 143 | // Returns the callback_result. |
| 144 | NavigationThrottle::ThrottleCheckResult callback_result() const { |
| 145 | return callback_result_; |
| 146 | } |
| 147 | |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 148 | NavigationRequest::NavigationState state() { return request_->state(); } |
Mohamed Abdelhalim | ccd149af | 2019-10-31 14:48:53 | [diff] [blame] | 149 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 150 | bool call_counts_match(TestNavigationThrottle* throttle, |
| 151 | int start, |
| 152 | int redirect, |
| 153 | int failure, |
| 154 | int process) { |
| 155 | return start == throttle->GetCallCount( |
| 156 | TestNavigationThrottle::WILL_START_REQUEST) && |
| 157 | redirect == throttle->GetCallCount( |
| 158 | TestNavigationThrottle::WILL_REDIRECT_REQUEST) && |
| 159 | failure == throttle->GetCallCount( |
| 160 | TestNavigationThrottle::WILL_FAIL_REQUEST) && |
| 161 | process == throttle->GetCallCount( |
| 162 | TestNavigationThrottle::WILL_PROCESS_RESPONSE); |
| 163 | } |
| 164 | |
| 165 | // Creates, register and returns a TestNavigationThrottle that will |
| 166 | // synchronously return |result| on checks by default. |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 167 | TestNavigationThrottle* CreateTestNavigationThrottle( |
| 168 | NavigationThrottle::ThrottleCheckResult result) { |
| 169 | TestNavigationThrottle* test_throttle = |
Mohamed Abdelhalim | f03d4a2 | 2019-10-01 13:34:31 | [diff] [blame] | 170 | new TestNavigationThrottle(request_.get()); |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 171 | test_throttle->SetResponseForAllMethods(TestNavigationThrottle::SYNCHRONOUS, |
| 172 | result); |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 173 | request_->RegisterThrottleForTesting( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 174 | std::unique_ptr<TestNavigationThrottle>(test_throttle)); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 175 | return test_throttle; |
| 176 | } |
| 177 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 178 | // Creates, register and returns a TestNavigationThrottle that will |
| 179 | // synchronously return |result| on check for the given |method|, and |
| 180 | // NavigationThrottle::PROCEED otherwise. |
| 181 | TestNavigationThrottle* CreateTestNavigationThrottle( |
| 182 | TestNavigationThrottle::ThrottleMethod method, |
| 183 | NavigationThrottle::ThrottleCheckResult result) { |
| 184 | TestNavigationThrottle* test_throttle = |
| 185 | CreateTestNavigationThrottle(NavigationThrottle::PROCEED); |
| 186 | test_throttle->SetResponse(method, TestNavigationThrottle::SYNCHRONOUS, |
| 187 | result); |
| 188 | return test_throttle; |
| 189 | } |
| 190 | |
Mohamed Abdelhalim | 9ef43fc | 2019-04-05 13:09:43 | [diff] [blame] | 191 | // TODO(zetamoo): Use NavigationSimulator instead of creating |
| 192 | // NavigationRequest and NavigationHandleImpl. |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 193 | void CreateNavigationHandle() { |
Lucas Furukawa Gadani | eeddf2de | 2019-08-01 23:37:57 | [diff] [blame] | 194 | auto common_params = CreateCommonNavigationParams(); |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 195 | common_params->initiator_origin = |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 196 | url::Origin::Create(GURL("https://initiator.example.com")); |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 197 | auto commit_params = CreateCommitNavigationParams(); |
| 198 | commit_params->frame_policy = |
| 199 | main_test_rfh()->frame_tree_node()->pending_frame_policy(); |
Camille Lamy | 7c02ec70 | 2019-01-17 17:50:23 | [diff] [blame] | 200 | request_ = NavigationRequest::CreateBrowserInitiated( |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 201 | main_test_rfh()->frame_tree_node(), std::move(common_params), |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 202 | std::move(commit_params), false /* browser-initiated */, |
John Delaney | f43556d | 2020-05-04 23:19:06 | [diff] [blame] | 203 | GlobalFrameRoutingId() /* initiator_routing_id */, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 204 | std::string() /* extra_headers */, nullptr /* frame_entry */, |
| 205 | nullptr /* entry */, nullptr /* post_body */, |
| 206 | nullptr /* navigation_ui_data */, base::nullopt /* impression */); |
Mohamed Abdelhalim | f03d4a2 | 2019-10-01 13:34:31 | [diff] [blame] | 207 | request_->StartNavigation(true); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 208 | } |
| 209 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 210 | private: |
Mohamed Abdelhalim | f03d4a2 | 2019-10-01 13:34:31 | [diff] [blame] | 211 | // The callback provided to NavigationRequest::WillStartRequest, |
| 212 | // NavigationRequest::WillRedirectRequest, and |
| 213 | // NavigationRequest::WillFailRequest during the tests. |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 214 | bool UpdateThrottleCheckResult( |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 215 | NavigationThrottle::ThrottleCheckResult result) { |
| 216 | callback_result_ = result; |
| 217 | was_callback_called_ = true; |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 218 | return true; |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 219 | } |
| 220 | |
Camille Lamy | 7c02ec70 | 2019-01-17 17:50:23 | [diff] [blame] | 221 | std::unique_ptr<NavigationRequest> request_; |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 222 | bool was_callback_called_; |
| 223 | NavigationThrottle::ThrottleCheckResult callback_result_; |
| 224 | }; |
| 225 | |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 226 | // Checks that the request_context_type is properly set. |
| 227 | // Note: can be extended to cover more internal members. |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 228 | TEST_F(NavigationRequestTest, SimpleDataChecksRedirectAndProcess) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 229 | const GURL kUrl1 = GURL("http://chromium.org"); |
| 230 | const GURL kUrl2 = GURL("http://google.com"); |
| 231 | auto navigation = |
| 232 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl1, main_rfh()); |
| 233 | navigation->Start(); |
| 234 | EXPECT_EQ(blink::mojom::RequestContextType::HYPERLINK, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 235 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 236 | ->request_context_type()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 237 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 238 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 239 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 240 | navigation->set_http_connection_info( |
| 241 | net::HttpResponseInfo::CONNECTION_INFO_HTTP1_1); |
| 242 | navigation->Redirect(kUrl2); |
| 243 | EXPECT_EQ(blink::mojom::RequestContextType::HYPERLINK, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 244 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 245 | ->request_context_type()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 246 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_HTTP1_1, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 247 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 248 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 249 | navigation->set_http_connection_info( |
| 250 | net::HttpResponseInfo::CONNECTION_INFO_QUIC_35); |
| 251 | navigation->ReadyToCommit(); |
| 252 | EXPECT_EQ(blink::mojom::RequestContextType::HYPERLINK, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 253 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 254 | ->request_context_type()); |
bnc | 90be5dd78 | 2016-11-09 16:28:44 | [diff] [blame] | 255 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_QUIC_35, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 256 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 257 | } |
| 258 | |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 259 | TEST_F(NavigationRequestTest, SimpleDataCheckNoRedirect) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 260 | const GURL kUrl = GURL("http://chromium.org"); |
| 261 | auto navigation = |
| 262 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 263 | navigation->Start(); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 264 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 265 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 266 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 267 | navigation->set_http_connection_info( |
| 268 | net::HttpResponseInfo::CONNECTION_INFO_QUIC_35); |
| 269 | navigation->ReadyToCommit(); |
bnc | 90be5dd78 | 2016-11-09 16:28:44 | [diff] [blame] | 270 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_QUIC_35, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 271 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 272 | } |
| 273 | |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 274 | TEST_F(NavigationRequestTest, SimpleDataChecksFailure) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 275 | const GURL kUrl = GURL("http://chromium.org"); |
| 276 | auto navigation = |
| 277 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 278 | navigation->Start(); |
| 279 | EXPECT_EQ(blink::mojom::RequestContextType::HYPERLINK, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 280 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 281 | ->request_context_type()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 282 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 283 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 284 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 285 | navigation->Fail(net::ERR_CERT_DATE_INVALID); |
| 286 | EXPECT_EQ(blink::mojom::RequestContextType::HYPERLINK, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 287 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 288 | ->request_context_type()); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 289 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, |
| 290 | navigation->GetNavigationHandle()->GetNetErrorCode()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 291 | } |
| 292 | |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 293 | // Checks that a navigation deferred during WillStartRequest can be properly |
| 294 | // cancelled. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 295 | TEST_F(NavigationRequestTest, CancelDeferredWillStart) { |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 296 | TestNavigationThrottle* test_throttle = |
| 297 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 298 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 299 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 300 | |
| 301 | // Simulate WillStartRequest. The request should be deferred. The callback |
| 302 | // should not have been called. |
| 303 | SimulateWillStartRequest(); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 304 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 305 | EXPECT_FALSE(was_callback_called()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 306 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 307 | |
| 308 | // Cancel the request. The callback should have been called. |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 309 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 310 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 311 | EXPECT_TRUE(was_callback_called()); |
| 312 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 313 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Checks that a navigation deferred during WillRedirectRequest can be properly |
| 317 | // cancelled. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 318 | TEST_F(NavigationRequestTest, CancelDeferredWillRedirect) { |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 319 | TestNavigationThrottle* test_throttle = |
| 320 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 321 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 322 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 323 | |
| 324 | // Simulate WillRedirectRequest. The request should be deferred. The callback |
| 325 | // should not have been called. |
| 326 | SimulateWillRedirectRequest(); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 327 | EXPECT_EQ(NavigationRequest::WILL_REDIRECT_REQUEST, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 328 | EXPECT_FALSE(was_callback_called()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 329 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 1, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 330 | |
| 331 | // Cancel the request. The callback should have been called. |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 332 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 333 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 334 | EXPECT_TRUE(was_callback_called()); |
| 335 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 336 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 1, 0, 0)); |
| 337 | } |
| 338 | |
| 339 | // Checks that a navigation deferred during WillFailRequest can be properly |
| 340 | // cancelled. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 341 | TEST_F(NavigationRequestTest, CancelDeferredWillFail) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 342 | TestNavigationThrottle* test_throttle = CreateTestNavigationThrottle( |
| 343 | TestNavigationThrottle::WILL_FAIL_REQUEST, NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 344 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 345 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
| 346 | |
| 347 | // Simulate WillStartRequest. |
| 348 | SimulateWillStartRequest(); |
| 349 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
| 350 | |
| 351 | // Simulate WillFailRequest. The request should be deferred. The callback |
| 352 | // should not have been called. |
| 353 | SimulateWillFailRequest(net::ERR_CERT_DATE_INVALID); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 354 | EXPECT_EQ(NavigationRequest::WILL_FAIL_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 355 | EXPECT_FALSE(was_callback_called()); |
| 356 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0)); |
| 357 | |
| 358 | // Cancel the request. The callback should have been called. |
| 359 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 360 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 361 | EXPECT_TRUE(was_callback_called()); |
| 362 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
| 363 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // Checks that a navigation deferred can be canceled and not ignored. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 367 | TEST_F(NavigationRequestTest, CancelDeferredWillRedirectNoIgnore) { |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 368 | TestNavigationThrottle* test_throttle = |
| 369 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 370 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 371 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 372 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 373 | // Simulate WillStartRequest. The request should be deferred. The callback |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 374 | // should not have been called. |
| 375 | SimulateWillStartRequest(); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 376 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 377 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 378 | |
| 379 | // Cancel the request. The callback should have been called with CANCEL, and |
| 380 | // not CANCEL_AND_IGNORE. |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 381 | CancelDeferredNavigation(NavigationThrottle::CANCEL); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 382 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 383 | EXPECT_TRUE(was_callback_called()); |
| 384 | EXPECT_EQ(NavigationThrottle::CANCEL, callback_result()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 385 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 386 | } |
| 387 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 388 | // Checks that a navigation deferred by WillFailRequest can be canceled and not |
| 389 | // ignored. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 390 | TEST_F(NavigationRequestTest, CancelDeferredWillFailNoIgnore) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 391 | TestNavigationThrottle* test_throttle = CreateTestNavigationThrottle( |
| 392 | TestNavigationThrottle::WILL_FAIL_REQUEST, NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 393 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 394 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
| 395 | |
| 396 | // Simulate WillStartRequest. |
| 397 | SimulateWillStartRequest(); |
| 398 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
| 399 | |
| 400 | // Simulate WillFailRequest. The request should be deferred. The callback |
| 401 | // should not have been called. |
| 402 | SimulateWillFailRequest(net::ERR_CERT_DATE_INVALID); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 403 | EXPECT_EQ(NavigationRequest::WILL_FAIL_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 404 | EXPECT_FALSE(was_callback_called()); |
| 405 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0)); |
| 406 | |
| 407 | // Cancel the request. The callback should have been called with CANCEL, and |
| 408 | // not CANCEL_AND_IGNORE. |
| 409 | CancelDeferredNavigation(NavigationThrottle::CANCEL); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 410 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 411 | EXPECT_TRUE(was_callback_called()); |
| 412 | EXPECT_EQ(NavigationThrottle::CANCEL, callback_result()); |
| 413 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0)); |
| 414 | } |
| 415 | |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 416 | // Checks that data from the SSLInfo passed into SimulateWillStartRequest() is |
John Abd-El-Malek | f36e05f | 2017-11-30 16:17:52 | [diff] [blame] | 417 | // stored on the handle. |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 418 | TEST_F(NavigationRequestTest, WillFailRequestSetsSSLInfo) { |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 419 | uint16_t cipher_suite = 0xc02f; // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 |
| 420 | int connection_status = 0; |
| 421 | net::SSLConnectionStatusSetCipherSuite(cipher_suite, &connection_status); |
| 422 | |
| 423 | // Set some test values. |
| 424 | net::SSLInfo ssl_info; |
| 425 | ssl_info.cert_status = net::CERT_STATUS_AUTHORITY_INVALID; |
| 426 | ssl_info.connection_status = connection_status; |
| 427 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 428 | const GURL kUrl = GURL("https://chromium.org"); |
| 429 | auto navigation = |
| 430 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
Emily Stark | fd6978ad1 | 2019-04-30 21:20:07 | [diff] [blame] | 431 | navigation->SetSSLInfo(ssl_info); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 432 | navigation->Fail(net::ERR_CERT_DATE_INVALID); |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 433 | |
| 434 | EXPECT_EQ(net::CERT_STATUS_AUTHORITY_INVALID, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 435 | navigation->GetNavigationHandle()->GetSSLInfo()->cert_status); |
| 436 | EXPECT_EQ(connection_status, |
| 437 | navigation->GetNavigationHandle()->GetSSLInfo()->connection_status); |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 438 | } |
| 439 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 440 | namespace { |
| 441 | |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 442 | // Helper throttle which checks that it can access NavigationHandle's |
| 443 | // RenderFrameHost in WillFailRequest() and then defers the failure. |
| 444 | class GetRenderFrameHostOnFailureNavigationThrottle |
| 445 | : public NavigationThrottle { |
| 446 | public: |
| 447 | GetRenderFrameHostOnFailureNavigationThrottle(NavigationHandle* handle) |
| 448 | : NavigationThrottle(handle) {} |
| 449 | ~GetRenderFrameHostOnFailureNavigationThrottle() override {} |
| 450 | |
| 451 | NavigationThrottle::ThrottleCheckResult WillFailRequest() override { |
| 452 | EXPECT_TRUE(navigation_handle()->GetRenderFrameHost()); |
| 453 | return NavigationThrottle::DEFER; |
| 454 | } |
| 455 | |
| 456 | const char* GetNameForLogging() override { |
| 457 | return "GetRenderFrameHostOnFailureNavigationThrottle"; |
| 458 | } |
| 459 | |
| 460 | private: |
| 461 | DISALLOW_COPY_AND_ASSIGN(GetRenderFrameHostOnFailureNavigationThrottle); |
| 462 | }; |
| 463 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 464 | class ThrottleTestContentBrowserClient : public ContentBrowserClient { |
| 465 | std::vector<std::unique_ptr<NavigationThrottle>> CreateThrottlesForNavigation( |
| 466 | NavigationHandle* navigation_handle) override { |
| 467 | std::vector<std::unique_ptr<NavigationThrottle>> throttle; |
| 468 | throttle.push_back( |
| 469 | std::make_unique<GetRenderFrameHostOnFailureNavigationThrottle>( |
| 470 | navigation_handle)); |
| 471 | return throttle; |
| 472 | } |
| 473 | }; |
| 474 | |
| 475 | } // namespace |
| 476 | |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 477 | // Verify that the NavigationHandle::GetRenderFrameHost() can be retrieved by a |
| 478 | // throttle in WillFailRequest(), as well as after deferring the failure. This |
| 479 | // is allowed, since at that point the final RenderFrameHost will have already |
| 480 | // been chosen. See https://crbug.com/817881. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 481 | TEST_F(NavigationRequestTest, WillFailRequestCanAccessRenderFrameHost) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 482 | std::unique_ptr<ContentBrowserClient> client( |
| 483 | new ThrottleTestContentBrowserClient); |
| 484 | ContentBrowserClient* old_browser_client = |
| 485 | SetBrowserClientForTesting(client.get()); |
| 486 | |
| 487 | const GURL kUrl = GURL("http://chromium.org"); |
| 488 | auto navigation = |
| 489 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 490 | navigation->SetAutoAdvance(false); |
| 491 | navigation->Start(); |
| 492 | navigation->Fail(net::ERR_CERT_DATE_INVALID); |
Mohamed Abdelhalim | 3b23527 | 2019-11-05 15:06:07 | [diff] [blame] | 493 | EXPECT_EQ( |
| 494 | NavigationRequest::WILL_FAIL_REQUEST, |
| 495 | NavigationRequest::From(navigation->GetNavigationHandle())->state()); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 496 | EXPECT_TRUE(navigation->GetNavigationHandle()->GetRenderFrameHost()); |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 497 | NavigationRequest::From(navigation->GetNavigationHandle()) |
danakj | f26536bf | 2020-09-10 00:46:13 | [diff] [blame] | 498 | ->GetNavigationThrottleRunnerForTesting() |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 499 | ->CallResumeForTesting(); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 500 | EXPECT_TRUE(navigation->GetNavigationHandle()->GetRenderFrameHost()); |
| 501 | |
| 502 | SetBrowserClientForTesting(old_browser_client); |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 503 | } |
| 504 | |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 505 | TEST_F(NavigationRequestTest, PolicyContainerInheritance) { |
| 506 | struct TestCase { |
| 507 | const char* url; |
| 508 | bool expect_inherit; |
| 509 | } cases[]{{"about:blank", true}, |
| 510 | {"data:text/plain,hello", true}, |
| 511 | {"file://local", false}, |
| 512 | {"http://chromium.org", false}}; |
| 513 | |
| 514 | const GURL kUrl1 = GURL("http://chromium.org"); |
| 515 | auto navigation = |
| 516 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl1, main_rfh()); |
| 517 | navigation->Commit(); |
| 518 | |
| 519 | for (auto test : cases) { |
| 520 | // We navigate child frames because the BlockedSchemeNavigationThrottle |
| 521 | // restricts navigations in the main frame. |
| 522 | auto* child_frame = static_cast<TestRenderFrameHost*>( |
| 523 | content::RenderFrameHostTester::For(main_rfh())->AppendChild("child")); |
| 524 | |
| 525 | // We set the referrer policy of the frame to "always". We then create a new |
| 526 | // navigation, set as initiator the frame itself, start the navigation, and |
| 527 | // change the referrer policy of the frame to "never". After we commit the |
| 528 | // navigation: |
| 529 | // - If navigating to a local scheme, the target frame should have inherited |
| 530 | // the referrer policy of the initiator ("always"). |
| 531 | // - If navigating to a non-local scheme, the target frame should have a new |
| 532 | // policy container (hence referrer policy set to "default"). |
| 533 | const GURL kUrl = GURL(test.url); |
| 534 | auto navigation = |
| 535 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, child_frame); |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame^] | 536 | static_cast<blink::mojom::PolicyContainerHost*>( |
| 537 | child_frame->policy_container()) |
| 538 | ->SetReferrerPolicy(network::mojom::ReferrerPolicy::kAlways); |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 539 | navigation->SetInitiatorFrame(child_frame); |
| 540 | navigation->Start(); |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame^] | 541 | static_cast<blink::mojom::PolicyContainerHost*>( |
| 542 | child_frame->policy_container()) |
| 543 | ->SetReferrerPolicy(network::mojom::ReferrerPolicy::kNever); |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 544 | navigation->Commit(); |
| 545 | EXPECT_EQ( |
| 546 | test.expect_inherit ? network::mojom::ReferrerPolicy::kAlways |
| 547 | : network::mojom::ReferrerPolicy::kDefault, |
| 548 | static_cast<RenderFrameHostImpl*>(navigation->GetFinalRenderFrameHost()) |
| 549 | ->policy_container() |
| 550 | ->referrer_policy()); |
| 551 | } |
| 552 | } |
| 553 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 554 | } // namespace content |