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