[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 1 | // Copyright (c) 2012 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/command_line.h" |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 6 | #include "base/files/file_path.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 7 | #include "base/location.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 8 | #include "base/macros.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
Jay Civelli | 1ba11572 | 2018-07-16 17:41:01 | [diff] [blame] | 11 | #include "base/test/bind_test_util.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | 9ea0cd3 | 2013-07-12 01:50:36 | [diff] [blame] | 13 | #include "chrome/browser/chrome_notification_types.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser.h" |
tfarina | 535e6f5 | 2016-03-31 13:46:49 | [diff] [blame] | 16 | #include "chrome/browser/ui/login/login_handler.h" |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 17 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 18 | #include "chrome/common/chrome_paths.h" |
| 19 | #include "chrome/common/chrome_switches.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 20 | #include "chrome/test/base/in_process_browser_test.h" |
| 21 | #include "chrome/test/base/ui_test_utils.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 23 | #include "content/public/browser/notification_details.h" |
| 24 | #include "content/public/browser/notification_source.h" |
Sergio Villar Senin | c4f55f7 | 2018-07-19 07:49:15 | [diff] [blame] | 25 | #include "content/public/browser/storage_partition.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 26 | #include "content/public/browser/web_contents.h" |
| 27 | #include "content/public/browser/web_contents_observer.h" |
yzshen | a90291c | 2017-04-26 16:22:52 | [diff] [blame] | 28 | #include "content/public/common/content_switches.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 29 | #include "content/public/test/browser_test_utils.h" |
Jay Civelli | 1ba11572 | 2018-07-16 17:41:01 | [diff] [blame] | 30 | #include "content/public/test/url_loader_interceptor.h" |
| 31 | #include "google_apis/gaia/gaia_urls.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 32 | #include "net/base/load_flags.h" |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 33 | #include "net/test/embedded_test_server/embedded_test_server.h" |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame] | 34 | #include "net/test/embedded_test_server/simple_connection_listener.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 35 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 36 | #include "net/test/test_data_directory.h" |
rhalavati | 8aa3fbb7 | 2017-05-22 23:27:18 | [diff] [blame] | 37 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
Sergio Villar Senin | c4f55f7 | 2018-07-19 07:49:15 | [diff] [blame] | 38 | #include "services/network/public/cpp/simple_url_loader.h" |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 39 | #include "url/gurl.h" |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 43 | // PAC script that sends all requests to an invalid proxy server. |
| 44 | const base::FilePath::CharType kPACScript[] = FILE_PATH_LITERAL( |
| 45 | "bad_server.pac"); |
| 46 | |
| 47 | // Verify kPACScript is installed as the PAC script. |
| 48 | void VerifyProxyScript(Browser* browser) { |
| 49 | ui_test_utils::NavigateToURL(browser, GURL("http://google.com")); |
| 50 | |
| 51 | // Verify we get the ERR_PROXY_CONNECTION_FAILED screen. |
| 52 | bool result = false; |
| 53 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 54 | browser->tab_strip_model()->GetActiveWebContents(), |
| 55 | "var textContent = document.body.textContent;" |
| 56 | "var hasError = textContent.indexOf('ERR_PROXY_CONNECTION_FAILED') >= 0;" |
| 57 | "domAutomationController.send(hasError);", |
| 58 | &result)); |
| 59 | EXPECT_TRUE(result); |
| 60 | } |
| 61 | |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 62 | // This class observes chrome::NOTIFICATION_AUTH_NEEDED and supplies |
| 63 | // the credential which is required by the test proxy server. |
| 64 | // "foo:bar" is the required username and password for our test proxy server. |
| 65 | class LoginPromptObserver : public content::NotificationObserver { |
| 66 | public: |
| 67 | LoginPromptObserver() : auth_handled_(false) {} |
| 68 | |
dcheng | fce29ad | 2014-10-23 03:47:47 | [diff] [blame] | 69 | void Observe(int type, |
| 70 | const content::NotificationSource& source, |
| 71 | const content::NotificationDetails& details) override { |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 72 | if (type == chrome::NOTIFICATION_AUTH_NEEDED) { |
| 73 | LoginNotificationDetails* login_details = |
| 74 | content::Details<LoginNotificationDetails>(details).ptr(); |
| 75 | // |login_details->handler()| is the associated LoginHandler object. |
| 76 | // SetAuth() will close the login dialog. |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 77 | login_details->handler()->SetAuth(base::ASCIIToUTF16("foo"), |
| 78 | base::ASCIIToUTF16("bar")); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 79 | auth_handled_ = true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | bool auth_handled() const { return auth_handled_; } |
| 84 | |
| 85 | private: |
| 86 | bool auth_handled_; |
[email protected] | 5b7115b3 | 2012-12-05 21:38:09 | [diff] [blame] | 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | class ProxyBrowserTest : public InProcessBrowserTest { |
| 92 | public: |
| 93 | ProxyBrowserTest() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 94 | : proxy_server_(net::SpawnedTestServer::TYPE_BASIC_AUTH_PROXY, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 95 | base::FilePath()) { |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 96 | } |
| 97 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 98 | void SetUp() override { |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 99 | ASSERT_TRUE(proxy_server_.Start()); |
Jay Civelli | 1ba11572 | 2018-07-16 17:41:01 | [diff] [blame] | 100 | // Block the GaiaAuthFetcher related requests, they somehow interfere with |
| 101 | // the test when the network service is running. |
| 102 | url_loader_interceptor_ = std::make_unique<content::URLLoaderInterceptor>( |
| 103 | base::BindLambdaForTesting( |
| 104 | [&](content::URLLoaderInterceptor::RequestParams* params) -> bool { |
| 105 | if (params->url_request.url.host() == |
| 106 | GaiaUrls::GetInstance()->gaia_url().host()) { |
| 107 | return true; |
| 108 | } |
| 109 | return false; |
| 110 | })); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 111 | InProcessBrowserTest::SetUp(); |
| 112 | } |
| 113 | |
Jay Civelli | 1ba11572 | 2018-07-16 17:41:01 | [diff] [blame] | 114 | void PostRunTestOnMainThread() override { |
| 115 | url_loader_interceptor_.reset(); |
| 116 | InProcessBrowserTest::PostRunTestOnMainThread(); |
| 117 | } |
| 118 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 119 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 120 | command_line->AppendSwitchASCII(switches::kProxyServer, |
| 121 | proxy_server_.host_port_pair().ToString()); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame^] | 122 | |
| 123 | // TODO(https://crbug.com/901896): Don't rely on proxying localhost (Relied |
| 124 | // on by BasicAuthWSConnect) |
| 125 | command_line->AppendSwitchASCII( |
| 126 | switches::kProxyBypassList, |
| 127 | net::ProxyBypassRules::GetRulesToSubtractImplicit()); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | protected: |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 131 | net::SpawnedTestServer proxy_server_; |
[email protected] | 5b7115b3 | 2012-12-05 21:38:09 | [diff] [blame] | 132 | |
| 133 | private: |
Jay Civelli | 1ba11572 | 2018-07-16 17:41:01 | [diff] [blame] | 134 | std::unique_ptr<content::URLLoaderInterceptor> url_loader_interceptor_; |
[email protected] | 5b7115b3 | 2012-12-05 21:38:09 | [diff] [blame] | 135 | DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 136 | }; |
| 137 | |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 138 | // We bypass manually installed proxy for localhost on chromeos. |
Adam Rice | 2f3efd6 | 2018-04-10 13:10:00 | [diff] [blame] | 139 | #if defined(OS_CHROMEOS) |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 140 | #define MAYBE_BasicAuthWSConnect DISABLED_BasicAuthWSConnect |
| 141 | #else |
| 142 | #define MAYBE_BasicAuthWSConnect BasicAuthWSConnect |
| 143 | #endif |
| 144 | // Test that the browser can establish a WebSocket connection via a proxy |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 145 | // that requires basic authentication. This test also checks the headers |
| 146 | // arrive at WebSocket server. |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 147 | IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, MAYBE_BasicAuthWSConnect) { |
| 148 | // Launch WebSocket server. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 149 | net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS, |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 150 | net::GetWebSocketTestDataDirectory()); |
[email protected] | e0e6f9f | 2012-10-24 05:35:44 | [diff] [blame] | 151 | ASSERT_TRUE(ws_server.Start()); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 152 | |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 153 | content::WebContents* tab = |
| 154 | browser()->tab_strip_model()->GetActiveWebContents(); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 155 | content::NavigationController* controller = &tab->GetController(); |
| 156 | content::NotificationRegistrar registrar; |
| 157 | // The proxy server will request basic authentication. |
| 158 | // |observer| supplies the credential. |
| 159 | LoginPromptObserver observer; |
| 160 | registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED, |
| 161 | content::Source<content::NavigationController>(controller)); |
| 162 | |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 163 | content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); |
| 164 | watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 165 | |
| 166 | // Visit a page that tries to establish WebSocket connection. The title |
| 167 | // of the page will be 'PASS' on success. |
[email protected] | e0e6f9f | 2012-10-24 05:35:44 | [diff] [blame] | 168 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 169 | replacements.SetSchemeStr("http"); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 170 | ui_test_utils::NavigateToURL(browser(), |
| 171 | ws_server.GetURL("proxied_request_check.html") |
| 172 | .ReplaceComponents(replacements)); |
[email protected] | f20dead | 2013-03-02 03:01:48 | [diff] [blame] | 173 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 174 | const base::string16 result = watcher.WaitAndGetTitle(); |
brettw | 00a56b7 | 2015-06-10 03:47:26 | [diff] [blame] | 175 | EXPECT_TRUE(base::EqualsASCII(result, "PASS")); |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 176 | EXPECT_TRUE(observer.auth_handled()); |
| 177 | } |
| 178 | |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 179 | // Fetch PAC script via an http:// URL. |
| 180 | class HttpProxyScriptBrowserTest : public InProcessBrowserTest { |
| 181 | public: |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 182 | HttpProxyScriptBrowserTest() { |
| 183 | http_server_.ServeFilesFromSourceDirectory("chrome/test/data"); |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 184 | } |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 185 | ~HttpProxyScriptBrowserTest() override {} |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 186 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 187 | void SetUp() override { |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 188 | ASSERT_TRUE(http_server_.Start()); |
| 189 | InProcessBrowserTest::SetUp(); |
| 190 | } |
| 191 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 192 | void SetUpCommandLine(base::CommandLine* command_line) override { |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 193 | base::FilePath pac_script_path(FILE_PATH_LITERAL("/")); |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 194 | command_line->AppendSwitchASCII(switches::kProxyPacUrl, http_server_.GetURL( |
| 195 | pac_script_path.Append(kPACScript).MaybeAsASCII()).spec()); |
| 196 | } |
| 197 | |
| 198 | private: |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 199 | net::EmbeddedTestServer http_server_; |
[email protected] | b3ae2db | 2013-05-30 05:00:05 | [diff] [blame] | 200 | |
| 201 | DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest); |
| 202 | }; |
| 203 | |
| 204 | IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) { |
| 205 | VerifyProxyScript(browser()); |
| 206 | } |
| 207 | |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 208 | // Fetch PAC script via a hanging http:// URL. |
| 209 | class HangingPacRequestProxyScriptBrowserTest : public InProcessBrowserTest { |
| 210 | public: |
| 211 | HangingPacRequestProxyScriptBrowserTest() {} |
| 212 | ~HangingPacRequestProxyScriptBrowserTest() override {} |
| 213 | |
| 214 | void SetUp() override { |
| 215 | // Must start listening (And get a port for the proxy) before calling |
| 216 | // SetUp(). |
| 217 | ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
| 218 | InProcessBrowserTest::SetUp(); |
| 219 | } |
| 220 | |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame] | 221 | void TearDown() override { |
| 222 | // Need to stop this before |connection_listener_| is destroyed. |
| 223 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 224 | InProcessBrowserTest::TearDown(); |
| 225 | } |
| 226 | |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 227 | void SetUpOnMainThread() override { |
| 228 | // This must be created after the main message loop has been set up. |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame] | 229 | // Waits for one connection. Additional connections are fine. |
| 230 | connection_listener_ = |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 231 | std::make_unique<net::test_server::SimpleConnectionListener>( |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame] | 232 | 1, net::test_server::SimpleConnectionListener:: |
| 233 | ALLOW_ADDITIONAL_CONNECTIONS); |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 234 | embedded_test_server()->SetConnectionListener(connection_listener_.get()); |
| 235 | embedded_test_server()->StartAcceptingConnections(); |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 239 | command_line->AppendSwitchASCII( |
| 240 | switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec()); |
| 241 | } |
| 242 | |
| 243 | protected: |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame] | 244 | std::unique_ptr<net::test_server::SimpleConnectionListener> |
| 245 | connection_listener_; |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 246 | |
| 247 | private: |
| 248 | DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest); |
| 249 | }; |
| 250 | |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 251 | // Check that the URLRequest for a PAC that is still alive during shutdown is |
| 252 | // safely cleaned up. This test relies on AssertNoURLRequests being called on |
| 253 | // the main URLRequestContext. |
| 254 | IN_PROC_BROWSER_TEST_F(HangingPacRequestProxyScriptBrowserTest, Shutdown) { |
| 255 | // Request that should hang while trying to request the PAC script. |
| 256 | // Enough requests are created on startup that this probably isn't needed, but |
| 257 | // best to be safe. |
Sergio Villar Senin | c4f55f7 | 2018-07-19 07:49:15 | [diff] [blame] | 258 | auto resource_request = std::make_unique<network::ResourceRequest>(); |
| 259 | resource_request->url = GURL("http://blah/"); |
| 260 | auto simple_loader = network::SimpleURLLoader::Create( |
| 261 | std::move(resource_request), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 262 | |
| 263 | auto* storage_partition = |
| 264 | content::BrowserContext::GetDefaultStoragePartition(browser()->profile()); |
| 265 | auto url_loader_factory = |
| 266 | storage_partition->GetURLLoaderFactoryForBrowserProcess(); |
| 267 | simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie( |
| 268 | url_loader_factory.get(), |
| 269 | base::BindOnce([](std::unique_ptr<std::string> body) { |
| 270 | ADD_FAILURE() << "This request should never complete."; |
| 271 | })); |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 272 | |
mmenke | ab0c11d | 2017-05-30 17:15:17 | [diff] [blame] | 273 | connection_listener_->WaitForConnections(); |
mmenke | 0a8ca0d1 | 2017-05-03 18:40:07 | [diff] [blame] | 274 | } |
| 275 | |
[email protected] | d46ca730 | 2012-09-08 17:37:24 | [diff] [blame] | 276 | } // namespace |