blob: 8146cc39174d39858a4c5aad47bd29c3fc583ce1 [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
7#include "base/basictypes.h"
[email protected]a31ecc02013-12-05 08:30:558#include "base/strings/stringprintf.h"
9#include "net/socket/socket_test_util.h"
[email protected]ce9f7ffd2013-10-11 06:04:1110
11namespace net {
12
13namespace {
14const uint64 kA =
15 (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d);
16const uint64 kC = 12345;
17const uint64 kM = static_cast<uint64>(1) << 48;
18
19} // namespace
20
21LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed)
22 : current_(seed) {}
23
24uint32 LinearCongruentialGenerator::Generate() {
25 uint64 result = current_;
26 current_ = (current_ * kA + kC) % kM;
27 return static_cast<uint32>(result >> 16);
28}
29
[email protected]a31ecc02013-12-05 08:30:5530std::string WebSocketStandardRequest(const std::string& path,
31 const std::string& origin,
32 const std::string& extra_headers) {
33 // Unrelated changes in net/http may change the order and default-values of
34 // HTTP headers, causing WebSocket tests to fail. It is safe to update this
35 // string in that case.
36 return base::StringPrintf(
37 "GET %s HTTP/1.1\r\n"
38 "Host: localhost\r\n"
39 "Connection: Upgrade\r\n"
[email protected]e5760f522014-02-05 12:28:5040 "Pragma: no-cache\r\n"
41 "Cache-Control: no-cache\r\n"
[email protected]a31ecc02013-12-05 08:30:5542 "Upgrade: websocket\r\n"
43 "Origin: %s\r\n"
44 "Sec-WebSocket-Version: 13\r\n"
45 "User-Agent:\r\n"
46 "Accept-Encoding: gzip,deflate\r\n"
47 "Accept-Language: en-us,fr\r\n"
48 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
[email protected]0be93922014-01-29 00:42:4549 "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
[email protected]a31ecc02013-12-05 08:30:5550 "%s\r\n",
51 path.c_str(),
52 origin.c_str(),
53 extra_headers.c_str());
54}
55
56std::string WebSocketStandardResponse(const std::string& extra_headers) {
57 return base::StringPrintf(
58 "HTTP/1.1 101 Switching Protocols\r\n"
59 "Upgrade: websocket\r\n"
60 "Connection: Upgrade\r\n"
61 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"
62 "%s\r\n",
63 extra_headers.c_str());
64}
65
66struct WebSocketDeterministicMockClientSocketFactoryMaker::Detail {
67 std::string expect_written;
68 std::string return_to_read;
69 MockRead read;
70 MockWrite write;
71 scoped_ptr<DeterministicSocketData> data;
72 DeterministicMockClientSocketFactory factory;
73};
74
75WebSocketDeterministicMockClientSocketFactoryMaker::
76 WebSocketDeterministicMockClientSocketFactoryMaker()
77 : detail_(new Detail) {}
78
79WebSocketDeterministicMockClientSocketFactoryMaker::
80 ~WebSocketDeterministicMockClientSocketFactoryMaker() {}
81
82DeterministicMockClientSocketFactory*
83WebSocketDeterministicMockClientSocketFactoryMaker::factory() {
84 return &detail_->factory;
85}
86
87void WebSocketDeterministicMockClientSocketFactoryMaker::SetExpectations(
88 const std::string& expect_written,
89 const std::string& return_to_read) {
90 // We need to extend the lifetime of these strings.
91 detail_->expect_written = expect_written;
92 detail_->return_to_read = return_to_read;
[email protected]0be93922014-01-29 00:42:4593 detail_->write = MockWrite(SYNCHRONOUS,
94 detail_->expect_written.data(),
95 detail_->expect_written.size(),
96 0);
97 detail_->read = MockRead(SYNCHRONOUS,
98 detail_->return_to_read.data(),
99 detail_->return_to_read.size(),
100 1);
[email protected]a31ecc02013-12-05 08:30:55101 scoped_ptr<DeterministicSocketData> socket_data(
102 new DeterministicSocketData(&detail_->read, 1, &detail_->write, 1));
103 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK));
104 socket_data->SetStop(2);
105 SetRawExpectations(socket_data.Pass());
106}
107
108void WebSocketDeterministicMockClientSocketFactoryMaker::SetRawExpectations(
109 scoped_ptr<DeterministicSocketData> socket_data) {
110 detail_->data = socket_data.Pass();
111 detail_->factory.AddSocketDataProvider(detail_->data.get());
112}
113
114WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost()
115 : url_request_context_(true) {
116 url_request_context_.set_client_socket_factory(maker_.factory());
117}
118
119WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {}
120
121void WebSocketTestURLRequestContextHost::SetRawExpectations(
122 scoped_ptr<DeterministicSocketData> socket_data) {
123 maker_.SetRawExpectations(socket_data.Pass());
124}
125
126TestURLRequestContext*
127WebSocketTestURLRequestContextHost::GetURLRequestContext() {
128 url_request_context_.Init();
129 // A Network Delegate is required to make the URLRequest::Delegate work.
130 url_request_context_.set_network_delegate(&network_delegate_);
131 return &url_request_context_;
132}
133
[email protected]ce9f7ffd2013-10-11 06:04:11134} // namespace net