[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 | #ifndef NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ |
| 6 | #define NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ |
| 7 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 8 | #include <stdint.h> |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 9 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 10 | #include <memory> |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 11 | #include <string> |
Bence Béky | 46bfbc1 | 2018-02-22 19:28:20 | [diff] [blame] | 12 | #include <utility> |
Bence Béky | e1805c4 | 2018-02-08 13:02:37 | [diff] [blame] | 13 | #include <vector> |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 14 | |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 15 | #include "base/macros.h" |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 16 | #include "net/http/http_basic_state.h" |
Yutaka Hirano | 2f65eec | 2018-05-23 01:58:22 | [diff] [blame^] | 17 | #include "net/http/http_request_headers.h" |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18 | #include "net/http/http_stream_parser.h" |
| 19 | #include "net/socket/client_socket_handle.h" |
Ryan Hamilton | 2e003eea | 2018-05-02 00:24:29 | [diff] [blame] | 20 | #include "net/third_party/spdy/core/spdy_header_block.h" |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 21 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 22 | #include "net/url_request/url_request_test_util.h" |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 23 | #include "net/websockets/websocket_handshake_stream_create_helper.h" |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 24 | #include "net/websockets/websocket_stream.h" |
| 25 | |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 26 | namespace url { |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 27 | class Origin; |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 28 | } // namespace url |
| 29 | |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 30 | namespace net { |
| 31 | |
Bence Béky | 46bfbc1 | 2018-02-22 19:28:20 | [diff] [blame] | 32 | using WebSocketExtraHeaders = std::vector<std::pair<std::string, std::string>>; |
| 33 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 34 | class MockClientSocketFactory; |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 35 | class WebSocketBasicHandshakeStream; |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 36 | class ProxyResolutionService; |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 37 | class SequencedSocketData; |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 38 | struct SSLSocketDataProvider; |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 39 | |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 40 | class LinearCongruentialGenerator { |
| 41 | public: |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 42 | explicit LinearCongruentialGenerator(uint32_t seed); |
| 43 | uint32_t Generate(); |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 44 | |
| 45 | private: |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 46 | uint64_t current_; |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 47 | }; |
| 48 | |
Bence Béky | 3a0c4853 | 2018-03-02 13:38:51 | [diff] [blame] | 49 | // Converts a vector of header key-value pairs into a single string. |
| 50 | std::string WebSocketExtraHeadersToString(const WebSocketExtraHeaders& headers); |
| 51 | |
Yutaka Hirano | 2f65eec | 2018-05-23 01:58:22 | [diff] [blame^] | 52 | // Converts a vector of header key-value pairs into an HttpRequestHeaders |
| 53 | HttpRequestHeaders WebSocketExtraHeadersToHttpRequestHeaders( |
| 54 | const WebSocketExtraHeaders& headers); |
| 55 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 56 | // Generates a standard WebSocket handshake request. The challenge key used is |
| 57 | // "dGhlIHNhbXBsZSBub25jZQ==". Each header in |extra_headers| must be terminated |
| 58 | // with "\r\n". |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 59 | std::string WebSocketStandardRequest( |
| 60 | const std::string& path, |
| 61 | const std::string& host, |
| 62 | const url::Origin& origin, |
| 63 | const std::string& send_additional_request_headers, |
| 64 | const std::string& extra_headers); |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 65 | |
| 66 | // Generates a standard WebSocket handshake request. The challenge key used is |
| 67 | // "dGhlIHNhbXBsZSBub25jZQ==". |cookies| must be empty or terminated with |
| 68 | // "\r\n". Each header in |extra_headers| must be terminated with "\r\n". |
| 69 | std::string WebSocketStandardRequestWithCookies( |
| 70 | const std::string& path, |
| 71 | const std::string& host, |
mkwst | 4997ce8 | 2015-07-25 12:00:05 | [diff] [blame] | 72 | const url::Origin& origin, |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 73 | const std::string& cookies, |
allada | cef397d | 2016-06-29 17:52:23 | [diff] [blame] | 74 | const std::string& send_additional_request_headers, |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 75 | const std::string& extra_headers); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 76 | |
| 77 | // A response with the appropriate accept header to match the above challenge |
| 78 | // key. Each header in |extra_headers| must be terminated with "\r\n". |
yhirano | 01a5d66 | 2015-02-12 04:33:06 | [diff] [blame] | 79 | std::string WebSocketStandardResponse(const std::string& extra_headers); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 80 | |
Bence Béky | 46bfbc1 | 2018-02-22 19:28:20 | [diff] [blame] | 81 | // Generates a handshake request header block when using WebSockets over HTTP/2. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 82 | spdy::SpdyHeaderBlock WebSocketHttp2Request( |
Bence Béky | 46bfbc1 | 2018-02-22 19:28:20 | [diff] [blame] | 83 | const std::string& path, |
| 84 | const std::string& authority, |
| 85 | const std::string& origin, |
| 86 | const WebSocketExtraHeaders& extra_headers); |
| 87 | |
| 88 | // Generates a handshake response header block when using WebSockets over |
| 89 | // HTTP/2. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 90 | spdy::SpdyHeaderBlock WebSocketHttp2Response( |
Bence Béky | 46bfbc1 | 2018-02-22 19:28:20 | [diff] [blame] | 91 | const WebSocketExtraHeaders& extra_headers); |
| 92 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 93 | // This class provides a convenient way to construct a MockClientSocketFactory |
| 94 | // for WebSocket tests. |
| 95 | class WebSocketMockClientSocketFactoryMaker { |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 96 | public: |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 97 | WebSocketMockClientSocketFactoryMaker(); |
| 98 | ~WebSocketMockClientSocketFactoryMaker(); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 99 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 100 | // Tell the factory to create a socket which expects |expect_written| to be |
| 101 | // written, and responds with |return_to_read|. The test will fail if the |
| 102 | // expected text is not written, or all the bytes are not read. This adds data |
| 103 | // for a new mock-socket using AddRawExpections(), and so can be called |
| 104 | // multiple times to queue up multiple mock sockets, but usually in those |
| 105 | // cases the lower-level AddRawExpections() interface is more appropriate. |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 106 | void SetExpectations(const std::string& expect_written, |
| 107 | const std::string& return_to_read); |
| 108 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 109 | // A low-level interface to permit arbitrary expectations to be added. The |
| 110 | // mock sockets will be created in the same order that they were added. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 111 | void AddRawExpectations(std::unique_ptr<SequencedSocketData> socket_data); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 112 | |
| 113 | // Allow an SSL socket data provider to be added. You must also supply a mock |
| 114 | // transport socket for it to use. If the mock SSL handshake fails then the |
| 115 | // mock transport socket will connect but have nothing read or written. If the |
| 116 | // mock handshake succeeds then the data from the underlying transport socket |
| 117 | // will be passed through unchanged (without encryption). |
| 118 | void AddSSLSocketDataProvider( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 119 | std::unique_ptr<SSLSocketDataProvider> ssl_socket_data); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 120 | |
| 121 | // Call to get a pointer to the factory, which remains owned by this object. |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 122 | MockClientSocketFactory* factory(); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 123 | |
| 124 | private: |
| 125 | struct Detail; |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 126 | std::unique_ptr<Detail> detail_; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 127 | |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 128 | DISALLOW_COPY_AND_ASSIGN(WebSocketMockClientSocketFactoryMaker); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | // This class encapsulates the details of creating a |
| 132 | // TestURLRequestContext that returns mock ClientSocketHandles that do what is |
| 133 | // required by the tests. |
| 134 | struct WebSocketTestURLRequestContextHost { |
| 135 | public: |
| 136 | WebSocketTestURLRequestContextHost(); |
| 137 | ~WebSocketTestURLRequestContextHost(); |
| 138 | |
| 139 | void SetExpectations(const std::string& expect_written, |
| 140 | const std::string& return_to_read) { |
| 141 | maker_.SetExpectations(expect_written, return_to_read); |
| 142 | } |
| 143 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 144 | void AddRawExpectations(std::unique_ptr<SequencedSocketData> socket_data); |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 145 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 146 | // Allow an SSL socket data provider to be added. |
| 147 | void AddSSLSocketDataProvider( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 148 | std::unique_ptr<SSLSocketDataProvider> ssl_socket_data); |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 149 | |
ricea | 38fc268c | 2015-02-09 02:41:29 | [diff] [blame] | 150 | // Allow a proxy to be set. Usage: |
| 151 | // SetProxyConfig("proxy1:8000"); |
| 152 | // Any syntax accepted by net::ProxyConfig::ParseFromString() will work. |
| 153 | // Do not call after GetURLRequestContext() has been called. |
| 154 | void SetProxyConfig(const std::string& proxy_rules); |
| 155 | |
[email protected] | a6244952 | 2014-06-05 11:11:15 | [diff] [blame] | 156 | // Call after calling one of SetExpections() or AddRawExpectations(). The |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 157 | // returned pointer remains owned by this object. |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 158 | TestURLRequestContext* GetURLRequestContext(); |
| 159 | |
yhirano | 96205c4 | 2017-01-18 22:45:18 | [diff] [blame] | 160 | const TestNetworkDelegate& network_delegate() const { |
| 161 | return network_delegate_; |
| 162 | } |
| 163 | |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 164 | private: |
mmenke | d0d201a | 2015-06-08 12:00:12 | [diff] [blame] | 165 | WebSocketMockClientSocketFactoryMaker maker_; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 166 | TestURLRequestContext url_request_context_; |
| 167 | TestNetworkDelegate network_delegate_; |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 168 | std::unique_ptr<ProxyResolutionService> proxy_resolution_service_; |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 169 | bool url_request_context_initialized_; |
[email protected] | a31ecc0 | 2013-12-05 08:30:55 | [diff] [blame] | 170 | |
| 171 | DISALLOW_COPY_AND_ASSIGN(WebSocketTestURLRequestContextHost); |
| 172 | }; |
| 173 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 174 | // WebSocketStream::ConnectDelegate implementation that does nothing. |
| 175 | class DummyConnectDelegate : public WebSocketStream::ConnectDelegate { |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 176 | public: |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 177 | DummyConnectDelegate() = default; |
| 178 | ~DummyConnectDelegate() override = default; |
| 179 | void OnCreateRequest(URLRequest* url_request) override {} |
| 180 | void OnSuccess(std::unique_ptr<WebSocketStream> stream) override {} |
| 181 | void OnFailure(const std::string& message) override {} |
| 182 | void OnStartOpeningHandshake( |
| 183 | std::unique_ptr<WebSocketHandshakeRequestInfo> request) override {} |
| 184 | void OnFinishOpeningHandshake( |
| 185 | std::unique_ptr<WebSocketHandshakeResponseInfo> response) override {} |
| 186 | void OnSSLCertificateError( |
| 187 | std::unique_ptr<WebSocketEventInterface::SSLErrorCallbacks> |
| 188 | ssl_error_callbacks, |
| 189 | const SSLInfo& ssl_info, |
| 190 | bool fatal) override {} |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 191 | }; |
| 192 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 193 | // WebSocketStreamRequest implementation that does nothing. |
| 194 | class DummyWebSocketStreamRequest : public WebSocketStreamRequest { |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 195 | public: |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 196 | DummyWebSocketStreamRequest() = default; |
| 197 | ~DummyWebSocketStreamRequest() override = default; |
| 198 | void OnHandshakeStreamCreated( |
| 199 | WebSocketHandshakeStreamBase* handshake_stream) override {} |
| 200 | void OnFailure(const std::string& message) override {} |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 201 | }; |
| 202 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 203 | // A sub-class of WebSocketHandshakeStreamCreateHelper which sets a |
| 204 | // deterministic key to use in the WebSocket handshake, and optionally uses a |
| 205 | // dummy ConnectDelegate and a dummy WebSocketStreamRequest. |
| 206 | class TestWebSocketHandshakeStreamCreateHelper |
| 207 | : public WebSocketHandshakeStreamCreateHelper { |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 208 | public: |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 209 | // Constructor for using dummy ConnectDelegate and WebSocketStreamRequest. |
| 210 | TestWebSocketHandshakeStreamCreateHelper() |
| 211 | : WebSocketHandshakeStreamCreateHelper( |
| 212 | &connect_delegate_, |
| 213 | std::vector<std::string>() /* requested_subprotocols */) { |
| 214 | WebSocketHandshakeStreamCreateHelper::set_stream_request(&request_); |
| 215 | } |
| 216 | |
| 217 | // Constructor for using custom ConnectDelegate and subprotocols. |
| 218 | // Caller must call set_stream_request() with a WebSocketStreamRequest before |
| 219 | // calling CreateBasicStream(). |
| 220 | TestWebSocketHandshakeStreamCreateHelper( |
| 221 | WebSocketStream::ConnectDelegate* connect_delegate, |
| 222 | const std::vector<std::string>& requested_subprotocols) |
| 223 | : WebSocketHandshakeStreamCreateHelper(connect_delegate, |
| 224 | requested_subprotocols) {} |
| 225 | |
| 226 | ~TestWebSocketHandshakeStreamCreateHelper() override = default; |
| 227 | |
| 228 | void OnBasicStreamCreated(WebSocketBasicHandshakeStream* stream) override; |
| 229 | |
| 230 | private: |
| 231 | DummyConnectDelegate connect_delegate_; |
| 232 | DummyWebSocketStreamRequest request_; |
| 233 | |
| 234 | DISALLOW_COPY_AND_ASSIGN(TestWebSocketHandshakeStreamCreateHelper); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 235 | }; |
| 236 | |
[email protected] | ce9f7ffd | 2013-10-11 06:04:11 | [diff] [blame] | 237 | } // namespace net |
| 238 | |
| 239 | #endif // NET_WEBSOCKETS_WEBSOCKET_TEST_UTIL_H_ |