Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 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" |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 6 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 7 | #include <optional> |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 11 | #include "base/containers/flat_map.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 12 | #include "base/functional/bind.h" |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 13 | #include "base/i18n/number_formatting.h" |
Lei Zhang | 0a85e65a | 2025-05-23 19:22:06 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 15 | #include "base/test/scoped_feature_list.h" |
Becky Zhou | 9898cb6e | 2019-06-05 00:35:30 | [diff] [blame] | 16 | #include "build/build_config.h" |
Takashi Toyoshima | cc4d947f | 2025-06-24 08:38:34 | [diff] [blame] | 17 | #include "content/browser/renderer_host/navigation_throttle_runner.h" |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 18 | #include "content/public/browser/navigation_throttle.h" |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 19 | #include "content/public/browser/origin_trials_controller_delegate.h" |
jam | 6d47c345 | 2016-09-09 18:51:01 | [diff] [blame] | 20 | #include "content/public/browser/ssl_status.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 21 | #include "content/public/common/content_client.h" |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 22 | #include "content/public/common/content_features.h" |
Robbie McElrath | 5641d57 | 2022-05-20 17:15:29 | [diff] [blame] | 23 | #include "content/public/common/content_switches.h" |
Charles Harrison | 860f7ef | 2017-06-28 15:31:41 | [diff] [blame] | 24 | #include "content/public/common/url_constants.h" |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 25 | #include "content/public/test/test_browser_context.h" |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 26 | #include "content/public/test/test_navigation_throttle.h" |
Yao Xiao | 1099e4c | 2022-03-24 22:40:37 | [diff] [blame] | 27 | #include "content/test/fenced_frame_test_utils.h" |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 28 | #include "content/test/navigation_simulator_impl.h" |
Charles Harrison | 860f7ef | 2017-06-28 15:31:41 | [diff] [blame] | 29 | #include "content/test/test_content_browser_client.h" |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 30 | #include "content/test/test_render_frame_host.h" |
scottmg | 276753cf | 2016-10-27 18:25:22 | [diff] [blame] | 31 | #include "content/test/test_web_contents.h" |
David Sanders | c5c9292 | 2025-04-01 23:47:21 | [diff] [blame] | 32 | #include "net/base/features.h" |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 33 | #include "net/ssl/ssl_connection_status_flags.h" |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 34 | #include "services/network/public/cpp/content_security_policy/content_security_policy.h" |
Sandor Major | f42e6bd6 | 2025-02-28 00:12:04 | [diff] [blame] | 35 | #include "services/network/public/cpp/features.h" |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 36 | #include "testing/gmock/include/gmock/gmock.h" |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 37 | #include "third_party/blink/public/common/features.h" |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 38 | #include "third_party/blink/public/common/navigation/navigation_params.h" |
Nasko Oskov | 32f9589 | 2025-06-02 20:45:08 | [diff] [blame] | 39 | #include "third_party/blink/public/common/navigation/navigation_params_mojom_traits.h" |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 40 | #include "third_party/blink/public/common/origin_trials/scoped_test_origin_trial_policy.h" |
sbingler | 4e4bd9b | 2023-02-27 19:56:53 | [diff] [blame] | 41 | #include "third_party/blink/public/common/runtime_feature_state/runtime_feature_state_context.h" |
Richard Li | e689995 | 2018-11-30 08:42:00 | [diff] [blame] | 42 | #include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom.h" |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 43 | |
| 44 | namespace content { |
| 45 | |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 46 | class NavigationRequestTest : public RenderViewHostImplTestHarness { |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 47 | public: |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 48 | NavigationRequestTest() : callback_result_(NavigationThrottle::DEFER) {} |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 49 | |
| 50 | void SetUp() override { |
| 51 | RenderViewHostImplTestHarness::SetUp(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 52 | CreateNavigationHandle(); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 53 | contents()->GetPrimaryMainFrame()->InitializeRenderFrameIfNeeded(); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 54 | } |
| 55 | |
Dmitrii Kuragin | b7f90da | 2022-11-02 22:09:30 | [diff] [blame] | 56 | void TearDown() override { RenderViewHostImplTestHarness::TearDown(); } |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 57 | |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 58 | void CancelDeferredNavigation( |
| 59 | NavigationThrottle::ThrottleCheckResult result) { |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 60 | GetNavigationRequest()->CancelDeferredNavigationInternal(result); |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 61 | } |
| 62 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 63 | // Helper function to call WillStartRequest on |handle|. If this function |
| 64 | // returns DEFER, |callback_result_| will be set to the actual result of |
| 65 | // the throttle checks when they are finished. |
| 66 | void SimulateWillStartRequest() { |
| 67 | was_callback_called_ = false; |
| 68 | callback_result_ = NavigationThrottle::DEFER; |
| 69 | |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 70 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 71 | // the NavigationRequestTest. |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 72 | GetNavigationRequest()->set_complete_callback_for_testing( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 73 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 74 | base::Unretained(this))); |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 75 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 76 | GetNavigationRequest()->WillStartRequest(); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Helper function to call WillRedirectRequest on |handle|. If this function |
| 80 | // returns DEFER, |callback_result_| will be set to the actual result of the |
| 81 | // throttle checks when they are finished. |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 82 | // TODO(clamy): this should also simulate that WillStartRequest was called if |
| 83 | // it has not been called before. |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 84 | void SimulateWillRedirectRequest() { |
| 85 | was_callback_called_ = false; |
| 86 | callback_result_ = NavigationThrottle::DEFER; |
| 87 | |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 88 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 89 | // the NavigationRequestTest. |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 90 | GetNavigationRequest()->set_complete_callback_for_testing( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 91 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 92 | base::Unretained(this))); |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 93 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 94 | GetNavigationRequest()->WillRedirectRequest( |
Arthur Hemery | 3a991c09 | 2021-12-22 12:04:24 | [diff] [blame] | 95 | GURL(), nullptr /* post_redirect_process */); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 96 | } |
| 97 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 98 | // Helper function to call WillFailRequest on |handle|. If this function |
| 99 | // returns DEFER, |callback_result_| will be set to the actual result of the |
| 100 | // throttle checks when they are finished. |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 101 | void SimulateWillFailRequest( |
| 102 | net::Error net_error_code, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 103 | const std::optional<net::SSLInfo> ssl_info = std::nullopt) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 104 | was_callback_called_ = false; |
| 105 | callback_result_ = NavigationThrottle::DEFER; |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 106 | GetNavigationRequest()->set_net_error(net_error_code); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 107 | |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 108 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 109 | // the NavigationRequestTest. |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 110 | GetNavigationRequest()->set_complete_callback_for_testing( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 111 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 112 | base::Unretained(this))); |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 113 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 114 | GetNavigationRequest()->WillFailRequest(); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 115 | } |
| 116 | |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 117 | // Helper function to call WillCommitWithoutUrlLoader on |handle|. If this |
| 118 | // function returns DEFER, |callback_result_| will be set to the actual result |
| 119 | // of the throttle checks when they are finished. |
| 120 | void SimulateWillCommitWithoutUrlLoader() { |
| 121 | was_callback_called_ = false; |
| 122 | callback_result_ = NavigationThrottle::DEFER; |
| 123 | |
| 124 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
| 125 | // the NavigationRequestTest. |
| 126 | GetNavigationRequest()->set_complete_callback_for_testing( |
| 127 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 128 | base::Unretained(this))); |
| 129 | |
| 130 | GetNavigationRequest()->WillCommitWithoutUrlLoader(); |
| 131 | } |
| 132 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 133 | // Whether the callback was called. |
| 134 | bool was_callback_called() const { return was_callback_called_; } |
| 135 | |
| 136 | // Returns the callback_result. |
| 137 | NavigationThrottle::ThrottleCheckResult callback_result() const { |
| 138 | return callback_result_; |
| 139 | } |
| 140 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 141 | NavigationRequest::NavigationState state() { |
| 142 | return GetNavigationRequest()->state(); |
| 143 | } |
Mohamed Abdelhalim | ccd149af | 2019-10-31 14:48:53 | [diff] [blame] | 144 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 145 | bool call_counts_match(TestNavigationThrottle* throttle, |
| 146 | int start, |
| 147 | int redirect, |
| 148 | int failure, |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 149 | int process, |
| 150 | int withoutUrlLoader) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 151 | return start == throttle->GetCallCount( |
| 152 | TestNavigationThrottle::WILL_START_REQUEST) && |
| 153 | redirect == throttle->GetCallCount( |
| 154 | TestNavigationThrottle::WILL_REDIRECT_REQUEST) && |
| 155 | failure == throttle->GetCallCount( |
| 156 | TestNavigationThrottle::WILL_FAIL_REQUEST) && |
| 157 | process == throttle->GetCallCount( |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 158 | TestNavigationThrottle::WILL_PROCESS_RESPONSE) && |
| 159 | withoutUrlLoader == |
| 160 | throttle->GetCallCount( |
| 161 | TestNavigationThrottle::WILL_COMMIT_WITHOUT_URL_LOADER); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Creates, register and returns a TestNavigationThrottle that will |
| 165 | // synchronously return |result| on checks by default. |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 166 | TestNavigationThrottle* CreateTestNavigationThrottle( |
| 167 | NavigationThrottle::ThrottleCheckResult result) { |
Takashi Toyoshima | dffe50e | 2025-05-21 04:24:21 | [diff] [blame] | 168 | TestNavigationThrottle* test_throttle = new TestNavigationThrottle( |
Takashi Toyoshima | 96a07bb | 2025-06-06 06:45:58 | [diff] [blame] | 169 | *GetNavigationRequest()->GetNavigationThrottleRegistryForTesting()); |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 170 | test_throttle->SetResponseForAllMethods(TestNavigationThrottle::SYNCHRONOUS, |
| 171 | result); |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 172 | GetNavigationRequest()->RegisterThrottleForTesting( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 173 | std::unique_ptr<TestNavigationThrottle>(test_throttle)); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 174 | return test_throttle; |
| 175 | } |
| 176 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 177 | // Creates, register and returns a TestNavigationThrottle that will |
| 178 | // synchronously return |result| on check for the given |method|, and |
| 179 | // NavigationThrottle::PROCEED otherwise. |
| 180 | TestNavigationThrottle* CreateTestNavigationThrottle( |
| 181 | TestNavigationThrottle::ThrottleMethod method, |
| 182 | NavigationThrottle::ThrottleCheckResult result) { |
| 183 | TestNavigationThrottle* test_throttle = |
| 184 | CreateTestNavigationThrottle(NavigationThrottle::PROCEED); |
| 185 | test_throttle->SetResponse(method, TestNavigationThrottle::SYNCHRONOUS, |
| 186 | result); |
| 187 | return test_throttle; |
| 188 | } |
| 189 | |
Mohamed Abdelhalim | 9ef43fc | 2019-04-05 13:09:43 | [diff] [blame] | 190 | // TODO(zetamoo): Use NavigationSimulator instead of creating |
| 191 | // NavigationRequest and NavigationHandleImpl. |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 192 | void CreateNavigationHandle() { |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 193 | auto common_params = blink::CreateCommonNavigationParams(); |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 194 | common_params->initiator_origin = |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 195 | url::Origin::Create(GURL("https://initiator.example.com")); |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 196 | auto commit_params = blink::CreateCommitNavigationParams(); |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 197 | commit_params->frame_policy = |
| 198 | main_test_rfh()->frame_tree_node()->pending_frame_policy(); |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 199 | auto request = NavigationRequest::CreateBrowserInitiated( |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 200 | main_test_rfh()->frame_tree_node(), std::move(common_params), |
Alex Moshchuk | 9321e6a | 2022-12-07 21:58:31 | [diff] [blame] | 201 | std::move(commit_params), false /* was_opener_suppressed */, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 202 | std::string() /* extra_headers */, nullptr /* frame_entry */, |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 203 | nullptr /* entry */, false /* is_form_submission */, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 204 | nullptr /* navigation_ui_data */, std::nullopt /* impression */, |
Daniel Hosseinian | f0fbfb4 | 2021-09-08 02:20:47 | [diff] [blame] | 205 | false /* is_pdf */); |
Charlie Reis | 09952ee | 2022-12-08 16:35:07 | [diff] [blame] | 206 | main_test_rfh()->frame_tree_node()->TakeNavigationRequest( |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 207 | std::move(request)); |
| 208 | GetNavigationRequest()->StartNavigation(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 209 | } |
| 210 | |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 211 | FrameTreeNode* AddFrame(FrameTree& frame_tree, |
| 212 | RenderFrameHostImpl* parent, |
| 213 | int process_id, |
| 214 | int new_routing_id, |
| 215 | const blink::FramePolicy& frame_policy, |
| 216 | blink::FrameOwnerElementType owner_type) { |
| 217 | return frame_tree.AddFrame( |
| 218 | parent, process_id, new_routing_id, |
| 219 | TestRenderFrameHost::CreateStubFrameRemote(), |
| 220 | TestRenderFrameHost::CreateStubBrowserInterfaceBrokerReceiver(), |
| 221 | TestRenderFrameHost::CreateStubPolicyContainerBindParams(), |
Dominic Farolino | 12e06d7 | 2022-08-05 02:29:49 | [diff] [blame] | 222 | TestRenderFrameHost::CreateStubAssociatedInterfaceProviderReceiver(), |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 223 | blink::mojom::TreeScopeType::kDocument, std::string(), "uniqueName0", |
| 224 | false, blink::LocalFrameToken(), base::UnguessableToken::Create(), |
Daniel Cheng | 284c3894 | 2022-09-22 23:30:34 | [diff] [blame] | 225 | blink::DocumentToken(), frame_policy, |
| 226 | blink::mojom::FrameOwnerProperties(), false, owner_type, |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 227 | /*is_dummy_frame_for_inner_tree=*/false); |
| 228 | } |
| 229 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 230 | private: |
Mohamed Abdelhalim | f03d4a2 | 2019-10-01 13:34:31 | [diff] [blame] | 231 | // The callback provided to NavigationRequest::WillStartRequest, |
| 232 | // NavigationRequest::WillRedirectRequest, and |
| 233 | // NavigationRequest::WillFailRequest during the tests. |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 234 | bool UpdateThrottleCheckResult( |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 235 | NavigationThrottle::ThrottleCheckResult result) { |
| 236 | callback_result_ = result; |
| 237 | was_callback_called_ = true; |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 238 | return true; |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 239 | } |
| 240 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 241 | // This must be called after CreateNavigationHandle(). |
| 242 | NavigationRequest* GetNavigationRequest() { |
| 243 | return main_test_rfh()->frame_tree_node()->navigation_request(); |
| 244 | } |
| 245 | |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 246 | bool was_callback_called_ = false; |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 247 | NavigationThrottle::ThrottleCheckResult callback_result_; |
| 248 | }; |
| 249 | |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 250 | // Checks that the request_context_type is properly set. |
| 251 | // Note: can be extended to cover more internal members. |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 252 | TEST_F(NavigationRequestTest, SimpleDataChecksRedirectAndProcess) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 253 | const GURL kUrl1 = GURL("http://chromium.org"); |
| 254 | const GURL kUrl2 = GURL("http://google.com"); |
| 255 | auto navigation = |
| 256 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl1, main_rfh()); |
| 257 | navigation->Start(); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 258 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 259 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 260 | ->request_context_type()); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 261 | EXPECT_EQ(net::HttpConnectionInfo::kUNKNOWN, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 262 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 263 | |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 264 | navigation->set_http_connection_info(net::HttpConnectionInfo::kHTTP1_1); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 265 | navigation->Redirect(kUrl2); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 266 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 267 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 268 | ->request_context_type()); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 269 | EXPECT_EQ(net::HttpConnectionInfo::kHTTP1_1, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 270 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 271 | |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 272 | navigation->set_http_connection_info(net::HttpConnectionInfo::kQUIC_35); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 273 | navigation->ReadyToCommit(); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 274 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 275 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 276 | ->request_context_type()); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 277 | EXPECT_EQ(net::HttpConnectionInfo::kQUIC_35, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 278 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 279 | } |
| 280 | |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 281 | TEST_F(NavigationRequestTest, SimpleDataCheckNoRedirect) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 282 | const GURL kUrl = GURL("http://chromium.org"); |
| 283 | auto navigation = |
| 284 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 285 | navigation->Start(); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 286 | EXPECT_EQ(net::HttpConnectionInfo::kUNKNOWN, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 287 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 288 | |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 289 | navigation->set_http_connection_info(net::HttpConnectionInfo::kQUIC_35); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 290 | navigation->ReadyToCommit(); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 291 | EXPECT_EQ(net::HttpConnectionInfo::kQUIC_35, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 292 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 293 | } |
| 294 | |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 295 | TEST_F(NavigationRequestTest, SimpleDataChecksFailure) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 296 | const GURL kUrl = GURL("http://chromium.org"); |
| 297 | auto navigation = |
| 298 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 299 | navigation->Start(); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 300 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 301 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 302 | ->request_context_type()); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 303 | EXPECT_EQ(net::HttpConnectionInfo::kUNKNOWN, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 304 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 305 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 306 | navigation->Fail(net::ERR_CERT_DATE_INVALID); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 307 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 308 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 309 | ->request_context_type()); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 310 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, |
| 311 | navigation->GetNavigationHandle()->GetNetErrorCode()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 312 | } |
| 313 | |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 314 | // Checks that a navigation deferred during WillStartRequest can be properly |
| 315 | // cancelled. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 316 | TEST_F(NavigationRequestTest, CancelDeferredWillStart) { |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 317 | TestNavigationThrottle* test_throttle = |
| 318 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 319 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 320 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 321 | |
| 322 | // Simulate WillStartRequest. The request should be deferred. The callback |
| 323 | // should not have been called. |
| 324 | SimulateWillStartRequest(); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 325 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 326 | EXPECT_FALSE(was_callback_called()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 327 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 328 | |
| 329 | // Cancel the request. The callback should have been called. |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 330 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 331 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 332 | EXPECT_TRUE(was_callback_called()); |
| 333 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 334 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // Checks that a navigation deferred during WillRedirectRequest can be properly |
| 338 | // cancelled. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 339 | TEST_F(NavigationRequestTest, CancelDeferredWillRedirect) { |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 340 | TestNavigationThrottle* test_throttle = |
| 341 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 342 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 343 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 344 | |
| 345 | // Simulate WillRedirectRequest. The request should be deferred. The callback |
| 346 | // should not have been called. |
| 347 | SimulateWillRedirectRequest(); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 348 | EXPECT_EQ(NavigationRequest::WILL_REDIRECT_REQUEST, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 349 | EXPECT_FALSE(was_callback_called()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 350 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 1, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 351 | |
| 352 | // Cancel the request. The callback should have been called. |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 353 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 354 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 355 | EXPECT_TRUE(was_callback_called()); |
| 356 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 357 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 1, 0, 0, 0)); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Checks that a navigation deferred during WillFailRequest can be properly |
| 361 | // cancelled. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 362 | TEST_F(NavigationRequestTest, CancelDeferredWillFail) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 363 | TestNavigationThrottle* test_throttle = CreateTestNavigationThrottle( |
| 364 | TestNavigationThrottle::WILL_FAIL_REQUEST, NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 365 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 366 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0, 0)); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 367 | |
| 368 | // Simulate WillStartRequest. |
| 369 | SimulateWillStartRequest(); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 370 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0, 0)); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 371 | |
| 372 | // Simulate WillFailRequest. The request should be deferred. The callback |
| 373 | // should not have been called. |
| 374 | SimulateWillFailRequest(net::ERR_CERT_DATE_INVALID); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 375 | EXPECT_EQ(NavigationRequest::WILL_FAIL_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 376 | EXPECT_FALSE(was_callback_called()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 377 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0, 0)); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 378 | |
| 379 | // Cancel the request. The callback should have been called. |
| 380 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 381 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 382 | EXPECT_TRUE(was_callback_called()); |
| 383 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 384 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | // Checks that a navigation deferred can be canceled and not ignored. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 388 | TEST_F(NavigationRequestTest, CancelDeferredWillRedirectNoIgnore) { |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 389 | TestNavigationThrottle* test_throttle = |
| 390 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 391 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 392 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 393 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 394 | // Simulate WillStartRequest. The request should be deferred. The callback |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 395 | // should not have been called. |
| 396 | SimulateWillStartRequest(); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 397 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 398 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 399 | |
| 400 | // Cancel the request. The callback should have been called with CANCEL, and |
| 401 | // not CANCEL_AND_IGNORE. |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 402 | CancelDeferredNavigation(NavigationThrottle::CANCEL); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 403 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 404 | EXPECT_TRUE(was_callback_called()); |
| 405 | EXPECT_EQ(NavigationThrottle::CANCEL, callback_result()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 406 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 407 | } |
| 408 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 409 | // Checks that a navigation deferred by WillFailRequest can be canceled and not |
| 410 | // ignored. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 411 | TEST_F(NavigationRequestTest, CancelDeferredWillFailNoIgnore) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 412 | TestNavigationThrottle* test_throttle = CreateTestNavigationThrottle( |
| 413 | TestNavigationThrottle::WILL_FAIL_REQUEST, NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 414 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 415 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0, 0)); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 416 | |
| 417 | // Simulate WillStartRequest. |
| 418 | SimulateWillStartRequest(); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 419 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0, 0)); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 420 | |
| 421 | // Simulate WillFailRequest. The request should be deferred. The callback |
| 422 | // should not have been called. |
| 423 | SimulateWillFailRequest(net::ERR_CERT_DATE_INVALID); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 424 | EXPECT_EQ(NavigationRequest::WILL_FAIL_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 425 | EXPECT_FALSE(was_callback_called()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 426 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0, 0)); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 427 | |
| 428 | // Cancel the request. The callback should have been called with CANCEL, and |
| 429 | // not CANCEL_AND_IGNORE. |
| 430 | CancelDeferredNavigation(NavigationThrottle::CANCEL); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 431 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 432 | EXPECT_TRUE(was_callback_called()); |
| 433 | EXPECT_EQ(NavigationThrottle::CANCEL, callback_result()); |
Nate Chapin | 060cb95 | 2023-02-08 21:13:07 | [diff] [blame] | 434 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0, 0)); |
| 435 | } |
| 436 | |
| 437 | // Checks that a navigation deferred during WillCommitWithoutUrlLoader can be |
| 438 | // properly cancelled. |
| 439 | TEST_F(NavigationRequestTest, CancelDeferredWillCommitWithoutUrlLoader) { |
| 440 | TestNavigationThrottle* test_throttle = |
| 441 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
| 442 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
| 443 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0, 0)); |
| 444 | |
| 445 | // Simulate WillCommitWithoutUrlLoader. The request should be deferred. The |
| 446 | // callback should not have been called. |
| 447 | SimulateWillCommitWithoutUrlLoader(); |
| 448 | EXPECT_EQ(NavigationRequest::WILL_COMMIT_WITHOUT_URL_LOADER, state()); |
| 449 | EXPECT_FALSE(was_callback_called()); |
| 450 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0, 1)); |
| 451 | |
| 452 | // Cancel the request. The callback should have been called. |
| 453 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
| 454 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
| 455 | EXPECT_TRUE(was_callback_called()); |
| 456 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
| 457 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0, 1)); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 458 | } |
| 459 | |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 460 | // Checks that data from the SSLInfo passed into SimulateWillStartRequest() is |
John Abd-El-Malek | f36e05f | 2017-11-30 16:17:52 | [diff] [blame] | 461 | // stored on the handle. |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 462 | TEST_F(NavigationRequestTest, WillFailRequestSetsSSLInfo) { |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 463 | uint16_t cipher_suite = 0xc02f; // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 |
| 464 | int connection_status = 0; |
| 465 | net::SSLConnectionStatusSetCipherSuite(cipher_suite, &connection_status); |
| 466 | |
| 467 | // Set some test values. |
| 468 | net::SSLInfo ssl_info; |
| 469 | ssl_info.cert_status = net::CERT_STATUS_AUTHORITY_INVALID; |
| 470 | ssl_info.connection_status = connection_status; |
| 471 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 472 | const GURL kUrl = GURL("https://chromium.org"); |
| 473 | auto navigation = |
| 474 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
Emily Stark | fd6978ad1 | 2019-04-30 21:20:07 | [diff] [blame] | 475 | navigation->SetSSLInfo(ssl_info); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 476 | navigation->Fail(net::ERR_CERT_DATE_INVALID); |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 477 | |
| 478 | EXPECT_EQ(net::CERT_STATUS_AUTHORITY_INVALID, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 479 | navigation->GetNavigationHandle()->GetSSLInfo()->cert_status); |
| 480 | EXPECT_EQ(connection_status, |
| 481 | navigation->GetNavigationHandle()->GetSSLInfo()->connection_status); |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 482 | } |
| 483 | |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 484 | TEST_F(NavigationRequestTest, SharedStorageWritable) { |
| 485 | base::test::ScopedFeatureList feature_list; |
| 486 | feature_list.InitWithFeatures( |
Sandor Major | f42e6bd6 | 2025-02-28 00:12:04 | [diff] [blame] | 487 | /*enabled_features=*/{network::features::kSharedStorageAPI, |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 488 | blink::features::kFencedFrames}, |
| 489 | /*disabled_features=*/{}); |
| 490 | |
| 491 | // Create and start a simulated `NavigationRequest` for the main frame. |
| 492 | GURL main_url = GURL("https://main.com"); |
| 493 | auto main_navigation = |
| 494 | NavigationSimulatorImpl::CreateBrowserInitiated(main_url, contents()); |
| 495 | main_navigation->Start(); |
| 496 | main_navigation->ReadyToCommit(); |
| 497 | |
| 498 | // Verify that the main frame's `NavigationRequest` will not be |
| 499 | // SharedStorageWritable. |
| 500 | ASSERT_TRUE(main_navigation->GetNavigationHandle()); |
Camillia Smith Barnes | 3cad1ba | 2023-10-30 20:10:16 | [diff] [blame] | 501 | EXPECT_FALSE(main_navigation->GetNavigationHandle() |
| 502 | ->shared_storage_writable_eligible()); |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 503 | |
| 504 | // Commit the navigation. |
| 505 | main_navigation->Commit(); |
| 506 | |
| 507 | // Append a child frame and set its `shared_storage_writable` attribute to |
| 508 | // true. |
| 509 | TestRenderFrameHost* child_frame = static_cast<TestRenderFrameHost*>( |
| 510 | content::RenderFrameHostTester::For(main_rfh())->AppendChild("child")); |
| 511 | blink::mojom::IframeAttributesPtr child_attributes = |
| 512 | blink::mojom::IframeAttributes::New(); |
Camillia Smith Barnes | c267be6 | 2023-11-01 20:01:02 | [diff] [blame] | 513 | child_attributes->shared_storage_writable_opted_in = true; |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 514 | child_frame->frame_tree_node()->SetAttributes(std::move(child_attributes)); |
| 515 | |
| 516 | // Create and start a simulated `NavigationRequest` for the child frame. |
| 517 | GURL a_url = GURL("https://a.com"); |
| 518 | auto child_navigation = |
| 519 | NavigationSimulatorImpl::CreateRendererInitiated(a_url, child_frame); |
| 520 | child_navigation->Start(); |
| 521 | |
| 522 | // Verify that the `NavigationRequest` will be SharedStorageWritable. |
| 523 | ASSERT_TRUE(child_navigation->GetNavigationHandle()); |
Camillia Smith Barnes | 3cad1ba | 2023-10-30 20:10:16 | [diff] [blame] | 524 | EXPECT_TRUE(child_navigation->GetNavigationHandle() |
| 525 | ->shared_storage_writable_eligible()); |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 526 | |
| 527 | // Commit the navigation. |
| 528 | child_navigation->Commit(); |
| 529 | |
| 530 | // Append a fenced frame and give it permission to access Shared Storage. |
| 531 | TestRenderFrameHost* fenced_frame_root = static_cast<TestRenderFrameHost*>( |
| 532 | content::RenderFrameHostTester::For(main_rfh())->AppendFencedFrame()); |
| 533 | FrameTreeNode* fenced_frame_node = |
| 534 | static_cast<RenderFrameHostImpl*>(fenced_frame_root)->frame_tree_node(); |
Garrett Tanzer | 0698070 | 2023-12-12 19:48:20 | [diff] [blame] | 535 | FencedFrameConfig new_config = FencedFrameConfig(GURL("about:blank")); |
Garrett Tanzer | 3923f9d | 2023-12-15 16:54:34 | [diff] [blame] | 536 | new_config.AddEffectiveEnabledPermissionForTesting( |
Sandor «Alex» Major | e9545a7 | 2025-01-31 20:40:46 | [diff] [blame] | 537 | network::mojom::PermissionsPolicyFeature::kSharedStorage); |
Garrett Tanzer | 0698070 | 2023-12-12 19:48:20 | [diff] [blame] | 538 | FencedFrameProperties new_props = FencedFrameProperties(new_config); |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 539 | fenced_frame_node->set_fenced_frame_properties(new_props); |
Fergal Daly | a6cfd11 | 2024-05-02 18:43:28 | [diff] [blame] | 540 | fenced_frame_root->ResetPermissionsPolicy({}); |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 541 | |
| 542 | // Append a child frame to the fenced frame root and set its |
| 543 | // `shared_storage_writable` attribute to true. |
| 544 | TestRenderFrameHost* child_of_fenced_frame = |
| 545 | static_cast<TestRenderFrameHost*>( |
| 546 | fenced_frame_root->AppendChild("child_of_fenced")); |
| 547 | blink::mojom::IframeAttributesPtr child_of_fenced_frame_attributes = |
| 548 | blink::mojom::IframeAttributes::New(); |
Camillia Smith Barnes | c267be6 | 2023-11-01 20:01:02 | [diff] [blame] | 549 | child_of_fenced_frame_attributes->shared_storage_writable_opted_in = true; |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 550 | child_of_fenced_frame->frame_tree_node()->SetAttributes( |
| 551 | std::move(child_of_fenced_frame_attributes)); |
| 552 | |
| 553 | // Create and start a simulated `NavigationRequest` for the child frame. |
| 554 | GURL b_url = GURL("https://b.com"); |
| 555 | auto child_of_fenced_frame_navigation = |
| 556 | NavigationSimulatorImpl::CreateRendererInitiated(b_url, |
| 557 | child_of_fenced_frame); |
| 558 | child_of_fenced_frame_navigation->Start(); |
| 559 | |
| 560 | // Verify that the `NavigationRequest` will be SharedStorageWritable. |
| 561 | ASSERT_TRUE(child_of_fenced_frame_navigation->GetNavigationHandle()); |
| 562 | EXPECT_TRUE(child_of_fenced_frame_navigation->GetNavigationHandle() |
Camillia Smith Barnes | 3cad1ba | 2023-10-30 20:10:16 | [diff] [blame] | 563 | ->shared_storage_writable_eligible()); |
Camillia Smith Barnes | 6d2966c8 | 2023-08-23 21:16:18 | [diff] [blame] | 564 | |
| 565 | // Commit the navigation. |
| 566 | child_of_fenced_frame_navigation->Commit(); |
| 567 | } |
| 568 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 569 | namespace { |
| 570 | |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 571 | // Helper throttle which checks that it can access NavigationHandle's |
| 572 | // RenderFrameHost in WillFailRequest() and then defers the failure. |
| 573 | class GetRenderFrameHostOnFailureNavigationThrottle |
| 574 | : public NavigationThrottle { |
| 575 | public: |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 576 | explicit GetRenderFrameHostOnFailureNavigationThrottle( |
Takashi Toyoshima | 74e6ef30 | 2025-06-06 11:28:41 | [diff] [blame] | 577 | NavigationThrottleRegistry& registry) |
| 578 | : NavigationThrottle(registry) {} |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 579 | |
| 580 | GetRenderFrameHostOnFailureNavigationThrottle( |
| 581 | const GetRenderFrameHostOnFailureNavigationThrottle&) = delete; |
| 582 | GetRenderFrameHostOnFailureNavigationThrottle& operator=( |
| 583 | const GetRenderFrameHostOnFailureNavigationThrottle&) = delete; |
| 584 | |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 585 | ~GetRenderFrameHostOnFailureNavigationThrottle() override = default; |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 586 | |
| 587 | NavigationThrottle::ThrottleCheckResult WillFailRequest() override { |
| 588 | EXPECT_TRUE(navigation_handle()->GetRenderFrameHost()); |
| 589 | return NavigationThrottle::DEFER; |
| 590 | } |
| 591 | |
| 592 | const char* GetNameForLogging() override { |
| 593 | return "GetRenderFrameHostOnFailureNavigationThrottle"; |
| 594 | } |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 595 | }; |
| 596 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 597 | class ThrottleTestContentBrowserClient : public ContentBrowserClient { |
Takashi Toyoshima | 2a7f8cc0 | 2025-05-07 07:58:46 | [diff] [blame] | 598 | void CreateThrottlesForNavigation( |
Takashi Toyoshima | be55153 | 2025-05-02 18:13:52 | [diff] [blame] | 599 | NavigationThrottleRegistry& registry) override { |
Takashi Toyoshima | be55153 | 2025-05-02 18:13:52 | [diff] [blame] | 600 | registry.AddThrottle( |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 601 | std::make_unique<GetRenderFrameHostOnFailureNavigationThrottle>( |
Takashi Toyoshima | 74e6ef30 | 2025-06-06 11:28:41 | [diff] [blame] | 602 | registry)); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 603 | } |
| 604 | }; |
| 605 | |
| 606 | } // namespace |
| 607 | |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 608 | // Verify that the NavigationHandle::GetRenderFrameHost() can be retrieved by a |
| 609 | // throttle in WillFailRequest(), as well as after deferring the failure. This |
| 610 | // is allowed, since at that point the final RenderFrameHost will have already |
| 611 | // been chosen. See https://crbug.com/817881. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 612 | TEST_F(NavigationRequestTest, WillFailRequestCanAccessRenderFrameHost) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 613 | std::unique_ptr<ContentBrowserClient> client( |
| 614 | new ThrottleTestContentBrowserClient); |
| 615 | ContentBrowserClient* old_browser_client = |
| 616 | SetBrowserClientForTesting(client.get()); |
| 617 | |
| 618 | const GURL kUrl = GURL("http://chromium.org"); |
| 619 | auto navigation = |
| 620 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 621 | navigation->SetAutoAdvance(false); |
| 622 | navigation->Start(); |
| 623 | navigation->Fail(net::ERR_CERT_DATE_INVALID); |
Mohamed Abdelhalim | 3b23527 | 2019-11-05 15:06:07 | [diff] [blame] | 624 | EXPECT_EQ( |
| 625 | NavigationRequest::WILL_FAIL_REQUEST, |
| 626 | NavigationRequest::From(navigation->GetNavigationHandle())->state()); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 627 | EXPECT_TRUE(navigation->GetNavigationHandle()->GetRenderFrameHost()); |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 628 | NavigationRequest::From(navigation->GetNavigationHandle()) |
Takashi Toyoshima | cc4d947f | 2025-06-24 08:38:34 | [diff] [blame] | 629 | ->GetNavigationThrottleRegistryForTesting() |
danakj | f26536bf | 2020-09-10 00:46:13 | [diff] [blame] | 630 | ->GetNavigationThrottleRunnerForTesting() |
Takashi Toyoshima | cc4d947f | 2025-06-24 08:38:34 | [diff] [blame] | 631 | .CallResumeForTesting(); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 632 | EXPECT_TRUE(navigation->GetNavigationHandle()->GetRenderFrameHost()); |
| 633 | |
| 634 | SetBrowserClientForTesting(old_browser_client); |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 635 | } |
| 636 | |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 637 | TEST_F(NavigationRequestTest, PolicyContainerInheritance) { |
| 638 | struct TestCase { |
| 639 | const char* url; |
| 640 | bool expect_inherit; |
| 641 | } cases[]{{"about:blank", true}, |
| 642 | {"data:text/plain,hello", true}, |
| 643 | {"file://local", false}, |
| 644 | {"http://chromium.org", false}}; |
| 645 | |
| 646 | const GURL kUrl1 = GURL("http://chromium.org"); |
| 647 | auto navigation = |
| 648 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl1, main_rfh()); |
| 649 | navigation->Commit(); |
| 650 | |
| 651 | for (auto test : cases) { |
| 652 | // We navigate child frames because the BlockedSchemeNavigationThrottle |
| 653 | // restricts navigations in the main frame. |
| 654 | auto* child_frame = static_cast<TestRenderFrameHost*>( |
| 655 | content::RenderFrameHostTester::For(main_rfh())->AppendChild("child")); |
| 656 | |
| 657 | // We set the referrer policy of the frame to "always". We then create a new |
| 658 | // navigation, set as initiator the frame itself, start the navigation, and |
| 659 | // change the referrer policy of the frame to "never". After we commit the |
| 660 | // navigation: |
| 661 | // - If navigating to a local scheme, the target frame should have inherited |
| 662 | // the referrer policy of the initiator ("always"). |
| 663 | // - If navigating to a non-local scheme, the target frame should have a new |
| 664 | // policy container (hence referrer policy set to "default"). |
| 665 | const GURL kUrl = GURL(test.url); |
Peter Kasting | eb8c3ce | 2021-08-20 04:39:35 | [diff] [blame] | 666 | navigation = |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 667 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, child_frame); |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 668 | static_cast<blink::mojom::PolicyContainerHost*>( |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 669 | child_frame->policy_container_host()) |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 670 | ->SetReferrerPolicy(network::mojom::ReferrerPolicy::kAlways); |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 671 | navigation->SetInitiatorFrame(child_frame); |
| 672 | navigation->Start(); |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 673 | static_cast<blink::mojom::PolicyContainerHost*>( |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 674 | child_frame->policy_container_host()) |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 675 | ->SetReferrerPolicy(network::mojom::ReferrerPolicy::kNever); |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 676 | navigation->Commit(); |
| 677 | EXPECT_EQ( |
| 678 | test.expect_inherit ? network::mojom::ReferrerPolicy::kAlways |
| 679 | : network::mojom::ReferrerPolicy::kDefault, |
| 680 | static_cast<RenderFrameHostImpl*>(navigation->GetFinalRenderFrameHost()) |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 681 | ->policy_container_host() |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 682 | ->referrer_policy()); |
| 683 | } |
| 684 | } |
| 685 | |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 686 | TEST_F(NavigationRequestTest, DnsAliasesCanBeAccessed) { |
| 687 | // Create simulated NavigationRequest for the URL, which has aliases. |
| 688 | const GURL kUrl = GURL("http://chromium.org"); |
| 689 | auto navigation = |
| 690 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 691 | std::vector<std::string> dns_aliases({"alias1", "alias2"}); |
| 692 | navigation->SetResponseDnsAliases(std::move(dns_aliases)); |
| 693 | |
| 694 | // Start the navigation. |
| 695 | navigation->Start(); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 696 | EXPECT_EQ(net::HttpConnectionInfo::kUNKNOWN, |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 697 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
| 698 | |
| 699 | // Commit the navigation. |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 700 | navigation->set_http_connection_info(net::HttpConnectionInfo::kQUIC_35); |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 701 | navigation->ReadyToCommit(); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 702 | EXPECT_EQ(net::HttpConnectionInfo::kQUIC_35, |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 703 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
| 704 | |
| 705 | // Verify that the aliases are accessible from the NavigationRequest. |
| 706 | EXPECT_THAT(navigation->GetNavigationHandle()->GetDnsAliases(), |
| 707 | testing::ElementsAre("alias1", "alias2")); |
| 708 | } |
| 709 | |
| 710 | TEST_F(NavigationRequestTest, NoDnsAliases) { |
| 711 | // Create simulated NavigationRequest for the URL, which does not |
| 712 | // have aliases. (Note the empty alias list.) |
| 713 | const GURL kUrl = GURL("http://chromium.org"); |
| 714 | auto navigation = |
| 715 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 716 | std::vector<std::string> dns_aliases; |
| 717 | navigation->SetResponseDnsAliases(std::move(dns_aliases)); |
| 718 | |
| 719 | // Start the navigation. |
| 720 | navigation->Start(); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 721 | EXPECT_EQ(net::HttpConnectionInfo::kUNKNOWN, |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 722 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
| 723 | |
| 724 | // Commit the navigation. |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 725 | navigation->set_http_connection_info(net::HttpConnectionInfo::kQUIC_35); |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 726 | navigation->ReadyToCommit(); |
Tsuyoshi Horo | 3023b5d | 2023-11-28 21:40:31 | [diff] [blame] | 727 | EXPECT_EQ(net::HttpConnectionInfo::kQUIC_35, |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 728 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
| 729 | |
| 730 | // Verify that there are no aliases in the NavigationRequest. |
| 731 | EXPECT_TRUE(navigation->GetNavigationHandle()->GetDnsAliases().empty()); |
| 732 | } |
| 733 | |
Antonio Sartori | 3e8de6d | 2021-07-26 10:28:41 | [diff] [blame] | 734 | TEST_F(NavigationRequestTest, StorageKeyToCommit) { |
| 735 | TestRenderFrameHost* child_document = static_cast<TestRenderFrameHost*>( |
| 736 | content::RenderFrameHostTester::For(main_rfh())->AppendChild("")); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 737 | auto attributes = child_document->frame_tree_node()->attributes_->Clone(); |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 738 | attributes->credentialless = true; |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 739 | child_document->frame_tree_node()->SetAttributes(std::move(attributes)); |
Antonio Sartori | 3e8de6d | 2021-07-26 10:28:41 | [diff] [blame] | 740 | |
| 741 | const GURL kUrl = GURL("http://chromium.org"); |
| 742 | auto navigation = |
| 743 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, child_document); |
| 744 | navigation->ReadyToCommit(); |
| 745 | NavigationRequest* request = |
| 746 | NavigationRequest::From(navigation->GetNavigationHandle()); |
| 747 | EXPECT_TRUE(request->commit_params().storage_key.nonce().has_value()); |
Adithya Srinivasan | 4e0bb964 | 2023-12-19 15:51:58 | [diff] [blame] | 748 | EXPECT_EQ(child_document->GetPage().credentialless_iframes_nonce(), |
Antonio Sartori | 3e8de6d | 2021-07-26 10:28:41 | [diff] [blame] | 749 | request->commit_params().storage_key.nonce().value()); |
| 750 | |
| 751 | navigation->Commit(); |
| 752 | child_document = |
| 753 | static_cast<TestRenderFrameHost*>(navigation->GetFinalRenderFrameHost()); |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 754 | EXPECT_TRUE(child_document->IsCredentialless()); |
Ari Chivukula | d9e9c38 | 2023-02-15 23:30:09 | [diff] [blame] | 755 | EXPECT_EQ(blink::StorageKey::CreateWithNonce( |
Ali Hijazi | 78de14c | 2022-07-21 11:32:19 | [diff] [blame] | 756 | url::Origin::Create(kUrl), |
Adithya Srinivasan | 4e0bb964 | 2023-12-19 15:51:58 | [diff] [blame] | 757 | child_document->GetPage().credentialless_iframes_nonce()), |
Mariam Ali | 8338d9fa | 2023-07-24 16:57:31 | [diff] [blame] | 758 | child_document->GetStorageKey()); |
Antonio Sartori | 3e8de6d | 2021-07-26 10:28:41 | [diff] [blame] | 759 | } |
| 760 | |
sbingler | 4e4bd9b | 2023-02-27 19:56:53 | [diff] [blame] | 761 | // Test that the StorageKey's value is correctly affected by the |
| 762 | // RuntimeFeatureStateContext. |
| 763 | TEST_F(NavigationRequestTest, RuntimeFeatureStateStorageKey) { |
| 764 | base::test::ScopedFeatureList scoped_feature_list; |
| 765 | // Because the StorageKey's (and Storage Partitioning's) usage of |
| 766 | // RuntimeFeatureState is only meant to disable partitioning (i.e.: |
| 767 | // first-party only), we need the make sure the net::features is always |
| 768 | // enabled. |
| 769 | scoped_feature_list.InitAndEnableFeature( |
| 770 | net::features::kThirdPartyStoragePartitioning); |
| 771 | |
| 772 | // This lambda performs the navigation and compares the commit_params' |
| 773 | // StorageKey against the passed in one. If `disable_sp` is true then it will |
Ari Chivukula | 2b14736 | 2025-05-27 16:32:53 | [diff] [blame] | 774 | // also enable the user bypass feature in the RFSC. It returns |
sbingler | 4e4bd9b | 2023-02-27 19:56:53 | [diff] [blame] | 775 | // the new TestRenderFrameHost* to the navigated frame. |
| 776 | auto NavigateAndCompareKeys = |
| 777 | [](NavigationSimulator* navigation, const blink::StorageKey& key, |
| 778 | bool disable_sp = false) -> TestRenderFrameHost* { |
| 779 | navigation->Start(); |
| 780 | |
| 781 | NavigationRequest* request = |
| 782 | NavigationRequest::From(navigation->GetNavigationHandle()); |
| 783 | |
| 784 | if (disable_sp) { |
| 785 | request->GetMutableRuntimeFeatureStateContext() |
Ari Chivukula | 2b14736 | 2025-05-27 16:32:53 | [diff] [blame] | 786 | .SetThirdPartyStoragePartitioningUserBypassEnabled(true); |
sbingler | 4e4bd9b | 2023-02-27 19:56:53 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | navigation->ReadyToCommit(); |
| 790 | |
| 791 | EXPECT_EQ(key, request->commit_params().storage_key); |
| 792 | |
| 793 | navigation->Commit(); |
| 794 | return static_cast<TestRenderFrameHost*>( |
| 795 | navigation->GetFinalRenderFrameHost()); |
| 796 | }; |
| 797 | |
| 798 | // Throughout the test we'll be creating a frame tree with a main frame, a |
| 799 | // child frame, and a grandchild frame. |
| 800 | GURL main_url("https://main.com"); |
| 801 | GURL b_url("https://b.com"); |
| 802 | GURL c_url("https://c.com"); |
| 803 | |
| 804 | url::Origin main_origin = url::Origin::Create(main_url); |
| 805 | url::Origin b_origin = url::Origin::Create(b_url); |
| 806 | url::Origin c_origin = url::Origin::Create(c_url); |
| 807 | |
| 808 | // Begin by testing with Storage Partitioning enabled. |
| 809 | |
| 810 | auto main_navigation = |
| 811 | NavigationSimulatorImpl::CreateBrowserInitiated(main_url, contents()); |
| 812 | |
| 813 | // By definition the main frame's StorageKey will always be first party |
| 814 | blink::StorageKey main_frame_key = |
| 815 | blink::StorageKey::CreateFirstParty(main_origin); |
| 816 | |
| 817 | NavigateAndCompareKeys(main_navigation.get(), main_frame_key); |
| 818 | |
| 819 | TestRenderFrameHost* child_frame = static_cast<TestRenderFrameHost*>( |
| 820 | content::RenderFrameHostTester::For(main_rfh())->AppendChild("child")); |
| 821 | |
| 822 | auto child_navigation = |
| 823 | NavigationSimulatorImpl::CreateRendererInitiated(b_url, child_frame); |
| 824 | |
| 825 | // The child and grandchild should both be third-party keys. |
| 826 | blink::StorageKey child_frame_key = |
| 827 | blink::StorageKey::Create(b_origin, net::SchemefulSite(main_origin), |
| 828 | blink::mojom::AncestorChainBit::kCrossSite); |
| 829 | |
| 830 | child_frame = NavigateAndCompareKeys(child_navigation.get(), child_frame_key); |
| 831 | |
| 832 | TestRenderFrameHost* grandchild_frame = |
| 833 | child_frame->AppendChild("grandchild"); |
| 834 | |
| 835 | auto grandchild_navigation = |
| 836 | NavigationSimulatorImpl::CreateRendererInitiated(c_url, grandchild_frame); |
| 837 | |
| 838 | blink::StorageKey grandchild_frame_key = |
| 839 | blink::StorageKey::Create(c_origin, net::SchemefulSite(main_origin), |
| 840 | blink::mojom::AncestorChainBit::kCrossSite); |
| 841 | grandchild_frame = |
| 842 | NavigateAndCompareKeys(grandchild_navigation.get(), grandchild_frame_key); |
| 843 | |
| 844 | // Only the RuntimeFeatureStateContext in the main frame's matters. So |
| 845 | // disabling Storage Partitioning in the child_frame shouldn't affect the |
| 846 | // child's or the grandchild's StorageKey. |
| 847 | child_navigation = |
| 848 | NavigationSimulatorImpl::CreateRendererInitiated(b_url, child_frame); |
| 849 | |
| 850 | child_frame = NavigateAndCompareKeys(child_navigation.get(), child_frame_key, |
| 851 | /*disable_sp=*/true); |
| 852 | |
| 853 | grandchild_frame = child_frame->AppendChild("grandchild"); |
| 854 | |
| 855 | grandchild_navigation = |
| 856 | NavigationSimulatorImpl::CreateRendererInitiated(c_url, grandchild_frame); |
| 857 | |
| 858 | grandchild_frame = |
| 859 | NavigateAndCompareKeys(grandchild_navigation.get(), grandchild_frame_key); |
| 860 | |
| 861 | // Disabling Storage Partitioning on the main frame should cause the child's |
| 862 | // and grandchild's StorageKey to be first-party. |
| 863 | main_navigation = |
| 864 | NavigationSimulatorImpl::CreateBrowserInitiated(main_url, contents()); |
| 865 | |
| 866 | NavigateAndCompareKeys(main_navigation.get(), main_frame_key, |
| 867 | /*disable_sp=*/true); |
| 868 | |
| 869 | child_frame = static_cast<TestRenderFrameHost*>( |
| 870 | content::RenderFrameHostTester::For(main_rfh())->AppendChild("child")); |
| 871 | |
| 872 | child_navigation = |
| 873 | NavigationSimulatorImpl::CreateRendererInitiated(b_url, child_frame); |
| 874 | |
| 875 | // The child and grandchild should both be first-party keys. |
| 876 | blink::StorageKey child_frame_key_1p = |
| 877 | blink::StorageKey::CreateFirstParty(b_origin); |
| 878 | |
| 879 | child_frame = |
| 880 | NavigateAndCompareKeys(child_navigation.get(), child_frame_key_1p); |
| 881 | |
| 882 | grandchild_frame = child_frame->AppendChild("grandchild"); |
| 883 | |
| 884 | blink::StorageKey grandchild_frame_key_1p = |
| 885 | blink::StorageKey::CreateFirstParty(c_origin); |
| 886 | |
| 887 | grandchild_navigation = |
| 888 | NavigationSimulatorImpl::CreateRendererInitiated(c_url, grandchild_frame); |
| 889 | |
| 890 | grandchild_frame = NavigateAndCompareKeys(grandchild_navigation.get(), |
| 891 | grandchild_frame_key_1p); |
| 892 | } |
| 893 | |
Antonio Sartori | bf27cc44 | 2021-08-25 13:08:23 | [diff] [blame] | 894 | TEST_F(NavigationRequestTest, |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 895 | NavigationToCredentiallessDocumentNetworkIsolationInfo) { |
Antonio Sartori | bf27cc44 | 2021-08-25 13:08:23 | [diff] [blame] | 896 | auto* child_frame = static_cast<TestRenderFrameHost*>( |
| 897 | content::RenderFrameHostTester::For(main_test_rfh()) |
| 898 | ->AppendChild("child")); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 899 | auto attributes = child_frame->frame_tree_node()->attributes_->Clone(); |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 900 | attributes->credentialless = true; |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 901 | child_frame->frame_tree_node()->SetAttributes(std::move(attributes)); |
Antonio Sartori | bf27cc44 | 2021-08-25 13:08:23 | [diff] [blame] | 902 | |
| 903 | std::unique_ptr<NavigationSimulator> navigation = |
| 904 | NavigationSimulator::CreateRendererInitiated( |
| 905 | GURL("https://example.com/navigation.html"), child_frame); |
| 906 | navigation->ReadyToCommit(); |
| 907 | |
Adithya Srinivasan | 4e0bb964 | 2023-12-19 15:51:58 | [diff] [blame] | 908 | EXPECT_EQ(main_test_rfh()->GetPage().credentialless_iframes_nonce(), |
Antonio Sartori | bf27cc44 | 2021-08-25 13:08:23 | [diff] [blame] | 909 | static_cast<NavigationRequest*>(navigation->GetNavigationHandle()) |
| 910 | ->isolation_info_for_subresources() |
| 911 | .network_isolation_key() |
| 912 | .GetNonce()); |
Adithya Srinivasan | 4e0bb964 | 2023-12-19 15:51:58 | [diff] [blame] | 913 | EXPECT_EQ(main_test_rfh()->GetPage().credentialless_iframes_nonce(), |
Antonio Sartori | bf27cc44 | 2021-08-25 13:08:23 | [diff] [blame] | 914 | static_cast<NavigationRequest*>(navigation->GetNavigationHandle()) |
| 915 | ->GetIsolationInfo() |
| 916 | .network_isolation_key() |
| 917 | .GetNonce()); |
| 918 | } |
| 919 | |
Yifan Luo | b5c352b | 2023-10-30 11:22:10 | [diff] [blame] | 920 | TEST_F(NavigationRequestTest, UpdatePrivateNetworkRequestPolicy) { |
| 921 | std::unique_ptr<NavigationSimulator> navigation = |
| 922 | NavigationSimulator::CreateRendererInitiated(GURL("https://example.com/"), |
| 923 | main_test_rfh()); |
| 924 | navigation->SetSocketAddress(net::IPEndPoint()); |
| 925 | |
| 926 | navigation->ReadyToCommit(); |
| 927 | NavigationRequest* request = |
| 928 | NavigationRequest::From(navigation->GetNavigationHandle()); |
| 929 | EXPECT_FALSE(request->GetSocketAddress().address().IsValid()); |
| 930 | navigation->Commit(); |
| 931 | } |
| 932 | |
Nasko Oskov | 32f9589 | 2025-06-02 20:45:08 | [diff] [blame] | 933 | // Test to ensure that the SanitizeRedirectsForCommit method correctly removes |
| 934 | // the query parameters parts of the URL that can contain sensitive information. |
| 935 | TEST_F(NavigationRequestTest, SanitizeRedirectsForCommit) { |
| 936 | const GURL start_url("https://a.com?param=1"); |
| 937 | const GURL url_2("https://b.com?param=2#foo"); |
| 938 | const GURL url_3("https://c.com?param=3"); |
| 939 | const GURL final_url("https://d.com?param=4"); |
| 940 | std::unique_ptr<NavigationSimulator> navigation = |
| 941 | NavigationSimulator::CreateRendererInitiated(start_url, main_test_rfh()); |
| 942 | navigation->Start(); |
| 943 | navigation->Redirect(url_2); |
| 944 | navigation->Redirect(url_3); |
| 945 | navigation->Redirect(final_url); |
| 946 | |
| 947 | NavigationRequest* request = |
| 948 | NavigationRequest::From(navigation->GetNavigationHandle()); |
| 949 | auto commit_params = request->commit_params().Clone(); |
| 950 | request->SanitizeRedirectsForCommit(commit_params); |
| 951 | |
| 952 | // redirect_infos contains entries for B, C, and D, but not the starting URL. |
| 953 | // Ensure that the full URL for D is preserved. |
| 954 | EXPECT_EQ(3, commit_params->redirect_infos.size()); |
| 955 | EXPECT_EQ(GURL("https://b.com"), commit_params->redirect_infos[0].new_url); |
| 956 | EXPECT_EQ(GURL("https://c.com"), commit_params->redirect_infos[1].new_url); |
| 957 | EXPECT_EQ(final_url, commit_params->redirect_infos[2].new_url); |
| 958 | |
| 959 | // In contrast, redirects contains A, B, and C (i.e., the starting URL but not |
| 960 | // the final URL). |
| 961 | EXPECT_EQ(3, commit_params->redirects.size()); |
| 962 | EXPECT_EQ(GURL("https://a.com"), commit_params->redirects[0]); |
| 963 | EXPECT_EQ(GURL("https://b.com"), commit_params->redirects[1]); |
| 964 | EXPECT_EQ(GURL("https://c.com"), commit_params->redirects[2]); |
| 965 | } |
| 966 | |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 967 | // Test that the required CSP of every frame is computed/inherited correctly and |
| 968 | // that the Sec-Required-CSP header is set. |
| 969 | class CSPEmbeddedEnforcementUnitTest : public NavigationRequestTest { |
| 970 | protected: |
| 971 | TestRenderFrameHost* main_rfh() { |
| 972 | return static_cast<TestRenderFrameHost*>(NavigationRequestTest::main_rfh()); |
| 973 | } |
| 974 | |
| 975 | // Simulate the |csp| attribute being set in |rfh|'s frame. Then navigate it. |
| 976 | // Returns the request's Sec-Required-CSP header. |
| 977 | std::string NavigateWithRequiredCSP(TestRenderFrameHost** rfh, |
| 978 | std::string required_csp) { |
| 979 | TestRenderFrameHost* document = *rfh; |
| 980 | |
| 981 | if (!required_csp.empty()) { |
| 982 | auto headers = |
| 983 | base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK"); |
| 984 | headers->SetHeader("Content-Security-Policy", required_csp); |
| 985 | std::vector<network::mojom::ContentSecurityPolicyPtr> policies; |
| 986 | network::AddContentSecurityPolicyFromHeaders( |
| 987 | *headers, GURL("https://example.com/"), &policies); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 988 | auto attributes = document->frame_tree_node()->attributes_->Clone(); |
| 989 | // Set csp value. |
| 990 | attributes->parsed_csp_attribute = std::move(policies[0]); |
| 991 | document->frame_tree_node()->SetAttributes(std::move(attributes)); |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | // Chrome blocks a document navigating to a URL if more than one of its |
| 995 | // ancestors have the same URL. Use a different URL every time, to |
| 996 | // avoid blocking navigation of the grandchild frame. |
| 997 | static int nonce = 0; |
| 998 | GURL url("https://www.example.com" + base::NumberToString(nonce++)); |
| 999 | |
| 1000 | auto navigation = |
| 1001 | content::NavigationSimulator::CreateRendererInitiated(url, *rfh); |
| 1002 | navigation->Start(); |
| 1003 | NavigationRequest* request = |
| 1004 | NavigationRequest::From(navigation->GetNavigationHandle()); |
Chris Fredrickson | fb77b04d | 2024-08-02 18:54:58 | [diff] [blame] | 1005 | std::string sec_required_csp = request->GetRequestHeaders() |
| 1006 | .GetHeader("sec-required-csp") |
| 1007 | .value_or(std::string()); |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 1008 | |
| 1009 | // Complete the navigation so that the required csp is stored in the |
| 1010 | // RenderFrameHost, so that when we will add children to this document they |
| 1011 | // will be able to get the parent's required csp (and hence also test that |
| 1012 | // the whole logic works). |
| 1013 | auto response_headers = |
| 1014 | base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK"); |
| 1015 | response_headers->SetHeader("Allow-CSP-From", "*"); |
| 1016 | navigation->SetResponseHeaders(response_headers); |
| 1017 | navigation->Commit(); |
| 1018 | |
| 1019 | *rfh = static_cast<TestRenderFrameHost*>( |
| 1020 | navigation->GetFinalRenderFrameHost()); |
| 1021 | |
| 1022 | return sec_required_csp; |
| 1023 | } |
| 1024 | |
| 1025 | TestRenderFrameHost* AddChild(TestRenderFrameHost* parent) { |
| 1026 | return static_cast<TestRenderFrameHost*>( |
| 1027 | content::RenderFrameHostTester::For(parent)->AppendChild("")); |
| 1028 | } |
| 1029 | }; |
| 1030 | |
| 1031 | TEST_F(CSPEmbeddedEnforcementUnitTest, TopLevel) { |
| 1032 | TestRenderFrameHost* top_document = main_rfh(); |
| 1033 | std::string sec_required_csp = NavigateWithRequiredCSP(&top_document, ""); |
| 1034 | EXPECT_EQ("", sec_required_csp); |
| 1035 | EXPECT_FALSE(top_document->required_csp()); |
| 1036 | } |
| 1037 | |
| 1038 | TEST_F(CSPEmbeddedEnforcementUnitTest, ChildNoCSP) { |
| 1039 | TestRenderFrameHost* top_document = main_rfh(); |
| 1040 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1041 | std::string sec_required_csp = NavigateWithRequiredCSP(&child_document, ""); |
| 1042 | EXPECT_EQ("", sec_required_csp); |
| 1043 | EXPECT_FALSE(child_document->required_csp()); |
| 1044 | } |
| 1045 | |
| 1046 | TEST_F(CSPEmbeddedEnforcementUnitTest, ChildWithCSP) { |
| 1047 | TestRenderFrameHost* top_document = main_rfh(); |
| 1048 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1049 | std::string sec_required_csp = |
| 1050 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1051 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1052 | EXPECT_TRUE(child_document->required_csp()); |
| 1053 | EXPECT_EQ("script-src 'none'", |
| 1054 | child_document->required_csp()->header->header_value); |
| 1055 | } |
| 1056 | |
| 1057 | TEST_F(CSPEmbeddedEnforcementUnitTest, ChildSiblingNoCSP) { |
| 1058 | TestRenderFrameHost* top_document = main_rfh(); |
| 1059 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1060 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1061 | TestRenderFrameHost* sibling_document = AddChild(top_document); |
| 1062 | std::string sec_required_csp = NavigateWithRequiredCSP(&sibling_document, ""); |
| 1063 | EXPECT_FALSE(sibling_document->required_csp()); |
| 1064 | } |
| 1065 | |
| 1066 | TEST_F(CSPEmbeddedEnforcementUnitTest, ChildSiblingCSP) { |
| 1067 | TestRenderFrameHost* top_document = main_rfh(); |
| 1068 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1069 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1070 | TestRenderFrameHost* sibling_document = AddChild(top_document); |
| 1071 | std::string sec_required_csp = |
| 1072 | NavigateWithRequiredCSP(&sibling_document, "script-src 'none'"); |
| 1073 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1074 | EXPECT_TRUE(sibling_document->required_csp()); |
| 1075 | EXPECT_EQ("script-src 'none'", |
| 1076 | sibling_document->required_csp()->header->header_value); |
| 1077 | } |
| 1078 | |
| 1079 | TEST_F(CSPEmbeddedEnforcementUnitTest, GrandChildNoCSP) { |
| 1080 | TestRenderFrameHost* top_document = main_rfh(); |
| 1081 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1082 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1083 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 1084 | std::string sec_required_csp = |
| 1085 | NavigateWithRequiredCSP(&grand_child_document, ""); |
| 1086 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1087 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 1088 | EXPECT_EQ("script-src 'none'", |
| 1089 | grand_child_document->required_csp()->header->header_value); |
| 1090 | } |
| 1091 | |
| 1092 | TEST_F(CSPEmbeddedEnforcementUnitTest, GrandChildSameCSP) { |
| 1093 | TestRenderFrameHost* top_document = main_rfh(); |
| 1094 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1095 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1096 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 1097 | std::string sec_required_csp = |
| 1098 | NavigateWithRequiredCSP(&grand_child_document, "script-src 'none'"); |
| 1099 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1100 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 1101 | EXPECT_EQ("script-src 'none'", |
| 1102 | grand_child_document->required_csp()->header->header_value); |
| 1103 | } |
| 1104 | |
| 1105 | TEST_F(CSPEmbeddedEnforcementUnitTest, GrandChildDifferentCSP) { |
| 1106 | TestRenderFrameHost* top_document = main_rfh(); |
| 1107 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1108 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1109 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 1110 | std::string sec_required_csp = |
| 1111 | NavigateWithRequiredCSP(&grand_child_document, "img-src 'none'"); |
| 1112 | |
| 1113 | // This seems weird, but it is the intended behaviour according to the spec. |
| 1114 | // The problem is that "script-src 'none'" does not subsume "img-src 'none'", |
| 1115 | // so "img-src 'none'" on the grandchild is an invalid csp attribute, and we |
| 1116 | // just discard it in favour of the parent's csp attribute. |
| 1117 | // |
| 1118 | // This should probably be fixed in the specification: |
| 1119 | // https://github.com/w3c/webappsec-cspee/pull/11 |
| 1120 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1121 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 1122 | EXPECT_EQ("script-src 'none'", |
| 1123 | grand_child_document->required_csp()->header->header_value); |
| 1124 | } |
| 1125 | |
| 1126 | TEST_F(CSPEmbeddedEnforcementUnitTest, InvalidCSP) { |
| 1127 | TestRenderFrameHost* top_document = main_rfh(); |
| 1128 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1129 | std::string sec_required_csp = |
| 1130 | NavigateWithRequiredCSP(&child_document, "report-to group"); |
| 1131 | EXPECT_EQ("", sec_required_csp); |
| 1132 | EXPECT_FALSE(child_document->required_csp()); |
| 1133 | } |
| 1134 | |
| 1135 | TEST_F(CSPEmbeddedEnforcementUnitTest, InvalidCspAndInheritFromParent) { |
| 1136 | TestRenderFrameHost* top_document = main_rfh(); |
| 1137 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1138 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1139 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 1140 | std::string sec_required_csp = |
Antonio Sartori | 4231e93 | 2021-02-04 12:01:14 | [diff] [blame] | 1141 | NavigateWithRequiredCSP(&grand_child_document, "report-to group"); |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 1142 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1143 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 1144 | EXPECT_EQ("script-src 'none'", |
| 1145 | grand_child_document->required_csp()->header->header_value); |
| 1146 | } |
| 1147 | |
| 1148 | TEST_F(CSPEmbeddedEnforcementUnitTest, |
| 1149 | SemiInvalidCspAndInheritSameCspFromParent) { |
| 1150 | TestRenderFrameHost* top_document = main_rfh(); |
| 1151 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1152 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1153 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 1154 | std::string sec_required_csp = NavigateWithRequiredCSP( |
Antonio Sartori | 4231e93 | 2021-02-04 12:01:14 | [diff] [blame] | 1155 | &grand_child_document, "script-src 'none'; report-to group"); |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 1156 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1157 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 1158 | EXPECT_EQ("script-src 'none'", |
| 1159 | grand_child_document->required_csp()->header->header_value); |
| 1160 | } |
| 1161 | |
| 1162 | TEST_F(CSPEmbeddedEnforcementUnitTest, |
| 1163 | SemiInvalidCspAndInheritDifferentCspFromParent) { |
| 1164 | TestRenderFrameHost* top_document = main_rfh(); |
| 1165 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1166 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1167 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 1168 | std::string sec_required_csp = NavigateWithRequiredCSP( |
Antonio Sartori | 4231e93 | 2021-02-04 12:01:14 | [diff] [blame] | 1169 | &grand_child_document, "sandbox; report-to group"); |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 1170 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1171 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 1172 | EXPECT_EQ("script-src 'none'", |
| 1173 | grand_child_document->required_csp()->header->header_value); |
| 1174 | } |
| 1175 | |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 1176 | namespace { |
| 1177 | |
| 1178 | // Mock that allows us to avoid depending on the origin_trials component. |
| 1179 | class OriginTrialsControllerDelegateMock |
| 1180 | : public OriginTrialsControllerDelegate { |
| 1181 | public: |
| 1182 | ~OriginTrialsControllerDelegateMock() override = default; |
| 1183 | |
| 1184 | void PersistTrialsFromTokens( |
| 1185 | const url::Origin& origin, |
Peter Birk Pakkenberg | f62286a | 2023-01-25 19:44:07 | [diff] [blame] | 1186 | const url::Origin& partition_origin, |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 1187 | const base::span<const std::string> header_tokens, |
Svend Larsen | 113ac5b | 2024-06-05 20:56:20 | [diff] [blame] | 1188 | const base::Time current_time, |
| 1189 | std::optional<ukm::SourceId> source_id) override { |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 1190 | persisted_tokens_[origin] = |
| 1191 | std::vector<std::string>(header_tokens.begin(), header_tokens.end()); |
| 1192 | } |
Peter Birk Pakkenberg | 582edfc | 2023-02-27 11:22:12 | [diff] [blame] | 1193 | void PersistAdditionalTrialsFromTokens( |
| 1194 | const url::Origin& origin, |
| 1195 | const url::Origin& partition_origin, |
| 1196 | const base::span<const url::Origin> script_origins, |
| 1197 | const base::span<const std::string> header_tokens, |
Svend Larsen | 113ac5b | 2024-06-05 20:56:20 | [diff] [blame] | 1198 | const base::Time current_time, |
| 1199 | std::optional<ukm::SourceId> source_id) override { |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 1200 | NOTREACHED() << "not used by test"; |
Peter Birk Pakkenberg | 582edfc | 2023-02-27 11:22:12 | [diff] [blame] | 1201 | } |
Peter Birk Pakkenberg | 36b79c91 | 2023-05-25 08:38:24 | [diff] [blame] | 1202 | bool IsFeaturePersistedForOrigin(const url::Origin& origin, |
| 1203 | const url::Origin& partition_origin, |
Yao Xiao | 5c6e1fa | 2023-10-04 19:12:22 | [diff] [blame] | 1204 | blink::mojom::OriginTrialFeature feature, |
Peter Birk Pakkenberg | 36b79c91 | 2023-05-25 08:38:24 | [diff] [blame] | 1205 | const base::Time current_time) override { |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 1206 | DCHECK(false) << "Method not implemented for test."; |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
Peter Birk Pakkenberg | 4c1bb27 | 2022-09-27 10:51:29 | [diff] [blame] | 1210 | base::flat_set<std::string> GetPersistedTrialsForOrigin( |
| 1211 | const url::Origin& origin, |
Peter Birk Pakkenberg | f62286a | 2023-01-25 19:44:07 | [diff] [blame] | 1212 | const url::Origin& partition_origin, |
Peter Birk Pakkenberg | 4c1bb27 | 2022-09-27 10:51:29 | [diff] [blame] | 1213 | base::Time current_time) override { |
| 1214 | DCHECK(false) << "Method not implemented for test."; |
| 1215 | return base::flat_set<std::string>(); |
| 1216 | } |
| 1217 | |
Peter Birk Pakkenberg | d74c6f8 | 2022-11-08 17:44:38 | [diff] [blame] | 1218 | void ClearPersistedTokens() override { persisted_tokens_.clear(); } |
| 1219 | |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 1220 | base::flat_map<url::Origin, std::vector<std::string>> persisted_tokens_; |
| 1221 | }; |
| 1222 | |
| 1223 | } // namespace |
| 1224 | |
| 1225 | class PersistentOriginTrialNavigationRequestTest |
| 1226 | : public NavigationRequestTest { |
| 1227 | public: |
| 1228 | PersistentOriginTrialNavigationRequestTest() |
| 1229 | : delegate_mock_(std::make_unique<OriginTrialsControllerDelegateMock>()) { |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 1230 | } |
| 1231 | ~PersistentOriginTrialNavigationRequestTest() override = default; |
| 1232 | |
| 1233 | std::vector<std::string> GetPersistedTokens(const url::Origin& origin) { |
| 1234 | return delegate_mock_->persisted_tokens_[origin]; |
| 1235 | } |
| 1236 | |
| 1237 | protected: |
| 1238 | std::unique_ptr<BrowserContext> CreateBrowserContext() override { |
| 1239 | std::unique_ptr<TestBrowserContext> context = |
| 1240 | std::make_unique<TestBrowserContext>(); |
| 1241 | context->SetOriginTrialsControllerDelegate(delegate_mock_.get()); |
| 1242 | return context; |
| 1243 | } |
| 1244 | |
| 1245 | private: |
| 1246 | std::unique_ptr<OriginTrialsControllerDelegateMock> delegate_mock_; |
| 1247 | }; |
| 1248 | |
| 1249 | // Ensure that navigations with a valid Origin-Trial header with a persistent |
| 1250 | // origin trial token results in the trial being marked as enabled. |
| 1251 | // Then check that subsequent navigations without headers trigger an update |
| 1252 | // that clears out stored trials. |
| 1253 | TEST_F(PersistentOriginTrialNavigationRequestTest, |
| 1254 | NavigationCommitsPersistentOriginTrials) { |
| 1255 | // Generated with: |
| 1256 | // tools/origin_trials/generate_token.py https://example.com |
| 1257 | // FrobulatePersistent |
| 1258 | // --expire-timestamp=2000000000 |
| 1259 | const char kPersistentOriginTrialToken[] = |
| 1260 | "AzZfd1vKZ0SSGRGk/" |
| 1261 | "8nIszQSlHYjbuYVE3jwaNZG3X4t11zRhzPWWJwTZ+JJDS3JJsyEZcpz+y20pAP6/" |
| 1262 | "6upOQ4AAABdeyJvcmlnaW4iOiAiaHR0cHM6Ly9leGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZ" |
| 1263 | "SI" |
| 1264 | "6ICJGcm9idWxhdGVQZXJzaXN0ZW50IiwgImV4cGlyeSI6IDIwMDAwMDAwMDB9"; |
| 1265 | |
Peter Birk Pakkenberg | 73e07b6 | 2022-09-21 11:20:30 | [diff] [blame] | 1266 | blink::ScopedTestOriginTrialPolicy origin_trial_policy_; |
| 1267 | |
| 1268 | const GURL kUrl = GURL("https://example.com"); |
| 1269 | auto navigation = |
| 1270 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 1271 | |
| 1272 | auto response_headers = |
| 1273 | base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK"); |
| 1274 | response_headers->SetHeader("Origin-Trial", kPersistentOriginTrialToken); |
| 1275 | navigation->SetResponseHeaders(response_headers); |
| 1276 | |
| 1277 | navigation->Commit(); |
| 1278 | |
| 1279 | url::Origin origin = url::Origin::Create(kUrl); |
| 1280 | EXPECT_EQ(std::vector<std::string>{kPersistentOriginTrialToken}, |
| 1281 | GetPersistedTokens(origin)); |
| 1282 | |
| 1283 | // Navigate again without response headers to assert the trial information is |
| 1284 | // still updated and cleared. |
| 1285 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh())->Commit(); |
| 1286 | EXPECT_EQ(std::vector<std::string>(), GetPersistedTokens(origin)); |
| 1287 | } |
| 1288 | |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1289 | namespace { |
| 1290 | |
| 1291 | // Test version of a NavigationThrottle that requests the response body. |
| 1292 | class ResponseBodyNavigationThrottle : public NavigationThrottle { |
| 1293 | public: |
| 1294 | using ResponseBodyCallback = base::OnceCallback<void(const std::string&)>; |
| 1295 | |
Takashi Toyoshima | 74e6ef30 | 2025-06-06 11:28:41 | [diff] [blame] | 1296 | ResponseBodyNavigationThrottle(NavigationThrottleRegistry& registry, |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1297 | ResponseBodyCallback callback) |
Takashi Toyoshima | 74e6ef30 | 2025-06-06 11:28:41 | [diff] [blame] | 1298 | : NavigationThrottle(registry), callback_(std::move(callback)) {} |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1299 | ResponseBodyNavigationThrottle(const ResponseBodyNavigationThrottle&) = |
| 1300 | delete; |
| 1301 | ResponseBodyNavigationThrottle& operator=( |
| 1302 | const ResponseBodyNavigationThrottle&) = delete; |
| 1303 | ~ResponseBodyNavigationThrottle() override = default; |
| 1304 | |
| 1305 | NavigationThrottle::ThrottleCheckResult WillProcessResponse() override { |
| 1306 | navigation_handle()->GetResponseBody( |
| 1307 | base::BindOnce(&ResponseBodyNavigationThrottle::OnResponseBodyReady, |
| 1308 | base::Unretained(this))); |
| 1309 | return NavigationThrottle::DEFER; |
| 1310 | } |
| 1311 | |
| 1312 | const char* GetNameForLogging() override { |
| 1313 | return "ResponseBodyNavigationThrottle"; |
| 1314 | } |
| 1315 | |
| 1316 | private: |
| 1317 | void OnResponseBodyReady(const std::string& response_body) { |
| 1318 | std::move(callback_).Run(response_body); |
| 1319 | NavigationRequest::From(navigation_handle()) |
Takashi Toyoshima | cc4d947f | 2025-06-24 08:38:34 | [diff] [blame] | 1320 | ->GetNavigationThrottleRegistryForTesting() |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1321 | ->GetNavigationThrottleRunnerForTesting() |
Takashi Toyoshima | cc4d947f | 2025-06-24 08:38:34 | [diff] [blame] | 1322 | .CallResumeForTesting(); |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | ResponseBodyCallback callback_; |
| 1326 | }; |
| 1327 | |
| 1328 | } // namespace |
| 1329 | |
| 1330 | // Tests response body. |
| 1331 | class NavigationRequestResponseBodyTest : public NavigationRequestTest { |
| 1332 | public: |
| 1333 | std::unique_ptr<NavigationSimulator> CreateNavigationSimulator() { |
| 1334 | auto navigation = NavigationSimulatorImpl::CreateRendererInitiated( |
| 1335 | GURL("http://example.test"), main_rfh()); |
| 1336 | navigation->SetAutoAdvance(false); |
| 1337 | navigation->Start(); |
| 1338 | // It is safe to use base::Unretained as the NavigationThrottle will not be |
| 1339 | // destroyed before the callback is called. |
Takashi Toyoshima | 74e6ef30 | 2025-06-06 11:28:41 | [diff] [blame] | 1340 | auto& registry = navigation->GetNavigationThrottleRegistry(); |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1341 | auto throttle = std::make_unique<ResponseBodyNavigationThrottle>( |
Takashi Toyoshima | 74e6ef30 | 2025-06-06 11:28:41 | [diff] [blame] | 1342 | registry, |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1343 | base::BindOnce(&NavigationRequestResponseBodyTest::UpdateResponseBody, |
| 1344 | base::Unretained(this))); |
Takashi Toyoshima | 74e6ef30 | 2025-06-06 11:28:41 | [diff] [blame] | 1345 | registry.AddThrottle(std::move(throttle)); |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1346 | return navigation; |
| 1347 | } |
| 1348 | |
| 1349 | void UpdateResponseBody(const std::string& response_body) { |
| 1350 | response_body_ = response_body; |
| 1351 | was_callback_called_ = true; |
| 1352 | } |
| 1353 | |
| 1354 | bool was_callback_called() const { return was_callback_called_; } |
| 1355 | |
| 1356 | const std::string& response_body() const { return response_body_; } |
| 1357 | |
| 1358 | protected: |
| 1359 | mojo::ScopedDataPipeProducerHandle producer_handle_; |
| 1360 | mojo::ScopedDataPipeConsumerHandle consumer_handle_; |
| 1361 | |
| 1362 | private: |
| 1363 | bool was_callback_called_ = false; |
| 1364 | std::string response_body_; |
| 1365 | }; |
| 1366 | |
| 1367 | TEST_F(NavigationRequestResponseBodyTest, Received) { |
| 1368 | auto navigation = CreateNavigationSimulator(); |
| 1369 | std::string response = "response-body-content"; |
Lukasz Anforowicz | 2a672e3 | 2024-06-14 17:27:49 | [diff] [blame] | 1370 | ASSERT_EQ(MOJO_RESULT_OK, |
| 1371 | mojo::CreateDataPipe(response.size(), producer_handle_, |
| 1372 | consumer_handle_)); |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1373 | navigation->SetResponseBody(std::move(consumer_handle_)); |
| 1374 | |
| 1375 | navigation->ReadyToCommit(); |
| 1376 | EXPECT_EQ( |
| 1377 | NavigationRequest::WILL_PROCESS_RESPONSE, |
| 1378 | NavigationRequest::From(navigation->GetNavigationHandle())->state()); |
| 1379 | EXPECT_FALSE(was_callback_called()); |
| 1380 | EXPECT_EQ(std::string(), response_body()); |
| 1381 | |
Lukasz Anforowicz | 2a672e3 | 2024-06-14 17:27:49 | [diff] [blame] | 1382 | size_t actually_written_bytes = 0; |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1383 | ASSERT_EQ(MOJO_RESULT_OK, |
Lukasz Anforowicz | 2a672e3 | 2024-06-14 17:27:49 | [diff] [blame] | 1384 | producer_handle_->WriteData(base::as_byte_span(response), |
| 1385 | MOJO_WRITE_DATA_FLAG_NONE, |
| 1386 | actually_written_bytes)); |
| 1387 | EXPECT_EQ(actually_written_bytes, response.size()); |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1388 | |
| 1389 | navigation->Wait(); |
| 1390 | EXPECT_EQ( |
| 1391 | NavigationRequest::READY_TO_COMMIT, |
| 1392 | NavigationRequest::From(navigation->GetNavigationHandle())->state()); |
| 1393 | EXPECT_TRUE(was_callback_called()); |
| 1394 | EXPECT_EQ(response, response_body()); |
| 1395 | } |
| 1396 | |
| 1397 | TEST_F(NavigationRequestResponseBodyTest, PartiallyReceived) { |
| 1398 | auto navigation = CreateNavigationSimulator(); |
| 1399 | |
| 1400 | // The data pipe size is smaller than the response body size. |
| 1401 | uint32_t pipe_size = 8u; |
| 1402 | ASSERT_EQ(MOJO_RESULT_OK, mojo::CreateDataPipe(pipe_size, producer_handle_, |
| 1403 | consumer_handle_)); |
| 1404 | navigation->SetResponseBody(std::move(consumer_handle_)); |
| 1405 | |
| 1406 | navigation->ReadyToCommit(); |
| 1407 | EXPECT_EQ( |
| 1408 | NavigationRequest::WILL_PROCESS_RESPONSE, |
| 1409 | NavigationRequest::From(navigation->GetNavigationHandle())->state()); |
| 1410 | EXPECT_FALSE(was_callback_called()); |
| 1411 | EXPECT_EQ(std::string(), response_body()); |
| 1412 | |
| 1413 | std::string response = "response-body-content"; |
Lukasz Anforowicz | 2a672e3 | 2024-06-14 17:27:49 | [diff] [blame] | 1414 | size_t actually_written_bytes = 0; |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1415 | ASSERT_EQ(MOJO_RESULT_OK, |
Lukasz Anforowicz | 2a672e3 | 2024-06-14 17:27:49 | [diff] [blame] | 1416 | producer_handle_->WriteData(base::as_byte_span(response), |
| 1417 | MOJO_WRITE_DATA_FLAG_NONE, |
| 1418 | actually_written_bytes)); |
| 1419 | EXPECT_EQ(actually_written_bytes, pipe_size); |
Igor Ruvinov | dcb8198 | 2023-06-21 22:22:17 | [diff] [blame] | 1420 | |
| 1421 | navigation->Wait(); |
| 1422 | EXPECT_EQ( |
| 1423 | NavigationRequest::READY_TO_COMMIT, |
| 1424 | NavigationRequest::From(navigation->GetNavigationHandle())->state()); |
| 1425 | EXPECT_TRUE(was_callback_called()); |
| 1426 | // Only the first part of the response body that fits in the pipe is received. |
| 1427 | EXPECT_EQ("response", response_body()); |
| 1428 | } |
| 1429 | |
| 1430 | TEST_F(NavigationRequestResponseBodyTest, PipeClosed) { |
| 1431 | auto navigation = CreateNavigationSimulator(); |
| 1432 | ASSERT_EQ(MOJO_RESULT_OK, |
| 1433 | mojo::CreateDataPipe(10u, producer_handle_, consumer_handle_)); |
| 1434 | navigation->SetResponseBody(std::move(consumer_handle_)); |
| 1435 | navigation->ReadyToCommit(); |
| 1436 | EXPECT_EQ( |
| 1437 | NavigationRequest::WILL_PROCESS_RESPONSE, |
| 1438 | NavigationRequest::From(navigation->GetNavigationHandle())->state()); |
| 1439 | EXPECT_FALSE(was_callback_called()); |
| 1440 | EXPECT_EQ(std::string(), response_body()); |
| 1441 | |
| 1442 | // Close the pipe before any data is sent. |
| 1443 | producer_handle_.reset(); |
| 1444 | navigation->Wait(); |
| 1445 | EXPECT_EQ( |
| 1446 | NavigationRequest::READY_TO_COMMIT, |
| 1447 | NavigationRequest::From(navigation->GetNavigationHandle())->state()); |
| 1448 | EXPECT_TRUE(was_callback_called()); |
| 1449 | EXPECT_EQ(std::string(), response_body()); |
| 1450 | } |
| 1451 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 1452 | } // namespace content |