blob: ce321348d2412b2dacf3083b25c5c92d01ca0a0d [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"
Nicolas Arciniegad2013f92020-02-07 23:00:5638#include "net/proxy_resolution/configured_proxy_resolution_service.h"
Adam Rice5b4a3d82018-08-02 15:28:4339#include "net/proxy_resolution/proxy_config.h"
40#include "net/proxy_resolution/proxy_config_service.h"
41#include "net/proxy_resolution/proxy_config_service_fixed.h"
42#include "net/proxy_resolution/proxy_config_with_annotation.h"
43#include "net/proxy_resolution/proxy_info.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"
Gabriel Charettec7108742019-08-23 03:31:4049#include "net/test/test_with_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
Yoichi Osato1ead61a2020-01-06 04:52:5799 void OnAddChannelResponse(
100 std::unique_ptr<WebSocketHandshakeResponseInfo> response,
101 const std::string& selected_subprotocol,
102 const std::string& extensions,
103 int64_t send_flow_control_quota) override;
ricea433bdab2015-01-26 07:25:37104
Yutaka Hirano4165de92018-04-10 11:46:49105 void OnDataFrame(bool fin,
106 WebSocketMessageType type,
Yutaka Hirano76aacb202019-09-05 16:36:56107 base::span<const char> payload) override;
ricea433bdab2015-01-26 07:25:37108
Yoichi Osatofcaa2a22019-08-28 08:22:36109 bool HasPendingDataFrames() override { return false; }
110
Yoichi Osatob088adc2019-06-06 05:52:19111 void OnSendFlowControlQuotaAdded(int64_t quota) override;
ricea433bdab2015-01-26 07:25:37112
Yutaka Hirano4165de92018-04-10 11:46:49113 void OnClosingHandshake() override;
ricea433bdab2015-01-26 07:25:37114
Yutaka Hirano4165de92018-04-10 11:46:49115 void OnDropChannel(bool was_clean,
116 uint16_t code,
117 const std::string& reason) override;
ricea433bdab2015-01-26 07:25:37118
Yutaka Hirano4165de92018-04-10 11:46:49119 void OnFailChannel(const std::string& message) override;
ricea433bdab2015-01-26 07:25:37120
Yutaka Hirano4165de92018-04-10 11:46:49121 void OnStartOpeningHandshake(
danakj9c5cab52016-04-16 00:54:33122 std::unique_ptr<WebSocketHandshakeRequestInfo> request) override;
ricea433bdab2015-01-26 07:25:37123
Yutaka Hirano4165de92018-04-10 11:46:49124 void OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:33125 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks,
ricea433bdab2015-01-26 07:25:37126 const GURL& url,
Emily Starkd9df3d32019-04-29 17:54:57127 int net_error,
ricea433bdab2015-01-26 07:25:37128 const SSLInfo& ssl_info,
129 bool fatal) override;
130
Emily Starkf2c9bbd2019-04-09 17:08:58131 int OnAuthRequired(const AuthChallengeInfo& auth_info,
Yutaka Hirano70fa25912018-06-06 05:26:54132 scoped_refptr<HttpResponseHeaders> response_headers,
Tsuyoshi Horo01faed62019-02-20 22:11:37133 const IPEndPoint& remote_endpoint,
Yutaka Hirano70fa25912018-06-06 05:26:54134 base::OnceCallback<void(const AuthCredentials*)> callback,
135 base::Optional<AuthCredentials>* credentials) override;
136
ricea433bdab2015-01-26 07:25:37137 private:
138 void QuitNestedEventLoop();
139
140 // failed_ is true if the handshake failed (ie. OnFailChannel was called).
141 bool failed_;
142 std::string selected_subprotocol_;
143 std::string extensions_;
144 std::string failure_message_;
145 base::RunLoop run_loop_;
146
147 DISALLOW_COPY_AND_ASSIGN(ConnectTestingEventInterface);
148};
149
tyoshinoc06da562015-03-06 06:02:42150ConnectTestingEventInterface::ConnectTestingEventInterface() : failed_(false) {
ricea433bdab2015-01-26 07:25:37151}
152
153void ConnectTestingEventInterface::WaitForResponse() {
154 run_loop_.Run();
155}
156
157std::string ConnectTestingEventInterface::failure_message() const {
158 return failure_message_;
159}
160
161std::string ConnectTestingEventInterface::selected_subprotocol() const {
162 return selected_subprotocol_;
163}
164
165std::string ConnectTestingEventInterface::extensions() const {
166 return extensions_;
167}
168
Yutaka Hirano4165de92018-04-10 11:46:49169void ConnectTestingEventInterface::OnAddChannelResponse(
Yoichi Osato1ead61a2020-01-06 04:52:57170 std::unique_ptr<WebSocketHandshakeResponseInfo> response,
ricea433bdab2015-01-26 07:25:37171 const std::string& selected_subprotocol,
Yoichi Osato1bcffbc2019-10-30 03:17:31172 const std::string& extensions,
173 int64_t send_flow_control_quota) {
ricea433bdab2015-01-26 07:25:37174 selected_subprotocol_ = selected_subprotocol;
175 extensions_ = extensions;
176 QuitNestedEventLoop();
ricea433bdab2015-01-26 07:25:37177}
178
Yutaka Hirano4165de92018-04-10 11:46:49179void ConnectTestingEventInterface::OnDataFrame(bool fin,
180 WebSocketMessageType type,
Yutaka Hirano76aacb202019-09-05 16:36:56181 base::span<const char> payload) {
182}
ricea433bdab2015-01-26 07:25:37183
Yoichi Osatob088adc2019-06-06 05:52:19184void ConnectTestingEventInterface::OnSendFlowControlQuotaAdded(int64_t quota) {}
ricea433bdab2015-01-26 07:25:37185
Yutaka Hirano4165de92018-04-10 11:46:49186void ConnectTestingEventInterface::OnClosingHandshake() {}
ricea433bdab2015-01-26 07:25:37187
Yutaka Hirano4165de92018-04-10 11:46:49188void ConnectTestingEventInterface::OnDropChannel(bool was_clean,
189 uint16_t code,
190 const std::string& reason) {}
ricea433bdab2015-01-26 07:25:37191
Yutaka Hirano4165de92018-04-10 11:46:49192void ConnectTestingEventInterface::OnFailChannel(const std::string& message) {
ricea433bdab2015-01-26 07:25:37193 failed_ = true;
194 failure_message_ = message;
195 QuitNestedEventLoop();
ricea433bdab2015-01-26 07:25:37196}
197
Yutaka Hirano4165de92018-04-10 11:46:49198void ConnectTestingEventInterface::OnStartOpeningHandshake(
199 std::unique_ptr<WebSocketHandshakeRequestInfo> request) {}
ricea433bdab2015-01-26 07:25:37200
Yutaka Hirano4165de92018-04-10 11:46:49201void ConnectTestingEventInterface::OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:33202 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks,
ricea433bdab2015-01-26 07:25:37203 const GURL& url,
Emily Starkd9df3d32019-04-29 17:54:57204 int net_error,
ricea433bdab2015-01-26 07:25:37205 const SSLInfo& ssl_info,
206 bool fatal) {
skyostil4891b25b2015-06-11 11:43:45207 base::ThreadTaskRunnerHandle::Get()->PostTask(
kylecharf4fe5172019-02-15 18:53:49208 FROM_HERE, base::BindOnce(&SSLErrorCallbacks::CancelSSLRequest,
209 base::Owned(ssl_error_callbacks.release()),
210 ERR_SSL_PROTOCOL_ERROR, &ssl_info));
ricea433bdab2015-01-26 07:25:37211}
212
Yutaka Hirano70fa25912018-06-06 05:26:54213int ConnectTestingEventInterface::OnAuthRequired(
Emily Starkf2c9bbd2019-04-09 17:08:58214 const AuthChallengeInfo& auth_info,
Yutaka Hirano70fa25912018-06-06 05:26:54215 scoped_refptr<HttpResponseHeaders> response_headers,
Tsuyoshi Horo01faed62019-02-20 22:11:37216 const IPEndPoint& remote_endpoint,
Yutaka Hirano70fa25912018-06-06 05:26:54217 base::OnceCallback<void(const AuthCredentials*)> callback,
218 base::Optional<AuthCredentials>* credentials) {
219 *credentials = base::nullopt;
220 return OK;
221}
222
ricea433bdab2015-01-26 07:25:37223void ConnectTestingEventInterface::QuitNestedEventLoop() {
224 run_loop_.Quit();
225}
226
227// A subclass of TestNetworkDelegate that additionally implements the
228// OnResolveProxy callback and records the information passed to it.
ryansturm7de050c2016-02-23 00:10:21229class TestProxyDelegateWithProxyInfo : public ProxyDelegate {
ricea433bdab2015-01-26 07:25:37230 public:
Chris Watkins28c2fdd2017-11-30 06:06:52231 TestProxyDelegateWithProxyInfo() = default;
ricea433bdab2015-01-26 07:25:37232
233 struct ResolvedProxyInfo {
234 GURL url;
235 ProxyInfo proxy_info;
236 };
237
238 const ResolvedProxyInfo& resolved_proxy_info() const {
239 return resolved_proxy_info_;
240 }
241
242 protected:
243 void OnResolveProxy(const GURL& url,
ryansturm4bab06832016-03-03 23:41:07244 const std::string& method,
Reilly Grantb414ace72017-11-14 23:03:22245 const ProxyRetryInfoMap& proxy_retry_info,
ricea433bdab2015-01-26 07:25:37246 ProxyInfo* result) override {
247 resolved_proxy_info_.url = url;
248 resolved_proxy_info_.proxy_info = *result;
249 }
250
ryansturm7de050c2016-02-23 00:10:21251 void OnFallback(const ProxyServer& bad_proxy, int net_error) override {}
ryansturm7de050c2016-02-23 00:10:21252
Wojciech Dzierżanowskic2704042019-02-07 20:54:28253 void OnBeforeHttp1TunnelRequest(const ProxyServer& proxy_server,
254 HttpRequestHeaders* extra_headers) override {}
Wojciech Dzierżanowski1f823562019-01-18 11:26:00255
Wojciech Dzierżanowskic2704042019-02-07 20:54:28256 Error OnHttp1TunnelHeadersReceived(
Wojciech Dzierżanowski1f823562019-01-18 11:26:00257 const ProxyServer& proxy_server,
258 const HttpResponseHeaders& response_headers) override {
259 return OK;
260 }
261
ricea433bdab2015-01-26 07:25:37262 private:
263 ResolvedProxyInfo resolved_proxy_info_;
264
ryansturm7de050c2016-02-23 00:10:21265 DISALLOW_COPY_AND_ASSIGN(TestProxyDelegateWithProxyInfo);
ricea433bdab2015-01-26 07:25:37266};
267
Gabriel Charette694c3c332019-08-19 14:53:05268class WebSocketEndToEndTest : public TestWithTaskEnvironment {
ricea433bdab2015-01-26 07:25:37269 protected:
270 WebSocketEndToEndTest()
Adam Ricecb76ac62015-02-20 05:33:25271 : event_interface_(),
Bence Béky65623972018-03-05 15:31:56272 proxy_delegate_(std::make_unique<TestProxyDelegateWithProxyInfo>()),
ricea433bdab2015-01-26 07:25:37273 context_(true),
Adam Ricecb76ac62015-02-20 05:33:25274 channel_(),
ricea433bdab2015-01-26 07:25:37275 initialised_context_(false) {}
276
277 // Initialise the URLRequestContext. Normally done automatically by
278 // ConnectAndWait(). This method is for the use of tests that need the
279 // URLRequestContext initialised before calling ConnectAndWait().
280 void InitialiseContext() {
ricea433bdab2015-01-26 07:25:37281 context_.Init();
Eric Roman3d8546a2018-09-10 17:00:52282 context_.proxy_resolution_service()->SetProxyDelegate(
283 proxy_delegate_.get());
ricea433bdab2015-01-26 07:25:37284 initialised_context_ = true;
285 }
286
287 // Send the connect request to |socket_url| and wait for a response. Returns
288 // true if the handshake succeeded.
289 bool ConnectAndWait(const GURL& socket_url) {
290 if (!initialised_context_) {
291 InitialiseContext();
292 }
Daniel Cheng88186bd52017-10-20 08:14:46293 url::Origin origin = url::Origin::Create(GURL("http://localhost"));
Maks Orlovich8be0e252019-12-09 18:35:49294 net::SiteForCookies site_for_cookies =
295 net::SiteForCookies::FromOrigin(origin);
Ehimare Okoyomon4446d8c2019-10-23 12:47:32296 net::NetworkIsolationKey network_isolation_key(origin, origin);
Adam Rice5b4a3d82018-08-02 15:28:43297 event_interface_ = new ConnectTestingEventInterface();
Bence Béky65623972018-03-05 15:31:56298 channel_ = std::make_unique<WebSocketChannel>(
299 base::WrapUnique(event_interface_), &context_);
alladacef397d2016-06-29 17:52:23300 channel_->SendAddChannelRequest(GURL(socket_url), sub_protocols_, origin,
Ehimare Okoyomon4446d8c2019-10-23 12:47:32301 site_for_cookies, network_isolation_key,
302 HttpRequestHeaders());
ricea433bdab2015-01-26 07:25:37303 event_interface_->WaitForResponse();
304 return !event_interface_->failed();
305 }
306
307 ConnectTestingEventInterface* event_interface_; // owned by channel_
danakj9c5cab52016-04-16 00:54:33308 std::unique_ptr<TestProxyDelegateWithProxyInfo> proxy_delegate_;
ricea433bdab2015-01-26 07:25:37309 TestURLRequestContext context_;
danakj9c5cab52016-04-16 00:54:33310 std::unique_ptr<WebSocketChannel> channel_;
ricea5acb1faf72015-03-16 15:34:00311 std::vector<std::string> sub_protocols_;
ricea433bdab2015-01-26 07:25:37312 bool initialised_context_;
313};
314
ricea433bdab2015-01-26 07:25:37315// Basic test of connectivity. If this test fails, nothing else can be expected
316// to work.
Sergey Ulanov4c786d32017-09-08 22:53:25317TEST_F(WebSocketEndToEndTest, BasicSmokeTest) {
ricea433bdab2015-01-26 07:25:37318 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37319 GetWebSocketTestDataDirectory());
320 ASSERT_TRUE(ws_server.Start());
321 EXPECT_TRUE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
322}
323
324// Test for issue crbug.com/433695 "Unencrypted WebSocket connection via
325// authenticated proxy times out"
326// TODO(ricea): Enable this when the issue is fixed.
327TEST_F(WebSocketEndToEndTest, DISABLED_HttpsProxyUnauthedFails) {
328 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37329 base::FilePath());
330 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37331 GetWebSocketTestDataDirectory());
332 ASSERT_TRUE(proxy_server.StartInBackground());
333 ASSERT_TRUE(ws_server.StartInBackground());
334 ASSERT_TRUE(proxy_server.BlockUntilStarted());
335 ASSERT_TRUE(ws_server.BlockUntilStarted());
336 std::string proxy_config =
337 "https=" + proxy_server.host_port_pair().ToString();
Nicolas Arciniegad2013f92020-02-07 23:00:56338 std::unique_ptr<ConfiguredProxyResolutionService> proxy_resolution_service(
339 ConfiguredProxyResolutionService::CreateFixed(
340 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS));
Lily Houghton8c2f97d2018-01-22 05:06:59341 ASSERT_TRUE(proxy_resolution_service);
342 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37343 EXPECT_FALSE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
344 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
345}
346
Sergey Ulanov4c786d32017-09-08 22:53:25347// These test are not compatible with RemoteTestServer because RemoteTestServer
348// doesn't support TYPE_BASIC_AUTH_PROXY.
349// TODO(ricea): Make these tests work. See crbug.com/441711.
350#if defined(OS_ANDROID) || defined(OS_FUCHSIA)
351#define MAYBE_HttpsWssProxyUnauthedFails DISABLED_HttpsWssProxyUnauthedFails
352#define MAYBE_HttpsProxyUsed DISABLED_HttpsProxyUsed
353#else
354#define MAYBE_HttpsWssProxyUnauthedFails HttpsWssProxyUnauthedFails
355#define MAYBE_HttpsProxyUsed HttpsProxyUsed
356#endif
357
358TEST_F(WebSocketEndToEndTest, MAYBE_HttpsWssProxyUnauthedFails) {
ricea433bdab2015-01-26 07:25:37359 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37360 base::FilePath());
361 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS,
ricea433bdab2015-01-26 07:25:37362 GetWebSocketTestDataDirectory());
363 ASSERT_TRUE(proxy_server.StartInBackground());
364 ASSERT_TRUE(wss_server.StartInBackground());
365 ASSERT_TRUE(proxy_server.BlockUntilStarted());
366 ASSERT_TRUE(wss_server.BlockUntilStarted());
Eric Romanda790f92018-11-07 19:17:15367 ProxyConfig proxy_config;
368 proxy_config.proxy_rules().ParseFromString(
369 "https=" + proxy_server.host_port_pair().ToString());
370 // TODO(https://crbug.com/901896): Don't rely on proxying localhost.
371 proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit();
372
Nicolas Arciniegad2013f92020-02-07 23:00:56373 std::unique_ptr<ConfiguredProxyResolutionService> proxy_resolution_service(
374 ConfiguredProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation(
Eric Romanda790f92018-11-07 19:17:15375 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)));
Lily Houghton8c2f97d2018-01-22 05:06:59376 ASSERT_TRUE(proxy_resolution_service);
377 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37378 EXPECT_FALSE(ConnectAndWait(wss_server.GetURL(kEchoServer)));
379 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
380}
381
382// Regression test for crbug/426736 "WebSocket connections not using configured
383// system HTTPS Proxy".
Sergey Ulanov4c786d32017-09-08 22:53:25384TEST_F(WebSocketEndToEndTest, MAYBE_HttpsProxyUsed) {
Adam Rice5b4a3d82018-08-02 15:28:43385 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
ricea433bdab2015-01-26 07:25:37386 base::FilePath());
387 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37388 GetWebSocketTestDataDirectory());
389 ASSERT_TRUE(proxy_server.StartInBackground());
390 ASSERT_TRUE(ws_server.StartInBackground());
391 ASSERT_TRUE(proxy_server.BlockUntilStarted());
392 ASSERT_TRUE(ws_server.BlockUntilStarted());
Eric Romanda790f92018-11-07 19:17:15393 ProxyConfig proxy_config;
394 proxy_config.proxy_rules().ParseFromString(
395 "https=" + proxy_server.host_port_pair().ToString() + ";" +
396 "http=" + proxy_server.host_port_pair().ToString());
397 // TODO(https://crbug.com/901896): Don't rely on proxying localhost.
398 proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit();
399
Nicolas Arciniegad2013f92020-02-07 23:00:56400 std::unique_ptr<ConfiguredProxyResolutionService> proxy_resolution_service(
401 ConfiguredProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation(
Eric Romanda790f92018-11-07 19:17:15402 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)));
Lily Houghton8c2f97d2018-01-22 05:06:59403 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37404 InitialiseContext();
405
ricea433bdab2015-01-26 07:25:37406 GURL ws_url = ws_server.GetURL(kEchoServer);
407 EXPECT_TRUE(ConnectAndWait(ws_url));
ryansturm7de050c2016-02-23 00:10:21408 const TestProxyDelegateWithProxyInfo::ResolvedProxyInfo& info =
409 proxy_delegate_->resolved_proxy_info();
ricea433bdab2015-01-26 07:25:37410 EXPECT_EQ(ws_url, info.url);
411 EXPECT_TRUE(info.proxy_info.is_http());
412}
413
Adam Rice5b4a3d82018-08-02 15:28:43414std::unique_ptr<HttpResponse> ProxyPacHandler(const HttpRequest& request) {
415 GURL url = request.GetURL();
416 EXPECT_EQ(url.path_piece(), "/proxy.pac");
417 EXPECT_TRUE(url.has_query());
418 std::string proxy;
419 EXPECT_TRUE(GetValueForKeyInQuery(url, "proxy", &proxy));
420 auto response = std::make_unique<BasicHttpResponse>();
421 response->set_content_type("application/x-ns-proxy-autoconfig");
422 response->set_content(
423 base::StringPrintf("function FindProxyForURL(url, host) {\n"
424 " return 'PROXY %s';\n"
425 "}\n",
426 proxy.c_str()));
427 return response;
428}
429
430// This tests the proxy.pac resolver that is built into the system. This is not
431// the one that Chrome normally uses. Chrome's normal implementation is defined
432// as a mojo service. It is outside //net and we can't use it from here. This
433// tests the alternative implementations that are selected when the
434// --winhttp-proxy-resolver flag is provided to Chrome. These only exist on OS X
435// and Windows.
436// TODO(ricea): Remove this test if --winhttp-proxy-resolver flag is removed.
437// See crbug.com/644030.
438
439#if defined(OS_WIN) || defined(OS_MACOSX)
440#define MAYBE_ProxyPacUsed ProxyPacUsed
441#else
442#define MAYBE_ProxyPacUsed DISABLED_ProxyPacUsed
443#endif
444
445TEST_F(WebSocketEndToEndTest, MAYBE_ProxyPacUsed) {
446 EmbeddedTestServer proxy_pac_server(net::EmbeddedTestServer::Type::TYPE_HTTP);
447 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
448 base::FilePath());
449 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
450 GetWebSocketTestDataDirectory());
451 proxy_pac_server.RegisterRequestHandler(base::BindRepeating(ProxyPacHandler));
452 proxy_server.set_redirect_connect_to_localhost(true);
453
454 ASSERT_TRUE(proxy_pac_server.Start());
455 ASSERT_TRUE(proxy_server.StartInBackground());
456 ASSERT_TRUE(ws_server.StartInBackground());
457 ASSERT_TRUE(proxy_server.BlockUntilStarted());
458 ASSERT_TRUE(ws_server.BlockUntilStarted());
459
460 ProxyConfig proxy_config =
461 ProxyConfig::CreateFromCustomPacURL(proxy_pac_server.GetURL(base::StrCat(
462 {"/proxy.pac?proxy=", proxy_server.host_port_pair().ToString()})));
463 proxy_config.set_pac_mandatory(true);
464 auto proxy_config_service = std::make_unique<ProxyConfigServiceFixed>(
465 ProxyConfigWithAnnotation(proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS));
Nicolas Arciniegad2013f92020-02-07 23:00:56466 std::unique_ptr<ConfiguredProxyResolutionService> proxy_resolution_service(
467 ConfiguredProxyResolutionService::CreateUsingSystemProxyResolver(
Matt Muellerde5dadf2019-11-27 20:11:58468 std::move(proxy_config_service), NetLog::Get()));
Adam Rice5b4a3d82018-08-02 15:28:43469 ASSERT_EQ(ws_server.host_port_pair().host(), "127.0.0.1");
470 context_.set_proxy_resolution_service(proxy_resolution_service.get());
471 InitialiseContext();
472
Eric Romanda790f92018-11-07 19:17:15473 // Use a name other than localhost, since localhost implicitly bypasses the
474 // use of proxy.pac.
Adam Rice5b4a3d82018-08-02 15:28:43475 HostPortPair fake_ws_host_port_pair("stealth-localhost",
476 ws_server.host_port_pair().port());
477
478 GURL ws_url(base::StrCat(
479 {"ws://", fake_ws_host_port_pair.ToString(), "/", kEchoServer}));
480 EXPECT_TRUE(ConnectAndWait(ws_url));
481 const auto& info = proxy_delegate_->resolved_proxy_info();
482 EXPECT_EQ(ws_url, info.url);
483 EXPECT_TRUE(info.proxy_info.is_http());
484 EXPECT_EQ(info.proxy_info.ToPacString(),
485 base::StrCat({"PROXY ", proxy_server.host_port_pair().ToString()}));
486}
487
ricea23c3f942015-02-02 13:35:13488// This is a regression test for crbug.com/408061 Crash in
489// net::WebSocketBasicHandshakeStream::Upgrade.
Sergey Ulanov4c786d32017-09-08 22:53:25490TEST_F(WebSocketEndToEndTest, TruncatedResponse) {
ricea23c3f942015-02-02 13:35:13491 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea23c3f942015-02-02 13:35:13492 GetWebSocketTestDataDirectory());
493 ASSERT_TRUE(ws_server.Start());
494 InitialiseContext();
495
496 GURL ws_url = ws_server.GetURL("truncated-headers");
497 EXPECT_FALSE(ConnectAndWait(ws_url));
498}
499
Adam Ricecb76ac62015-02-20 05:33:25500// Regression test for crbug.com/455215 "HSTS not applied to WebSocket"
Sergey Ulanov4c786d32017-09-08 22:53:25501TEST_F(WebSocketEndToEndTest, HstsHttpsToWebSocket) {
tommycli59a63432015-11-06 00:10:55502 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
503 https_server.SetSSLConfig(
504 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
505 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
506
estarka5da76702015-04-09 04:00:16507 SpawnedTestServer::SSLOptions ssl_options(
508 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25509 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
510 GetWebSocketTestDataDirectory());
estarka5da76702015-04-09 04:00:16511
tommycli59a63432015-11-06 00:10:55512 ASSERT_TRUE(https_server.Start());
513 ASSERT_TRUE(wss_server.Start());
Adam Ricecb76ac62015-02-20 05:33:25514 InitialiseContext();
515 // Set HSTS via https:
516 TestDelegate delegate;
tommycli59a63432015-11-06 00:10:55517 GURL https_page = https_server.GetURL("/hsts-headers.html");
rhalavati9ebaba7e2017-04-27 06:16:29518 std::unique_ptr<URLRequest> request(context_.CreateRequest(
519 https_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
Adam Ricecb76ac62015-02-20 05:33:25520 request->Start();
Wez2a31b222018-06-07 22:07:15521 delegate.RunUntilComplete();
maksim.sisov7f60c8e2016-09-16 19:38:17522 EXPECT_EQ(OK, delegate.request_status());
Adam Ricecb76ac62015-02-20 05:33:25523
524 // Check HSTS with ws:
525 // Change the scheme from wss: to ws: to verify that it is switched back.
526 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
527 EXPECT_TRUE(ConnectAndWait(ws_url));
528}
529
Sergey Ulanov4c786d32017-09-08 22:53:25530TEST_F(WebSocketEndToEndTest, HstsWebSocketToHttps) {
tommycli59a63432015-11-06 00:10:55531 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
532 https_server.SetSSLConfig(
533 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
534 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
535
estarka5da76702015-04-09 04:00:16536 SpawnedTestServer::SSLOptions ssl_options(
537 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25538 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
539 GetWebSocketTestDataDirectory());
tommycli59a63432015-11-06 00:10:55540 ASSERT_TRUE(https_server.Start());
541 ASSERT_TRUE(wss_server.Start());
Adam Ricecb76ac62015-02-20 05:33:25542 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 http:
548 TestDelegate delegate;
549 GURL http_page =
tommycli59a63432015-11-06 00:10:55550 ReplaceUrlScheme(https_server.GetURL("/simple.html"), "http");
rhalavati9ebaba7e2017-04-27 06:16:29551 std::unique_ptr<URLRequest> request(context_.CreateRequest(
552 http_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
Adam Ricecb76ac62015-02-20 05:33:25553 request->Start();
Wez2a31b222018-06-07 22:07:15554 delegate.RunUntilComplete();
maksim.sisov7f60c8e2016-09-16 19:38:17555 EXPECT_EQ(OK, delegate.request_status());
Adam Ricecb76ac62015-02-20 05:33:25556 EXPECT_TRUE(request->url().SchemeIs("https"));
557}
558
Sergey Ulanov4c786d32017-09-08 22:53:25559TEST_F(WebSocketEndToEndTest, HstsWebSocketToWebSocket) {
estarka5da76702015-04-09 04:00:16560 SpawnedTestServer::SSLOptions ssl_options(
561 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
Adam Ricecb76ac62015-02-20 05:33:25562 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
563 GetWebSocketTestDataDirectory());
564 ASSERT_TRUE(wss_server.Start());
565 InitialiseContext();
566 // Set HSTS via wss:
567 GURL wss_url = wss_server.GetURL("set-hsts");
568 EXPECT_TRUE(ConnectAndWait(wss_url));
569
570 // Verify via wss:
571 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
572 EXPECT_TRUE(ConnectAndWait(ws_url));
573}
574
ricea5acb1faf72015-03-16 15:34:00575// Regression test for crbug.com/180504 "WebSocket handshake fails when HTTP
576// headers have trailing LWS".
Sergey Ulanov4c786d32017-09-08 22:53:25577TEST_F(WebSocketEndToEndTest, TrailingWhitespace) {
ricea5acb1faf72015-03-16 15:34:00578 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea5acb1faf72015-03-16 15:34:00579 GetWebSocketTestDataDirectory());
580 ASSERT_TRUE(ws_server.Start());
581
582 GURL ws_url = ws_server.GetURL("trailing-whitespace");
583 sub_protocols_.push_back("sip");
584 EXPECT_TRUE(ConnectAndWait(ws_url));
585 EXPECT_EQ("sip", event_interface_->selected_subprotocol());
586}
587
riceae1d67672015-03-19 10:10:17588// This is a regression test for crbug.com/169448 "WebSockets should support
589// header continuations"
590// TODO(ricea): HTTP continuation headers have been deprecated by RFC7230. If
591// support for continuation headers is removed from Chrome, then this test will
592// break and should be removed.
Sergey Ulanov4c786d32017-09-08 22:53:25593TEST_F(WebSocketEndToEndTest, HeaderContinuations) {
riceae1d67672015-03-19 10:10:17594 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
riceae1d67672015-03-19 10:10:17595 GetWebSocketTestDataDirectory());
596 ASSERT_TRUE(ws_server.Start());
597
598 GURL ws_url = ws_server.GetURL("header-continuation");
599
600 EXPECT_TRUE(ConnectAndWait(ws_url));
601 EXPECT_EQ("permessage-deflate; server_max_window_bits=10",
602 event_interface_->extensions());
603}
604
ricea433bdab2015-01-26 07:25:37605} // namespace
606
607} // namespace net