| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // End-to-end tests for WebSocket. | 5 // End-to-end tests for WebSocket. |
| 6 // | 6 // |
| 7 // A python server is (re)started for each test, which is moderately | 7 // A python server is (re)started for each test, which is moderately |
| 8 // inefficient. However, it makes these tests a good fit for scenarios which | 8 // inefficient. However, it makes these tests a good fit for scenarios which |
| 9 // require special server configurations. | 9 // require special server configurations. |
| 10 | 10 |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/bind_helpers.h" | 15 #include "base/bind_helpers.h" |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/location.h" | 17 #include "base/location.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
| 21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
| 22 #include "base/strings/string_piece.h" | 22 #include "base/strings/string_piece.h" |
| 23 #include "base/thread_task_runner_handle.h" | 23 #include "base/thread_task_runner_handle.h" |
| 24 #include "net/base/auth.h" | 24 #include "net/base/auth.h" |
| 25 #include "net/base/proxy_delegate.h" | 25 #include "net/base/network_delegate.h" |
| 26 #include "net/base/test_data_directory.h" | 26 #include "net/base/test_data_directory.h" |
| 27 #include "net/proxy/proxy_service.h" | 27 #include "net/proxy/proxy_service.h" |
| 28 #include "net/test/embedded_test_server/embedded_test_server.h" | 28 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 29 #include "net/test/spawned_test_server/spawned_test_server.h" | 29 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 30 #include "net/url_request/url_request_test_util.h" | 30 #include "net/url_request/url_request_test_util.h" |
| 31 #include "net/websockets/websocket_channel.h" | 31 #include "net/websockets/websocket_channel.h" |
| 32 #include "net/websockets/websocket_event_interface.h" | 32 #include "net/websockets/websocket_event_interface.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "url/origin.h" | 34 #include "url/origin.h" |
| 35 | 35 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 ERR_SSL_PROTOCOL_ERROR, &ssl_info)); | 188 ERR_SSL_PROTOCOL_ERROR, &ssl_info)); |
| 189 return CHANNEL_ALIVE; | 189 return CHANNEL_ALIVE; |
| 190 } | 190 } |
| 191 | 191 |
| 192 void ConnectTestingEventInterface::QuitNestedEventLoop() { | 192 void ConnectTestingEventInterface::QuitNestedEventLoop() { |
| 193 run_loop_.Quit(); | 193 run_loop_.Quit(); |
| 194 } | 194 } |
| 195 | 195 |
| 196 // A subclass of TestNetworkDelegate that additionally implements the | 196 // A subclass of TestNetworkDelegate that additionally implements the |
| 197 // OnResolveProxy callback and records the information passed to it. | 197 // OnResolveProxy callback and records the information passed to it. |
| 198 class TestProxyDelegateWithProxyInfo : public ProxyDelegate { | 198 class TestNetworkDelegateWithProxyInfo : public TestNetworkDelegate { |
| 199 public: | 199 public: |
| 200 TestProxyDelegateWithProxyInfo() {} | 200 TestNetworkDelegateWithProxyInfo() {} |
| 201 | 201 |
| 202 struct ResolvedProxyInfo { | 202 struct ResolvedProxyInfo { |
| 203 GURL url; | 203 GURL url; |
| 204 ProxyInfo proxy_info; | 204 ProxyInfo proxy_info; |
| 205 }; | 205 }; |
| 206 | 206 |
| 207 const ResolvedProxyInfo& resolved_proxy_info() const { | 207 const ResolvedProxyInfo& resolved_proxy_info() const { |
| 208 return resolved_proxy_info_; | 208 return resolved_proxy_info_; |
| 209 } | 209 } |
| 210 | 210 |
| 211 protected: | 211 protected: |
| 212 void OnResolveProxy(const GURL& url, | 212 void OnResolveProxy(const GURL& url, |
| 213 int load_flags, | 213 int load_flags, |
| 214 const ProxyService& proxy_service, | 214 const ProxyService& proxy_service, |
| 215 ProxyInfo* result) override { | 215 ProxyInfo* result) override { |
| 216 resolved_proxy_info_.url = url; | 216 resolved_proxy_info_.url = url; |
| 217 resolved_proxy_info_.proxy_info = *result; | 217 resolved_proxy_info_.proxy_info = *result; |
| 218 } | 218 } |
| 219 | 219 |
| 220 void OnTunnelConnectCompleted(const HostPortPair& endpoint, | |
| 221 const HostPortPair& proxy_server, | |
| 222 int net_error) override {} | |
| 223 void OnFallback(const ProxyServer& bad_proxy, int net_error) override {} | |
| 224 void OnBeforeSendHeaders(URLRequest* request, | |
| 225 const ProxyInfo& proxy_info, | |
| 226 HttpRequestHeaders* headers) override {} | |
| 227 void OnBeforeTunnelRequest(const HostPortPair& proxy_server, | |
| 228 HttpRequestHeaders* extra_headers) override {} | |
| 229 void OnTunnelHeadersReceived( | |
| 230 const HostPortPair& origin, | |
| 231 const HostPortPair& proxy_server, | |
| 232 const HttpResponseHeaders& response_headers) override {} | |
| 233 bool IsTrustedSpdyProxy(const net::ProxyServer& proxy_server) override { | |
| 234 return true; | |
| 235 } | |
| 236 | |
| 237 private: | 220 private: |
| 238 ResolvedProxyInfo resolved_proxy_info_; | 221 ResolvedProxyInfo resolved_proxy_info_; |
| 239 | 222 |
| 240 DISALLOW_COPY_AND_ASSIGN(TestProxyDelegateWithProxyInfo); | 223 DISALLOW_COPY_AND_ASSIGN(TestNetworkDelegateWithProxyInfo); |
| 241 }; | 224 }; |
| 242 | 225 |
| 243 class WebSocketEndToEndTest : public ::testing::Test { | 226 class WebSocketEndToEndTest : public ::testing::Test { |
| 244 protected: | 227 protected: |
| 245 WebSocketEndToEndTest() | 228 WebSocketEndToEndTest() |
| 246 : event_interface_(), | 229 : event_interface_(), |
| 247 proxy_delegate_(new TestProxyDelegateWithProxyInfo), | 230 network_delegate_(new TestNetworkDelegateWithProxyInfo), |
| 248 context_(true), | 231 context_(true), |
| 249 channel_(), | 232 channel_(), |
| 250 initialised_context_(false) {} | 233 initialised_context_(false) {} |
| 251 | 234 |
| 252 // Initialise the URLRequestContext. Normally done automatically by | 235 // Initialise the URLRequestContext. Normally done automatically by |
| 253 // ConnectAndWait(). This method is for the use of tests that need the | 236 // ConnectAndWait(). This method is for the use of tests that need the |
| 254 // URLRequestContext initialised before calling ConnectAndWait(). | 237 // URLRequestContext initialised before calling ConnectAndWait(). |
| 255 void InitialiseContext() { | 238 void InitialiseContext() { |
| 256 context_.set_proxy_delegate(proxy_delegate_.get()); | 239 context_.set_network_delegate(network_delegate_.get()); |
| 257 context_.Init(); | 240 context_.Init(); |
| 258 initialised_context_ = true; | 241 initialised_context_ = true; |
| 259 } | 242 } |
| 260 | 243 |
| 261 // Send the connect request to |socket_url| and wait for a response. Returns | 244 // Send the connect request to |socket_url| and wait for a response. Returns |
| 262 // true if the handshake succeeded. | 245 // true if the handshake succeeded. |
| 263 bool ConnectAndWait(const GURL& socket_url) { | 246 bool ConnectAndWait(const GURL& socket_url) { |
| 264 if (!initialised_context_) { | 247 if (!initialised_context_) { |
| 265 InitialiseContext(); | 248 InitialiseContext(); |
| 266 } | 249 } |
| 267 url::Origin origin(GURL("http://localhost")); | 250 url::Origin origin(GURL("http://localhost")); |
| 268 event_interface_ = new ConnectTestingEventInterface; | 251 event_interface_ = new ConnectTestingEventInterface; |
| 269 channel_.reset( | 252 channel_.reset( |
| 270 new WebSocketChannel(make_scoped_ptr(event_interface_), &context_)); | 253 new WebSocketChannel(make_scoped_ptr(event_interface_), &context_)); |
| 271 channel_->SendAddChannelRequest(GURL(socket_url), sub_protocols_, origin); | 254 channel_->SendAddChannelRequest(GURL(socket_url), sub_protocols_, origin); |
| 272 event_interface_->WaitForResponse(); | 255 event_interface_->WaitForResponse(); |
| 273 return !event_interface_->failed(); | 256 return !event_interface_->failed(); |
| 274 } | 257 } |
| 275 | 258 |
| 276 ConnectTestingEventInterface* event_interface_; // owned by channel_ | 259 ConnectTestingEventInterface* event_interface_; // owned by channel_ |
| 277 scoped_ptr<TestProxyDelegateWithProxyInfo> proxy_delegate_; | 260 scoped_ptr<TestNetworkDelegateWithProxyInfo> network_delegate_; |
| 278 TestURLRequestContext context_; | 261 TestURLRequestContext context_; |
| 279 scoped_ptr<WebSocketChannel> channel_; | 262 scoped_ptr<WebSocketChannel> channel_; |
| 280 std::vector<std::string> sub_protocols_; | 263 std::vector<std::string> sub_protocols_; |
| 281 bool initialised_context_; | 264 bool initialised_context_; |
| 282 }; | 265 }; |
| 283 | 266 |
| 284 // None of these tests work on Android. | 267 // None of these tests work on Android. |
| 285 // TODO(ricea): Make these tests work on Android. See crbug.com/441711. | 268 // TODO(ricea): Make these tests work on Android. See crbug.com/441711. |
| 286 #if defined(OS_ANDROID) | 269 #if defined(OS_ANDROID) |
| 287 #define DISABLED_ON_ANDROID(test) DISABLED_##test | 270 #define DISABLED_ON_ANDROID(test) DISABLED_##test |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 context_.CreateRequest(http_page, DEFAULT_PRIORITY, &delegate)); | 362 context_.CreateRequest(http_page, DEFAULT_PRIORITY, &delegate)); |
| 380 request->Start(); | 363 request->Start(); |
| 381 // TestDelegate exits the message loop when the request completes by | 364 // TestDelegate exits the message loop when the request completes by |
| 382 // default. | 365 // default. |
| 383 base::RunLoop().Run(); | 366 base::RunLoop().Run(); |
| 384 EXPECT_TRUE(delegate.auth_required_called()); | 367 EXPECT_TRUE(delegate.auth_required_called()); |
| 385 } | 368 } |
| 386 | 369 |
| 387 GURL ws_url = ws_server.GetURL(kEchoServer); | 370 GURL ws_url = ws_server.GetURL(kEchoServer); |
| 388 EXPECT_TRUE(ConnectAndWait(ws_url)); | 371 EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 389 const TestProxyDelegateWithProxyInfo::ResolvedProxyInfo& info = | 372 const TestNetworkDelegateWithProxyInfo::ResolvedProxyInfo& info = |
| 390 proxy_delegate_->resolved_proxy_info(); | 373 network_delegate_->resolved_proxy_info(); |
| 391 EXPECT_EQ(ws_url, info.url); | 374 EXPECT_EQ(ws_url, info.url); |
| 392 EXPECT_TRUE(info.proxy_info.is_http()); | 375 EXPECT_TRUE(info.proxy_info.is_http()); |
| 393 } | 376 } |
| 394 | 377 |
| 395 // This is a regression test for crbug.com/408061 Crash in | 378 // This is a regression test for crbug.com/408061 Crash in |
| 396 // net::WebSocketBasicHandshakeStream::Upgrade. | 379 // net::WebSocketBasicHandshakeStream::Upgrade. |
| 397 TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(TruncatedResponse)) { | 380 TEST_F(WebSocketEndToEndTest, DISABLED_ON_ANDROID(TruncatedResponse)) { |
| 398 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, | 381 SpawnedTestServer ws_server(SpawnedTestServer::TYPE_WS, |
| 399 SpawnedTestServer::kLocalhost, | 382 SpawnedTestServer::kLocalhost, |
| 400 GetWebSocketTestDataDirectory()); | 383 GetWebSocketTestDataDirectory()); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 GURL ws_url = ws_server.GetURL("header-continuation"); | 493 GURL ws_url = ws_server.GetURL("header-continuation"); |
| 511 | 494 |
| 512 EXPECT_TRUE(ConnectAndWait(ws_url)); | 495 EXPECT_TRUE(ConnectAndWait(ws_url)); |
| 513 EXPECT_EQ("permessage-deflate; server_max_window_bits=10", | 496 EXPECT_EQ("permessage-deflate; server_max_window_bits=10", |
| 514 event_interface_->extensions()); | 497 event_interface_->extensions()); |
| 515 } | 498 } |
| 516 | 499 |
| 517 } // namespace | 500 } // namespace |
| 518 | 501 |
| 519 } // namespace net | 502 } // namespace net |
| OLD | NEW |