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