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