blob: 15162014c8884148170a0ac9feb19f023141102a [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
5#include "base/command_line.h"
svaldeze2745872015-11-04 23:30:206#include "base/files/file_path.h"
mmenke0a8ca0d12017-05-03 18:40:077#include "base/location.h"
avi6846aef2015-12-26 01:09:388#include "base/macros.h"
Eric Roman7193c9f2018-11-22 01:39:589#include "base/run_loop.h"
[email protected]f9b294362013-06-10 20:22:3110#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1811#include "base/strings/utf_string_conversions.h"
Jay Civelli1ba115722018-07-16 17:41:0112#include "base/test/bind_test_util.h"
avi6846aef2015-12-26 01:09:3813#include "build/build_config.h"
[email protected]9ea0cd32013-07-12 01:50:3614#include "chrome/browser/chrome_notification_types.h"
mmenke0a8ca0d12017-05-03 18:40:0715#include "chrome/browser/profiles/profile.h"
[email protected]d46ca7302012-09-08 17:37:2416#include "chrome/browser/ui/browser.h"
tfarina535e6f52016-03-31 13:46:4917#include "chrome/browser/ui/login/login_handler.h"
[email protected]47ae23372013-01-29 01:50:4818#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]d46ca7302012-09-08 17:37:2419#include "chrome/common/chrome_paths.h"
20#include "chrome/common/chrome_switches.h"
[email protected]d46ca7302012-09-08 17:37:2421#include "chrome/test/base/in_process_browser_test.h"
22#include "chrome/test/base/ui_test_utils.h"
mmenke0a8ca0d12017-05-03 18:40:0723#include "content/public/browser/browser_thread.h"
[email protected]d46ca7302012-09-08 17:37:2424#include "content/public/browser/notification_details.h"
25#include "content/public/browser/notification_source.h"
Sergio Villar Seninc4f55f72018-07-19 07:49:1526#include "content/public/browser/storage_partition.h"
[email protected]d46ca7302012-09-08 17:37:2427#include "content/public/browser/web_contents.h"
28#include "content/public/browser/web_contents_observer.h"
yzshena90291c2017-04-26 16:22:5229#include "content/public/common/content_switches.h"
[email protected]d46ca7302012-09-08 17:37:2430#include "content/public/test/browser_test_utils.h"
Jay Civelli1ba115722018-07-16 17:41:0131#include "content/public/test/url_loader_interceptor.h"
32#include "google_apis/gaia/gaia_urls.h"
mmenke0a8ca0d12017-05-03 18:40:0733#include "net/base/load_flags.h"
svaldeze2745872015-11-04 23:30:2034#include "net/test/embedded_test_server/embedded_test_server.h"
mmenkeab0c11d2017-05-30 17:15:1735#include "net/test/embedded_test_server/simple_connection_listener.h"
[email protected]89b32522013-05-07 20:04:2136#include "net/test/spawned_test_server/spawned_test_server.h"
rsleevia69c79a2016-06-22 03:28:4337#include "net/test/test_data_directory.h"
rhalavati8aa3fbb72017-05-22 23:27:1838#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
Sergio Villar Seninc4f55f72018-07-19 07:49:1539#include "services/network/public/cpp/simple_url_loader.h"
mmenke0a8ca0d12017-05-03 18:40:0740#include "url/gurl.h"
[email protected]d46ca7302012-09-08 17:37:2441
42namespace {
43
[email protected]b3ae2db2013-05-30 05:00:0544// Verify kPACScript is installed as the PAC script.
45void VerifyProxyScript(Browser* browser) {
46 ui_test_utils::NavigateToURL(browser, GURL("http://google.com"));
47
48 // Verify we get the ERR_PROXY_CONNECTION_FAILED screen.
49 bool result = false;
50 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
51 browser->tab_strip_model()->GetActiveWebContents(),
52 "var textContent = document.body.textContent;"
53 "var hasError = textContent.indexOf('ERR_PROXY_CONNECTION_FAILED') >= 0;"
54 "domAutomationController.send(hasError);",
55 &result));
56 EXPECT_TRUE(result);
57}
58
[email protected]d46ca7302012-09-08 17:37:2459// This class observes chrome::NOTIFICATION_AUTH_NEEDED and supplies
60// the credential which is required by the test proxy server.
61// "foo:bar" is the required username and password for our test proxy server.
62class LoginPromptObserver : public content::NotificationObserver {
63 public:
64 LoginPromptObserver() : auth_handled_(false) {}
65
dchengfce29ad2014-10-23 03:47:4766 void Observe(int type,
67 const content::NotificationSource& source,
68 const content::NotificationDetails& details) override {
[email protected]d46ca7302012-09-08 17:37:2469 if (type == chrome::NOTIFICATION_AUTH_NEEDED) {
70 LoginNotificationDetails* login_details =
71 content::Details<LoginNotificationDetails>(details).ptr();
72 // |login_details->handler()| is the associated LoginHandler object.
73 // SetAuth() will close the login dialog.
[email protected]6778fed2013-12-24 20:09:3774 login_details->handler()->SetAuth(base::ASCIIToUTF16("foo"),
75 base::ASCIIToUTF16("bar"));
[email protected]d46ca7302012-09-08 17:37:2476 auth_handled_ = true;
77 }
78 }
79
80 bool auth_handled() const { return auth_handled_; }
81
82 private:
83 bool auth_handled_;
[email protected]5b7115b32012-12-05 21:38:0984
85 DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver);
[email protected]d46ca7302012-09-08 17:37:2486};
87
88class ProxyBrowserTest : public InProcessBrowserTest {
89 public:
90 ProxyBrowserTest()
[email protected]ce7d0cbc2013-05-03 18:57:2291 : proxy_server_(net::SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
[email protected]650b2d52013-02-10 03:41:4592 base::FilePath()) {
[email protected]d46ca7302012-09-08 17:37:2493 }
94
dchenge1bc7982014-10-30 00:32:4095 void SetUp() override {
[email protected]d46ca7302012-09-08 17:37:2496 ASSERT_TRUE(proxy_server_.Start());
Jay Civelli1ba115722018-07-16 17:41:0197 // Block the GaiaAuthFetcher related requests, they somehow interfere with
98 // the test when the network service is running.
99 url_loader_interceptor_ = std::make_unique<content::URLLoaderInterceptor>(
100 base::BindLambdaForTesting(
101 [&](content::URLLoaderInterceptor::RequestParams* params) -> bool {
102 if (params->url_request.url.host() ==
103 GaiaUrls::GetInstance()->gaia_url().host()) {
104 return true;
105 }
106 return false;
107 }));
[email protected]d46ca7302012-09-08 17:37:24108 InProcessBrowserTest::SetUp();
109 }
110
Jay Civelli1ba115722018-07-16 17:41:01111 void PostRunTestOnMainThread() override {
112 url_loader_interceptor_.reset();
113 InProcessBrowserTest::PostRunTestOnMainThread();
114 }
115
avi556c05022014-12-22 23:31:43116 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]d46ca7302012-09-08 17:37:24117 command_line->AppendSwitchASCII(switches::kProxyServer,
118 proxy_server_.host_port_pair().ToString());
Eric Romanda790f92018-11-07 19:17:15119
120 // TODO(https://crbug.com/901896): Don't rely on proxying localhost (Relied
121 // on by BasicAuthWSConnect)
122 command_line->AppendSwitchASCII(
123 switches::kProxyBypassList,
124 net::ProxyBypassRules::GetRulesToSubtractImplicit());
[email protected]d46ca7302012-09-08 17:37:24125 }
126
127 protected:
[email protected]ce7d0cbc2013-05-03 18:57:22128 net::SpawnedTestServer proxy_server_;
[email protected]5b7115b32012-12-05 21:38:09129
130 private:
Jay Civelli1ba115722018-07-16 17:41:01131 std::unique_ptr<content::URLLoaderInterceptor> url_loader_interceptor_;
[email protected]5b7115b32012-12-05 21:38:09132 DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest);
[email protected]d46ca7302012-09-08 17:37:24133};
134
[email protected]d46ca7302012-09-08 17:37:24135// Test that the browser can establish a WebSocket connection via a proxy
Adam Rice425cf122015-01-19 06:18:24136// that requires basic authentication. This test also checks the headers
137// arrive at WebSocket server.
Eric Romanefdf315b2018-11-12 16:57:08138IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, BasicAuthWSConnect) {
[email protected]d46ca7302012-09-08 17:37:24139 // Launch WebSocket server.
[email protected]ce7d0cbc2013-05-03 18:57:22140 net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
[email protected]ce7d0cbc2013-05-03 18:57:22141 net::GetWebSocketTestDataDirectory());
[email protected]e0e6f9f2012-10-24 05:35:44142 ASSERT_TRUE(ws_server.Start());
[email protected]d46ca7302012-09-08 17:37:24143
[email protected]47ae23372013-01-29 01:50:48144 content::WebContents* tab =
145 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]d46ca7302012-09-08 17:37:24146 content::NavigationController* controller = &tab->GetController();
147 content::NotificationRegistrar registrar;
148 // The proxy server will request basic authentication.
149 // |observer| supplies the credential.
150 LoginPromptObserver observer;
151 registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED,
152 content::Source<content::NavigationController>(controller));
153
[email protected]6778fed2013-12-24 20:09:37154 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS"));
155 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
[email protected]d46ca7302012-09-08 17:37:24156
157 // Visit a page that tries to establish WebSocket connection. The title
158 // of the page will be 'PASS' on success.
[email protected]e0e6f9f2012-10-24 05:35:44159 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18160 replacements.SetSchemeStr("http");
Adam Rice425cf122015-01-19 06:18:24161 ui_test_utils::NavigateToURL(browser(),
162 ws_server.GetURL("proxied_request_check.html")
163 .ReplaceComponents(replacements));
[email protected]f20dead2013-03-02 03:01:48164
[email protected]439f1e32013-12-09 20:09:09165 const base::string16 result = watcher.WaitAndGetTitle();
brettw00a56b72015-06-10 03:47:26166 EXPECT_TRUE(base::EqualsASCII(result, "PASS"));
[email protected]d46ca7302012-09-08 17:37:24167 EXPECT_TRUE(observer.auth_handled());
168}
169
Eric Roman7193c9f2018-11-22 01:39:58170// Fetches a PAC script via an http:// URL, and ensures that requests to
171// http://www.google.com fail with ERR_PROXY_CONNECTION_FAILED (by virtue of
172// PAC file having selected a non-existent PROXY server).
173class BaseHttpProxyScriptBrowserTest : public InProcessBrowserTest {
[email protected]b3ae2db2013-05-30 05:00:05174 public:
Eric Roman7193c9f2018-11-22 01:39:58175 BaseHttpProxyScriptBrowserTest() {
svaldeze2745872015-11-04 23:30:20176 http_server_.ServeFilesFromSourceDirectory("chrome/test/data");
[email protected]b3ae2db2013-05-30 05:00:05177 }
Eric Roman7193c9f2018-11-22 01:39:58178 ~BaseHttpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05179
dchenge1bc7982014-10-30 00:32:40180 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05181 ASSERT_TRUE(http_server_.Start());
182 InProcessBrowserTest::SetUp();
183 }
184
avi556c05022014-12-22 23:31:43185 void SetUpCommandLine(base::CommandLine* command_line) override {
Eric Roman7193c9f2018-11-22 01:39:58186 command_line->AppendSwitchASCII(
187 switches::kProxyPacUrl,
188 http_server_.GetURL("/" + GetPacFilename()).spec());
[email protected]b3ae2db2013-05-30 05:00:05189 }
190
Eric Roman7193c9f2018-11-22 01:39:58191 protected:
192 virtual std::string GetPacFilename() = 0;
193
[email protected]b3ae2db2013-05-30 05:00:05194 private:
svaldeze2745872015-11-04 23:30:20195 net::EmbeddedTestServer http_server_;
Eric Roman7193c9f2018-11-22 01:39:58196 DISALLOW_COPY_AND_ASSIGN(BaseHttpProxyScriptBrowserTest);
197};
[email protected]b3ae2db2013-05-30 05:00:05198
Eric Roman7193c9f2018-11-22 01:39:58199// Tests the use of a PAC script that rejects requests to http://www.google.com/
200class HttpProxyScriptBrowserTest : public BaseHttpProxyScriptBrowserTest {
201 public:
202 HttpProxyScriptBrowserTest() = default;
203 ~HttpProxyScriptBrowserTest() override {}
204
205 std::string GetPacFilename() override {
206 // PAC script that sends all requests to an invalid proxy server.
207 return "bad_server.pac";
208 }
209
210 private:
[email protected]b3ae2db2013-05-30 05:00:05211 DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest);
212};
213
214IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) {
215 VerifyProxyScript(browser());
216}
217
Eric Roman7193c9f2018-11-22 01:39:58218// Tests the use of a PAC script that rejects requests to http://www.google.com/
219// when myIpAddress() and myIpAddressEx() appear to be working.
220class MyIpAddressProxyScriptBrowserTest
221 : public BaseHttpProxyScriptBrowserTest {
222 public:
223 MyIpAddressProxyScriptBrowserTest() = default;
224 ~MyIpAddressProxyScriptBrowserTest() override {}
225
226 std::string GetPacFilename() override {
227 // PAC script that sends all requests to an invalid proxy server provided
228 // myIpAddress() and myIpAddressEx() are not loopback addresses.
229 return "my_ip_address.pac";
230 }
231
232 private:
233 DISALLOW_COPY_AND_ASSIGN(MyIpAddressProxyScriptBrowserTest);
234};
235
236IN_PROC_BROWSER_TEST_F(MyIpAddressProxyScriptBrowserTest, Verify) {
237 VerifyProxyScript(browser());
238}
239
mmenke0a8ca0d12017-05-03 18:40:07240// Fetch PAC script via a hanging http:// URL.
241class HangingPacRequestProxyScriptBrowserTest : public InProcessBrowserTest {
242 public:
243 HangingPacRequestProxyScriptBrowserTest() {}
244 ~HangingPacRequestProxyScriptBrowserTest() override {}
245
246 void SetUp() override {
247 // Must start listening (And get a port for the proxy) before calling
248 // SetUp().
249 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
250 InProcessBrowserTest::SetUp();
251 }
252
mmenkeab0c11d2017-05-30 17:15:17253 void TearDown() override {
254 // Need to stop this before |connection_listener_| is destroyed.
255 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
256 InProcessBrowserTest::TearDown();
257 }
258
mmenke0a8ca0d12017-05-03 18:40:07259 void SetUpOnMainThread() override {
260 // This must be created after the main message loop has been set up.
mmenkeab0c11d2017-05-30 17:15:17261 // Waits for one connection. Additional connections are fine.
262 connection_listener_ =
Jeremy Romanec48d7a2018-03-01 17:35:09263 std::make_unique<net::test_server::SimpleConnectionListener>(
mmenkeab0c11d2017-05-30 17:15:17264 1, net::test_server::SimpleConnectionListener::
265 ALLOW_ADDITIONAL_CONNECTIONS);
mmenke0a8ca0d12017-05-03 18:40:07266 embedded_test_server()->SetConnectionListener(connection_listener_.get());
267 embedded_test_server()->StartAcceptingConnections();
mmenke0a8ca0d12017-05-03 18:40:07268 }
269
270 void SetUpCommandLine(base::CommandLine* command_line) override {
271 command_line->AppendSwitchASCII(
272 switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec());
273 }
274
275 protected:
mmenkeab0c11d2017-05-30 17:15:17276 std::unique_ptr<net::test_server::SimpleConnectionListener>
277 connection_listener_;
mmenke0a8ca0d12017-05-03 18:40:07278
279 private:
280 DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest);
281};
282
mmenke0a8ca0d12017-05-03 18:40:07283// Check that the URLRequest for a PAC that is still alive during shutdown is
284// safely cleaned up. This test relies on AssertNoURLRequests being called on
285// the main URLRequestContext.
286IN_PROC_BROWSER_TEST_F(HangingPacRequestProxyScriptBrowserTest, Shutdown) {
287 // Request that should hang while trying to request the PAC script.
288 // Enough requests are created on startup that this probably isn't needed, but
289 // best to be safe.
Sergio Villar Seninc4f55f72018-07-19 07:49:15290 auto resource_request = std::make_unique<network::ResourceRequest>();
291 resource_request->url = GURL("http://blah/");
292 auto simple_loader = network::SimpleURLLoader::Create(
293 std::move(resource_request), TRAFFIC_ANNOTATION_FOR_TESTS);
294
295 auto* storage_partition =
296 content::BrowserContext::GetDefaultStoragePartition(browser()->profile());
297 auto url_loader_factory =
298 storage_partition->GetURLLoaderFactoryForBrowserProcess();
299 simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
300 url_loader_factory.get(),
301 base::BindOnce([](std::unique_ptr<std::string> body) {
302 ADD_FAILURE() << "This request should never complete.";
303 }));
mmenke0a8ca0d12017-05-03 18:40:07304
mmenkeab0c11d2017-05-30 17:15:17305 connection_listener_->WaitForConnections();
mmenke0a8ca0d12017-05-03 18:40:07306}
307
[email protected]d46ca7302012-09-08 17:37:24308} // namespace