blob: b720996fa4fb70d41edbcaf978900c6f69c6eda4 [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() {
ricea433bdab2015-01-26 07:25:37269 context_.Init();
Eric Roman3d8546a2018-09-10 17:00:52270 context_.proxy_resolution_service()->SetProxyDelegate(
271 proxy_delegate_.get());
ricea433bdab2015-01-26 07:25:37272 initialised_context_ = true;
273 }
274
275 // Send the connect request to |socket_url| and wait for a response. Returns
276 // true if the handshake succeeded.
277 bool ConnectAndWait(const GURL& socket_url) {
278 if (!initialised_context_) {
279 InitialiseContext();
280 }
Daniel Cheng88186bd52017-10-20 08:14:46281 url::Origin origin = url::Origin::Create(GURL("http://localhost"));
Mike Westb85da8ed2017-08-10 14:16:46282 GURL site_for_cookies("http://localhost/");
Adam Rice5b4a3d82018-08-02 15:28:43283 event_interface_ = new ConnectTestingEventInterface();
Bence Béky65623972018-03-05 15:31:56284 channel_ = std::make_unique<WebSocketChannel>(
285 base::WrapUnique(event_interface_), &context_);
alladacef397d2016-06-29 17:52:23286 channel_->SendAddChannelRequest(GURL(socket_url), sub_protocols_, origin,
Yutaka Hirano2f65eec2018-05-23 01:58:22287 site_for_cookies, HttpRequestHeaders());
ricea433bdab2015-01-26 07:25:37288 event_interface_->WaitForResponse();
289 return !event_interface_->failed();
290 }
291
292 ConnectTestingEventInterface* event_interface_; // owned by channel_
danakj9c5cab52016-04-16 00:54:33293 std::unique_ptr<TestProxyDelegateWithProxyInfo> proxy_delegate_;
ricea433bdab2015-01-26 07:25:37294 TestURLRequestContext context_;
danakj9c5cab52016-04-16 00:54:33295 std::unique_ptr<WebSocketChannel> channel_;
ricea5acb1faf72015-03-16 15:34:00296 std::vector<std::string> sub_protocols_;
ricea433bdab2015-01-26 07:25:37297 bool initialised_context_;
298};
299
ricea433bdab2015-01-26 07:25:37300// Basic test of connectivity. If this test fails, nothing else can be expected
301// to work.
Sergey Ulanov4c786d32017-09-08 22:53:25302TEST_F(WebSocketEndToEndTest, BasicSmokeTest) {
ricea433bdab2015-01-26 07:25:37303 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37304 GetWebSocketTestDataDirectory());
305 ASSERT_TRUE(ws_server.Start());
306 EXPECT_TRUE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
307}
308
309// Test for issue crbug.com/433695 "Unencrypted WebSocket connection via
310// authenticated proxy times out"
311// TODO(ricea): Enable this when the issue is fixed.
312TEST_F(WebSocketEndToEndTest, DISABLED_HttpsProxyUnauthedFails) {
313 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37314 base::FilePath());
315 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37316 GetWebSocketTestDataDirectory());
317 ASSERT_TRUE(proxy_server.StartInBackground());
318 ASSERT_TRUE(ws_server.StartInBackground());
319 ASSERT_TRUE(proxy_server.BlockUntilStarted());
320 ASSERT_TRUE(ws_server.BlockUntilStarted());
321 std::string proxy_config =
322 "https=" + proxy_server.host_port_pair().ToString();
Lily Houghton8c2f97d2018-01-22 05:06:59323 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Ramin Halavatica8d5252018-03-12 05:33:49324 ProxyResolutionService::CreateFixed(proxy_config,
325 TRAFFIC_ANNOTATION_FOR_TESTS));
Lily Houghton8c2f97d2018-01-22 05:06:59326 ASSERT_TRUE(proxy_resolution_service);
327 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37328 EXPECT_FALSE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
329 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
330}
331
Sergey Ulanov4c786d32017-09-08 22:53:25332// These test are not compatible with RemoteTestServer because RemoteTestServer
333// doesn't support TYPE_BASIC_AUTH_PROXY.
334// TODO(ricea): Make these tests work. See crbug.com/441711.
335#if defined(OS_ANDROID) || defined(OS_FUCHSIA)
336#define MAYBE_HttpsWssProxyUnauthedFails DISABLED_HttpsWssProxyUnauthedFails
337#define MAYBE_HttpsProxyUsed DISABLED_HttpsProxyUsed
338#else
339#define MAYBE_HttpsWssProxyUnauthedFails HttpsWssProxyUnauthedFails
340#define MAYBE_HttpsProxyUsed HttpsProxyUsed
341#endif
342
343TEST_F(WebSocketEndToEndTest, MAYBE_HttpsWssProxyUnauthedFails) {
ricea433bdab2015-01-26 07:25:37344 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37345 base::FilePath());
346 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS,
ricea433bdab2015-01-26 07:25:37347 GetWebSocketTestDataDirectory());
348 ASSERT_TRUE(proxy_server.StartInBackground());
349 ASSERT_TRUE(wss_server.StartInBackground());
350 ASSERT_TRUE(proxy_server.BlockUntilStarted());
351 ASSERT_TRUE(wss_server.BlockUntilStarted());
Eric Romanda790f92018-11-07 19:17:15352 ProxyConfig proxy_config;
353 proxy_config.proxy_rules().ParseFromString(
354 "https=" + proxy_server.host_port_pair().ToString());
355 // TODO(https://crbug.com/901896): Don't rely on proxying localhost.
356 proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit();
357
Lily Houghton8c2f97d2018-01-22 05:06:59358 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Eric Romanda790f92018-11-07 19:17:15359 ProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation(
360 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)));
Lily Houghton8c2f97d2018-01-22 05:06:59361 ASSERT_TRUE(proxy_resolution_service);
362 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37363 EXPECT_FALSE(ConnectAndWait(wss_server.GetURL(kEchoServer)));
364 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
365}
366
367// Regression test for crbug/426736 "WebSocket connections not using configured
368// system HTTPS Proxy".
Sergey Ulanov4c786d32017-09-08 22:53:25369TEST_F(WebSocketEndToEndTest, MAYBE_HttpsProxyUsed) {
Adam Rice5b4a3d82018-08-02 15:28:43370 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
ricea433bdab2015-01-26 07:25:37371 base::FilePath());
372 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37373 GetWebSocketTestDataDirectory());
374 ASSERT_TRUE(proxy_server.StartInBackground());
375 ASSERT_TRUE(ws_server.StartInBackground());
376 ASSERT_TRUE(proxy_server.BlockUntilStarted());
377 ASSERT_TRUE(ws_server.BlockUntilStarted());
Eric Romanda790f92018-11-07 19:17:15378 ProxyConfig proxy_config;
379 proxy_config.proxy_rules().ParseFromString(
380 "https=" + proxy_server.host_port_pair().ToString() + ";" +
381 "http=" + proxy_server.host_port_pair().ToString());
382 // TODO(https://crbug.com/901896): Don't rely on proxying localhost.
383 proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit();
384
Lily Houghton8c2f97d2018-01-22 05:06:59385 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Eric Romanda790f92018-11-07 19:17:15386 ProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation(
387 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)));
Lily Houghton8c2f97d2018-01-22 05:06:59388 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37389 InitialiseContext();
390
ricea433bdab2015-01-26 07:25:37391 GURL ws_url = ws_server.GetURL(kEchoServer);
392 EXPECT_TRUE(ConnectAndWait(ws_url));
ryansturm7de050c2016-02-23 00:10:21393 const TestProxyDelegateWithProxyInfo::ResolvedProxyInfo& info =
394 proxy_delegate_->resolved_proxy_info();
ricea433bdab2015-01-26 07:25:37395 EXPECT_EQ(ws_url, info.url);
396 EXPECT_TRUE(info.proxy_info.is_http());
397}
398
Adam Rice5b4a3d82018-08-02 15:28:43399std::unique_ptr<HttpResponse> ProxyPacHandler(const HttpRequest& request) {
400 GURL url = request.GetURL();
401 EXPECT_EQ(url.path_piece(), "/proxy.pac");
402 EXPECT_TRUE(url.has_query());
403 std::string proxy;
404 EXPECT_TRUE(GetValueForKeyInQuery(url, "proxy", &proxy));
405 auto response = std::make_unique<BasicHttpResponse>();
406 response->set_content_type("application/x-ns-proxy-autoconfig");
407 response->set_content(
408 base::StringPrintf("function FindProxyForURL(url, host) {\n"
409 " return 'PROXY %s';\n"
410 "}\n",
411 proxy.c_str()));
412 return response;
413}
414
415// This tests the proxy.pac resolver that is built into the system. This is not
416// the one that Chrome normally uses. Chrome's normal implementation is defined
417// as a mojo service. It is outside //net and we can't use it from here. This
418// tests the alternative implementations that are selected when the
419// --winhttp-proxy-resolver flag is provided to Chrome. These only exist on OS X
420// and Windows.
421// TODO(ricea): Remove this test if --winhttp-proxy-resolver flag is removed.
422// See crbug.com/644030.
423
424#if defined(OS_WIN) || defined(OS_MACOSX)
425#define MAYBE_ProxyPacUsed ProxyPacUsed
426#else
427#define MAYBE_ProxyPacUsed DISABLED_ProxyPacUsed
428#endif
429
430TEST_F(WebSocketEndToEndTest, MAYBE_ProxyPacUsed) {
431 EmbeddedTestServer proxy_pac_server(net::EmbeddedTestServer::Type::TYPE_HTTP);
432 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
433 base::FilePath());
434 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
435 GetWebSocketTestDataDirectory());
436 proxy_pac_server.RegisterRequestHandler(base::BindRepeating(ProxyPacHandler));
437 proxy_server.set_redirect_connect_to_localhost(true);
438
439 ASSERT_TRUE(proxy_pac_server.Start());
440 ASSERT_TRUE(proxy_server.StartInBackground());
441 ASSERT_TRUE(ws_server.StartInBackground());
442 ASSERT_TRUE(proxy_server.BlockUntilStarted());
443 ASSERT_TRUE(ws_server.BlockUntilStarted());
444
445 ProxyConfig proxy_config =
446 ProxyConfig::CreateFromCustomPacURL(proxy_pac_server.GetURL(base::StrCat(
447 {"/proxy.pac?proxy=", proxy_server.host_port_pair().ToString()})));
448 proxy_config.set_pac_mandatory(true);
449 auto proxy_config_service = std::make_unique<ProxyConfigServiceFixed>(
450 ProxyConfigWithAnnotation(proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS));
451 NetLog net_log;
452 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
453 ProxyResolutionService::CreateUsingSystemProxyResolver(
454 std::move(proxy_config_service), &net_log));
455 ASSERT_EQ(ws_server.host_port_pair().host(), "127.0.0.1");
456 context_.set_proxy_resolution_service(proxy_resolution_service.get());
457 InitialiseContext();
458
Eric Romanda790f92018-11-07 19:17:15459 // Use a name other than localhost, since localhost implicitly bypasses the
460 // use of proxy.pac.
Adam Rice5b4a3d82018-08-02 15:28:43461 HostPortPair fake_ws_host_port_pair("stealth-localhost",
462 ws_server.host_port_pair().port());
463
464 GURL ws_url(base::StrCat(
465 {"ws://", fake_ws_host_port_pair.ToString(), "/", kEchoServer}));
466 EXPECT_TRUE(ConnectAndWait(ws_url));
467 const auto& info = proxy_delegate_->resolved_proxy_info();
468 EXPECT_EQ(ws_url, info.url);
469 EXPECT_TRUE(info.proxy_info.is_http());
470 EXPECT_EQ(info.proxy_info.ToPacString(),
471 base::StrCat({"PROXY ", proxy_server.host_port_pair().ToString()}));
472}
473
ricea23c3f942015-02-02 13:35:13474// This is a regression test for crbug.com/408061 Crash in
475// net::WebSocketBasicHandshakeStream::Upgrade.
Sergey Ulanov4c786d32017-09-08 22:53:25476TEST_F(WebSocketEndToEndTest, TruncatedResponse) {
ricea23c3f942015-02-02 13:35:13477 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea23c3f942015-02-02 13:35:13478 GetWebSocketTestDataDirectory());
479 ASSERT_TRUE(ws_server.Start());
480 InitialiseContext();
481
482 GURL ws_url = ws_server.GetURL("truncated-headers");
483 EXPECT_FALSE(ConnectAndWait(ws_url));
484}
485
Adam Ricecb76ac62015-02-20 05:33:25486// Regression test for crbug.com/455215 "HSTS not applied to WebSocket"
Sergey Ulanov4c786d32017-09-08 22:53:25487TEST_F(WebSocketEndToEndTest, HstsHttpsToWebSocket) {
tommycli59a63432015-11-06 00:10:55488 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
489 https_server.SetSSLConfig(
490 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
491 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
492
estarka5da76702015-04-09 04:00:16493 SpawnedTestServer::SSLOptions ssl_options(
494 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25495 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
496 GetWebSocketTestDataDirectory());
estarka5da76702015-04-09 04:00:16497
tommycli59a63432015-11-06 00:10:55498 ASSERT_TRUE(https_server.Start());
499 ASSERT_TRUE(wss_server.Start());
Adam Ricecb76ac62015-02-20 05:33:25500 InitialiseContext();
501 // Set HSTS via https:
502 TestDelegate delegate;
tommycli59a63432015-11-06 00:10:55503 GURL https_page = https_server.GetURL("/hsts-headers.html");
rhalavati9ebaba7e2017-04-27 06:16:29504 std::unique_ptr<URLRequest> request(context_.CreateRequest(
505 https_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
Adam Ricecb76ac62015-02-20 05:33:25506 request->Start();
Wez2a31b222018-06-07 22:07:15507 delegate.RunUntilComplete();
maksim.sisov7f60c8e2016-09-16 19:38:17508 EXPECT_EQ(OK, delegate.request_status());
Adam Ricecb76ac62015-02-20 05:33:25509
510 // Check HSTS with ws:
511 // Change the scheme from wss: to ws: to verify that it is switched back.
512 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
513 EXPECT_TRUE(ConnectAndWait(ws_url));
514}
515
Sergey Ulanov4c786d32017-09-08 22:53:25516TEST_F(WebSocketEndToEndTest, HstsWebSocketToHttps) {
tommycli59a63432015-11-06 00:10:55517 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
518 https_server.SetSSLConfig(
519 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
520 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
521
estarka5da76702015-04-09 04:00:16522 SpawnedTestServer::SSLOptions ssl_options(
523 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25524 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
525 GetWebSocketTestDataDirectory());
tommycli59a63432015-11-06 00:10:55526 ASSERT_TRUE(https_server.Start());
527 ASSERT_TRUE(wss_server.Start());
Adam Ricecb76ac62015-02-20 05:33:25528 InitialiseContext();
529 // Set HSTS via wss:
530 GURL wss_url = wss_server.GetURL("set-hsts");
531 EXPECT_TRUE(ConnectAndWait(wss_url));
532
533 // Verify via http:
534 TestDelegate delegate;
535 GURL http_page =
tommycli59a63432015-11-06 00:10:55536 ReplaceUrlScheme(https_server.GetURL("/simple.html"), "http");
rhalavati9ebaba7e2017-04-27 06:16:29537 std::unique_ptr<URLRequest> request(context_.CreateRequest(
538 http_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
Adam Ricecb76ac62015-02-20 05:33:25539 request->Start();
Wez2a31b222018-06-07 22:07:15540 delegate.RunUntilComplete();
maksim.sisov7f60c8e2016-09-16 19:38:17541 EXPECT_EQ(OK, delegate.request_status());
Adam Ricecb76ac62015-02-20 05:33:25542 EXPECT_TRUE(request->url().SchemeIs("https"));
543}
544
Sergey Ulanov4c786d32017-09-08 22:53:25545TEST_F(WebSocketEndToEndTest, HstsWebSocketToWebSocket) {
estarka5da76702015-04-09 04:00:16546 SpawnedTestServer::SSLOptions ssl_options(
547 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25548 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
549 GetWebSocketTestDataDirectory());
550 ASSERT_TRUE(wss_server.Start());
551 InitialiseContext();
552 // Set HSTS via wss:
553 GURL wss_url = wss_server.GetURL("set-hsts");
554 EXPECT_TRUE(ConnectAndWait(wss_url));
555
556 // Verify via wss:
557 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
558 EXPECT_TRUE(ConnectAndWait(ws_url));
559}
560
ricea5acb1faf72015-03-16 15:34:00561// Regression test for crbug.com/180504 "WebSocket handshake fails when HTTP
562// headers have trailing LWS".
Sergey Ulanov4c786d32017-09-08 22:53:25563TEST_F(WebSocketEndToEndTest, TrailingWhitespace) {
ricea5acb1faf72015-03-16 15:34:00564 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea5acb1faf72015-03-16 15:34:00565 GetWebSocketTestDataDirectory());
566 ASSERT_TRUE(ws_server.Start());
567
568 GURL ws_url = ws_server.GetURL("trailing-whitespace");
569 sub_protocols_.push_back("sip");
570 EXPECT_TRUE(ConnectAndWait(ws_url));
571 EXPECT_EQ("sip", event_interface_->selected_subprotocol());
572}
573
riceae1d67672015-03-19 10:10:17574// This is a regression test for crbug.com/169448 "WebSockets should support
575// header continuations"
576// TODO(ricea): HTTP continuation headers have been deprecated by RFC7230. If
577// support for continuation headers is removed from Chrome, then this test will
578// break and should be removed.
Sergey Ulanov4c786d32017-09-08 22:53:25579TEST_F(WebSocketEndToEndTest, HeaderContinuations) {
riceae1d67672015-03-19 10:10:17580 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
riceae1d67672015-03-19 10:10:17581 GetWebSocketTestDataDirectory());
582 ASSERT_TRUE(ws_server.Start());
583
584 GURL ws_url = ws_server.GetURL("header-continuation");
585
586 EXPECT_TRUE(ConnectAndWait(ws_url));
587 EXPECT_EQ("permessage-deflate; server_max_window_bits=10",
588 event_interface_->extensions());
589}
590
ricea433bdab2015-01-26 07:25:37591} // namespace
592
593} // namespace net