blob: e67989f8e4094214608550842d1859961c51127c [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"
ricea433bdab2015-01-26 07:25:3718#include "base/callback.h"
danakjdb9ae7942020-11-11 16:01:3519#include "base/callback_helpers.h"
Eric Orthcd71ede2021-09-14 18:18:5020#include "base/files/file_path.h"
skyostil4891b25b2015-06-11 11:43:4521#include "base/location.h"
Avi Drissman13fc8932015-12-20 04:40:4622#include "base/macros.h"
danakj9c5cab52016-04-16 00:54:3323#include "base/memory/ptr_util.h"
Bence Béky65623972018-03-05 15:31:5624#include "base/memory/scoped_refptr.h"
ricea433bdab2015-01-26 07:25:3725#include "base/run_loop.h"
skyostil4891b25b2015-06-11 11:43:4526#include "base/single_thread_task_runner.h"
Adam Rice5b4a3d82018-08-02 15:28:4327#include "base/strings/strcat.h"
Eric Orthcd71ede2021-09-14 18:18:5028#include "base/strings/string_number_conversions.h"
Adam Ricecb76ac62015-02-20 05:33:2529#include "base/strings/string_piece.h"
Adam Rice5b4a3d82018-08-02 15:28:4330#include "base/strings/stringprintf.h"
Eric Orthcd71ede2021-09-14 18:18:5031#include "base/test/scoped_feature_list.h"
gabf767595f2016-05-11 18:50:3532#include "base/threading/thread_task_runner_handle.h"
Sergey Ulanova337dcd2017-09-08 20:53:1433#include "build/build_config.h"
ricea433bdab2015-01-26 07:25:3734#include "net/base/auth.h"
Eric Orthcd71ede2021-09-14 18:18:5035#include "net/base/features.h"
Adam Rice5b4a3d82018-08-02 15:28:4336#include "net/base/host_port_pair.h"
Tsuyoshi Horo01faed62019-02-20 22:11:3737#include "net/base/ip_endpoint.h"
Matt Menke29a538d2020-04-29 16:12:1738#include "net/base/isolation_info.h"
Chris Thompsone3c3a3b02020-12-17 23:20:4039#include "net/base/net_errors.h"
ryansturm7de050c2016-02-23 00:10:2140#include "net/base/proxy_delegate.h"
Adam Rice5b4a3d82018-08-02 15:28:4341#include "net/base/url_util.h"
Chris Thompsone3c3a3b02020-12-17 23:20:4042#include "net/cert/ct_policy_status.h"
Eric Orthcd71ede2021-09-14 18:18:5043#include "net/dns/mock_host_resolver.h"
Yutaka Hirano2f65eec2018-05-23 01:58:2244#include "net/http/http_request_headers.h"
Matt Menkece5d765372021-08-17 18:24:1245#include "net/http/transport_security_state.h"
Adam Rice5b4a3d82018-08-02 15:28:4346#include "net/log/net_log.h"
Nicolas Arciniegad2013f92020-02-07 23:00:5647#include "net/proxy_resolution/configured_proxy_resolution_service.h"
Adam Rice5b4a3d82018-08-02 15:28:4348#include "net/proxy_resolution/proxy_config.h"
49#include "net/proxy_resolution/proxy_config_service.h"
50#include "net/proxy_resolution/proxy_config_service_fixed.h"
51#include "net/proxy_resolution/proxy_config_with_annotation.h"
52#include "net/proxy_resolution/proxy_info.h"
tommycli59a63432015-11-06 00:10:5553#include "net/test/embedded_test_server/embedded_test_server.h"
Adam Rice5b4a3d82018-08-02 15:28:4354#include "net/test/embedded_test_server/http_request.h"
55#include "net/test/embedded_test_server/http_response.h"
ricea433bdab2015-01-26 07:25:3756#include "net/test/spawned_test_server/spawned_test_server.h"
rsleevia69c79a2016-06-22 03:28:4357#include "net/test/test_data_directory.h"
Gabriel Charettec7108742019-08-23 03:31:4058#include "net/test/test_with_task_environment.h"
rhalavati9ebaba7e2017-04-27 06:16:2959#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
Adam Rice5b4a3d82018-08-02 15:28:4360#include "net/url_request/url_request.h"
61#include "net/url_request/url_request_context.h"
ricea433bdab2015-01-26 07:25:3762#include "net/url_request/url_request_test_util.h"
ricea433bdab2015-01-26 07:25:3763#include "net/websockets/websocket_channel.h"
64#include "net/websockets/websocket_event_interface.h"
ricea433bdab2015-01-26 07:25:3765#include "testing/gtest/include/gtest/gtest.h"
Adam Rice5b4a3d82018-08-02 15:28:4366#include "url/gurl.h"
mkwst4997ce82015-07-25 12:00:0567#include "url/origin.h"
ricea433bdab2015-01-26 07:25:3768
69namespace net {
70
yhirano4a593832016-10-24 18:58:2271class URLRequest;
72
ricea433bdab2015-01-26 07:25:3773namespace {
74
Adam Rice5b4a3d82018-08-02 15:28:4375using test_server::BasicHttpResponse;
76using test_server::HttpRequest;
77using test_server::HttpResponse;
78
ricea433bdab2015-01-26 07:25:3779static const char kEchoServer[] = "echo-with-no-extension";
80
David Benjamin51eeec92021-09-21 03:56:3681// Simplify changing URL schemes.
82GURL ReplaceUrlScheme(const GURL& in_url, const base::StringPiece& scheme) {
83 GURL::Replacements replacements;
84 replacements.SetSchemeStr(scheme);
85 return in_url.ReplaceComponents(replacements);
86}
87
ricea433bdab2015-01-26 07:25:3788// An implementation of WebSocketEventInterface that waits for and records the
89// results of the connect.
90class ConnectTestingEventInterface : public WebSocketEventInterface {
91 public:
92 ConnectTestingEventInterface();
93
Peter Boström407869b2021-10-07 04:42:4894 ConnectTestingEventInterface(const ConnectTestingEventInterface&) = delete;
95 ConnectTestingEventInterface& operator=(const ConnectTestingEventInterface&) =
96 delete;
97
ricea433bdab2015-01-26 07:25:3798 void WaitForResponse();
99
100 bool failed() const { return failed_; }
101
Eric Orthcd71ede2021-09-14 18:18:50102 const std::unique_ptr<WebSocketHandshakeResponseInfo>& response() const {
103 return response_;
104 }
105
ricea433bdab2015-01-26 07:25:37106 // Only set if the handshake failed, otherwise empty.
107 std::string failure_message() const;
108
109 std::string selected_subprotocol() const;
110
111 std::string extensions() const;
112
113 // Implementation of WebSocketEventInterface.
yhirano4a593832016-10-24 18:58:22114 void OnCreateURLRequest(URLRequest* request) override {}
115
Yoichi Osato1ead61a2020-01-06 04:52:57116 void OnAddChannelResponse(
117 std::unique_ptr<WebSocketHandshakeResponseInfo> response,
118 const std::string& selected_subprotocol,
Adam Rice250bb012020-05-26 15:56:10119 const std::string& extensions) override;
ricea433bdab2015-01-26 07:25:37120
Yutaka Hirano4165de92018-04-10 11:46:49121 void OnDataFrame(bool fin,
122 WebSocketMessageType type,
Yutaka Hirano76aacb202019-09-05 16:36:56123 base::span<const char> payload) override;
ricea433bdab2015-01-26 07:25:37124
Yoichi Osatofcaa2a22019-08-28 08:22:36125 bool HasPendingDataFrames() override { return false; }
126
Adam Riced0095702020-05-26 06:18:25127 void OnSendDataFrameDone() override;
ricea433bdab2015-01-26 07:25:37128
Yutaka Hirano4165de92018-04-10 11:46:49129 void OnClosingHandshake() override;
ricea433bdab2015-01-26 07:25:37130
Yutaka Hirano4165de92018-04-10 11:46:49131 void OnDropChannel(bool was_clean,
132 uint16_t code,
133 const std::string& reason) override;
ricea433bdab2015-01-26 07:25:37134
Adam Langleya48b636a2020-11-12 23:42:52135 void OnFailChannel(const std::string& message,
136 int net_error,
Anton Bikineev068d2912021-05-15 20:43:52137 absl::optional<int> response_code) override;
ricea433bdab2015-01-26 07:25:37138
Yutaka Hirano4165de92018-04-10 11:46:49139 void OnStartOpeningHandshake(
danakj9c5cab52016-04-16 00:54:33140 std::unique_ptr<WebSocketHandshakeRequestInfo> request) override;
ricea433bdab2015-01-26 07:25:37141
Yutaka Hirano4165de92018-04-10 11:46:49142 void OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:33143 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks,
ricea433bdab2015-01-26 07:25:37144 const GURL& url,
Emily Starkd9df3d32019-04-29 17:54:57145 int net_error,
ricea433bdab2015-01-26 07:25:37146 const SSLInfo& ssl_info,
147 bool fatal) override;
148
Emily Starkf2c9bbd2019-04-09 17:08:58149 int OnAuthRequired(const AuthChallengeInfo& auth_info,
Yutaka Hirano70fa25912018-06-06 05:26:54150 scoped_refptr<HttpResponseHeaders> response_headers,
Tsuyoshi Horo01faed62019-02-20 22:11:37151 const IPEndPoint& remote_endpoint,
Yutaka Hirano70fa25912018-06-06 05:26:54152 base::OnceCallback<void(const AuthCredentials*)> callback,
Anton Bikineev068d2912021-05-15 20:43:52153 absl::optional<AuthCredentials>* credentials) override;
Yutaka Hirano70fa25912018-06-06 05:26:54154
ricea433bdab2015-01-26 07:25:37155 private:
156 void QuitNestedEventLoop();
157
158 // failed_ is true if the handshake failed (ie. OnFailChannel was called).
159 bool failed_;
Eric Orthcd71ede2021-09-14 18:18:50160 std::unique_ptr<WebSocketHandshakeResponseInfo> response_;
ricea433bdab2015-01-26 07:25:37161 std::string selected_subprotocol_;
162 std::string extensions_;
163 std::string failure_message_;
164 base::RunLoop run_loop_;
ricea433bdab2015-01-26 07:25:37165};
166
tyoshinoc06da562015-03-06 06:02:42167ConnectTestingEventInterface::ConnectTestingEventInterface() : failed_(false) {
ricea433bdab2015-01-26 07:25:37168}
169
170void ConnectTestingEventInterface::WaitForResponse() {
171 run_loop_.Run();
172}
173
174std::string ConnectTestingEventInterface::failure_message() const {
175 return failure_message_;
176}
177
178std::string ConnectTestingEventInterface::selected_subprotocol() const {
179 return selected_subprotocol_;
180}
181
182std::string ConnectTestingEventInterface::extensions() const {
183 return extensions_;
184}
185
Yutaka Hirano4165de92018-04-10 11:46:49186void ConnectTestingEventInterface::OnAddChannelResponse(
Yoichi Osato1ead61a2020-01-06 04:52:57187 std::unique_ptr<WebSocketHandshakeResponseInfo> response,
ricea433bdab2015-01-26 07:25:37188 const std::string& selected_subprotocol,
Adam Rice250bb012020-05-26 15:56:10189 const std::string& extensions) {
Eric Orthcd71ede2021-09-14 18:18:50190 response_ = std::move(response);
ricea433bdab2015-01-26 07:25:37191 selected_subprotocol_ = selected_subprotocol;
192 extensions_ = extensions;
193 QuitNestedEventLoop();
ricea433bdab2015-01-26 07:25:37194}
195
Yutaka Hirano4165de92018-04-10 11:46:49196void ConnectTestingEventInterface::OnDataFrame(bool fin,
197 WebSocketMessageType type,
Yutaka Hirano76aacb202019-09-05 16:36:56198 base::span<const char> payload) {
199}
ricea433bdab2015-01-26 07:25:37200
Adam Riced0095702020-05-26 06:18:25201void ConnectTestingEventInterface::OnSendDataFrameDone() {}
ricea433bdab2015-01-26 07:25:37202
Yutaka Hirano4165de92018-04-10 11:46:49203void ConnectTestingEventInterface::OnClosingHandshake() {}
ricea433bdab2015-01-26 07:25:37204
Yutaka Hirano4165de92018-04-10 11:46:49205void ConnectTestingEventInterface::OnDropChannel(bool was_clean,
206 uint16_t code,
207 const std::string& reason) {}
ricea433bdab2015-01-26 07:25:37208
Adam Langleya48b636a2020-11-12 23:42:52209void ConnectTestingEventInterface::OnFailChannel(
210 const std::string& message,
211 int net_error,
Anton Bikineev068d2912021-05-15 20:43:52212 absl::optional<int> response_code) {
ricea433bdab2015-01-26 07:25:37213 failed_ = true;
214 failure_message_ = message;
215 QuitNestedEventLoop();
ricea433bdab2015-01-26 07:25:37216}
217
Yutaka Hirano4165de92018-04-10 11:46:49218void ConnectTestingEventInterface::OnStartOpeningHandshake(
219 std::unique_ptr<WebSocketHandshakeRequestInfo> request) {}
ricea433bdab2015-01-26 07:25:37220
Yutaka Hirano4165de92018-04-10 11:46:49221void ConnectTestingEventInterface::OnSSLCertificateError(
danakj9c5cab52016-04-16 00:54:33222 std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks,
ricea433bdab2015-01-26 07:25:37223 const GURL& url,
Emily Starkd9df3d32019-04-29 17:54:57224 int net_error,
ricea433bdab2015-01-26 07:25:37225 const SSLInfo& ssl_info,
226 bool fatal) {
skyostil4891b25b2015-06-11 11:43:45227 base::ThreadTaskRunnerHandle::Get()->PostTask(
kylecharf4fe5172019-02-15 18:53:49228 FROM_HERE, base::BindOnce(&SSLErrorCallbacks::CancelSSLRequest,
229 base::Owned(ssl_error_callbacks.release()),
230 ERR_SSL_PROTOCOL_ERROR, &ssl_info));
ricea433bdab2015-01-26 07:25:37231}
232
Yutaka Hirano70fa25912018-06-06 05:26:54233int ConnectTestingEventInterface::OnAuthRequired(
Emily Starkf2c9bbd2019-04-09 17:08:58234 const AuthChallengeInfo& auth_info,
Yutaka Hirano70fa25912018-06-06 05:26:54235 scoped_refptr<HttpResponseHeaders> response_headers,
Tsuyoshi Horo01faed62019-02-20 22:11:37236 const IPEndPoint& remote_endpoint,
Yutaka Hirano70fa25912018-06-06 05:26:54237 base::OnceCallback<void(const AuthCredentials*)> callback,
Anton Bikineev068d2912021-05-15 20:43:52238 absl::optional<AuthCredentials>* credentials) {
239 *credentials = absl::nullopt;
Yutaka Hirano70fa25912018-06-06 05:26:54240 return OK;
241}
242
ricea433bdab2015-01-26 07:25:37243void ConnectTestingEventInterface::QuitNestedEventLoop() {
244 run_loop_.Quit();
245}
246
247// A subclass of TestNetworkDelegate that additionally implements the
248// OnResolveProxy callback and records the information passed to it.
ryansturm7de050c2016-02-23 00:10:21249class TestProxyDelegateWithProxyInfo : public ProxyDelegate {
ricea433bdab2015-01-26 07:25:37250 public:
Chris Watkins28c2fdd2017-11-30 06:06:52251 TestProxyDelegateWithProxyInfo() = default;
ricea433bdab2015-01-26 07:25:37252
Peter Boström407869b2021-10-07 04:42:48253 TestProxyDelegateWithProxyInfo(const TestProxyDelegateWithProxyInfo&) =
254 delete;
255 TestProxyDelegateWithProxyInfo& operator=(
256 const TestProxyDelegateWithProxyInfo&) = delete;
257
ricea433bdab2015-01-26 07:25:37258 struct ResolvedProxyInfo {
259 GURL url;
260 ProxyInfo proxy_info;
261 };
262
263 const ResolvedProxyInfo& resolved_proxy_info() const {
264 return resolved_proxy_info_;
265 }
266
267 protected:
268 void OnResolveProxy(const GURL& url,
ryansturm4bab06832016-03-03 23:41:07269 const std::string& method,
Reilly Grantb414ace72017-11-14 23:03:22270 const ProxyRetryInfoMap& proxy_retry_info,
ricea433bdab2015-01-26 07:25:37271 ProxyInfo* result) override {
272 resolved_proxy_info_.url = url;
273 resolved_proxy_info_.proxy_info = *result;
274 }
275
ryansturm7de050c2016-02-23 00:10:21276 void OnFallback(const ProxyServer& bad_proxy, int net_error) override {}
ryansturm7de050c2016-02-23 00:10:21277
Robert Ogden78d4f9eb2020-03-17 20:56:38278 void OnBeforeTunnelRequest(const ProxyServer& proxy_server,
279 HttpRequestHeaders* extra_headers) override {}
Wojciech Dzierżanowski1f823562019-01-18 11:26:00280
Robert Ogden78d4f9eb2020-03-17 20:56:38281 Error OnTunnelHeadersReceived(
Wojciech Dzierżanowski1f823562019-01-18 11:26:00282 const ProxyServer& proxy_server,
283 const HttpResponseHeaders& response_headers) override {
284 return OK;
285 }
286
ricea433bdab2015-01-26 07:25:37287 private:
288 ResolvedProxyInfo resolved_proxy_info_;
ricea433bdab2015-01-26 07:25:37289};
290
Gabriel Charette694c3c332019-08-19 14:53:05291class WebSocketEndToEndTest : public TestWithTaskEnvironment {
ricea433bdab2015-01-26 07:25:37292 protected:
293 WebSocketEndToEndTest()
Adam Ricecb76ac62015-02-20 05:33:25294 : event_interface_(),
Bence Béky65623972018-03-05 15:31:56295 proxy_delegate_(std::make_unique<TestProxyDelegateWithProxyInfo>()),
ricea433bdab2015-01-26 07:25:37296 context_(true),
Adam Ricecb76ac62015-02-20 05:33:25297 channel_(),
ricea433bdab2015-01-26 07:25:37298 initialised_context_(false) {}
299
300 // Initialise the URLRequestContext. Normally done automatically by
301 // ConnectAndWait(). This method is for the use of tests that need the
302 // URLRequestContext initialised before calling ConnectAndWait().
303 void InitialiseContext() {
ricea433bdab2015-01-26 07:25:37304 context_.Init();
Eric Roman3d8546a2018-09-10 17:00:52305 context_.proxy_resolution_service()->SetProxyDelegate(
306 proxy_delegate_.get());
ricea433bdab2015-01-26 07:25:37307 initialised_context_ = true;
308 }
309
310 // Send the connect request to |socket_url| and wait for a response. Returns
311 // true if the handshake succeeded.
312 bool ConnectAndWait(const GURL& socket_url) {
313 if (!initialised_context_) {
314 InitialiseContext();
315 }
Daniel Cheng88186bd52017-10-20 08:14:46316 url::Origin origin = url::Origin::Create(GURL("http://localhost"));
Maks Orlovich8be0e252019-12-09 18:35:49317 net::SiteForCookies site_for_cookies =
318 net::SiteForCookies::FromOrigin(origin);
shivanigithub4e78015f592020-10-21 13:26:23319 IsolationInfo isolation_info =
320 IsolationInfo::Create(IsolationInfo::RequestType::kOther, origin,
321 origin, SiteForCookies::FromOrigin(origin));
Adam Rice5b4a3d82018-08-02 15:28:43322 event_interface_ = new ConnectTestingEventInterface();
Bence Béky65623972018-03-05 15:31:56323 channel_ = std::make_unique<WebSocketChannel>(
324 base::WrapUnique(event_interface_), &context_);
Adam Langleyacbad242020-08-18 15:14:52325 channel_->SendAddChannelRequest(
326 GURL(socket_url), sub_protocols_, origin, site_for_cookies,
327 isolation_info, HttpRequestHeaders(), TRAFFIC_ANNOTATION_FOR_TESTS);
ricea433bdab2015-01-26 07:25:37328 event_interface_->WaitForResponse();
329 return !event_interface_->failed();
330 }
331
332 ConnectTestingEventInterface* event_interface_; // owned by channel_
danakj9c5cab52016-04-16 00:54:33333 std::unique_ptr<TestProxyDelegateWithProxyInfo> proxy_delegate_;
ricea433bdab2015-01-26 07:25:37334 TestURLRequestContext context_;
danakj9c5cab52016-04-16 00:54:33335 std::unique_ptr<WebSocketChannel> channel_;
ricea5acb1faf72015-03-16 15:34:00336 std::vector<std::string> sub_protocols_;
ricea433bdab2015-01-26 07:25:37337 bool initialised_context_;
338};
339
ricea433bdab2015-01-26 07:25:37340// Basic test of connectivity. If this test fails, nothing else can be expected
341// to work.
Sergey Ulanov4c786d32017-09-08 22:53:25342TEST_F(WebSocketEndToEndTest, BasicSmokeTest) {
ricea433bdab2015-01-26 07:25:37343 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37344 GetWebSocketTestDataDirectory());
345 ASSERT_TRUE(ws_server.Start());
346 EXPECT_TRUE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
347}
348
349// Test for issue crbug.com/433695 "Unencrypted WebSocket connection via
350// authenticated proxy times out"
351// TODO(ricea): Enable this when the issue is fixed.
352TEST_F(WebSocketEndToEndTest, DISABLED_HttpsProxyUnauthedFails) {
353 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37354 base::FilePath());
355 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37356 GetWebSocketTestDataDirectory());
357 ASSERT_TRUE(proxy_server.StartInBackground());
358 ASSERT_TRUE(ws_server.StartInBackground());
359 ASSERT_TRUE(proxy_server.BlockUntilStarted());
360 ASSERT_TRUE(ws_server.BlockUntilStarted());
361 std::string proxy_config =
362 "https=" + proxy_server.host_port_pair().ToString();
Nicolas Arciniega8ec5bfa2020-03-20 05:07:26363 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Nicolas Arciniegad2013f92020-02-07 23:00:56364 ConfiguredProxyResolutionService::CreateFixed(
365 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS));
Lily Houghton8c2f97d2018-01-22 05:06:59366 ASSERT_TRUE(proxy_resolution_service);
367 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37368 EXPECT_FALSE(ConnectAndWait(ws_server.GetURL(kEchoServer)));
369 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
370}
371
Sergey Ulanov4c786d32017-09-08 22:53:25372// These test are not compatible with RemoteTestServer because RemoteTestServer
373// doesn't support TYPE_BASIC_AUTH_PROXY.
374// TODO(ricea): Make these tests work. See crbug.com/441711.
375#if defined(OS_ANDROID) || defined(OS_FUCHSIA)
376#define MAYBE_HttpsWssProxyUnauthedFails DISABLED_HttpsWssProxyUnauthedFails
377#define MAYBE_HttpsProxyUsed DISABLED_HttpsProxyUsed
378#else
379#define MAYBE_HttpsWssProxyUnauthedFails HttpsWssProxyUnauthedFails
380#define MAYBE_HttpsProxyUsed HttpsProxyUsed
381#endif
382
383TEST_F(WebSocketEndToEndTest, MAYBE_HttpsWssProxyUnauthedFails) {
ricea433bdab2015-01-26 07:25:37384 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY,
ricea433bdab2015-01-26 07:25:37385 base::FilePath());
386 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS,
ricea433bdab2015-01-26 07:25:37387 GetWebSocketTestDataDirectory());
388 ASSERT_TRUE(proxy_server.StartInBackground());
389 ASSERT_TRUE(wss_server.StartInBackground());
390 ASSERT_TRUE(proxy_server.BlockUntilStarted());
391 ASSERT_TRUE(wss_server.BlockUntilStarted());
Eric Romanda790f92018-11-07 19:17:15392 ProxyConfig proxy_config;
393 proxy_config.proxy_rules().ParseFromString(
394 "https=" + proxy_server.host_port_pair().ToString());
395 // TODO(https://crbug.com/901896): Don't rely on proxying localhost.
396 proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit();
397
Nicolas Arciniega8ec5bfa2020-03-20 05:07:26398 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Nicolas Arciniegad2013f92020-02-07 23:00:56399 ConfiguredProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation(
Eric Romanda790f92018-11-07 19:17:15400 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)));
Lily Houghton8c2f97d2018-01-22 05:06:59401 ASSERT_TRUE(proxy_resolution_service);
402 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37403 EXPECT_FALSE(ConnectAndWait(wss_server.GetURL(kEchoServer)));
404 EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message());
405}
406
407// Regression test for crbug/426736 "WebSocket connections not using configured
408// system HTTPS Proxy".
Sergey Ulanov4c786d32017-09-08 22:53:25409TEST_F(WebSocketEndToEndTest, MAYBE_HttpsProxyUsed) {
Adam Rice5b4a3d82018-08-02 15:28:43410 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
ricea433bdab2015-01-26 07:25:37411 base::FilePath());
412 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea433bdab2015-01-26 07:25:37413 GetWebSocketTestDataDirectory());
414 ASSERT_TRUE(proxy_server.StartInBackground());
415 ASSERT_TRUE(ws_server.StartInBackground());
416 ASSERT_TRUE(proxy_server.BlockUntilStarted());
417 ASSERT_TRUE(ws_server.BlockUntilStarted());
Eric Romanda790f92018-11-07 19:17:15418 ProxyConfig proxy_config;
419 proxy_config.proxy_rules().ParseFromString(
420 "https=" + proxy_server.host_port_pair().ToString() + ";" +
421 "http=" + proxy_server.host_port_pair().ToString());
422 // TODO(https://crbug.com/901896): Don't rely on proxying localhost.
423 proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit();
424
Nicolas Arciniega8ec5bfa2020-03-20 05:07:26425 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Nicolas Arciniegad2013f92020-02-07 23:00:56426 ConfiguredProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation(
Eric Romanda790f92018-11-07 19:17:15427 proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)));
Lily Houghton8c2f97d2018-01-22 05:06:59428 context_.set_proxy_resolution_service(proxy_resolution_service.get());
ricea433bdab2015-01-26 07:25:37429 InitialiseContext();
430
ricea433bdab2015-01-26 07:25:37431 GURL ws_url = ws_server.GetURL(kEchoServer);
432 EXPECT_TRUE(ConnectAndWait(ws_url));
ryansturm7de050c2016-02-23 00:10:21433 const TestProxyDelegateWithProxyInfo::ResolvedProxyInfo& info =
434 proxy_delegate_->resolved_proxy_info();
ricea433bdab2015-01-26 07:25:37435 EXPECT_EQ(ws_url, info.url);
436 EXPECT_TRUE(info.proxy_info.is_http());
437}
438
Adam Rice5b4a3d82018-08-02 15:28:43439std::unique_ptr<HttpResponse> ProxyPacHandler(const HttpRequest& request) {
440 GURL url = request.GetURL();
441 EXPECT_EQ(url.path_piece(), "/proxy.pac");
442 EXPECT_TRUE(url.has_query());
443 std::string proxy;
444 EXPECT_TRUE(GetValueForKeyInQuery(url, "proxy", &proxy));
445 auto response = std::make_unique<BasicHttpResponse>();
446 response->set_content_type("application/x-ns-proxy-autoconfig");
447 response->set_content(
448 base::StringPrintf("function FindProxyForURL(url, host) {\n"
449 " return 'PROXY %s';\n"
450 "}\n",
451 proxy.c_str()));
452 return response;
453}
454
455// This tests the proxy.pac resolver that is built into the system. This is not
456// the one that Chrome normally uses. Chrome's normal implementation is defined
457// as a mojo service. It is outside //net and we can't use it from here. This
458// tests the alternative implementations that are selected when the
459// --winhttp-proxy-resolver flag is provided to Chrome. These only exist on OS X
460// and Windows.
461// TODO(ricea): Remove this test if --winhttp-proxy-resolver flag is removed.
462// See crbug.com/644030.
463
Avi Drissman25292af62020-07-29 21:57:11464#if defined(OS_WIN) || defined(OS_APPLE)
Adam Rice5b4a3d82018-08-02 15:28:43465#define MAYBE_ProxyPacUsed ProxyPacUsed
466#else
467#define MAYBE_ProxyPacUsed DISABLED_ProxyPacUsed
468#endif
469
470TEST_F(WebSocketEndToEndTest, MAYBE_ProxyPacUsed) {
471 EmbeddedTestServer proxy_pac_server(net::EmbeddedTestServer::Type::TYPE_HTTP);
472 SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY,
473 base::FilePath());
474 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
475 GetWebSocketTestDataDirectory());
476 proxy_pac_server.RegisterRequestHandler(base::BindRepeating(ProxyPacHandler));
477 proxy_server.set_redirect_connect_to_localhost(true);
478
479 ASSERT_TRUE(proxy_pac_server.Start());
480 ASSERT_TRUE(proxy_server.StartInBackground());
481 ASSERT_TRUE(ws_server.StartInBackground());
482 ASSERT_TRUE(proxy_server.BlockUntilStarted());
483 ASSERT_TRUE(ws_server.BlockUntilStarted());
484
485 ProxyConfig proxy_config =
486 ProxyConfig::CreateFromCustomPacURL(proxy_pac_server.GetURL(base::StrCat(
487 {"/proxy.pac?proxy=", proxy_server.host_port_pair().ToString()})));
488 proxy_config.set_pac_mandatory(true);
489 auto proxy_config_service = std::make_unique<ProxyConfigServiceFixed>(
490 ProxyConfigWithAnnotation(proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS));
Nicolas Arciniega8ec5bfa2020-03-20 05:07:26491 std::unique_ptr<ProxyResolutionService> proxy_resolution_service(
Nicolas Arciniegad2013f92020-02-07 23:00:56492 ConfiguredProxyResolutionService::CreateUsingSystemProxyResolver(
Eric Roman3be01ba2020-04-03 21:37:09493 std::move(proxy_config_service), NetLog::Get(),
494 /*quick_check_enabled=*/true));
Adam Rice5b4a3d82018-08-02 15:28:43495 ASSERT_EQ(ws_server.host_port_pair().host(), "127.0.0.1");
496 context_.set_proxy_resolution_service(proxy_resolution_service.get());
497 InitialiseContext();
498
Eric Romanda790f92018-11-07 19:17:15499 // Use a name other than localhost, since localhost implicitly bypasses the
500 // use of proxy.pac.
Adam Rice5b4a3d82018-08-02 15:28:43501 HostPortPair fake_ws_host_port_pair("stealth-localhost",
502 ws_server.host_port_pair().port());
503
504 GURL ws_url(base::StrCat(
505 {"ws://", fake_ws_host_port_pair.ToString(), "/", kEchoServer}));
506 EXPECT_TRUE(ConnectAndWait(ws_url));
507 const auto& info = proxy_delegate_->resolved_proxy_info();
508 EXPECT_EQ(ws_url, info.url);
509 EXPECT_TRUE(info.proxy_info.is_http());
510 EXPECT_EQ(info.proxy_info.ToPacString(),
511 base::StrCat({"PROXY ", proxy_server.host_port_pair().ToString()}));
512}
513
ricea23c3f942015-02-02 13:35:13514// This is a regression test for crbug.com/408061 Crash in
515// net::WebSocketBasicHandshakeStream::Upgrade.
Sergey Ulanov4c786d32017-09-08 22:53:25516TEST_F(WebSocketEndToEndTest, TruncatedResponse) {
ricea23c3f942015-02-02 13:35:13517 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea23c3f942015-02-02 13:35:13518 GetWebSocketTestDataDirectory());
519 ASSERT_TRUE(ws_server.Start());
520 InitialiseContext();
521
522 GURL ws_url = ws_server.GetURL("truncated-headers");
523 EXPECT_FALSE(ConnectAndWait(ws_url));
524}
525
David Benjamin51eeec92021-09-21 03:56:36526// Regression test for crbug.com/455215 "HSTS not applied to WebSocket"
527TEST_F(WebSocketEndToEndTest, HstsHttpsToWebSocket) {
528 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
529 https_server.SetSSLConfig(
530 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
531 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
532
533 SpawnedTestServer::SSLOptions ssl_options(
534 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
535 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
536 GetWebSocketTestDataDirectory());
537
538 ASSERT_TRUE(https_server.Start());
539 ASSERT_TRUE(wss_server.Start());
540 InitialiseContext();
541 // Set HSTS via https:
542 TestDelegate delegate;
543 GURL https_page = https_server.GetURL("/hsts-headers.html");
544 std::unique_ptr<URLRequest> request(context_.CreateRequest(
545 https_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
546 request->Start();
547 delegate.RunUntilComplete();
548 EXPECT_EQ(OK, delegate.request_status());
549
550 // Check HSTS with ws:
551 // Change the scheme from wss: to ws: to verify that it is switched back.
552 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
553 EXPECT_TRUE(ConnectAndWait(ws_url));
554}
555
556TEST_F(WebSocketEndToEndTest, HstsWebSocketToHttps) {
557 EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS);
558 https_server.SetSSLConfig(
559 net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN);
560 https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest");
561
562 SpawnedTestServer::SSLOptions ssl_options(
563 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
564 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
565 GetWebSocketTestDataDirectory());
566 ASSERT_TRUE(https_server.Start());
567 ASSERT_TRUE(wss_server.Start());
568 InitialiseContext();
569 // Set HSTS via wss:
570 GURL wss_url = wss_server.GetURL("set-hsts");
571 EXPECT_TRUE(ConnectAndWait(wss_url));
572
573 // Verify via http:
574 TestDelegate delegate;
575 GURL http_page =
576 ReplaceUrlScheme(https_server.GetURL("/simple.html"), "http");
577 std::unique_ptr<URLRequest> request(context_.CreateRequest(
578 http_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
579 request->Start();
580 delegate.RunUntilComplete();
581 EXPECT_EQ(OK, delegate.request_status());
582 EXPECT_TRUE(request->url().SchemeIs("https"));
583}
584
585TEST_F(WebSocketEndToEndTest, HstsWebSocketToWebSocket) {
586 SpawnedTestServer::SSLOptions ssl_options(
587 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
588 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options,
589 GetWebSocketTestDataDirectory());
590 ASSERT_TRUE(wss_server.Start());
591 InitialiseContext();
592 // Set HSTS via wss:
593 GURL wss_url = wss_server.GetURL("set-hsts");
594 EXPECT_TRUE(ConnectAndWait(wss_url));
595
596 // Verify via wss:
597 GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws");
598 EXPECT_TRUE(ConnectAndWait(ws_url));
599}
600
ricea5acb1faf72015-03-16 15:34:00601// Regression test for crbug.com/180504 "WebSocket handshake fails when HTTP
602// headers have trailing LWS".
Sergey Ulanov4c786d32017-09-08 22:53:25603TEST_F(WebSocketEndToEndTest, TrailingWhitespace) {
ricea5acb1faf72015-03-16 15:34:00604 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
ricea5acb1faf72015-03-16 15:34:00605 GetWebSocketTestDataDirectory());
606 ASSERT_TRUE(ws_server.Start());
607
608 GURL ws_url = ws_server.GetURL("trailing-whitespace");
609 sub_protocols_.push_back("sip");
610 EXPECT_TRUE(ConnectAndWait(ws_url));
611 EXPECT_EQ("sip", event_interface_->selected_subprotocol());
612}
613
riceae1d67672015-03-19 10:10:17614// This is a regression test for crbug.com/169448 "WebSockets should support
615// header continuations"
616// TODO(ricea): HTTP continuation headers have been deprecated by RFC7230. If
617// support for continuation headers is removed from Chrome, then this test will
618// break and should be removed.
Sergey Ulanov4c786d32017-09-08 22:53:25619TEST_F(WebSocketEndToEndTest, HeaderContinuations) {
riceae1d67672015-03-19 10:10:17620 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS,
riceae1d67672015-03-19 10:10:17621 GetWebSocketTestDataDirectory());
622 ASSERT_TRUE(ws_server.Start());
623
624 GURL ws_url = ws_server.GetURL("header-continuation");
625
626 EXPECT_TRUE(ConnectAndWait(ws_url));
627 EXPECT_EQ("permessage-deflate; server_max_window_bits=10",
628 event_interface_->extensions());
629}
630
Eric Orthcd71ede2021-09-14 18:18:50631// Test that ws->wss scheme upgrade is supported on receiving a DNS HTTPS
632// record.
David Benjamin51eeec92021-09-21 03:56:36633TEST_F(WebSocketEndToEndTest, DnsSchemeUpgradeSupported) {
Eric Orthcd71ede2021-09-14 18:18:50634 base::test::ScopedFeatureList features;
635 features.InitAndEnableFeatureWithParameters(
636 features::kUseDnsHttpsSvcb, {{"UseDnsHttpsSvcbHttpUpgrade", "true"}});
637
638 SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS,
639 SpawnedTestServer::SSLOptions(base::FilePath(
640 FILE_PATH_LITERAL("test_names.pem"))),
641 GetWebSocketTestDataDirectory());
642 ASSERT_TRUE(wss_server.Start());
643
644 GURL wss_url("wss://a.test:" +
645 base::NumberToString(wss_server.host_port_pair().port()) + "/" +
646 kEchoServer);
647 GURL::Replacements replacements;
648 replacements.SetSchemeStr(url::kWsScheme);
649 GURL ws_url = wss_url.ReplaceComponents(replacements);
650
651 // Build a mocked resolver that returns ERR_DNS_NAME_HTTPS_ONLY for the
652 // first lookup, regardless of the request scheme. Real resolvers should
653 // only return this error when the scheme is "http" or "ws".
654 MockHostResolver host_resolver;
655 host_resolver.rules()->AddSimulatedHTTPSServiceFormRecord("a.test");
656 host_resolver.rules()->AddRule("*", "127.0.0.1");
657 context_.set_host_resolver(&host_resolver);
658
659 EXPECT_TRUE(ConnectAndWait(ws_url));
660
661 // Expect request to have reached the server using the upgraded URL.
662 EXPECT_EQ(event_interface_->response()->url, wss_url);
663}
664
ricea433bdab2015-01-26 07:25:37665} // namespace
666
667} // namespace net