1 // 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.
5 #include "base/command_line.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/login/login_prompt.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_source.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "net/base/test_data_directory.h"
23 #include "net/test/spawned_test_server/spawned_test_server.h"
27 // PAC script that sends all requests to an invalid proxy server.
28 const base::FilePath::CharType kPACScript
[] = FILE_PATH_LITERAL(
31 // Verify kPACScript is installed as the PAC script.
32 void VerifyProxyScript(Browser
* browser
) {
33 ui_test_utils::NavigateToURL(browser
, GURL("http://google.com"));
35 // Verify we get the ERR_PROXY_CONNECTION_FAILED screen.
37 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
38 browser
->tab_strip_model()->GetActiveWebContents(),
39 "var textContent = document.body.textContent;"
40 "var hasError = textContent.indexOf('ERR_PROXY_CONNECTION_FAILED') >= 0;"
41 "domAutomationController.send(hasError);",
46 // This class observes chrome::NOTIFICATION_AUTH_NEEDED and supplies
47 // the credential which is required by the test proxy server.
48 // "foo:bar" is the required username and password for our test proxy server.
49 class LoginPromptObserver
: public content::NotificationObserver
{
51 LoginPromptObserver() : auth_handled_(false) {}
53 void Observe(int type
,
54 const content::NotificationSource
& source
,
55 const content::NotificationDetails
& details
) override
{
56 if (type
== chrome::NOTIFICATION_AUTH_NEEDED
) {
57 LoginNotificationDetails
* login_details
=
58 content::Details
<LoginNotificationDetails
>(details
).ptr();
59 // |login_details->handler()| is the associated LoginHandler object.
60 // SetAuth() will close the login dialog.
61 login_details
->handler()->SetAuth(base::ASCIIToUTF16("foo"),
62 base::ASCIIToUTF16("bar"));
67 bool auth_handled() const { return auth_handled_
; }
72 DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver
);
75 class ProxyBrowserTest
: public InProcessBrowserTest
{
78 : proxy_server_(net::SpawnedTestServer::TYPE_BASIC_AUTH_PROXY
,
79 net::SpawnedTestServer::kLocalhost
,
83 void SetUp() override
{
84 ASSERT_TRUE(proxy_server_
.Start());
85 InProcessBrowserTest::SetUp();
88 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
89 command_line
->AppendSwitchASCII(switches::kProxyServer
,
90 proxy_server_
.host_port_pair().ToString());
94 net::SpawnedTestServer proxy_server_
;
98 DISALLOW_COPY_AND_ASSIGN(ProxyBrowserTest
);
101 #if defined(OS_CHROMEOS)
102 // We bypass manually installed proxy for localhost on chromeos.
103 #define MAYBE_BasicAuthWSConnect DISABLED_BasicAuthWSConnect
105 #define MAYBE_BasicAuthWSConnect BasicAuthWSConnect
107 // Test that the browser can establish a WebSocket connection via a proxy
108 // that requires basic authentication. This test also checks the headers
109 // arrive at WebSocket server.
110 IN_PROC_BROWSER_TEST_F(ProxyBrowserTest
, MAYBE_BasicAuthWSConnect
) {
111 // Launch WebSocket server.
112 net::SpawnedTestServer
ws_server(net::SpawnedTestServer::TYPE_WS
,
113 net::SpawnedTestServer::kLocalhost
,
114 net::GetWebSocketTestDataDirectory());
115 ASSERT_TRUE(ws_server
.Start());
117 content::WebContents
* tab
=
118 browser()->tab_strip_model()->GetActiveWebContents();
119 content::NavigationController
* controller
= &tab
->GetController();
120 content::NotificationRegistrar registrar
;
121 // The proxy server will request basic authentication.
122 // |observer| supplies the credential.
123 LoginPromptObserver observer
;
124 registrar
.Add(&observer
, chrome::NOTIFICATION_AUTH_NEEDED
,
125 content::Source
<content::NavigationController
>(controller
));
127 content::TitleWatcher
watcher(tab
, base::ASCIIToUTF16("PASS"));
128 watcher
.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
130 // Visit a page that tries to establish WebSocket connection. The title
131 // of the page will be 'PASS' on success.
132 GURL::Replacements replacements
;
133 replacements
.SetSchemeStr("http");
134 ui_test_utils::NavigateToURL(browser(),
135 ws_server
.GetURL("proxied_request_check.html")
136 .ReplaceComponents(replacements
));
138 const base::string16 result
= watcher
.WaitAndGetTitle();
139 EXPECT_TRUE(base::EqualsASCII(result
, "PASS"));
140 EXPECT_TRUE(observer
.auth_handled());
143 // Fetch PAC script via an http:// URL.
144 class HttpProxyScriptBrowserTest
: public InProcessBrowserTest
{
146 HttpProxyScriptBrowserTest()
147 : http_server_(net::SpawnedTestServer::TYPE_HTTP
,
148 net::SpawnedTestServer::kLocalhost
,
149 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {
151 ~HttpProxyScriptBrowserTest() override
{}
153 void SetUp() override
{
154 ASSERT_TRUE(http_server_
.Start());
155 InProcessBrowserTest::SetUp();
158 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
159 base::FilePath
pac_script_path(FILE_PATH_LITERAL("files"));
160 command_line
->AppendSwitchASCII(switches::kProxyPacUrl
, http_server_
.GetURL(
161 pac_script_path
.Append(kPACScript
).MaybeAsASCII()).spec());
165 net::SpawnedTestServer http_server_
;
167 DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest
);
170 IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest
, Verify
) {
171 VerifyProxyScript(browser());
174 // Fetch PAC script via a file:// URL.
175 class FileProxyScriptBrowserTest
: public InProcessBrowserTest
{
177 FileProxyScriptBrowserTest() {}
178 ~FileProxyScriptBrowserTest() override
{}
180 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
181 command_line
->AppendSwitchASCII(switches::kProxyPacUrl
,
182 ui_test_utils::GetTestUrl(
183 base::FilePath(base::FilePath::kCurrentDirectory
),
184 base::FilePath(kPACScript
)).spec());
188 DISALLOW_COPY_AND_ASSIGN(FileProxyScriptBrowserTest
);
191 IN_PROC_BROWSER_TEST_F(FileProxyScriptBrowserTest
, Verify
) {
192 VerifyProxyScript(browser());
195 // Fetch PAC script via an ftp:// URL.
196 class FtpProxyScriptBrowserTest
: public InProcessBrowserTest
{
198 FtpProxyScriptBrowserTest()
199 : ftp_server_(net::SpawnedTestServer::TYPE_FTP
,
200 net::SpawnedTestServer::kLocalhost
,
201 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))) {
203 ~FtpProxyScriptBrowserTest() override
{}
205 void SetUp() override
{
206 ASSERT_TRUE(ftp_server_
.Start());
207 InProcessBrowserTest::SetUp();
210 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
211 base::FilePath
pac_script_path(kPACScript
);
212 command_line
->AppendSwitchASCII(
213 switches::kProxyPacUrl
,
214 ftp_server_
.GetURL(pac_script_path
.MaybeAsASCII()).spec());
218 net::SpawnedTestServer ftp_server_
;
220 DISALLOW_COPY_AND_ASSIGN(FtpProxyScriptBrowserTest
);
223 IN_PROC_BROWSER_TEST_F(FtpProxyScriptBrowserTest
, Verify
) {
224 VerifyProxyScript(browser());
227 // Fetch PAC script via a data: URL.
228 class DataProxyScriptBrowserTest
: public InProcessBrowserTest
{
230 DataProxyScriptBrowserTest() {}
231 ~DataProxyScriptBrowserTest() override
{}
233 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
234 std::string contents
;
235 // Read in kPACScript contents.
236 ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath(
237 base::FilePath(base::FilePath::kCurrentDirectory
),
238 base::FilePath(kPACScript
)),
240 command_line
->AppendSwitchASCII(switches::kProxyPacUrl
,
241 std::string("data:,") + contents
);
245 DISALLOW_COPY_AND_ASSIGN(DataProxyScriptBrowserTest
);
248 IN_PROC_BROWSER_TEST_F(DataProxyScriptBrowserTest
, Verify
) {
249 VerifyProxyScript(browser());
252 // Fetch PAC script via a data: URL and run out-of-process using Mojo.
253 class OutOfProcessProxyResolverBrowserTest
: public InProcessBrowserTest
{
255 OutOfProcessProxyResolverBrowserTest() {}
256 ~OutOfProcessProxyResolverBrowserTest() override
{}
258 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
259 std::string contents
;
260 // Read in kPACScript contents.
261 ASSERT_TRUE(base::ReadFileToString(ui_test_utils::GetTestFilePath(
262 base::FilePath(base::FilePath::kCurrentDirectory
),
263 base::FilePath(kPACScript
)),
265 command_line
->AppendSwitchASCII(
266 switches::kProxyPacUrl
, "data:," + contents
);
267 command_line
->AppendSwitch(switches::kV8PacMojoOutOfProcess
);
271 DISALLOW_COPY_AND_ASSIGN(OutOfProcessProxyResolverBrowserTest
);
274 IN_PROC_BROWSER_TEST_F(OutOfProcessProxyResolverBrowserTest
, Verify
) {
275 VerifyProxyScript(browser());