blob: 578a01c1401dc06c00458b2ebf03d719edb58653 [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
[email protected]d46ca7302012-09-08 17:37:24112// We bypass manually installed proxy for localhost on chromeos.
Adam Rice2f3efd62018-04-10 13:10:00113#if defined(OS_CHROMEOS)
[email protected]d46ca7302012-09-08 17:37:24114#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
mmenke0a8ca0d12017-05-03 18:40:07182// Fetch PAC script via a hanging http:// URL.
183class HangingPacRequestProxyScriptBrowserTest : public InProcessBrowserTest {
184 public:
185 HangingPacRequestProxyScriptBrowserTest() {}
186 ~HangingPacRequestProxyScriptBrowserTest() override {}
187
188 void SetUp() override {
189 // Must start listening (And get a port for the proxy) before calling
190 // SetUp().
191 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
192 InProcessBrowserTest::SetUp();
193 }
194
mmenkeab0c11d2017-05-30 17:15:17195 void TearDown() override {
196 // Need to stop this before |connection_listener_| is destroyed.
197 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
198 InProcessBrowserTest::TearDown();
199 }
200
mmenke0a8ca0d12017-05-03 18:40:07201 void SetUpOnMainThread() override {
202 // This must be created after the main message loop has been set up.
mmenkeab0c11d2017-05-30 17:15:17203 // Waits for one connection. Additional connections are fine.
204 connection_listener_ =
Jeremy Romanec48d7a2018-03-01 17:35:09205 std::make_unique<net::test_server::SimpleConnectionListener>(
mmenkeab0c11d2017-05-30 17:15:17206 1, net::test_server::SimpleConnectionListener::
207 ALLOW_ADDITIONAL_CONNECTIONS);
mmenke0a8ca0d12017-05-03 18:40:07208 embedded_test_server()->SetConnectionListener(connection_listener_.get());
209 embedded_test_server()->StartAcceptingConnections();
mmenke0a8ca0d12017-05-03 18:40:07210 }
211
212 void SetUpCommandLine(base::CommandLine* command_line) override {
213 command_line->AppendSwitchASCII(
214 switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec());
215 }
216
217 protected:
mmenkeab0c11d2017-05-30 17:15:17218 std::unique_ptr<net::test_server::SimpleConnectionListener>
219 connection_listener_;
mmenke0a8ca0d12017-05-03 18:40:07220
221 private:
222 DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest);
223};
224
225// URLFetcherDelegate that expects a request to hang.
226class HangingURLFetcherDelegate : public net::URLFetcherDelegate {
227 public:
228 HangingURLFetcherDelegate() {}
229 ~HangingURLFetcherDelegate() override {}
230
231 void OnURLFetchComplete(const net::URLFetcher* source) override {
232 ADD_FAILURE() << "This request should never complete.";
233 }
234
235 private:
236 DISALLOW_COPY_AND_ASSIGN(HangingURLFetcherDelegate);
237};
238
239// Check that the URLRequest for a PAC that is still alive during shutdown is
240// safely cleaned up. This test relies on AssertNoURLRequests being called on
241// the main URLRequestContext.
242IN_PROC_BROWSER_TEST_F(HangingPacRequestProxyScriptBrowserTest, Shutdown) {
243 // Request that should hang while trying to request the PAC script.
244 // Enough requests are created on startup that this probably isn't needed, but
245 // best to be safe.
246 HangingURLFetcherDelegate hanging_request_delegate;
247 std::unique_ptr<net::URLFetcher> hanging_fetcher = net::URLFetcher::Create(
rhalavati8aa3fbb72017-05-22 23:27:18248 GURL("http://blah/"), net::URLFetcher::GET, &hanging_request_delegate,
249 TRAFFIC_ANNOTATION_FOR_TESTS);
mmenke0a8ca0d12017-05-03 18:40:07250 hanging_fetcher->SetRequestContext(browser()->profile()->GetRequestContext());
251 hanging_fetcher->Start();
252
mmenkeab0c11d2017-05-30 17:15:17253 connection_listener_->WaitForConnections();
mmenke0a8ca0d12017-05-03 18:40:07254}
255
[email protected]d46ca7302012-09-08 17:37:24256} // namespace