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 | |
| 5 | #include "base/test/scoped_feature_list.h" |
| 6 | #include "build/build_config.h" |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 7 | #include "content/browser/storage_partition_impl.h" |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 8 | #include "content/browser/url_loader_factory_getter.h" |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 9 | #include "content/public/browser/browser_context.h" |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 10 | #include "content/public/browser/network_service_instance.h" |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 11 | #include "content/public/browser/web_contents.h" |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 12 | #include "content/public/common/content_features.h" |
| 13 | #include "content/public/common/network_service.mojom.h" |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 14 | #include "content/public/common/simple_url_loader.h" |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 15 | #include "content/public/test/browser_test_utils.h" |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 16 | #include "content/public/test/content_browser_test.h" |
| 17 | #include "content/public/test/content_browser_test_utils.h" |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 18 | #include "content/public/test/simple_url_loader_test_helper.h" |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 19 | #include "content/shell/browser/shell.h" |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 20 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 21 | |
| 22 | namespace content { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | mojom::NetworkContextPtr CreateNetworkContext() { |
| 27 | mojom::NetworkContextPtr network_context; |
| 28 | mojom::NetworkContextParamsPtr context_params = |
| 29 | mojom::NetworkContextParams::New(); |
| 30 | GetNetworkService()->CreateNetworkContext(mojo::MakeRequest(&network_context), |
| 31 | std::move(context_params)); |
| 32 | return network_context; |
| 33 | } |
| 34 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 35 | int LoadBasicRequestOnIOThread( |
| 36 | URLLoaderFactoryGetter* url_loader_factory_getter, |
| 37 | const GURL& url) { |
| 38 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
John Abd-El-Malek | 1df6179 | 2018-01-12 20:40:45 | [diff] [blame^] | 39 | auto request = std::make_unique<network::ResourceRequest>(); |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 40 | request->url = url; |
| 41 | |
| 42 | SimpleURLLoaderTestHelper simple_loader_helper; |
| 43 | // Wait for callback on UI thread to avoid nesting IO message loops. |
| 44 | simple_loader_helper.SetRunLoopQuitThread(BrowserThread::UI); |
| 45 | |
| 46 | std::unique_ptr<content::SimpleURLLoader> simple_loader = |
| 47 | content::SimpleURLLoader::Create(std::move(request), |
| 48 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 49 | |
| 50 | // |URLLoaderFactoryGetter::GetNetworkFactory()| can only be accessed on IO |
| 51 | // thread. |
| 52 | BrowserThread::PostTask( |
| 53 | BrowserThread::IO, FROM_HERE, |
| 54 | base::BindOnce( |
| 55 | [](content::SimpleURLLoader* loader, |
| 56 | URLLoaderFactoryGetter* factory_getter, |
| 57 | SimpleURLLoader::BodyAsStringCallback body_as_string_callback) { |
| 58 | loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie( |
| 59 | factory_getter->GetNetworkFactory(), |
| 60 | std::move(body_as_string_callback)); |
| 61 | }, |
| 62 | base::Unretained(simple_loader.get()), |
| 63 | base::Unretained(url_loader_factory_getter), |
| 64 | simple_loader_helper.GetCallback())); |
| 65 | |
| 66 | simple_loader_helper.WaitForCallback(); |
| 67 | return simple_loader->NetError(); |
| 68 | } |
| 69 | |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 70 | } // namespace |
| 71 | |
| 72 | // This test source has been excluded from Android as Android doesn't have |
| 73 | // out-of-process Network Service. |
| 74 | class NetworkServiceRestartBrowserTest : public ContentBrowserTest { |
| 75 | public: |
| 76 | NetworkServiceRestartBrowserTest() { |
| 77 | scoped_feature_list_.InitAndEnableFeature(features::kNetworkService); |
| 78 | EXPECT_TRUE(embedded_test_server()->Start()); |
| 79 | } |
| 80 | |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 81 | GURL GetTestURL() const { |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 82 | // Use '/echoheader' instead of '/echo' to avoid a disk_cache bug. |
| 83 | // See https://crbug.com/792255. |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 84 | return embedded_test_server()->GetURL("/echoheader"); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 85 | } |
| 86 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 87 | BrowserContext* browser_context() { |
| 88 | return shell()->web_contents()->GetBrowserContext(); |
| 89 | } |
| 90 | |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 91 | private: |
| 92 | base::test::ScopedFeatureList scoped_feature_list_; |
| 93 | |
| 94 | DISALLOW_COPY_AND_ASSIGN(NetworkServiceRestartBrowserTest); |
| 95 | }; |
| 96 | |
| 97 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 98 | NetworkServiceProcessRecovery) { |
| 99 | mojom::NetworkContextPtr network_context = CreateNetworkContext(); |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 100 | EXPECT_EQ(net::OK, LoadBasicRequest(network_context.get(), GetTestURL())); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 101 | EXPECT_TRUE(network_context.is_bound()); |
| 102 | EXPECT_FALSE(network_context.encountered_error()); |
| 103 | |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 104 | // Crash the NetworkService process. Existing interfaces should receive error |
| 105 | // notifications at some point. |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 106 | SimulateNetworkServiceCrash(); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 107 | // |network_context| will receive an error notification, but it's not |
| 108 | // guaranteed to have arrived at this point. Flush the pointer to make sure |
| 109 | // the notification has been received. |
| 110 | network_context.FlushForTesting(); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 111 | EXPECT_TRUE(network_context.is_bound()); |
| 112 | EXPECT_TRUE(network_context.encountered_error()); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 113 | // Make sure we could get |net::ERR_FAILED| with an invalid |network_context|. |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 114 | EXPECT_EQ(net::ERR_FAILED, |
| 115 | LoadBasicRequest(network_context.get(), GetTestURL())); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 116 | |
| 117 | // NetworkService should restart automatically and return valid interface. |
| 118 | mojom::NetworkContextPtr network_context2 = CreateNetworkContext(); |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 119 | EXPECT_EQ(net::OK, LoadBasicRequest(network_context2.get(), GetTestURL())); |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 120 | EXPECT_TRUE(network_context2.is_bound()); |
| 121 | EXPECT_FALSE(network_context2.encountered_error()); |
| 122 | } |
| 123 | |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 124 | // Make sure |StoragePartitionImpl::GetNetworkContext()| returns valid interface |
| 125 | // after crash. |
| 126 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
Chong Zhang | e0dfdb6 | 2017-12-09 18:25:14 | [diff] [blame] | 127 | StoragePartitionImplGetNetworkContext) { |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 128 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 129 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 130 | |
| 131 | mojom::NetworkContext* old_network_context = partition->GetNetworkContext(); |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 132 | EXPECT_EQ(net::OK, LoadBasicRequest(old_network_context, GetTestURL())); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 133 | |
| 134 | // Crash the NetworkService process. Existing interfaces should receive error |
| 135 | // notifications at some point. |
| 136 | SimulateNetworkServiceCrash(); |
| 137 | // Flush the interface to make sure the error notification was received. |
| 138 | partition->FlushNetworkInterfaceForTesting(); |
| 139 | |
| 140 | // |partition->GetNetworkContext()| should return a valid new pointer after |
| 141 | // crash. |
| 142 | EXPECT_NE(old_network_context, partition->GetNetworkContext()); |
Chong Zhang | c40a6ce5 | 2017-12-10 03:00:28 | [diff] [blame] | 143 | EXPECT_EQ(net::OK, |
| 144 | LoadBasicRequest(partition->GetNetworkContext(), GetTestURL())); |
Chong Zhang | 5f46827 | 2017-12-07 23:59:14 | [diff] [blame] | 145 | } |
| 146 | |
Chong Zhang | d4c92364 | 2018-01-03 21:22:29 | [diff] [blame] | 147 | // Make sure |URLLoaderFactoryGetter| returns valid interface after crash. |
| 148 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 149 | URLLoaderFactoryGetterGetNetworkFactory) { |
| 150 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 151 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 152 | scoped_refptr<URLLoaderFactoryGetter> url_loader_factory_getter = |
| 153 | partition->url_loader_factory_getter(); |
| 154 | EXPECT_EQ(net::OK, LoadBasicRequestOnIOThread(url_loader_factory_getter.get(), |
| 155 | GetTestURL())); |
| 156 | // Crash the NetworkService process. Existing interfaces should receive error |
| 157 | // notifications at some point. |
| 158 | SimulateNetworkServiceCrash(); |
| 159 | // Flush the interface to make sure the error notification was received. |
| 160 | partition->FlushNetworkInterfaceForTesting(); |
| 161 | url_loader_factory_getter->FlushNetworkInterfaceOnIOThreadForTesting(); |
| 162 | |
| 163 | // |url_loader_factory_getter| should be able to get a valid new pointer after |
| 164 | // crash. |
| 165 | EXPECT_EQ(net::OK, LoadBasicRequestOnIOThread(url_loader_factory_getter.get(), |
| 166 | GetTestURL())); |
| 167 | } |
| 168 | |
| 169 | // Make sure basic navigation works after crash. |
| 170 | IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, |
| 171 | NavigationURLLoaderBasic) { |
| 172 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 173 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 174 | |
| 175 | EXPECT_TRUE( |
| 176 | NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"))); |
| 177 | |
| 178 | // Crash the NetworkService process. Existing interfaces should receive error |
| 179 | // notifications at some point. |
| 180 | SimulateNetworkServiceCrash(); |
| 181 | // Flush the interface to make sure the error notification was received. |
| 182 | partition->FlushNetworkInterfaceForTesting(); |
| 183 | partition->url_loader_factory_getter() |
| 184 | ->FlushNetworkInterfaceOnIOThreadForTesting(); |
| 185 | |
| 186 | EXPECT_TRUE( |
| 187 | NavigateToURL(shell(), embedded_test_server()->GetURL("/title2.html"))); |
| 188 | } |
| 189 | |
Chong Zhang | c583e67 | 2017-11-08 16:34:08 | [diff] [blame] | 190 | } // namespace content |