Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sebastien Marchand | f8cbfab | 2019-01-25 16:02:30 | [diff] [blame] | 5 | #include "base/bind.h" |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 6 | #include "base/callback.h" |
| 7 | #include "base/run_loop.h" |
Christopher Thompson | 2a4c69e9 | 2019-08-21 16:29:11 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
David Benjamin | ff93edd | 2019-06-19 22:51:58 | [diff] [blame] | 9 | #include "base/synchronization/lock.h" |
John Abd-El-Malek | 1f61a5fd | 2018-09-21 01:13:14 | [diff] [blame] | 10 | #include "base/test/bind_test_util.h" |
Christopher Thompson | 2a4c69e9 | 2019-08-21 16:29:11 | [diff] [blame] | 11 | #include "base/test/metrics/histogram_tester.h" |
| 12 | #include "base/test/scoped_command_line.h" |
| 13 | #include "base/test/scoped_environment_variable_override.h" |
Hiroki Nakagawa | 3b90e64 | 2019-09-10 01:59:58 | [diff] [blame] | 14 | #include "base/test/scoped_feature_list.h" |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 15 | #include "base/test/test_timeouts.h" |
David Benjamin | ff93edd | 2019-06-19 22:51:58 | [diff] [blame] | 16 | #include "base/thread_annotations.h" |
John Abd-El-Malek | a728915 | 2018-02-17 00:16:19 | [diff] [blame] | 17 | #include "base/threading/thread_restrictions.h" |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 18 | #include "build/build_config.h" |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 19 | #include "content/browser/frame_host/render_frame_host_impl.h" |
John Abd-El-Malek | 1f61a5fd | 2018-09-21 01:13:14 | [diff] [blame] | 20 | #include "content/browser/frame_host/render_frame_message_filter.h" |
John Abd-El-Malek | 984dc00 | 2019-05-29 01:26:59 | [diff] [blame] | 21 | #include "content/browser/network_service_instance_impl.h" |
John Abd-El-Malek | 1f61a5fd | 2018-09-21 01:13:14 | [diff] [blame] | 22 | #include "content/browser/renderer_host/render_process_host_impl.h" |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 23 | #include "content/browser/service_worker/embedded_worker_instance.h" |
| 24 | #include "content/browser/service_worker/embedded_worker_status.h" |
| 25 | #include "content/browser/service_worker/service_worker_context_core_observer.h" |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 26 | #include "content/browser/storage_partition_impl.h" |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 27 | #include "content/browser/url_loader_factory_getter.h" |
Patrick Monette | 3f51a04 | 2019-08-30 17:57:04 | [diff] [blame] | 28 | #include "content/browser/worker_host/test_shared_worker_service_impl.h" |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 29 | #include "content/public/browser/browser_context.h" |
Gabriel Charette | 790754c | 2018-03-16 21:32:59 | [diff] [blame] | 30 | #include "content/public/browser/browser_thread.h" |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 31 | #include "content/public/browser/network_service_instance.h" |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 32 | #include "content/public/browser/web_contents.h" |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 33 | #include "content/public/common/content_switches.h" |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 34 | #include "content/public/common/network_service_util.h" |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 35 | #include "content/public/test/browser_test_utils.h" |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 36 | #include "content/public/test/content_browser_test.h" |
| 37 | #include "content/public/test/content_browser_test_utils.h" |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 38 | #include "content/public/test/simple_url_loader_test_helper.h" |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 39 | #include "content/public/test/test_utils.h" |
Patrick Monette | 3f51a04 | 2019-08-30 17:57:04 | [diff] [blame] | 40 | #include "content/public/test/web_test_support.h" |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 41 | #include "content/shell/browser/shell.h" |
John Abd-El-Malek | a728915 | 2018-02-17 00:16:19 | [diff] [blame] | 42 | #include "content/shell/browser/shell_browser_context.h" |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 43 | #include "content/test/storage_partition_test_utils.h" |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 44 | #include "mojo/public/cpp/bindings/pending_remote.h" |
| 45 | #include "mojo/public/cpp/bindings/remote.h" |
Clark DuVall | f742bcbd | 2018-11-07 17:16:20 | [diff] [blame] | 46 | #include "mojo/public/cpp/bindings/sync_call_restrictions.h" |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 47 | #include "net/dns/mock_host_resolver.h" |
| 48 | #include "net/test/embedded_test_server/http_request.h" |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 49 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
John Abd-El-Malek | bdafede | 2018-02-06 15:10:36 | [diff] [blame] | 50 | #include "services/network/public/cpp/simple_url_loader.h" |
Ken Rockot | 54311e6 | 2018-02-10 19:01:52 | [diff] [blame] | 51 | #include "services/network/public/mojom/network_service.mojom.h" |
Clark DuVall | f742bcbd | 2018-11-07 17:16:20 | [diff] [blame] | 52 | #include "services/network/public/mojom/network_service_test.mojom.h" |
Hiroki Nakagawa | 3b90e64 | 2019-09-10 01:59:58 | [diff] [blame] | 53 | #include "third_party/blink/public/common/features.h" |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 54 | |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 55 | #if BUILDFLAG(ENABLE_PLUGINS) |
| 56 | #include "content/public/test/ppapi_test_utils.h" |
| 57 | #endif |
| 58 | |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 59 | namespace content { |
| 60 | |
| 61 | namespace { |
| 62 | |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 63 | using SharedURLLoaderFactoryGetterCallback = |
Chong Zhang | b7c8d1ce | 2018-03-13 19:14:11 | [diff] [blame] | 64 | base::OnceCallback<scoped_refptr<network::SharedURLLoaderFactory>()>; |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 65 | |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 66 | mojo::PendingRemote<network::mojom::NetworkContext> CreateNetworkContext() { |
| 67 | mojo::PendingRemote<network::mojom::NetworkContext> network_context; |
John Abd-El-Malek | 53670dd | 2018-01-18 22:07:21 | [diff] [blame] | 68 | network::mojom::NetworkContextParamsPtr context_params = |
| 69 | network::mojom::NetworkContextParams::New(); |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 70 | GetNetworkService()->CreateNetworkContext( |
| 71 | network_context.InitWithNewPipeAndPassReceiver(), |
| 72 | std::move(context_params)); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 73 | return network_context; |
| 74 | } |
| 75 | |
John Abd-El-Malek | a728915 | 2018-02-17 00:16:19 | [diff] [blame] | 76 | int LoadBasicRequestOnUIThread( |
| 77 | network::mojom::URLLoaderFactory* url_loader_factory, |
| 78 | const GURL& url) { |
| 79 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 80 | auto request = std::make_unique<network::ResourceRequest>(); |
| 81 | request->url = url; |
| 82 | |
| 83 | SimpleURLLoaderTestHelper simple_loader_helper; |
| 84 | std::unique_ptr<network::SimpleURLLoader> simple_loader = |
| 85 | network::SimpleURLLoader::Create(std::move(request), |
| 86 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 87 | simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie( |
| 88 | url_loader_factory, simple_loader_helper.GetCallback()); |
| 89 | simple_loader_helper.WaitForCallback(); |
| 90 | return simple_loader->NetError(); |
| 91 | } |
| 92 | |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 93 | std::vector<network::mojom::NetworkUsagePtr> GetTotalNetworkUsages() { |
| 94 | std::vector<network::mojom::NetworkUsagePtr> network_usages; |
| 95 | base::RunLoop run_loop; |
| 96 | GetNetworkService()->GetTotalNetworkUsages(base::BindOnce( |
| 97 | [](std::vector<network::mojom::NetworkUsagePtr>* p_network_usages, |
| 98 | base::OnceClosure quit_closure, |
| 99 | std::vector<network::mojom::NetworkUsagePtr> returned_usages) { |
| 100 | *p_network_usages = std::move(returned_usages); |
| 101 | std::move(quit_closure).Run(); |
| 102 | }, |
| 103 | base::Unretained(&network_usages), run_loop.QuitClosure())); |
| 104 | run_loop.Run(); |
| 105 | return network_usages; |
| 106 | } |
| 107 | |
| 108 | bool CheckContainsProcessID( |
| 109 | const std::vector<network::mojom::NetworkUsagePtr>& usages, |
| 110 | int process_id) { |
| 111 | for (const auto& usage : usages) { |
| 112 | if ((int)usage->process_id == process_id) |
| 113 | return true; |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | // Wait until |condition| returns true. |
| 119 | void WaitForCondition(base::RepeatingCallback<bool()> condition) { |
| 120 | while (!condition.Run()) { |
| 121 | base::RunLoop run_loop; |
| 122 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 123 | FROM_HERE, run_loop.QuitClosure(), TestTimeouts::tiny_timeout()); |
| 124 | run_loop.Run(); |
| 125 | } |
| 126 | } |
| 127 | |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 128 | class ServiceWorkerStatusObserver : public ServiceWorkerContextCoreObserver { |
| 129 | public: |
Patrick Monette | 9f3aae6 | 2019-10-08 00:16:03 | [diff] [blame] | 130 | void WaitForStopped() { |
| 131 | if (stopped_) |
| 132 | return; |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 133 | |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 134 | base::RunLoop loop; |
| 135 | callback_ = loop.QuitClosure(); |
| 136 | loop.Run(); |
| 137 | } |
| 138 | |
| 139 | private: |
Patrick Monette | 9f3aae6 | 2019-10-08 00:16:03 | [diff] [blame] | 140 | void OnStopped(int64_t version_id) override { |
| 141 | stopped_ = true; |
| 142 | |
| 143 | if (callback_) |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 144 | std::move(callback_).Run(); |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 145 | } |
| 146 | |
Patrick Monette | 9f3aae6 | 2019-10-08 00:16:03 | [diff] [blame] | 147 | bool stopped_ = false; |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 148 | base::OnceClosure callback_; |
| 149 | }; |
| 150 | |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 151 | } // namespace |
| 152 | |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 153 | class NetworkServiceRestartBrowserTest : public ContentBrowserTest { |
| 154 | public: |
John Abd-El-Malek | c134dd8 | 2019-07-31 20:51:44 | [diff] [blame] | 155 | NetworkServiceRestartBrowserTest() {} |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 156 | |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 157 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 158 | #if BUILDFLAG(ENABLE_PLUGINS) |
| 159 | ASSERT_TRUE(ppapi::RegisterCorbTestPlugin(command_line)); |
| 160 | #endif |
| 161 | ContentBrowserTest::SetUpCommandLine(command_line); |
| 162 | } |
| 163 | |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 164 | void SetUpOnMainThread() override { |
Chong Zhang | 5d9b3418 | 2018-01-30 16:20:57 | [diff] [blame] | 165 | embedded_test_server()->RegisterRequestMonitor( |
| 166 | base::BindRepeating(&NetworkServiceRestartBrowserTest::MonitorRequest, |
| 167 | base::Unretained(this))); |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 168 | host_resolver()->AddRule("*", "127.0.0.1"); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 169 | EXPECT_TRUE(embedded_test_server()->Start()); |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 170 | ContentBrowserTest::SetUpOnMainThread(); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 171 | } |
| 172 | |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 173 | GURL GetTestURL() const { |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 174 | // Use '/echoheader' instead of '/echo' to avoid a disk_cache bug. |
| 175 | // See https://crbug.com/792255. |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 176 | return embedded_test_server()->GetURL("/echoheader"); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 177 | } |
| 178 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 179 | BrowserContext* browser_context() { |
| 180 | return shell()->web_contents()->GetBrowserContext(); |
| 181 | } |
| 182 | |
Chong Zhang | 5d9b3418 | 2018-01-30 16:20:57 | [diff] [blame] | 183 | RenderFrameHostImpl* main_frame() { |
| 184 | return static_cast<RenderFrameHostImpl*>( |
| 185 | shell()->web_contents()->GetMainFrame()); |
| 186 | } |
| 187 | |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 188 | bool CheckCanLoadHttp(Shell* shell, const std::string& relative_url) { |
Chong Zhang | 5d9b3418 | 2018-01-30 16:20:57 | [diff] [blame] | 189 | GURL test_url = embedded_test_server()->GetURL(relative_url); |
| 190 | std::string script( |
| 191 | "var xhr = new XMLHttpRequest();" |
| 192 | "xhr.open('GET', '"); |
| 193 | script += test_url.spec() + |
| 194 | "', true);" |
| 195 | "xhr.onload = function (e) {" |
| 196 | " if (xhr.readyState === 4) {" |
| 197 | " window.domAutomationController.send(xhr.status === 200);" |
| 198 | " }" |
| 199 | "};" |
| 200 | "xhr.onerror = function () {" |
| 201 | " window.domAutomationController.send(false);" |
| 202 | "};" |
| 203 | "xhr.send(null)"; |
| 204 | bool xhr_result = false; |
| 205 | // The JS call will fail if disallowed because the process will be killed. |
| 206 | bool execute_result = |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 207 | ExecuteScriptAndExtractBool(shell, script, &xhr_result); |
Chong Zhang | 5d9b3418 | 2018-01-30 16:20:57 | [diff] [blame] | 208 | return xhr_result && execute_result; |
| 209 | } |
| 210 | |
Chong Zhang | 4513fab | 2018-02-28 18:50:18 | [diff] [blame] | 211 | // Will reuse the single opened windows through the test case. |
| 212 | bool CheckCanLoadHttpInWindowOpen(const std::string& relative_url) { |
| 213 | GURL test_url = embedded_test_server()->GetURL(relative_url); |
| 214 | std::string inject_script = base::StringPrintf( |
| 215 | "var xhr = new XMLHttpRequest();" |
| 216 | "xhr.open('GET', '%s', true);" |
| 217 | "xhr.onload = function (e) {" |
| 218 | " if (xhr.readyState === 4) {" |
| 219 | " window.opener.domAutomationController.send(xhr.status === 200);" |
| 220 | " }" |
| 221 | "};" |
| 222 | "xhr.onerror = function () {" |
| 223 | " window.opener.domAutomationController.send(false);" |
| 224 | "};" |
| 225 | "xhr.send(null)", |
| 226 | test_url.spec().c_str()); |
| 227 | std::string window_open_script = base::StringPrintf( |
| 228 | "var new_window = new_window || window.open('');" |
| 229 | "var inject_script = document.createElement('script');" |
| 230 | "inject_script.innerHTML = \"%s\";" |
| 231 | "new_window.document.body.appendChild(inject_script);", |
| 232 | inject_script.c_str()); |
| 233 | |
| 234 | bool xhr_result = false; |
| 235 | // The JS call will fail if disallowed because the process will be killed. |
| 236 | bool execute_result = |
| 237 | ExecuteScriptAndExtractBool(shell(), window_open_script, &xhr_result); |
| 238 | return xhr_result && execute_result; |
| 239 | } |
| 240 | |
| 241 | // Workers will live throughout the test case unless terminated. |
| 242 | bool CheckCanWorkerFetch(const std::string& worker_name, |
| 243 | const std::string& relative_url) { |
| 244 | GURL worker_url = |
| 245 | embedded_test_server()->GetURL("/workers/worker_common.js"); |
| 246 | GURL fetch_url = embedded_test_server()->GetURL(relative_url); |
| 247 | std::string script = base::StringPrintf( |
| 248 | "var workers = workers || {};" |
| 249 | "var worker_name = '%s';" |
| 250 | "workers[worker_name] = workers[worker_name] || new Worker('%s');" |
| 251 | "workers[worker_name].onmessage = evt => {" |
| 252 | " if (evt.data != 'wait')" |
| 253 | " window.domAutomationController.send(evt.data === 200);" |
| 254 | "};" |
| 255 | "workers[worker_name].postMessage(\"eval " |
| 256 | " fetch(new Request('%s'))" |
| 257 | " .then(res => postMessage(res.status))" |
| 258 | " .catch(error => postMessage(error.toString()));" |
| 259 | " 'wait'" |
| 260 | "\");", |
| 261 | worker_name.c_str(), worker_url.spec().c_str(), |
| 262 | fetch_url.spec().c_str()); |
| 263 | bool fetch_result = false; |
| 264 | // The JS call will fail if disallowed because the process will be killed. |
| 265 | bool execute_result = |
| 266 | ExecuteScriptAndExtractBool(shell(), script, &fetch_result); |
| 267 | return fetch_result && execute_result; |
| 268 | } |
| 269 | |
| 270 | // Terminate and delete the worker. |
| 271 | bool TerminateWorker(const std::string& worker_name) { |
| 272 | std::string script = base::StringPrintf( |
| 273 | "var workers = workers || {};" |
| 274 | "var worker_name = '%s';" |
| 275 | "if (workers[worker_name]) {" |
| 276 | " workers[worker_name].terminate();" |
| 277 | " delete workers[worker_name];" |
| 278 | " window.domAutomationController.send(true);" |
| 279 | "} else {" |
| 280 | " window.domAutomationController.send(false);" |
| 281 | "}", |
| 282 | worker_name.c_str()); |
| 283 | bool fetch_result = false; |
| 284 | // The JS call will fail if disallowed because the process will be killed. |
| 285 | bool execute_result = |
| 286 | ExecuteScriptAndExtractBool(shell(), script, &fetch_result); |
| 287 | return fetch_result && execute_result; |
| 288 | } |
| 289 | |
Chong Zhang | 5d9b3418 | 2018-01-30 16:20:57 | [diff] [blame] | 290 | // Called by |embedded_test_server()|. |
| 291 | void MonitorRequest(const net::test_server::HttpRequest& request) { |
David Benjamin | ff93edd | 2019-06-19 22:51:58 | [diff] [blame] | 292 | base::AutoLock lock(last_request_lock_); |
Chong Zhang | 5d9b3418 | 2018-01-30 16:20:57 | [diff] [blame] | 293 | last_request_relative_url_ = request.relative_url; |
| 294 | } |
| 295 | |
| 296 | std::string last_request_relative_url() const { |
David Benjamin | ff93edd | 2019-06-19 22:51:58 | [diff] [blame] | 297 | base::AutoLock lock(last_request_lock_); |
Chong Zhang | 5d9b3418 | 2018-01-30 16:20:57 | [diff] [blame] | 298 | return last_request_relative_url_; |
| 299 | } |
| 300 | |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 301 | private: |
David Benjamin | ff93edd | 2019-06-19 22:51:58 | [diff] [blame] | 302 | mutable base::Lock last_request_lock_; |
| 303 | std::string last_request_relative_url_ GUARDED_BY(last_request_lock_); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 304 | |
| 305 | DISALLOW_COPY_AND_ASSIGN(NetworkServiceRestartBrowserTest); |
| 306 | }; |
| 307 | |
| 308 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 309 | NetworkServiceProcessRecovery) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 310 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 311 | return; |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 312 | mojo::Remote<network::mojom::NetworkContext> network_context( |
| 313 | CreateNetworkContext()); |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 314 | EXPECT_EQ(net::OK, LoadBasicRequest(network_context.get(), GetTestURL())); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 315 | EXPECT_TRUE(network_context.is_bound()); |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 316 | EXPECT_TRUE(network_context.is_connected()); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 317 | |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 318 | // Crash the NetworkService process. Existing interfaces should receive error |
| 319 | // notifications at some point. |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 320 | SimulateNetworkServiceCrash(); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 321 | // |network_context| will receive an error notification, but it's not |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 322 | // guaranteed to have arrived at this point. Flush the remote to make sure |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 323 | // the notification has been received. |
| 324 | network_context.FlushForTesting(); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 325 | EXPECT_TRUE(network_context.is_bound()); |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 326 | EXPECT_FALSE(network_context.is_connected()); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 327 | // Make sure we could get |net::ERR_FAILED| with an invalid |network_context|. |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 328 | EXPECT_EQ(net::ERR_FAILED, |
| 329 | LoadBasicRequest(network_context.get(), GetTestURL())); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 330 | |
| 331 | // NetworkService should restart automatically and return valid interface. |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 332 | mojo::Remote<network::mojom::NetworkContext> network_context2( |
| 333 | CreateNetworkContext()); |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 334 | EXPECT_EQ(net::OK, LoadBasicRequest(network_context2.get(), GetTestURL())); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 335 | EXPECT_TRUE(network_context2.is_bound()); |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 336 | EXPECT_TRUE(network_context2.is_connected()); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 337 | } |
| 338 | |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 339 | void IncrementInt(int* i) { |
| 340 | *i = *i + 1; |
| 341 | } |
| 342 | |
| 343 | // This test verifies basic functionality of RegisterNetworkServiceCrashHandler |
| 344 | // and UnregisterNetworkServiceCrashHandler. |
| 345 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, CrashHandlers) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 346 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 347 | return; |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 348 | mojo::Remote<network::mojom::NetworkContext> network_context( |
| 349 | CreateNetworkContext()); |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 350 | EXPECT_TRUE(network_context.is_bound()); |
| 351 | |
| 352 | // Register 2 crash handlers. |
| 353 | int counter1 = 0; |
| 354 | int counter2 = 0; |
Lukasz Anforowicz | c195e91 | 2018-10-30 16:20:54 | [diff] [blame] | 355 | auto handler1 = RegisterNetworkServiceCrashHandler( |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 356 | base::BindRepeating(&IncrementInt, base::Unretained(&counter1))); |
Lukasz Anforowicz | c195e91 | 2018-10-30 16:20:54 | [diff] [blame] | 357 | auto handler2 = RegisterNetworkServiceCrashHandler( |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 358 | base::BindRepeating(&IncrementInt, base::Unretained(&counter2))); |
| 359 | |
| 360 | // Crash the NetworkService process. |
| 361 | SimulateNetworkServiceCrash(); |
| 362 | // |network_context| will receive an error notification, but it's not |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 363 | // guaranteed to have arrived at this point. Flush the remote to make sure |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 364 | // the notification has been received. |
| 365 | network_context.FlushForTesting(); |
| 366 | EXPECT_TRUE(network_context.is_bound()); |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 367 | EXPECT_FALSE(network_context.is_connected()); |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 368 | |
| 369 | // Verify the crash handlers executed. |
| 370 | EXPECT_EQ(1, counter1); |
| 371 | EXPECT_EQ(1, counter2); |
| 372 | |
| 373 | // Revive the NetworkService process. |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 374 | network_context.reset(); |
| 375 | network_context.Bind(CreateNetworkContext()); |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 376 | EXPECT_TRUE(network_context.is_bound()); |
| 377 | |
| 378 | // Unregister one of the handlers. |
Lukasz Anforowicz | c195e91 | 2018-10-30 16:20:54 | [diff] [blame] | 379 | handler2.reset(); |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 380 | |
| 381 | // Crash the NetworkService process. |
| 382 | SimulateNetworkServiceCrash(); |
| 383 | // |network_context| will receive an error notification, but it's not |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 384 | // guaranteed to have arrived at this point. Flush the remote to make sure |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 385 | // the notification has been received. |
| 386 | network_context.FlushForTesting(); |
| 387 | EXPECT_TRUE(network_context.is_bound()); |
Julie Jeongeun Kim | 5984e99 | 2019-09-11 11:00:46 | [diff] [blame] | 388 | EXPECT_FALSE(network_context.is_connected()); |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 389 | |
| 390 | // Verify only the first crash handler executed. |
| 391 | EXPECT_EQ(2, counter1); |
| 392 | EXPECT_EQ(1, counter2); |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 393 | } |
| 394 | |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 395 | // Make sure |StoragePartitionImpl::GetNetworkContext()| returns valid interface |
| 396 | // after crash. |
| 397 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
Chong Zhang | e0dfdb6 | 2017-12-09 18:25:14 | [diff] [blame] | 398 | StoragePartitionImplGetNetworkContext) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 399 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 400 | return; |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 401 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 402 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 403 | |
John Abd-El-Malek | 53670dd | 2018-01-18 22:07:21 | [diff] [blame] | 404 | network::mojom::NetworkContext* old_network_context = |
| 405 | partition->GetNetworkContext(); |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 406 | EXPECT_EQ(net::OK, LoadBasicRequest(old_network_context, GetTestURL())); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 407 | |
| 408 | // Crash the NetworkService process. Existing interfaces should receive error |
| 409 | // notifications at some point. |
| 410 | SimulateNetworkServiceCrash(); |
| 411 | // Flush the interface to make sure the error notification was received. |
| 412 | partition->FlushNetworkInterfaceForTesting(); |
| 413 | |
| 414 | // |partition->GetNetworkContext()| should return a valid new pointer after |
| 415 | // crash. |
| 416 | EXPECT_NE(old_network_context, partition->GetNetworkContext()); |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 417 | EXPECT_EQ(net::OK, |
| 418 | LoadBasicRequest(partition->GetNetworkContext(), GetTestURL())); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 419 | } |
| 420 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 421 | // Make sure |URLLoaderFactoryGetter| returns valid interface after crash. |
| 422 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 423 | URLLoaderFactoryGetterGetNetworkFactory) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 424 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 425 | return; |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 426 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 427 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 428 | scoped_refptr<URLLoaderFactoryGetter> url_loader_factory_getter = |
| 429 | partition->url_loader_factory_getter(); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 430 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 431 | auto factory_owner = IOThreadSharedURLLoaderFactoryOwner::Create( |
| 432 | url_loader_factory_getter.get()); |
| 433 | EXPECT_EQ(net::OK, factory_owner->LoadBasicRequestOnIOThread(GetTestURL())); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 434 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 435 | // Crash the NetworkService process. Existing interfaces should receive error |
| 436 | // notifications at some point. |
| 437 | SimulateNetworkServiceCrash(); |
| 438 | // Flush the interface to make sure the error notification was received. |
| 439 | partition->FlushNetworkInterfaceForTesting(); |
| 440 | url_loader_factory_getter->FlushNetworkInterfaceOnIOThreadForTesting(); |
| 441 | |
| 442 | // |url_loader_factory_getter| should be able to get a valid new pointer after |
| 443 | // crash. |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 444 | factory_owner = IOThreadSharedURLLoaderFactoryOwner::Create( |
| 445 | url_loader_factory_getter.get()); |
| 446 | EXPECT_EQ(net::OK, factory_owner->LoadBasicRequestOnIOThread(GetTestURL())); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // Make sure the factory returned from |
| 450 | // |URLLoaderFactoryGetter::GetNetworkFactory()| continues to work after |
| 451 | // crashes. |
| 452 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 453 | BrowserIOSharedURLLoaderFactory) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 454 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 455 | return; |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 456 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 457 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 458 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 459 | auto factory_owner = IOThreadSharedURLLoaderFactoryOwner::Create( |
| 460 | partition->url_loader_factory_getter().get()); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 461 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 462 | EXPECT_EQ(net::OK, factory_owner->LoadBasicRequestOnIOThread(GetTestURL())); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 463 | |
| 464 | // Crash the NetworkService process. Existing interfaces should receive error |
| 465 | // notifications at some point. |
| 466 | SimulateNetworkServiceCrash(); |
| 467 | // Flush the interface to make sure the error notification was received. |
| 468 | partition->FlushNetworkInterfaceForTesting(); |
| 469 | partition->url_loader_factory_getter() |
| 470 | ->FlushNetworkInterfaceOnIOThreadForTesting(); |
| 471 | |
| 472 | // |shared_factory| should continue to work. |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 473 | EXPECT_EQ(net::OK, factory_owner->LoadBasicRequestOnIOThread(GetTestURL())); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // Make sure the factory returned from |
| 477 | // |URLLoaderFactoryGetter::GetNetworkFactory()| doesn't crash if |
| 478 | // it's called after the StoragePartition is deleted. |
Chong Zhang | f857218 | 2018-03-16 23:10:18 | [diff] [blame] | 479 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 480 | BrowserIOSharedFactoryAfterStoragePartitionGone) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 481 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 482 | return; |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 483 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 484 | std::unique_ptr<ShellBrowserContext> browser_context = |
Eric Roman | b56d0ef | 2019-07-02 17:36:06 | [diff] [blame] | 485 | std::make_unique<ShellBrowserContext>(true); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 486 | auto* partition = static_cast<StoragePartitionImpl*>( |
| 487 | BrowserContext::GetDefaultStoragePartition(browser_context.get())); |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 488 | auto factory_owner = IOThreadSharedURLLoaderFactoryOwner::Create( |
| 489 | partition->url_loader_factory_getter().get()); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 490 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 491 | EXPECT_EQ(net::OK, factory_owner->LoadBasicRequestOnIOThread(GetTestURL())); |
Chong Zhang | 5271432a | 2018-03-01 23:31:02 | [diff] [blame] | 492 | |
| 493 | browser_context.reset(); |
| 494 | |
| 495 | EXPECT_EQ(net::ERR_FAILED, |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 496 | factory_owner->LoadBasicRequestOnIOThread(GetTestURL())); |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // Make sure basic navigation works after crash. |
| 500 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 501 | NavigationURLLoaderBasic) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 502 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 503 | return; |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 504 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 505 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 506 | |
| 507 | EXPECT_TRUE( |
| 508 | NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"))); |
| 509 | |
| 510 | // Crash the NetworkService process. Existing interfaces should receive error |
| 511 | // notifications at some point. |
| 512 | SimulateNetworkServiceCrash(); |
| 513 | // Flush the interface to make sure the error notification was received. |
| 514 | partition->FlushNetworkInterfaceForTesting(); |
| 515 | partition->url_loader_factory_getter() |
| 516 | ->FlushNetworkInterfaceOnIOThreadForTesting(); |
| 517 | |
| 518 | EXPECT_TRUE( |
| 519 | NavigateToURL(shell(), embedded_test_server()->GetURL("/title2.html"))); |
| 520 | } |
| 521 | |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 522 | // Make sure basic XHR works after crash. |
Chong Zhang | 5d9b3418 | 2018-01-30 16:20:57 | [diff] [blame] | 523 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, BasicXHR) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 524 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 525 | return; |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 526 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 527 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 528 | |
Chong Zhang | 5d9b3418 | 2018-01-30 16:20:57 | [diff] [blame] | 529 | EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL("/echo"))); |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 530 | EXPECT_TRUE(CheckCanLoadHttp(shell(), "/title1.html")); |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 531 | EXPECT_EQ(last_request_relative_url(), "/title1.html"); |
| 532 | |
| 533 | // Crash the NetworkService process. Existing interfaces should receive error |
| 534 | // notifications at some point. |
| 535 | SimulateNetworkServiceCrash(); |
| 536 | // Flush the interface to make sure the error notification was received. |
| 537 | partition->FlushNetworkInterfaceForTesting(); |
| 538 | // Flush the interface to make sure the frame host has received error |
| 539 | // notification and the new URLLoaderFactoryBundle has been received by the |
| 540 | // frame. |
| 541 | main_frame()->FlushNetworkAndNavigationInterfacesForTesting(); |
| 542 | |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 543 | EXPECT_TRUE(CheckCanLoadHttp(shell(), "/title2.html")); |
Chong Zhang | 7306b0b | 2018-01-24 05:59:24 | [diff] [blame] | 544 | EXPECT_EQ(last_request_relative_url(), "/title2.html"); |
| 545 | } |
| 546 | |
John Abd-El-Malek | a728915 | 2018-02-17 00:16:19 | [diff] [blame] | 547 | // Make sure the factory returned from |
| 548 | // |StoragePartition::GetURLLoaderFactoryForBrowserProcess()| continues to work |
| 549 | // after crashes. |
| 550 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, BrowserUIFactory) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 551 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 552 | return; |
John Abd-El-Malek | a728915 | 2018-02-17 00:16:19 | [diff] [blame] | 553 | auto* partition = |
| 554 | BrowserContext::GetDefaultStoragePartition(browser_context()); |
| 555 | auto* factory = partition->GetURLLoaderFactoryForBrowserProcess().get(); |
| 556 | |
| 557 | EXPECT_EQ(net::OK, LoadBasicRequestOnUIThread(factory, GetTestURL())); |
| 558 | |
| 559 | SimulateNetworkServiceCrash(); |
| 560 | // Flush the interface to make sure the error notification was received. |
| 561 | partition->FlushNetworkInterfaceForTesting(); |
| 562 | |
| 563 | EXPECT_EQ(net::OK, LoadBasicRequestOnUIThread(factory, GetTestURL())); |
| 564 | } |
| 565 | |
| 566 | // Make sure the factory returned from |
| 567 | // |StoragePartition::GetURLLoaderFactoryForBrowserProcess()| doesn't crash if |
| 568 | // it's called after the StoragePartition is deleted. |
| 569 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 570 | BrowserUIFactoryAfterStoragePartitionGone) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 571 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 572 | return; |
John Abd-El-Malek | a728915 | 2018-02-17 00:16:19 | [diff] [blame] | 573 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 574 | std::unique_ptr<ShellBrowserContext> browser_context = |
Eric Roman | b56d0ef | 2019-07-02 17:36:06 | [diff] [blame] | 575 | std::make_unique<ShellBrowserContext>(true); |
John Abd-El-Malek | a728915 | 2018-02-17 00:16:19 | [diff] [blame] | 576 | auto* partition = |
| 577 | BrowserContext::GetDefaultStoragePartition(browser_context.get()); |
Chong Zhang | b7c8d1ce | 2018-03-13 19:14:11 | [diff] [blame] | 578 | scoped_refptr<network::SharedURLLoaderFactory> factory( |
John Abd-El-Malek | a728915 | 2018-02-17 00:16:19 | [diff] [blame] | 579 | partition->GetURLLoaderFactoryForBrowserProcess()); |
| 580 | |
| 581 | EXPECT_EQ(net::OK, LoadBasicRequestOnUIThread(factory.get(), GetTestURL())); |
| 582 | |
| 583 | browser_context.reset(); |
| 584 | |
| 585 | EXPECT_EQ(net::ERR_FAILED, |
| 586 | LoadBasicRequestOnUIThread(factory.get(), GetTestURL())); |
| 587 | } |
| 588 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 589 | // Make sure the factory info returned from |
| 590 | // |StoragePartition::GetURLLoaderFactoryForBrowserProcessIOThread()| can be |
| 591 | // used after crashes. |
Guido Urdaneta | 9cbbe38 | 2018-05-07 14:51:06 | [diff] [blame] | 592 | // Flaky on Windows. https://crbug.com/840127 |
| 593 | #if defined(OS_WIN) |
| 594 | #define MAYBE_BrowserIOFactoryInfo DISABLED_BrowserIOFactoryInfo |
| 595 | #else |
| 596 | #define MAYBE_BrowserIOFactoryInfo BrowserIOFactoryInfo |
| 597 | #endif |
| 598 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 599 | MAYBE_BrowserIOFactoryInfo) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 600 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 601 | return; |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 602 | auto* partition = |
| 603 | BrowserContext::GetDefaultStoragePartition(browser_context()); |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 604 | auto shared_url_loader_factory_info = |
| 605 | partition->GetURLLoaderFactoryForBrowserProcessIOThread(); |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 606 | |
| 607 | SimulateNetworkServiceCrash(); |
| 608 | // Flush the interface to make sure the error notification was received. |
| 609 | partition->FlushNetworkInterfaceForTesting(); |
| 610 | static_cast<StoragePartitionImpl*>(partition) |
| 611 | ->url_loader_factory_getter() |
| 612 | ->FlushNetworkInterfaceOnIOThreadForTesting(); |
| 613 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 614 | auto factory_owner = IOThreadSharedURLLoaderFactoryOwner::Create( |
| 615 | std::move(shared_url_loader_factory_info)); |
| 616 | |
| 617 | EXPECT_EQ(net::OK, factory_owner->LoadBasicRequestOnIOThread(GetTestURL())); |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 618 | } |
| 619 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 620 | // Make sure the factory constructed from |
| 621 | // |StoragePartition::GetURLLoaderFactoryForBrowserProcessIOThread()| continues |
| 622 | // to work after crashes. |
| 623 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, BrowserIOFactory) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 624 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 625 | return; |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 626 | auto* partition = |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 627 | BrowserContext::GetDefaultStoragePartition(browser_context()); |
| 628 | auto factory_owner = IOThreadSharedURLLoaderFactoryOwner::Create( |
| 629 | partition->GetURLLoaderFactoryForBrowserProcessIOThread()); |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 630 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 631 | EXPECT_EQ(net::OK, factory_owner->LoadBasicRequestOnIOThread(GetTestURL())); |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 632 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 633 | SimulateNetworkServiceCrash(); |
| 634 | // Flush the interface to make sure the error notification was received. |
| 635 | partition->FlushNetworkInterfaceForTesting(); |
| 636 | static_cast<StoragePartitionImpl*>(partition) |
| 637 | ->url_loader_factory_getter() |
| 638 | ->FlushNetworkInterfaceOnIOThreadForTesting(); |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 639 | |
Chong Zhang | 83d0e4f | 2018-05-04 18:55:09 | [diff] [blame] | 640 | EXPECT_EQ(net::OK, factory_owner->LoadBasicRequestOnIOThread(GetTestURL())); |
Chong Zhang | 4dd97ebf | 2018-03-07 04:48:48 | [diff] [blame] | 641 | } |
| 642 | |
Chong Zhang | 4513fab | 2018-02-28 18:50:18 | [diff] [blame] | 643 | // Make sure the window from |window.open()| can load XHR after crash. |
| 644 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, WindowOpenXHR) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 645 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 646 | return; |
Chong Zhang | 4513fab | 2018-02-28 18:50:18 | [diff] [blame] | 647 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 648 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 649 | |
| 650 | EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL("/echo"))); |
| 651 | EXPECT_TRUE(CheckCanLoadHttpInWindowOpen("/title1.html")); |
| 652 | EXPECT_EQ(last_request_relative_url(), "/title1.html"); |
| 653 | |
| 654 | // Crash the NetworkService process. Existing interfaces should receive error |
| 655 | // notifications at some point. |
| 656 | SimulateNetworkServiceCrash(); |
| 657 | // Flush the interface to make sure the error notification was received. |
| 658 | partition->FlushNetworkInterfaceForTesting(); |
| 659 | // Flush the interface to make sure the frame host has received error |
| 660 | // notification and the new URLLoaderFactoryBundle has been received by the |
| 661 | // frame. |
| 662 | main_frame()->FlushNetworkAndNavigationInterfacesForTesting(); |
| 663 | |
| 664 | EXPECT_TRUE(CheckCanLoadHttpInWindowOpen("/title2.html")); |
| 665 | EXPECT_EQ(last_request_relative_url(), "/title2.html"); |
| 666 | } |
| 667 | |
Hiroki Nakagawa | 3b90e64 | 2019-09-10 01:59:58 | [diff] [blame] | 668 | // Run tests with PlzDedicatedWorker. |
| 669 | // TODO(https://crbug.com/906991): Merge this test fixture into |
| 670 | // NetworkServiceRestartBrowserTest once PlzDedicatedWorker is enabled by |
| 671 | // default. |
| 672 | class NetworkServiceRestartForWorkerBrowserTest |
| 673 | : public NetworkServiceRestartBrowserTest, |
| 674 | public ::testing::WithParamInterface<bool> { |
| 675 | public: |
| 676 | NetworkServiceRestartForWorkerBrowserTest() { |
| 677 | if (GetParam()) { |
| 678 | scoped_feature_list_.InitAndEnableFeature( |
| 679 | blink::features::kPlzDedicatedWorker); |
| 680 | } else { |
| 681 | scoped_feature_list_.InitAndDisableFeature( |
| 682 | blink::features::kPlzDedicatedWorker); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | private: |
| 687 | base::test::ScopedFeatureList scoped_feature_list_; |
| 688 | }; |
| 689 | |
| 690 | INSTANTIATE_TEST_SUITE_P(, |
| 691 | NetworkServiceRestartForWorkerBrowserTest, |
| 692 | ::testing::Values(false, true)); |
| 693 | |
Chong Zhang | 4513fab | 2018-02-28 18:50:18 | [diff] [blame] | 694 | // Make sure worker fetch works after crash. |
Hiroki Nakagawa | 3b90e64 | 2019-09-10 01:59:58 | [diff] [blame] | 695 | IN_PROC_BROWSER_TEST_P(NetworkServiceRestartForWorkerBrowserTest, WorkerFetch) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 696 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 697 | return; |
Chong Zhang | 4513fab | 2018-02-28 18:50:18 | [diff] [blame] | 698 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 699 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 700 | |
| 701 | EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL("/echo"))); |
| 702 | EXPECT_TRUE(CheckCanWorkerFetch("worker1", "/title1.html")); |
| 703 | EXPECT_EQ(last_request_relative_url(), "/title1.html"); |
| 704 | |
| 705 | // Crash the NetworkService process. Existing interfaces should receive error |
| 706 | // notifications at some point. |
| 707 | SimulateNetworkServiceCrash(); |
| 708 | // Flush the interface to make sure the error notification was received. |
| 709 | partition->FlushNetworkInterfaceForTesting(); |
| 710 | // Flush the interface to make sure the frame host has received error |
| 711 | // notification and the new URLLoaderFactoryBundle has been received by the |
| 712 | // frame. |
| 713 | main_frame()->FlushNetworkAndNavigationInterfacesForTesting(); |
| 714 | |
| 715 | EXPECT_TRUE(CheckCanWorkerFetch("worker1", "/title2.html")); |
| 716 | EXPECT_EQ(last_request_relative_url(), "/title2.html"); |
| 717 | } |
| 718 | |
| 719 | // Make sure multiple workers are tracked correctly and work after crash. |
Hiroki Nakagawa | 3b90e64 | 2019-09-10 01:59:58 | [diff] [blame] | 720 | IN_PROC_BROWSER_TEST_P(NetworkServiceRestartForWorkerBrowserTest, |
| 721 | MultipleWorkerFetch) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 722 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 723 | return; |
Chong Zhang | 4513fab | 2018-02-28 18:50:18 | [diff] [blame] | 724 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 725 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 726 | |
| 727 | EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL("/echo"))); |
| 728 | EXPECT_TRUE(CheckCanWorkerFetch("worker1", "/title1.html")); |
| 729 | EXPECT_TRUE(CheckCanWorkerFetch("worker2", "/title1.html")); |
| 730 | EXPECT_EQ(last_request_relative_url(), "/title1.html"); |
| 731 | |
| 732 | // Crash the NetworkService process. Existing interfaces should receive error |
| 733 | // notifications at some point. |
| 734 | SimulateNetworkServiceCrash(); |
| 735 | // Flush the interface to make sure the error notification was received. |
| 736 | partition->FlushNetworkInterfaceForTesting(); |
| 737 | // Flush the interface to make sure the frame host has received error |
| 738 | // notification and the new URLLoaderFactoryBundle has been received by the |
| 739 | // frame. |
| 740 | main_frame()->FlushNetworkAndNavigationInterfacesForTesting(); |
| 741 | |
| 742 | // Both workers should work after crash. |
| 743 | EXPECT_TRUE(CheckCanWorkerFetch("worker1", "/title2.html")); |
| 744 | EXPECT_TRUE(CheckCanWorkerFetch("worker2", "/title2.html")); |
| 745 | EXPECT_EQ(last_request_relative_url(), "/title2.html"); |
| 746 | |
| 747 | // Terminate "worker1". "worker2" shouldn't be affected. |
| 748 | EXPECT_TRUE(TerminateWorker("worker1")); |
| 749 | EXPECT_TRUE(CheckCanWorkerFetch("worker2", "/title1.html")); |
| 750 | EXPECT_EQ(last_request_relative_url(), "/title1.html"); |
| 751 | |
| 752 | // Crash the NetworkService process again. "worker2" should still work. |
| 753 | SimulateNetworkServiceCrash(); |
| 754 | partition->FlushNetworkInterfaceForTesting(); |
| 755 | main_frame()->FlushNetworkAndNavigationInterfacesForTesting(); |
| 756 | |
| 757 | EXPECT_TRUE(CheckCanWorkerFetch("worker2", "/title2.html")); |
| 758 | EXPECT_EQ(last_request_relative_url(), "/title2.html"); |
| 759 | } |
| 760 | |
Makoto Shimazu | 775101a5 | 2018-09-26 04:46:52 | [diff] [blame] | 761 | // Make sure fetch from a page controlled by a service worker which doesn't have |
| 762 | // a fetch handler works after crash. |
Makoto Shimazu | 2540a64 | 2018-11-15 06:24:44 | [diff] [blame] | 763 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 764 | FetchFromServiceWorkerControlledPage_NoFetchHandler) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 765 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 766 | return; |
Makoto Shimazu | 775101a5 | 2018-09-26 04:46:52 | [diff] [blame] | 767 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 768 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 769 | ServiceWorkerStatusObserver observer; |
| 770 | ServiceWorkerContextWrapper* service_worker_context = |
| 771 | partition->GetServiceWorkerContext(); |
| 772 | service_worker_context->AddObserver(&observer); |
| 773 | |
| 774 | // Register a service worker which controls /service_worker/. |
| 775 | EXPECT_TRUE(NavigateToURL(shell(), |
| 776 | embedded_test_server()->GetURL( |
| 777 | "/service_worker/create_service_worker.html"))); |
| 778 | EXPECT_EQ("DONE", EvalJs(shell(), "register('empty.js')")); |
| 779 | |
| 780 | // Navigate to a controlled page. |
| 781 | EXPECT_TRUE(NavigateToURL( |
| 782 | shell(), |
| 783 | embedded_test_server()->GetURL("/service_worker/fetch_from_page.html"))); |
| 784 | |
| 785 | // Fetch from the controlled page. |
| 786 | const std::string script = "fetch_from_page('/echo');"; |
| 787 | EXPECT_EQ("Echo", EvalJs(shell(), script)); |
| 788 | |
| 789 | // Crash the NetworkService process. Existing interfaces should receive error |
| 790 | // notifications at some point. |
| 791 | SimulateNetworkServiceCrash(); |
| 792 | // Flush the interface to make sure the error notification was received. |
| 793 | partition->FlushNetworkInterfaceForTesting(); |
| 794 | |
| 795 | // Service worker should be stopped when network service crashes. |
Patrick Monette | 9f3aae6 | 2019-10-08 00:16:03 | [diff] [blame] | 796 | observer.WaitForStopped(); |
Makoto Shimazu | 775101a5 | 2018-09-26 04:46:52 | [diff] [blame] | 797 | |
| 798 | // Fetch from the controlled page again. |
| 799 | EXPECT_EQ("Echo", EvalJs(shell(), script)); |
| 800 | |
| 801 | service_worker_context->RemoveObserver(&observer); |
| 802 | } |
| 803 | |
| 804 | // Make sure fetch from a page controlled by a service worker which has a fetch |
| 805 | // handler but falls back to the network works after crash. |
| 806 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
Makoto Shimazu | 2540a64 | 2018-11-15 06:24:44 | [diff] [blame] | 807 | FetchFromServiceWorkerControlledPage_PassThrough) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 808 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 809 | return; |
Makoto Shimazu | 775101a5 | 2018-09-26 04:46:52 | [diff] [blame] | 810 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 811 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 812 | ServiceWorkerStatusObserver observer; |
| 813 | ServiceWorkerContextWrapper* service_worker_context = |
| 814 | partition->GetServiceWorkerContext(); |
| 815 | service_worker_context->AddObserver(&observer); |
| 816 | |
| 817 | // Register a service worker which controls /service_worker/. |
| 818 | EXPECT_TRUE(NavigateToURL(shell(), |
| 819 | embedded_test_server()->GetURL( |
| 820 | "/service_worker/create_service_worker.html"))); |
| 821 | EXPECT_EQ("DONE", EvalJs(shell(), "register('fetch_event_pass_through.js')")); |
| 822 | |
| 823 | // Navigate to a controlled page. |
| 824 | EXPECT_TRUE(NavigateToURL( |
| 825 | shell(), |
| 826 | embedded_test_server()->GetURL("/service_worker/fetch_from_page.html"))); |
| 827 | |
| 828 | // Fetch from the controlled page. |
| 829 | const std::string script = "fetch_from_page('/echo');"; |
| 830 | EXPECT_EQ("Echo", EvalJs(shell(), script)); |
| 831 | |
| 832 | // Crash the NetworkService process. Existing interfaces should receive error |
| 833 | // notifications at some point. |
| 834 | SimulateNetworkServiceCrash(); |
| 835 | // Flush the interface to make sure the error notification was received. |
| 836 | partition->FlushNetworkInterfaceForTesting(); |
| 837 | |
| 838 | // Service worker should be stopped when network service crashes. |
Patrick Monette | 9f3aae6 | 2019-10-08 00:16:03 | [diff] [blame] | 839 | observer.WaitForStopped(); |
Makoto Shimazu | 775101a5 | 2018-09-26 04:46:52 | [diff] [blame] | 840 | |
| 841 | // Fetch from the controlled page again. |
| 842 | EXPECT_EQ("Echo", EvalJs(shell(), script)); |
| 843 | |
| 844 | service_worker_context->RemoveObserver(&observer); |
| 845 | } |
| 846 | |
| 847 | // Make sure fetch from a page controlled by a service worker which has a fetch |
| 848 | // handler and responds with fetch() works after crash. |
Makoto Shimazu | 2540a64 | 2018-11-15 06:24:44 | [diff] [blame] | 849 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 850 | FetchFromServiceWorkerControlledPage_RespondWithFetch) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 851 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 852 | return; |
Makoto Shimazu | 775101a5 | 2018-09-26 04:46:52 | [diff] [blame] | 853 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 854 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 855 | ServiceWorkerStatusObserver observer; |
| 856 | ServiceWorkerContextWrapper* service_worker_context = |
| 857 | partition->GetServiceWorkerContext(); |
| 858 | service_worker_context->AddObserver(&observer); |
| 859 | |
| 860 | // Register a service worker which controls /service_worker/. |
| 861 | EXPECT_TRUE(NavigateToURL(shell(), |
| 862 | embedded_test_server()->GetURL( |
| 863 | "/service_worker/create_service_worker.html"))); |
| 864 | EXPECT_EQ("DONE", |
| 865 | EvalJs(shell(), "register('fetch_event_respond_with_fetch.js')")); |
| 866 | |
| 867 | // Navigate to a controlled page. |
| 868 | EXPECT_TRUE(NavigateToURL( |
| 869 | shell(), |
| 870 | embedded_test_server()->GetURL("/service_worker/fetch_from_page.html"))); |
| 871 | |
| 872 | // Fetch from the controlled page. |
| 873 | const std::string script = "fetch_from_page('/echo');"; |
| 874 | EXPECT_EQ("Echo", EvalJs(shell(), script)); |
| 875 | |
| 876 | // Crash the NetworkService process. Existing interfaces should receive error |
| 877 | // notifications at some point. |
| 878 | SimulateNetworkServiceCrash(); |
| 879 | // Flush the interface to make sure the error notification was received. |
| 880 | partition->FlushNetworkInterfaceForTesting(); |
| 881 | |
| 882 | // Service worker should be stopped when network service crashes. |
Patrick Monette | 9f3aae6 | 2019-10-08 00:16:03 | [diff] [blame] | 883 | observer.WaitForStopped(); |
Makoto Shimazu | 775101a5 | 2018-09-26 04:46:52 | [diff] [blame] | 884 | |
| 885 | // Fetch from the controlled page again. |
| 886 | EXPECT_EQ("Echo", EvalJs(shell(), script)); |
| 887 | |
| 888 | service_worker_context->RemoveObserver(&observer); |
| 889 | } |
| 890 | |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 891 | // Make sure fetch from service worker context works after crash. |
Makoto Shimazu | 6a96cb6 | 2018-09-21 11:39:13 | [diff] [blame] | 892 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, ServiceWorkerFetch) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 893 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 894 | return; |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 895 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 896 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 897 | ServiceWorkerStatusObserver observer; |
| 898 | ServiceWorkerContextWrapper* service_worker_context = |
| 899 | partition->GetServiceWorkerContext(); |
| 900 | service_worker_context->AddObserver(&observer); |
| 901 | |
| 902 | const GURL page_url = embedded_test_server()->GetURL( |
| 903 | "/service_worker/fetch_from_service_worker.html"); |
| 904 | const GURL fetch_url = embedded_test_server()->GetURL("/echo"); |
| 905 | |
| 906 | // Navigate to the page and register a service worker. |
| 907 | EXPECT_TRUE(NavigateToURL(shell(), page_url)); |
| 908 | EXPECT_EQ("ready", EvalJs(shell(), "setup();")); |
| 909 | |
| 910 | // Fetch from the service worker. |
| 911 | const std::string script = |
| 912 | "fetch_from_service_worker('" + fetch_url.spec() + "');"; |
| 913 | EXPECT_EQ("Echo", EvalJs(shell(), script)); |
| 914 | |
| 915 | // Crash the NetworkService process. Existing interfaces should receive error |
| 916 | // notifications at some point. |
| 917 | SimulateNetworkServiceCrash(); |
| 918 | // Flush the interface to make sure the error notification was received. |
| 919 | partition->FlushNetworkInterfaceForTesting(); |
| 920 | |
| 921 | // Service worker should be stopped when network service crashes. |
Patrick Monette | 9f3aae6 | 2019-10-08 00:16:03 | [diff] [blame] | 922 | observer.WaitForStopped(); |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 923 | |
| 924 | // Fetch from the service worker again. |
| 925 | EXPECT_EQ("Echo", EvalJs(shell(), script)); |
| 926 | |
| 927 | service_worker_context->RemoveObserver(&observer); |
| 928 | } |
| 929 | |
Clark DuVall | f742bcbd | 2018-11-07 17:16:20 | [diff] [blame] | 930 | // TODO(crbug.com/154571): Shared workers are not available on Android. |
| 931 | #if defined(OS_ANDROID) |
| 932 | #define MAYBE_SharedWorker DISABLED_SharedWorker |
| 933 | #else |
| 934 | #define MAYBE_SharedWorker SharedWorker |
| 935 | #endif |
Matt Falkenhagen | e672196 | 2018-10-05 10:17:05 | [diff] [blame] | 936 | // Make sure shared workers terminate after crash. |
Clark DuVall | f742bcbd | 2018-11-07 17:16:20 | [diff] [blame] | 937 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, MAYBE_SharedWorker) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 938 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 939 | return; |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 940 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 941 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 942 | |
Patrick Monette | 3f51a04 | 2019-08-30 17:57:04 | [diff] [blame] | 943 | InjectTestSharedWorkerService(partition); |
| 944 | |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 945 | const GURL page_url = |
| 946 | embedded_test_server()->GetURL("/workers/fetch_from_shared_worker.html"); |
| 947 | const GURL fetch_url = embedded_test_server()->GetURL("/echo"); |
| 948 | |
| 949 | // Navigate to the page and prepare a shared worker. |
| 950 | EXPECT_TRUE(NavigateToURL(shell(), page_url)); |
| 951 | |
Matt Falkenhagen | e672196 | 2018-10-05 10:17:05 | [diff] [blame] | 952 | // Fetch from the shared worker to ensure it has started. |
Findit | c0f7c620 | 2018-10-05 07:14:23 | [diff] [blame] | 953 | const std::string script = |
| 954 | "fetch_from_shared_worker('" + fetch_url.spec() + "');"; |
| 955 | EXPECT_EQ("Echo", EvalJs(shell(), script)); |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 956 | |
Matt Falkenhagen | e672196 | 2018-10-05 10:17:05 | [diff] [blame] | 957 | // There should be one worker host. We will later wait for it to terminate. |
Patrick Monette | 3f51a04 | 2019-08-30 17:57:04 | [diff] [blame] | 958 | TestSharedWorkerServiceImpl* service = |
| 959 | static_cast<TestSharedWorkerServiceImpl*>( |
| 960 | partition->GetSharedWorkerService()); |
Matt Falkenhagen | e672196 | 2018-10-05 10:17:05 | [diff] [blame] | 961 | EXPECT_EQ(1u, service->worker_hosts_.size()); |
| 962 | base::RunLoop loop; |
Patrick Monette | 3f51a04 | 2019-08-30 17:57:04 | [diff] [blame] | 963 | service->SetWorkerTerminationCallback(loop.QuitClosure()); |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 964 | |
Matt Falkenhagen | e672196 | 2018-10-05 10:17:05 | [diff] [blame] | 965 | // Crash the NetworkService process. |
| 966 | SimulateNetworkServiceCrash(); |
| 967 | |
| 968 | // Wait for the worker to detect the crash and self-terminate. |
| 969 | loop.Run(); |
| 970 | EXPECT_TRUE(service->worker_hosts_.empty()); |
Makoto Shimazu | 2075b0d | 2018-09-21 09:04:22 | [diff] [blame] | 971 | } |
| 972 | |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 973 | // Make sure the entry in |NetworkService::GetTotalNetworkUsages()| was cleared |
| 974 | // after process closed. |
| 975 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 976 | GetNetworkUsagesClosed) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 977 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 978 | return; |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 979 | EXPECT_TRUE(NavigateToURL(shell(), GetTestURL())); |
| 980 | Shell* shell2 = CreateBrowser(); |
| 981 | EXPECT_TRUE(NavigateToURL(shell2, GetTestURL())); |
| 982 | |
| 983 | int process_id1 = |
| 984 | shell()->web_contents()->GetMainFrame()->GetProcess()->GetID(); |
| 985 | int process_id2 = |
| 986 | shell2->web_contents()->GetMainFrame()->GetProcess()->GetID(); |
| 987 | |
| 988 | // Load resource on the renderer to make sure the traffic was recorded. |
| 989 | EXPECT_TRUE(CheckCanLoadHttp(shell(), "/title2.html")); |
| 990 | EXPECT_TRUE(CheckCanLoadHttp(shell2, "/title3.html")); |
| 991 | |
| 992 | // Both processes should have traffic recorded. |
| 993 | auto network_usages = GetTotalNetworkUsages(); |
| 994 | EXPECT_TRUE(CheckContainsProcessID(network_usages, process_id1)); |
| 995 | EXPECT_TRUE(CheckContainsProcessID(network_usages, process_id2)); |
| 996 | |
| 997 | // Closing |shell2| should cause the entry to be cleared. |
| 998 | shell2->Close(); |
| 999 | shell2 = nullptr; |
| 1000 | |
| 1001 | // Wait until the Network Service has noticed the change. We don't have a |
| 1002 | // better way to force a flush on the Network Service side. |
| 1003 | WaitForCondition(base::BindRepeating( |
| 1004 | [](int process_id) { |
| 1005 | auto usages = GetTotalNetworkUsages(); |
| 1006 | return !CheckContainsProcessID(usages, process_id); |
| 1007 | }, |
| 1008 | process_id2)); |
| 1009 | |
| 1010 | network_usages = GetTotalNetworkUsages(); |
| 1011 | EXPECT_TRUE(CheckContainsProcessID(network_usages, process_id1)); |
| 1012 | EXPECT_FALSE(CheckContainsProcessID(network_usages, process_id2)); |
| 1013 | } |
| 1014 | |
Christopher Thompson | 2a4c69e9 | 2019-08-21 16:29:11 | [diff] [blame] | 1015 | // Make sure that kSSLKeyLogFileHistogram is correctly recorded when the |
| 1016 | // network service instance is started and the SSLKEYLOGFILE env var is set or |
| 1017 | // the "--ssl-key-log-file" arg is set. |
| 1018 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, SSLKeyLogFileMetrics) { |
| 1019 | if (IsInProcessNetworkService()) |
| 1020 | return; |
| 1021 | // Actions on temporary files are blocking. |
| 1022 | base::ScopedAllowBlockingForTesting scoped_allow_blocking; |
| 1023 | base::FilePath log_file_path; |
| 1024 | base::CreateTemporaryFile(&log_file_path); |
| 1025 | |
| 1026 | #if defined(OS_WIN) |
| 1027 | // On Windows, FilePath::value() returns base::string16, so convert. |
| 1028 | std::string log_file_path_str = base::UTF16ToUTF8(log_file_path.value()); |
| 1029 | #else |
| 1030 | std::string log_file_path_str = log_file_path.value(); |
| 1031 | #endif |
| 1032 | |
| 1033 | // Test that env var causes the histogram to be recorded. |
| 1034 | { |
| 1035 | base::test::ScopedEnvironmentVariableOverride scoped_env("SSLKEYLOGFILE", |
| 1036 | log_file_path_str); |
| 1037 | base::HistogramTester histograms; |
| 1038 | // Restart network service to cause SSLKeyLogger to be re-initialized. |
| 1039 | SimulateNetworkServiceCrash(); |
| 1040 | histograms.ExpectBucketCount(kSSLKeyLogFileHistogram, |
| 1041 | SSLKeyLogFileAction::kLogFileEnabled, 1); |
| 1042 | histograms.ExpectBucketCount(kSSLKeyLogFileHistogram, |
| 1043 | SSLKeyLogFileAction::kEnvVarFound, 1); |
| 1044 | } |
| 1045 | |
| 1046 | // Test that the command-line switch causes the histogram to be recorded. |
| 1047 | { |
| 1048 | base::test::ScopedCommandLine scoped_command_line; |
| 1049 | scoped_command_line.GetProcessCommandLine()->AppendSwitchPath( |
| 1050 | "ssl-key-log-file", log_file_path); |
| 1051 | base::HistogramTester histograms; |
| 1052 | // Restart network service to cause SSLKeyLogger to be re-initialized. |
| 1053 | SimulateNetworkServiceCrash(); |
| 1054 | histograms.ExpectBucketCount(kSSLKeyLogFileHistogram, |
| 1055 | SSLKeyLogFileAction::kLogFileEnabled, 1); |
| 1056 | histograms.ExpectBucketCount(kSSLKeyLogFileHistogram, |
| 1057 | SSLKeyLogFileAction::kSwitchFound, 1); |
| 1058 | } |
| 1059 | } |
| 1060 | |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 1061 | // Make sure |NetworkService::GetTotalNetworkUsages()| continues to work after |
| 1062 | // crash. See 'network_usage_accumulator_unittest' for quantified tests. |
| 1063 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 1064 | GetNetworkUsagesCrashed) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 1065 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 1066 | return; |
Chong Zhang | 5304776 | 2018-05-02 21:24:08 | [diff] [blame] | 1067 | EXPECT_TRUE(NavigateToURL(shell(), GetTestURL())); |
| 1068 | Shell* shell2 = CreateBrowser(); |
| 1069 | EXPECT_TRUE(NavigateToURL(shell2, GetTestURL())); |
| 1070 | |
| 1071 | int process_id1 = |
| 1072 | shell()->web_contents()->GetMainFrame()->GetProcess()->GetID(); |
| 1073 | int process_id2 = |
| 1074 | shell2->web_contents()->GetMainFrame()->GetProcess()->GetID(); |
| 1075 | |
| 1076 | // Load resource on the renderer to make sure the traffic was recorded. |
| 1077 | EXPECT_TRUE(CheckCanLoadHttp(shell(), "/title2.html")); |
| 1078 | EXPECT_TRUE(CheckCanLoadHttp(shell2, "/title3.html")); |
| 1079 | |
| 1080 | // Both processes should have traffic recorded. |
| 1081 | auto network_usages = GetTotalNetworkUsages(); |
| 1082 | EXPECT_TRUE(CheckContainsProcessID(network_usages, process_id1)); |
| 1083 | EXPECT_TRUE(CheckContainsProcessID(network_usages, process_id2)); |
| 1084 | |
| 1085 | // Crashing Network Service should cause all entries to be cleared. |
| 1086 | SimulateNetworkServiceCrash(); |
| 1087 | network_usages = GetTotalNetworkUsages(); |
| 1088 | EXPECT_FALSE(CheckContainsProcessID(network_usages, process_id1)); |
| 1089 | EXPECT_FALSE(CheckContainsProcessID(network_usages, process_id2)); |
| 1090 | |
| 1091 | // Should still be able to recored new traffic after crash. |
| 1092 | EXPECT_TRUE(CheckCanLoadHttp(shell(), "/title2.html")); |
| 1093 | network_usages = GetTotalNetworkUsages(); |
| 1094 | EXPECT_TRUE(CheckContainsProcessID(network_usages, process_id1)); |
| 1095 | EXPECT_FALSE(CheckContainsProcessID(network_usages, process_id2)); |
| 1096 | } |
| 1097 | |
John Abd-El-Malek | 1f61a5fd | 2018-09-21 01:13:14 | [diff] [blame] | 1098 | // Make sure cookie access doesn't hang or fail after a network process crash. |
| 1099 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, Cookies) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 1100 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 1101 | return; |
John Abd-El-Malek | 1f61a5fd | 2018-09-21 01:13:14 | [diff] [blame] | 1102 | auto* web_contents = shell()->web_contents(); |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 1103 | ASSERT_TRUE( |
| 1104 | NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"))); |
John Abd-El-Malek | 1f61a5fd | 2018-09-21 01:13:14 | [diff] [blame] | 1105 | EXPECT_TRUE(ExecuteScript(web_contents, "document.cookie = 'foo=bar';")); |
| 1106 | |
| 1107 | std::string cookie; |
| 1108 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 1109 | web_contents, "window.domAutomationController.send(document.cookie);", |
| 1110 | &cookie)); |
| 1111 | EXPECT_EQ("foo=bar", cookie); |
| 1112 | |
| 1113 | SimulateNetworkServiceCrash(); |
| 1114 | |
John Abd-El-Malek | 1f61a5fd | 2018-09-21 01:13:14 | [diff] [blame] | 1115 | // content_shell uses in-memory cookie database, so the value saved earlier |
| 1116 | // won't persist across crashes. What matters is that new access works. |
| 1117 | EXPECT_TRUE(ExecuteScript(web_contents, "document.cookie = 'foo=bar';")); |
| 1118 | |
| 1119 | // This will hang without the fix. |
| 1120 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 1121 | web_contents, "window.domAutomationController.send(document.cookie);", |
| 1122 | &cookie)); |
| 1123 | EXPECT_EQ("foo=bar", cookie); |
| 1124 | } |
| 1125 | |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 1126 | #if BUILDFLAG(ENABLE_PLUGINS) |
| 1127 | // Make sure that "trusted" plugins continue to be able to issue cross-origin |
| 1128 | // requests after a network process crash. |
| 1129 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, Plugin) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 1130 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 1131 | return; |
Lukasz Anforowicz | ce4487b7 | 2018-10-23 18:05:53 | [diff] [blame] | 1132 | auto* web_contents = shell()->web_contents(); |
| 1133 | ASSERT_TRUE(NavigateToURL(web_contents, |
| 1134 | embedded_test_server()->GetURL("/title1.html"))); |
| 1135 | |
| 1136 | // Load the test plugin (see ppapi::RegisterFlashTestPlugin and |
| 1137 | // ppapi/tests/power_saver_test_plugin.cc). |
| 1138 | const char kLoadingScript[] = R"( |
| 1139 | var obj = document.createElement('object'); |
| 1140 | obj.id = 'plugin'; |
| 1141 | obj.data = 'test.swf'; |
| 1142 | obj.type = 'application/x-shockwave-flash'; |
| 1143 | obj.width = 400; |
| 1144 | obj.height = 400; |
| 1145 | |
| 1146 | document.body.appendChild(obj); |
| 1147 | )"; |
| 1148 | ASSERT_TRUE(ExecJs(web_contents, kLoadingScript)); |
| 1149 | |
| 1150 | // Ask the plugin to perform a cross-origin, CORB-eligible (i.e. |
| 1151 | // application/json + nosniff) URL request. Plugins with universal access |
| 1152 | // should not be subject to CORS/CORB and so the request should go through. |
| 1153 | // See also https://crbug.com/874515 and https://crbug.com/846339. |
| 1154 | GURL cross_origin_url = embedded_test_server()->GetURL( |
| 1155 | "cross.origin.com", "/site_isolation/nosniff.json"); |
| 1156 | const char kFetchScriptTemplate[] = R"( |
| 1157 | new Promise(function (resolve, reject) { |
| 1158 | var obj = document.getElementById('plugin'); |
| 1159 | function callback(event) { |
| 1160 | // Ignore plugin messages unrelated to requestUrl. |
| 1161 | if (!event.data.startsWith('requestUrl: ')) |
| 1162 | return; |
| 1163 | |
| 1164 | obj.removeEventListener('message', callback); |
| 1165 | resolve('msg-from-plugin: ' + event.data); |
| 1166 | }; |
| 1167 | obj.addEventListener('message', callback); |
| 1168 | obj.postMessage('requestUrl: ' + $1); |
| 1169 | }); |
| 1170 | )"; |
| 1171 | std::string fetch_script = JsReplace(kFetchScriptTemplate, cross_origin_url); |
| 1172 | ASSERT_EQ( |
| 1173 | "msg-from-plugin: requestUrl: RESPONSE BODY: " |
| 1174 | "runMe({ \"name\" : \"chromium\" });\n", |
| 1175 | EvalJs(web_contents, fetch_script)); |
| 1176 | |
| 1177 | // Crash the Network Service process and wait until host frame's |
| 1178 | // URLLoaderFactory has been refreshed. |
| 1179 | SimulateNetworkServiceCrash(); |
| 1180 | main_frame()->FlushNetworkAndNavigationInterfacesForTesting(); |
| 1181 | |
| 1182 | // Try the fetch again - it should still work (i.e. the mechanism for relaxing |
| 1183 | // CORB for universal-access-plugins should be resilient to network process |
| 1184 | // crashes). See also https://crbug.com/891904. |
| 1185 | ASSERT_EQ( |
| 1186 | "msg-from-plugin: requestUrl: RESPONSE BODY: " |
| 1187 | "runMe({ \"name\" : \"chromium\" });\n", |
| 1188 | EvalJs(web_contents, fetch_script)); |
| 1189 | } |
| 1190 | #endif |
| 1191 | |
Clark DuVall | f742bcbd | 2018-11-07 17:16:20 | [diff] [blame] | 1192 | // TODO(crbug.com/901026): Fix deadlock on process startup on Android. |
| 1193 | #if defined(OS_ANDROID) |
| 1194 | #define MAYBE_SyncCallDuringRestart DISABLED_SyncCallDuringRestart |
| 1195 | #else |
| 1196 | #define MAYBE_SyncCallDuringRestart SyncCallDuringRestart |
| 1197 | #endif |
| 1198 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 1199 | MAYBE_SyncCallDuringRestart) { |
Clark DuVall | 3290462e | 2019-02-16 02:19:50 | [diff] [blame] | 1200 | if (IsInProcessNetworkService()) |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 1201 | return; |
Clark DuVall | f742bcbd | 2018-11-07 17:16:20 | [diff] [blame] | 1202 | base::RunLoop run_loop; |
Ken Rockot | 91216c0f | 2019-10-11 20:38:11 | [diff] [blame] | 1203 | mojo::Remote<network::mojom::NetworkServiceTest> network_service_test; |
| 1204 | content::GetNetworkService()->BindTestInterface( |
| 1205 | network_service_test.BindNewPipeAndPassReceiver()); |
Clark DuVall | f742bcbd | 2018-11-07 17:16:20 | [diff] [blame] | 1206 | |
| 1207 | // Crash the network service, but do not wait for full startup. |
Ken Rockot | 91216c0f | 2019-10-11 20:38:11 | [diff] [blame] | 1208 | network_service_test.set_disconnect_handler(run_loop.QuitClosure()); |
Clark DuVall | f742bcbd | 2018-11-07 17:16:20 | [diff] [blame] | 1209 | network_service_test->SimulateCrash(); |
| 1210 | run_loop.Run(); |
| 1211 | |
Ken Rockot | 91216c0f | 2019-10-11 20:38:11 | [diff] [blame] | 1212 | network_service_test.reset(); |
| 1213 | content::GetNetworkService()->BindTestInterface( |
| 1214 | network_service_test.BindNewPipeAndPassReceiver()); |
Clark DuVall | f742bcbd | 2018-11-07 17:16:20 | [diff] [blame] | 1215 | |
| 1216 | // Sync call should be fine, even though network process is still starting up. |
| 1217 | mojo::ScopedAllowSyncCallForTesting allow_sync_call; |
| 1218 | network_service_test->AddRules({}); |
| 1219 | } |
| 1220 | |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 1221 | } // namespace content |