blob: e1ac8cacc9d73666743c5d3d56bcdd409509d443 [file] [log] [blame]
[email protected]d46ca7302012-09-08 17:37:241// 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
Sebastien Marchandf1349f52019-01-25 03:16:415#include "base/bind.h"
[email protected]d46ca7302012-09-08 17:37:246#include "base/command_line.h"
svaldeze2745872015-11-04 23:30:207#include "base/files/file_path.h"
mmenke0a8ca0d12017-05-03 18:40:078#include "base/location.h"
avi6846aef2015-12-26 01:09:389#include "base/macros.h"
Eric Roman7193c9f2018-11-22 01:39:5810#include "base/run_loop.h"
[email protected]f9b294362013-06-10 20:22:3111#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1812#include "base/strings/utf_string_conversions.h"
Jay Civelli1ba115722018-07-16 17:41:0113#include "base/test/bind_test_util.h"
avi6846aef2015-12-26 01:09:3814#include "build/build_config.h"
[email protected]9ea0cd32013-07-12 01:50:3615#include "chrome/browser/chrome_notification_types.h"
Emily Starka4cfb8722019-07-03 01:14:4016#include "chrome/browser/net/proxy_test_utils.h"
mmenke0a8ca0d12017-05-03 18:40:0717#include "chrome/browser/profiles/profile.h"
[email protected]d46ca7302012-09-08 17:37:2418#include "chrome/browser/ui/browser.h"
tfarina535e6f52016-03-31 13:46:4919#include "chrome/browser/ui/login/login_handler.h"
[email protected]47ae23372013-01-29 01:50:4820#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]d46ca7302012-09-08 17:37:2421#include "chrome/common/chrome_paths.h"
22#include "chrome/common/chrome_switches.h"
[email protected]d46ca7302012-09-08 17:37:2423#include "chrome/test/base/in_process_browser_test.h"
24#include "chrome/test/base/ui_test_utils.h"
mmenke0a8ca0d12017-05-03 18:40:0725#include "content/public/browser/browser_thread.h"
[email protected]d46ca7302012-09-08 17:37:2426#include "content/public/browser/notification_details.h"
27#include "content/public/browser/notification_source.h"
Sergio Villar Seninc4f55f72018-07-19 07:49:1528#include "content/public/browser/storage_partition.h"
[email protected]d46ca7302012-09-08 17:37:2429#include "content/public/browser/web_contents.h"
30#include "content/public/browser/web_contents_observer.h"
yzshena90291c2017-04-26 16:22:5231#include "content/public/common/content_switches.h"
[email protected]d46ca7302012-09-08 17:37:2432#include "content/public/test/browser_test_utils.h"
Jay Civelli1ba115722018-07-16 17:41:0133#include "content/public/test/url_loader_interceptor.h"
34#include "google_apis/gaia/gaia_urls.h"
mmenke0a8ca0d12017-05-03 18:40:0735#include "net/base/load_flags.h"
svaldeze2745872015-11-04 23:30:2036#include "net/test/embedded_test_server/embedded_test_server.h"
mmenkeab0c11d2017-05-30 17:15:1737#include "net/test/embedded_test_server/simple_connection_listener.h"
[email protected]89b32522013-05-07 20:04:2138#include "net/test/spawned_test_server/spawned_test_server.h"
rsleevia69c79a2016-06-22 03:28:4339#include "net/test/test_data_directory.h"
rhalavati8aa3fbb72017-05-22 23:27:1840#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
Sergio Villar Seninc4f55f72018-07-19 07:49:1541#include "services/network/public/cpp/simple_url_loader.h"
mmenke0a8ca0d12017-05-03 18:40:0742#include "url/gurl.h"
[email protected]d46ca7302012-09-08 17:37:2443
Andreea Costinas3ee19b62019-09-23 08:29:2644#if defined(OS_CHROMEOS)
45#include "chrome/browser/chromeos/net/dhcp_wpad_url_client.h"
46#endif // defined(OS_CHROMEOS)
47
[email protected]d46ca7302012-09-08 17:37:2448namespace {
49
[email protected]b3ae2db2013-05-30 05:00:0550// Verify kPACScript is installed as the PAC script.
51void VerifyProxyScript(Browser* browser) {
52 ui_test_utils::NavigateToURL(browser, GURL("http://google.com"));
53
54 // Verify we get the ERR_PROXY_CONNECTION_FAILED screen.
55 bool result = false;
56 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
57 browser->tab_strip_model()->GetActiveWebContents(),
58 "var textContent = document.body.textContent;"
59 "var hasError = textContent.indexOf('ERR_PROXY_CONNECTION_FAILED') >= 0;"
60 "domAutomationController.send(hasError);",
61 &result));
62 EXPECT_TRUE(result);
63}
64
[email protected]d46ca7302012-09-08 17:37:2465// This class observes chrome::NOTIFICATION_AUTH_NEEDED and supplies
66// the credential which is required by the test proxy server.
67// "foo:bar" is the required username and password for our test proxy server.
68class LoginPromptObserver : public content::NotificationObserver {
69 public:
70 LoginPromptObserver() : auth_handled_(false) {}
71
dchengfce29ad2014-10-23 03:47:4772 void Observe(int type,
73 const content::NotificationSource& source,
74 const content::NotificationDetails& details) override {
[email protected]d46ca7302012-09-08 17:37:2475 if (type == chrome::NOTIFICATION_AUTH_NEEDED) {
76 LoginNotificationDetails* login_details =
77 content::Details<LoginNotificationDetails>(details).ptr();
78 // |login_details->handler()| is the associated LoginHandler object.
79 // SetAuth() will close the login dialog.
[email protected]6778fed2013-12-24 20:09:3780 login_details->handler()->SetAuth(base::ASCIIToUTF16("foo"),
81 base::ASCIIToUTF16("bar"));
[email protected]d46ca7302012-09-08 17:37:2482 auth_handled_ = true;
83 }
84 }
85
86 bool auth_handled() const { return auth_handled_; }
87
88 private:
89 bool auth_handled_;
[email protected]5b7115b32012-12-05 21:38:0990
91 DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver);
[email protected]d46ca7302012-09-08 17:37:2492};
93
[email protected]d46ca7302012-09-08 17:37:2494// Test that the browser can establish a WebSocket connection via a proxy
Adam Rice425cf122015-01-19 06:18:2495// that requires basic authentication. This test also checks the headers
96// arrive at WebSocket server.
Eric Romanefdf315b2018-11-12 16:57:0897IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, BasicAuthWSConnect) {
[email protected]d46ca7302012-09-08 17:37:2498 // Launch WebSocket server.
[email protected]ce7d0cbc2013-05-03 18:57:2299 net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
[email protected]ce7d0cbc2013-05-03 18:57:22100 net::GetWebSocketTestDataDirectory());
[email protected]e0e6f9f2012-10-24 05:35:44101 ASSERT_TRUE(ws_server.Start());
[email protected]d46ca7302012-09-08 17:37:24102
[email protected]47ae23372013-01-29 01:50:48103 content::WebContents* tab =
104 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]d46ca7302012-09-08 17:37:24105 content::NavigationController* controller = &tab->GetController();
106 content::NotificationRegistrar registrar;
107 // The proxy server will request basic authentication.
108 // |observer| supplies the credential.
109 LoginPromptObserver observer;
110 registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED,
111 content::Source<content::NavigationController>(controller));
112
[email protected]6778fed2013-12-24 20:09:37113 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS"));
114 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
[email protected]d46ca7302012-09-08 17:37:24115
116 // Visit a page that tries to establish WebSocket connection. The title
117 // of the page will be 'PASS' on success.
[email protected]e0e6f9f2012-10-24 05:35:44118 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18119 replacements.SetSchemeStr("http");
Adam Rice425cf122015-01-19 06:18:24120 ui_test_utils::NavigateToURL(browser(),
121 ws_server.GetURL("proxied_request_check.html")
122 .ReplaceComponents(replacements));
[email protected]f20dead2013-03-02 03:01:48123
[email protected]439f1e32013-12-09 20:09:09124 const base::string16 result = watcher.WaitAndGetTitle();
brettw00a56b72015-06-10 03:47:26125 EXPECT_TRUE(base::EqualsASCII(result, "PASS"));
[email protected]d46ca7302012-09-08 17:37:24126 EXPECT_TRUE(observer.auth_handled());
127}
128
Eric Roman7193c9f2018-11-22 01:39:58129// Fetches a PAC script via an http:// URL, and ensures that requests to
130// http://www.google.com fail with ERR_PROXY_CONNECTION_FAILED (by virtue of
131// PAC file having selected a non-existent PROXY server).
132class BaseHttpProxyScriptBrowserTest : public InProcessBrowserTest {
[email protected]b3ae2db2013-05-30 05:00:05133 public:
Eric Roman7193c9f2018-11-22 01:39:58134 BaseHttpProxyScriptBrowserTest() {
Alan Cuttera9a96c52019-03-28 07:15:39135 http_server_.ServeFilesFromSourceDirectory(GetChromeTestDataDir());
[email protected]b3ae2db2013-05-30 05:00:05136 }
Eric Roman7193c9f2018-11-22 01:39:58137 ~BaseHttpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05138
dchenge1bc7982014-10-30 00:32:40139 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05140 ASSERT_TRUE(http_server_.Start());
141 InProcessBrowserTest::SetUp();
142 }
143
avi556c05022014-12-22 23:31:43144 void SetUpCommandLine(base::CommandLine* command_line) override {
Eric Roman7193c9f2018-11-22 01:39:58145 command_line->AppendSwitchASCII(
146 switches::kProxyPacUrl,
147 http_server_.GetURL("/" + GetPacFilename()).spec());
[email protected]b3ae2db2013-05-30 05:00:05148 }
149
Eric Roman7193c9f2018-11-22 01:39:58150 protected:
151 virtual std::string GetPacFilename() = 0;
Andreea Costinas3ee19b62019-09-23 08:29:26152 net::EmbeddedTestServer http_server_;
Eric Roman7193c9f2018-11-22 01:39:58153
[email protected]b3ae2db2013-05-30 05:00:05154 private:
Eric Roman7193c9f2018-11-22 01:39:58155 DISALLOW_COPY_AND_ASSIGN(BaseHttpProxyScriptBrowserTest);
156};
[email protected]b3ae2db2013-05-30 05:00:05157
Eric Roman7193c9f2018-11-22 01:39:58158// Tests the use of a PAC script that rejects requests to http://www.google.com/
159class HttpProxyScriptBrowserTest : public BaseHttpProxyScriptBrowserTest {
160 public:
161 HttpProxyScriptBrowserTest() = default;
162 ~HttpProxyScriptBrowserTest() override {}
163
164 std::string GetPacFilename() override {
165 // PAC script that sends all requests to an invalid proxy server.
166 return "bad_server.pac";
167 }
168
169 private:
[email protected]b3ae2db2013-05-30 05:00:05170 DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest);
171};
172
173IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) {
174 VerifyProxyScript(browser());
175}
176
Andreea Costinas3ee19b62019-09-23 08:29:26177#if defined(OS_CHROMEOS)
178// Tests the use of a PAC script set via Web Proxy Autodiscovery Protocol.
179// TODO(crbug.com/991867): Add a test case for when DhcpWpadUrlClient
180// returns an empty PAC URL.
181class WPADHttpProxyScriptBrowserTest : public HttpProxyScriptBrowserTest {
182 public:
183 WPADHttpProxyScriptBrowserTest() = default;
184 ~WPADHttpProxyScriptBrowserTest() override {}
185
186 void SetUp() override {
187 ASSERT_TRUE(http_server_.Start());
188 pac_url_ = http_server_.GetURL("/" + GetPacFilename());
189 chromeos::DhcpWpadUrlClient::SetPacUrlForTesting(pac_url_);
190 InProcessBrowserTest::SetUp();
191 }
192
193 void TearDown() override {
194 chromeos::DhcpWpadUrlClient::ClearPacUrlForTesting();
195 InProcessBrowserTest::TearDown();
196 }
197
198 void SetUpCommandLine(base::CommandLine* command_line) override {
199 command_line->AppendSwitch(switches::kProxyAutoDetect);
200 }
201
202 private:
203 GURL pac_url_;
204 DISALLOW_COPY_AND_ASSIGN(WPADHttpProxyScriptBrowserTest);
205};
206
207IN_PROC_BROWSER_TEST_F(WPADHttpProxyScriptBrowserTest, Verify) {
208 VerifyProxyScript(browser());
209}
210#endif // defined(OS_CHROMEOS)
211
Eric Roman7193c9f2018-11-22 01:39:58212// Tests the use of a PAC script that rejects requests to http://www.google.com/
213// when myIpAddress() and myIpAddressEx() appear to be working.
214class MyIpAddressProxyScriptBrowserTest
215 : public BaseHttpProxyScriptBrowserTest {
216 public:
217 MyIpAddressProxyScriptBrowserTest() = default;
218 ~MyIpAddressProxyScriptBrowserTest() override {}
219
220 std::string GetPacFilename() override {
221 // PAC script that sends all requests to an invalid proxy server provided
222 // myIpAddress() and myIpAddressEx() are not loopback addresses.
223 return "my_ip_address.pac";
224 }
225
226 private:
227 DISALLOW_COPY_AND_ASSIGN(MyIpAddressProxyScriptBrowserTest);
228};
229
230IN_PROC_BROWSER_TEST_F(MyIpAddressProxyScriptBrowserTest, Verify) {
231 VerifyProxyScript(browser());
232}
233
mmenke0a8ca0d12017-05-03 18:40:07234// Fetch PAC script via a hanging http:// URL.
235class HangingPacRequestProxyScriptBrowserTest : public InProcessBrowserTest {
236 public:
237 HangingPacRequestProxyScriptBrowserTest() {}
238 ~HangingPacRequestProxyScriptBrowserTest() override {}
239
240 void SetUp() override {
241 // Must start listening (And get a port for the proxy) before calling
242 // SetUp().
243 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
244 InProcessBrowserTest::SetUp();
245 }
246
mmenkeab0c11d2017-05-30 17:15:17247 void TearDown() override {
248 // Need to stop this before |connection_listener_| is destroyed.
249 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
250 InProcessBrowserTest::TearDown();
251 }
252
mmenke0a8ca0d12017-05-03 18:40:07253 void SetUpOnMainThread() override {
254 // This must be created after the main message loop has been set up.
mmenkeab0c11d2017-05-30 17:15:17255 // Waits for one connection. Additional connections are fine.
256 connection_listener_ =
Jeremy Romanec48d7a2018-03-01 17:35:09257 std::make_unique<net::test_server::SimpleConnectionListener>(
mmenkeab0c11d2017-05-30 17:15:17258 1, net::test_server::SimpleConnectionListener::
259 ALLOW_ADDITIONAL_CONNECTIONS);
mmenke0a8ca0d12017-05-03 18:40:07260 embedded_test_server()->SetConnectionListener(connection_listener_.get());
261 embedded_test_server()->StartAcceptingConnections();
mmenke0a8ca0d12017-05-03 18:40:07262 }
263
264 void SetUpCommandLine(base::CommandLine* command_line) override {
265 command_line->AppendSwitchASCII(
266 switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec());
267 }
268
269 protected:
mmenkeab0c11d2017-05-30 17:15:17270 std::unique_ptr<net::test_server::SimpleConnectionListener>
271 connection_listener_;
mmenke0a8ca0d12017-05-03 18:40:07272
273 private:
274 DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest);
275};
276
mmenke0a8ca0d12017-05-03 18:40:07277// Check that the URLRequest for a PAC that is still alive during shutdown is
278// safely cleaned up. This test relies on AssertNoURLRequests being called on
279// the main URLRequestContext.
280IN_PROC_BROWSER_TEST_F(HangingPacRequestProxyScriptBrowserTest, Shutdown) {
281 // Request that should hang while trying to request the PAC script.
282 // Enough requests are created on startup that this probably isn't needed, but
283 // best to be safe.
Sergio Villar Seninc4f55f72018-07-19 07:49:15284 auto resource_request = std::make_unique<network::ResourceRequest>();
285 resource_request->url = GURL("http://blah/");
286 auto simple_loader = network::SimpleURLLoader::Create(
287 std::move(resource_request), TRAFFIC_ANNOTATION_FOR_TESTS);
288
289 auto* storage_partition =
290 content::BrowserContext::GetDefaultStoragePartition(browser()->profile());
291 auto url_loader_factory =
292 storage_partition->GetURLLoaderFactoryForBrowserProcess();
293 simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
294 url_loader_factory.get(),
295 base::BindOnce([](std::unique_ptr<std::string> body) {
296 ADD_FAILURE() << "This request should never complete.";
297 }));
mmenke0a8ca0d12017-05-03 18:40:07298
mmenkeab0c11d2017-05-30 17:15:17299 connection_listener_->WaitForConnections();
mmenke0a8ca0d12017-05-03 18:40:07300}
301
[email protected]d46ca7302012-09-08 17:37:24302} // namespace