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