blob: f2453ea9d816794b392a4cb642f41f07a13bc188 [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"
avi6846aef2015-12-26 01:09:387#include "base/macros.h"
[email protected]f9b294362013-06-10 20:22:318#include "base/strings/string_util.h"
9#include "base/strings/stringprintf.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"
[email protected]d46ca7302012-09-08 17:37:2413#include "chrome/browser/ui/browser.h"
tfarina535e6f52016-03-31 13:46:4914#include "chrome/browser/ui/login/login_handler.h"
[email protected]47ae23372013-01-29 01:50:4815#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]d46ca7302012-09-08 17:37:2416#include "chrome/common/chrome_paths.h"
17#include "chrome/common/chrome_switches.h"
[email protected]d46ca7302012-09-08 17:37:2418#include "chrome/test/base/in_process_browser_test.h"
19#include "chrome/test/base/ui_test_utils.h"
20#include "content/public/browser/notification_details.h"
21#include "content/public/browser/notification_source.h"
22#include "content/public/browser/web_contents.h"
23#include "content/public/browser/web_contents_observer.h"
24#include "content/public/test/browser_test_utils.h"
svaldeze2745872015-11-04 23:30:2025#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]89b32522013-05-07 20:04:2126#include "net/test/spawned_test_server/spawned_test_server.h"
rsleevia69c79a2016-06-22 03:28:4327#include "net/test/test_data_directory.h"
[email protected]d46ca7302012-09-08 17:37:2428
29namespace {
30
[email protected]b3ae2db2013-05-30 05:00:0531// PAC script that sends all requests to an invalid proxy server.
32const base::FilePath::CharType kPACScript[] = FILE_PATH_LITERAL(
33 "bad_server.pac");
34
35// Verify kPACScript is installed as the PAC script.
36void VerifyProxyScript(Browser* browser) {
37 ui_test_utils::NavigateToURL(browser, GURL("http://google.com"));
38
39 // Verify we get the ERR_PROXY_CONNECTION_FAILED screen.
40 bool result = false;
41 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
42 browser->tab_strip_model()->GetActiveWebContents(),
43 "var textContent = document.body.textContent;"
44 "var hasError = textContent.indexOf('ERR_PROXY_CONNECTION_FAILED') >= 0;"
45 "domAutomationController.send(hasError);",
46 &result));
47 EXPECT_TRUE(result);
48}
49
[email protected]d46ca7302012-09-08 17:37:2450// This class observes chrome::NOTIFICATION_AUTH_NEEDED and supplies
51// the credential which is required by the test proxy server.
52// "foo:bar" is the required username and password for our test proxy server.
53class LoginPromptObserver : public content::NotificationObserver {
54 public:
55 LoginPromptObserver() : auth_handled_(false) {}
56
dchengfce29ad2014-10-23 03:47:4757 void Observe(int type,
58 const content::NotificationSource& source,
59 const content::NotificationDetails& details) override {
[email protected]d46ca7302012-09-08 17:37:2460 if (type == chrome::NOTIFICATION_AUTH_NEEDED) {
61 LoginNotificationDetails* login_details =
62 content::Details<LoginNotificationDetails>(details).ptr();
63 // |login_details->handler()| is the associated LoginHandler object.
64 // SetAuth() will close the login dialog.
[email protected]6778fed2013-12-24 20:09:3765 login_details->handler()->SetAuth(base::ASCIIToUTF16("foo"),
66 base::ASCIIToUTF16("bar"));
[email protected]d46ca7302012-09-08 17:37:2467 auth_handled_ = true;
68 }
69 }
70
71 bool auth_handled() const { return auth_handled_; }
72
73 private:
74 bool auth_handled_;
[email protected]5b7115b32012-12-05 21:38:0975
76 DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver);
[email protected]d46ca7302012-09-08 17:37:2477};
78
79class ProxyBrowserTest : public InProcessBrowserTest {
80 public:
81 ProxyBrowserTest()
[email protected]ce7d0cbc2013-05-03 18:57:2282 : proxy_server_(net::SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
83 net::SpawnedTestServer::kLocalhost,
[email protected]650b2d52013-02-10 03:41:4584 base::FilePath()) {
[email protected]d46ca7302012-09-08 17:37:2485 }
86
dchenge1bc7982014-10-30 00:32:4087 void SetUp() override {
[email protected]d46ca7302012-09-08 17:37:2488 ASSERT_TRUE(proxy_server_.Start());
89 InProcessBrowserTest::SetUp();
90 }
91
avi556c05022014-12-22 23:31:4392 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]d46ca7302012-09-08 17:37:2493 command_line->AppendSwitchASCII(switches::kProxyServer,
94 proxy_server_.host_port_pair().ToString());
95 }
96
97 protected:
[email protected]ce7d0cbc2013-05-03 18:57:2298 net::SpawnedTestServer proxy_server_;
[email protected]5b7115b32012-12-05 21:38:0999
100 private:
101
102 DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest);
[email protected]d46ca7302012-09-08 17:37:24103};
104
105#if defined(OS_CHROMEOS)
106// We bypass manually installed proxy for localhost on chromeos.
107#define MAYBE_BasicAuthWSConnect DISABLED_BasicAuthWSConnect
108#else
109#define MAYBE_BasicAuthWSConnect BasicAuthWSConnect
110#endif
111// Test that the browser can establish a WebSocket connection via a proxy
Adam Rice425cf122015-01-19 06:18:24112// that requires basic authentication. This test also checks the headers
113// arrive at WebSocket server.
[email protected]d46ca7302012-09-08 17:37:24114IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, MAYBE_BasicAuthWSConnect) {
115 // Launch WebSocket server.
[email protected]ce7d0cbc2013-05-03 18:57:22116 net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
117 net::SpawnedTestServer::kLocalhost,
118 net::GetWebSocketTestDataDirectory());
[email protected]e0e6f9f2012-10-24 05:35:44119 ASSERT_TRUE(ws_server.Start());
[email protected]d46ca7302012-09-08 17:37:24120
[email protected]47ae23372013-01-29 01:50:48121 content::WebContents* tab =
122 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]d46ca7302012-09-08 17:37:24123 content::NavigationController* controller = &tab->GetController();
124 content::NotificationRegistrar registrar;
125 // The proxy server will request basic authentication.
126 // |observer| supplies the credential.
127 LoginPromptObserver observer;
128 registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED,
129 content::Source<content::NavigationController>(controller));
130
[email protected]6778fed2013-12-24 20:09:37131 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS"));
132 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
[email protected]d46ca7302012-09-08 17:37:24133
134 // Visit a page that tries to establish WebSocket connection. The title
135 // of the page will be 'PASS' on success.
[email protected]e0e6f9f2012-10-24 05:35:44136 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18137 replacements.SetSchemeStr("http");
Adam Rice425cf122015-01-19 06:18:24138 ui_test_utils::NavigateToURL(browser(),
139 ws_server.GetURL("proxied_request_check.html")
140 .ReplaceComponents(replacements));
[email protected]f20dead2013-03-02 03:01:48141
[email protected]439f1e32013-12-09 20:09:09142 const base::string16 result = watcher.WaitAndGetTitle();
brettw00a56b72015-06-10 03:47:26143 EXPECT_TRUE(base::EqualsASCII(result, "PASS"));
[email protected]d46ca7302012-09-08 17:37:24144 EXPECT_TRUE(observer.auth_handled());
145}
146
[email protected]b3ae2db2013-05-30 05:00:05147// Fetch PAC script via an http:// URL.
148class HttpProxyScriptBrowserTest : public InProcessBrowserTest {
149 public:
svaldeze2745872015-11-04 23:30:20150 HttpProxyScriptBrowserTest() {
151 http_server_.ServeFilesFromSourceDirectory("chrome/test/data");
[email protected]b3ae2db2013-05-30 05:00:05152 }
dchenge1bc7982014-10-30 00:32:40153 ~HttpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05154
dchenge1bc7982014-10-30 00:32:40155 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05156 ASSERT_TRUE(http_server_.Start());
157 InProcessBrowserTest::SetUp();
158 }
159
avi556c05022014-12-22 23:31:43160 void SetUpCommandLine(base::CommandLine* command_line) override {
svaldeze2745872015-11-04 23:30:20161 base::FilePath pac_script_path(FILE_PATH_LITERAL("/"));
[email protected]b3ae2db2013-05-30 05:00:05162 command_line->AppendSwitchASCII(switches::kProxyPacUrl, http_server_.GetURL(
163 pac_script_path.Append(kPACScript).MaybeAsASCII()).spec());
164 }
165
166 private:
svaldeze2745872015-11-04 23:30:20167 net::EmbeddedTestServer http_server_;
[email protected]b3ae2db2013-05-30 05:00:05168
169 DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest);
170};
171
172IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) {
173 VerifyProxyScript(browser());
174}
175
176// Fetch PAC script via a file:// URL.
177class FileProxyScriptBrowserTest : public InProcessBrowserTest {
178 public:
179 FileProxyScriptBrowserTest() {}
dchenge1bc7982014-10-30 00:32:40180 ~FileProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05181
avi556c05022014-12-22 23:31:43182 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]b3ae2db2013-05-30 05:00:05183 command_line->AppendSwitchASCII(switches::kProxyPacUrl,
184 ui_test_utils::GetTestUrl(
185 base::FilePath(base::FilePath::kCurrentDirectory),
186 base::FilePath(kPACScript)).spec());
187 }
188
189 private:
190 DISALLOW_COPY_AND_ASSIGN(FileProxyScriptBrowserTest);
191};
192
193IN_PROC_BROWSER_TEST_F(FileProxyScriptBrowserTest, Verify) {
194 VerifyProxyScript(browser());
195}
196
197// Fetch PAC script via an ftp:// URL.
198class FtpProxyScriptBrowserTest : public InProcessBrowserTest {
199 public:
200 FtpProxyScriptBrowserTest()
201 : ftp_server_(net::SpawnedTestServer::TYPE_FTP,
202 net::SpawnedTestServer::kLocalhost,
203 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {
204 }
dchenge1bc7982014-10-30 00:32:40205 ~FtpProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05206
dchenge1bc7982014-10-30 00:32:40207 void SetUp() override {
[email protected]b3ae2db2013-05-30 05:00:05208 ASSERT_TRUE(ftp_server_.Start());
209 InProcessBrowserTest::SetUp();
210 }
211
avi556c05022014-12-22 23:31:43212 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]b3ae2db2013-05-30 05:00:05213 base::FilePath pac_script_path(kPACScript);
214 command_line->AppendSwitchASCII(
215 switches::kProxyPacUrl,
216 ftp_server_.GetURL(pac_script_path.MaybeAsASCII()).spec());
217 }
218
219 private:
220 net::SpawnedTestServer ftp_server_;
221
222 DISALLOW_COPY_AND_ASSIGN(FtpProxyScriptBrowserTest);
223};
224
225IN_PROC_BROWSER_TEST_F(FtpProxyScriptBrowserTest, Verify) {
226 VerifyProxyScript(browser());
227}
228
229// Fetch PAC script via a data: URL.
230class DataProxyScriptBrowserTest : public InProcessBrowserTest {
231 public:
232 DataProxyScriptBrowserTest() {}
dchenge1bc7982014-10-30 00:32:40233 ~DataProxyScriptBrowserTest() override {}
[email protected]b3ae2db2013-05-30 05:00:05234
avi556c05022014-12-22 23:31:43235 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]b3ae2db2013-05-30 05:00:05236 std::string contents;
237 // Read in kPACScript contents.
[email protected]82f84b92013-08-30 18:23:50238 ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath(
[email protected]b3ae2db2013-05-30 05:00:05239 base::FilePath(base::FilePath::kCurrentDirectory),
240 base::FilePath(kPACScript)),
241 &contents));
242 command_line->AppendSwitchASCII(switches::kProxyPacUrl,
243 std::string("data:,") + contents);
244 }
245
246 private:
247 DISALLOW_COPY_AND_ASSIGN(DataProxyScriptBrowserTest);
248};
249
250IN_PROC_BROWSER_TEST_F(DataProxyScriptBrowserTest, Verify) {
251 VerifyProxyScript(browser());
252}
253
amistry467cdc72015-03-13 01:58:47254// Fetch PAC script via a data: URL and run out-of-process using Mojo.
255class OutOfProcessProxyResolverBrowserTest : public InProcessBrowserTest {
256 public:
257 OutOfProcessProxyResolverBrowserTest() {}
258 ~OutOfProcessProxyResolverBrowserTest() override {}
259
260 void SetUpCommandLine(base::CommandLine* command_line) override {
261 std::string contents;
262 // Read in kPACScript contents.
263 ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath(
264 base::FilePath(base::FilePath::kCurrentDirectory),
265 base::FilePath(kPACScript)),
266 &contents));
267 command_line->AppendSwitchASCII(
268 switches::kProxyPacUrl, "data:," + contents);
269 command_line->AppendSwitch(switches::kV8PacMojoOutOfProcess);
270 }
271
272 private:
273 DISALLOW_COPY_AND_ASSIGN(OutOfProcessProxyResolverBrowserTest);
274};
275
276IN_PROC_BROWSER_TEST_F(OutOfProcessProxyResolverBrowserTest, Verify) {
277 VerifyProxyScript(browser());
278}
279
[email protected]d46ca7302012-09-08 17:37:24280} // namespace