blob: aea2424fe6090b6c62b3b0a353214ff329684afb [file] [log] [blame]
ricea433bdab2015-01-26 07:25:371// Copyright 2014 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// End-to-end tests for WebSocket.
6//
7// A python server is (re)started for each test, which is moderately
8// inefficient. However, it makes these tests a good fit for scenarios which
9// require special server configurations.
10
tfarina8a2c66c22015-10-13 19:14:4911#include <stdint.h>
ryansturm4bab06832016-03-03 23:41:0712
danakj9c5cab52016-04-16 00:54:3313#include <memory>
ricea433bdab2015-01-26 07:25:3714#include <string>
Adam Rice5b4a3d82018-08-02 15:28:4315#include <utility>
ricea433bdab2015-01-26 07:25:3716
17#include "base/bind.h"
18#include "base/bind_helpers.h"
19#include "base/callback.h"
skyostil4891b25b2015-06-11 11:43:4520#include "base/location.h"
Avi Drissman13fc8932015-12-20 04:40:4621#include "base/macros.h"
danakj9c5cab52016-04-16 00:54:3322#include "base/memory/ptr_util.h"
Bence Béky65623972018-03-05 15:31:5623#include "base/memory/scoped_refptr.h"
ricea433bdab2015-01-26 07:25:3724#include "base/run_loop.h"
skyostil4891b25b2015-06-11 11:43:4525#include "base/single_thread_task_runner.h"
Adam Rice5b4a3d82018-08-02 15:28:4326#include "base/strings/strcat.h"
Adam Ricecb76ac62015-02-20 05:33:2527#include "base/strings/string_piece.h"
Adam Rice5b4a3d82018-08-02 15:28:4328#include "base/strings/stringprintf.h"
gabf767595f2016-05-11 18:50:3529#include "base/threading/thread_task_runner_handle.h"
Sergey Ulanova337dcd2017-09-08 20:53:1430#include "build/build_config.h"
ricea433bdab2015-01-26 07:25:3731#include "net/base/auth.h"
Adam Rice5b4a3d82018-08-02 15:28:4332#include "net/base/host_port_pair.h"
ryansturm7de050c2016-02-23 00:10:2133#include "net/base/proxy_delegate.h"
Adam Rice5b4a3d82018-08-02 15:28:4334#include "net/base/url_util.h"
Yutaka Hirano2f65eec2018-05-23 01:58:2235#include "net/http/http_request_headers.h"
Adam Rice5b4a3d82018-08-02 15:28:4336#include "net/log/net_log.h"
37#include "net/proxy_resolution/proxy_config.h"
38#include "net/proxy_resolution/proxy_config_service.h"
39#include "net/proxy_resolution/proxy_config_service_fixed.h"
40#include "net/proxy_resolution/proxy_config_with_annotation.h"
41#include "net/proxy_resolution/proxy_info.h"
Lily Houghtonffe89daa02018-03-09 18:30:0342#include "net/proxy_resolution/proxy_resolution_service.h"
tommycli59a63432015-11-06 00:10:5543#include "net/test/embedded_test_server/embedded_test_server.h"
Adam Rice5b4a3d82018-08-02 15:28:4344#include "net/test/embedded_test_server/http_request.h"
45#include "net/test/embedded_test_server/http_response.h"
ricea433bdab2015-01-26 07:25:3746#include "net/test/spawned_test_server/spawned_test_server.h"
rsleevia69c79a2016-06-22 03:28:4347#include "net/test/test_data_directory.h"
Bence Béky98447b12018-05-08 03:14:0148#include "net/test/test_with_scoped_task_environment.h"
rhalavati9ebaba7e2017-04-27 06:16:2949#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
Adam Rice5b4a3d82018-08-02 15:28:4350#include "net/url_request/url_request.h"
51#include "net/url_request/url_request_context.h"
ricea433bdab2015-01-26 07:25:3752#include "net/url_request/url_request_test_util.h"
53#include "net/websockets/websocket_channel.h"
54#include "net/websockets/websocket_event_interface.h"
55#include "testing/gtest/include/gtest/gtest.h"
Adam Rice5b4a3d82018-08-02 15:28:4356#include "url/gurl.h"
mkwst4997ce82015-07-25 12:00:0557#include "url/origin.h"
ricea433bdab2015-01-26 07:25:3758
59namespace net {
60
yhirano4a593832016-10-24 18:58:2261class URLRequest;
62
ricea433bdab2015-01-26 07:25:3763namespace {
64
Adam Rice5b4a3d82018-08-02 15:28:4365using test_server::BasicHttpResponse;
66using test_server::HttpRequest;
67using test_server::HttpResponse;
68
ricea433bdab2015-01-26 07:25:3769static const char kEchoServer[] = "echo-with-no-extension";
70
Adam Ricecb76ac62015-02-20 05:33:2571// Simplify changing URL schemes.
72GURL ReplaceUrlScheme(const GURL& in_url, const base::StringPiece& scheme) {
73 GURL::Replacements replacements;
74 replacements.SetSchemeStr(scheme);
75 return in_url.ReplaceComponents(replacements);
76}
77
ricea433bdab2015-01-26 07:25:3778// An implementation of WebSocketEventInterface that waits for and records the
79// results of the connect.
80class ConnectTestingEventInterface : public WebSocketEventInterface {
81 public:
82 ConnectTestingEventInterface();
83
84 void WaitForResponse();
85
86 bool failed() const { return failed_; }
87
88 // Only set if the handshake failed, otherwise empty.
89 std::string failure_message() const;
90
91 std::string selected_subprotocol() const;
92
93 std::string extensions() const;
94
95 // Implementation of WebSocketEventInterface.
yhirano4a593832016-10-24 18:58:2296 void OnCreateURLRequest(URLRequest* request) override {}
97
Yutaka Hirano4165de92018-04-10 11:46:4998 void OnAddChannelResponse(const std::string& selected_subprotocol,
99 const std::string& extensions) override;
ricea433bdab2015-01-26 07:25:37100
Yutaka Hirano4165de92018-04-10 11:46:49101 void OnDataFrame(bool fin,
102 WebSocketMessageType type,
103 scoped_refptr<IOBuffer> data,
104 size_t data_size) override;
ricea433bdab2015-01-26 07:25:37105
Yutaka Hirano4165de92018-04-10 11:46:49106 void OnFlowControl(int64_t quota) override;
ricea433bdab2015-01-26 07:25:37107
Yutaka Hirano4165de92018-04-10 11:46:49108 void OnClosingHandshake() override;
ricea433bdab2015-01-26 07:25:37109
Yutaka Hirano4165de92018-04-10 11:46:49110 void OnDropChannel(bool was_clean,
111 uint16_t code,
112 const std::string& reason) override;
ricea433bdab2015-01-26 07:25:37113
Yutaka Hirano4165de92018-04-10 11:46:49114 void OnFailChannel(const std::string& message) override;
ricea433bdab2015-01-26 07:25:37115
Yutaka Hirano4165de92018-04-10 11:46:49116 void OnStartOpeningHandshake(
danakj9c5cab52016-04-16 00:54:33117 std::unique_ptr<WebSocketHandshakeRequestInfo> request) override;
ricea433bdab2015-01-26 07:25:37118
Yutaka Hirano4165de92018-04-10 11:46:49119 void OnFinishOpeningHandshake(
danakj9c5cab52016-04-16 00:54:33120 std::unique_ptr<WebSocketHandshakeResponseInfo> response) override;
ricea433bdab2015-01-26 07:25:37121
Yutaka Hirano4165de92018-04-10 11:46:49122 void OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:33123 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks,
ricea433bdab2015-01-26 07:25:37124 const GURL& url,
125 const SSLInfo& ssl_info,
126 bool fatal) override;
127
Yutaka Hirano70fa25912018-06-06 05:26:54128 int OnAuthRequired(scoped_refptr<AuthChallengeInfo> auth_info,
129 scoped_refptr<HttpResponseHeaders> response_headers,
130 const HostPortPair& host_port_pair,
131 base::OnceCallback<void(const AuthCredentials*)> callback,
132 base::Optional<AuthCredentials>* credentials) override;
133
ricea433bdab2015-01-26 07:25:37134 private:
135 void QuitNestedEventLoop();
136
137 // failed_ is true if the handshake failed (ie. OnFailChannel was called).
138 bool failed_;
139 std::string selected_subprotocol_;
140 std::string extensions_;
141 std::string failure_message_;
142 base::RunLoop run_loop_;
143
144 DISALLOW_COPY_AND_ASSIGN(ConnectTestingEventInterface);
145};
146
tyoshinoc06da562015-03-06 06:02:42147ConnectTestingEventInterface::ConnectTestingEventInterface() : failed_(false) {
ricea433bdab2015-01-26 07:25:37148}
149
150void ConnectTestingEventInterface::WaitForResponse() {
151 run_loop_.Run();
152}
153
154std::string ConnectTestingEventInterface::failure_message() const {
155 return failure_message_;
156}
157
158std::string ConnectTestingEventInterface::selected_subprotocol() const {
159 return selected_subprotocol_;
160}
161
162std::string ConnectTestingEventInterface::extensions() const {
163 return extensions_;
164}
165
Yutaka Hirano4165de92018-04-10 11:46:49166void ConnectTestingEventInterface::OnAddChannelResponse(
ricea433bdab2015-01-26 07:25:37167 const std::string& selected_subprotocol,
168 const std::string& extensions) {
ricea433bdab2015-01-26 07:25:37169 selected_subprotocol_ = selected_subprotocol;
170 extensions_ = extensions;
171 QuitNestedEventLoop();
ricea433bdab2015-01-26 07:25:37172}
173
Yutaka Hirano4165de92018-04-10 11:46:49174void ConnectTestingEventInterface::OnDataFrame(bool fin,
175 WebSocketMessageType type,
176 scoped_refptr<IOBuffer> data,
177 size_t data_size) {}
ricea433bdab2015-01-26 07:25:37178
Yutaka Hirano4165de92018-04-10 11:46:49179void ConnectTestingEventInterface::OnFlowControl(int64_t quota) {}
ricea433bdab2015-01-26 07:25:37180
Yutaka Hirano4165de92018-04-10 11:46:49181void ConnectTestingEventInterface::OnClosingHandshake() {}
ricea433bdab2015-01-26 07:25:37182
Yutaka Hirano4165de92018-04-10 11:46:49183void ConnectTestingEventInterface::OnDropChannel(bool was_clean,
184 uint16_t code,
185 const std::string& reason) {}
ricea433bdab2015-01-26 07:25:37186
Yutaka Hirano4165de92018-04-10 11:46:49187void ConnectTestingEventInterface::OnFailChannel(const std::string& message) {
ricea433bdab2015-01-26 07:25:37188 failed_ = true;
189 failure_message_ = message;
190 QuitNestedEventLoop();
ricea433bdab2015-01-26 07:25:37191}
192
Yutaka Hirano4165de92018-04-10 11:46:49193void ConnectTestingEventInterface::OnStartOpeningHandshake(
194 std::unique_ptr<WebSocketHandshakeRequestInfo> request) {}
ricea433bdab2015-01-26 07:25:37195
Yutaka Hirano4165de92018-04-10 11:46:49196void ConnectTestingEventInterface::OnFinishOpeningHandshake(
197 std::unique_ptr<WebSocketHandshakeResponseInfo> response) {}
ricea433bdab2015-01-26 07:25:37198
Yutaka Hirano4165de92018-04-10 11:46:49199void ConnectTestingEventInterface::OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:33200 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks,
ricea433bdab2015-01-26 07:25:37201 const GURL& url,
202 const SSLInfo& ssl_info,
203 bool fatal) {
skyostil4891b25b2015-06-11 11:43:45204 base::ThreadTaskRunnerHandle::Get()->PostTask(
ricea433bdab2015-01-26 07:25:37205 FROM_HERE, base::Bind(&SSLErrorCallbacks::CancelSSLRequest,
206 base::Owned(ssl_error_callbacks.release()),
207 ERR_SSL_PROTOCOL_ERROR, &ssl_info));
ricea433bdab2015-01-26 07:25:37208}
209
Yutaka Hirano70fa25912018-06-06 05:26:54210int ConnectTestingEventInterface::OnAuthRequired(
211 scoped_refptr<AuthChallengeInfo> auth_info,
212 scoped_refptr<HttpResponseHeaders> response_headers,
213 const HostPortPair& host_port_pair,
214 base::OnceCallback<void(const AuthCredentials*)> callback,
215 base::Optional<AuthCredentials>* credentials) {
216 *credentials = base::nullopt;
217 return OK;
218}
219
ricea433bdab2015-01-26 07:25:37220void ConnectTestingEventInterface::QuitNestedEventLoop() {
221 run_loop_.Quit();
222}
223
224// A subclass of TestNetworkDelegate that additionally implements the
225// OnResolveProxy callback and records the information passed to it.
ryansturm7de050c2016-02-23 00:10:21226class TestProxyDelegateWithProxyInfo : public ProxyDelegate {
ricea433bdab2015-01-26 07:25:37227 public:
Chris Watkins28c2fdd2017-11-30 06:06:52228 TestProxyDelegateWithProxyInfo() = default;
ricea433bdab2015-01-26 07:25:37229
230 struct ResolvedProxyInfo {
231 GURL url;
232 ProxyInfo proxy_info;
233 };
234
235 const ResolvedProxyInfo& resolved_proxy_info() const {
236 return resolved_proxy_info_;
237 }
238
239 protected:
240 void OnResolveProxy(const GURL& url,
ryansturm4bab06832016-03-03 23:41:07241 const std::string& method,
Reilly Grantb414ace72017-11-14 23:03:22242 const ProxyRetryInfoMap& proxy_retry_info,
ricea433bdab2015-01-26 07:25:37243 ProxyInfo* result) override {
244 resolved_proxy_info_.url = url;
245 resolved_proxy_info_.proxy_info = *result;
246 }
247
ryansturm7de050c2016-02-23 00:10:21248 void OnFallback(const ProxyServer& bad_proxy, int net_error) override {}
ryansturm7de050c2016-02-23 00:10:21249
ricea433bdab2015-01-26 07:25:37250 private:
251 ResolvedProxyInfo resolved_proxy_info_;
252
ryansturm7de050c2016-02-23 00:10:21253 DISALLOW_COPY_AND_ASSIGN(TestProxyDelegateWithProxyInfo);
ricea433bdab2015-01-26 07:25:37254};
255
Bence Béky98447b12018-05-08 03:14:01256class WebSocketEndToEndTest : public TestWithScopedTaskEnvironment {
ricea433bdab2015-01-26 07:25:37257 protected:
258 WebSocketEndToEndTest()
Adam Ricecb76ac62015-02-20 05:33:25259 : event_interface_(),
Bence Béky65623972018-03-05 15:31:56260 proxy_delegate_(std::make_unique<TestProxyDelegateWithProxyInfo>()),
ricea433bdab2015-01-26 07:25:37261 context_(true),
Adam Ricecb76ac62015-02-20 05:33:25262 channel_(),
ricea433bdab2015-01-26 07:25:37263 initialised_context_(false) {}
264
265 // Initialise the URLRequestContext. Normally done automatically by
266 // ConnectAndWait(). This method is for the use of tests that need the
267 // URLRequestContext initialised before calling ConnectAndWait().
268 void InitialiseContext() {
ryansturm7de050c2016-02-23 00:10:21269 context_.set_proxy_delegate(proxy_delegate_.get());
ricea433bdab2015-01-26 07:25:37270 context_.Init();
271 initialised_context_ = true;
272 }
273
274 // Send the connect request to |socket_url| and wait for a response. Returns
275 // true if the handshake succeeded.
276 bool ConnectAndWait(const GURL& socket_url) {
277 if (!initialised_context_) {
278 InitialiseContext();
279 }
Daniel Cheng88186bd52017-10-20 08:14:46280 url::Origin origin = url::Origin::Create(GURL("http://localhost"));
Mike Westb85da8ed2017-08-10 14:16:46281 GURL site_for_cookies("http://localhost/");
Adam Rice5b4a3d82018-08-02 15:28:43282 event_interface_ = new ConnectTestingEventInterface();
Bence Béky65623972018-03-05 15:31:56283 channel_ = std::make_unique<WebSocketChannel>(
284 base::WrapUnique(event_interface_), &context_);
alladacef397d2016-06-29 17:52:23285 channel_->SendAddChannelRequest(GURL(socket_url), sub_protocols_, origin,
Yutaka Hirano2f65eec2018-05-23 01:58:22286 site_for_cookies, HttpRequestHeaders());
ricea433bdab2015-01-26 07:25:37287 event_interface_->WaitForResponse();
288 return !event_interface_->failed();
289 }
290
291 ConnectTestingEventInterface* event_interface_; // owned by channel_
danakj9c5cab52016-04-16 00:54:33292 std::unique_ptr<TestProxyDelegateWithProxyInfo> proxy_delegate_;
ricea433bdab2015-01-26 07:25:37293 TestURLRequestContext context_;
danakj9c5cab52016-04-16 00:54:33294 std::unique_ptr<WebSocketChannel> channel_;
ricea5acb1faf72015-03-16 15:34:00295 std::vector<std::string> sub_protocols_;
ricea433bdab2015-01-26 07:25:37296 bool initialised_context_;
297};
298
ricea433bdab2015-01-26 07:25:37299// Basic test of connectivity. If this test fails, nothing else can be expected
300// to work.
Sergey Ulanov4c786d32017-09-08 22:53:25301TEST_F(WebSocketEndToEndTest, BasicSmokeTest) {
ricea433bdab2015-01-26 07:25:37302 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37303 GetWebSocketTestDataDirectory());
304 ASSERT_TRUE(ws_server.Start());
305 EXPECT_TRUE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
306}
307
308// Test for issue crbug.com/433695 "Unencrypted WebSocket connection via
309// authenticated proxy times out"
310// TODO(ricea): Enable this when the issue is fixed.
311TEST_F(WebSocketEndToEndTest, DISABLED_HttpsProxyUnauthedFails) {
312 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37313 base::FilePath());
314 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37315 GetWebSocketTestDataDirectory());
316 ASSERT_TRUE(proxy_server.StartInBackground());
317 ASSERT_TRUE(ws_server.StartInBackground());
318 ASSERT_TRUE(proxy_server.BlockUntilStarted());
319 ASSERT_TRUE(ws_server.BlockUntilStarted());
320 std::string proxy_config =
321 "https=" + proxy_server.host_port_pair().ToString();
Lily Houghton8c2f97d2018-01-22 05:06:59322 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Ramin Halavatica8d5252018-03-12 05:33:49323 ProxyResolutionService::CreateFixed(proxy_config,
324 TRAFFIC_ANNOTATION_FOR_TESTS));
Lily Houghton8c2f97d2018-01-22 05:06:59325 ASSERT_TRUE(proxy_resolution_service);
326 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37327 EXPECT_FALSE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
328 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
329}
330
Sergey Ulanov4c786d32017-09-08 22:53:25331// These test are not compatible with RemoteTestServer because RemoteTestServer
332// doesn't support TYPE_BASIC_AUTH_PROXY.
333// TODO(ricea): Make these tests work. See crbug.com/441711.
334#if defined(OS_ANDROID) || defined(OS_FUCHSIA)
335#define MAYBE_HttpsWssProxyUnauthedFails DISABLED_HttpsWssProxyUnauthedFails
336#define MAYBE_HttpsProxyUsed DISABLED_HttpsProxyUsed
337#else
338#define MAYBE_HttpsWssProxyUnauthedFails HttpsWssProxyUnauthedFails
339#define MAYBE_HttpsProxyUsed HttpsProxyUsed
340#endif
341
342TEST_F(WebSocketEndToEndTest, MAYBE_HttpsWssProxyUnauthedFails) {
ricea433bdab2015-01-26 07:25:37343 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37344 base::FilePath());
345 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS,
ricea433bdab2015-01-26 07:25:37346 GetWebSocketTestDataDirectory());
347 ASSERT_TRUE(proxy_server.StartInBackground());
348 ASSERT_TRUE(wss_server.StartInBackground());
349 ASSERT_TRUE(proxy_server.BlockUntilStarted());
350 ASSERT_TRUE(wss_server.BlockUntilStarted());
351 std::string proxy_config =
352 "https=" + proxy_server.host_port_pair().ToString();
Lily Houghton8c2f97d2018-01-22 05:06:59353 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Ramin Halavatica8d5252018-03-12 05:33:49354 ProxyResolutionService::CreateFixed(proxy_config,
355 TRAFFIC_ANNOTATION_FOR_TESTS));
Lily Houghton8c2f97d2018-01-22 05:06:59356 ASSERT_TRUE(proxy_resolution_service);
357 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37358 EXPECT_FALSE(ConnectAndWait(wss_server.GetURL(kEchoServer)));
359 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
360}
361
362// Regression test for crbug/426736 "WebSocket connections not using configured
363// system HTTPS Proxy".
Sergey Ulanov4c786d32017-09-08 22:53:25364TEST_F(WebSocketEndToEndTest, MAYBE_HttpsProxyUsed) {
Adam Rice5b4a3d82018-08-02 15:28:43365 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
ricea433bdab2015-01-26 07:25:37366 base::FilePath());
367 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37368 GetWebSocketTestDataDirectory());
369 ASSERT_TRUE(proxy_server.StartInBackground());
370 ASSERT_TRUE(ws_server.StartInBackground());
371 ASSERT_TRUE(proxy_server.BlockUntilStarted());
372 ASSERT_TRUE(ws_server.BlockUntilStarted());
373 std::string proxy_config = "https=" +
374 proxy_server.host_port_pair().ToString() + ";" +
375 "http=" + proxy_server.host_port_pair().ToString();
Lily Houghton8c2f97d2018-01-22 05:06:59376 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Ramin Halavatica8d5252018-03-12 05:33:49377 ProxyResolutionService::CreateFixed(proxy_config,
378 TRAFFIC_ANNOTATION_FOR_TESTS));
Lily Houghton8c2f97d2018-01-22 05:06:59379 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37380 InitialiseContext();
381
ricea433bdab2015-01-26 07:25:37382 GURL ws_url = ws_server.GetURL(kEchoServer);
383 EXPECT_TRUE(ConnectAndWait(ws_url));
ryansturm7de050c2016-02-23 00:10:21384 const TestProxyDelegateWithProxyInfo::ResolvedProxyInfo& info =
385 proxy_delegate_->resolved_proxy_info();
ricea433bdab2015-01-26 07:25:37386 EXPECT_EQ(ws_url, info.url);
387 EXPECT_TRUE(info.proxy_info.is_http());
388}
389
Adam Rice5b4a3d82018-08-02 15:28:43390std::unique_ptr<HttpResponse> ProxyPacHandler(const HttpRequest& request) {
391 GURL url = request.GetURL();
392 EXPECT_EQ(url.path_piece(), "/proxy.pac");
393 EXPECT_TRUE(url.has_query());
394 std::string proxy;
395 EXPECT_TRUE(GetValueForKeyInQuery(url, "proxy", &proxy));
396 auto response = std::make_unique<BasicHttpResponse>();
397 response->set_content_type("application/x-ns-proxy-autoconfig");
398 response->set_content(
399 base::StringPrintf("function FindProxyForURL(url, host) {\n"
400 " return 'PROXY %s';\n"
401 "}\n",
402 proxy.c_str()));
403 return response;
404}
405
406// This tests the proxy.pac resolver that is built into the system. This is not
407// the one that Chrome normally uses. Chrome's normal implementation is defined
408// as a mojo service. It is outside //net and we can't use it from here. This
409// tests the alternative implementations that are selected when the
410// --winhttp-proxy-resolver flag is provided to Chrome. These only exist on OS X
411// and Windows.
412// TODO(ricea): Remove this test if --winhttp-proxy-resolver flag is removed.
413// See crbug.com/644030.
414
415#if defined(OS_WIN) || defined(OS_MACOSX)
416#define MAYBE_ProxyPacUsed ProxyPacUsed
417#else
418#define MAYBE_ProxyPacUsed DISABLED_ProxyPacUsed
419#endif
420
421TEST_F(WebSocketEndToEndTest, MAYBE_ProxyPacUsed) {
422 EmbeddedTestServer proxy_pac_server(net::EmbeddedTestServer::Type::TYPE_HTTP);
423 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
424 base::FilePath());
425 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
426 GetWebSocketTestDataDirectory());
427 proxy_pac_server.RegisterRequestHandler(base::BindRepeating(ProxyPacHandler));
428 proxy_server.set_redirect_connect_to_localhost(true);
429
430 ASSERT_TRUE(proxy_pac_server.Start());
431 ASSERT_TRUE(proxy_server.StartInBackground());
432 ASSERT_TRUE(ws_server.StartInBackground());
433 ASSERT_TRUE(proxy_server.BlockUntilStarted());
434 ASSERT_TRUE(ws_server.BlockUntilStarted());
435
436 ProxyConfig proxy_config =
437 ProxyConfig::CreateFromCustomPacURL(proxy_pac_server.GetURL(base::StrCat(
438 {"/proxy.pac?proxy=", proxy_server.host_port_pair().ToString()})));
439 proxy_config.set_pac_mandatory(true);
440 auto proxy_config_service = std::make_unique<ProxyConfigServiceFixed>(
441 ProxyConfigWithAnnotation(proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS));
442 NetLog net_log;
443 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
444 ProxyResolutionService::CreateUsingSystemProxyResolver(
445 std::move(proxy_config_service), &net_log));
446 ASSERT_EQ(ws_server.host_port_pair().host(), "127.0.0.1");
447 context_.set_proxy_resolution_service(proxy_resolution_service.get());
448 InitialiseContext();
449
450 // We need to use something that doesn't look like localhost, or Windows'
451 // resolver will send us direct regardless of what proxy.pac says.
452 HostPortPair fake_ws_host_port_pair("stealth-localhost",
453 ws_server.host_port_pair().port());
454
455 GURL ws_url(base::StrCat(
456 {"ws://", fake_ws_host_port_pair.ToString(), "/", kEchoServer}));
457 EXPECT_TRUE(ConnectAndWait(ws_url));
458 const auto& info = proxy_delegate_->resolved_proxy_info();
459 EXPECT_EQ(ws_url, info.url);
460 EXPECT_TRUE(info.proxy_info.is_http());
461 EXPECT_EQ(info.proxy_info.ToPacString(),
462 base::StrCat({"PROXY ", proxy_server.host_port_pair().ToString()}));
463}
464
ricea23c3f942015-02-02 13:35:13465// This is a regression test for crbug.com/408061 Crash in
466// net::WebSocketBasicHandshakeStream::Upgrade.
Sergey Ulanov4c786d32017-09-08 22:53:25467TEST_F(WebSocketEndToEndTest, TruncatedResponse) {
ricea23c3f942015-02-02 13:35:13468 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea23c3f942015-02-02 13:35:13469 GetWebSocketTestDataDirectory());
470 ASSERT_TRUE(ws_server.Start());
471 InitialiseContext();
472
473 GURL ws_url = ws_server.GetURL("truncated-headers");
474 EXPECT_FALSE(ConnectAndWait(ws_url));
475}
476
Adam Ricecb76ac62015-02-20 05:33:25477// Regression test for crbug.com/455215 "HSTS not applied to WebSocket"
Sergey Ulanov4c786d32017-09-08 22:53:25478TEST_F(WebSocketEndToEndTest, HstsHttpsToWebSocket) {
tommycli59a63432015-11-06 00:10:55479 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
480 https_server.SetSSLConfig(
481 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
482 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
483
estarka5da76702015-04-09 04:00:16484 SpawnedTestServer::SSLOptions ssl_options(
485 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25486 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
487 GetWebSocketTestDataDirectory());
estarka5da76702015-04-09 04:00:16488
tommycli59a63432015-11-06 00:10:55489 ASSERT_TRUE(https_server.Start());
490 ASSERT_TRUE(wss_server.Start());
Adam Ricecb76ac62015-02-20 05:33:25491 InitialiseContext();
492 // Set HSTS via https:
493 TestDelegate delegate;
tommycli59a63432015-11-06 00:10:55494 GURL https_page = https_server.GetURL("/hsts-headers.html");
rhalavati9ebaba7e2017-04-27 06:16:29495 std::unique_ptr<URLRequest> request(context_.CreateRequest(
496 https_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
Adam Ricecb76ac62015-02-20 05:33:25497 request->Start();
Wez2a31b222018-06-07 22:07:15498 delegate.RunUntilComplete();
maksim.sisov7f60c8e2016-09-16 19:38:17499 EXPECT_EQ(OK, delegate.request_status());
Adam Ricecb76ac62015-02-20 05:33:25500
501 // Check HSTS with ws:
502 // Change the scheme from wss: to ws: to verify that it is switched back.
503 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
504 EXPECT_TRUE(ConnectAndWait(ws_url));
505}
506
Sergey Ulanov4c786d32017-09-08 22:53:25507TEST_F(WebSocketEndToEndTest, HstsWebSocketToHttps) {
tommycli59a63432015-11-06 00:10:55508 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
509 https_server.SetSSLConfig(
510 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
511 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
512
estarka5da76702015-04-09 04:00:16513 SpawnedTestServer::SSLOptions ssl_options(
514 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25515 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
516 GetWebSocketTestDataDirectory());
tommycli59a63432015-11-06 00:10:55517 ASSERT_TRUE(https_server.Start());
518 ASSERT_TRUE(wss_server.Start());
Adam Ricecb76ac62015-02-20 05:33:25519 InitialiseContext();
520 // Set HSTS via wss:
521 GURL wss_url = wss_server.GetURL("set-hsts");
522 EXPECT_TRUE(ConnectAndWait(wss_url));
523
524 // Verify via http:
525 TestDelegate delegate;
526 GURL http_page =
tommycli59a63432015-11-06 00:10:55527 ReplaceUrlScheme(https_server.GetURL("/simple.html"), "http");
rhalavati9ebaba7e2017-04-27 06:16:29528 std::unique_ptr<URLRequest> request(context_.CreateRequest(
529 http_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
Adam Ricecb76ac62015-02-20 05:33:25530 request->Start();
Wez2a31b222018-06-07 22:07:15531 delegate.RunUntilComplete();
maksim.sisov7f60c8e2016-09-16 19:38:17532 EXPECT_EQ(OK, delegate.request_status());
Adam Ricecb76ac62015-02-20 05:33:25533 EXPECT_TRUE(request->url().SchemeIs("https"));
534}
535
Sergey Ulanov4c786d32017-09-08 22:53:25536TEST_F(WebSocketEndToEndTest, HstsWebSocketToWebSocket) {
estarka5da76702015-04-09 04:00:16537 SpawnedTestServer::SSLOptions ssl_options(
538 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25539 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
540 GetWebSocketTestDataDirectory());
541 ASSERT_TRUE(wss_server.Start());
542 InitialiseContext();
543 // Set HSTS via wss:
544 GURL wss_url = wss_server.GetURL("set-hsts");
545 EXPECT_TRUE(ConnectAndWait(wss_url));
546
547 // Verify via wss:
548 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
549 EXPECT_TRUE(ConnectAndWait(ws_url));
550}
551
ricea5acb1faf72015-03-16 15:34:00552// Regression test for crbug.com/180504 "WebSocket handshake fails when HTTP
553// headers have trailing LWS".
Sergey Ulanov4c786d32017-09-08 22:53:25554TEST_F(WebSocketEndToEndTest, TrailingWhitespace) {
ricea5acb1faf72015-03-16 15:34:00555 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea5acb1faf72015-03-16 15:34:00556 GetWebSocketTestDataDirectory());
557 ASSERT_TRUE(ws_server.Start());
558
559 GURL ws_url = ws_server.GetURL("trailing-whitespace");
560 sub_protocols_.push_back("sip");
561 EXPECT_TRUE(ConnectAndWait(ws_url));
562 EXPECT_EQ("sip", event_interface_->selected_subprotocol());
563}
564
riceae1d67672015-03-19 10:10:17565// This is a regression test for crbug.com/169448 "WebSockets should support
566// header continuations"
567// TODO(ricea): HTTP continuation headers have been deprecated by RFC7230. If
568// support for continuation headers is removed from Chrome, then this test will
569// break and should be removed.
Sergey Ulanov4c786d32017-09-08 22:53:25570TEST_F(WebSocketEndToEndTest, HeaderContinuations) {
riceae1d67672015-03-19 10:10:17571 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
riceae1d67672015-03-19 10:10:17572 GetWebSocketTestDataDirectory());
573 ASSERT_TRUE(ws_server.Start());
574
575 GURL ws_url = ws_server.GetURL("header-continuation");
576
577 EXPECT_TRUE(ConnectAndWait(ws_url));
578 EXPECT_EQ("permessage-deflate; server_max_window_bits=10",
579 event_interface_->extensions());
580}
581
ricea433bdab2015-01-26 07:25:37582} // namespace
583
584} // namespace net