blob: 5528be8fe28695687af251f045941a733864340d [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 Houghtonffe89daa02018-03-09 18:30:0315#include "net/proxy_resolution/proxy_resolution_service.h"
[email protected]a31ecc02013-12-05 08:30:5516#include "net/socket/socket_test_util.h"
Ryan Hamilton2e003eea2018-05-02 00:24:2917#include "net/third_party/spdy/core/spdy_protocol.h"
Ramin Halavatica8d5252018-03-12 05:33:4918#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
Bence Béky8d1c6052018-02-07 12:48:1519#include "net/websockets/websocket_basic_handshake_stream.h"
mkwst4997ce82015-07-25 12:00:0520#include "url/origin.h"
[email protected]ce9f7ffd2013-10-11 06:04:1121
22namespace net {
23
24namespace {
tfarinaea94afc232015-10-20 04:23:3625
tfarina8a2c66c22015-10-13 19:14:4926const uint64_t kA = (static_cast<uint64_t>(0x5851f42d) << 32) +
27 static_cast<uint64_t>(0x4c957f2d);
28const uint64_t kC = 12345;
29const uint64_t kM = static_cast<uint64_t>(1) << 48;
[email protected]ce9f7ffd2013-10-11 06:04:1130
31} // namespace
32
tfarina8a2c66c22015-10-13 19:14:4933LinearCongruentialGenerator::LinearCongruentialGenerator(uint32_t seed)
[email protected]ce9f7ffd2013-10-11 06:04:1134 : current_(seed) {}
35
tfarina8a2c66c22015-10-13 19:14:4936uint32_t LinearCongruentialGenerator::Generate() {
37 uint64_t result = current_;
[email protected]ce9f7ffd2013-10-11 06:04:1138 current_ = (current_ * kA + kC) % kM;
tfarina8a2c66c22015-10-13 19:14:4939 return static_cast<uint32_t>(result >> 16);
[email protected]ce9f7ffd2013-10-11 06:04:1140}
41
Bence Béky3a0c48532018-03-02 13:38:5142std::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
alladacef397d2016-06-29 17:52:2351std::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) {
yhirano01a5d662015-02-12 04:33:0657 return WebSocketStandardRequestWithCookies(path, host, origin, std::string(),
alladacef397d2016-06-29 17:52:2358 send_additional_request_headers,
yhirano01a5d662015-02-12 04:33:0659 extra_headers);
60}
61
62std::string WebSocketStandardRequestWithCookies(
63 const std::string& path,
64 const std::string& host,
mkwst4997ce82015-07-25 12:00:0565 const url::Origin& origin,
yhirano01a5d662015-02-12 04:33:0666 const std::string& cookies,
alladacef397d2016-06-29 17:52:2367 const std::string& send_additional_request_headers,
yhirano01a5d662015-02-12 04:33:0668 const std::string& extra_headers) {
[email protected]a31ecc02013-12-05 08:30:5569 // 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
alladacef397d2016-06-29 17:52:2371 // 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]a31ecc02013-12-05 08:30:5595}
96
97std::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éky46bfbc12018-02-22 19:28:20107SpdyHeaderBlock 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
133SpdyHeaderBlock 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
mmenked0d201a2015-06-08 12:00:12143struct WebSocketMockClientSocketFactoryMaker::Detail {
[email protected]a31ecc02013-12-05 08:30:55144 std::string expect_written;
145 std::string return_to_read;
[email protected]94831522014-02-06 12:05:18146 std::vector<MockRead> reads;
[email protected]a31ecc02013-12-05 08:30:55147 MockWrite write;
danakj9c5cab52016-04-16 00:54:33148 std::vector<std::unique_ptr<SequencedSocketData>> socket_data_vector;
149 std::vector<std::unique_ptr<SSLSocketDataProvider>> ssl_socket_data_vector;
mmenked0d201a2015-06-08 12:00:12150 MockClientSocketFactory factory;
[email protected]a31ecc02013-12-05 08:30:55151};
152
mmenked0d201a2015-06-08 12:00:12153WebSocketMockClientSocketFactoryMaker::WebSocketMockClientSocketFactoryMaker()
Bence Béky65623972018-03-05 15:31:56154 : detail_(std::make_unique<Detail>()) {}
[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 }
Bence Béky65623972018-03-05 15:31:56185 auto socket_data = std::make_unique<SequencedSocketData>(
Ryan Sleevib8d7ea02018-05-07 20:01:01186 detail_->reads, base::make_span(&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_);
Ramin Halavatica8d5252018-03-12 05:33:49230 proxy_resolution_service_ = ProxyResolutionService::CreateFixed(
231 proxy_rules, TRAFFIC_ANNOTATION_FOR_TESTS);
Lily Houghton8c2f97d2018-01-22 05:06:59232 url_request_context_.set_proxy_resolution_service(
233 proxy_resolution_service_.get());
ricea38fc268c2015-02-09 02:41:29234}
235
[email protected]a31ecc02013-12-05 08:30:55236TestURLRequestContext*
237WebSocketTestURLRequestContextHost::GetURLRequestContext() {
[email protected]654866142014-06-24 22:53:31238 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]a31ecc02013-12-05 08:30:55244 return &url_request_context_;
245}
246
Bence Béky8d1c6052018-02-07 12:48:15247void TestWebSocketHandshakeStreamCreateHelper::OnBasicStreamCreated(
248 WebSocketBasicHandshakeStream* stream) {
249 stream->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ==");
Bence Békydca6bd92018-01-30 13:43:06250}
251
[email protected]ce9f7ffd2013-10-11 06:04:11252} // namespace net