blob: 6091c9e0be16a1a95d48e2c7cb7ba5f37b41eda7 [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"
14#include "net/socket/socket_test_util.h"
[email protected]ce9f7ffd2013-10-11 06:04:1115
16namespace net {
17
18namespace {
19const uint64 kA =
20 (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d);
21const uint64 kC = 12345;
22const uint64 kM = static_cast<uint64>(1) << 48;
23
24} // namespace
25
26LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed)
27 : current_(seed) {}
28
29uint32 LinearCongruentialGenerator::Generate() {
30 uint64 result = current_;
31 current_ = (current_ * kA + kC) % kM;
32 return static_cast<uint32>(result >> 16);
33}
34
[email protected]a31ecc02013-12-05 08:30:5535std::string WebSocketStandardRequest(const std::string& path,
yhiranoa505d292015-01-22 07:34:2936 const std::string& host,
[email protected]a31ecc02013-12-05 08:30:5537 const std::string& origin,
38 const std::string& extra_headers) {
39 // Unrelated changes in net/http may change the order and default-values of
40 // HTTP headers, causing WebSocket tests to fail. It is safe to update this
41 // string in that case.
42 return base::StringPrintf(
43 "GET %s HTTP/1.1\r\n"
yhiranoa505d292015-01-22 07:34:2944 "Host: %s\r\n"
[email protected]a31ecc02013-12-05 08:30:5545 "Connection: Upgrade\r\n"
[email protected]e5760f522014-02-05 12:28:5046 "Pragma: no-cache\r\n"
47 "Cache-Control: no-cache\r\n"
[email protected]a31ecc02013-12-05 08:30:5548 "Upgrade: websocket\r\n"
49 "Origin: %s\r\n"
50 "Sec-WebSocket-Version: 13\r\n"
51 "User-Agent:\r\n"
jgraettinger8f00db8b2014-09-24 23:00:0652 "Accept-Encoding: gzip, deflate\r\n"
[email protected]a31ecc02013-12-05 08:30:5553 "Accept-Language: en-us,fr\r\n"
54 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
[email protected]0be93922014-01-29 00:42:4555 "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
[email protected]a31ecc02013-12-05 08:30:5556 "%s\r\n",
yhiranoa505d292015-01-22 07:34:2957 path.c_str(), host.c_str(), origin.c_str(), extra_headers.c_str());
[email protected]a31ecc02013-12-05 08:30:5558}
59
60std::string WebSocketStandardResponse(const std::string& extra_headers) {
61 return base::StringPrintf(
62 "HTTP/1.1 101 Switching Protocols\r\n"
63 "Upgrade: websocket\r\n"
64 "Connection: Upgrade\r\n"
65 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
66 "%s\r\n",
67 extra_headers.c_str());
68}
69
70struct WebSocketDeterministicMockClientSocketFactoryMaker::Detail {
71 std::string expect_written;
72 std::string return_to_read;
[email protected]94831522014-02-06 12:05:1873 std::vector<MockRead> reads;
[email protected]a31ecc02013-12-05 08:30:5574 MockWrite write;
[email protected]a62449522014-06-05 11:11:1575 ScopedVector<DeterministicSocketData> socket_data_vector;
76 ScopedVector<SSLSocketDataProvider> ssl_socket_data_vector;
[email protected]a31ecc02013-12-05 08:30:5577 DeterministicMockClientSocketFactory factory;
78};
79
80WebSocketDeterministicMockClientSocketFactoryMaker::
81 WebSocketDeterministicMockClientSocketFactoryMaker()
82 : detail_(new Detail) {}
83
84WebSocketDeterministicMockClientSocketFactoryMaker::
85 ~WebSocketDeterministicMockClientSocketFactoryMaker() {}
86
87DeterministicMockClientSocketFactory*
88WebSocketDeterministicMockClientSocketFactoryMaker::factory() {
89 return &detail_->factory;
90}
91
92void WebSocketDeterministicMockClientSocketFactoryMaker::SetExpectations(
93 const std::string& expect_written,
94 const std::string& return_to_read) {
[email protected]94831522014-02-06 12:05:1895 const size_t kHttpStreamParserBufferSize = 4096;
[email protected]a31ecc02013-12-05 08:30:5596 // We need to extend the lifetime of these strings.
97 detail_->expect_written = expect_written;
98 detail_->return_to_read = return_to_read;
[email protected]94831522014-02-06 12:05:1899 int sequence = 0;
[email protected]0be93922014-01-29 00:42:45100 detail_->write = MockWrite(SYNCHRONOUS,
101 detail_->expect_written.data(),
102 detail_->expect_written.size(),
[email protected]94831522014-02-06 12:05:18103 sequence++);
104 // HttpStreamParser reads 4KB at a time. We need to take this implementation
105 // detail into account if |return_to_read| is big enough.
106 for (size_t place = 0; place < detail_->return_to_read.size();
107 place += kHttpStreamParserBufferSize) {
108 detail_->reads.push_back(
109 MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place,
110 std::min(detail_->return_to_read.size() - place,
111 kHttpStreamParserBufferSize),
112 sequence++));
113 }
[email protected]a31ecc02013-12-05 08:30:55114 scoped_ptr<DeterministicSocketData> socket_data(
[email protected]94831522014-02-06 12:05:18115 new DeterministicSocketData(vector_as_array(&detail_->reads),
116 detail_->reads.size(),
117 &detail_->write,
118 1));
[email protected]a31ecc02013-12-05 08:30:55119 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
[email protected]94831522014-02-06 12:05:18120 socket_data->SetStop(sequence);
[email protected]a62449522014-06-05 11:11:15121 AddRawExpectations(socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55122}
123
[email protected]a62449522014-06-05 11:11:15124void WebSocketDeterministicMockClientSocketFactoryMaker::AddRawExpectations(
[email protected]a31ecc02013-12-05 08:30:55125 scoped_ptr<DeterministicSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15126 detail_->factory.AddSocketDataProvider(socket_data.get());
127 detail_->socket_data_vector.push_back(socket_data.release());
128}
129
130void
131WebSocketDeterministicMockClientSocketFactoryMaker::AddSSLSocketDataProvider(
132 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
133 detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get());
134 detail_->ssl_socket_data_vector.push_back(ssl_socket_data.release());
[email protected]a31ecc02013-12-05 08:30:55135}
136
137WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
[email protected]654866142014-06-24 22:53:31138 : url_request_context_(true), url_request_context_initialized_(false) {
[email protected]a31ecc02013-12-05 08:30:55139 url_request_context_.set_client_socket_factory(maker_.factory());
140}
141
142WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {}
143
[email protected]a62449522014-06-05 11:11:15144void WebSocketTestURLRequestContextHost::AddRawExpectations(
[email protected]a31ecc02013-12-05 08:30:55145 scoped_ptr<DeterministicSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15146 maker_.AddRawExpectations(socket_data.Pass());
147}
148
149void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider(
150 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
151 maker_.AddSSLSocketDataProvider(ssl_socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55152}
153
154TestURLRequestContext*
155WebSocketTestURLRequestContextHost::GetURLRequestContext() {
[email protected]654866142014-06-24 22:53:31156 if (!url_request_context_initialized_) {
157 url_request_context_.Init();
158 // A Network Delegate is required to make the URLRequest::Delegate work.
159 url_request_context_.set_network_delegate(&network_delegate_);
160 url_request_context_initialized_ = true;
161 }
[email protected]a31ecc02013-12-05 08:30:55162 return &url_request_context_;
163}
164
[email protected]ce9f7ffd2013-10-11 06:04:11165} // namespace net