blob: 4acb636a0e37f0cc6461f0445a6be9966bac6bc5 [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"
[email protected]f9b294362013-06-10 20:22:319#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1810#include "base/strings/utf_string_conversions.h"
Jay Civelli1ba115722018-07-16 17:41:0111#include "base/test/bind_test_util.h"
avi6846aef2015-12-26 01:09:3812#include "build/build_config.h"
[email protected]9ea0cd32013-07-12 01:50:3613#include "chrome/browser/chrome_notification_types.h"
mmenke0a8ca0d12017-05-03 18:40:0714#include "chrome/browser/profiles/profile.h"
[email protected]d46ca7302012-09-08 17:37:2415#include "chrome/browser/ui/browser.h"
tfarina535e6f52016-03-31 13:46:4916#include "chrome/browser/ui/login/login_handler.h"
[email protected]47ae23372013-01-29 01:50:4817#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]d46ca7302012-09-08 17:37:2418#include "chrome/common/chrome_paths.h"
19#include "chrome/common/chrome_switches.h"
[email protected]d46ca7302012-09-08 17:37:2420#include "chrome/test/base/in_process_browser_test.h"
21#include "chrome/test/base/ui_test_utils.h"
mmenke0a8ca0d12017-05-03 18:40:0722#include "content/public/browser/browser_thread.h"
[email protected]d46ca7302012-09-08 17:37:2423#include "content/public/browser/notification_details.h"
24#include "content/public/browser/notification_source.h"
Sergio Villar Seninc4f55f72018-07-19 07:49:1525#include "content/public/browser/storage_partition.h"
[email protected]d46ca7302012-09-08 17:37:2426#include "content/public/browser/web_contents.h"
27#include "content/public/browser/web_contents_observer.h"
yzshena90291c2017-04-26 16:22:5228#include "content/public/common/content_switches.h"
[email protected]d46ca7302012-09-08 17:37:2429#include "content/public/test/browser_test_utils.h"
Jay Civelli1ba115722018-07-16 17:41:0130#include "content/public/test/url_loader_interceptor.h"
31#include "google_apis/gaia/gaia_urls.h"
mmenke0a8ca0d12017-05-03 18:40:0732#include "net/base/load_flags.h"
svaldeze2745872015-11-04 23:30:2033#include "net/test/embedded_test_server/embedded_test_server.h"
mmenkeab0c11d2017-05-30 17:15:1734#include "net/test/embedded_test_server/simple_connection_listener.h"
[email protected]89b32522013-05-07 20:04:2135#include "net/test/spawned_test_server/spawned_test_server.h"
rsleevia69c79a2016-06-22 03:28:4336#include "net/test/test_data_directory.h"
rhalavati8aa3fbb72017-05-22 23:27:1837#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
Sergio Villar Seninc4f55f72018-07-19 07:49:1538#include "services/network/public/cpp/simple_url_loader.h"
mmenke0a8ca0d12017-05-03 18:40:0739#include "url/gurl.h"
[email protected]d46ca7302012-09-08 17:37:2440
41namespace {
42
[email protected]b3ae2db2013-05-30 05:00:0543// PAC script that sends all requests to an invalid proxy server.
44const base::FilePath::CharType kPACScript[] = FILE_PATH_LITERAL(
45 "bad_server.pac");
46
47// Verify kPACScript is installed as the PAC script.
48void 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]d46ca7302012-09-08 17:37:2462// 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.
65class LoginPromptObserver : public content::NotificationObserver {
66 public:
67 LoginPromptObserver() : auth_handled_(false) {}
68
dchengfce29ad2014-10-23 03:47:4769 void Observe(int type,
70 const content::NotificationSource& source,
71 const content::NotificationDetails& details) override {
[email protected]d46ca7302012-09-08 17:37:2472 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]6778fed2013-12-24 20:09:3777 login_details->handler()->SetAuth(base::ASCIIToUTF16("foo"),
78 base::ASCIIToUTF16("bar"));
[email protected]d46ca7302012-09-08 17:37:2479 auth_handled_ = true;
80 }
81 }
82
83 bool auth_handled() const { return auth_handled_; }
84
85 private:
86 bool auth_handled_;
[email protected]5b7115b32012-12-05 21:38:0987
88 DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver);
[email protected]d46ca7302012-09-08 17:37:2489};
90
91class ProxyBrowserTest : public InProcessBrowserTest {
92 public:
93 ProxyBrowserTest()
[email protected]ce7d0cbc2013-05-03 18:57:2294 : proxy_server_(net::SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
[email protected]650b2d52013-02-10 03:41:4595 base::FilePath()) {
[email protected]d46ca7302012-09-08 17:37:2496 }
97
dchenge1bc7982014-10-30 00:32:4098 void SetUp() override {
[email protected]d46ca7302012-09-08 17:37:2499 ASSERT_TRUE(proxy_server_.Start());
Jay Civelli1ba115722018-07-16 17:41:01100 // 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]d46ca7302012-09-08 17:37:24111 InProcessBrowserTest::SetUp();
112 }
113
Jay Civelli1ba115722018-07-16 17:41:01114 void PostRunTestOnMainThread() override {
115 url_loader_interceptor_.reset();
116 InProcessBrowserTest::PostRunTestOnMainThread();
117 }
118
avi556c05022014-12-22 23:31:43119 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]d46ca7302012-09-08 17:37:24120 command_line->AppendSwitchASCII(switches::kProxyServer,
121 proxy_server_.host_port_pair().ToString());
Eric Romanda790f92018-11-07 19:17:15122
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]d46ca7302012-09-08 17:37:24128 }
129
130 protected:
[email protected]ce7d0cbc2013-05-03 18:57:22131 net::SpawnedTestServer proxy_server_;
[email protected]5b7115b32012-12-05 21:38:09132
133 private:
Jay Civelli1ba115722018-07-16 17:41:01134 std::unique_ptr<content::URLLoaderInterceptor> url_loader_interceptor_;
[email protected]5b7115b32012-12-05 21:38:09135 DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest);
[email protected]d46ca7302012-09-08 17:37:24136};
137
[email protected]d46ca7302012-09-08 17:37:24138// We bypass manually installed proxy for localhost on chromeos.
Adam Rice2f3efd62018-04-10 13:10:00139#if defined(OS_CHROMEOS)
[email protected]d46ca7302012-09-08 17:37:24140#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 Rice425cf122015-01-19 06:18:24145// that requires basic authentication. This test also checks the headers
146// arrive at WebSocket server.
[email protected]d46ca7302012-09-08 17:37:24147IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, MAYBE_BasicAuthWSConnect) {
148 // Launch WebSocket server.
[email protected]ce7d0cbc2013-05-03 18:57:22149 net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
[email protected]ce7d0cbc2013-05-03 18:57:22150 net::GetWebSocketTestDataDirectory());
[email protected]e0e6f9f2012-10-24 05:35:44151 ASSERT_TRUE(ws_server.Start());
[email protected]d46ca7302012-09-08 17:37:24152
[email protected]47ae23372013-01-29 01:50:48153 content::WebContents* tab =
154 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]d46ca7302012-09-08 17:37:24155 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]6778fed2013-12-24 20:09:37163 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS"));
164 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
[email protected]d46ca7302012-09-08 17:37:24165
166 // Visit a page that tries to establish WebSocket connection. The title
167 // of the page will be 'PASS' on success.
[email protected]e0e6f9f2012-10-24 05:35:44168 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18169 replacements.SetSchemeStr("http");
Adam Rice425cf122015-01-19 06:18:24170 ui_test_utils::NavigateToURL(browser(),
171 ws_server.GetURL("proxied_request_check.html")
172 .ReplaceComponents(replacements));
[email protected]f20dead2013-03-02 03:01:48173
[email protected]439f1e32013-12-09 20:09:09174 const base::string16 result = watcher.WaitAndGetTitle();
brettw00a56b72015-06-10 03:47:26175 EXPECT_TRUE(base::EqualsASCII(result, "PASS"));
[email protected]d46ca7302012-09-08 17:37:24176 EXPECT_TRUE(observer.auth_handled());
177}
178
[email protected]b3ae2db2013-05-30 05:00:05179// Fetch PAC script via an http:// URL.
180class HttpProxyScriptBrowserTest : public InProcessBrowserTest {
181 public:
svaldeze2745872015-11-04 23:30:20182 HttpProxyScriptBrowserTest() {
183 http_server_.ServeFilesFromSourceDirectory("chrome/test/data");
[email protected]b3ae2db2013-05-30 05:00:05184 }
dchenge1bc7982014-10-30 00:32:40185 ~HttpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05186
dchenge1bc7982014-10-30 00:32:40187 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05188 ASSERT_TRUE(http_server_.Start());
189 InProcessBrowserTest::SetUp();
190 }
191
avi556c05022014-12-22 23:31:43192 void SetUpCommandLine(base::CommandLine* command_line) override {
svaldeze2745872015-11-04 23:30:20193 base::FilePath pac_script_path(FILE_PATH_LITERAL("/"));
[email protected]b3ae2db2013-05-30 05:00:05194 command_line->AppendSwitchASCII(switches::kProxyPacUrl, http_server_.GetURL(
195 pac_script_path.Append(kPACScript).MaybeAsASCII()).spec());
196 }
197
198 private:
svaldeze2745872015-11-04 23:30:20199 net::EmbeddedTestServer http_server_;
[email protected]b3ae2db2013-05-30 05:00:05200
201 DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest);
202};
203
204IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) {
205 VerifyProxyScript(browser());
206}
207
mmenke0a8ca0d12017-05-03 18:40:07208// Fetch PAC script via a hanging http:// URL.
209class 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
mmenkeab0c11d2017-05-30 17:15:17221 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
mmenke0a8ca0d12017-05-03 18:40:07227 void SetUpOnMainThread() override {
228 // This must be created after the main message loop has been set up.
mmenkeab0c11d2017-05-30 17:15:17229 // Waits for one connection. Additional connections are fine.
230 connection_listener_ =
Jeremy Romanec48d7a2018-03-01 17:35:09231 std::make_unique<net::test_server::SimpleConnectionListener>(
mmenkeab0c11d2017-05-30 17:15:17232 1, net::test_server::SimpleConnectionListener::
233 ALLOW_ADDITIONAL_CONNECTIONS);
mmenke0a8ca0d12017-05-03 18:40:07234 embedded_test_server()->SetConnectionListener(connection_listener_.get());
235 embedded_test_server()->StartAcceptingConnections();
mmenke0a8ca0d12017-05-03 18:40:07236 }
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:
mmenkeab0c11d2017-05-30 17:15:17244 std::unique_ptr<net::test_server::SimpleConnectionListener>
245 connection_listener_;
mmenke0a8ca0d12017-05-03 18:40:07246
247 private:
248 DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest);
249};
250
mmenke0a8ca0d12017-05-03 18:40:07251// 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.
254IN_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 Seninc4f55f72018-07-19 07:49:15258 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 }));
mmenke0a8ca0d12017-05-03 18:40:07272
mmenkeab0c11d2017-05-30 17:15:17273 connection_listener_->WaitForConnections();
mmenke0a8ca0d12017-05-03 18:40:07274}
275
[email protected]d46ca7302012-09-08 17:37:24276} // namespace