OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/websockets/websocket_stream.h" | 5 #include "net/websockets/websocket_stream.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
25 #include "net/socket/client_socket_handle.h" | 25 #include "net/socket/client_socket_handle.h" |
26 #include "net/socket/socket_test_util.h" | 26 #include "net/socket/socket_test_util.h" |
27 #include "net/test/cert_test_util.h" | 27 #include "net/test/cert_test_util.h" |
28 #include "net/url_request/url_request_test_util.h" | 28 #include "net/url_request/url_request_test_util.h" |
29 #include "net/websockets/websocket_basic_handshake_stream.h" | 29 #include "net/websockets/websocket_basic_handshake_stream.h" |
30 #include "net/websockets/websocket_frame.h" | 30 #include "net/websockets/websocket_frame.h" |
31 #include "net/websockets/websocket_handshake_request_info.h" | 31 #include "net/websockets/websocket_handshake_request_info.h" |
32 #include "net/websockets/websocket_handshake_response_info.h" | 32 #include "net/websockets/websocket_handshake_response_info.h" |
33 #include "net/websockets/websocket_handshake_stream_create_helper.h" | 33 #include "net/websockets/websocket_handshake_stream_create_helper.h" |
| 34 #include "net/websockets/websocket_stream_test_util.h" |
34 #include "net/websockets/websocket_test_util.h" | 35 #include "net/websockets/websocket_test_util.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
36 #include "url/gurl.h" | 37 #include "url/gurl.h" |
37 #include "url/origin.h" | 38 #include "url/origin.h" |
38 | 39 |
39 namespace net { | 40 namespace net { |
40 namespace { | 41 namespace { |
41 | 42 |
42 typedef std::pair<std::string, std::string> HeaderKeyValuePair; | |
43 | |
44 std::vector<HeaderKeyValuePair> ToVector(const HttpRequestHeaders& headers) { | |
45 HttpRequestHeaders::Iterator it(headers); | |
46 std::vector<HeaderKeyValuePair> result; | |
47 while (it.GetNext()) | |
48 result.push_back(HeaderKeyValuePair(it.name(), it.value())); | |
49 return result; | |
50 } | |
51 | |
52 std::vector<HeaderKeyValuePair> ToVector(const HttpResponseHeaders& headers) { | |
53 void* iter = NULL; | |
54 std::string name, value; | |
55 std::vector<HeaderKeyValuePair> result; | |
56 while (headers.EnumerateHeaderLines(&iter, &name, &value)) | |
57 result.push_back(HeaderKeyValuePair(name, value)); | |
58 return result; | |
59 } | |
60 | |
61 // Simple builder for a DeterministicSocketData object to save repetitive code. | 43 // Simple builder for a DeterministicSocketData object to save repetitive code. |
62 // It always sets the connect data to MockConnect(SYNCHRONOUS, OK), so it cannot | 44 // It always sets the connect data to MockConnect(SYNCHRONOUS, OK), so it cannot |
63 // be used in tests where the connect fails. In practice, those tests never have | 45 // be used in tests where the connect fails. In practice, those tests never have |
64 // any read/write data and so can't benefit from it anyway. The arrays are not | 46 // any read/write data and so can't benefit from it anyway. The arrays are not |
65 // copied. It is up to the caller to ensure they stay in scope until the test | 47 // copied. It is up to the caller to ensure they stay in scope until the test |
66 // ends. | 48 // ends. |
67 template <size_t reads_count, size_t writes_count> | 49 template <size_t reads_count, size_t writes_count> |
68 scoped_ptr<DeterministicSocketData> BuildSocketData( | 50 scoped_ptr<DeterministicSocketData> BuildSocketData( |
69 MockRead (&reads)[reads_count], | 51 MockRead (&reads)[reads_count], |
70 MockWrite (&writes)[writes_count]) { | 52 MockWrite (&writes)[writes_count]) { |
(...skipping 10 matching lines...) Expand all Loading... |
81 return make_scoped_ptr(new DeterministicSocketData(NULL, 0, NULL, 0)); | 63 return make_scoped_ptr(new DeterministicSocketData(NULL, 0, NULL, 0)); |
82 } | 64 } |
83 | 65 |
84 class MockWeakTimer : public base::MockTimer, | 66 class MockWeakTimer : public base::MockTimer, |
85 public base::SupportsWeakPtr<MockWeakTimer> { | 67 public base::SupportsWeakPtr<MockWeakTimer> { |
86 public: | 68 public: |
87 MockWeakTimer(bool retain_user_task, bool is_repeating) | 69 MockWeakTimer(bool retain_user_task, bool is_repeating) |
88 : MockTimer(retain_user_task, is_repeating) {} | 70 : MockTimer(retain_user_task, is_repeating) {} |
89 }; | 71 }; |
90 | 72 |
91 // A sub-class of WebSocketHandshakeStreamCreateHelper which always sets a | 73 class WebSocketStreamCreateTest : public ::testing::Test, |
92 // deterministic key to use in the WebSocket handshake. | 74 public WebSocketStreamCreateTestBase { |
93 class DeterministicKeyWebSocketHandshakeStreamCreateHelper | |
94 : public WebSocketHandshakeStreamCreateHelper { | |
95 public: | 75 public: |
96 DeterministicKeyWebSocketHandshakeStreamCreateHelper( | |
97 WebSocketStream::ConnectDelegate* connect_delegate, | |
98 const std::vector<std::string>& requested_subprotocols) | |
99 : WebSocketHandshakeStreamCreateHelper(connect_delegate, | |
100 requested_subprotocols) {} | |
101 | |
102 void OnStreamCreated(WebSocketBasicHandshakeStream* stream) override { | |
103 stream->SetWebSocketKeyForTesting("dGhlIHNhbXBsZSBub25jZQ=="); | |
104 } | |
105 }; | |
106 | |
107 class WebSocketStreamCreateTest : public ::testing::Test { | |
108 public: | |
109 WebSocketStreamCreateTest() : has_failed_(false), ssl_fatal_(false) {} | |
110 ~WebSocketStreamCreateTest() override { | 76 ~WebSocketStreamCreateTest() override { |
111 // Permit any endpoint locks to be released. | 77 // Permit any endpoint locks to be released. |
112 stream_request_.reset(); | 78 stream_request_.reset(); |
113 stream_.reset(); | 79 stream_.reset(); |
114 RunUntilIdle(); | 80 RunUntilIdle(); |
115 } | 81 } |
116 | 82 |
117 void CreateAndConnectCustomResponse( | 83 void CreateAndConnectCustomResponse( |
118 const std::string& socket_url, | 84 const std::string& socket_url, |
119 const std::string& socket_host, | 85 const std::string& socket_host, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 scoped_ptr<DeterministicSocketData> socket_data, | 120 scoped_ptr<DeterministicSocketData> socket_data, |
155 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { | 121 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { |
156 AddRawExpectations(socket_data.Pass()); | 122 AddRawExpectations(socket_data.Pass()); |
157 CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); | 123 CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); |
158 } | 124 } |
159 | 125 |
160 // Add additional raw expectations for sockets created before the final one. | 126 // Add additional raw expectations for sockets created before the final one. |
161 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data) { | 127 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data) { |
162 url_request_context_host_.AddRawExpectations(socket_data.Pass()); | 128 url_request_context_host_.AddRawExpectations(socket_data.Pass()); |
163 } | 129 } |
164 | |
165 // A wrapper for CreateAndConnectStreamForTesting that knows about our default | |
166 // parameters. | |
167 void CreateAndConnectStream(const std::string& socket_url, | |
168 const std::vector<std::string>& sub_protocols, | |
169 const std::string& origin, | |
170 scoped_ptr<base::Timer> timer) { | |
171 for (size_t i = 0; i < ssl_data_.size(); ++i) { | |
172 scoped_ptr<SSLSocketDataProvider> ssl_data(ssl_data_[i]); | |
173 ssl_data_[i] = NULL; | |
174 url_request_context_host_.AddSSLSocketDataProvider(ssl_data.Pass()); | |
175 } | |
176 ssl_data_.clear(); | |
177 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate( | |
178 new TestConnectDelegate(this)); | |
179 WebSocketStream::ConnectDelegate* delegate = connect_delegate.get(); | |
180 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper( | |
181 new DeterministicKeyWebSocketHandshakeStreamCreateHelper( | |
182 delegate, sub_protocols)); | |
183 stream_request_ = ::net::CreateAndConnectStreamForTesting( | |
184 GURL(socket_url), | |
185 create_helper.Pass(), | |
186 url::Origin(origin), | |
187 url_request_context_host_.GetURLRequestContext(), | |
188 BoundNetLog(), | |
189 connect_delegate.Pass(), | |
190 timer ? timer.Pass() : scoped_ptr<base::Timer>( | |
191 new base::Timer(false, false))); | |
192 } | |
193 | |
194 static void RunUntilIdle() { base::RunLoop().RunUntilIdle(); } | |
195 | |
196 // A simple function to make the tests more readable. Creates an empty vector. | |
197 static std::vector<std::string> NoSubProtocols() { | |
198 return std::vector<std::string>(); | |
199 } | |
200 | |
201 const std::string& failure_message() const { return failure_message_; } | |
202 bool has_failed() const { return has_failed_; } | |
203 | |
204 class TestConnectDelegate : public WebSocketStream::ConnectDelegate { | |
205 public: | |
206 explicit TestConnectDelegate(WebSocketStreamCreateTest* owner) | |
207 : owner_(owner) {} | |
208 | |
209 void OnSuccess(scoped_ptr<WebSocketStream> stream) override { | |
210 stream.swap(owner_->stream_); | |
211 } | |
212 | |
213 void OnFailure(const std::string& message) override { | |
214 owner_->has_failed_ = true; | |
215 owner_->failure_message_ = message; | |
216 } | |
217 | |
218 void OnStartOpeningHandshake( | |
219 scoped_ptr<WebSocketHandshakeRequestInfo> request) override { | |
220 // Can be called multiple times (in the case of HTTP auth). Last call | |
221 // wins. | |
222 owner_->request_info_ = request.Pass(); | |
223 } | |
224 void OnFinishOpeningHandshake( | |
225 scoped_ptr<WebSocketHandshakeResponseInfo> response) override { | |
226 if (owner_->response_info_) | |
227 ADD_FAILURE(); | |
228 owner_->response_info_ = response.Pass(); | |
229 } | |
230 void OnSSLCertificateError( | |
231 scoped_ptr<WebSocketEventInterface::SSLErrorCallbacks> | |
232 ssl_error_callbacks, | |
233 const SSLInfo& ssl_info, | |
234 bool fatal) override { | |
235 owner_->ssl_error_callbacks_ = ssl_error_callbacks.Pass(); | |
236 owner_->ssl_info_ = ssl_info; | |
237 owner_->ssl_fatal_ = fatal; | |
238 } | |
239 | |
240 private: | |
241 WebSocketStreamCreateTest* owner_; | |
242 }; | |
243 | |
244 WebSocketTestURLRequestContextHost url_request_context_host_; | |
245 scoped_ptr<WebSocketStreamRequest> stream_request_; | |
246 // Only set if the connection succeeded. | |
247 scoped_ptr<WebSocketStream> stream_; | |
248 // Only set if the connection failed. | |
249 std::string failure_message_; | |
250 bool has_failed_; | |
251 scoped_ptr<WebSocketHandshakeRequestInfo> request_info_; | |
252 scoped_ptr<WebSocketHandshakeResponseInfo> response_info_; | |
253 scoped_ptr<WebSocketEventInterface::SSLErrorCallbacks> ssl_error_callbacks_; | |
254 SSLInfo ssl_info_; | |
255 bool ssl_fatal_; | |
256 ScopedVector<SSLSocketDataProvider> ssl_data_; | |
257 ScopedWebSocketEndpointZeroUnlockDelay zero_unlock_delay_; | |
258 }; | 130 }; |
259 | 131 |
260 // There are enough tests of the Sec-WebSocket-Extensions header that they | 132 // There are enough tests of the Sec-WebSocket-Extensions header that they |
261 // deserve their own test fixture. | 133 // deserve their own test fixture. |
262 class WebSocketStreamCreateExtensionTest : public WebSocketStreamCreateTest { | 134 class WebSocketStreamCreateExtensionTest : public WebSocketStreamCreateTest { |
263 public: | 135 public: |
264 // Performs a standard connect, with the value of the Sec-WebSocket-Extensions | 136 // Performs a standard connect, with the value of the Sec-WebSocket-Extensions |
265 // header in the response set to |extensions_header_value|. Runs the event | 137 // header in the response set to |extensions_header_value|. Runs the event |
266 // loop to allow the connect to complete. | 138 // loop to allow the connect to complete. |
267 void CreateAndConnectWithExtensions( | 139 void CreateAndConnectWithExtensions( |
(...skipping 1115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1383 if (original) { | 1255 if (original) { |
1384 samples->Subtract(*original); // Cancel the original values. | 1256 samples->Subtract(*original); // Cancel the original values. |
1385 } | 1257 } |
1386 EXPECT_EQ(1, samples->GetCount(INCOMPLETE)); | 1258 EXPECT_EQ(1, samples->GetCount(INCOMPLETE)); |
1387 EXPECT_EQ(0, samples->GetCount(CONNECTED)); | 1259 EXPECT_EQ(0, samples->GetCount(CONNECTED)); |
1388 EXPECT_EQ(0, samples->GetCount(FAILED)); | 1260 EXPECT_EQ(0, samples->GetCount(FAILED)); |
1389 } | 1261 } |
1390 | 1262 |
1391 } // namespace | 1263 } // namespace |
1392 } // namespace net | 1264 } // namespace net |
OLD | NEW |