blob: 8c371e4f22847b6749ec0a642b498bf0e88c50cd [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()
153 : detail_(new Detail) {
154}
[email protected]a31ecc02013-12-05 08:30:55155
mmenked0d201a2015-06-08 12:00:12156WebSocketMockClientSocketFactoryMaker::
Chris Watkins28c2fdd2017-11-30 06:06:52157 ~WebSocketMockClientSocketFactoryMaker() = default;
[email protected]a31ecc02013-12-05 08:30:55158
mmenked0d201a2015-06-08 12:00:12159MockClientSocketFactory* WebSocketMockClientSocketFactoryMaker::factory() {
[email protected]a31ecc02013-12-05 08:30:55160 return &detail_->factory;
161}
162
mmenked0d201a2015-06-08 12:00:12163void WebSocketMockClientSocketFactoryMaker::SetExpectations(
[email protected]a31ecc02013-12-05 08:30:55164 const std::string& expect_written,
165 const std::string& return_to_read) {
[email protected]94831522014-02-06 12:05:18166 const size_t kHttpStreamParserBufferSize = 4096;
[email protected]a31ecc02013-12-05 08:30:55167 // 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]94831522014-02-06 12:05:18170 int sequence = 0;
[email protected]0be93922014-01-29 00:42:45171 detail_->write = MockWrite(SYNCHRONOUS,
172 detail_->expect_written.data(),
173 detail_->expect_written.size(),
[email protected]94831522014-02-06 12:05:18174 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 }
danakj9c5cab52016-04-16 00:54:33185 std::unique_ptr<SequencedSocketData> socket_data(new SequencedSocketData(
davidben5f8b6bc2015-11-25 03:19:54186 detail_->reads.data(), detail_->reads.size(), &detail_->write, 1));
[email protected]a31ecc02013-12-05 08:30:55187 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
dchengc7eeda422015-12-26 03:56:48188 AddRawExpectations(std::move(socket_data));
[email protected]a31ecc02013-12-05 08:30:55189}
190
mmenked0d201a2015-06-08 12:00:12191void WebSocketMockClientSocketFactoryMaker::AddRawExpectations(
danakj9c5cab52016-04-16 00:54:33192 std::unique_ptr<SequencedSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15193 detail_->factory.AddSocketDataProvider(socket_data.get());
dchengc7eeda422015-12-26 03:56:48194 detail_->socket_data_vector.push_back(std::move(socket_data));
[email protected]a62449522014-06-05 11:11:15195}
196
mmenked0d201a2015-06-08 12:00:12197void WebSocketMockClientSocketFactoryMaker::AddSSLSocketDataProvider(
danakj9c5cab52016-04-16 00:54:33198 std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) {
[email protected]a62449522014-06-05 11:11:15199 detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get());
dchengc7eeda422015-12-26 03:56:48200 detail_->ssl_socket_data_vector.push_back(std::move(ssl_socket_data));
[email protected]a31ecc02013-12-05 08:30:55201}
202
203WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
[email protected]654866142014-06-24 22:53:31204 : url_request_context_(true), url_request_context_initialized_(false) {
[email protected]a31ecc02013-12-05 08:30:55205 url_request_context_.set_client_socket_factory(maker_.factory());
Bence Béky70dd7662018-03-02 17:13:24206 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]a31ecc02013-12-05 08:30:55212}
213
Chris Watkins28c2fdd2017-11-30 06:06:52214WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() =
215 default;
[email protected]a31ecc02013-12-05 08:30:55216
[email protected]a62449522014-06-05 11:11:15217void WebSocketTestURLRequestContextHost::AddRawExpectations(
danakj9c5cab52016-04-16 00:54:33218 std::unique_ptr<SequencedSocketData> socket_data) {
dchengc7eeda422015-12-26 03:56:48219 maker_.AddRawExpectations(std::move(socket_data));
[email protected]a62449522014-06-05 11:11:15220}
221
222void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider(
danakj9c5cab52016-04-16 00:54:33223 std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) {
dchengc7eeda422015-12-26 03:56:48224 maker_.AddSSLSocketDataProvider(std::move(ssl_socket_data));
[email protected]a31ecc02013-12-05 08:30:55225}
226
ricea38fc268c2015-02-09 02:41:29227void WebSocketTestURLRequestContextHost::SetProxyConfig(
228 const std::string& proxy_rules) {
229 DCHECK(!url_request_context_initialized_);
Lily Houghton8c2f97d2018-01-22 05:06:59230 proxy_resolution_service_ = ProxyResolutionService::CreateFixed(proxy_rules);
231 url_request_context_.set_proxy_resolution_service(
232 proxy_resolution_service_.get());
ricea38fc268c2015-02-09 02:41:29233}
234
[email protected]a31ecc02013-12-05 08:30:55235TestURLRequestContext*
236WebSocketTestURLRequestContextHost::GetURLRequestContext() {
[email protected]654866142014-06-24 22:53:31237 if (!url_request_context_initialized_) {
238 url_request_context_.Init();
239 // A Network Delegate is required to make the URLRequest::Delegate work.
240 url_request_context_.set_network_delegate(&network_delegate_);
241 url_request_context_initialized_ = true;
242 }
[email protected]a31ecc02013-12-05 08:30:55243 return &url_request_context_;
244}
245
Bence Béky8d1c6052018-02-07 12:48:15246void TestWebSocketHandshakeStreamCreateHelper::OnBasicStreamCreated(
247 WebSocketBasicHandshakeStream* stream) {
248 stream->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ==");
Bence Békydca6bd92018-01-30 13:43:06249}
250
[email protected]ce9f7ffd2013-10-11 06:04:11251} // namespace net