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