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