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