blob: 4ce998af51aa9d5c2e6423cf9bf12ddbdd54ecca [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"
Tsuyoshi Horo01faed62019-02-20 22:11:3733#include "net/base/ip_endpoint.h"
ryansturm7de050c2016-02-23 00:10:2134#include "net/base/proxy_delegate.h"
Adam Rice5b4a3d82018-08-02 15:28:4335#include "net/base/url_util.h"
Yutaka Hirano2f65eec2018-05-23 01:58:2236#include "net/http/http_request_headers.h"
Adam Rice5b4a3d82018-08-02 15:28:4337#include "net/log/net_log.h"
38#include "net/proxy_resolution/proxy_config.h"
39#include "net/proxy_resolution/proxy_config_service.h"
40#include "net/proxy_resolution/proxy_config_service_fixed.h"
41#include "net/proxy_resolution/proxy_config_with_annotation.h"
42#include "net/proxy_resolution/proxy_info.h"
Lily Houghtonffe89daa02018-03-09 18:30:0343#include "net/proxy_resolution/proxy_resolution_service.h"
tommycli59a63432015-11-06 00:10:5544#include "net/test/embedded_test_server/embedded_test_server.h"
Adam Rice5b4a3d82018-08-02 15:28:4345#include "net/test/embedded_test_server/http_request.h"
46#include "net/test/embedded_test_server/http_response.h"
ricea433bdab2015-01-26 07:25:3747#include "net/test/spawned_test_server/spawned_test_server.h"
rsleevia69c79a2016-06-22 03:28:4348#include "net/test/test_data_directory.h"
Bence Béky98447b12018-05-08 03:14:0149#include "net/test/test_with_scoped_task_environment.h"
rhalavati9ebaba7e2017-04-27 06:16:2950#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
Adam Rice5b4a3d82018-08-02 15:28:4351#include "net/url_request/url_request.h"
52#include "net/url_request/url_request_context.h"
ricea433bdab2015-01-26 07:25:3753#include "net/url_request/url_request_test_util.h"
54#include "net/websockets/websocket_channel.h"
55#include "net/websockets/websocket_event_interface.h"
56#include "testing/gtest/include/gtest/gtest.h"
Adam Rice5b4a3d82018-08-02 15:28:4357#include "url/gurl.h"
mkwst4997ce82015-07-25 12:00:0558#include "url/origin.h"
ricea433bdab2015-01-26 07:25:3759
60namespace net {
61
yhirano4a593832016-10-24 18:58:2262class URLRequest;
63
ricea433bdab2015-01-26 07:25:3764namespace {
65
Adam Rice5b4a3d82018-08-02 15:28:4366using test_server::BasicHttpResponse;
67using test_server::HttpRequest;
68using test_server::HttpResponse;
69
ricea433bdab2015-01-26 07:25:3770static const char kEchoServer[] = "echo-with-no-extension";
71
Adam Ricecb76ac62015-02-20 05:33:2572// Simplify changing URL schemes.
73GURL ReplaceUrlScheme(const GURL& in_url, const base::StringPiece& scheme) {
74 GURL::Replacements replacements;
75 replacements.SetSchemeStr(scheme);
76 return in_url.ReplaceComponents(replacements);
77}
78
ricea433bdab2015-01-26 07:25:3779// An implementation of WebSocketEventInterface that waits for and records the
80// results of the connect.
81class ConnectTestingEventInterface : public WebSocketEventInterface {
82 public:
83 ConnectTestingEventInterface();
84
85 void WaitForResponse();
86
87 bool failed() const { return failed_; }
88
89 // Only set if the handshake failed, otherwise empty.
90 std::string failure_message() const;
91
92 std::string selected_subprotocol() const;
93
94 std::string extensions() const;
95
96 // Implementation of WebSocketEventInterface.
yhirano4a593832016-10-24 18:58:2297 void OnCreateURLRequest(URLRequest* request) override {}
98
Yutaka Hirano4165de92018-04-10 11:46:4999 void OnAddChannelResponse(const std::string& selected_subprotocol,
100 const std::string& extensions) override;
ricea433bdab2015-01-26 07:25:37101
Yutaka Hirano4165de92018-04-10 11:46:49102 void OnDataFrame(bool fin,
103 WebSocketMessageType type,
104 scoped_refptr<IOBuffer> data,
105 size_t data_size) override;
ricea433bdab2015-01-26 07:25:37106
Yutaka Hirano4165de92018-04-10 11:46:49107 void OnFlowControl(int64_t quota) override;
ricea433bdab2015-01-26 07:25:37108
Yutaka Hirano4165de92018-04-10 11:46:49109 void OnClosingHandshake() override;
ricea433bdab2015-01-26 07:25:37110
Yutaka Hirano4165de92018-04-10 11:46:49111 void OnDropChannel(bool was_clean,
112 uint16_t code,
113 const std::string& reason) override;
ricea433bdab2015-01-26 07:25:37114
Yutaka Hirano4165de92018-04-10 11:46:49115 void OnFailChannel(const std::string& message) override;
ricea433bdab2015-01-26 07:25:37116
Yutaka Hirano4165de92018-04-10 11:46:49117 void OnStartOpeningHandshake(
danakj9c5cab52016-04-16 00:54:33118 std::unique_ptr<WebSocketHandshakeRequestInfo> request) override;
ricea433bdab2015-01-26 07:25:37119
Yutaka Hirano4165de92018-04-10 11:46:49120 void OnFinishOpeningHandshake(
danakj9c5cab52016-04-16 00:54:33121 std::unique_ptr<WebSocketHandshakeResponseInfo> response) override;
ricea433bdab2015-01-26 07:25:37122
Yutaka Hirano4165de92018-04-10 11:46:49123 void OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:33124 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks,
ricea433bdab2015-01-26 07:25:37125 const GURL& url,
126 const SSLInfo& ssl_info,
127 bool fatal) override;
128
Emily Starkf2c9bbd2019-04-09 17:08:58129 int OnAuthRequired(const AuthChallengeInfo& auth_info,
Yutaka Hirano70fa25912018-06-06 05:26:54130 scoped_refptr<HttpResponseHeaders> response_headers,
Tsuyoshi Horo01faed62019-02-20 22:11:37131 const IPEndPoint& remote_endpoint,
Yutaka Hirano70fa25912018-06-06 05:26:54132 base::OnceCallback<void(const AuthCredentials*)> callback,
133 base::Optional<AuthCredentials>* credentials) override;
134
ricea433bdab2015-01-26 07:25:37135 private:
136 void QuitNestedEventLoop();
137
138 // failed_ is true if the handshake failed (ie. OnFailChannel was called).
139 bool failed_;
140 std::string selected_subprotocol_;
141 std::string extensions_;
142 std::string failure_message_;
143 base::RunLoop run_loop_;
144
145 DISALLOW_COPY_AND_ASSIGN(ConnectTestingEventInterface);
146};
147
tyoshinoc06da562015-03-06 06:02:42148ConnectTestingEventInterface::ConnectTestingEventInterface() : failed_(false) {
ricea433bdab2015-01-26 07:25:37149}
150
151void ConnectTestingEventInterface::WaitForResponse() {
152 run_loop_.Run();
153}
154
155std::string ConnectTestingEventInterface::failure_message() const {
156 return failure_message_;
157}
158
159std::string ConnectTestingEventInterface::selected_subprotocol() const {
160 return selected_subprotocol_;
161}
162
163std::string ConnectTestingEventInterface::extensions() const {
164 return extensions_;
165}
166
Yutaka Hirano4165de92018-04-10 11:46:49167void ConnectTestingEventInterface::OnAddChannelResponse(
ricea433bdab2015-01-26 07:25:37168 const std::string& selected_subprotocol,
169 const std::string& extensions) {
ricea433bdab2015-01-26 07:25:37170 selected_subprotocol_ = selected_subprotocol;
171 extensions_ = extensions;
172 QuitNestedEventLoop();
ricea433bdab2015-01-26 07:25:37173}
174
Yutaka Hirano4165de92018-04-10 11:46:49175void ConnectTestingEventInterface::OnDataFrame(bool fin,
176 WebSocketMessageType type,
177 scoped_refptr<IOBuffer> data,
178 size_t data_size) {}
ricea433bdab2015-01-26 07:25:37179
Yutaka Hirano4165de92018-04-10 11:46:49180void ConnectTestingEventInterface::OnFlowControl(int64_t quota) {}
ricea433bdab2015-01-26 07:25:37181
Yutaka Hirano4165de92018-04-10 11:46:49182void ConnectTestingEventInterface::OnClosingHandshake() {}
ricea433bdab2015-01-26 07:25:37183
Yutaka Hirano4165de92018-04-10 11:46:49184void ConnectTestingEventInterface::OnDropChannel(bool was_clean,
185 uint16_t code,
186 const std::string& reason) {}
ricea433bdab2015-01-26 07:25:37187
Yutaka Hirano4165de92018-04-10 11:46:49188void ConnectTestingEventInterface::OnFailChannel(const std::string& message) {
ricea433bdab2015-01-26 07:25:37189 failed_ = true;
190 failure_message_ = message;
191 QuitNestedEventLoop();
ricea433bdab2015-01-26 07:25:37192}
193
Yutaka Hirano4165de92018-04-10 11:46:49194void ConnectTestingEventInterface::OnStartOpeningHandshake(
195 std::unique_ptr<WebSocketHandshakeRequestInfo> request) {}
ricea433bdab2015-01-26 07:25:37196
Yutaka Hirano4165de92018-04-10 11:46:49197void ConnectTestingEventInterface::OnFinishOpeningHandshake(
198 std::unique_ptr<WebSocketHandshakeResponseInfo> response) {}
ricea433bdab2015-01-26 07:25:37199
Yutaka Hirano4165de92018-04-10 11:46:49200void ConnectTestingEventInterface::OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:33201 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks,
ricea433bdab2015-01-26 07:25:37202 const GURL& url,
203 const SSLInfo& ssl_info,
204 bool fatal) {
skyostil4891b25b2015-06-11 11:43:45205 base::ThreadTaskRunnerHandle::Get()->PostTask(
kylecharf4fe5172019-02-15 18:53:49206 FROM_HERE, base::BindOnce(&SSLErrorCallbacks::CancelSSLRequest,
207 base::Owned(ssl_error_callbacks.release()),
208 ERR_SSL_PROTOCOL_ERROR, &ssl_info));
ricea433bdab2015-01-26 07:25:37209}
210
Yutaka Hirano70fa25912018-06-06 05:26:54211int ConnectTestingEventInterface::OnAuthRequired(
Emily Starkf2c9bbd2019-04-09 17:08:58212 const AuthChallengeInfo& auth_info,
Yutaka Hirano70fa25912018-06-06 05:26:54213 scoped_refptr<HttpResponseHeaders> response_headers,
Tsuyoshi Horo01faed62019-02-20 22:11:37214 const IPEndPoint& remote_endpoint,
Yutaka Hirano70fa25912018-06-06 05:26:54215 base::OnceCallback<void(const AuthCredentials*)> callback,
216 base::Optional<AuthCredentials>* credentials) {
217 *credentials = base::nullopt;
218 return OK;
219}
220
ricea433bdab2015-01-26 07:25:37221void ConnectTestingEventInterface::QuitNestedEventLoop() {
222 run_loop_.Quit();
223}
224
225// A subclass of TestNetworkDelegate that additionally implements the
226// OnResolveProxy callback and records the information passed to it.
ryansturm7de050c2016-02-23 00:10:21227class TestProxyDelegateWithProxyInfo : public ProxyDelegate {
ricea433bdab2015-01-26 07:25:37228 public:
Chris Watkins28c2fdd2017-11-30 06:06:52229 TestProxyDelegateWithProxyInfo() = default;
ricea433bdab2015-01-26 07:25:37230
231 struct ResolvedProxyInfo {
232 GURL url;
233 ProxyInfo proxy_info;
234 };
235
236 const ResolvedProxyInfo& resolved_proxy_info() const {
237 return resolved_proxy_info_;
238 }
239
240 protected:
241 void OnResolveProxy(const GURL& url,
ryansturm4bab06832016-03-03 23:41:07242 const std::string& method,
Reilly Grantb414ace72017-11-14 23:03:22243 const ProxyRetryInfoMap& proxy_retry_info,
ricea433bdab2015-01-26 07:25:37244 ProxyInfo* result) override {
245 resolved_proxy_info_.url = url;
246 resolved_proxy_info_.proxy_info = *result;
247 }
248
ryansturm7de050c2016-02-23 00:10:21249 void OnFallback(const ProxyServer& bad_proxy, int net_error) override {}
ryansturm7de050c2016-02-23 00:10:21250
Wojciech Dzierżanowskic2704042019-02-07 20:54:28251 void OnBeforeHttp1TunnelRequest(const ProxyServer& proxy_server,
252 HttpRequestHeaders* extra_headers) override {}
Wojciech Dzierżanowski1f823562019-01-18 11:26:00253
Wojciech Dzierżanowskic2704042019-02-07 20:54:28254 Error OnHttp1TunnelHeadersReceived(
Wojciech Dzierżanowski1f823562019-01-18 11:26:00255 const ProxyServer& proxy_server,
256 const HttpResponseHeaders& response_headers) override {
257 return OK;
258 }
259
ricea433bdab2015-01-26 07:25:37260 private:
261 ResolvedProxyInfo resolved_proxy_info_;
262
ryansturm7de050c2016-02-23 00:10:21263 DISALLOW_COPY_AND_ASSIGN(TestProxyDelegateWithProxyInfo);
ricea433bdab2015-01-26 07:25:37264};
265
Bence Béky98447b12018-05-08 03:14:01266class WebSocketEndToEndTest : public TestWithScopedTaskEnvironment {
ricea433bdab2015-01-26 07:25:37267 protected:
268 WebSocketEndToEndTest()
Adam Ricecb76ac62015-02-20 05:33:25269 : event_interface_(),
Bence Béky65623972018-03-05 15:31:56270 proxy_delegate_(std::make_unique<TestProxyDelegateWithProxyInfo>()),
ricea433bdab2015-01-26 07:25:37271 context_(true),
Adam Ricecb76ac62015-02-20 05:33:25272 channel_(),
ricea433bdab2015-01-26 07:25:37273 initialised_context_(false) {}
274
275 // Initialise the URLRequestContext. Normally done automatically by
276 // ConnectAndWait(). This method is for the use of tests that need the
277 // URLRequestContext initialised before calling ConnectAndWait().
278 void InitialiseContext() {
ricea433bdab2015-01-26 07:25:37279 context_.Init();
Eric Roman3d8546a2018-09-10 17:00:52280 context_.proxy_resolution_service()->SetProxyDelegate(
281 proxy_delegate_.get());
ricea433bdab2015-01-26 07:25:37282 initialised_context_ = true;
283 }
284
285 // Send the connect request to |socket_url| and wait for a response. Returns
286 // true if the handshake succeeded.
287 bool ConnectAndWait(const GURL& socket_url) {
288 if (!initialised_context_) {
289 InitialiseContext();
290 }
Daniel Cheng88186bd52017-10-20 08:14:46291 url::Origin origin = url::Origin::Create(GURL("http://localhost"));
Mike Westb85da8ed2017-08-10 14:16:46292 GURL site_for_cookies("http://localhost/");
Adam Rice5b4a3d82018-08-02 15:28:43293 event_interface_ = new ConnectTestingEventInterface();
Bence Béky65623972018-03-05 15:31:56294 channel_ = std::make_unique<WebSocketChannel>(
295 base::WrapUnique(event_interface_), &context_);
alladacef397d2016-06-29 17:52:23296 channel_->SendAddChannelRequest(GURL(socket_url), sub_protocols_, origin,
Yutaka Hirano2f65eec2018-05-23 01:58:22297 site_for_cookies, HttpRequestHeaders());
ricea433bdab2015-01-26 07:25:37298 event_interface_->WaitForResponse();
299 return !event_interface_->failed();
300 }
301
302 ConnectTestingEventInterface* event_interface_; // owned by channel_
danakj9c5cab52016-04-16 00:54:33303 std::unique_ptr<TestProxyDelegateWithProxyInfo> proxy_delegate_;
ricea433bdab2015-01-26 07:25:37304 TestURLRequestContext context_;
danakj9c5cab52016-04-16 00:54:33305 std::unique_ptr<WebSocketChannel> channel_;
ricea5acb1faf72015-03-16 15:34:00306 std::vector<std::string> sub_protocols_;
ricea433bdab2015-01-26 07:25:37307 bool initialised_context_;
308};
309
ricea433bdab2015-01-26 07:25:37310// Basic test of connectivity. If this test fails, nothing else can be expected
311// to work.
Sergey Ulanov4c786d32017-09-08 22:53:25312TEST_F(WebSocketEndToEndTest, BasicSmokeTest) {
ricea433bdab2015-01-26 07:25:37313 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37314 GetWebSocketTestDataDirectory());
315 ASSERT_TRUE(ws_server.Start());
316 EXPECT_TRUE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
317}
318
319// Test for issue crbug.com/433695 "Unencrypted WebSocket connection via
320// authenticated proxy times out"
321// TODO(ricea): Enable this when the issue is fixed.
322TEST_F(WebSocketEndToEndTest, DISABLED_HttpsProxyUnauthedFails) {
323 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37324 base::FilePath());
325 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37326 GetWebSocketTestDataDirectory());
327 ASSERT_TRUE(proxy_server.StartInBackground());
328 ASSERT_TRUE(ws_server.StartInBackground());
329 ASSERT_TRUE(proxy_server.BlockUntilStarted());
330 ASSERT_TRUE(ws_server.BlockUntilStarted());
331 std::string proxy_config =
332 "https=" + proxy_server.host_port_pair().ToString();
Lily Houghton8c2f97d2018-01-22 05:06:59333 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Ramin Halavatica8d5252018-03-12 05:33:49334 ProxyResolutionService::CreateFixed(proxy_config,
335 TRAFFIC_ANNOTATION_FOR_TESTS));
Lily Houghton8c2f97d2018-01-22 05:06:59336 ASSERT_TRUE(proxy_resolution_service);
337 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37338 EXPECT_FALSE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
339 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
340}
341
Sergey Ulanov4c786d32017-09-08 22:53:25342// These test are not compatible with RemoteTestServer because RemoteTestServer
343// doesn't support TYPE_BASIC_AUTH_PROXY.
344// TODO(ricea): Make these tests work. See crbug.com/441711.
345#if defined(OS_ANDROID) || defined(OS_FUCHSIA)
346#define MAYBE_HttpsWssProxyUnauthedFails DISABLED_HttpsWssProxyUnauthedFails
347#define MAYBE_HttpsProxyUsed DISABLED_HttpsProxyUsed
348#else
349#define MAYBE_HttpsWssProxyUnauthedFails HttpsWssProxyUnauthedFails
350#define MAYBE_HttpsProxyUsed HttpsProxyUsed
351#endif
352
353TEST_F(WebSocketEndToEndTest, MAYBE_HttpsWssProxyUnauthedFails) {
ricea433bdab2015-01-26 07:25:37354 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37355 base::FilePath());
356 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS,
ricea433bdab2015-01-26 07:25:37357 GetWebSocketTestDataDirectory());
358 ASSERT_TRUE(proxy_server.StartInBackground());
359 ASSERT_TRUE(wss_server.StartInBackground());
360 ASSERT_TRUE(proxy_server.BlockUntilStarted());
361 ASSERT_TRUE(wss_server.BlockUntilStarted());
Eric Romanda790f92018-11-07 19:17:15362 ProxyConfig proxy_config;
363 proxy_config.proxy_rules().ParseFromString(
364 "https=" + proxy_server.host_port_pair().ToString());
365 // TODO(https://crbug.com/901896): Don't rely on proxying localhost.
366 proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit();
367
Lily Houghton8c2f97d2018-01-22 05:06:59368 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Eric Romanda790f92018-11-07 19:17:15369 ProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation(
370 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)));
Lily Houghton8c2f97d2018-01-22 05:06:59371 ASSERT_TRUE(proxy_resolution_service);
372 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37373 EXPECT_FALSE(ConnectAndWait(wss_server.GetURL(kEchoServer)));
374 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
375}
376
377// Regression test for crbug/426736 "WebSocket connections not using configured
378// system HTTPS Proxy".
Sergey Ulanov4c786d32017-09-08 22:53:25379TEST_F(WebSocketEndToEndTest, MAYBE_HttpsProxyUsed) {
Adam Rice5b4a3d82018-08-02 15:28:43380 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
ricea433bdab2015-01-26 07:25:37381 base::FilePath());
382 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37383 GetWebSocketTestDataDirectory());
384 ASSERT_TRUE(proxy_server.StartInBackground());
385 ASSERT_TRUE(ws_server.StartInBackground());
386 ASSERT_TRUE(proxy_server.BlockUntilStarted());
387 ASSERT_TRUE(ws_server.BlockUntilStarted());
Eric Romanda790f92018-11-07 19:17:15388 ProxyConfig proxy_config;
389 proxy_config.proxy_rules().ParseFromString(
390 "https=" + proxy_server.host_port_pair().ToString() + ";" +
391 "http=" + proxy_server.host_port_pair().ToString());
392 // TODO(https://crbug.com/901896): Don't rely on proxying localhost.
393 proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit();
394
Lily Houghton8c2f97d2018-01-22 05:06:59395 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Eric Romanda790f92018-11-07 19:17:15396 ProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation(
397 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)));
Lily Houghton8c2f97d2018-01-22 05:06:59398 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37399 InitialiseContext();
400
ricea433bdab2015-01-26 07:25:37401 GURL ws_url = ws_server.GetURL(kEchoServer);
402 EXPECT_TRUE(ConnectAndWait(ws_url));
ryansturm7de050c2016-02-23 00:10:21403 const TestProxyDelegateWithProxyInfo::ResolvedProxyInfo& info =
404 proxy_delegate_->resolved_proxy_info();
ricea433bdab2015-01-26 07:25:37405 EXPECT_EQ(ws_url, info.url);
406 EXPECT_TRUE(info.proxy_info.is_http());
407}
408
Adam Rice5b4a3d82018-08-02 15:28:43409std::unique_ptr<HttpResponse> ProxyPacHandler(const HttpRequest& request) {
410 GURL url = request.GetURL();
411 EXPECT_EQ(url.path_piece(), "/proxy.pac");
412 EXPECT_TRUE(url.has_query());
413 std::string proxy;
414 EXPECT_TRUE(GetValueForKeyInQuery(url, "proxy", &proxy));
415 auto response = std::make_unique<BasicHttpResponse>();
416 response->set_content_type("application/x-ns-proxy-autoconfig");
417 response->set_content(
418 base::StringPrintf("function FindProxyForURL(url, host) {\n"
419 " return 'PROXY %s';\n"
420 "}\n",
421 proxy.c_str()));
422 return response;
423}
424
425// This tests the proxy.pac resolver that is built into the system. This is not
426// the one that Chrome normally uses. Chrome's normal implementation is defined
427// as a mojo service. It is outside //net and we can't use it from here. This
428// tests the alternative implementations that are selected when the
429// --winhttp-proxy-resolver flag is provided to Chrome. These only exist on OS X
430// and Windows.
431// TODO(ricea): Remove this test if --winhttp-proxy-resolver flag is removed.
432// See crbug.com/644030.
433
434#if defined(OS_WIN) || defined(OS_MACOSX)
435#define MAYBE_ProxyPacUsed ProxyPacUsed
436#else
437#define MAYBE_ProxyPacUsed DISABLED_ProxyPacUsed
438#endif
439
440TEST_F(WebSocketEndToEndTest, MAYBE_ProxyPacUsed) {
441 EmbeddedTestServer proxy_pac_server(net::EmbeddedTestServer::Type::TYPE_HTTP);
442 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
443 base::FilePath());
444 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
445 GetWebSocketTestDataDirectory());
446 proxy_pac_server.RegisterRequestHandler(base::BindRepeating(ProxyPacHandler));
447 proxy_server.set_redirect_connect_to_localhost(true);
448
449 ASSERT_TRUE(proxy_pac_server.Start());
450 ASSERT_TRUE(proxy_server.StartInBackground());
451 ASSERT_TRUE(ws_server.StartInBackground());
452 ASSERT_TRUE(proxy_server.BlockUntilStarted());
453 ASSERT_TRUE(ws_server.BlockUntilStarted());
454
455 ProxyConfig proxy_config =
456 ProxyConfig::CreateFromCustomPacURL(proxy_pac_server.GetURL(base::StrCat(
457 {"/proxy.pac?proxy=", proxy_server.host_port_pair().ToString()})));
458 proxy_config.set_pac_mandatory(true);
459 auto proxy_config_service = std::make_unique<ProxyConfigServiceFixed>(
460 ProxyConfigWithAnnotation(proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS));
461 NetLog net_log;
462 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
463 ProxyResolutionService::CreateUsingSystemProxyResolver(
464 std::move(proxy_config_service), &net_log));
465 ASSERT_EQ(ws_server.host_port_pair().host(), "127.0.0.1");
466 context_.set_proxy_resolution_service(proxy_resolution_service.get());
467 InitialiseContext();
468
Eric Romanda790f92018-11-07 19:17:15469 // Use a name other than localhost, since localhost implicitly bypasses the
470 // use of proxy.pac.
Adam Rice5b4a3d82018-08-02 15:28:43471 HostPortPair fake_ws_host_port_pair("stealth-localhost",
472 ws_server.host_port_pair().port());
473
474 GURL ws_url(base::StrCat(
475 {"ws://", fake_ws_host_port_pair.ToString(), "/", kEchoServer}));
476 EXPECT_TRUE(ConnectAndWait(ws_url));
477 const auto& info = proxy_delegate_->resolved_proxy_info();
478 EXPECT_EQ(ws_url, info.url);
479 EXPECT_TRUE(info.proxy_info.is_http());
480 EXPECT_EQ(info.proxy_info.ToPacString(),
481 base::StrCat({"PROXY ", proxy_server.host_port_pair().ToString()}));
482}
483
ricea23c3f942015-02-02 13:35:13484// This is a regression test for crbug.com/408061 Crash in
485// net::WebSocketBasicHandshakeStream::Upgrade.
Sergey Ulanov4c786d32017-09-08 22:53:25486TEST_F(WebSocketEndToEndTest, TruncatedResponse) {
ricea23c3f942015-02-02 13:35:13487 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea23c3f942015-02-02 13:35:13488 GetWebSocketTestDataDirectory());
489 ASSERT_TRUE(ws_server.Start());
490 InitialiseContext();
491
492 GURL ws_url = ws_server.GetURL("truncated-headers");
493 EXPECT_FALSE(ConnectAndWait(ws_url));
494}
495
Adam Ricecb76ac62015-02-20 05:33:25496// Regression test for crbug.com/455215 "HSTS not applied to WebSocket"
Sergey Ulanov4c786d32017-09-08 22:53:25497TEST_F(WebSocketEndToEndTest, HstsHttpsToWebSocket) {
tommycli59a63432015-11-06 00:10:55498 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
499 https_server.SetSSLConfig(
500 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
501 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
502
estarka5da76702015-04-09 04:00:16503 SpawnedTestServer::SSLOptions ssl_options(
504 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25505 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
506 GetWebSocketTestDataDirectory());
estarka5da76702015-04-09 04:00:16507
tommycli59a63432015-11-06 00:10:55508 ASSERT_TRUE(https_server.Start());
509 ASSERT_TRUE(wss_server.Start());
Adam Ricecb76ac62015-02-20 05:33:25510 InitialiseContext();
511 // Set HSTS via https:
512 TestDelegate delegate;
tommycli59a63432015-11-06 00:10:55513 GURL https_page = https_server.GetURL("/hsts-headers.html");
rhalavati9ebaba7e2017-04-27 06:16:29514 std::unique_ptr<URLRequest> request(context_.CreateRequest(
515 https_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
Adam Ricecb76ac62015-02-20 05:33:25516 request->Start();
Wez2a31b222018-06-07 22:07:15517 delegate.RunUntilComplete();
maksim.sisov7f60c8e2016-09-16 19:38:17518 EXPECT_EQ(OK, delegate.request_status());
Adam Ricecb76ac62015-02-20 05:33:25519
520 // Check HSTS with ws:
521 // Change the scheme from wss: to ws: to verify that it is switched back.
522 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
523 EXPECT_TRUE(ConnectAndWait(ws_url));
524}
525
Sergey Ulanov4c786d32017-09-08 22:53:25526TEST_F(WebSocketEndToEndTest, HstsWebSocketToHttps) {
tommycli59a63432015-11-06 00:10:55527 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
528 https_server.SetSSLConfig(
529 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
530 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
531
estarka5da76702015-04-09 04:00:16532 SpawnedTestServer::SSLOptions ssl_options(
533 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25534 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
535 GetWebSocketTestDataDirectory());
tommycli59a63432015-11-06 00:10:55536 ASSERT_TRUE(https_server.Start());
537 ASSERT_TRUE(wss_server.Start());
Adam Ricecb76ac62015-02-20 05:33:25538 InitialiseContext();
539 // Set HSTS via wss:
540 GURL wss_url = wss_server.GetURL("set-hsts");
541 EXPECT_TRUE(ConnectAndWait(wss_url));
542
543 // Verify via http:
544 TestDelegate delegate;
545 GURL http_page =
tommycli59a63432015-11-06 00:10:55546 ReplaceUrlScheme(https_server.GetURL("/simple.html"), "http");
rhalavati9ebaba7e2017-04-27 06:16:29547 std::unique_ptr<URLRequest> request(context_.CreateRequest(
548 http_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
Adam Ricecb76ac62015-02-20 05:33:25549 request->Start();
Wez2a31b222018-06-07 22:07:15550 delegate.RunUntilComplete();
maksim.sisov7f60c8e2016-09-16 19:38:17551 EXPECT_EQ(OK, delegate.request_status());
Adam Ricecb76ac62015-02-20 05:33:25552 EXPECT_TRUE(request->url().SchemeIs("https"));
553}
554
Sergey Ulanov4c786d32017-09-08 22:53:25555TEST_F(WebSocketEndToEndTest, HstsWebSocketToWebSocket) {
estarka5da76702015-04-09 04:00:16556 SpawnedTestServer::SSLOptions ssl_options(
557 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25558 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
559 GetWebSocketTestDataDirectory());
560 ASSERT_TRUE(wss_server.Start());
561 InitialiseContext();
562 // Set HSTS via wss:
563 GURL wss_url = wss_server.GetURL("set-hsts");
564 EXPECT_TRUE(ConnectAndWait(wss_url));
565
566 // Verify via wss:
567 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
568 EXPECT_TRUE(ConnectAndWait(ws_url));
569}
570
ricea5acb1faf72015-03-16 15:34:00571// Regression test for crbug.com/180504 "WebSocket handshake fails when HTTP
572// headers have trailing LWS".
Sergey Ulanov4c786d32017-09-08 22:53:25573TEST_F(WebSocketEndToEndTest, TrailingWhitespace) {
ricea5acb1faf72015-03-16 15:34:00574 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea5acb1faf72015-03-16 15:34:00575 GetWebSocketTestDataDirectory());
576 ASSERT_TRUE(ws_server.Start());
577
578 GURL ws_url = ws_server.GetURL("trailing-whitespace");
579 sub_protocols_.push_back("sip");
580 EXPECT_TRUE(ConnectAndWait(ws_url));
581 EXPECT_EQ("sip", event_interface_->selected_subprotocol());
582}
583
riceae1d67672015-03-19 10:10:17584// This is a regression test for crbug.com/169448 "WebSockets should support
585// header continuations"
586// TODO(ricea): HTTP continuation headers have been deprecated by RFC7230. If
587// support for continuation headers is removed from Chrome, then this test will
588// break and should be removed.
Sergey Ulanov4c786d32017-09-08 22:53:25589TEST_F(WebSocketEndToEndTest, HeaderContinuations) {
riceae1d67672015-03-19 10:10:17590 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
riceae1d67672015-03-19 10:10:17591 GetWebSocketTestDataDirectory());
592 ASSERT_TRUE(ws_server.Start());
593
594 GURL ws_url = ws_server.GetURL("header-continuation");
595
596 EXPECT_TRUE(ConnectAndWait(ws_url));
597 EXPECT_EQ("permessage-deflate; server_max_window_bits=10",
598 event_interface_->extensions());
599}
600
ricea433bdab2015-01-26 07:25:37601} // namespace
602
603} // namespace net