1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/net/proxy_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/login/login_handler.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "google_apis/gaia/gaia_urls.h"
#include "net/base/load_flags.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/simple_connection_listener.h"
#include "net/test/spawned_test_server/spawned_test_server.h"
#include "net/test/test_data_directory.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/chromeos/net/dhcp_wpad_url_client.h"
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
namespace {
// Verify kPACScript is installed as the PAC script.
void VerifyProxyScript(Browser* browser) {
ui_test_utils::NavigateToURL(browser, GURL("http://google.com"));
// Verify we get the ERR_PROXY_CONNECTION_FAILED screen.
EXPECT_EQ(true, content::EvalJs(
browser->tab_strip_model()->GetActiveWebContents(),
"var textContent = document.body.textContent;"
"var hasError = "
"textContent.indexOf('ERR_PROXY_CONNECTION_FAILED') >= 0;"
"hasError;"));
}
// This class observes chrome::NOTIFICATION_AUTH_NEEDED and supplies
// the credential which is required by the test proxy server.
// "foo:bar" is the required username and password for our test proxy server.
class LoginPromptObserver : public content::NotificationObserver {
public:
LoginPromptObserver() : auth_handled_(false) {}
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override {
if (type == chrome::NOTIFICATION_AUTH_NEEDED) {
LoginNotificationDetails* login_details =
content::Details<LoginNotificationDetails>(details).ptr();
// |login_details->handler()| is the associated LoginHandler object.
// SetAuth() will close the login dialog.
login_details->handler()->SetAuth(u"foo", u"bar");
auth_handled_ = true;
}
}
bool auth_handled() const { return auth_handled_; }
private:
bool auth_handled_;
DISALLOW_COPY_AND_ASSIGN(LoginPromptObserver);
};
// Test that the browser can establish a WebSocket connection via a proxy
// that requires basic authentication. This test also checks the headers
// arrive at WebSocket server.
IN_PROC_BROWSER_TEST_F(ProxyBrowserTest, BasicAuthWSConnect) {
// Launch WebSocket server.
net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
net::GetWebSocketTestDataDirectory());
ASSERT_TRUE(ws_server.Start());
content::WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
content::NavigationController* controller = &tab->GetController();
content::NotificationRegistrar registrar;
// The proxy server will request basic authentication.
// |observer| supplies the credential.
LoginPromptObserver observer;
registrar.Add(&observer, chrome::NOTIFICATION_AUTH_NEEDED,
content::Source<content::NavigationController>(controller));
content::TitleWatcher watcher(tab, u"PASS");
watcher.AlsoWaitForTitle(u"FAIL");
// Visit a page that tries to establish WebSocket connection. The title
// of the page will be 'PASS' on success.
GURL::Replacements replacements;
replacements.SetSchemeStr("http");
ui_test_utils::NavigateToURL(browser(),
ws_server.GetURL("proxied_request_check.html")
.ReplaceComponents(replacements));
const std::u16string result = watcher.WaitAndGetTitle();
EXPECT_TRUE(base::EqualsASCII(result, "PASS"));
EXPECT_TRUE(observer.auth_handled());
}
// Fetches a PAC script via an http:// URL, and ensures that requests to
// http://www.google.com fail with ERR_PROXY_CONNECTION_FAILED (by virtue of
// PAC file having selected a non-existent PROXY server).
class BaseHttpProxyScriptBrowserTest : public InProcessBrowserTest {
public:
BaseHttpProxyScriptBrowserTest() {
http_server_.ServeFilesFromSourceDirectory(GetChromeTestDataDir());
}
~BaseHttpProxyScriptBrowserTest() override {}
void SetUp() override {
ASSERT_TRUE(http_server_.Start());
InProcessBrowserTest::SetUp();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII(
switches::kProxyPacUrl,
http_server_.GetURL("/" + GetPacFilename()).spec());
}
protected:
virtual std::string GetPacFilename() = 0;
net::EmbeddedTestServer http_server_;
private:
DISALLOW_COPY_AND_ASSIGN(BaseHttpProxyScriptBrowserTest);
};
// Tests the use of a PAC script that rejects requests to http://www.google.com/
class HttpProxyScriptBrowserTest : public BaseHttpProxyScriptBrowserTest {
public:
HttpProxyScriptBrowserTest() = default;
~HttpProxyScriptBrowserTest() override {}
std::string GetPacFilename() override {
// PAC script that sends all requests to an invalid proxy server.
return "bad_server.pac";
}
private:
DISALLOW_COPY_AND_ASSIGN(HttpProxyScriptBrowserTest);
};
IN_PROC_BROWSER_TEST_F(HttpProxyScriptBrowserTest, Verify) {
VerifyProxyScript(browser());
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
// Tests the use of a PAC script set via Web Proxy Autodiscovery Protocol.
// TODO(crbug.com/991867): Add a test case for when DhcpWpadUrlClient
// returns an empty PAC URL.
class WPADHttpProxyScriptBrowserTest : public HttpProxyScriptBrowserTest {
public:
WPADHttpProxyScriptBrowserTest() = default;
~WPADHttpProxyScriptBrowserTest() override {}
void SetUp() override {
ASSERT_TRUE(http_server_.Start());
pac_url_ = http_server_.GetURL("/" + GetPacFilename());
chromeos::DhcpWpadUrlClient::SetPacUrlForTesting(pac_url_);
InProcessBrowserTest::SetUp();
}
void TearDown() override {
chromeos::DhcpWpadUrlClient::ClearPacUrlForTesting();
InProcessBrowserTest::TearDown();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(switches::kProxyAutoDetect);
}
private:
GURL pac_url_;
DISALLOW_COPY_AND_ASSIGN(WPADHttpProxyScriptBrowserTest);
};
IN_PROC_BROWSER_TEST_F(WPADHttpProxyScriptBrowserTest, Verify) {
VerifyProxyScript(browser());
}
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
// Tests the use of a PAC script that rejects requests to http://www.google.com/
// when myIpAddress() and myIpAddressEx() appear to be working.
class MyIpAddressProxyScriptBrowserTest
: public BaseHttpProxyScriptBrowserTest {
public:
MyIpAddressProxyScriptBrowserTest() = default;
~MyIpAddressProxyScriptBrowserTest() override {}
std::string GetPacFilename() override {
// PAC script that sends all requests to an invalid proxy server provided
// myIpAddress() and myIpAddressEx() are not loopback addresses.
return "my_ip_address.pac";
}
private:
DISALLOW_COPY_AND_ASSIGN(MyIpAddressProxyScriptBrowserTest);
};
IN_PROC_BROWSER_TEST_F(MyIpAddressProxyScriptBrowserTest, Verify) {
VerifyProxyScript(browser());
}
// Fetch PAC script via a hanging http:// URL.
class HangingPacRequestProxyScriptBrowserTest : public InProcessBrowserTest {
public:
HangingPacRequestProxyScriptBrowserTest() {}
~HangingPacRequestProxyScriptBrowserTest() override {}
void SetUp() override {
// Must start listening (And get a port for the proxy) before calling
// SetUp().
ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
InProcessBrowserTest::SetUp();
}
void TearDown() override {
// Need to stop this before |connection_listener_| is destroyed.
EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
InProcessBrowserTest::TearDown();
}
void SetUpOnMainThread() override {
// This must be created after the main message loop has been set up.
// Waits for one connection. Additional connections are fine.
connection_listener_ =
std::make_unique<net::test_server::SimpleConnectionListener>(
1, net::test_server::SimpleConnectionListener::
ALLOW_ADDITIONAL_CONNECTIONS);
embedded_test_server()->SetConnectionListener(connection_listener_.get());
embedded_test_server()->StartAcceptingConnections();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII(
switches::kProxyPacUrl, embedded_test_server()->GetURL("/hung").spec());
}
protected:
std::unique_ptr<net::test_server::SimpleConnectionListener>
connection_listener_;
private:
DISALLOW_COPY_AND_ASSIGN(HangingPacRequestProxyScriptBrowserTest);
};
// Check that the URLRequest for a PAC that is still alive during shutdown is
// safely cleaned up. This test relies on AssertNoURLRequests being called on
// the main URLRequestContext.
IN_PROC_BROWSER_TEST_F(HangingPacRequestProxyScriptBrowserTest, Shutdown) {
// Request that should hang while trying to request the PAC script.
// Enough requests are created on startup that this probably isn't needed, but
// best to be safe.
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = GURL("http://blah/");
auto simple_loader = network::SimpleURLLoader::Create(
std::move(resource_request), TRAFFIC_ANNOTATION_FOR_TESTS);
auto* storage_partition = browser()->profile()->GetDefaultStoragePartition();
auto url_loader_factory =
storage_partition->GetURLLoaderFactoryForBrowserProcess();
simple_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
url_loader_factory.get(),
base::BindOnce([](std::unique_ptr<std::string> body) {
ADD_FAILURE() << "This request should never complete.";
}));
connection_listener_->WaitForConnections();
}
} // namespace
|