blob: a5f0c1cf56e49f1eef44004c5b092a6f933027f4 [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"
avi6846aef2015-12-26 01:09:3811#include "build/build_config.h"
[email protected]9ea0cd32013-07-12 01:50:3612#include "chrome/browser/chrome_notification_types.h"
mmenke0a8ca0d12017-05-03 18:40:0713#include "chrome/browser/profiles/profile.h"
[email protected]d46ca7302012-09-08 17:37:2414#include "chrome/browser/ui/browser.h"
tfarina535e6f52016-03-31 13:46:4915#include "chrome/browser/ui/login/login_handler.h"
[email protected]47ae23372013-01-29 01:50:4816#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]d46ca7302012-09-08 17:37:2417#include "chrome/common/chrome_paths.h"
18#include "chrome/common/chrome_switches.h"
[email protected]d46ca7302012-09-08 17:37:2419#include "chrome/test/base/in_process_browser_test.h"
20#include "chrome/test/base/ui_test_utils.h"
mmenke0a8ca0d12017-05-03 18:40:0721#include "content/public/browser/browser_thread.h"
[email protected]d46ca7302012-09-08 17:37:2422#include "content/public/browser/notification_details.h"
23#include "content/public/browser/notification_source.h"
24#include "content/public/browser/web_contents.h"
25#include "content/public/browser/web_contents_observer.h"
yzshena90291c2017-04-26 16:22:5226#include "content/public/common/content_switches.h"
[email protected]d46ca7302012-09-08 17:37:2427#include "content/public/test/browser_test_utils.h"
mmenke0a8ca0d12017-05-03 18:40:0728#include "net/base/load_flags.h"
svaldeze2745872015-11-04 23:30:2029#include "net/test/embedded_test_server/embedded_test_server.h"
mmenkeab0c11d2017-05-30 17:15:1730#include "net/test/embedded_test_server/simple_connection_listener.h"
[email protected]89b32522013-05-07 20:04:2131#include "net/test/spawned_test_server/spawned_test_server.h"
rsleevia69c79a2016-06-22 03:28:4332#include "net/test/test_data_directory.h"
rhalavati8aa3fbb72017-05-22 23:27:1833#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
mmenke0a8ca0d12017-05-03 18:40:0734#include "net/url_request/url_fetcher.h"
35#include "net/url_request/url_fetcher_delegate.h"
36#include "url/gurl.h"
[email protected]d46ca7302012-09-08 17:37:2437
38namespace {
39
[email protected]b3ae2db2013-05-30 05:00:0540// PAC script that sends all requests to an invalid proxy server.
41const base::FilePath::CharType kPACScript[] = FILE_PATH_LITERAL(
42 "bad_server.pac");
43
44// 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,
92 net::SpawnedTestServer::kLocalhost,
[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());
98 InProcessBrowserTest::SetUp();
99 }
100
avi556c05022014-12-22 23:31:43101 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]d46ca7302012-09-08 17:37:24102 command_line->AppendSwitchASCII(switches::kProxyServer,
103 proxy_server_.host_port_pair().ToString());
104 }
105
106 protected:
[email protected]ce7d0cbc2013-05-03 18:57:22107 net::SpawnedTestServer proxy_server_;
[email protected]5b7115b32012-12-05 21:38:09108
109 private:
[email protected]5b7115b32012-12-05 21:38:09110 DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest);
[email protected]d46ca7302012-09-08 17:37:24111};
112
113#if defined(OS_CHROMEOS)
114// We bypass manually installed proxy for localhost on chromeos.
115#define MAYBE_BasicAuthWSConnect DISABLED_BasicAuthWSConnect
116#else
117#define MAYBE_BasicAuthWSConnect BasicAuthWSConnect
118#endif
119// Test that the browser can establish a WebSocket connection via a proxy
Adam Rice425cf122015-01-19 06:18:24120// that requires basic authentication. This test also checks the headers
121// arrive at WebSocket server.
[email protected]d46ca7302012-09-08 17:37:24122IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, MAYBE_BasicAuthWSConnect) {
123 // Launch WebSocket server.
[email protected]ce7d0cbc2013-05-03 18:57:22124 net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
125 net::SpawnedTestServer::kLocalhost,
126 net::GetWebSocketTestDataDirectory());
[email protected]e0e6f9f2012-10-24 05:35:44127 ASSERT_TRUE(ws_server.Start());
[email protected]d46ca7302012-09-08 17:37:24128
[email protected]47ae23372013-01-29 01:50:48129 content::WebContents* tab =
130 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]d46ca7302012-09-08 17:37:24131 content::NavigationController* controller = &tab->GetController();
132 content::NotificationRegistrar registrar;
133 // The proxy server will request basic authentication.
134 // |observer| supplies the credential.
135 LoginPromptObserver observer;
136 registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED,
137 content::Source<content::NavigationController>(controller));
138
[email protected]6778fed2013-12-24 20:09:37139 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS"));
140 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
[email protected]d46ca7302012-09-08 17:37:24141
142 // Visit a page that tries to establish WebSocket connection. The title
143 // of the page will be 'PASS' on success.
[email protected]e0e6f9f2012-10-24 05:35:44144 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18145 replacements.SetSchemeStr("http");
Adam Rice425cf122015-01-19 06:18:24146 ui_test_utils::NavigateToURL(browser(),
147 ws_server.GetURL("proxied_request_check.html")
148 .ReplaceComponents(replacements));
[email protected]f20dead2013-03-02 03:01:48149
[email protected]439f1e32013-12-09 20:09:09150 const base::string16 result = watcher.WaitAndGetTitle();
brettw00a56b72015-06-10 03:47:26151 EXPECT_TRUE(base::EqualsASCII(result, "PASS"));
[email protected]d46ca7302012-09-08 17:37:24152 EXPECT_TRUE(observer.auth_handled());
153}
154
[email protected]b3ae2db2013-05-30 05:00:05155// Fetch PAC script via an http:// URL.
156class HttpProxyScriptBrowserTest : public InProcessBrowserTest {
157 public:
svaldeze2745872015-11-04 23:30:20158 HttpProxyScriptBrowserTest() {
159 http_server_.ServeFilesFromSourceDirectory("chrome/test/data");
[email protected]b3ae2db2013-05-30 05:00:05160 }
dchenge1bc7982014-10-30 00:32:40161 ~HttpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05162
dchenge1bc7982014-10-30 00:32:40163 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05164 ASSERT_TRUE(http_server_.Start());
165 InProcessBrowserTest::SetUp();
166 }
167
avi556c05022014-12-22 23:31:43168 void SetUpCommandLine(base::CommandLine* command_line) override {
svaldeze2745872015-11-04 23:30:20169 base::FilePath pac_script_path(FILE_PATH_LITERAL("/"));
[email protected]b3ae2db2013-05-30 05:00:05170 command_line->AppendSwitchASCII(switches::kProxyPacUrl, http_server_.GetURL(
171 pac_script_path.Append(kPACScript).MaybeAsASCII()).spec());
172 }
173
174 private:
svaldeze2745872015-11-04 23:30:20175 net::EmbeddedTestServer http_server_;
[email protected]b3ae2db2013-05-30 05:00:05176
177 DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest);
178};
179
180IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) {
181 VerifyProxyScript(browser());
182}
183
184// Fetch PAC script via a file:// URL.
185class FileProxyScriptBrowserTest : public InProcessBrowserTest {
186 public:
187 FileProxyScriptBrowserTest() {}
dchenge1bc7982014-10-30 00:32:40188 ~FileProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05189
avi556c05022014-12-22 23:31:43190 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]b3ae2db2013-05-30 05:00:05191 command_line->AppendSwitchASCII(switches::kProxyPacUrl,
192 ui_test_utils::GetTestUrl(
193 base::FilePath(base::FilePath::kCurrentDirectory),
194 base::FilePath(kPACScript)).spec());
195 }
196
197 private:
198 DISALLOW_COPY_AND_ASSIGN(FileProxyScriptBrowserTest);
199};
200
201IN_PROC_BROWSER_TEST_F(FileProxyScriptBrowserTest, Verify) {
202 VerifyProxyScript(browser());
203}
204
205// Fetch PAC script via an ftp:// URL.
206class FtpProxyScriptBrowserTest : public InProcessBrowserTest {
207 public:
208 FtpProxyScriptBrowserTest()
209 : ftp_server_(net::SpawnedTestServer::TYPE_FTP,
210 net::SpawnedTestServer::kLocalhost,
211 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {
212 }
dchenge1bc7982014-10-30 00:32:40213 ~FtpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05214
dchenge1bc7982014-10-30 00:32:40215 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05216 ASSERT_TRUE(ftp_server_.Start());
217 InProcessBrowserTest::SetUp();
218 }
219
avi556c05022014-12-22 23:31:43220 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]b3ae2db2013-05-30 05:00:05221 base::FilePath pac_script_path(kPACScript);
222 command_line->AppendSwitchASCII(
223 switches::kProxyPacUrl,
224 ftp_server_.GetURL(pac_script_path.MaybeAsASCII()).spec());
225 }
226
227 private:
228 net::SpawnedTestServer ftp_server_;
229
230 DISALLOW_COPY_AND_ASSIGN(FtpProxyScriptBrowserTest);
231};
232
233IN_PROC_BROWSER_TEST_F(FtpProxyScriptBrowserTest, Verify) {
234 VerifyProxyScript(browser());
235}
236
237// Fetch PAC script via a data: URL.
238class DataProxyScriptBrowserTest : public InProcessBrowserTest {
239 public:
240 DataProxyScriptBrowserTest() {}
dchenge1bc7982014-10-30 00:32:40241 ~DataProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05242
avi556c05022014-12-22 23:31:43243 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]b3ae2db2013-05-30 05:00:05244 std::string contents;
245 // Read in kPACScript contents.
[email protected]82f84b92013-08-30 18:23:50246 ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath(
[email protected]b3ae2db2013-05-30 05:00:05247 base::FilePath(base::FilePath::kCurrentDirectory),
248 base::FilePath(kPACScript)),
249 &contents));
250 command_line->AppendSwitchASCII(switches::kProxyPacUrl,
251 std::string("data:,") + contents);
252 }
253
254 private:
255 DISALLOW_COPY_AND_ASSIGN(DataProxyScriptBrowserTest);
256};
257
258IN_PROC_BROWSER_TEST_F(DataProxyScriptBrowserTest, Verify) {
259 VerifyProxyScript(browser());
260}
261
scottmge8de13f12017-01-19 01:42:01262// Fetch PAC script via a data: URL.
amistry467cdc72015-03-13 01:58:47263class OutOfProcessProxyResolverBrowserTest : public InProcessBrowserTest {
264 public:
265 OutOfProcessProxyResolverBrowserTest() {}
266 ~OutOfProcessProxyResolverBrowserTest() override {}
267
268 void SetUpCommandLine(base::CommandLine* command_line) override {
269 std::string contents;
270 // Read in kPACScript contents.
271 ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath(
272 base::FilePath(base::FilePath::kCurrentDirectory),
273 base::FilePath(kPACScript)),
274 &contents));
275 command_line->AppendSwitchASCII(
276 switches::kProxyPacUrl, "data:," + contents);
amistry467cdc72015-03-13 01:58:47277 }
278
279 private:
280 DISALLOW_COPY_AND_ASSIGN(OutOfProcessProxyResolverBrowserTest);
281};
282
283IN_PROC_BROWSER_TEST_F(OutOfProcessProxyResolverBrowserTest, Verify) {
284 VerifyProxyScript(browser());
285}
286
mmenke0a8ca0d12017-05-03 18:40:07287// Fetch PAC script via a hanging http:// URL.
288class HangingPacRequestProxyScriptBrowserTest : public InProcessBrowserTest {
289 public:
290 HangingPacRequestProxyScriptBrowserTest() {}
291 ~HangingPacRequestProxyScriptBrowserTest() override {}
292
293 void SetUp() override {
294 // Must start listening (And get a port for the proxy) before calling
295 // SetUp().
296 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
297 InProcessBrowserTest::SetUp();
298 }
299
mmenkeab0c11d2017-05-30 17:15:17300 void TearDown() override {
301 // Need to stop this before |connection_listener_| is destroyed.
302 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
303 InProcessBrowserTest::TearDown();
304 }
305
mmenke0a8ca0d12017-05-03 18:40:07306 void SetUpOnMainThread() override {
307 // This must be created after the main message loop has been set up.
mmenkeab0c11d2017-05-30 17:15:17308 // Waits for one connection. Additional connections are fine.
309 connection_listener_ =
310 base::MakeUnique<net::test_server::SimpleConnectionListener>(
311 1, net::test_server::SimpleConnectionListener::
312 ALLOW_ADDITIONAL_CONNECTIONS);
mmenke0a8ca0d12017-05-03 18:40:07313 embedded_test_server()->SetConnectionListener(connection_listener_.get());
314 embedded_test_server()->StartAcceptingConnections();
315
316 InProcessBrowserTest::SetUpOnMainThread();
317 }
318
319 void SetUpCommandLine(base::CommandLine* command_line) override {
320 command_line->AppendSwitchASCII(
321 switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec());
322 }
323
324 protected:
mmenkeab0c11d2017-05-30 17:15:17325 std::unique_ptr<net::test_server::SimpleConnectionListener>
326 connection_listener_;
mmenke0a8ca0d12017-05-03 18:40:07327
328 private:
329 DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest);
330};
331
332// URLFetcherDelegate that expects a request to hang.
333class HangingURLFetcherDelegate : public net::URLFetcherDelegate {
334 public:
335 HangingURLFetcherDelegate() {}
336 ~HangingURLFetcherDelegate() override {}
337
338 void OnURLFetchComplete(const net::URLFetcher* source) override {
339 ADD_FAILURE() << "This request should never complete.";
340 }
341
342 private:
343 DISALLOW_COPY_AND_ASSIGN(HangingURLFetcherDelegate);
344};
345
346// Check that the URLRequest for a PAC that is still alive during shutdown is
347// safely cleaned up. This test relies on AssertNoURLRequests being called on
348// the main URLRequestContext.
349IN_PROC_BROWSER_TEST_F(HangingPacRequestProxyScriptBrowserTest, Shutdown) {
350 // Request that should hang while trying to request the PAC script.
351 // Enough requests are created on startup that this probably isn't needed, but
352 // best to be safe.
353 HangingURLFetcherDelegate hanging_request_delegate;
354 std::unique_ptr<net::URLFetcher> hanging_fetcher = net::URLFetcher::Create(
rhalavati8aa3fbb72017-05-22 23:27:18355 GURL("http://blah/"), net::URLFetcher::GET, &hanging_request_delegate,
356 TRAFFIC_ANNOTATION_FOR_TESTS);
mmenke0a8ca0d12017-05-03 18:40:07357 hanging_fetcher->SetRequestContext(browser()->profile()->GetRequestContext());
358 hanging_fetcher->Start();
359
mmenkeab0c11d2017-05-30 17:15:17360 connection_listener_->WaitForConnections();
mmenke0a8ca0d12017-05-03 18:40:07361}
362
[email protected]d46ca7302012-09-08 17:37:24363} // namespace