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(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.
109 IN_PROC_BROWSER_TEST_F(ProxyBrowserTest
, MAYBE_BasicAuthWSConnect
) {
110 // Launch WebSocket server.
111 net::SpawnedTestServer
ws_server(net::SpawnedTestServer::TYPE_WS
,
112 net::SpawnedTestServer::kLocalhost
,
113 net::GetWebSocketTestDataDirectory());
114 ASSERT_TRUE(ws_server
.Start());
116 content::WebContents
* tab
=
117 browser()->tab_strip_model()->GetActiveWebContents();
118 content::NavigationController
* controller
= &tab
->GetController();
119 content::NotificationRegistrar registrar
;
120 // The proxy server will request basic authentication.
121 // |observer| supplies the credential.
122 LoginPromptObserver observer
;
123 registrar
.Add(&observer
, chrome::NOTIFICATION_AUTH_NEEDED
,
124 content::Source
<content::NavigationController
>(controller
));
126 content::TitleWatcher
watcher(tab
, base::ASCIIToUTF16("PASS"));
127 watcher
.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
129 // Visit a page that tries to establish WebSocket connection. The title
130 // of the page will be 'PASS' on success.
131 std::string
scheme("http");
132 GURL::Replacements replacements
;
133 replacements
.SetSchemeStr(scheme
);
134 ui_test_utils::NavigateToURL(
136 ws_server
.GetURL("connect_check.html").ReplaceComponents(replacements
));
138 const base::string16 result
= watcher
.WaitAndGetTitle();
139 EXPECT_TRUE(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(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(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(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(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());