blob: 3411515b2123cd9377ea91cf9841f4fb65152b3f [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,
[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());
97 InProcessBrowserTest::SetUp();
98 }
99
avi556c05022014-12-22 23:31:43100 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]d46ca7302012-09-08 17:37:24101 command_line->AppendSwitchASCII(switches::kProxyServer,
102 proxy_server_.host_port_pair().ToString());
103 }
104
105 protected:
[email protected]ce7d0cbc2013-05-03 18:57:22106 net::SpawnedTestServer proxy_server_;
[email protected]5b7115b32012-12-05 21:38:09107
108 private:
[email protected]5b7115b32012-12-05 21:38:09109 DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest);
[email protected]d46ca7302012-09-08 17:37:24110};
111
112#if defined(OS_CHROMEOS)
113// We bypass manually installed proxy for localhost on chromeos.
114#define MAYBE_BasicAuthWSConnect DISABLED_BasicAuthWSConnect
115#else
116#define MAYBE_BasicAuthWSConnect BasicAuthWSConnect
117#endif
118// Test that the browser can establish a WebSocket connection via a proxy
Adam Rice425cf122015-01-19 06:18:24119// that requires basic authentication. This test also checks the headers
120// arrive at WebSocket server.
[email protected]d46ca7302012-09-08 17:37:24121IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, MAYBE_BasicAuthWSConnect) {
122 // Launch WebSocket server.
[email protected]ce7d0cbc2013-05-03 18:57:22123 net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
[email protected]ce7d0cbc2013-05-03 18:57:22124 net::GetWebSocketTestDataDirectory());
[email protected]e0e6f9f2012-10-24 05:35:44125 ASSERT_TRUE(ws_server.Start());
[email protected]d46ca7302012-09-08 17:37:24126
[email protected]47ae23372013-01-29 01:50:48127 content::WebContents* tab =
128 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]d46ca7302012-09-08 17:37:24129 content::NavigationController* controller = &tab->GetController();
130 content::NotificationRegistrar registrar;
131 // The proxy server will request basic authentication.
132 // |observer| supplies the credential.
133 LoginPromptObserver observer;
134 registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED,
135 content::Source<content::NavigationController>(controller));
136
[email protected]6778fed2013-12-24 20:09:37137 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS"));
138 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
[email protected]d46ca7302012-09-08 17:37:24139
140 // Visit a page that tries to establish WebSocket connection. The title
141 // of the page will be 'PASS' on success.
[email protected]e0e6f9f2012-10-24 05:35:44142 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18143 replacements.SetSchemeStr("http");
Adam Rice425cf122015-01-19 06:18:24144 ui_test_utils::NavigateToURL(browser(),
145 ws_server.GetURL("proxied_request_check.html")
146 .ReplaceComponents(replacements));
[email protected]f20dead2013-03-02 03:01:48147
[email protected]439f1e32013-12-09 20:09:09148 const base::string16 result = watcher.WaitAndGetTitle();
brettw00a56b72015-06-10 03:47:26149 EXPECT_TRUE(base::EqualsASCII(result, "PASS"));
[email protected]d46ca7302012-09-08 17:37:24150 EXPECT_TRUE(observer.auth_handled());
151}
152
[email protected]b3ae2db2013-05-30 05:00:05153// Fetch PAC script via an http:// URL.
154class HttpProxyScriptBrowserTest : public InProcessBrowserTest {
155 public:
svaldeze2745872015-11-04 23:30:20156 HttpProxyScriptBrowserTest() {
157 http_server_.ServeFilesFromSourceDirectory("chrome/test/data");
[email protected]b3ae2db2013-05-30 05:00:05158 }
dchenge1bc7982014-10-30 00:32:40159 ~HttpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05160
dchenge1bc7982014-10-30 00:32:40161 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05162 ASSERT_TRUE(http_server_.Start());
163 InProcessBrowserTest::SetUp();
164 }
165
avi556c05022014-12-22 23:31:43166 void SetUpCommandLine(base::CommandLine* command_line) override {
svaldeze2745872015-11-04 23:30:20167 base::FilePath pac_script_path(FILE_PATH_LITERAL("/"));
[email protected]b3ae2db2013-05-30 05:00:05168 command_line->AppendSwitchASCII(switches::kProxyPacUrl, http_server_.GetURL(
169 pac_script_path.Append(kPACScript).MaybeAsASCII()).spec());
170 }
171
172 private:
svaldeze2745872015-11-04 23:30:20173 net::EmbeddedTestServer http_server_;
[email protected]b3ae2db2013-05-30 05:00:05174
175 DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest);
176};
177
178IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) {
179 VerifyProxyScript(browser());
180}
181
182// Fetch PAC script via a file:// URL.
183class FileProxyScriptBrowserTest : public InProcessBrowserTest {
184 public:
185 FileProxyScriptBrowserTest() {}
dchenge1bc7982014-10-30 00:32:40186 ~FileProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05187
avi556c05022014-12-22 23:31:43188 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]b3ae2db2013-05-30 05:00:05189 command_line->AppendSwitchASCII(switches::kProxyPacUrl,
190 ui_test_utils::GetTestUrl(
191 base::FilePath(base::FilePath::kCurrentDirectory),
192 base::FilePath(kPACScript)).spec());
193 }
194
195 private:
196 DISALLOW_COPY_AND_ASSIGN(FileProxyScriptBrowserTest);
197};
198
199IN_PROC_BROWSER_TEST_F(FileProxyScriptBrowserTest, Verify) {
200 VerifyProxyScript(browser());
201}
202
203// Fetch PAC script via an ftp:// URL.
204class FtpProxyScriptBrowserTest : public InProcessBrowserTest {
205 public:
206 FtpProxyScriptBrowserTest()
207 : ftp_server_(net::SpawnedTestServer::TYPE_FTP,
Sergey Ulanov9e8d6f32017-08-14 22:12:58208 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {}
dchenge1bc7982014-10-30 00:32:40209 ~FtpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05210
dchenge1bc7982014-10-30 00:32:40211 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05212 ASSERT_TRUE(ftp_server_.Start());
213 InProcessBrowserTest::SetUp();
214 }
215
avi556c05022014-12-22 23:31:43216 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]b3ae2db2013-05-30 05:00:05217 base::FilePath pac_script_path(kPACScript);
218 command_line->AppendSwitchASCII(
219 switches::kProxyPacUrl,
220 ftp_server_.GetURL(pac_script_path.MaybeAsASCII()).spec());
221 }
222
223 private:
224 net::SpawnedTestServer ftp_server_;
225
226 DISALLOW_COPY_AND_ASSIGN(FtpProxyScriptBrowserTest);
227};
228
229IN_PROC_BROWSER_TEST_F(FtpProxyScriptBrowserTest, Verify) {
230 VerifyProxyScript(browser());
231}
232
233// Fetch PAC script via a data: URL.
234class DataProxyScriptBrowserTest : public InProcessBrowserTest {
235 public:
236 DataProxyScriptBrowserTest() {}
dchenge1bc7982014-10-30 00:32:40237 ~DataProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05238
avi556c05022014-12-22 23:31:43239 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]b3ae2db2013-05-30 05:00:05240 std::string contents;
241 // Read in kPACScript contents.
[email protected]82f84b92013-08-30 18:23:50242 ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath(
[email protected]b3ae2db2013-05-30 05:00:05243 base::FilePath(base::FilePath::kCurrentDirectory),
244 base::FilePath(kPACScript)),
245 &contents));
246 command_line->AppendSwitchASCII(switches::kProxyPacUrl,
247 std::string("data:,") + contents);
248 }
249
250 private:
251 DISALLOW_COPY_AND_ASSIGN(DataProxyScriptBrowserTest);
252};
253
254IN_PROC_BROWSER_TEST_F(DataProxyScriptBrowserTest, Verify) {
255 VerifyProxyScript(browser());
256}
257
scottmge8de13f12017-01-19 01:42:01258// Fetch PAC script via a data: URL.
amistry467cdc72015-03-13 01:58:47259class OutOfProcessProxyResolverBrowserTest : public InProcessBrowserTest {
260 public:
261 OutOfProcessProxyResolverBrowserTest() {}
262 ~OutOfProcessProxyResolverBrowserTest() override {}
263
264 void SetUpCommandLine(base::CommandLine* command_line) override {
265 std::string contents;
266 // Read in kPACScript contents.
267 ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath(
268 base::FilePath(base::FilePath::kCurrentDirectory),
269 base::FilePath(kPACScript)),
270 &contents));
271 command_line->AppendSwitchASCII(
272 switches::kProxyPacUrl, "data:," + contents);
amistry467cdc72015-03-13 01:58:47273 }
274
275 private:
276 DISALLOW_COPY_AND_ASSIGN(OutOfProcessProxyResolverBrowserTest);
277};
278
279IN_PROC_BROWSER_TEST_F(OutOfProcessProxyResolverBrowserTest, Verify) {
280 VerifyProxyScript(browser());
281}
282
mmenke0a8ca0d12017-05-03 18:40:07283// Fetch PAC script via a hanging http:// URL.
284class HangingPacRequestProxyScriptBrowserTest : public InProcessBrowserTest {
285 public:
286 HangingPacRequestProxyScriptBrowserTest() {}
287 ~HangingPacRequestProxyScriptBrowserTest() override {}
288
289 void SetUp() override {
290 // Must start listening (And get a port for the proxy) before calling
291 // SetUp().
292 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
293 InProcessBrowserTest::SetUp();
294 }
295
mmenkeab0c11d2017-05-30 17:15:17296 void TearDown() override {
297 // Need to stop this before |connection_listener_| is destroyed.
298 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
299 InProcessBrowserTest::TearDown();
300 }
301
mmenke0a8ca0d12017-05-03 18:40:07302 void SetUpOnMainThread() override {
303 // This must be created after the main message loop has been set up.
mmenkeab0c11d2017-05-30 17:15:17304 // Waits for one connection. Additional connections are fine.
305 connection_listener_ =
306 base::MakeUnique<net::test_server::SimpleConnectionListener>(
307 1, net::test_server::SimpleConnectionListener::
308 ALLOW_ADDITIONAL_CONNECTIONS);
mmenke0a8ca0d12017-05-03 18:40:07309 embedded_test_server()->SetConnectionListener(connection_listener_.get());
310 embedded_test_server()->StartAcceptingConnections();
mmenke0a8ca0d12017-05-03 18:40:07311 }
312
313 void SetUpCommandLine(base::CommandLine* command_line) override {
314 command_line->AppendSwitchASCII(
315 switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec());
316 }
317
318 protected:
mmenkeab0c11d2017-05-30 17:15:17319 std::unique_ptr<net::test_server::SimpleConnectionListener>
320 connection_listener_;
mmenke0a8ca0d12017-05-03 18:40:07321
322 private:
323 DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest);
324};
325
326// URLFetcherDelegate that expects a request to hang.
327class HangingURLFetcherDelegate : public net::URLFetcherDelegate {
328 public:
329 HangingURLFetcherDelegate() {}
330 ~HangingURLFetcherDelegate() override {}
331
332 void OnURLFetchComplete(const net::URLFetcher* source) override {
333 ADD_FAILURE() << "This request should never complete.";
334 }
335
336 private:
337 DISALLOW_COPY_AND_ASSIGN(HangingURLFetcherDelegate);
338};
339
340// Check that the URLRequest for a PAC that is still alive during shutdown is
341// safely cleaned up. This test relies on AssertNoURLRequests being called on
342// the main URLRequestContext.
343IN_PROC_BROWSER_TEST_F(HangingPacRequestProxyScriptBrowserTest, Shutdown) {
344 // Request that should hang while trying to request the PAC script.
345 // Enough requests are created on startup that this probably isn't needed, but
346 // best to be safe.
347 HangingURLFetcherDelegate hanging_request_delegate;
348 std::unique_ptr<net::URLFetcher> hanging_fetcher = net::URLFetcher::Create(
rhalavati8aa3fbb72017-05-22 23:27:18349 GURL("http://blah/"), net::URLFetcher::GET, &hanging_request_delegate,
350 TRAFFIC_ANNOTATION_FOR_TESTS);
mmenke0a8ca0d12017-05-03 18:40:07351 hanging_fetcher->SetRequestContext(browser()->profile()->GetRequestContext());
352 hanging_fetcher->Start();
353
mmenkeab0c11d2017-05-30 17:15:17354 connection_listener_->WaitForConnections();
mmenke0a8ca0d12017-05-03 18:40:07355}
356
[email protected]d46ca7302012-09-08 17:37:24357} // namespace