[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 | |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <vector> |
| 9 | |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame^] | 11 | #include "base/memory/scoped_vector.h" |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 12 | #include "base/stl_util.h" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
| 14 | #include "net/socket/socket_test_util.h" |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | namespace { |
| 19 | const uint64 kA = |
| 20 | (static_cast<uint64>(0x5851f42d) << 32) + static_cast<uint64>(0x4c957f2d); |
| 21 | const uint64 kC = 12345; |
| 22 | const uint64 kM = static_cast<uint64>(1) << 48; |
| 23 | |
| 24 | } // namespace |
| 25 | |
| 26 | LinearCongruentialGenerator::LinearCongruentialGenerator(uint32 seed) |
| 27 | : current_(seed) {} |
| 28 | |
| 29 | uint32 LinearCongruentialGenerator::Generate() { |
| 30 | uint64 result = current_; |
| 31 | current_ = (current_ * kA + kC) % kM; |
| 32 | return static_cast<uint32>(result >> 16); |
| 33 | } |
| 34 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 35 | std::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] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 45 | "Pragma: no-cache\r\n" |
| 46 | "Cache-Control: no-cache\r\n" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 47 | "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] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 54 | "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 55 | "%s\r\n", |
| 56 | path.c_str(), |
| 57 | origin.c_str(), |
| 58 | extra_headers.c_str()); |
| 59 | } |
| 60 | |
| 61 | std::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 | |
| 71 | struct WebSocketDeterministicMockClientSocketFactoryMaker::Detail { |
| 72 | std::string expect_written; |
| 73 | std::string return_to_read; |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 74 | std::vector<MockRead> reads; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 75 | MockWrite write; |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame^] | 76 | ScopedVector<DeterministicSocketData> socket_data_vector; |
| 77 | ScopedVector<SSLSocketDataProvider> ssl_socket_data_vector; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 78 | DeterministicMockClientSocketFactory factory; |
| 79 | }; |
| 80 | |
| 81 | WebSocketDeterministicMockClientSocketFactoryMaker:: |
| 82 | WebSocketDeterministicMockClientSocketFactoryMaker() |
| 83 | : detail_(new Detail) {} |
| 84 | |
| 85 | WebSocketDeterministicMockClientSocketFactoryMaker:: |
| 86 | ~WebSocketDeterministicMockClientSocketFactoryMaker() {} |
| 87 | |
| 88 | DeterministicMockClientSocketFactory* |
| 89 | WebSocketDeterministicMockClientSocketFactoryMaker::factory() { |
| 90 | return &detail_->factory; |
| 91 | } |
| 92 | |
| 93 | void WebSocketDeterministicMockClientSocketFactoryMaker::SetExpectations( |
| 94 | const std::string& expect_written, |
| 95 | const std::string& return_to_read) { |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 96 | const size_t kHttpStreamParserBufferSize = 4096; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 97 | // 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] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 100 | int sequence = 0; |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 101 | detail_->write = MockWrite(SYNCHRONOUS, |
| 102 | detail_->expect_written.data(), |
| 103 | detail_->expect_written.size(), |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 104 | 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] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 115 | scoped_ptr<DeterministicSocketData> socket_data( |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 116 | new DeterministicSocketData(vector_as_array(&detail_->reads), |
| 117 | detail_->reads.size(), |
| 118 | &detail_->write, |
| 119 | 1)); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 120 | socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 121 | socket_data->SetStop(sequence); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame^] | 122 | AddRawExpectations(socket_data.Pass()); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame^] | 125 | void WebSocketDeterministicMockClientSocketFactoryMaker::AddRawExpectations( |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 126 | scoped_ptr<DeterministicSocketData> socket_data) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame^] | 127 | detail_->factory.AddSocketDataProvider(socket_data.get()); |
| 128 | detail_->socket_data_vector.push_back(socket_data.release()); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | WebSocketDeterministicMockClientSocketFactoryMaker::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] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost() |
| 139 | : url_request_context_(true) { |
| 140 | url_request_context_.set_client_socket_factory(maker_.factory()); |
| 141 | } |
| 142 | |
| 143 | WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {} |
| 144 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame^] | 145 | void WebSocketTestURLRequestContextHost::AddRawExpectations( |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 146 | scoped_ptr<DeterministicSocketData> socket_data) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame^] | 147 | maker_.AddRawExpectations(socket_data.Pass()); |
| 148 | } |
| 149 | |
| 150 | void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider( |
| 151 | scoped_ptr<SSLSocketDataProvider> ssl_socket_data) { |
| 152 | maker_.AddSSLSocketDataProvider(ssl_socket_data.Pass()); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | TestURLRequestContext* |
| 156 | WebSocketTestURLRequestContextHost::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] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 163 | } // namespace net |