[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 1 | // 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] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 8 | #include "base/strings/stringprintf.h" |
| 9 | #include "net/socket/socket_test_util.h" |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 10 | |
| 11 | namespace net { |
| 12 | |
| 13 | namespace { |
| 14 | const uint64 kA = |
| 15 | (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d); |
| 16 | const uint64 kC = 12345; |
| 17 | const uint64 kM = static_cast<uint64>(1) << 48; |
| 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed) |
| 22 | : current_(seed) {} |
| 23 | |
| 24 | uint32 LinearCongruentialGenerator::Generate() { |
| 25 | uint64 result = current_; |
| 26 | current_ = (current_ * kA + kC) % kM; |
| 27 | return static_cast<uint32>(result >> 16); |
| 28 | } |
| 29 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 30 | std::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] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame^] | 40 | "Pragma: no-cache\r\n" |
| 41 | "Cache-Control: no-cache\r\n" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 42 | "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] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 49 | "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 50 | "%s\r\n", |
| 51 | path.c_str(), |
| 52 | origin.c_str(), |
| 53 | extra_headers.c_str()); |
| 54 | } |
| 55 | |
| 56 | std::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 | |
| 66 | struct 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 | |
| 75 | WebSocketDeterministicMockClientSocketFactoryMaker:: |
| 76 | WebSocketDeterministicMockClientSocketFactoryMaker() |
| 77 | : detail_(new Detail) {} |
| 78 | |
| 79 | WebSocketDeterministicMockClientSocketFactoryMaker:: |
| 80 | ~WebSocketDeterministicMockClientSocketFactoryMaker() {} |
| 81 | |
| 82 | DeterministicMockClientSocketFactory* |
| 83 | WebSocketDeterministicMockClientSocketFactoryMaker::factory() { |
| 84 | return &detail_->factory; |
| 85 | } |
| 86 | |
| 87 | void 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] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 93 | 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] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 101 | 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 | |
| 108 | void WebSocketDeterministicMockClientSocketFactoryMaker::SetRawExpectations( |
| 109 | scoped_ptr<DeterministicSocketData> socket_data) { |
| 110 | detail_->data = socket_data.Pass(); |
| 111 | detail_->factory.AddSocketDataProvider(detail_->data.get()); |
| 112 | } |
| 113 | |
| 114 | WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost() |
| 115 | : url_request_context_(true) { |
| 116 | url_request_context_.set_client_socket_factory(maker_.factory()); |
| 117 | } |
| 118 | |
| 119 | WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {} |
| 120 | |
| 121 | void WebSocketTestURLRequestContextHost::SetRawExpectations( |
| 122 | scoped_ptr<DeterministicSocketData> socket_data) { |
| 123 | maker_.SetRawExpectations(socket_data.Pass()); |
| 124 | } |
| 125 | |
| 126 | TestURLRequestContext* |
| 127 | WebSocketTestURLRequestContextHost::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] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 134 | } // namespace net |