blob: 3c75c4f8f99acca32aa18d73f52c97c05e9cc361 [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"
mmenke0a8ca0d12017-05-03 18:40:0716#include "chrome/browser/profiles/profile.h"
[email protected]d46ca7302012-09-08 17:37:2417#include "chrome/browser/ui/browser.h"
tfarina535e6f52016-03-31 13:46:4918#include "chrome/browser/ui/login/login_handler.h"
[email protected]47ae23372013-01-29 01:50:4819#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]d46ca7302012-09-08 17:37:2420#include "chrome/common/chrome_paths.h"
21#include "chrome/common/chrome_switches.h"
[email protected]d46ca7302012-09-08 17:37:2422#include "chrome/test/base/in_process_browser_test.h"
23#include "chrome/test/base/ui_test_utils.h"
mmenke0a8ca0d12017-05-03 18:40:0724#include "content/public/browser/browser_thread.h"
[email protected]d46ca7302012-09-08 17:37:2425#include "content/public/browser/notification_details.h"
26#include "content/public/browser/notification_source.h"
Sergio Villar Seninc4f55f72018-07-19 07:49:1527#include "content/public/browser/storage_partition.h"
[email protected]d46ca7302012-09-08 17:37:2428#include "content/public/browser/web_contents.h"
29#include "content/public/browser/web_contents_observer.h"
yzshena90291c2017-04-26 16:22:5230#include "content/public/common/content_switches.h"
[email protected]d46ca7302012-09-08 17:37:2431#include "content/public/test/browser_test_utils.h"
Jay Civelli1ba115722018-07-16 17:41:0132#include "content/public/test/url_loader_interceptor.h"
33#include "google_apis/gaia/gaia_urls.h"
mmenke0a8ca0d12017-05-03 18:40:0734#include "net/base/load_flags.h"
svaldeze2745872015-11-04 23:30:2035#include "net/test/embedded_test_server/embedded_test_server.h"
mmenkeab0c11d2017-05-30 17:15:1736#include "net/test/embedded_test_server/simple_connection_listener.h"
[email protected]89b32522013-05-07 20:04:2137#include "net/test/spawned_test_server/spawned_test_server.h"
rsleevia69c79a2016-06-22 03:28:4338#include "net/test/test_data_directory.h"
rhalavati8aa3fbb72017-05-22 23:27:1839#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
Sergio Villar Seninc4f55f72018-07-19 07:49:1540#include "services/network/public/cpp/simple_url_loader.h"
mmenke0a8ca0d12017-05-03 18:40:0741#include "url/gurl.h"
[email protected]d46ca7302012-09-08 17:37:2442
43namespace {
44
[email protected]b3ae2db2013-05-30 05:00:0545// Verify kPACScript is installed as the PAC script.
46void VerifyProxyScript(Browser* browser) {
47 ui_test_utils::NavigateToURL(browser, GURL("http://google.com"));
48
49 // Verify we get the ERR_PROXY_CONNECTION_FAILED screen.
50 bool result = false;
51 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
52 browser->tab_strip_model()->GetActiveWebContents(),
53 "var textContent = document.body.textContent;"
54 "var hasError = textContent.indexOf('ERR_PROXY_CONNECTION_FAILED') >= 0;"
55 "domAutomationController.send(hasError);",
56 &result));
57 EXPECT_TRUE(result);
58}
59
[email protected]d46ca7302012-09-08 17:37:2460// This class observes chrome::NOTIFICATION_AUTH_NEEDED and supplies
61// the credential which is required by the test proxy server.
62// "foo:bar" is the required username and password for our test proxy server.
63class LoginPromptObserver : public content::NotificationObserver {
64 public:
65 LoginPromptObserver() : auth_handled_(false) {}
66
dchengfce29ad2014-10-23 03:47:4767 void Observe(int type,
68 const content::NotificationSource& source,
69 const content::NotificationDetails& details) override {
[email protected]d46ca7302012-09-08 17:37:2470 if (type == chrome::NOTIFICATION_AUTH_NEEDED) {
71 LoginNotificationDetails* login_details =
72 content::Details<LoginNotificationDetails>(details).ptr();
73 // |login_details->handler()| is the associated LoginHandler object.
74 // SetAuth() will close the login dialog.
[email protected]6778fed2013-12-24 20:09:3775 login_details->handler()->SetAuth(base::ASCIIToUTF16("foo"),
76 base::ASCIIToUTF16("bar"));
[email protected]d46ca7302012-09-08 17:37:2477 auth_handled_ = true;
78 }
79 }
80
81 bool auth_handled() const { return auth_handled_; }
82
83 private:
84 bool auth_handled_;
[email protected]5b7115b32012-12-05 21:38:0985
86 DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver);
[email protected]d46ca7302012-09-08 17:37:2487};
88
89class ProxyBrowserTest : public InProcessBrowserTest {
90 public:
91 ProxyBrowserTest()
[email protected]ce7d0cbc2013-05-03 18:57:2292 : proxy_server_(net::SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
[email protected]650b2d52013-02-10 03:41:4593 base::FilePath()) {
[email protected]d46ca7302012-09-08 17:37:2494 }
95
dchenge1bc7982014-10-30 00:32:4096 void SetUp() override {
[email protected]d46ca7302012-09-08 17:37:2497 ASSERT_TRUE(proxy_server_.Start());
Jay Civelli1ba115722018-07-16 17:41:0198 // Block the GaiaAuthFetcher related requests, they somehow interfere with
99 // the test when the network service is running.
100 url_loader_interceptor_ = std::make_unique<content::URLLoaderInterceptor>(
101 base::BindLambdaForTesting(
102 [&](content::URLLoaderInterceptor::RequestParams* params) -> bool {
103 if (params->url_request.url.host() ==
104 GaiaUrls::GetInstance()->gaia_url().host()) {
105 return true;
106 }
107 return false;
108 }));
[email protected]d46ca7302012-09-08 17:37:24109 InProcessBrowserTest::SetUp();
110 }
111
Jay Civelli1ba115722018-07-16 17:41:01112 void PostRunTestOnMainThread() override {
113 url_loader_interceptor_.reset();
114 InProcessBrowserTest::PostRunTestOnMainThread();
115 }
116
avi556c05022014-12-22 23:31:43117 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]d46ca7302012-09-08 17:37:24118 command_line->AppendSwitchASCII(switches::kProxyServer,
119 proxy_server_.host_port_pair().ToString());
Eric Romanda790f92018-11-07 19:17:15120
121 // TODO(https://crbug.com/901896): Don't rely on proxying localhost (Relied
122 // on by BasicAuthWSConnect)
123 command_line->AppendSwitchASCII(
124 switches::kProxyBypassList,
125 net::ProxyBypassRules::GetRulesToSubtractImplicit());
[email protected]d46ca7302012-09-08 17:37:24126 }
127
128 protected:
[email protected]ce7d0cbc2013-05-03 18:57:22129 net::SpawnedTestServer proxy_server_;
[email protected]5b7115b32012-12-05 21:38:09130
131 private:
Jay Civelli1ba115722018-07-16 17:41:01132 std::unique_ptr<content::URLLoaderInterceptor> url_loader_interceptor_;
[email protected]5b7115b32012-12-05 21:38:09133 DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest);
[email protected]d46ca7302012-09-08 17:37:24134};
135
[email protected]d46ca7302012-09-08 17:37:24136// Test that the browser can establish a WebSocket connection via a proxy
Adam Rice425cf122015-01-19 06:18:24137// that requires basic authentication. This test also checks the headers
138// arrive at WebSocket server.
Eric Romanefdf315b2018-11-12 16:57:08139IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, BasicAuthWSConnect) {
[email protected]d46ca7302012-09-08 17:37:24140 // Launch WebSocket server.
[email protected]ce7d0cbc2013-05-03 18:57:22141 net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
[email protected]ce7d0cbc2013-05-03 18:57:22142 net::GetWebSocketTestDataDirectory());
[email protected]e0e6f9f2012-10-24 05:35:44143 ASSERT_TRUE(ws_server.Start());
[email protected]d46ca7302012-09-08 17:37:24144
[email protected]47ae23372013-01-29 01:50:48145 content::WebContents* tab =
146 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]d46ca7302012-09-08 17:37:24147 content::NavigationController* controller = &tab->GetController();
148 content::NotificationRegistrar registrar;
149 // The proxy server will request basic authentication.
150 // |observer| supplies the credential.
151 LoginPromptObserver observer;
152 registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED,
153 content::Source<content::NavigationController>(controller));
154
[email protected]6778fed2013-12-24 20:09:37155 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS"));
156 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
[email protected]d46ca7302012-09-08 17:37:24157
158 // Visit a page that tries to establish WebSocket connection. The title
159 // of the page will be 'PASS' on success.
[email protected]e0e6f9f2012-10-24 05:35:44160 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18161 replacements.SetSchemeStr("http");
Adam Rice425cf122015-01-19 06:18:24162 ui_test_utils::NavigateToURL(browser(),
163 ws_server.GetURL("proxied_request_check.html")
164 .ReplaceComponents(replacements));
[email protected]f20dead2013-03-02 03:01:48165
[email protected]439f1e32013-12-09 20:09:09166 const base::string16 result = watcher.WaitAndGetTitle();
brettw00a56b72015-06-10 03:47:26167 EXPECT_TRUE(base::EqualsASCII(result, "PASS"));
[email protected]d46ca7302012-09-08 17:37:24168 EXPECT_TRUE(observer.auth_handled());
169}
170
Eric Roman7193c9f2018-11-22 01:39:58171// Fetches a PAC script via an http:// URL, and ensures that requests to
172// http://www.google.com fail with ERR_PROXY_CONNECTION_FAILED (by virtue of
173// PAC file having selected a non-existent PROXY server).
174class BaseHttpProxyScriptBrowserTest : public InProcessBrowserTest {
[email protected]b3ae2db2013-05-30 05:00:05175 public:
Eric Roman7193c9f2018-11-22 01:39:58176 BaseHttpProxyScriptBrowserTest() {
svaldeze2745872015-11-04 23:30:20177 http_server_.ServeFilesFromSourceDirectory("chrome/test/data");
[email protected]b3ae2db2013-05-30 05:00:05178 }
Eric Roman7193c9f2018-11-22 01:39:58179 ~BaseHttpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05180
dchenge1bc7982014-10-30 00:32:40181 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05182 ASSERT_TRUE(http_server_.Start());
183 InProcessBrowserTest::SetUp();
184 }
185
avi556c05022014-12-22 23:31:43186 void SetUpCommandLine(base::CommandLine* command_line) override {
Eric Roman7193c9f2018-11-22 01:39:58187 command_line->AppendSwitchASCII(
188 switches::kProxyPacUrl,
189 http_server_.GetURL("/" + GetPacFilename()).spec());
[email protected]b3ae2db2013-05-30 05:00:05190 }
191
Eric Roman7193c9f2018-11-22 01:39:58192 protected:
193 virtual std::string GetPacFilename() = 0;
194
[email protected]b3ae2db2013-05-30 05:00:05195 private:
svaldeze2745872015-11-04 23:30:20196 net::EmbeddedTestServer http_server_;
Eric Roman7193c9f2018-11-22 01:39:58197 DISALLOW_COPY_AND_ASSIGN(BaseHttpProxyScriptBrowserTest);
198};
[email protected]b3ae2db2013-05-30 05:00:05199
Eric Roman7193c9f2018-11-22 01:39:58200// Tests the use of a PAC script that rejects requests to http://www.google.com/
201class HttpProxyScriptBrowserTest : public BaseHttpProxyScriptBrowserTest {
202 public:
203 HttpProxyScriptBrowserTest() = default;
204 ~HttpProxyScriptBrowserTest() override {}
205
206 std::string GetPacFilename() override {
207 // PAC script that sends all requests to an invalid proxy server.
208 return "bad_server.pac";
209 }
210
211 private:
[email protected]b3ae2db2013-05-30 05:00:05212 DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest);
213};
214
215IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) {
216 VerifyProxyScript(browser());
217}
218
Eric Roman7193c9f2018-11-22 01:39:58219// Tests the use of a PAC script that rejects requests to http://www.google.com/
220// when myIpAddress() and myIpAddressEx() appear to be working.
221class MyIpAddressProxyScriptBrowserTest
222 : public BaseHttpProxyScriptBrowserTest {
223 public:
224 MyIpAddressProxyScriptBrowserTest() = default;
225 ~MyIpAddressProxyScriptBrowserTest() override {}
226
227 std::string GetPacFilename() override {
228 // PAC script that sends all requests to an invalid proxy server provided
229 // myIpAddress() and myIpAddressEx() are not loopback addresses.
230 return "my_ip_address.pac";
231 }
232
233 private:
234 DISALLOW_COPY_AND_ASSIGN(MyIpAddressProxyScriptBrowserTest);
235};
236
237IN_PROC_BROWSER_TEST_F(MyIpAddressProxyScriptBrowserTest, Verify) {
238 VerifyProxyScript(browser());
239}
240
mmenke0a8ca0d12017-05-03 18:40:07241// Fetch PAC script via a hanging http:// URL.
242class HangingPacRequestProxyScriptBrowserTest : public InProcessBrowserTest {
243 public:
244 HangingPacRequestProxyScriptBrowserTest() {}
245 ~HangingPacRequestProxyScriptBrowserTest() override {}
246
247 void SetUp() override {
248 // Must start listening (And get a port for the proxy) before calling
249 // SetUp().
250 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
251 InProcessBrowserTest::SetUp();
252 }
253
mmenkeab0c11d2017-05-30 17:15:17254 void TearDown() override {
255 // Need to stop this before |connection_listener_| is destroyed.
256 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
257 InProcessBrowserTest::TearDown();
258 }
259
mmenke0a8ca0d12017-05-03 18:40:07260 void SetUpOnMainThread() override {
261 // This must be created after the main message loop has been set up.
mmenkeab0c11d2017-05-30 17:15:17262 // Waits for one connection. Additional connections are fine.
263 connection_listener_ =
Jeremy Romanec48d7a2018-03-01 17:35:09264 std::make_unique<net::test_server::SimpleConnectionListener>(
mmenkeab0c11d2017-05-30 17:15:17265 1, net::test_server::SimpleConnectionListener::
266 ALLOW_ADDITIONAL_CONNECTIONS);
mmenke0a8ca0d12017-05-03 18:40:07267 embedded_test_server()->SetConnectionListener(connection_listener_.get());
268 embedded_test_server()->StartAcceptingConnections();
mmenke0a8ca0d12017-05-03 18:40:07269 }
270
271 void SetUpCommandLine(base::CommandLine* command_line) override {
272 command_line->AppendSwitchASCII(
273 switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec());
274 }
275
276 protected:
mmenkeab0c11d2017-05-30 17:15:17277 std::unique_ptr<net::test_server::SimpleConnectionListener>
278 connection_listener_;
mmenke0a8ca0d12017-05-03 18:40:07279
280 private:
281 DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest);
282};
283
mmenke0a8ca0d12017-05-03 18:40:07284// Check that the URLRequest for a PAC that is still alive during shutdown is
285// safely cleaned up. This test relies on AssertNoURLRequests being called on
286// the main URLRequestContext.
287IN_PROC_BROWSER_TEST_F(HangingPacRequestProxyScriptBrowserTest, Shutdown) {
288 // Request that should hang while trying to request the PAC script.
289 // Enough requests are created on startup that this probably isn't needed, but
290 // best to be safe.
Sergio Villar Seninc4f55f72018-07-19 07:49:15291 auto resource_request = std::make_unique<network::ResourceRequest>();
292 resource_request->url = GURL("http://blah/");
293 auto simple_loader = network::SimpleURLLoader::Create(
294 std::move(resource_request), TRAFFIC_ANNOTATION_FOR_TESTS);
295
296 auto* storage_partition =
297 content::BrowserContext::GetDefaultStoragePartition(browser()->profile());
298 auto url_loader_factory =
299 storage_partition->GetURLLoaderFactoryForBrowserProcess();
300 simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
301 url_loader_factory.get(),
302 base::BindOnce([](std::unique_ptr<std::string> body) {
303 ADD_FAILURE() << "This request should never complete.";
304 }));
mmenke0a8ca0d12017-05-03 18:40:07305
mmenkeab0c11d2017-05-30 17:15:17306 connection_listener_->WaitForConnections();
mmenke0a8ca0d12017-05-03 18:40:07307}
308
[email protected]d46ca7302012-09-08 17:37:24309} // namespace