| 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> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/metrics/histogram_samples.h" | 15 #include "base/metrics/histogram_samples.h" |
| 16 #include "base/metrics/statistics_recorder.h" | 16 #include "base/metrics/statistics_recorder.h" |
| 17 #include "base/run_loop.h" | |
| 18 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 19 #include "base/timer/mock_timer.h" | 18 #include "base/timer/mock_timer.h" |
| 20 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
| 21 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 22 #include "net/base/test_data_directory.h" | 21 #include "net/base/test_data_directory.h" |
| 23 #include "net/http/http_request_headers.h" | 22 #include "net/http/http_request_headers.h" |
| 24 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
| 25 #include "net/socket/client_socket_handle.h" | 24 #include "net/socket/client_socket_handle.h" |
| 26 #include "net/socket/socket_test_util.h" | 25 #include "net/socket/socket_test_util.h" |
| 27 #include "net/test/cert_test_util.h" | 26 #include "net/test/cert_test_util.h" |
| 28 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
| 29 #include "net/websockets/websocket_basic_handshake_stream.h" | 28 #include "net/websockets/websocket_basic_handshake_stream.h" |
| 30 #include "net/websockets/websocket_frame.h" | 29 #include "net/websockets/websocket_frame.h" |
| 31 #include "net/websockets/websocket_handshake_request_info.h" | 30 #include "net/websockets/websocket_stream_create_test_base.h" |
| 32 #include "net/websockets/websocket_handshake_response_info.h" | |
| 33 #include "net/websockets/websocket_handshake_stream_create_helper.h" | |
| 34 #include "net/websockets/websocket_test_util.h" | 31 #include "net/websockets/websocket_test_util.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 36 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 37 #include "url/origin.h" | 34 #include "url/origin.h" |
| 38 | 35 |
| 39 namespace net { | 36 namespace net { |
| 40 namespace { | 37 namespace { |
| 41 | 38 |
| 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. | 39 // Simple builder for a DeterministicSocketData object to save repetitive code. |
| 62 // It always sets the connect data to MockConnect(SYNCHRONOUS, OK), so it cannot | 40 // 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 | 41 // 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 | 42 // 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 | 43 // copied. It is up to the caller to ensure they stay in scope until the test |
| 66 // ends. | 44 // ends. |
| 67 template <size_t reads_count, size_t writes_count> | 45 template <size_t reads_count, size_t writes_count> |
| 68 scoped_ptr<DeterministicSocketData> BuildSocketData( | 46 scoped_ptr<DeterministicSocketData> BuildSocketData( |
| 69 MockRead (&reads)[reads_count], | 47 MockRead (&reads)[reads_count], |
| 70 MockWrite (&writes)[writes_count]) { | 48 MockWrite (&writes)[writes_count]) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 81 return make_scoped_ptr(new DeterministicSocketData(NULL, 0, NULL, 0)); | 59 return make_scoped_ptr(new DeterministicSocketData(NULL, 0, NULL, 0)); |
| 82 } | 60 } |
| 83 | 61 |
| 84 class MockWeakTimer : public base::MockTimer, | 62 class MockWeakTimer : public base::MockTimer, |
| 85 public base::SupportsWeakPtr<MockWeakTimer> { | 63 public base::SupportsWeakPtr<MockWeakTimer> { |
| 86 public: | 64 public: |
| 87 MockWeakTimer(bool retain_user_task, bool is_repeating) | 65 MockWeakTimer(bool retain_user_task, bool is_repeating) |
| 88 : MockTimer(retain_user_task, is_repeating) {} | 66 : MockTimer(retain_user_task, is_repeating) {} |
| 89 }; | 67 }; |
| 90 | 68 |
| 91 // A sub-class of WebSocketHandshakeStreamCreateHelper which always sets a | 69 class WebSocketStreamCreateTest : public ::testing::Test, |
| 92 // deterministic key to use in the WebSocket handshake. | 70 public WebSocketStreamCreateTestBase { |
| 93 class DeterministicKeyWebSocketHandshakeStreamCreateHelper | |
| 94 : public WebSocketHandshakeStreamCreateHelper { | |
| 95 public: | 71 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 { | 72 ~WebSocketStreamCreateTest() override { |
| 111 // Permit any endpoint locks to be released. | 73 // Permit any endpoint locks to be released. |
| 112 stream_request_.reset(); | 74 stream_request_.reset(); |
| 113 stream_.reset(); | 75 stream_.reset(); |
| 114 RunUntilIdle(); | 76 RunUntilIdle(); |
| 115 } | 77 } |
| 116 | 78 |
| 117 void CreateAndConnectCustomResponse( | 79 void CreateAndConnectCustomResponse( |
| 118 const std::string& socket_url, | 80 const std::string& socket_url, |
| 119 const std::string& socket_host, | 81 const std::string& socket_host, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 scoped_ptr<DeterministicSocketData> socket_data, | 116 scoped_ptr<DeterministicSocketData> socket_data, |
| 155 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { | 117 scoped_ptr<base::Timer> timer = scoped_ptr<base::Timer>()) { |
| 156 AddRawExpectations(socket_data.Pass()); | 118 AddRawExpectations(socket_data.Pass()); |
| 157 CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); | 119 CreateAndConnectStream(socket_url, sub_protocols, origin, timer.Pass()); |
| 158 } | 120 } |
| 159 | 121 |
| 160 // Add additional raw expectations for sockets created before the final one. | 122 // Add additional raw expectations for sockets created before the final one. |
| 161 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data) { | 123 void AddRawExpectations(scoped_ptr<DeterministicSocketData> socket_data) { |
| 162 url_request_context_host_.AddRawExpectations(socket_data.Pass()); | 124 url_request_context_host_.AddRawExpectations(socket_data.Pass()); |
| 163 } | 125 } |
| 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 }; | 126 }; |
| 259 | 127 |
| 260 // There are enough tests of the Sec-WebSocket-Extensions header that they | 128 // There are enough tests of the Sec-WebSocket-Extensions header that they |
| 261 // deserve their own test fixture. | 129 // deserve their own test fixture. |
| 262 class WebSocketStreamCreateExtensionTest : public WebSocketStreamCreateTest { | 130 class WebSocketStreamCreateExtensionTest : public WebSocketStreamCreateTest { |
| 263 public: | 131 public: |
| 264 // Performs a standard connect, with the value of the Sec-WebSocket-Extensions | 132 // 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 | 133 // header in the response set to |extensions_header_value|. Runs the event |
| 266 // loop to allow the connect to complete. | 134 // loop to allow the connect to complete. |
| 267 void CreateAndConnectWithExtensions( | 135 void CreateAndConnectWithExtensions( |
| (...skipping 1139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1407 BuildSocketData(reads, writes)); | 1275 BuildSocketData(reads, writes)); |
| 1408 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 1276 socket_data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 1409 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), | 1277 CreateAndConnectRawExpectations("ws://localhost/", NoSubProtocols(), |
| 1410 "http://localhost", socket_data.Pass()); | 1278 "http://localhost", socket_data.Pass()); |
| 1411 RunUntilIdle(); | 1279 RunUntilIdle(); |
| 1412 EXPECT_TRUE(has_failed()); | 1280 EXPECT_TRUE(has_failed()); |
| 1413 } | 1281 } |
| 1414 | 1282 |
| 1415 } // namespace | 1283 } // namespace |
| 1416 } // namespace net | 1284 } // namespace net |
| OLD | NEW |