blob: 5396020b1375c6c445f00beb44fcec5729636335 [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
[email protected]94831522014-02-06 12:05:187#include <algorithm>
8#include <vector>
9
[email protected]ce9f7ffd2013-10-11 06:04:1110#include "base/basictypes.h"
[email protected]a62449522014-06-05 11:11:1511#include "base/memory/scoped_vector.h"
[email protected]94831522014-02-06 12:05:1812#include "base/stl_util.h"
[email protected]a31ecc02013-12-05 08:30:5513#include "base/strings/stringprintf.h"
ricea38fc268c2015-02-09 02:41:2914#include "net/proxy/proxy_service.h"
[email protected]a31ecc02013-12-05 08:30:5515#include "net/socket/socket_test_util.h"
[email protected]ce9f7ffd2013-10-11 06:04:1116
17namespace net {
18
19namespace {
20const uint64 kA =
21 (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d);
22const uint64 kC = 12345;
23const uint64 kM = static_cast<uint64>(1) << 48;
24
25} // namespace
26
27LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed)
28 : current_(seed) {}
29
30uint32 LinearCongruentialGenerator::Generate() {
31 uint64 result = current_;
32 current_ = (current_ * kA + kC) % kM;
33 return static_cast<uint32>(result >> 16);
34}
35
[email protected]a31ecc02013-12-05 08:30:5536std::string WebSocketStandardRequest(const std::string& path,
yhiranoa505d292015-01-22 07:34:2937 const std::string& host,
[email protected]a31ecc02013-12-05 08:30:5538 const std::string& origin,
39 const std::string& extra_headers) {
yhirano01a5d662015-02-12 04:33:0640 return WebSocketStandardRequestWithCookies(path, host, origin, std::string(),
41 extra_headers);
42}
43
44std::string WebSocketStandardRequestWithCookies(
45 const std::string& path,
46 const std::string& host,
47 const std::string& origin,
48 const std::string& cookies,
49 const std::string& extra_headers) {
[email protected]a31ecc02013-12-05 08:30:5550 // Unrelated changes in net/http may change the order and default-values of
51 // HTTP headers, causing WebSocket tests to fail. It is safe to update this
52 // string in that case.
53 return base::StringPrintf(
54 "GET %s HTTP/1.1\r\n"
yhiranoa505d292015-01-22 07:34:2955 "Host: %s\r\n"
[email protected]a31ecc02013-12-05 08:30:5556 "Connection: Upgrade\r\n"
[email protected]e5760f522014-02-05 12:28:5057 "Pragma: no-cache\r\n"
58 "Cache-Control: no-cache\r\n"
[email protected]a31ecc02013-12-05 08:30:5559 "Upgrade: websocket\r\n"
60 "Origin: %s\r\n"
61 "Sec-WebSocket-Version: 13\r\n"
62 "User-Agent:\r\n"
jgraettinger8f00db8b2014-09-24 23:00:0663 "Accept-Encoding: gzip, deflate\r\n"
[email protected]a31ecc02013-12-05 08:30:5564 "Accept-Language: en-us,fr\r\n"
yhirano01a5d662015-02-12 04:33:0665 "%s"
[email protected]a31ecc02013-12-05 08:30:5566 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
[email protected]0be93922014-01-29 00:42:4567 "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
[email protected]a31ecc02013-12-05 08:30:5568 "%s\r\n",
yhirano01a5d662015-02-12 04:33:0669 path.c_str(), host.c_str(), origin.c_str(), cookies.c_str(),
70 extra_headers.c_str());
[email protected]a31ecc02013-12-05 08:30:5571}
72
73std::string WebSocketStandardResponse(const std::string& extra_headers) {
74 return base::StringPrintf(
75 "HTTP/1.1 101 Switching Protocols\r\n"
76 "Upgrade: websocket\r\n"
77 "Connection: Upgrade\r\n"
78 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
79 "%s\r\n",
80 extra_headers.c_str());
81}
82
mmenked0d201a2015-06-08 12:00:1283struct WebSocketMockClientSocketFactoryMaker::Detail {
[email protected]a31ecc02013-12-05 08:30:5584 std::string expect_written;
85 std::string return_to_read;
[email protected]94831522014-02-06 12:05:1886 std::vector<MockRead> reads;
[email protected]a31ecc02013-12-05 08:30:5587 MockWrite write;
mmenked0d201a2015-06-08 12:00:1288 ScopedVector<SequencedSocketData> socket_data_vector;
[email protected]a62449522014-06-05 11:11:1589 ScopedVector<SSLSocketDataProvider> ssl_socket_data_vector;
mmenked0d201a2015-06-08 12:00:1290 MockClientSocketFactory factory;
[email protected]a31ecc02013-12-05 08:30:5591};
92
mmenked0d201a2015-06-08 12:00:1293WebSocketMockClientSocketFactoryMaker::WebSocketMockClientSocketFactoryMaker()
94 : detail_(new Detail) {
95}
[email protected]a31ecc02013-12-05 08:30:5596
mmenked0d201a2015-06-08 12:00:1297WebSocketMockClientSocketFactoryMaker::
98 ~WebSocketMockClientSocketFactoryMaker() {
99}
[email protected]a31ecc02013-12-05 08:30:55100
mmenked0d201a2015-06-08 12:00:12101MockClientSocketFactory* WebSocketMockClientSocketFactoryMaker::factory() {
[email protected]a31ecc02013-12-05 08:30:55102 return &detail_->factory;
103}
104
mmenked0d201a2015-06-08 12:00:12105void WebSocketMockClientSocketFactoryMaker::SetExpectations(
[email protected]a31ecc02013-12-05 08:30:55106 const std::string& expect_written,
107 const std::string& return_to_read) {
[email protected]94831522014-02-06 12:05:18108 const size_t kHttpStreamParserBufferSize = 4096;
[email protected]a31ecc02013-12-05 08:30:55109 // We need to extend the lifetime of these strings.
110 detail_->expect_written = expect_written;
111 detail_->return_to_read = return_to_read;
[email protected]94831522014-02-06 12:05:18112 int sequence = 0;
[email protected]0be93922014-01-29 00:42:45113 detail_->write = MockWrite(SYNCHRONOUS,
114 detail_->expect_written.data(),
115 detail_->expect_written.size(),
[email protected]94831522014-02-06 12:05:18116 sequence++);
117 // HttpStreamParser reads 4KB at a time. We need to take this implementation
118 // detail into account if |return_to_read| is big enough.
119 for (size_t place = 0; place < detail_->return_to_read.size();
120 place += kHttpStreamParserBufferSize) {
121 detail_->reads.push_back(
122 MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place,
123 std::min(detail_->return_to_read.size() - place,
124 kHttpStreamParserBufferSize),
125 sequence++));
126 }
mmenked0d201a2015-06-08 12:00:12127 scoped_ptr<SequencedSocketData> socket_data(
128 new SequencedSocketData(vector_as_array(&detail_->reads),
129 detail_->reads.size(), &detail_->write, 1));
[email protected]a31ecc02013-12-05 08:30:55130 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
[email protected]a62449522014-06-05 11:11:15131 AddRawExpectations(socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55132}
133
mmenked0d201a2015-06-08 12:00:12134void WebSocketMockClientSocketFactoryMaker::AddRawExpectations(
135 scoped_ptr<SequencedSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15136 detail_->factory.AddSocketDataProvider(socket_data.get());
hari.singh1b87973c2015-06-01 13:34:07137 detail_->socket_data_vector.push_back(socket_data.Pass());
[email protected]a62449522014-06-05 11:11:15138}
139
mmenked0d201a2015-06-08 12:00:12140void WebSocketMockClientSocketFactoryMaker::AddSSLSocketDataProvider(
[email protected]a62449522014-06-05 11:11:15141 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
142 detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get());
hari.singh1b87973c2015-06-01 13:34:07143 detail_->ssl_socket_data_vector.push_back(ssl_socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55144}
145
146WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
[email protected]654866142014-06-24 22:53:31147 : url_request_context_(true), url_request_context_initialized_(false) {
[email protected]a31ecc02013-12-05 08:30:55148 url_request_context_.set_client_socket_factory(maker_.factory());
149}
150
151WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {}
152
[email protected]a62449522014-06-05 11:11:15153void WebSocketTestURLRequestContextHost::AddRawExpectations(
mmenked0d201a2015-06-08 12:00:12154 scoped_ptr<SequencedSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15155 maker_.AddRawExpectations(socket_data.Pass());
156}
157
158void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider(
159 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
160 maker_.AddSSLSocketDataProvider(ssl_socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55161}
162
ricea38fc268c2015-02-09 02:41:29163void WebSocketTestURLRequestContextHost::SetProxyConfig(
164 const std::string& proxy_rules) {
165 DCHECK(!url_request_context_initialized_);
166 proxy_service_.reset(ProxyService::CreateFixed(proxy_rules));
167 url_request_context_.set_proxy_service(proxy_service_.get());
168}
169
[email protected]a31ecc02013-12-05 08:30:55170TestURLRequestContext*
171WebSocketTestURLRequestContextHost::GetURLRequestContext() {
[email protected]654866142014-06-24 22:53:31172 if (!url_request_context_initialized_) {
173 url_request_context_.Init();
174 // A Network Delegate is required to make the URLRequest::Delegate work.
175 url_request_context_.set_network_delegate(&network_delegate_);
176 url_request_context_initialized_ = true;
177 }
[email protected]a31ecc02013-12-05 08:30:55178 return &url_request_context_;
179}
180
[email protected]ce9f7ffd2013-10-11 06:04:11181} // namespace net