[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 1 | // Copyright 2013 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 | #include "net/websockets/websocket_test_util.h" |
| 6 | |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 8 | #include <algorithm> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9 | #include <utility> |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 10 | |
Bence Béky | 3a0c4853 | 2018-03-02 13:38:51 | [diff] [blame] | 11 | #include "base/strings/strcat.h" |
Bence Béky | 46bfbc1 | 2018-02-22 19:28:20 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
Bence Béky | 70dd766 | 2018-03-02 17:13:24 | [diff] [blame] | 14 | #include "net/http/http_network_session.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 15 | #include "net/proxy_resolution/proxy_resolution_service.h" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 16 | #include "net/socket/socket_test_util.h" |
Ryan Hamilton | 2e003eea | 2018-05-02 00:24:29 | [diff] [blame] | 17 | #include "net/third_party/spdy/core/spdy_protocol.h" |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 18 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 19 | #include "net/websockets/websocket_basic_handshake_stream.h" |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 20 | #include "url/origin.h" |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 21 | |
| 22 | namespace net { |
| 23 | |
| 24 | namespace { |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 25 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 26 | const uint64_t kA = (static_cast<uint64_t>(0x5851f42d) << 32) + |
| 27 | static_cast<uint64_t>(0x4c957f2d); |
| 28 | const uint64_t kC = 12345; |
| 29 | const uint64_t kM = static_cast<uint64_t>(1) << 48; |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 30 | |
| 31 | } // namespace |
| 32 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 33 | LinearCongruentialGenerator::LinearCongruentialGenerator(uint32_t seed) |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 34 | : current_(seed) {} |
| 35 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 36 | uint32_t LinearCongruentialGenerator::Generate() { |
| 37 | uint64_t result = current_; |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 38 | current_ = (current_ * kA + kC) % kM; |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 39 | return static_cast<uint32_t>(result >> 16); |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 40 | } |
| 41 | |
Bence Béky | 3a0c4853 | 2018-03-02 13:38:51 | [diff] [blame] | 42 | std::string WebSocketExtraHeadersToString( |
| 43 | const WebSocketExtraHeaders& headers) { |
| 44 | std::string answer; |
| 45 | for (const auto& header : headers) { |
| 46 | base::StrAppend(&answer, {header.first, ": ", header.second, "\r\n"}); |
| 47 | } |
| 48 | return answer; |
| 49 | } |
| 50 | |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 51 | std::string WebSocketStandardRequest( |
| 52 | const std::string& path, |
| 53 | const std::string& host, |
| 54 | const url::Origin& origin, |
| 55 | const std::string& send_additional_request_headers, |
| 56 | const std::string& extra_headers) { |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 57 | return WebSocketStandardRequestWithCookies(path, host, origin, std::string(), |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 58 | send_additional_request_headers, |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 59 | extra_headers); |
| 60 | } |
| 61 | |
| 62 | std::string WebSocketStandardRequestWithCookies( |
| 63 | const std::string& path, |
| 64 | const std::string& host, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 65 | const url::Origin& origin, |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 66 | const std::string& cookies, |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 67 | const std::string& send_additional_request_headers, |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 68 | const std::string& extra_headers) { |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 69 | // Unrelated changes in net/http may change the order and default-values of |
| 70 | // HTTP headers, causing WebSocket tests to fail. It is safe to update this |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 71 | // in that case. |
| 72 | HttpRequestHeaders headers; |
| 73 | std::stringstream request_headers; |
| 74 | |
| 75 | request_headers << base::StringPrintf("GET %s HTTP/1.1\r\n", path.c_str()); |
| 76 | headers.SetHeader("Host", host); |
| 77 | headers.SetHeader("Connection", "Upgrade"); |
| 78 | headers.SetHeader("Pragma", "no-cache"); |
| 79 | headers.SetHeader("Cache-Control", "no-cache"); |
| 80 | headers.SetHeader("Upgrade", "websocket"); |
| 81 | headers.SetHeader("Origin", origin.Serialize()); |
| 82 | headers.SetHeader("Sec-WebSocket-Version", "13"); |
| 83 | headers.SetHeader("User-Agent", ""); |
| 84 | headers.AddHeadersFromString(send_additional_request_headers); |
| 85 | headers.SetHeader("Accept-Encoding", "gzip, deflate"); |
| 86 | headers.SetHeader("Accept-Language", "en-us,fr"); |
| 87 | headers.AddHeadersFromString(cookies); |
| 88 | headers.SetHeader("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ=="); |
| 89 | headers.SetHeader("Sec-WebSocket-Extensions", |
| 90 | "permessage-deflate; client_max_window_bits"); |
| 91 | headers.AddHeadersFromString(extra_headers); |
| 92 | |
| 93 | request_headers << headers.ToString(); |
| 94 | return request_headers.str(); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | std::string WebSocketStandardResponse(const std::string& extra_headers) { |
| 98 | return base::StringPrintf( |
| 99 | "HTTP/1.1 101 Switching Protocols\r\n" |
| 100 | "Upgrade: websocket\r\n" |
| 101 | "Connection: Upgrade\r\n" |
| 102 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
| 103 | "%s\r\n", |
| 104 | extra_headers.c_str()); |
| 105 | } |
| 106 | |
Bence Béky | 46bfbc1 | 2018-02-22 19:28:20 | [diff] [blame] | 107 | SpdyHeaderBlock WebSocketHttp2Request( |
| 108 | const std::string& path, |
| 109 | const std::string& authority, |
| 110 | const std::string& origin, |
| 111 | const WebSocketExtraHeaders& extra_headers) { |
| 112 | SpdyHeaderBlock request_headers; |
| 113 | request_headers[kHttp2MethodHeader] = "CONNECT"; |
| 114 | request_headers[kHttp2AuthorityHeader] = authority; |
| 115 | request_headers[kHttp2SchemeHeader] = "https"; |
| 116 | request_headers[kHttp2PathHeader] = path; |
| 117 | request_headers[kHttp2ProtocolHeader] = "websocket"; |
| 118 | request_headers["pragma"] = "no-cache"; |
| 119 | request_headers["cache-control"] = "no-cache"; |
| 120 | request_headers["origin"] = origin; |
| 121 | request_headers["sec-websocket-version"] = "13"; |
| 122 | request_headers["user-agent"] = ""; |
| 123 | request_headers["accept-encoding"] = "gzip, deflate"; |
| 124 | request_headers["accept-language"] = "en-us,fr"; |
| 125 | request_headers["sec-websocket-extensions"] = |
| 126 | "permessage-deflate; client_max_window_bits"; |
| 127 | for (const auto& header : extra_headers) { |
| 128 | request_headers[base::ToLowerASCII(header.first)] = header.second; |
| 129 | } |
| 130 | return request_headers; |
| 131 | } |
| 132 | |
| 133 | SpdyHeaderBlock WebSocketHttp2Response( |
| 134 | const WebSocketExtraHeaders& extra_headers) { |
| 135 | SpdyHeaderBlock response_headers; |
| 136 | response_headers[kHttp2StatusHeader] = "200"; |
| 137 | for (const auto& header : extra_headers) { |
| 138 | response_headers[base::ToLowerASCII(header.first)] = header.second; |
| 139 | } |
| 140 | return response_headers; |
| 141 | } |
| 142 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 143 | struct WebSocketMockClientSocketFactoryMaker::Detail { |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 144 | std::string expect_written; |
| 145 | std::string return_to_read; |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 146 | std::vector<MockRead> reads; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 147 | MockWrite write; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 148 | std::vector<std::unique_ptr<SequencedSocketData>> socket_data_vector; |
| 149 | std::vector<std::unique_ptr<SSLSocketDataProvider>> ssl_socket_data_vector; |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 150 | MockClientSocketFactory factory; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 151 | }; |
| 152 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 153 | WebSocketMockClientSocketFactoryMaker::WebSocketMockClientSocketFactoryMaker() |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 154 | : detail_(std::make_unique<Detail>()) {} |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 155 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 156 | WebSocketMockClientSocketFactoryMaker:: |
Chris Watkins | 28c2fdd | 2017-11-30 06:06:52 | [diff] [blame] | 157 | ~WebSocketMockClientSocketFactoryMaker() = default; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 158 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 159 | MockClientSocketFactory* WebSocketMockClientSocketFactoryMaker::factory() { |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 160 | return &detail_->factory; |
| 161 | } |
| 162 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 163 | void WebSocketMockClientSocketFactoryMaker::SetExpectations( |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 164 | const std::string& expect_written, |
| 165 | const std::string& return_to_read) { |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 166 | const size_t kHttpStreamParserBufferSize = 4096; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 167 | // We need to extend the lifetime of these strings. |
| 168 | detail_->expect_written = expect_written; |
| 169 | detail_->return_to_read = return_to_read; |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 170 | int sequence = 0; |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 171 | detail_->write = MockWrite(SYNCHRONOUS, |
| 172 | detail_->expect_written.data(), |
| 173 | detail_->expect_written.size(), |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 174 | sequence++); |
| 175 | // HttpStreamParser reads 4KB at a time. We need to take this implementation |
| 176 | // detail into account if |return_to_read| is big enough. |
| 177 | for (size_t place = 0; place < detail_->return_to_read.size(); |
| 178 | place += kHttpStreamParserBufferSize) { |
| 179 | detail_->reads.push_back( |
| 180 | MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place, |
| 181 | std::min(detail_->return_to_read.size() - place, |
| 182 | kHttpStreamParserBufferSize), |
| 183 | sequence++)); |
| 184 | } |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 185 | auto socket_data = std::make_unique<SequencedSocketData>( |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame^] | 186 | detail_->reads, base::make_span(&detail_->write, 1)); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 187 | socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 188 | AddRawExpectations(std::move(socket_data)); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 189 | } |
| 190 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 191 | void WebSocketMockClientSocketFactoryMaker::AddRawExpectations( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 192 | std::unique_ptr<SequencedSocketData> socket_data) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 193 | detail_->factory.AddSocketDataProvider(socket_data.get()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 194 | detail_->socket_data_vector.push_back(std::move(socket_data)); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 195 | } |
| 196 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 197 | void WebSocketMockClientSocketFactoryMaker::AddSSLSocketDataProvider( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 198 | std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 199 | detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 200 | detail_->ssl_socket_data_vector.push_back(std::move(ssl_socket_data)); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost() |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 204 | : url_request_context_(true), url_request_context_initialized_(false) { |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 205 | url_request_context_.set_client_socket_factory(maker_.factory()); |
Bence Béky | 70dd766 | 2018-03-02 17:13:24 | [diff] [blame] | 206 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 207 | params->enable_spdy_ping_based_connection_checking = false; |
| 208 | params->enable_quic = false; |
| 209 | params->enable_websocket_over_http2 = true; |
| 210 | params->disable_idle_sockets_close_on_memory_pressure = false; |
| 211 | url_request_context_.set_http_network_session_params(std::move(params)); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 212 | } |
| 213 | |
Chris Watkins | 28c2fdd | 2017-11-30 06:06:52 | [diff] [blame] | 214 | WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() = |
| 215 | default; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 216 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 217 | void WebSocketTestURLRequestContextHost::AddRawExpectations( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 218 | std::unique_ptr<SequencedSocketData> socket_data) { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 219 | maker_.AddRawExpectations(std::move(socket_data)); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 223 | std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 224 | maker_.AddSSLSocketDataProvider(std::move(ssl_socket_data)); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 225 | } |
| 226 | |
ricea | 38fc268c | 2015-02-09 02:41:29 | [diff] [blame] | 227 | void WebSocketTestURLRequestContextHost::SetProxyConfig( |
| 228 | const std::string& proxy_rules) { |
| 229 | DCHECK(!url_request_context_initialized_); |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 230 | proxy_resolution_service_ = ProxyResolutionService::CreateFixed( |
| 231 | proxy_rules, TRAFFIC_ANNOTATION_FOR_TESTS); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 232 | url_request_context_.set_proxy_resolution_service( |
| 233 | proxy_resolution_service_.get()); |
ricea | 38fc268c | 2015-02-09 02:41:29 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 236 | TestURLRequestContext* |
| 237 | WebSocketTestURLRequestContextHost::GetURLRequestContext() { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 238 | if (!url_request_context_initialized_) { |
| 239 | url_request_context_.Init(); |
| 240 | // A Network Delegate is required to make the URLRequest::Delegate work. |
| 241 | url_request_context_.set_network_delegate(&network_delegate_); |
| 242 | url_request_context_initialized_ = true; |
| 243 | } |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 244 | return &url_request_context_; |
| 245 | } |
| 246 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 247 | void TestWebSocketHandshakeStreamCreateHelper::OnBasicStreamCreated( |
| 248 | WebSocketBasicHandshakeStream* stream) { |
| 249 | stream->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ=="); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 252 | } // namespace net |