ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 1 | // 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 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 11 | #include <stdint.h> |
ryansturm | 4bab0683 | 2016-03-03 23:41:07 | [diff] [blame] | 12 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 13 | #include <memory> |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 14 | #include <string> |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 15 | #include <utility> |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 16 | |
| 17 | #include "base/bind.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 18 | #include "base/callback.h" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 19 | #include "base/callback_helpers.h" |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 20 | #include "base/files/file_path.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 21 | #include "base/location.h" |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 22 | #include "base/memory/ptr_util.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 23 | #include "base/memory/raw_ptr.h" |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 24 | #include "base/memory/scoped_refptr.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 25 | #include "base/run_loop.h" |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 26 | #include "base/strings/strcat.h" |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 27 | #include "base/strings/string_number_conversions.h" |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 28 | #include "base/strings/string_piece.h" |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 29 | #include "base/strings/stringprintf.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 30 | #include "base/task/single_thread_task_runner.h" |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 31 | #include "base/test/scoped_feature_list.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 32 | #include "base/threading/thread_task_runner_handle.h" |
Sergey Ulanov | a337dcd | 2017-09-08 20:53:14 | [diff] [blame] | 33 | #include "build/build_config.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 34 | #include "net/base/auth.h" |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 35 | #include "net/base/features.h" |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 36 | #include "net/base/host_port_pair.h" |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 37 | #include "net/base/ip_endpoint.h" |
Matt Menke | 29a538d | 2020-04-29 16:12:17 | [diff] [blame] | 38 | #include "net/base/isolation_info.h" |
Chris Thompson | e3c3a3b0 | 2020-12-17 23:20:40 | [diff] [blame] | 39 | #include "net/base/net_errors.h" |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 40 | #include "net/base/proxy_delegate.h" |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 41 | #include "net/base/url_util.h" |
Chris Thompson | e3c3a3b0 | 2020-12-17 23:20:40 | [diff] [blame] | 42 | #include "net/cert/ct_policy_status.h" |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 43 | #include "net/dns/mock_host_resolver.h" |
Yutaka Hirano | 2f65eec | 2018-05-23 01:58:22 | [diff] [blame] | 44 | #include "net/http/http_request_headers.h" |
Matt Menke | ce5d76537 | 2021-08-17 18:24:12 | [diff] [blame] | 45 | #include "net/http/transport_security_state.h" |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 46 | #include "net/log/net_log.h" |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 47 | #include "net/proxy_resolution/configured_proxy_resolution_service.h" |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 48 | #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" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 53 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 54 | #include "net/test/embedded_test_server/http_request.h" |
| 55 | #include "net/test/embedded_test_server/http_response.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 56 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 57 | #include "net/test/test_data_directory.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 58 | #include "net/test/test_with_task_environment.h" |
rhalavati | 9ebaba7e | 2017-04-27 06:16:29 | [diff] [blame] | 59 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 60 | #include "net/url_request/url_request.h" |
| 61 | #include "net/url_request/url_request_context.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 62 | #include "net/url_request/url_request_test_util.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 63 | #include "net/websockets/websocket_channel.h" |
| 64 | #include "net/websockets/websocket_event_interface.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 65 | #include "testing/gtest/include/gtest/gtest.h" |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 66 | #include "url/gurl.h" |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 67 | #include "url/origin.h" |
Eric Orth | be86fee | 2021-10-28 22:31:11 | [diff] [blame] | 68 | #include "url/url_constants.h" |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 69 | |
| 70 | namespace net { |
| 71 | |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 72 | class URLRequest; |
| 73 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 74 | namespace { |
| 75 | |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 76 | using test_server::BasicHttpResponse; |
| 77 | using test_server::HttpRequest; |
| 78 | using test_server::HttpResponse; |
| 79 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 80 | static const char kEchoServer[] = "echo-with-no-extension"; |
| 81 | |
David Benjamin | 51eeec9 | 2021-09-21 03:56:36 | [diff] [blame] | 82 | // Simplify changing URL schemes. |
| 83 | GURL ReplaceUrlScheme(const GURL& in_url, const base::StringPiece& scheme) { |
| 84 | GURL::Replacements replacements; |
| 85 | replacements.SetSchemeStr(scheme); |
| 86 | return in_url.ReplaceComponents(replacements); |
| 87 | } |
| 88 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 89 | // An implementation of WebSocketEventInterface that waits for and records the |
| 90 | // results of the connect. |
| 91 | class ConnectTestingEventInterface : public WebSocketEventInterface { |
| 92 | public: |
| 93 | ConnectTestingEventInterface(); |
| 94 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 95 | ConnectTestingEventInterface(const ConnectTestingEventInterface&) = delete; |
| 96 | ConnectTestingEventInterface& operator=(const ConnectTestingEventInterface&) = |
| 97 | delete; |
| 98 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 99 | void WaitForResponse(); |
| 100 | |
| 101 | bool failed() const { return failed_; } |
| 102 | |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 103 | const std::unique_ptr<WebSocketHandshakeResponseInfo>& response() const { |
| 104 | return response_; |
| 105 | } |
| 106 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 107 | // Only set if the handshake failed, otherwise empty. |
| 108 | std::string failure_message() const; |
| 109 | |
| 110 | std::string selected_subprotocol() const; |
| 111 | |
| 112 | std::string extensions() const; |
| 113 | |
| 114 | // Implementation of WebSocketEventInterface. |
yhirano | 4a59383 | 2016-10-24 18:58:22 | [diff] [blame] | 115 | void OnCreateURLRequest(URLRequest* request) override {} |
| 116 | |
Yoichi Osato | 1ead61a | 2020-01-06 04:52:57 | [diff] [blame] | 117 | void OnAddChannelResponse( |
| 118 | std::unique_ptr<WebSocketHandshakeResponseInfo> response, |
| 119 | const std::string& selected_subprotocol, |
Adam Rice | 250bb01 | 2020-05-26 15:56:10 | [diff] [blame] | 120 | const std::string& extensions) override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 121 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 122 | void OnDataFrame(bool fin, |
| 123 | WebSocketMessageType type, |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 124 | base::span<const char> payload) override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 125 | |
Yoichi Osato | fcaa2a2 | 2019-08-28 08:22:36 | [diff] [blame] | 126 | bool HasPendingDataFrames() override { return false; } |
| 127 | |
Adam Rice | d009570 | 2020-05-26 06:18:25 | [diff] [blame] | 128 | void OnSendDataFrameDone() override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 129 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 130 | void OnClosingHandshake() override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 131 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 132 | void OnDropChannel(bool was_clean, |
| 133 | uint16_t code, |
| 134 | const std::string& reason) override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 135 | |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 136 | void OnFailChannel(const std::string& message, |
| 137 | int net_error, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 138 | absl::optional<int> response_code) override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 139 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 140 | void OnStartOpeningHandshake( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 141 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) override; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 142 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 143 | void OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 144 | std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 145 | const GURL& url, |
Emily Stark | d9df3d3 | 2019-04-29 17:54:57 | [diff] [blame] | 146 | int net_error, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 147 | const SSLInfo& ssl_info, |
| 148 | bool fatal) override; |
| 149 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 150 | int OnAuthRequired(const AuthChallengeInfo& auth_info, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 151 | scoped_refptr<HttpResponseHeaders> response_headers, |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 152 | const IPEndPoint& remote_endpoint, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 153 | base::OnceCallback<void(const AuthCredentials*)> callback, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 154 | absl::optional<AuthCredentials>* credentials) override; |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 155 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 156 | private: |
| 157 | void QuitNestedEventLoop(); |
| 158 | |
| 159 | // failed_ is true if the handshake failed (ie. OnFailChannel was called). |
| 160 | bool failed_; |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 161 | std::unique_ptr<WebSocketHandshakeResponseInfo> response_; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 162 | std::string selected_subprotocol_; |
| 163 | std::string extensions_; |
| 164 | std::string failure_message_; |
| 165 | base::RunLoop run_loop_; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 166 | }; |
| 167 | |
tyoshino | c06da56 | 2015-03-06 06:02:42 | [diff] [blame] | 168 | ConnectTestingEventInterface::ConnectTestingEventInterface() : failed_(false) { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void ConnectTestingEventInterface::WaitForResponse() { |
| 172 | run_loop_.Run(); |
| 173 | } |
| 174 | |
| 175 | std::string ConnectTestingEventInterface::failure_message() const { |
| 176 | return failure_message_; |
| 177 | } |
| 178 | |
| 179 | std::string ConnectTestingEventInterface::selected_subprotocol() const { |
| 180 | return selected_subprotocol_; |
| 181 | } |
| 182 | |
| 183 | std::string ConnectTestingEventInterface::extensions() const { |
| 184 | return extensions_; |
| 185 | } |
| 186 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 187 | void ConnectTestingEventInterface::OnAddChannelResponse( |
Yoichi Osato | 1ead61a | 2020-01-06 04:52:57 | [diff] [blame] | 188 | std::unique_ptr<WebSocketHandshakeResponseInfo> response, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 189 | const std::string& selected_subprotocol, |
Adam Rice | 250bb01 | 2020-05-26 15:56:10 | [diff] [blame] | 190 | const std::string& extensions) { |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 191 | response_ = std::move(response); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 192 | selected_subprotocol_ = selected_subprotocol; |
| 193 | extensions_ = extensions; |
| 194 | QuitNestedEventLoop(); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 195 | } |
| 196 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 197 | void ConnectTestingEventInterface::OnDataFrame(bool fin, |
| 198 | WebSocketMessageType type, |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 199 | base::span<const char> payload) { |
| 200 | } |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 201 | |
Adam Rice | d009570 | 2020-05-26 06:18:25 | [diff] [blame] | 202 | void ConnectTestingEventInterface::OnSendDataFrameDone() {} |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 203 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 204 | void ConnectTestingEventInterface::OnClosingHandshake() {} |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 205 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 206 | void ConnectTestingEventInterface::OnDropChannel(bool was_clean, |
| 207 | uint16_t code, |
| 208 | const std::string& reason) {} |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 209 | |
Adam Langley | a48b636a | 2020-11-12 23:42:52 | [diff] [blame] | 210 | void ConnectTestingEventInterface::OnFailChannel( |
| 211 | const std::string& message, |
| 212 | int net_error, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 213 | absl::optional<int> response_code) { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 214 | failed_ = true; |
| 215 | failure_message_ = message; |
| 216 | QuitNestedEventLoop(); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 217 | } |
| 218 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 219 | void ConnectTestingEventInterface::OnStartOpeningHandshake( |
| 220 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) {} |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 221 | |
Yutaka Hirano | 4165de9 | 2018-04-10 11:46:49 | [diff] [blame] | 222 | void ConnectTestingEventInterface::OnSSLCertificateError( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 223 | std::unique_ptr<SSLErrorCallbacks> ssl_error_callbacks, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 224 | const GURL& url, |
Emily Stark | d9df3d3 | 2019-04-29 17:54:57 | [diff] [blame] | 225 | int net_error, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 226 | const SSLInfo& ssl_info, |
| 227 | bool fatal) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 228 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 229 | FROM_HERE, base::BindOnce(&SSLErrorCallbacks::CancelSSLRequest, |
| 230 | base::Owned(ssl_error_callbacks.release()), |
| 231 | ERR_SSL_PROTOCOL_ERROR, &ssl_info)); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 232 | } |
| 233 | |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 234 | int ConnectTestingEventInterface::OnAuthRequired( |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 235 | const AuthChallengeInfo& auth_info, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 236 | scoped_refptr<HttpResponseHeaders> response_headers, |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 237 | const IPEndPoint& remote_endpoint, |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 238 | base::OnceCallback<void(const AuthCredentials*)> callback, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 239 | absl::optional<AuthCredentials>* credentials) { |
| 240 | *credentials = absl::nullopt; |
Yutaka Hirano | 70fa2591 | 2018-06-06 05:26:54 | [diff] [blame] | 241 | return OK; |
| 242 | } |
| 243 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 244 | void ConnectTestingEventInterface::QuitNestedEventLoop() { |
| 245 | run_loop_.Quit(); |
| 246 | } |
| 247 | |
| 248 | // A subclass of TestNetworkDelegate that additionally implements the |
| 249 | // OnResolveProxy callback and records the information passed to it. |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 250 | class TestProxyDelegateWithProxyInfo : public ProxyDelegate { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 251 | public: |
Chris Watkins | 28c2fdd | 2017-11-30 06:06:52 | [diff] [blame] | 252 | TestProxyDelegateWithProxyInfo() = default; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 253 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 254 | TestProxyDelegateWithProxyInfo(const TestProxyDelegateWithProxyInfo&) = |
| 255 | delete; |
| 256 | TestProxyDelegateWithProxyInfo& operator=( |
| 257 | const TestProxyDelegateWithProxyInfo&) = delete; |
| 258 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 259 | struct ResolvedProxyInfo { |
| 260 | GURL url; |
| 261 | ProxyInfo proxy_info; |
| 262 | }; |
| 263 | |
| 264 | const ResolvedProxyInfo& resolved_proxy_info() const { |
| 265 | return resolved_proxy_info_; |
| 266 | } |
| 267 | |
| 268 | protected: |
| 269 | void OnResolveProxy(const GURL& url, |
ryansturm | 4bab0683 | 2016-03-03 23:41:07 | [diff] [blame] | 270 | const std::string& method, |
Reilly Grant | b414ace7 | 2017-11-14 23:03:22 | [diff] [blame] | 271 | const ProxyRetryInfoMap& proxy_retry_info, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 272 | ProxyInfo* result) override { |
| 273 | resolved_proxy_info_.url = url; |
| 274 | resolved_proxy_info_.proxy_info = *result; |
| 275 | } |
| 276 | |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 277 | void OnFallback(const ProxyServer& bad_proxy, int net_error) override {} |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 278 | |
Robert Ogden | 78d4f9eb | 2020-03-17 20:56:38 | [diff] [blame] | 279 | void OnBeforeTunnelRequest(const ProxyServer& proxy_server, |
| 280 | HttpRequestHeaders* extra_headers) override {} |
Wojciech Dzierżanowski | 1f82356 | 2019-01-18 11:26:00 | [diff] [blame] | 281 | |
Robert Ogden | 78d4f9eb | 2020-03-17 20:56:38 | [diff] [blame] | 282 | Error OnTunnelHeadersReceived( |
Wojciech Dzierżanowski | 1f82356 | 2019-01-18 11:26:00 | [diff] [blame] | 283 | const ProxyServer& proxy_server, |
| 284 | const HttpResponseHeaders& response_headers) override { |
| 285 | return OK; |
| 286 | } |
| 287 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 288 | private: |
| 289 | ResolvedProxyInfo resolved_proxy_info_; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 290 | }; |
| 291 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 292 | class WebSocketEndToEndTest : public TestWithTaskEnvironment { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 293 | protected: |
| 294 | WebSocketEndToEndTest() |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 295 | : event_interface_(), |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 296 | proxy_delegate_(std::make_unique<TestProxyDelegateWithProxyInfo>()), |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 297 | context_(true), |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 298 | channel_(), |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 299 | initialised_context_(false) {} |
| 300 | |
| 301 | // Initialise the URLRequestContext. Normally done automatically by |
| 302 | // ConnectAndWait(). This method is for the use of tests that need the |
| 303 | // URLRequestContext initialised before calling ConnectAndWait(). |
| 304 | void InitialiseContext() { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 305 | context_.Init(); |
Eric Roman | 3d8546a | 2018-09-10 17:00:52 | [diff] [blame] | 306 | context_.proxy_resolution_service()->SetProxyDelegate( |
| 307 | proxy_delegate_.get()); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 308 | initialised_context_ = true; |
| 309 | } |
| 310 | |
| 311 | // Send the connect request to |socket_url| and wait for a response. Returns |
| 312 | // true if the handshake succeeded. |
| 313 | bool ConnectAndWait(const GURL& socket_url) { |
| 314 | if (!initialised_context_) { |
| 315 | InitialiseContext(); |
| 316 | } |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 317 | url::Origin origin = url::Origin::Create(GURL("http://localhost")); |
Maks Orlovich | 8be0e25 | 2019-12-09 18:35:49 | [diff] [blame] | 318 | net::SiteForCookies site_for_cookies = |
| 319 | net::SiteForCookies::FromOrigin(origin); |
shivanigithub | 4e78015f59 | 2020-10-21 13:26:23 | [diff] [blame] | 320 | IsolationInfo isolation_info = |
| 321 | IsolationInfo::Create(IsolationInfo::RequestType::kOther, origin, |
| 322 | origin, SiteForCookies::FromOrigin(origin)); |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 323 | event_interface_ = new ConnectTestingEventInterface(); |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 324 | channel_ = std::make_unique<WebSocketChannel>( |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 325 | base::WrapUnique(event_interface_.get()), &context_); |
Adam Langley | acbad24 | 2020-08-18 15:14:52 | [diff] [blame] | 326 | channel_->SendAddChannelRequest( |
| 327 | GURL(socket_url), sub_protocols_, origin, site_for_cookies, |
| 328 | isolation_info, HttpRequestHeaders(), TRAFFIC_ANNOTATION_FOR_TESTS); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 329 | event_interface_->WaitForResponse(); |
| 330 | return !event_interface_->failed(); |
| 331 | } |
| 332 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 333 | raw_ptr<ConnectTestingEventInterface> event_interface_; // owned by channel_ |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 334 | std::unique_ptr<TestProxyDelegateWithProxyInfo> proxy_delegate_; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 335 | TestURLRequestContext context_; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 336 | std::unique_ptr<WebSocketChannel> channel_; |
ricea | 5acb1faf7 | 2015-03-16 15:34:00 | [diff] [blame] | 337 | std::vector<std::string> sub_protocols_; |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 338 | bool initialised_context_; |
| 339 | }; |
| 340 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 341 | // Basic test of connectivity. If this test fails, nothing else can be expected |
| 342 | // to work. |
Sergey Ulanov | 4c786d3 | 2017-09-08 22:53:25 | [diff] [blame] | 343 | TEST_F(WebSocketEndToEndTest, BasicSmokeTest) { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 344 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 345 | GetWebSocketTestDataDirectory()); |
| 346 | ASSERT_TRUE(ws_server.Start()); |
| 347 | EXPECT_TRUE(ConnectAndWait(ws_server.GetURL(kEchoServer))); |
| 348 | } |
| 349 | |
| 350 | // Test for issue crbug.com/433695 "Unencrypted WebSocket connection via |
| 351 | // authenticated proxy times out" |
| 352 | // TODO(ricea): Enable this when the issue is fixed. |
| 353 | TEST_F(WebSocketEndToEndTest, DISABLED_HttpsProxyUnauthedFails) { |
| 354 | SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 355 | base::FilePath()); |
| 356 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 357 | GetWebSocketTestDataDirectory()); |
| 358 | ASSERT_TRUE(proxy_server.StartInBackground()); |
| 359 | ASSERT_TRUE(ws_server.StartInBackground()); |
| 360 | ASSERT_TRUE(proxy_server.BlockUntilStarted()); |
| 361 | ASSERT_TRUE(ws_server.BlockUntilStarted()); |
| 362 | std::string proxy_config = |
| 363 | "https=" + proxy_server.host_port_pair().ToString(); |
Nicolas Arciniega | 8ec5bfa | 2020-03-20 05:07:26 | [diff] [blame] | 364 | std::unique_ptr<ProxyResolutionService> proxy_resolution_service( |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 365 | ConfiguredProxyResolutionService::CreateFixed( |
| 366 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 367 | ASSERT_TRUE(proxy_resolution_service); |
| 368 | context_.set_proxy_resolution_service(proxy_resolution_service.get()); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 369 | EXPECT_FALSE(ConnectAndWait(ws_server.GetURL(kEchoServer))); |
| 370 | EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message()); |
| 371 | } |
| 372 | |
Sergey Ulanov | 4c786d3 | 2017-09-08 22:53:25 | [diff] [blame] | 373 | // These test are not compatible with RemoteTestServer because RemoteTestServer |
| 374 | // doesn't support TYPE_BASIC_AUTH_PROXY. |
| 375 | // TODO(ricea): Make these tests work. See crbug.com/441711. |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame^] | 376 | #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_FUCHSIA) |
Sergey Ulanov | 4c786d3 | 2017-09-08 22:53:25 | [diff] [blame] | 377 | #define MAYBE_HttpsWssProxyUnauthedFails DISABLED_HttpsWssProxyUnauthedFails |
| 378 | #define MAYBE_HttpsProxyUsed DISABLED_HttpsProxyUsed |
| 379 | #else |
| 380 | #define MAYBE_HttpsWssProxyUnauthedFails HttpsWssProxyUnauthedFails |
| 381 | #define MAYBE_HttpsProxyUsed HttpsProxyUsed |
| 382 | #endif |
| 383 | |
| 384 | TEST_F(WebSocketEndToEndTest, MAYBE_HttpsWssProxyUnauthedFails) { |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 385 | SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_BASIC_AUTH_PROXY, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 386 | base::FilePath()); |
| 387 | SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 388 | GetWebSocketTestDataDirectory()); |
| 389 | ASSERT_TRUE(proxy_server.StartInBackground()); |
| 390 | ASSERT_TRUE(wss_server.StartInBackground()); |
| 391 | ASSERT_TRUE(proxy_server.BlockUntilStarted()); |
| 392 | ASSERT_TRUE(wss_server.BlockUntilStarted()); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 393 | ProxyConfig proxy_config; |
| 394 | proxy_config.proxy_rules().ParseFromString( |
| 395 | "https=" + proxy_server.host_port_pair().ToString()); |
| 396 | // TODO(https://crbug.com/901896): Don't rely on proxying localhost. |
| 397 | proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit(); |
| 398 | |
Nicolas Arciniega | 8ec5bfa | 2020-03-20 05:07:26 | [diff] [blame] | 399 | std::unique_ptr<ProxyResolutionService> proxy_resolution_service( |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 400 | ConfiguredProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation( |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 401 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS))); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 402 | ASSERT_TRUE(proxy_resolution_service); |
| 403 | context_.set_proxy_resolution_service(proxy_resolution_service.get()); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 404 | EXPECT_FALSE(ConnectAndWait(wss_server.GetURL(kEchoServer))); |
| 405 | EXPECT_EQ("Proxy authentication failed", event_interface_->failure_message()); |
| 406 | } |
| 407 | |
| 408 | // Regression test for crbug/426736 "WebSocket connections not using configured |
| 409 | // system HTTPS Proxy". |
Sergey Ulanov | 4c786d3 | 2017-09-08 22:53:25 | [diff] [blame] | 410 | TEST_F(WebSocketEndToEndTest, MAYBE_HttpsProxyUsed) { |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 411 | SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 412 | base::FilePath()); |
| 413 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 414 | GetWebSocketTestDataDirectory()); |
| 415 | ASSERT_TRUE(proxy_server.StartInBackground()); |
| 416 | ASSERT_TRUE(ws_server.StartInBackground()); |
| 417 | ASSERT_TRUE(proxy_server.BlockUntilStarted()); |
| 418 | ASSERT_TRUE(ws_server.BlockUntilStarted()); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 419 | ProxyConfig proxy_config; |
| 420 | proxy_config.proxy_rules().ParseFromString( |
| 421 | "https=" + proxy_server.host_port_pair().ToString() + ";" + |
| 422 | "http=" + proxy_server.host_port_pair().ToString()); |
| 423 | // TODO(https://crbug.com/901896): Don't rely on proxying localhost. |
| 424 | proxy_config.proxy_rules().bypass_rules.AddRulesToSubtractImplicit(); |
| 425 | |
Nicolas Arciniega | 8ec5bfa | 2020-03-20 05:07:26 | [diff] [blame] | 426 | std::unique_ptr<ProxyResolutionService> proxy_resolution_service( |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 427 | ConfiguredProxyResolutionService::CreateFixed(ProxyConfigWithAnnotation( |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 428 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS))); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 429 | context_.set_proxy_resolution_service(proxy_resolution_service.get()); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 430 | InitialiseContext(); |
| 431 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 432 | GURL ws_url = ws_server.GetURL(kEchoServer); |
| 433 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
ryansturm | 7de050c | 2016-02-23 00:10:21 | [diff] [blame] | 434 | const TestProxyDelegateWithProxyInfo::ResolvedProxyInfo& info = |
| 435 | proxy_delegate_->resolved_proxy_info(); |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 436 | EXPECT_EQ(ws_url, info.url); |
| 437 | EXPECT_TRUE(info.proxy_info.is_http()); |
| 438 | } |
| 439 | |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 440 | std::unique_ptr<HttpResponse> ProxyPacHandler(const HttpRequest& request) { |
| 441 | GURL url = request.GetURL(); |
| 442 | EXPECT_EQ(url.path_piece(), "/proxy.pac"); |
| 443 | EXPECT_TRUE(url.has_query()); |
| 444 | std::string proxy; |
| 445 | EXPECT_TRUE(GetValueForKeyInQuery(url, "proxy", &proxy)); |
| 446 | auto response = std::make_unique<BasicHttpResponse>(); |
| 447 | response->set_content_type("application/x-ns-proxy-autoconfig"); |
| 448 | response->set_content( |
| 449 | base::StringPrintf("function FindProxyForURL(url, host) {\n" |
| 450 | " return 'PROXY %s';\n" |
| 451 | "}\n", |
| 452 | proxy.c_str())); |
| 453 | return response; |
| 454 | } |
| 455 | |
| 456 | // This tests the proxy.pac resolver that is built into the system. This is not |
| 457 | // the one that Chrome normally uses. Chrome's normal implementation is defined |
| 458 | // as a mojo service. It is outside //net and we can't use it from here. This |
| 459 | // tests the alternative implementations that are selected when the |
| 460 | // --winhttp-proxy-resolver flag is provided to Chrome. These only exist on OS X |
| 461 | // and Windows. |
| 462 | // TODO(ricea): Remove this test if --winhttp-proxy-resolver flag is removed. |
| 463 | // See crbug.com/644030. |
| 464 | |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame^] | 465 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_APPLE) |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 466 | #define MAYBE_ProxyPacUsed ProxyPacUsed |
| 467 | #else |
| 468 | #define MAYBE_ProxyPacUsed DISABLED_ProxyPacUsed |
| 469 | #endif |
| 470 | |
| 471 | TEST_F(WebSocketEndToEndTest, MAYBE_ProxyPacUsed) { |
| 472 | EmbeddedTestServer proxy_pac_server(net::EmbeddedTestServer::Type::TYPE_HTTP); |
| 473 | SpawnedTestServer proxy_server(SpawnedTestServer::TYPE_PROXY, |
| 474 | base::FilePath()); |
| 475 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
| 476 | GetWebSocketTestDataDirectory()); |
| 477 | proxy_pac_server.RegisterRequestHandler(base::BindRepeating(ProxyPacHandler)); |
| 478 | proxy_server.set_redirect_connect_to_localhost(true); |
| 479 | |
| 480 | ASSERT_TRUE(proxy_pac_server.Start()); |
| 481 | ASSERT_TRUE(proxy_server.StartInBackground()); |
| 482 | ASSERT_TRUE(ws_server.StartInBackground()); |
| 483 | ASSERT_TRUE(proxy_server.BlockUntilStarted()); |
| 484 | ASSERT_TRUE(ws_server.BlockUntilStarted()); |
| 485 | |
| 486 | ProxyConfig proxy_config = |
| 487 | ProxyConfig::CreateFromCustomPacURL(proxy_pac_server.GetURL(base::StrCat( |
| 488 | {"/proxy.pac?proxy=", proxy_server.host_port_pair().ToString()}))); |
| 489 | proxy_config.set_pac_mandatory(true); |
| 490 | auto proxy_config_service = std::make_unique<ProxyConfigServiceFixed>( |
| 491 | ProxyConfigWithAnnotation(proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Nicolas Arciniega | 8ec5bfa | 2020-03-20 05:07:26 | [diff] [blame] | 492 | std::unique_ptr<ProxyResolutionService> proxy_resolution_service( |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 493 | ConfiguredProxyResolutionService::CreateUsingSystemProxyResolver( |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 494 | std::move(proxy_config_service), NetLog::Get(), |
| 495 | /*quick_check_enabled=*/true)); |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 496 | ASSERT_EQ(ws_server.host_port_pair().host(), "127.0.0.1"); |
| 497 | context_.set_proxy_resolution_service(proxy_resolution_service.get()); |
| 498 | InitialiseContext(); |
| 499 | |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 500 | // Use a name other than localhost, since localhost implicitly bypasses the |
| 501 | // use of proxy.pac. |
Adam Rice | 5b4a3d8 | 2018-08-02 15:28:43 | [diff] [blame] | 502 | HostPortPair fake_ws_host_port_pair("stealth-localhost", |
| 503 | ws_server.host_port_pair().port()); |
| 504 | |
| 505 | GURL ws_url(base::StrCat( |
| 506 | {"ws://", fake_ws_host_port_pair.ToString(), "/", kEchoServer})); |
| 507 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 508 | const auto& info = proxy_delegate_->resolved_proxy_info(); |
| 509 | EXPECT_EQ(ws_url, info.url); |
| 510 | EXPECT_TRUE(info.proxy_info.is_http()); |
| 511 | EXPECT_EQ(info.proxy_info.ToPacString(), |
| 512 | base::StrCat({"PROXY ", proxy_server.host_port_pair().ToString()})); |
| 513 | } |
| 514 | |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 515 | // This is a regression test for crbug.com/408061 Crash in |
| 516 | // net::WebSocketBasicHandshakeStream::Upgrade. |
Sergey Ulanov | 4c786d3 | 2017-09-08 22:53:25 | [diff] [blame] | 517 | TEST_F(WebSocketEndToEndTest, TruncatedResponse) { |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 518 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
ricea | 23c3f94 | 2015-02-02 13:35:13 | [diff] [blame] | 519 | GetWebSocketTestDataDirectory()); |
| 520 | ASSERT_TRUE(ws_server.Start()); |
| 521 | InitialiseContext(); |
| 522 | |
| 523 | GURL ws_url = ws_server.GetURL("truncated-headers"); |
| 524 | EXPECT_FALSE(ConnectAndWait(ws_url)); |
| 525 | } |
| 526 | |
David Benjamin | 51eeec9 | 2021-09-21 03:56:36 | [diff] [blame] | 527 | // Regression test for crbug.com/455215 "HSTS not applied to WebSocket" |
| 528 | TEST_F(WebSocketEndToEndTest, HstsHttpsToWebSocket) { |
| 529 | EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS); |
| 530 | https_server.SetSSLConfig( |
| 531 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 532 | https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest"); |
| 533 | |
| 534 | SpawnedTestServer::SSLOptions ssl_options( |
| 535 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
| 536 | SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options, |
| 537 | GetWebSocketTestDataDirectory()); |
| 538 | |
| 539 | ASSERT_TRUE(https_server.Start()); |
| 540 | ASSERT_TRUE(wss_server.Start()); |
| 541 | InitialiseContext(); |
| 542 | // Set HSTS via https: |
| 543 | TestDelegate delegate; |
| 544 | GURL https_page = https_server.GetURL("/hsts-headers.html"); |
| 545 | std::unique_ptr<URLRequest> request(context_.CreateRequest( |
| 546 | https_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 547 | request->Start(); |
| 548 | delegate.RunUntilComplete(); |
| 549 | EXPECT_EQ(OK, delegate.request_status()); |
| 550 | |
| 551 | // Check HSTS with ws: |
| 552 | // Change the scheme from wss: to ws: to verify that it is switched back. |
| 553 | GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws"); |
| 554 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 555 | } |
| 556 | |
| 557 | TEST_F(WebSocketEndToEndTest, HstsWebSocketToHttps) { |
| 558 | EmbeddedTestServer https_server(net::EmbeddedTestServer::Type::TYPE_HTTPS); |
| 559 | https_server.SetSSLConfig( |
| 560 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 561 | https_server.ServeFilesFromSourceDirectory("net/data/url_request_unittest"); |
| 562 | |
| 563 | SpawnedTestServer::SSLOptions ssl_options( |
| 564 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
| 565 | SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options, |
| 566 | GetWebSocketTestDataDirectory()); |
| 567 | ASSERT_TRUE(https_server.Start()); |
| 568 | ASSERT_TRUE(wss_server.Start()); |
| 569 | InitialiseContext(); |
| 570 | // Set HSTS via wss: |
| 571 | GURL wss_url = wss_server.GetURL("set-hsts"); |
| 572 | EXPECT_TRUE(ConnectAndWait(wss_url)); |
| 573 | |
| 574 | // Verify via http: |
| 575 | TestDelegate delegate; |
| 576 | GURL http_page = |
| 577 | ReplaceUrlScheme(https_server.GetURL("/simple.html"), "http"); |
| 578 | std::unique_ptr<URLRequest> request(context_.CreateRequest( |
| 579 | http_page, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 580 | request->Start(); |
| 581 | delegate.RunUntilComplete(); |
| 582 | EXPECT_EQ(OK, delegate.request_status()); |
| 583 | EXPECT_TRUE(request->url().SchemeIs("https")); |
| 584 | } |
| 585 | |
| 586 | TEST_F(WebSocketEndToEndTest, HstsWebSocketToWebSocket) { |
| 587 | SpawnedTestServer::SSLOptions ssl_options( |
| 588 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
| 589 | SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, ssl_options, |
| 590 | GetWebSocketTestDataDirectory()); |
| 591 | ASSERT_TRUE(wss_server.Start()); |
| 592 | InitialiseContext(); |
| 593 | // Set HSTS via wss: |
| 594 | GURL wss_url = wss_server.GetURL("set-hsts"); |
| 595 | EXPECT_TRUE(ConnectAndWait(wss_url)); |
| 596 | |
| 597 | // Verify via wss: |
| 598 | GURL ws_url = ReplaceUrlScheme(wss_server.GetURL(kEchoServer), "ws"); |
| 599 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 600 | } |
| 601 | |
ricea | 5acb1faf7 | 2015-03-16 15:34:00 | [diff] [blame] | 602 | // Regression test for crbug.com/180504 "WebSocket handshake fails when HTTP |
| 603 | // headers have trailing LWS". |
Sergey Ulanov | 4c786d3 | 2017-09-08 22:53:25 | [diff] [blame] | 604 | TEST_F(WebSocketEndToEndTest, TrailingWhitespace) { |
ricea | 5acb1faf7 | 2015-03-16 15:34:00 | [diff] [blame] | 605 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
ricea | 5acb1faf7 | 2015-03-16 15:34:00 | [diff] [blame] | 606 | GetWebSocketTestDataDirectory()); |
| 607 | ASSERT_TRUE(ws_server.Start()); |
| 608 | |
| 609 | GURL ws_url = ws_server.GetURL("trailing-whitespace"); |
| 610 | sub_protocols_.push_back("sip"); |
| 611 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 612 | EXPECT_EQ("sip", event_interface_->selected_subprotocol()); |
| 613 | } |
| 614 | |
ricea | e1d6767 | 2015-03-19 10:10:17 | [diff] [blame] | 615 | // This is a regression test for crbug.com/169448 "WebSockets should support |
| 616 | // header continuations" |
| 617 | // TODO(ricea): HTTP continuation headers have been deprecated by RFC7230. If |
| 618 | // support for continuation headers is removed from Chrome, then this test will |
| 619 | // break and should be removed. |
Sergey Ulanov | 4c786d3 | 2017-09-08 22:53:25 | [diff] [blame] | 620 | TEST_F(WebSocketEndToEndTest, HeaderContinuations) { |
ricea | e1d6767 | 2015-03-19 10:10:17 | [diff] [blame] | 621 | SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
ricea | e1d6767 | 2015-03-19 10:10:17 | [diff] [blame] | 622 | GetWebSocketTestDataDirectory()); |
| 623 | ASSERT_TRUE(ws_server.Start()); |
| 624 | |
| 625 | GURL ws_url = ws_server.GetURL("header-continuation"); |
| 626 | |
| 627 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 628 | EXPECT_EQ("permessage-deflate; server_max_window_bits=10", |
| 629 | event_interface_->extensions()); |
| 630 | } |
| 631 | |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 632 | // Test that ws->wss scheme upgrade is supported on receiving a DNS HTTPS |
| 633 | // record. |
David Benjamin | 51eeec9 | 2021-09-21 03:56:36 | [diff] [blame] | 634 | TEST_F(WebSocketEndToEndTest, DnsSchemeUpgradeSupported) { |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 635 | base::test::ScopedFeatureList features; |
| 636 | features.InitAndEnableFeatureWithParameters( |
| 637 | features::kUseDnsHttpsSvcb, {{"UseDnsHttpsSvcbHttpUpgrade", "true"}}); |
| 638 | |
| 639 | SpawnedTestServer wss_server(SpawnedTestServer::TYPE_WSS, |
| 640 | SpawnedTestServer::SSLOptions(base::FilePath( |
| 641 | FILE_PATH_LITERAL("test_names.pem"))), |
| 642 | GetWebSocketTestDataDirectory()); |
| 643 | ASSERT_TRUE(wss_server.Start()); |
| 644 | |
| 645 | GURL wss_url("wss://a.test:" + |
| 646 | base::NumberToString(wss_server.host_port_pair().port()) + "/" + |
| 647 | kEchoServer); |
| 648 | GURL::Replacements replacements; |
| 649 | replacements.SetSchemeStr(url::kWsScheme); |
| 650 | GURL ws_url = wss_url.ReplaceComponents(replacements); |
| 651 | |
Eric Orth | be86fee | 2021-10-28 22:31:11 | [diff] [blame] | 652 | // Note that due to socket pool behavior, HostResolver will see the ws/wss |
| 653 | // requests as http/https. |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 654 | MockHostResolver host_resolver; |
Eric Orth | be86fee | 2021-10-28 22:31:11 | [diff] [blame] | 655 | MockHostResolverBase::RuleResolver::RuleKey unencrypted_resolve_key; |
| 656 | unencrypted_resolve_key.scheme = url::kHttpScheme; |
| 657 | host_resolver.rules()->AddRule(std::move(unencrypted_resolve_key), |
| 658 | ERR_DNS_NAME_HTTPS_ONLY); |
| 659 | MockHostResolverBase::RuleResolver::RuleKey encrypted_resolve_key; |
| 660 | encrypted_resolve_key.scheme = url::kHttpsScheme; |
| 661 | host_resolver.rules()->AddRule(std::move(encrypted_resolve_key), "127.0.0.1"); |
Eric Orth | cd71ede | 2021-09-14 18:18:50 | [diff] [blame] | 662 | context_.set_host_resolver(&host_resolver); |
| 663 | |
| 664 | EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 665 | |
| 666 | // Expect request to have reached the server using the upgraded URL. |
| 667 | EXPECT_EQ(event_interface_->response()->url, wss_url); |
| 668 | } |
| 669 | |
ricea | 433bdab | 2015-01-26 07:25:37 | [diff] [blame] | 670 | } // namespace |
| 671 | |
| 672 | } // namespace net |