blob: 9dd69d46a1b271064e08b67f3737c500f8c52e4b [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#include <vector>
11
[email protected]a31ecc02013-12-05 08:30:5512#include "base/strings/stringprintf.h"
ricea38fc268c2015-02-09 02:41:2913#include "net/proxy/proxy_service.h"
[email protected]a31ecc02013-12-05 08:30:5514#include "net/socket/socket_test_util.h"
mkwst4997ce82015-07-25 12:00:0515#include "url/origin.h"
[email protected]ce9f7ffd2013-10-11 06:04:1116
17namespace net {
18
19namespace {
tfarinaea94afc232015-10-20 04:23:3620
tfarina8a2c66c22015-10-13 19:14:4921const uint64_t kA = (static_cast<uint64_t>(0x5851f42d) << 32) +
22 static_cast<uint64_t>(0x4c957f2d);
23const uint64_t kC = 12345;
24const uint64_t kM = static_cast<uint64_t>(1) << 48;
[email protected]ce9f7ffd2013-10-11 06:04:1125
26} // namespace
27
tfarina8a2c66c22015-10-13 19:14:4928LinearCongruentialGenerator::LinearCongruentialGenerator(uint32_t seed)
[email protected]ce9f7ffd2013-10-11 06:04:1129 : current_(seed) {}
30
tfarina8a2c66c22015-10-13 19:14:4931uint32_t LinearCongruentialGenerator::Generate() {
32 uint64_t result = current_;
[email protected]ce9f7ffd2013-10-11 06:04:1133 current_ = (current_ * kA + kC) % kM;
tfarina8a2c66c22015-10-13 19:14:4934 return static_cast<uint32_t>(result >> 16);
[email protected]ce9f7ffd2013-10-11 06:04:1135}
36
alladacef397d2016-06-29 17:52:2337std::string WebSocketStandardRequest(
38 const std::string& path,
39 const std::string& host,
40 const url::Origin& origin,
41 const std::string& send_additional_request_headers,
42 const std::string& extra_headers) {
yhirano01a5d662015-02-12 04:33:0643 return WebSocketStandardRequestWithCookies(path, host, origin, std::string(),
alladacef397d2016-06-29 17:52:2344 send_additional_request_headers,
yhirano01a5d662015-02-12 04:33:0645 extra_headers);
46}
47
48std::string WebSocketStandardRequestWithCookies(
49 const std::string& path,
50 const std::string& host,
mkwst4997ce82015-07-25 12:00:0551 const url::Origin& origin,
yhirano01a5d662015-02-12 04:33:0652 const std::string& cookies,
alladacef397d2016-06-29 17:52:2353 const std::string& send_additional_request_headers,
yhirano01a5d662015-02-12 04:33:0654 const std::string& extra_headers) {
[email protected]a31ecc02013-12-05 08:30:5555 // Unrelated changes in net/http may change the order and default-values of
56 // HTTP headers, causing WebSocket tests to fail. It is safe to update this
alladacef397d2016-06-29 17:52:2357 // in that case.
58 HttpRequestHeaders headers;
59 std::stringstream request_headers;
60
61 request_headers << base::StringPrintf("GET %s HTTP/1.1\r\n", path.c_str());
62 headers.SetHeader("Host", host);
63 headers.SetHeader("Connection", "Upgrade");
64 headers.SetHeader("Pragma", "no-cache");
65 headers.SetHeader("Cache-Control", "no-cache");
66 headers.SetHeader("Upgrade", "websocket");
67 headers.SetHeader("Origin", origin.Serialize());
68 headers.SetHeader("Sec-WebSocket-Version", "13");
69 headers.SetHeader("User-Agent", "");
70 headers.AddHeadersFromString(send_additional_request_headers);
71 headers.SetHeader("Accept-Encoding", "gzip, deflate");
72 headers.SetHeader("Accept-Language", "en-us,fr");
73 headers.AddHeadersFromString(cookies);
74 headers.SetHeader("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==");
75 headers.SetHeader("Sec-WebSocket-Extensions",
76 "permessage-deflate; client_max_window_bits");
77 headers.AddHeadersFromString(extra_headers);
78
79 request_headers << headers.ToString();
80 return request_headers.str();
[email protected]a31ecc02013-12-05 08:30:5581}
82
83std::string WebSocketStandardResponse(const std::string& extra_headers) {
84 return base::StringPrintf(
85 "HTTP/1.1 101 Switching Protocols\r\n"
86 "Upgrade: websocket\r\n"
87 "Connection: Upgrade\r\n"
88 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
89 "%s\r\n",
90 extra_headers.c_str());
91}
92
mmenked0d201a2015-06-08 12:00:1293struct WebSocketMockClientSocketFactoryMaker::Detail {
[email protected]a31ecc02013-12-05 08:30:5594 std::string expect_written;
95 std::string return_to_read;
[email protected]94831522014-02-06 12:05:1896 std::vector<MockRead> reads;
[email protected]a31ecc02013-12-05 08:30:5597 MockWrite write;
danakj9c5cab52016-04-16 00:54:3398 std::vector<std::unique_ptr<SequencedSocketData>> socket_data_vector;
99 std::vector<std::unique_ptr<SSLSocketDataProvider>> ssl_socket_data_vector;
mmenked0d201a2015-06-08 12:00:12100 MockClientSocketFactory factory;
[email protected]a31ecc02013-12-05 08:30:55101};
102
mmenked0d201a2015-06-08 12:00:12103WebSocketMockClientSocketFactoryMaker::WebSocketMockClientSocketFactoryMaker()
104 : detail_(new Detail) {
105}
[email protected]a31ecc02013-12-05 08:30:55106
mmenked0d201a2015-06-08 12:00:12107WebSocketMockClientSocketFactoryMaker::
108 ~WebSocketMockClientSocketFactoryMaker() {
109}
[email protected]a31ecc02013-12-05 08:30:55110
mmenked0d201a2015-06-08 12:00:12111MockClientSocketFactory* WebSocketMockClientSocketFactoryMaker::factory() {
[email protected]a31ecc02013-12-05 08:30:55112 return &detail_->factory;
113}
114
mmenked0d201a2015-06-08 12:00:12115void WebSocketMockClientSocketFactoryMaker::SetExpectations(
[email protected]a31ecc02013-12-05 08:30:55116 const std::string& expect_written,
117 const std::string& return_to_read) {
[email protected]94831522014-02-06 12:05:18118 const size_t kHttpStreamParserBufferSize = 4096;
[email protected]a31ecc02013-12-05 08:30:55119 // We need to extend the lifetime of these strings.
120 detail_->expect_written = expect_written;
121 detail_->return_to_read = return_to_read;
[email protected]94831522014-02-06 12:05:18122 int sequence = 0;
[email protected]0be93922014-01-29 00:42:45123 detail_->write = MockWrite(SYNCHRONOUS,
124 detail_->expect_written.data(),
125 detail_->expect_written.size(),
[email protected]94831522014-02-06 12:05:18126 sequence++);
127 // HttpStreamParser reads 4KB at a time. We need to take this implementation
128 // detail into account if |return_to_read| is big enough.
129 for (size_t place = 0; place < detail_->return_to_read.size();
130 place += kHttpStreamParserBufferSize) {
131 detail_->reads.push_back(
132 MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place,
133 std::min(detail_->return_to_read.size() - place,
134 kHttpStreamParserBufferSize),
135 sequence++));
136 }
danakj9c5cab52016-04-16 00:54:33137 std::unique_ptr<SequencedSocketData> socket_data(new SequencedSocketData(
davidben5f8b6bc2015-11-25 03:19:54138 detail_->reads.data(), detail_->reads.size(), &detail_->write, 1));
[email protected]a31ecc02013-12-05 08:30:55139 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
dchengc7eeda422015-12-26 03:56:48140 AddRawExpectations(std::move(socket_data));
[email protected]a31ecc02013-12-05 08:30:55141}
142
mmenked0d201a2015-06-08 12:00:12143void WebSocketMockClientSocketFactoryMaker::AddRawExpectations(
danakj9c5cab52016-04-16 00:54:33144 std::unique_ptr<SequencedSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15145 detail_->factory.AddSocketDataProvider(socket_data.get());
dchengc7eeda422015-12-26 03:56:48146 detail_->socket_data_vector.push_back(std::move(socket_data));
[email protected]a62449522014-06-05 11:11:15147}
148
mmenked0d201a2015-06-08 12:00:12149void WebSocketMockClientSocketFactoryMaker::AddSSLSocketDataProvider(
danakj9c5cab52016-04-16 00:54:33150 std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) {
[email protected]a62449522014-06-05 11:11:15151 detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get());
dchengc7eeda422015-12-26 03:56:48152 detail_->ssl_socket_data_vector.push_back(std::move(ssl_socket_data));
[email protected]a31ecc02013-12-05 08:30:55153}
154
155WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
[email protected]654866142014-06-24 22:53:31156 : url_request_context_(true), url_request_context_initialized_(false) {
[email protected]a31ecc02013-12-05 08:30:55157 url_request_context_.set_client_socket_factory(maker_.factory());
158}
159
160WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {}
161
[email protected]a62449522014-06-05 11:11:15162void WebSocketTestURLRequestContextHost::AddRawExpectations(
danakj9c5cab52016-04-16 00:54:33163 std::unique_ptr<SequencedSocketData> socket_data) {
dchengc7eeda422015-12-26 03:56:48164 maker_.AddRawExpectations(std::move(socket_data));
[email protected]a62449522014-06-05 11:11:15165}
166
167void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider(
danakj9c5cab52016-04-16 00:54:33168 std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) {
dchengc7eeda422015-12-26 03:56:48169 maker_.AddSSLSocketDataProvider(std::move(ssl_socket_data));
[email protected]a31ecc02013-12-05 08:30:55170}
171
ricea38fc268c2015-02-09 02:41:29172void WebSocketTestURLRequestContextHost::SetProxyConfig(
173 const std::string& proxy_rules) {
174 DCHECK(!url_request_context_initialized_);
rdsmith82957ad2015-09-16 19:42:03175 proxy_service_ = ProxyService::CreateFixed(proxy_rules);
ricea38fc268c2015-02-09 02:41:29176 url_request_context_.set_proxy_service(proxy_service_.get());
177}
178
[email protected]a31ecc02013-12-05 08:30:55179TestURLRequestContext*
180WebSocketTestURLRequestContextHost::GetURLRequestContext() {
[email protected]654866142014-06-24 22:53:31181 if (!url_request_context_initialized_) {
182 url_request_context_.Init();
183 // A Network Delegate is required to make the URLRequest::Delegate work.
184 url_request_context_.set_network_delegate(&network_delegate_);
185 url_request_context_initialized_ = true;
186 }
[email protected]a31ecc02013-12-05 08:30:55187 return &url_request_context_;
188}
189
[email protected]ce9f7ffd2013-10-11 06:04:11190} // namespace net