blob: 112db2d42594965ec394ac40361015ac80bac5f8 [file] [log] [blame]
[email protected]ce9f7ffd2013-10-11 06:04:111// 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
tfarinaea94afc232015-10-20 04:23:367#include <stddef.h>
[email protected]94831522014-02-06 12:05:188#include <algorithm>
dchengc7eeda422015-12-26 03:56:489#include <utility>
[email protected]94831522014-02-06 12:05:1810
Bence Béky3a0c48532018-03-02 13:38:5111#include "base/strings/strcat.h"
Bence Béky46bfbc12018-02-22 19:28:2012#include "base/strings/string_util.h"
[email protected]a31ecc02013-12-05 08:30:5513#include "base/strings/stringprintf.h"
Bence Béky70dd7662018-03-02 17:13:2414#include "net/http/http_network_session.h"
Lily Houghton582d4622018-01-22 22:43:4015#include "net/proxy_resolution/proxy_service.h"
[email protected]a31ecc02013-12-05 08:30:5516#include "net/socket/socket_test_util.h"
Bence Béky46bfbc12018-02-22 19:28:2017#include "net/spdy/core/spdy_protocol.h"
Bence Béky8d1c6052018-02-07 12:48:1518#include "net/websockets/websocket_basic_handshake_stream.h"
mkwst4997ce82015-07-25 12:00:0519#include "url/origin.h"
[email protected]ce9f7ffd2013-10-11 06:04:1120
21namespace net {
22
23namespace {
tfarinaea94afc232015-10-20 04:23:3624
tfarina8a2c66c22015-10-13 19:14:4925const uint64_t kA = (static_cast<uint64_t>(0x5851f42d) << 32) +
26 static_cast<uint64_t>(0x4c957f2d);
27const uint64_t kC = 12345;
28const uint64_t kM = static_cast<uint64_t>(1) << 48;
[email protected]ce9f7ffd2013-10-11 06:04:1129
30} // namespace
31
tfarina8a2c66c22015-10-13 19:14:4932LinearCongruentialGenerator::LinearCongruentialGenerator(uint32_t seed)
[email protected]ce9f7ffd2013-10-11 06:04:1133 : current_(seed) {}
34
tfarina8a2c66c22015-10-13 19:14:4935uint32_t LinearCongruentialGenerator::Generate() {
36 uint64_t result = current_;
[email protected]ce9f7ffd2013-10-11 06:04:1137 current_ = (current_ * kA + kC) % kM;
tfarina8a2c66c22015-10-13 19:14:4938 return static_cast<uint32_t>(result >> 16);
[email protected]ce9f7ffd2013-10-11 06:04:1139}
40
Bence Béky3a0c48532018-03-02 13:38:5141std::string WebSocketExtraHeadersToString(
42 const WebSocketExtraHeaders& headers) {
43 std::string answer;
44 for (const auto& header : headers) {
45 base::StrAppend(&answer, {header.first, ": ", header.second, "\r\n"});
46 }
47 return answer;
48}
49
alladacef397d2016-06-29 17:52:2350std::string WebSocketStandardRequest(
51 const std::string& path,
52 const std::string& host,
53 const url::Origin& origin,
54 const std::string& send_additional_request_headers,
55 const std::string& extra_headers) {
yhirano01a5d662015-02-12 04:33:0656 return WebSocketStandardRequestWithCookies(path, host, origin, std::string(),
alladacef397d2016-06-29 17:52:2357 send_additional_request_headers,
yhirano01a5d662015-02-12 04:33:0658 extra_headers);
59}
60
61std::string WebSocketStandardRequestWithCookies(
62 const std::string& path,
63 const std::string& host,
mkwst4997ce82015-07-25 12:00:0564 const url::Origin& origin,
yhirano01a5d662015-02-12 04:33:0665 const std::string& cookies,
alladacef397d2016-06-29 17:52:2366 const std::string& send_additional_request_headers,
yhirano01a5d662015-02-12 04:33:0667 const std::string& extra_headers) {
[email protected]a31ecc02013-12-05 08:30:5568 // Unrelated changes in net/http may change the order and default-values of
69 // HTTP headers, causing WebSocket tests to fail. It is safe to update this
alladacef397d2016-06-29 17:52:2370 // in that case.
71 HttpRequestHeaders headers;
72 std::stringstream request_headers;
73
74 request_headers << base::StringPrintf("GET %s HTTP/1.1\r\n", path.c_str());
75 headers.SetHeader("Host", host);
76 headers.SetHeader("Connection", "Upgrade");
77 headers.SetHeader("Pragma", "no-cache");
78 headers.SetHeader("Cache-Control", "no-cache");
79 headers.SetHeader("Upgrade", "websocket");
80 headers.SetHeader("Origin", origin.Serialize());
81 headers.SetHeader("Sec-WebSocket-Version", "13");
82 headers.SetHeader("User-Agent", "");
83 headers.AddHeadersFromString(send_additional_request_headers);
84 headers.SetHeader("Accept-Encoding", "gzip, deflate");
85 headers.SetHeader("Accept-Language", "en-us,fr");
86 headers.AddHeadersFromString(cookies);
87 headers.SetHeader("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==");
88 headers.SetHeader("Sec-WebSocket-Extensions",
89 "permessage-deflate; client_max_window_bits");
90 headers.AddHeadersFromString(extra_headers);
91
92 request_headers << headers.ToString();
93 return request_headers.str();
[email protected]a31ecc02013-12-05 08:30:5594}
95
96std::string WebSocketStandardResponse(const std::string& extra_headers) {
97 return base::StringPrintf(
98 "HTTP/1.1 101 Switching Protocols\r\n"
99 "Upgrade: websocket\r\n"
100 "Connection: Upgrade\r\n"
101 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
102 "%s\r\n",
103 extra_headers.c_str());
104}
105
Bence Béky46bfbc12018-02-22 19:28:20106SpdyHeaderBlock WebSocketHttp2Request(
107 const std::string& path,
108 const std::string& authority,
109 const std::string& origin,
110 const WebSocketExtraHeaders& extra_headers) {
111 SpdyHeaderBlock request_headers;
112 request_headers[kHttp2MethodHeader] = "CONNECT";
113 request_headers[kHttp2AuthorityHeader] = authority;
114 request_headers[kHttp2SchemeHeader] = "https";
115 request_headers[kHttp2PathHeader] = path;
116 request_headers[kHttp2ProtocolHeader] = "websocket";
117 request_headers["pragma"] = "no-cache";
118 request_headers["cache-control"] = "no-cache";
119 request_headers["origin"] = origin;
120 request_headers["sec-websocket-version"] = "13";
121 request_headers["user-agent"] = "";
122 request_headers["accept-encoding"] = "gzip, deflate";
123 request_headers["accept-language"] = "en-us,fr";
124 request_headers["sec-websocket-extensions"] =
125 "permessage-deflate; client_max_window_bits";
126 for (const auto& header : extra_headers) {
127 request_headers[base::ToLowerASCII(header.first)] = header.second;
128 }
129 return request_headers;
130}
131
132SpdyHeaderBlock WebSocketHttp2Response(
133 const WebSocketExtraHeaders& extra_headers) {
134 SpdyHeaderBlock response_headers;
135 response_headers[kHttp2StatusHeader] = "200";
136 for (const auto& header : extra_headers) {
137 response_headers[base::ToLowerASCII(header.first)] = header.second;
138 }
139 return response_headers;
140}
141
mmenked0d201a2015-06-08 12:00:12142struct WebSocketMockClientSocketFactoryMaker::Detail {
[email protected]a31ecc02013-12-05 08:30:55143 std::string expect_written;
144 std::string return_to_read;
[email protected]94831522014-02-06 12:05:18145 std::vector<MockRead> reads;
[email protected]a31ecc02013-12-05 08:30:55146 MockWrite write;
danakj9c5cab52016-04-16 00:54:33147 std::vector<std::unique_ptr<SequencedSocketData>> socket_data_vector;
148 std::vector<std::unique_ptr<SSLSocketDataProvider>> ssl_socket_data_vector;
mmenked0d201a2015-06-08 12:00:12149 MockClientSocketFactory factory;
[email protected]a31ecc02013-12-05 08:30:55150};
151
mmenked0d201a2015-06-08 12:00:12152WebSocketMockClientSocketFactoryMaker::WebSocketMockClientSocketFactoryMaker()
Bence Béky65623972018-03-05 15:31:56153 : detail_(std::make_unique<Detail>()) {}
[email protected]a31ecc02013-12-05 08:30:55154
mmenked0d201a2015-06-08 12:00:12155WebSocketMockClientSocketFactoryMaker::
Chris Watkins28c2fdd2017-11-30 06:06:52156 ~WebSocketMockClientSocketFactoryMaker() = default;
[email protected]a31ecc02013-12-05 08:30:55157
mmenked0d201a2015-06-08 12:00:12158MockClientSocketFactory* WebSocketMockClientSocketFactoryMaker::factory() {
[email protected]a31ecc02013-12-05 08:30:55159 return &detail_->factory;
160}
161
mmenked0d201a2015-06-08 12:00:12162void WebSocketMockClientSocketFactoryMaker::SetExpectations(
[email protected]a31ecc02013-12-05 08:30:55163 const std::string& expect_written,
164 const std::string& return_to_read) {
[email protected]94831522014-02-06 12:05:18165 const size_t kHttpStreamParserBufferSize = 4096;
[email protected]a31ecc02013-12-05 08:30:55166 // We need to extend the lifetime of these strings.
167 detail_->expect_written = expect_written;
168 detail_->return_to_read = return_to_read;
[email protected]94831522014-02-06 12:05:18169 int sequence = 0;
[email protected]0be93922014-01-29 00:42:45170 detail_->write = MockWrite(SYNCHRONOUS,
171 detail_->expect_written.data(),
172 detail_->expect_written.size(),
[email protected]94831522014-02-06 12:05:18173 sequence++);
174 // HttpStreamParser reads 4KB at a time. We need to take this implementation
175 // detail into account if |return_to_read| is big enough.
176 for (size_t place = 0; place < detail_->return_to_read.size();
177 place += kHttpStreamParserBufferSize) {
178 detail_->reads.push_back(
179 MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place,
180 std::min(detail_->return_to_read.size() - place,
181 kHttpStreamParserBufferSize),
182 sequence++));
183 }
Bence Béky65623972018-03-05 15:31:56184 auto socket_data = std::make_unique<SequencedSocketData>(
185 detail_->reads.data(), detail_->reads.size(), &detail_->write, 1);
[email protected]a31ecc02013-12-05 08:30:55186 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
dchengc7eeda422015-12-26 03:56:48187 AddRawExpectations(std::move(socket_data));
[email protected]a31ecc02013-12-05 08:30:55188}
189
mmenked0d201a2015-06-08 12:00:12190void WebSocketMockClientSocketFactoryMaker::AddRawExpectations(
danakj9c5cab52016-04-16 00:54:33191 std::unique_ptr<SequencedSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15192 detail_->factory.AddSocketDataProvider(socket_data.get());
dchengc7eeda422015-12-26 03:56:48193 detail_->socket_data_vector.push_back(std::move(socket_data));
[email protected]a62449522014-06-05 11:11:15194}
195
mmenked0d201a2015-06-08 12:00:12196void WebSocketMockClientSocketFactoryMaker::AddSSLSocketDataProvider(
danakj9c5cab52016-04-16 00:54:33197 std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) {
[email protected]a62449522014-06-05 11:11:15198 detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get());
dchengc7eeda422015-12-26 03:56:48199 detail_->ssl_socket_data_vector.push_back(std::move(ssl_socket_data));
[email protected]a31ecc02013-12-05 08:30:55200}
201
202WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
[email protected]654866142014-06-24 22:53:31203 : url_request_context_(true), url_request_context_initialized_(false) {
[email protected]a31ecc02013-12-05 08:30:55204 url_request_context_.set_client_socket_factory(maker_.factory());
Bence Béky70dd7662018-03-02 17:13:24205 auto params = std::make_unique<HttpNetworkSession::Params>();
206 params->enable_spdy_ping_based_connection_checking = false;
207 params->enable_quic = false;
208 params->enable_websocket_over_http2 = true;
209 params->disable_idle_sockets_close_on_memory_pressure = false;
210 url_request_context_.set_http_network_session_params(std::move(params));
[email protected]a31ecc02013-12-05 08:30:55211}
212
Chris Watkins28c2fdd2017-11-30 06:06:52213WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() =
214 default;
[email protected]a31ecc02013-12-05 08:30:55215
[email protected]a62449522014-06-05 11:11:15216void WebSocketTestURLRequestContextHost::AddRawExpectations(
danakj9c5cab52016-04-16 00:54:33217 std::unique_ptr<SequencedSocketData> socket_data) {
dchengc7eeda422015-12-26 03:56:48218 maker_.AddRawExpectations(std::move(socket_data));
[email protected]a62449522014-06-05 11:11:15219}
220
221void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider(
danakj9c5cab52016-04-16 00:54:33222 std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) {
dchengc7eeda422015-12-26 03:56:48223 maker_.AddSSLSocketDataProvider(std::move(ssl_socket_data));
[email protected]a31ecc02013-12-05 08:30:55224}
225
ricea38fc268c2015-02-09 02:41:29226void WebSocketTestURLRequestContextHost::SetProxyConfig(
227 const std::string& proxy_rules) {
228 DCHECK(!url_request_context_initialized_);
Lily Houghton8c2f97d2018-01-22 05:06:59229 proxy_resolution_service_ = ProxyResolutionService::CreateFixed(proxy_rules);
230 url_request_context_.set_proxy_resolution_service(
231 proxy_resolution_service_.get());
ricea38fc268c2015-02-09 02:41:29232}
233
[email protected]a31ecc02013-12-05 08:30:55234TestURLRequestContext*
235WebSocketTestURLRequestContextHost::GetURLRequestContext() {
[email protected]654866142014-06-24 22:53:31236 if (!url_request_context_initialized_) {
237 url_request_context_.Init();
238 // A Network Delegate is required to make the URLRequest::Delegate work.
239 url_request_context_.set_network_delegate(&network_delegate_);
240 url_request_context_initialized_ = true;
241 }
[email protected]a31ecc02013-12-05 08:30:55242 return &url_request_context_;
243}
244
Bence Béky8d1c6052018-02-07 12:48:15245void TestWebSocketHandshakeStreamCreateHelper::OnBasicStreamCreated(
246 WebSocketBasicHandshakeStream* stream) {
247 stream->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ==");
Bence Békydca6bd92018-01-30 13:43:06248}
249
[email protected]ce9f7ffd2013-10-11 06:04:11250} // namespace net