blob: 5b5d13a44ab7d8d1910d2cd087f5b6f5f6054e12 [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>
9#include <vector>
10
[email protected]a31ecc02013-12-05 08:30:5511#include "base/strings/stringprintf.h"
ricea38fc268c2015-02-09 02:41:2912#include "net/proxy/proxy_service.h"
[email protected]a31ecc02013-12-05 08:30:5513#include "net/socket/socket_test_util.h"
mkwst4997ce82015-07-25 12:00:0514#include "url/origin.h"
[email protected]ce9f7ffd2013-10-11 06:04:1115
16namespace net {
17
18namespace {
tfarinaea94afc232015-10-20 04:23:3619
tfarina8a2c66c22015-10-13 19:14:4920const uint64_t kA = (static_cast<uint64_t>(0x5851f42d) << 32) +
21 static_cast<uint64_t>(0x4c957f2d);
22const uint64_t kC = 12345;
23const uint64_t kM = static_cast<uint64_t>(1) << 48;
[email protected]ce9f7ffd2013-10-11 06:04:1124
25} // namespace
26
tfarina8a2c66c22015-10-13 19:14:4927LinearCongruentialGenerator::LinearCongruentialGenerator(uint32_t seed)
[email protected]ce9f7ffd2013-10-11 06:04:1128 : current_(seed) {}
29
tfarina8a2c66c22015-10-13 19:14:4930uint32_t LinearCongruentialGenerator::Generate() {
31 uint64_t result = current_;
[email protected]ce9f7ffd2013-10-11 06:04:1132 current_ = (current_ * kA + kC) % kM;
tfarina8a2c66c22015-10-13 19:14:4933 return static_cast<uint32_t>(result >> 16);
[email protected]ce9f7ffd2013-10-11 06:04:1134}
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,
mkwst4997ce82015-07-25 12:00:0538 const url::Origin& origin,
[email protected]a31ecc02013-12-05 08:30:5539 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,
mkwst4997ce82015-07-25 12:00:0547 const url::Origin& origin,
yhirano01a5d662015-02-12 04:33:0648 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",
mkwst4997ce82015-07-25 12:00:0569 path.c_str(), host.c_str(), origin.Serialize().c_str(), cookies.c_str(),
yhirano01a5d662015-02-12 04:33:0670 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;
yhirano592ff7f2015-12-07 08:45:1988 std::vector<scoped_ptr<SequencedSocketData>> socket_data_vector;
89 std::vector<scoped_ptr<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 }
davidben5f8b6bc2015-11-25 03:19:54127 scoped_ptr<SequencedSocketData> socket_data(new SequencedSocketData(
128 detail_->reads.data(), detail_->reads.size(), &detail_->write, 1));
[email protected]a31ecc02013-12-05 08:30:55129 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
[email protected]a62449522014-06-05 11:11:15130 AddRawExpectations(socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55131}
132
mmenked0d201a2015-06-08 12:00:12133void WebSocketMockClientSocketFactoryMaker::AddRawExpectations(
134 scoped_ptr<SequencedSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15135 detail_->factory.AddSocketDataProvider(socket_data.get());
hari.singh1b87973c2015-06-01 13:34:07136 detail_->socket_data_vector.push_back(socket_data.Pass());
[email protected]a62449522014-06-05 11:11:15137}
138
mmenked0d201a2015-06-08 12:00:12139void WebSocketMockClientSocketFactoryMaker::AddSSLSocketDataProvider(
[email protected]a62449522014-06-05 11:11:15140 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
141 detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get());
hari.singh1b87973c2015-06-01 13:34:07142 detail_->ssl_socket_data_vector.push_back(ssl_socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55143}
144
145WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
[email protected]654866142014-06-24 22:53:31146 : url_request_context_(true), url_request_context_initialized_(false) {
[email protected]a31ecc02013-12-05 08:30:55147 url_request_context_.set_client_socket_factory(maker_.factory());
148}
149
150WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {}
151
[email protected]a62449522014-06-05 11:11:15152void WebSocketTestURLRequestContextHost::AddRawExpectations(
mmenked0d201a2015-06-08 12:00:12153 scoped_ptr<SequencedSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15154 maker_.AddRawExpectations(socket_data.Pass());
155}
156
157void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider(
158 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
159 maker_.AddSSLSocketDataProvider(ssl_socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55160}
161
ricea38fc268c2015-02-09 02:41:29162void WebSocketTestURLRequestContextHost::SetProxyConfig(
163 const std::string& proxy_rules) {
164 DCHECK(!url_request_context_initialized_);
rdsmith82957ad2015-09-16 19:42:03165 proxy_service_ = ProxyService::CreateFixed(proxy_rules);
ricea38fc268c2015-02-09 02:41:29166 url_request_context_.set_proxy_service(proxy_service_.get());
167}
168
[email protected]a31ecc02013-12-05 08:30:55169TestURLRequestContext*
170WebSocketTestURLRequestContextHost::GetURLRequestContext() {
[email protected]654866142014-06-24 22:53:31171 if (!url_request_context_initialized_) {
172 url_request_context_.Init();
173 // A Network Delegate is required to make the URLRequest::Delegate work.
174 url_request_context_.set_network_delegate(&network_delegate_);
175 url_request_context_initialized_ = true;
176 }
[email protected]a31ecc02013-12-05 08:30:55177 return &url_request_context_;
178}
179
[email protected]ce9f7ffd2013-10-11 06:04:11180} // namespace net