[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 | |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <vector> |
| 10 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 11 | #include "base/strings/stringprintf.h" |
ricea | 38fc268c | 2015-02-09 02:41:29 | [diff] [blame] | 12 | #include "net/proxy/proxy_service.h" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 13 | #include "net/socket/socket_test_util.h" |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 14 | #include "url/origin.h" |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | namespace { |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 19 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 20 | const uint64_t kA = (static_cast<uint64_t>(0x5851f42d) << 32) + |
| 21 | static_cast<uint64_t>(0x4c957f2d); |
| 22 | const uint64_t kC = 12345; |
| 23 | const uint64_t kM = static_cast<uint64_t>(1) << 48; |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 24 | |
| 25 | } // namespace |
| 26 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 27 | LinearCongruentialGenerator::LinearCongruentialGenerator(uint32_t seed) |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 28 | : current_(seed) {} |
| 29 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 30 | uint32_t LinearCongruentialGenerator::Generate() { |
| 31 | uint64_t result = current_; |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 32 | current_ = (current_ * kA + kC) % kM; |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 33 | return static_cast<uint32_t>(result >> 16); |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 34 | } |
| 35 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 36 | std::string WebSocketStandardRequest(const std::string& path, |
yhirano | a505d29 | 2015-01-22 07:34:29 | [diff] [blame] | 37 | const std::string& host, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 38 | const url::Origin& origin, |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 39 | const std::string& extra_headers) { |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 40 | return WebSocketStandardRequestWithCookies(path, host, origin, std::string(), |
| 41 | extra_headers); |
| 42 | } |
| 43 | |
| 44 | std::string WebSocketStandardRequestWithCookies( |
| 45 | const std::string& path, |
| 46 | const std::string& host, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 47 | const url::Origin& origin, |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 48 | const std::string& cookies, |
| 49 | const std::string& extra_headers) { |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 50 | // Unrelated changes in net/http may change the order and default-values of |
| 51 | // HTTP headers, causing WebSocket tests to fail. It is safe to update this |
| 52 | // string in that case. |
| 53 | return base::StringPrintf( |
| 54 | "GET %s HTTP/1.1\r\n" |
yhirano | a505d29 | 2015-01-22 07:34:29 | [diff] [blame] | 55 | "Host: %s\r\n" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 56 | "Connection: Upgrade\r\n" |
[email protected] | e5760f52 | 2014-02-05 12:28:50 | [diff] [blame] | 57 | "Pragma: no-cache\r\n" |
| 58 | "Cache-Control: no-cache\r\n" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 59 | "Upgrade: websocket\r\n" |
| 60 | "Origin: %s\r\n" |
| 61 | "Sec-WebSocket-Version: 13\r\n" |
| 62 | "User-Agent:\r\n" |
jgraettinger | 8f00db8b | 2014-09-24 23:00:06 | [diff] [blame] | 63 | "Accept-Encoding: gzip, deflate\r\n" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 64 | "Accept-Language: en-us,fr\r\n" |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 65 | "%s" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 66 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 67 | "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 68 | "%s\r\n", |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 69 | path.c_str(), host.c_str(), origin.Serialize().c_str(), cookies.c_str(), |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 70 | extra_headers.c_str()); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | std::string WebSocketStandardResponse(const std::string& extra_headers) { |
| 74 | return base::StringPrintf( |
| 75 | "HTTP/1.1 101 Switching Protocols\r\n" |
| 76 | "Upgrade: websocket\r\n" |
| 77 | "Connection: Upgrade\r\n" |
| 78 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n" |
| 79 | "%s\r\n", |
| 80 | extra_headers.c_str()); |
| 81 | } |
| 82 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 83 | struct WebSocketMockClientSocketFactoryMaker::Detail { |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 84 | std::string expect_written; |
| 85 | std::string return_to_read; |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 86 | std::vector<MockRead> reads; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 87 | MockWrite write; |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 88 | std::vector<scoped_ptr<SequencedSocketData>> socket_data_vector; |
| 89 | std::vector<scoped_ptr<SSLSocketDataProvider>> ssl_socket_data_vector; |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 90 | MockClientSocketFactory factory; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 91 | }; |
| 92 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 93 | WebSocketMockClientSocketFactoryMaker::WebSocketMockClientSocketFactoryMaker() |
| 94 | : detail_(new Detail) { |
| 95 | } |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 96 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 97 | WebSocketMockClientSocketFactoryMaker:: |
| 98 | ~WebSocketMockClientSocketFactoryMaker() { |
| 99 | } |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 100 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 101 | MockClientSocketFactory* WebSocketMockClientSocketFactoryMaker::factory() { |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 102 | return &detail_->factory; |
| 103 | } |
| 104 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 105 | void WebSocketMockClientSocketFactoryMaker::SetExpectations( |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 106 | const std::string& expect_written, |
| 107 | const std::string& return_to_read) { |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 108 | const size_t kHttpStreamParserBufferSize = 4096; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 109 | // We need to extend the lifetime of these strings. |
| 110 | detail_->expect_written = expect_written; |
| 111 | detail_->return_to_read = return_to_read; |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 112 | int sequence = 0; |
[email protected] | 0be9392 | 2014-01-29 00:42:45 | [diff] [blame] | 113 | detail_->write = MockWrite(SYNCHRONOUS, |
| 114 | detail_->expect_written.data(), |
| 115 | detail_->expect_written.size(), |
[email protected] | 9483152 | 2014-02-06 12:05:18 | [diff] [blame] | 116 | sequence++); |
| 117 | // HttpStreamParser reads 4KB at a time. We need to take this implementation |
| 118 | // detail into account if |return_to_read| is big enough. |
| 119 | for (size_t place = 0; place < detail_->return_to_read.size(); |
| 120 | place += kHttpStreamParserBufferSize) { |
| 121 | detail_->reads.push_back( |
| 122 | MockRead(SYNCHRONOUS, detail_->return_to_read.data() + place, |
| 123 | std::min(detail_->return_to_read.size() - place, |
| 124 | kHttpStreamParserBufferSize), |
| 125 | sequence++)); |
| 126 | } |
davidben | 5f8b6bc | 2015-11-25 03:19:54 | [diff] [blame] | 127 | scoped_ptr<SequencedSocketData> socket_data(new SequencedSocketData( |
| 128 | detail_->reads.data(), detail_->reads.size(), &detail_->write, 1)); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 129 | socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 130 | AddRawExpectations(socket_data.Pass()); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 131 | } |
| 132 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 133 | void WebSocketMockClientSocketFactoryMaker::AddRawExpectations( |
| 134 | scoped_ptr<SequencedSocketData> socket_data) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 135 | detail_->factory.AddSocketDataProvider(socket_data.get()); |
hari.singh1 | b87973c | 2015-06-01 13:34:07 | [diff] [blame] | 136 | detail_->socket_data_vector.push_back(socket_data.Pass()); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 137 | } |
| 138 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 139 | void WebSocketMockClientSocketFactoryMaker::AddSSLSocketDataProvider( |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 140 | scoped_ptr<SSLSocketDataProvider> ssl_socket_data) { |
| 141 | detail_->factory.AddSSLSocketDataProvider(ssl_socket_data.get()); |
hari.singh1 | b87973c | 2015-06-01 13:34:07 | [diff] [blame] | 142 | detail_->ssl_socket_data_vector.push_back(ssl_socket_data.Pass()); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | WebSocketTestURLRequestContextHost::WebSocketTestURLRequestContextHost() |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 146 | : url_request_context_(true), url_request_context_initialized_(false) { |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 147 | url_request_context_.set_client_socket_factory(maker_.factory()); |
| 148 | } |
| 149 | |
| 150 | WebSocketTestURLRequestContextHost::~WebSocketTestURLRequestContextHost() {} |
| 151 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 152 | void WebSocketTestURLRequestContextHost::AddRawExpectations( |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 153 | scoped_ptr<SequencedSocketData> socket_data) { |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 154 | maker_.AddRawExpectations(socket_data.Pass()); |
| 155 | } |
| 156 | |
| 157 | void WebSocketTestURLRequestContextHost::AddSSLSocketDataProvider( |
| 158 | scoped_ptr<SSLSocketDataProvider> ssl_socket_data) { |
| 159 | maker_.AddSSLSocketDataProvider(ssl_socket_data.Pass()); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 160 | } |
| 161 | |
ricea | 38fc268c | 2015-02-09 02:41:29 | [diff] [blame] | 162 | void WebSocketTestURLRequestContextHost::SetProxyConfig( |
| 163 | const std::string& proxy_rules) { |
| 164 | DCHECK(!url_request_context_initialized_); |
rdsmith | 82957ad | 2015-09-16 19:42:03 | [diff] [blame] | 165 | proxy_service_ = ProxyService::CreateFixed(proxy_rules); |
ricea | 38fc268c | 2015-02-09 02:41:29 | [diff] [blame] | 166 | url_request_context_.set_proxy_service(proxy_service_.get()); |
| 167 | } |
| 168 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 169 | TestURLRequestContext* |
| 170 | WebSocketTestURLRequestContextHost::GetURLRequestContext() { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 171 | if (!url_request_context_initialized_) { |
| 172 | url_request_context_.Init(); |
| 173 | // A Network Delegate is required to make the URLRequest::Delegate work. |
| 174 | url_request_context_.set_network_delegate(&network_delegate_); |
| 175 | url_request_context_initialized_ = true; |
| 176 | } |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 177 | return &url_request_context_; |
| 178 | } |
| 179 | |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 180 | } // namespace net |