blob: bfa8980344775b3ab1c37aeaf0aa4f5a9bc6cadb [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,
36 const std::string& origin,
37 const std::string& extra_headers) {
38 // Unrelated changes in net/http may change the order and default-values of
39 // HTTP headers, causing WebSocket tests to fail. It is safe to update this
40 // string in that case.
41 return base::StringPrintf(
42 "GET %s HTTP/1.1\r\n"
43 "Host: localhost\r\n"
44 "Connection: Upgrade\r\n"
[email protected]e5760f522014-02-05 12:28:5045 "Pragma: no-cache\r\n"
46 "Cache-Control: no-cache\r\n"
[email protected]a31ecc02013-12-05 08:30:5547 "Upgrade: websocket\r\n"
48 "Origin: %s\r\n"
49 "Sec-WebSocket-Version: 13\r\n"
50 "User-Agent:\r\n"
51 "Accept-Encoding: gzip,deflate\r\n"
52 "Accept-Language: en-us,fr\r\n"
53 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
[email protected]0be93922014-01-29 00:42:4554 "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
[email protected]a31ecc02013-12-05 08:30:5555 "%s\r\n",
56 path.c_str(),
57 origin.c_str(),
58 extra_headers.c_str());
59}
60
61std::string WebSocketStandardResponse(const std::string& extra_headers) {
62 return base::StringPrintf(
63 "HTTP/1.1 101 Switching Protocols\r\n"
64 "Upgrade: websocket\r\n"
65 "Connection: Upgrade\r\n"
66 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
67 "%s\r\n",
68 extra_headers.c_str());
69}
70
71struct WebSocketDeterministicMockClientSocketFactoryMaker::Detail {
72 std::string expect_written;
73 std::string return_to_read;
[email protected]94831522014-02-06 12:05:1874 std::vector<MockRead> reads;
[email protected]a31ecc02013-12-05 08:30:5575 MockWrite write;
[email protected]a62449522014-06-05 11:11:1576 ScopedVector<DeterministicSocketData> socket_data_vector;
77 ScopedVector<SSLSocketDataProvider> ssl_socket_data_vector;
[email protected]a31ecc02013-12-05 08:30:5578 DeterministicMockClientSocketFactory factory;
79};
80
81WebSocketDeterministicMockClientSocketFactoryMaker::
82 WebSocketDeterministicMockClientSocketFactoryMaker()
83 : detail_(new Detail) {}
84
85WebSocketDeterministicMockClientSocketFactoryMaker::
86 ~WebSocketDeterministicMockClientSocketFactoryMaker() {}
87
88DeterministicMockClientSocketFactory*
89WebSocketDeterministicMockClientSocketFactoryMaker::factory() {
90 return &detail_->factory;
91}
92
93void WebSocketDeterministicMockClientSocketFactoryMaker::SetExpectations(
94 const std::string& expect_written,
95 const std::string& return_to_read) {
[email protected]94831522014-02-06 12:05:1896 const size_t kHttpStreamParserBufferSize = 4096;
[email protected]a31ecc02013-12-05 08:30:5597 // We need to extend the lifetime of these strings.
98 detail_->expect_written = expect_written;
99 detail_->return_to_read = return_to_read;
[email protected]94831522014-02-06 12:05:18100 int sequence = 0;
[email protected]0be93922014-01-29 00:42:45101 detail_->write = MockWrite(SYNCHRONOUS,
102 detail_->expect_written.data(),
103 detail_->expect_written.size(),
[email protected]94831522014-02-06 12:05:18104 sequence++);
105 // HttpStreamParser reads 4KB at a time. We need to take this implementation
106 // detail into account if |return_to_read| is big enough.
107 for (size_t place = 0; place < detail_->return_to_read.size();
108 place += kHttpStreamParserBufferSize) {
109 detail_->reads.push_back(
110 MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place,
111 std::min(detail_->return_to_read.size() - place,
112 kHttpStreamParserBufferSize),
113 sequence++));
114 }
[email protected]a31ecc02013-12-05 08:30:55115 scoped_ptr<DeterministicSocketData> socket_data(
[email protected]94831522014-02-06 12:05:18116 new DeterministicSocketData(vector_as_array(&detail_->reads),
117 detail_->reads.size(),
118 &detail_->write,
119 1));
[email protected]a31ecc02013-12-05 08:30:55120 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
[email protected]94831522014-02-06 12:05:18121 socket_data->SetStop(sequence);
[email protected]a62449522014-06-05 11:11:15122 AddRawExpectations(socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55123}
124
[email protected]a62449522014-06-05 11:11:15125void WebSocketDeterministicMockClientSocketFactoryMaker::AddRawExpectations(
[email protected]a31ecc02013-12-05 08:30:55126 scoped_ptr<DeterministicSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15127 detail_->factory.AddSocketDataProvider(socket_data.get());
128 detail_->socket_data_vector.push_back(socket_data.release());
129}
130
131void
132WebSocketDeterministicMockClientSocketFactoryMaker::AddSSLSocketDataProvider(
133 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
134 detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get());
135 detail_->ssl_socket_data_vector.push_back(ssl_socket_data.release());
[email protected]a31ecc02013-12-05 08:30:55136}
137
138WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
139 : url_request_context_(true) {
140 url_request_context_.set_client_socket_factory(maker_.factory());
141}
142
143WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {}
144
[email protected]a62449522014-06-05 11:11:15145void WebSocketTestURLRequestContextHost::AddRawExpectations(
[email protected]a31ecc02013-12-05 08:30:55146 scoped_ptr<DeterministicSocketData> socket_data) {
[email protected]a62449522014-06-05 11:11:15147 maker_.AddRawExpectations(socket_data.Pass());
148}
149
150void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider(
151 scoped_ptr<SSLSocketDataProvider> ssl_socket_data) {
152 maker_.AddSSLSocketDataProvider(ssl_socket_data.Pass());
[email protected]a31ecc02013-12-05 08:30:55153}
154
155TestURLRequestContext*
156WebSocketTestURLRequestContextHost::GetURLRequestContext() {
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 return &url_request_context_;
161}
162
[email protected]ce9f7ffd2013-10-11 06:04:11163} // namespace net