blob: 46dda2be83b9805f35b1a859a6530be785e6ec77 [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
penghuang58024d02016-06-29 16:45:5337std::string WebSocketStandardRequest(const std::string& path,
38 const std::string& host,
39 const url::Origin& origin,
40 const std::string& extra_headers) {
yhirano01a5d662015-02-12 04:33:0641 return WebSocketStandardRequestWithCookies(path, host, origin, std::string(),
42 extra_headers);
43}
44
45std::string WebSocketStandardRequestWithCookies(
46 const std::string& path,
47 const std::string& host,
mkwst4997ce82015-07-25 12:00:0548 const url::Origin& origin,
yhirano01a5d662015-02-12 04:33:0649 const std::string& cookies,
50 const std::string& extra_headers) {
[email protected]a31ecc02013-12-05 08:30:5551 // Unrelated changes in net/http may change the order and default-values of
52 // HTTP headers, causing WebSocket tests to fail. It is safe to update this
penghuang58024d02016-06-29 16:45:5353 // string in that case.
54 return base::StringPrintf(
55 "GET %s HTTP/1.1\r\n"
56 "Host: %s\r\n"
57 "Connection: Upgrade\r\n"
58 "Pragma: no-cache\r\n"
59 "Cache-Control: no-cache\r\n"
60 "Upgrade: websocket\r\n"
61 "Origin: %s\r\n"
62 "Sec-WebSocket-Version: 13\r\n"
63 "User-Agent:\r\n"
64 "Accept-Encoding: gzip, deflate\r\n"
65 "Accept-Language: en-us,fr\r\n"
66 "%s"
67 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
68 "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
69 "%s\r\n",
70 path.c_str(), host.c_str(), origin.Serialize().c_str(), cookies.c_str(),
71 extra_headers.c_str());
[email protected]a31ecc02013-12-05 08:30:5572}
73
74std::string WebSocketStandardResponse(const std::string& extra_headers) {
75 return base::StringPrintf(
76 "HTTP/1.1 101 Switching Protocols\r\n"
77 "Upgrade: websocket\r\n"
78 "Connection: Upgrade\r\n"
79 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
80 "%s\r\n",
81 extra_headers.c_str());
82}
83
mmenked0d201a2015-06-08 12:00:1284struct WebSocketMockClientSocketFactoryMaker::Detail {
[email protected]a31ecc02013-12-05 08:30:5585 std::string expect_written;
86 std::string return_to_read;
[email protected]94831522014-02-06 12:05:1887 std::vector<MockRead> reads;
[email protected]a31ecc02013-12-05 08:30:5588 MockWrite write;
danakj9c5cab52016-04-16 00:54:3389 std::vector<std::unique_ptr<SequencedSocketData>> socket_data_vector;
90 std::vector<std::unique_ptr<SSLSocketDataProvider>> ssl_socket_data_vector;
mmenked0d201a2015-06-08 12:00:1291 MockClientSocketFactory factory;
[email protected]a31ecc02013-12-05 08:30:5592};
93
mmenked0d201a2015-06-08 12:00:1294WebSocketMockClientSocketFactoryMaker::WebSocketMockClientSocketFactoryMaker()
95 : detail_(new Detail) {
96}
[email protected]a31ecc02013-12-05 08:30:5597
mmenked0d201a2015-06-08 12:00:1298WebSocketMockClientSocketFactoryMaker::
99 ~WebSocketMockClientSocketFactoryMaker() {
100}
[email protected]a31ecc02013-12-05 08:30:55101
mmenked0d201a2015-06-08 12:00:12102MockClientSocketFactory* WebSocketMockClientSocketFactoryMaker::factory() {
[email protected]a31ecc02013-12-05 08:30:55103 return &detail_->factory;
104}
105
mmenked0d201a2015-06-08 12:00:12106void WebSocketMockClientSocketFactoryMaker::SetExpectations(
[email protected]a31ecc02013-12-05 08:30:55107 const std::string& expect_written,
108 const std::string& return_to_read) {
[email protected]94831522014-02-06 12:05:18109 const size_t kHttpStreamParserBufferSize = 4096;
[email protected]a31ecc02013-12-05 08:30:55110 // We need to extend the lifetime of these strings.
111 detail_->expect_written = expect_written;
112 detail_->return_to_read = return_to_read;
[email protected]94831522014-02-06 12:05:18113 int sequence = 0;
[email protected]0be93922014-01-29 00:42:45114 detail_->write = MockWrite(SYNCHRONOUS,
115 detail_->expect_written.data(),
116 detail_->expect_written.size(),
[email protected]94831522014-02-06 12:05:18117 sequence++);
118 // HttpStreamParser reads 4KB at a time. We need to take this implementation
119 // detail into account if |return_to_read| is big enough.
120 for (size_t place = 0; place < detail_->return_to_read.size();
121 place += kHttpStreamParserBufferSize) {
122 detail_->reads.push_back(
123 MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place,
124 std::min(detail_->return_to_read.size() - place,
125 kHttpStreamParserBufferSize),
126 sequence++));
127 }
danakj9c5cab52016-04-16 00:54:33128 std::unique_ptr<SequencedSocketData> socket_data(new SequencedSocketData(
davidben5f8b6bc2015-11-25 03:19:54129 detail_->reads.data(), detail_->reads.size(), &detail_->write, 1));
[email protected]a31ecc02013-12-05 08:30:55130 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
dchengc7eeda422015-12-26 03:56:48131 AddRawExpectations(std::move(socket_data));
[email protected]a31ecc02013-12-05 08:30:55132}
133
mmenked0d201a2015-06-08 12:00:12134void WebSocketMockClientSocketFactoryMaker::AddRawExpectations(
danakj9c5cab52016-04-16 00:54:33135 std::unique_ptr<SequencedSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15136 detail_->factory.AddSocketDataProvider(socket_data.get());
dchengc7eeda422015-12-26 03:56:48137 detail_->socket_data_vector.push_back(std::move(socket_data));
[email protected]a62449522014-06-05 11:11:15138}
139
mmenked0d201a2015-06-08 12:00:12140void WebSocketMockClientSocketFactoryMaker::AddSSLSocketDataProvider(
danakj9c5cab52016-04-16 00:54:33141 std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) {
[email protected]a62449522014-06-05 11:11:15142 detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get());
dchengc7eeda422015-12-26 03:56:48143 detail_->ssl_socket_data_vector.push_back(std::move(ssl_socket_data));
[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(
danakj9c5cab52016-04-16 00:54:33154 std::unique_ptr<SequencedSocketData> socket_data) {
dchengc7eeda422015-12-26 03:56:48155 maker_.AddRawExpectations(std::move(socket_data));
[email protected]a62449522014-06-05 11:11:15156}
157
158void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider(
danakj9c5cab52016-04-16 00:54:33159 std::unique_ptr<SSLSocketDataProvider> ssl_socket_data) {
dchengc7eeda422015-12-26 03:56:48160 maker_.AddSSLSocketDataProvider(std::move(ssl_socket_data));
[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_);
rdsmith82957ad2015-09-16 19:42:03166 proxy_service_ = ProxyService::CreateFixed(proxy_rules);
ricea38fc268c2015-02-09 02:41:29167 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