| 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_job.h" | 5 #include "net/websockets/websocket_job.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 35 #include "testing/platform_test.h" | 35 #include "testing/platform_test.h" |
| 36 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| 37 | 37 |
| 38 namespace net { | 38 namespace net { |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 | 41 |
| 42 class MockSocketStream : public SocketStream { | 42 class MockSocketStream : public SocketStream { |
| 43 public: | 43 public: |
| 44 MockSocketStream(const GURL& url, SocketStream::Delegate* delegate, | 44 MockSocketStream(const GURL& url, |
| 45 URLRequestContext* context, CookieStore* cookie_store) | 45 SocketStream::Delegate* delegate, |
| 46 URLRequestContext* context, |
| 47 CookieStore* cookie_store) |
| 46 : SocketStream(url, delegate, context, cookie_store) {} | 48 : SocketStream(url, delegate, context, cookie_store) {} |
| 47 | 49 |
| 48 virtual void Connect() OVERRIDE {} | 50 virtual void Connect() OVERRIDE {} |
| 49 virtual bool SendData(const char* data, int len) OVERRIDE { | 51 virtual bool SendData(const char* data, int len) OVERRIDE { |
| 50 sent_data_ += std::string(data, len); | 52 sent_data_ += std::string(data, len); |
| 51 return true; | 53 return true; |
| 52 } | 54 } |
| 53 | 55 |
| 54 virtual void Close() OVERRIDE {} | 56 virtual void Close() OVERRIDE {} |
| 55 virtual void RestartWithAuth( | 57 virtual void RestartWithAuth(const AuthCredentials& credentials) OVERRIDE {} |
| 56 const AuthCredentials& credentials) OVERRIDE { | |
| 57 } | |
| 58 | 58 |
| 59 virtual void DetachDelegate() OVERRIDE { | 59 virtual void DetachDelegate() OVERRIDE { delegate_ = NULL; } |
| 60 delegate_ = NULL; | |
| 61 } | |
| 62 | 60 |
| 63 const std::string& sent_data() const { | 61 const std::string& sent_data() const { return sent_data_; } |
| 64 return sent_data_; | |
| 65 } | |
| 66 | 62 |
| 67 protected: | 63 protected: |
| 68 virtual ~MockSocketStream() {} | 64 virtual ~MockSocketStream() {} |
| 69 | 65 |
| 70 private: | 66 private: |
| 71 std::string sent_data_; | 67 std::string sent_data_; |
| 72 }; | 68 }; |
| 73 | 69 |
| 74 class MockSocketStreamDelegate : public SocketStream::Delegate { | 70 class MockSocketStreamDelegate : public SocketStream::Delegate { |
| 75 public: | 71 public: |
| 76 MockSocketStreamDelegate() | 72 MockSocketStreamDelegate() : amount_sent_(0), allow_all_cookies_(true) {} |
| 77 : amount_sent_(0), allow_all_cookies_(true) {} | |
| 78 void set_allow_all_cookies(bool allow_all_cookies) { | 73 void set_allow_all_cookies(bool allow_all_cookies) { |
| 79 allow_all_cookies_ = allow_all_cookies; | 74 allow_all_cookies_ = allow_all_cookies; |
| 80 } | 75 } |
| 81 virtual ~MockSocketStreamDelegate() {} | 76 virtual ~MockSocketStreamDelegate() {} |
| 82 | 77 |
| 83 void SetOnStartOpenConnection(const base::Closure& callback) { | 78 void SetOnStartOpenConnection(const base::Closure& callback) { |
| 84 on_start_open_connection_ = callback; | 79 on_start_open_connection_ = callback; |
| 85 } | 80 } |
| 86 void SetOnConnected(const base::Closure& callback) { | 81 void SetOnConnected(const base::Closure& callback) { |
| 87 on_connected_ = callback; | 82 on_connected_ = callback; |
| 88 } | 83 } |
| 89 void SetOnSentData(const base::Closure& callback) { | 84 void SetOnSentData(const base::Closure& callback) { |
| 90 on_sent_data_ = callback; | 85 on_sent_data_ = callback; |
| 91 } | 86 } |
| 92 void SetOnReceivedData(const base::Closure& callback) { | 87 void SetOnReceivedData(const base::Closure& callback) { |
| 93 on_received_data_ = callback; | 88 on_received_data_ = callback; |
| 94 } | 89 } |
| 95 void SetOnClose(const base::Closure& callback) { | 90 void SetOnClose(const base::Closure& callback) { on_close_ = callback; } |
| 96 on_close_ = callback; | |
| 97 } | |
| 98 | 91 |
| 99 virtual int OnStartOpenConnection( | 92 virtual int OnStartOpenConnection( |
| 100 SocketStream* socket, | 93 SocketStream* socket, |
| 101 const CompletionCallback& callback) OVERRIDE { | 94 const CompletionCallback& callback) OVERRIDE { |
| 102 if (!on_start_open_connection_.is_null()) | 95 if (!on_start_open_connection_.is_null()) |
| 103 on_start_open_connection_.Run(); | 96 on_start_open_connection_.Run(); |
| 104 return OK; | 97 return OK; |
| 105 } | 98 } |
| 106 virtual void OnConnected(SocketStream* socket, | 99 virtual void OnConnected(SocketStream* socket, |
| 107 int max_pending_send_allowed) OVERRIDE { | 100 int max_pending_send_allowed) OVERRIDE { |
| 108 if (!on_connected_.is_null()) | 101 if (!on_connected_.is_null()) |
| 109 on_connected_.Run(); | 102 on_connected_.Run(); |
| 110 } | 103 } |
| 111 virtual void OnSentData(SocketStream* socket, | 104 virtual void OnSentData(SocketStream* socket, int amount_sent) OVERRIDE { |
| 112 int amount_sent) OVERRIDE { | |
| 113 amount_sent_ += amount_sent; | 105 amount_sent_ += amount_sent; |
| 114 if (!on_sent_data_.is_null()) | 106 if (!on_sent_data_.is_null()) |
| 115 on_sent_data_.Run(); | 107 on_sent_data_.Run(); |
| 116 } | 108 } |
| 117 virtual void OnReceivedData(SocketStream* socket, | 109 virtual void OnReceivedData(SocketStream* socket, |
| 118 const char* data, int len) OVERRIDE { | 110 const char* data, |
| 111 int len) OVERRIDE { |
| 119 received_data_ += std::string(data, len); | 112 received_data_ += std::string(data, len); |
| 120 if (!on_received_data_.is_null()) | 113 if (!on_received_data_.is_null()) |
| 121 on_received_data_.Run(); | 114 on_received_data_.Run(); |
| 122 } | 115 } |
| 123 virtual void OnClose(SocketStream* socket) OVERRIDE { | 116 virtual void OnClose(SocketStream* socket) OVERRIDE { |
| 124 if (!on_close_.is_null()) | 117 if (!on_close_.is_null()) |
| 125 on_close_.Run(); | 118 on_close_.Run(); |
| 126 } | 119 } |
| 127 virtual bool CanGetCookies(SocketStream* socket, | 120 virtual bool CanGetCookies(SocketStream* socket, const GURL& url) OVERRIDE { |
| 128 const GURL& url) OVERRIDE { | |
| 129 return allow_all_cookies_; | 121 return allow_all_cookies_; |
| 130 } | 122 } |
| 131 virtual bool CanSetCookie(SocketStream* request, | 123 virtual bool CanSetCookie(SocketStream* request, |
| 132 const GURL& url, | 124 const GURL& url, |
| 133 const std::string& cookie_line, | 125 const std::string& cookie_line, |
| 134 CookieOptions* options) OVERRIDE { | 126 CookieOptions* options) OVERRIDE { |
| 135 return allow_all_cookies_; | 127 return allow_all_cookies_; |
| 136 } | 128 } |
| 137 | 129 |
| 138 size_t amount_sent() const { return amount_sent_; } | 130 size_t amount_sent() const { return amount_sent_; } |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 }; | 247 }; |
| 256 | 248 |
| 257 class MockURLRequestContext : public URLRequestContext { | 249 class MockURLRequestContext : public URLRequestContext { |
| 258 public: | 250 public: |
| 259 explicit MockURLRequestContext(CookieStore* cookie_store) | 251 explicit MockURLRequestContext(CookieStore* cookie_store) |
| 260 : transport_security_state_() { | 252 : transport_security_state_() { |
| 261 set_cookie_store(cookie_store); | 253 set_cookie_store(cookie_store); |
| 262 set_transport_security_state(&transport_security_state_); | 254 set_transport_security_state(&transport_security_state_); |
| 263 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); | 255 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 264 bool include_subdomains = false; | 256 bool include_subdomains = false; |
| 265 transport_security_state_.AddHSTS("upgrademe.com", expiry, | 257 transport_security_state_.AddHSTS( |
| 266 include_subdomains); | 258 "upgrademe.com", expiry, include_subdomains); |
| 267 } | 259 } |
| 268 | 260 |
| 269 virtual ~MockURLRequestContext() {} | 261 virtual ~MockURLRequestContext() {} |
| 270 | 262 |
| 271 private: | 263 private: |
| 272 TransportSecurityState transport_security_state_; | 264 TransportSecurityState transport_security_state_; |
| 273 }; | 265 }; |
| 274 | 266 |
| 275 class MockHttpTransactionFactory : public HttpTransactionFactory { | 267 class MockHttpTransactionFactory : public HttpTransactionFactory { |
| 276 public: | 268 public: |
| 277 MockHttpTransactionFactory(NextProto next_proto, OrderedSocketData* data) { | 269 MockHttpTransactionFactory(NextProto next_proto, OrderedSocketData* data) { |
| 278 data_ = data; | 270 data_ = data; |
| 279 MockConnect connect_data(SYNCHRONOUS, OK); | 271 MockConnect connect_data(SYNCHRONOUS, OK); |
| 280 data_->set_connect_data(connect_data); | 272 data_->set_connect_data(connect_data); |
| 281 session_deps_.reset(new SpdySessionDependencies(next_proto)); | 273 session_deps_.reset(new SpdySessionDependencies(next_proto)); |
| 282 session_deps_->socket_factory->AddSocketDataProvider(data_); | 274 session_deps_->socket_factory->AddSocketDataProvider(data_); |
| 283 http_session_ = | 275 http_session_ = |
| 284 SpdySessionDependencies::SpdyCreateSession(session_deps_.get()); | 276 SpdySessionDependencies::SpdyCreateSession(session_deps_.get()); |
| 285 host_port_pair_.set_host("example.com"); | 277 host_port_pair_.set_host("example.com"); |
| 286 host_port_pair_.set_port(80); | 278 host_port_pair_.set_port(80); |
| 287 spdy_session_key_ = SpdySessionKey(host_port_pair_, | 279 spdy_session_key_ = SpdySessionKey( |
| 288 ProxyServer::Direct(), | 280 host_port_pair_, ProxyServer::Direct(), PRIVACY_MODE_DISABLED); |
| 289 PRIVACY_MODE_DISABLED); | |
| 290 session_ = CreateInsecureSpdySession( | 281 session_ = CreateInsecureSpdySession( |
| 291 http_session_, spdy_session_key_, BoundNetLog()); | 282 http_session_, spdy_session_key_, BoundNetLog()); |
| 292 } | 283 } |
| 293 | 284 |
| 294 virtual int CreateTransaction( | 285 virtual int CreateTransaction(RequestPriority priority, |
| 295 RequestPriority priority, | 286 scoped_ptr<HttpTransaction>* trans) OVERRIDE { |
| 296 scoped_ptr<HttpTransaction>* trans) OVERRIDE { | |
| 297 NOTREACHED(); | 287 NOTREACHED(); |
| 298 return ERR_UNEXPECTED; | 288 return ERR_UNEXPECTED; |
| 299 } | 289 } |
| 300 | 290 |
| 301 virtual HttpCache* GetCache() OVERRIDE { | 291 virtual HttpCache* GetCache() OVERRIDE { |
| 302 NOTREACHED(); | 292 NOTREACHED(); |
| 303 return NULL; | 293 return NULL; |
| 304 } | 294 } |
| 305 | 295 |
| 306 virtual HttpNetworkSession* GetSession() OVERRIDE { | 296 virtual HttpNetworkSession* GetSession() OVERRIDE { |
| 307 return http_session_.get(); | 297 return http_session_.get(); |
| 308 } | 298 } |
| 309 | 299 |
| 310 private: | 300 private: |
| 311 OrderedSocketData* data_; | 301 OrderedSocketData* data_; |
| 312 scoped_ptr<SpdySessionDependencies> session_deps_; | 302 scoped_ptr<SpdySessionDependencies> session_deps_; |
| 313 scoped_refptr<HttpNetworkSession> http_session_; | 303 scoped_refptr<HttpNetworkSession> http_session_; |
| 314 base::WeakPtr<SpdySession> session_; | 304 base::WeakPtr<SpdySession> session_; |
| 315 HostPortPair host_port_pair_; | 305 HostPortPair host_port_pair_; |
| 316 SpdySessionKey spdy_session_key_; | 306 SpdySessionKey spdy_session_key_; |
| 317 }; | 307 }; |
| 318 | 308 |
| 319 class DeletingSocketStreamDelegate : public SocketStream::Delegate { | 309 class DeletingSocketStreamDelegate : public SocketStream::Delegate { |
| 320 public: | 310 public: |
| 321 DeletingSocketStreamDelegate() | 311 DeletingSocketStreamDelegate() : delete_next_(false) {} |
| 322 : delete_next_(false) {} | |
| 323 | 312 |
| 324 // Since this class needs to be able to delete |job_|, it must be the only | 313 // Since this class needs to be able to delete |job_|, it must be the only |
| 325 // reference holder (except for temporary references). Provide access to the | 314 // reference holder (except for temporary references). Provide access to the |
| 326 // pointer for tests to use. | 315 // pointer for tests to use. |
| 327 WebSocketJob* job() { return job_.get(); } | 316 WebSocketJob* job() { return job_.get(); } |
| 328 | 317 |
| 329 void set_job(WebSocketJob* job) { job_ = job; } | 318 void set_job(WebSocketJob* job) { job_ = job; } |
| 330 | 319 |
| 331 // After calling this, the next call to a method on this delegate will delete | 320 // After calling this, the next call to a method on this delegate will delete |
| 332 // the WebSocketJob object. | 321 // the WebSocketJob object. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 socket_ = NULL; | 392 socket_ = NULL; |
| 404 } | 393 } |
| 405 void DoSendRequest() { | 394 void DoSendRequest() { |
| 406 EXPECT_TRUE(websocket_->SendData(kHandshakeRequestWithoutCookie, | 395 EXPECT_TRUE(websocket_->SendData(kHandshakeRequestWithoutCookie, |
| 407 kHandshakeRequestWithoutCookieLength)); | 396 kHandshakeRequestWithoutCookieLength)); |
| 408 } | 397 } |
| 409 void DoSendData() { | 398 void DoSendData() { |
| 410 if (received_data().size() == kHandshakeResponseWithoutCookieLength) | 399 if (received_data().size() == kHandshakeResponseWithoutCookieLength) |
| 411 websocket_->SendData(kDataHello, kDataHelloLength); | 400 websocket_->SendData(kDataHello, kDataHelloLength); |
| 412 } | 401 } |
| 413 void DoSync() { | 402 void DoSync() { sync_test_callback_.callback().Run(OK); } |
| 414 sync_test_callback_.callback().Run(OK); | 403 int WaitForResult() { return sync_test_callback_.WaitForResult(); } |
| 415 } | 404 |
| 416 int WaitForResult() { | |
| 417 return sync_test_callback_.WaitForResult(); | |
| 418 } | |
| 419 protected: | 405 protected: |
| 420 enum StreamType { | 406 enum StreamType { |
| 421 STREAM_INVALID, | 407 STREAM_INVALID, |
| 422 STREAM_MOCK_SOCKET, | 408 STREAM_MOCK_SOCKET, |
| 423 STREAM_SOCKET, | 409 STREAM_SOCKET, |
| 424 STREAM_SPDY_WEBSOCKET, | 410 STREAM_SPDY_WEBSOCKET, |
| 425 }; | 411 }; |
| 426 enum ThrottlingOption { | 412 enum ThrottlingOption { |
| 427 THROTTLING_OFF, | 413 THROTTLING_OFF, |
| 428 THROTTLING_ON, | 414 THROTTLING_ON, |
| 429 }; | 415 }; |
| 430 enum SpdyOption { | 416 enum SpdyOption { |
| 431 SPDY_OFF, | 417 SPDY_OFF, |
| 432 SPDY_ON, | 418 SPDY_ON, |
| 433 }; | 419 }; |
| 434 void InitWebSocketJob(const GURL& url, | 420 void InitWebSocketJob(const GURL& url, |
| 435 MockSocketStreamDelegate* delegate, | 421 MockSocketStreamDelegate* delegate, |
| 436 StreamType stream_type) { | 422 StreamType stream_type) { |
| 437 DCHECK_NE(STREAM_INVALID, stream_type); | 423 DCHECK_NE(STREAM_INVALID, stream_type); |
| 438 stream_type_ = stream_type; | 424 stream_type_ = stream_type; |
| 439 websocket_ = new WebSocketJob(delegate); | 425 websocket_ = new WebSocketJob(delegate); |
| 440 | 426 |
| 441 if (stream_type == STREAM_MOCK_SOCKET) | 427 if (stream_type == STREAM_MOCK_SOCKET) |
| 442 socket_ = new MockSocketStream(url, websocket_.get(), context_.get(), | 428 socket_ = |
| 443 NULL); | 429 new MockSocketStream(url, websocket_.get(), context_.get(), NULL); |
| 444 | 430 |
| 445 if (stream_type == STREAM_SOCKET || stream_type == STREAM_SPDY_WEBSOCKET) { | 431 if (stream_type == STREAM_SOCKET || stream_type == STREAM_SPDY_WEBSOCKET) { |
| 446 if (stream_type == STREAM_SPDY_WEBSOCKET) { | 432 if (stream_type == STREAM_SPDY_WEBSOCKET) { |
| 447 http_factory_.reset( | 433 http_factory_.reset( |
| 448 new MockHttpTransactionFactory(GetParam(), data_.get())); | 434 new MockHttpTransactionFactory(GetParam(), data_.get())); |
| 449 context_->set_http_transaction_factory(http_factory_.get()); | 435 context_->set_http_transaction_factory(http_factory_.get()); |
| 450 } | 436 } |
| 451 | 437 |
| 452 ssl_config_service_ = new MockSSLConfigService(); | 438 ssl_config_service_ = new MockSSLConfigService(); |
| 453 context_->set_ssl_config_service(ssl_config_service_.get()); | 439 context_->set_ssl_config_service(ssl_config_service_.get()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 469 // perform a real connect. In that case, the following address is used | 455 // perform a real connect. In that case, the following address is used |
| 470 // instead. | 456 // instead. |
| 471 IPAddressNumber ip; | 457 IPAddressNumber ip; |
| 472 ParseIPLiteralToNumber("127.0.0.1", &ip); | 458 ParseIPLiteralToNumber("127.0.0.1", &ip); |
| 473 websocket_->addresses_ = AddressList::CreateFromIPAddress(ip, 80); | 459 websocket_->addresses_ = AddressList::CreateFromIPAddress(ip, 80); |
| 474 } | 460 } |
| 475 void SkipToConnecting() { | 461 void SkipToConnecting() { |
| 476 websocket_->state_ = WebSocketJob::CONNECTING; | 462 websocket_->state_ = WebSocketJob::CONNECTING; |
| 477 ASSERT_TRUE(WebSocketThrottle::GetInstance()->PutInQueue(websocket_.get())); | 463 ASSERT_TRUE(WebSocketThrottle::GetInstance()->PutInQueue(websocket_.get())); |
| 478 } | 464 } |
| 479 WebSocketJob::State GetWebSocketJobState() { | 465 WebSocketJob::State GetWebSocketJobState() { return websocket_->state_; } |
| 480 return websocket_->state_; | |
| 481 } | |
| 482 void CloseWebSocketJob() { | 466 void CloseWebSocketJob() { |
| 483 if (websocket_->socket_.get()) { | 467 if (websocket_->socket_.get()) { |
| 484 websocket_->socket_->DetachDelegate(); | 468 websocket_->socket_->DetachDelegate(); |
| 485 WebSocketThrottle::GetInstance()->RemoveFromQueue(websocket_.get()); | 469 WebSocketThrottle::GetInstance()->RemoveFromQueue(websocket_.get()); |
| 486 } | 470 } |
| 487 websocket_->state_ = WebSocketJob::CLOSED; | 471 websocket_->state_ = WebSocketJob::CLOSED; |
| 488 websocket_->delegate_ = NULL; | 472 websocket_->delegate_ = NULL; |
| 489 websocket_->socket_ = NULL; | 473 websocket_->socket_ = NULL; |
| 490 } | 474 } |
| 491 SocketStream* GetSocket(SocketStreamJob* job) { | 475 SocketStream* GetSocket(SocketStreamJob* job) { return job->socket_.get(); } |
| 492 return job->socket_.get(); | |
| 493 } | |
| 494 const std::string& sent_data() const { | 476 const std::string& sent_data() const { |
| 495 DCHECK_EQ(STREAM_MOCK_SOCKET, stream_type_); | 477 DCHECK_EQ(STREAM_MOCK_SOCKET, stream_type_); |
| 496 MockSocketStream* socket = | 478 MockSocketStream* socket = static_cast<MockSocketStream*>(socket_.get()); |
| 497 static_cast<MockSocketStream*>(socket_.get()); | |
| 498 DCHECK(socket); | 479 DCHECK(socket); |
| 499 return socket->sent_data(); | 480 return socket->sent_data(); |
| 500 } | 481 } |
| 501 const std::string& received_data() const { | 482 const std::string& received_data() const { |
| 502 DCHECK_NE(STREAM_INVALID, stream_type_); | 483 DCHECK_NE(STREAM_INVALID, stream_type_); |
| 503 MockSocketStreamDelegate* delegate = | 484 MockSocketStreamDelegate* delegate = |
| 504 static_cast<MockSocketStreamDelegate*>(websocket_->delegate_); | 485 static_cast<MockSocketStreamDelegate*>(websocket_->delegate_); |
| 505 DCHECK(delegate); | 486 DCHECK(delegate); |
| 506 return delegate->received_data(); | 487 return delegate->received_data(); |
| 507 } | 488 } |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 const size_t WebSocketJobTest::kHandshakeRequestWithoutCookieLength = | 616 const size_t WebSocketJobTest::kHandshakeRequestWithoutCookieLength = |
| 636 arraysize(kHandshakeRequestWithoutCookie) - 1; | 617 arraysize(kHandshakeRequestWithoutCookie) - 1; |
| 637 const size_t WebSocketJobTest::kHandshakeRequestWithCookieLength = | 618 const size_t WebSocketJobTest::kHandshakeRequestWithCookieLength = |
| 638 arraysize(kHandshakeRequestWithCookie) - 1; | 619 arraysize(kHandshakeRequestWithCookie) - 1; |
| 639 const size_t WebSocketJobTest::kHandshakeRequestWithFilteredCookieLength = | 620 const size_t WebSocketJobTest::kHandshakeRequestWithFilteredCookieLength = |
| 640 arraysize(kHandshakeRequestWithFilteredCookie) - 1; | 621 arraysize(kHandshakeRequestWithFilteredCookie) - 1; |
| 641 const size_t WebSocketJobTest::kHandshakeResponseWithoutCookieLength = | 622 const size_t WebSocketJobTest::kHandshakeResponseWithoutCookieLength = |
| 642 arraysize(kHandshakeResponseWithoutCookie) - 1; | 623 arraysize(kHandshakeResponseWithoutCookie) - 1; |
| 643 const size_t WebSocketJobTest::kHandshakeResponseWithCookieLength = | 624 const size_t WebSocketJobTest::kHandshakeResponseWithCookieLength = |
| 644 arraysize(kHandshakeResponseWithCookie) - 1; | 625 arraysize(kHandshakeResponseWithCookie) - 1; |
| 645 const size_t WebSocketJobTest::kDataHelloLength = | 626 const size_t WebSocketJobTest::kDataHelloLength = arraysize(kDataHello) - 1; |
| 646 arraysize(kDataHello) - 1; | 627 const size_t WebSocketJobTest::kDataWorldLength = arraysize(kDataWorld) - 1; |
| 647 const size_t WebSocketJobTest::kDataWorldLength = | |
| 648 arraysize(kDataWorld) - 1; | |
| 649 | 628 |
| 650 void WebSocketJobTest::TestSimpleHandshake() { | 629 void WebSocketJobTest::TestSimpleHandshake() { |
| 651 GURL url("ws://example.com/demo"); | 630 GURL url("ws://example.com/demo"); |
| 652 MockSocketStreamDelegate delegate; | 631 MockSocketStreamDelegate delegate; |
| 653 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); | 632 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); |
| 654 SkipToConnecting(); | 633 SkipToConnecting(); |
| 655 | 634 |
| 656 DoSendRequest(); | 635 DoSendRequest(); |
| 657 base::MessageLoop::current()->RunUntilIdle(); | 636 base::MessageLoop::current()->RunUntilIdle(); |
| 658 EXPECT_EQ(kHandshakeRequestWithoutCookie, sent_data()); | 637 EXPECT_EQ(kHandshakeRequestWithoutCookie, sent_data()); |
| 659 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); | 638 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); |
| 660 websocket_->OnSentData(socket_.get(), | 639 websocket_->OnSentData(socket_.get(), kHandshakeRequestWithoutCookieLength); |
| 661 kHandshakeRequestWithoutCookieLength); | |
| 662 EXPECT_EQ(kHandshakeRequestWithoutCookieLength, delegate.amount_sent()); | 640 EXPECT_EQ(kHandshakeRequestWithoutCookieLength, delegate.amount_sent()); |
| 663 | 641 |
| 664 websocket_->OnReceivedData(socket_.get(), | 642 websocket_->OnReceivedData(socket_.get(), |
| 665 kHandshakeResponseWithoutCookie, | 643 kHandshakeResponseWithoutCookie, |
| 666 kHandshakeResponseWithoutCookieLength); | 644 kHandshakeResponseWithoutCookieLength); |
| 667 base::MessageLoop::current()->RunUntilIdle(); | 645 base::MessageLoop::current()->RunUntilIdle(); |
| 668 EXPECT_EQ(kHandshakeResponseWithoutCookie, delegate.received_data()); | 646 EXPECT_EQ(kHandshakeResponseWithoutCookie, delegate.received_data()); |
| 669 EXPECT_EQ(WebSocketJob::OPEN, GetWebSocketJobState()); | 647 EXPECT_EQ(WebSocketJob::OPEN, GetWebSocketJobState()); |
| 670 CloseWebSocketJob(); | 648 CloseWebSocketJob(); |
| 671 } | 649 } |
| 672 | 650 |
| 673 void WebSocketJobTest::TestSlowHandshake() { | 651 void WebSocketJobTest::TestSlowHandshake() { |
| 674 GURL url("ws://example.com/demo"); | 652 GURL url("ws://example.com/demo"); |
| 675 MockSocketStreamDelegate delegate; | 653 MockSocketStreamDelegate delegate; |
| 676 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); | 654 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); |
| 677 SkipToConnecting(); | 655 SkipToConnecting(); |
| 678 | 656 |
| 679 DoSendRequest(); | 657 DoSendRequest(); |
| 680 // We assume request is sent in one data chunk (from WebKit) | 658 // We assume request is sent in one data chunk (from WebKit) |
| 681 // We don't support streaming request. | 659 // We don't support streaming request. |
| 682 base::MessageLoop::current()->RunUntilIdle(); | 660 base::MessageLoop::current()->RunUntilIdle(); |
| 683 EXPECT_EQ(kHandshakeRequestWithoutCookie, sent_data()); | 661 EXPECT_EQ(kHandshakeRequestWithoutCookie, sent_data()); |
| 684 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); | 662 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); |
| 685 websocket_->OnSentData(socket_.get(), | 663 websocket_->OnSentData(socket_.get(), kHandshakeRequestWithoutCookieLength); |
| 686 kHandshakeRequestWithoutCookieLength); | |
| 687 EXPECT_EQ(kHandshakeRequestWithoutCookieLength, delegate.amount_sent()); | 664 EXPECT_EQ(kHandshakeRequestWithoutCookieLength, delegate.amount_sent()); |
| 688 | 665 |
| 689 std::vector<std::string> lines; | 666 std::vector<std::string> lines; |
| 690 base::SplitString(kHandshakeResponseWithoutCookie, '\n', &lines); | 667 base::SplitString(kHandshakeResponseWithoutCookie, '\n', &lines); |
| 691 for (size_t i = 0; i < lines.size() - 2; i++) { | 668 for (size_t i = 0; i < lines.size() - 2; i++) { |
| 692 std::string line = lines[i] + "\r\n"; | 669 std::string line = lines[i] + "\r\n"; |
| 693 SCOPED_TRACE("Line: " + line); | 670 SCOPED_TRACE("Line: " + line); |
| 694 websocket_->OnReceivedData(socket_.get(), line.c_str(), line.size()); | 671 websocket_->OnReceivedData(socket_.get(), line.c_str(), line.size()); |
| 695 base::MessageLoop::current()->RunUntilIdle(); | 672 base::MessageLoop::current()->RunUntilIdle(); |
| 696 EXPECT_TRUE(delegate.received_data().empty()); | 673 EXPECT_TRUE(delegate.received_data().empty()); |
| 697 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); | 674 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); |
| 698 } | 675 } |
| 699 websocket_->OnReceivedData(socket_.get(), "\r\n", 2); | 676 websocket_->OnReceivedData(socket_.get(), "\r\n", 2); |
| 700 base::MessageLoop::current()->RunUntilIdle(); | 677 base::MessageLoop::current()->RunUntilIdle(); |
| 701 EXPECT_FALSE(delegate.received_data().empty()); | 678 EXPECT_FALSE(delegate.received_data().empty()); |
| 702 EXPECT_EQ(kHandshakeResponseWithoutCookie, delegate.received_data()); | 679 EXPECT_EQ(kHandshakeResponseWithoutCookie, delegate.received_data()); |
| 703 EXPECT_EQ(WebSocketJob::OPEN, GetWebSocketJobState()); | 680 EXPECT_EQ(WebSocketJob::OPEN, GetWebSocketJobState()); |
| 704 CloseWebSocketJob(); | 681 CloseWebSocketJob(); |
| 705 } | 682 } |
| 706 | 683 |
| 707 INSTANTIATE_TEST_CASE_P( | 684 INSTANTIATE_TEST_CASE_P(NextProto, |
| 708 NextProto, | 685 WebSocketJobTest, |
| 709 WebSocketJobTest, | 686 testing::Values(kProtoDeprecatedSPDY2, |
| 710 testing::Values(kProtoDeprecatedSPDY2, | 687 kProtoSPDY3, |
| 711 kProtoSPDY3, kProtoSPDY31, kProtoSPDY4)); | 688 kProtoSPDY31, |
| 689 kProtoSPDY4)); |
| 712 | 690 |
| 713 TEST_P(WebSocketJobTest, DelayedCookies) { | 691 TEST_P(WebSocketJobTest, DelayedCookies) { |
| 714 WebSocketJob::set_websocket_over_spdy_enabled(true); | 692 WebSocketJob::set_websocket_over_spdy_enabled(true); |
| 715 GURL url("ws://example.com/demo"); | 693 GURL url("ws://example.com/demo"); |
| 716 GURL cookieUrl("http://example.com/demo"); | 694 GURL cookieUrl("http://example.com/demo"); |
| 717 CookieOptions cookie_options; | 695 CookieOptions cookie_options; |
| 718 scoped_refptr<DelayedCookieMonster> cookie_store = new DelayedCookieMonster(); | 696 scoped_refptr<DelayedCookieMonster> cookie_store = new DelayedCookieMonster(); |
| 719 context_->set_cookie_store(cookie_store.get()); | 697 context_->set_cookie_store(cookie_store.get()); |
| 720 cookie_store->SetCookieWithOptionsAsync(cookieUrl, | 698 cookie_store->SetCookieWithOptionsAsync(cookieUrl, |
| 721 "CR-test=1", | 699 "CR-test=1", |
| 722 cookie_options, | 700 cookie_options, |
| 723 CookieMonster::SetCookiesCallback()); | 701 CookieMonster::SetCookiesCallback()); |
| 724 cookie_options.set_include_httponly(); | 702 cookie_options.set_include_httponly(); |
| 725 cookie_store->SetCookieWithOptionsAsync( | 703 cookie_store->SetCookieWithOptionsAsync(cookieUrl, |
| 726 cookieUrl, "CR-test-httponly=1", cookie_options, | 704 "CR-test-httponly=1", |
| 727 CookieMonster::SetCookiesCallback()); | 705 cookie_options, |
| 706 CookieMonster::SetCookiesCallback()); |
| 728 | 707 |
| 729 MockSocketStreamDelegate delegate; | 708 MockSocketStreamDelegate delegate; |
| 730 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); | 709 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); |
| 731 SkipToConnecting(); | 710 SkipToConnecting(); |
| 732 | 711 |
| 733 bool sent = websocket_->SendData(kHandshakeRequestWithCookie, | 712 bool sent = websocket_->SendData(kHandshakeRequestWithCookie, |
| 734 kHandshakeRequestWithCookieLength); | 713 kHandshakeRequestWithCookieLength); |
| 735 EXPECT_TRUE(sent); | 714 EXPECT_TRUE(sent); |
| 736 base::MessageLoop::current()->RunUntilIdle(); | 715 base::MessageLoop::current()->RunUntilIdle(); |
| 737 EXPECT_EQ(kHandshakeRequestWithFilteredCookie, sent_data()); | 716 EXPECT_EQ(kHandshakeRequestWithFilteredCookie, sent_data()); |
| 738 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); | 717 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); |
| 739 websocket_->OnSentData(socket_.get(), | 718 websocket_->OnSentData(socket_.get(), |
| 740 kHandshakeRequestWithFilteredCookieLength); | 719 kHandshakeRequestWithFilteredCookieLength); |
| 741 EXPECT_EQ(kHandshakeRequestWithCookieLength, | 720 EXPECT_EQ(kHandshakeRequestWithCookieLength, delegate.amount_sent()); |
| 742 delegate.amount_sent()); | |
| 743 | 721 |
| 744 websocket_->OnReceivedData(socket_.get(), | 722 websocket_->OnReceivedData(socket_.get(), |
| 745 kHandshakeResponseWithCookie, | 723 kHandshakeResponseWithCookie, |
| 746 kHandshakeResponseWithCookieLength); | 724 kHandshakeResponseWithCookieLength); |
| 747 base::MessageLoop::current()->RunUntilIdle(); | 725 base::MessageLoop::current()->RunUntilIdle(); |
| 748 EXPECT_EQ(kHandshakeResponseWithoutCookie, delegate.received_data()); | 726 EXPECT_EQ(kHandshakeResponseWithoutCookie, delegate.received_data()); |
| 749 EXPECT_EQ(WebSocketJob::OPEN, GetWebSocketJobState()); | 727 EXPECT_EQ(WebSocketJob::OPEN, GetWebSocketJobState()); |
| 750 | 728 |
| 751 CloseWebSocketJob(); | 729 CloseWebSocketJob(); |
| 752 } | 730 } |
| 753 | 731 |
| 754 void WebSocketJobTest::TestHandshakeWithCookie() { | 732 void WebSocketJobTest::TestHandshakeWithCookie() { |
| 755 GURL url("ws://example.com/demo"); | 733 GURL url("ws://example.com/demo"); |
| 756 GURL cookieUrl("http://example.com/demo"); | 734 GURL cookieUrl("http://example.com/demo"); |
| 757 CookieOptions cookie_options; | 735 CookieOptions cookie_options; |
| 758 cookie_store_->SetCookieWithOptions( | 736 cookie_store_->SetCookieWithOptions(cookieUrl, "CR-test=1", cookie_options); |
| 759 cookieUrl, "CR-test=1", cookie_options); | |
| 760 cookie_options.set_include_httponly(); | 737 cookie_options.set_include_httponly(); |
| 761 cookie_store_->SetCookieWithOptions( | 738 cookie_store_->SetCookieWithOptions( |
| 762 cookieUrl, "CR-test-httponly=1", cookie_options); | 739 cookieUrl, "CR-test-httponly=1", cookie_options); |
| 763 | 740 |
| 764 MockSocketStreamDelegate delegate; | 741 MockSocketStreamDelegate delegate; |
| 765 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); | 742 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); |
| 766 SkipToConnecting(); | 743 SkipToConnecting(); |
| 767 | 744 |
| 768 bool sent = websocket_->SendData(kHandshakeRequestWithCookie, | 745 bool sent = websocket_->SendData(kHandshakeRequestWithCookie, |
| 769 kHandshakeRequestWithCookieLength); | 746 kHandshakeRequestWithCookieLength); |
| 770 EXPECT_TRUE(sent); | 747 EXPECT_TRUE(sent); |
| 771 base::MessageLoop::current()->RunUntilIdle(); | 748 base::MessageLoop::current()->RunUntilIdle(); |
| 772 EXPECT_EQ(kHandshakeRequestWithFilteredCookie, sent_data()); | 749 EXPECT_EQ(kHandshakeRequestWithFilteredCookie, sent_data()); |
| 773 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); | 750 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); |
| 774 websocket_->OnSentData(socket_.get(), | 751 websocket_->OnSentData(socket_.get(), |
| 775 kHandshakeRequestWithFilteredCookieLength); | 752 kHandshakeRequestWithFilteredCookieLength); |
| 776 EXPECT_EQ(kHandshakeRequestWithCookieLength, | 753 EXPECT_EQ(kHandshakeRequestWithCookieLength, delegate.amount_sent()); |
| 777 delegate.amount_sent()); | |
| 778 | 754 |
| 779 websocket_->OnReceivedData(socket_.get(), | 755 websocket_->OnReceivedData(socket_.get(), |
| 780 kHandshakeResponseWithCookie, | 756 kHandshakeResponseWithCookie, |
| 781 kHandshakeResponseWithCookieLength); | 757 kHandshakeResponseWithCookieLength); |
| 782 base::MessageLoop::current()->RunUntilIdle(); | 758 base::MessageLoop::current()->RunUntilIdle(); |
| 783 EXPECT_EQ(kHandshakeResponseWithoutCookie, delegate.received_data()); | 759 EXPECT_EQ(kHandshakeResponseWithoutCookie, delegate.received_data()); |
| 784 EXPECT_EQ(WebSocketJob::OPEN, GetWebSocketJobState()); | 760 EXPECT_EQ(WebSocketJob::OPEN, GetWebSocketJobState()); |
| 785 | 761 |
| 786 EXPECT_EQ(3U, cookie_store_->entries().size()); | 762 EXPECT_EQ(3U, cookie_store_->entries().size()); |
| 787 EXPECT_EQ(cookieUrl, cookie_store_->entries()[0].url); | 763 EXPECT_EQ(cookieUrl, cookie_store_->entries()[0].url); |
| 788 EXPECT_EQ("CR-test=1", cookie_store_->entries()[0].cookie_line); | 764 EXPECT_EQ("CR-test=1", cookie_store_->entries()[0].cookie_line); |
| 789 EXPECT_EQ(cookieUrl, cookie_store_->entries()[1].url); | 765 EXPECT_EQ(cookieUrl, cookie_store_->entries()[1].url); |
| 790 EXPECT_EQ("CR-test-httponly=1", cookie_store_->entries()[1].cookie_line); | 766 EXPECT_EQ("CR-test-httponly=1", cookie_store_->entries()[1].cookie_line); |
| 791 EXPECT_EQ(cookieUrl, cookie_store_->entries()[2].url); | 767 EXPECT_EQ(cookieUrl, cookie_store_->entries()[2].url); |
| 792 EXPECT_EQ("CR-set-test=1", cookie_store_->entries()[2].cookie_line); | 768 EXPECT_EQ("CR-set-test=1", cookie_store_->entries()[2].cookie_line); |
| 793 | 769 |
| 794 CloseWebSocketJob(); | 770 CloseWebSocketJob(); |
| 795 } | 771 } |
| 796 | 772 |
| 797 void WebSocketJobTest::TestHandshakeWithCookieButNotAllowed() { | 773 void WebSocketJobTest::TestHandshakeWithCookieButNotAllowed() { |
| 798 GURL url("ws://example.com/demo"); | 774 GURL url("ws://example.com/demo"); |
| 799 GURL cookieUrl("http://example.com/demo"); | 775 GURL cookieUrl("http://example.com/demo"); |
| 800 CookieOptions cookie_options; | 776 CookieOptions cookie_options; |
| 801 cookie_store_->SetCookieWithOptions( | 777 cookie_store_->SetCookieWithOptions(cookieUrl, "CR-test=1", cookie_options); |
| 802 cookieUrl, "CR-test=1", cookie_options); | |
| 803 cookie_options.set_include_httponly(); | 778 cookie_options.set_include_httponly(); |
| 804 cookie_store_->SetCookieWithOptions( | 779 cookie_store_->SetCookieWithOptions( |
| 805 cookieUrl, "CR-test-httponly=1", cookie_options); | 780 cookieUrl, "CR-test-httponly=1", cookie_options); |
| 806 | 781 |
| 807 MockSocketStreamDelegate delegate; | 782 MockSocketStreamDelegate delegate; |
| 808 delegate.set_allow_all_cookies(false); | 783 delegate.set_allow_all_cookies(false); |
| 809 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); | 784 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); |
| 810 SkipToConnecting(); | 785 SkipToConnecting(); |
| 811 | 786 |
| 812 bool sent = websocket_->SendData(kHandshakeRequestWithCookie, | 787 bool sent = websocket_->SendData(kHandshakeRequestWithCookie, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 830 EXPECT_EQ("CR-test=1", cookie_store_->entries()[0].cookie_line); | 805 EXPECT_EQ("CR-test=1", cookie_store_->entries()[0].cookie_line); |
| 831 EXPECT_EQ(cookieUrl, cookie_store_->entries()[1].url); | 806 EXPECT_EQ(cookieUrl, cookie_store_->entries()[1].url); |
| 832 EXPECT_EQ("CR-test-httponly=1", cookie_store_->entries()[1].cookie_line); | 807 EXPECT_EQ("CR-test-httponly=1", cookie_store_->entries()[1].cookie_line); |
| 833 | 808 |
| 834 CloseWebSocketJob(); | 809 CloseWebSocketJob(); |
| 835 } | 810 } |
| 836 | 811 |
| 837 void WebSocketJobTest::TestHSTSUpgrade() { | 812 void WebSocketJobTest::TestHSTSUpgrade() { |
| 838 GURL url("ws://upgrademe.com/"); | 813 GURL url("ws://upgrademe.com/"); |
| 839 MockSocketStreamDelegate delegate; | 814 MockSocketStreamDelegate delegate; |
| 840 scoped_refptr<SocketStreamJob> job = | 815 scoped_refptr<SocketStreamJob> job = SocketStreamJob::CreateSocketStreamJob( |
| 841 SocketStreamJob::CreateSocketStreamJob( | 816 url, |
| 842 url, &delegate, context_->transport_security_state(), | 817 &delegate, |
| 843 context_->ssl_config_service(), NULL, NULL); | 818 context_->transport_security_state(), |
| 819 context_->ssl_config_service(), |
| 820 NULL, |
| 821 NULL); |
| 844 EXPECT_TRUE(GetSocket(job.get())->is_secure()); | 822 EXPECT_TRUE(GetSocket(job.get())->is_secure()); |
| 845 job->DetachDelegate(); | 823 job->DetachDelegate(); |
| 846 | 824 |
| 847 url = GURL("ws://donotupgrademe.com/"); | 825 url = GURL("ws://donotupgrademe.com/"); |
| 848 job = SocketStreamJob::CreateSocketStreamJob( | 826 job = SocketStreamJob::CreateSocketStreamJob( |
| 849 url, &delegate, context_->transport_security_state(), | 827 url, |
| 850 context_->ssl_config_service(), NULL, NULL); | 828 &delegate, |
| 829 context_->transport_security_state(), |
| 830 context_->ssl_config_service(), |
| 831 NULL, |
| 832 NULL); |
| 851 EXPECT_FALSE(GetSocket(job.get())->is_secure()); | 833 EXPECT_FALSE(GetSocket(job.get())->is_secure()); |
| 852 job->DetachDelegate(); | 834 job->DetachDelegate(); |
| 853 } | 835 } |
| 854 | 836 |
| 855 void WebSocketJobTest::TestInvalidSendData() { | 837 void WebSocketJobTest::TestInvalidSendData() { |
| 856 GURL url("ws://example.com/demo"); | 838 GURL url("ws://example.com/demo"); |
| 857 MockSocketStreamDelegate delegate; | 839 MockSocketStreamDelegate delegate; |
| 858 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); | 840 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); |
| 859 SkipToConnecting(); | 841 SkipToConnecting(); |
| 860 | 842 |
| 861 DoSendRequest(); | 843 DoSendRequest(); |
| 862 // We assume request is sent in one data chunk (from WebKit) | 844 // We assume request is sent in one data chunk (from WebKit) |
| 863 // We don't support streaming request. | 845 // We don't support streaming request. |
| 864 base::MessageLoop::current()->RunUntilIdle(); | 846 base::MessageLoop::current()->RunUntilIdle(); |
| 865 EXPECT_EQ(kHandshakeRequestWithoutCookie, sent_data()); | 847 EXPECT_EQ(kHandshakeRequestWithoutCookie, sent_data()); |
| 866 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); | 848 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); |
| 867 websocket_->OnSentData(socket_.get(), | 849 websocket_->OnSentData(socket_.get(), kHandshakeRequestWithoutCookieLength); |
| 868 kHandshakeRequestWithoutCookieLength); | |
| 869 EXPECT_EQ(kHandshakeRequestWithoutCookieLength, delegate.amount_sent()); | 850 EXPECT_EQ(kHandshakeRequestWithoutCookieLength, delegate.amount_sent()); |
| 870 | 851 |
| 871 // We could not send any data until connection is established. | 852 // We could not send any data until connection is established. |
| 872 bool sent = websocket_->SendData(kHandshakeRequestWithoutCookie, | 853 bool sent = websocket_->SendData(kHandshakeRequestWithoutCookie, |
| 873 kHandshakeRequestWithoutCookieLength); | 854 kHandshakeRequestWithoutCookieLength); |
| 874 EXPECT_FALSE(sent); | 855 EXPECT_FALSE(sent); |
| 875 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); | 856 EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState()); |
| 876 CloseWebSocketJob(); | 857 CloseWebSocketJob(); |
| 877 } | 858 } |
| 878 | 859 |
| 879 // Following tests verify cooperation between WebSocketJob and SocketStream. | 860 // Following tests verify cooperation between WebSocketJob and SocketStream. |
| 880 // Other former tests use MockSocketStream as SocketStream, so we could not | 861 // Other former tests use MockSocketStream as SocketStream, so we could not |
| 881 // check SocketStream behavior. | 862 // check SocketStream behavior. |
| 882 // OrderedSocketData provide socket level verifiation by checking out-going | 863 // OrderedSocketData provide socket level verifiation by checking out-going |
| 883 // packets in comparison with the MockWrite array and emulating in-coming | 864 // packets in comparison with the MockWrite array and emulating in-coming |
| 884 // packets with MockRead array. | 865 // packets with MockRead array. |
| 885 | 866 |
| 886 void WebSocketJobTest::TestConnectByWebSocket( | 867 void WebSocketJobTest::TestConnectByWebSocket(ThrottlingOption throttling) { |
| 887 ThrottlingOption throttling) { | |
| 888 // This is a test for verifying cooperation between WebSocketJob and | 868 // This is a test for verifying cooperation between WebSocketJob and |
| 889 // SocketStream. If |throttling| was |THROTTLING_OFF|, it test basic | 869 // SocketStream. If |throttling| was |THROTTLING_OFF|, it test basic |
| 890 // situation. If |throttling| was |THROTTLING_ON|, throttling limits the | 870 // situation. If |throttling| was |THROTTLING_ON|, throttling limits the |
| 891 // latter connection. | 871 // latter connection. |
| 892 MockWrite writes[] = { | 872 MockWrite writes[] = {MockWrite(ASYNC, |
| 893 MockWrite(ASYNC, | 873 kHandshakeRequestWithoutCookie, |
| 894 kHandshakeRequestWithoutCookie, | 874 kHandshakeRequestWithoutCookieLength, |
| 895 kHandshakeRequestWithoutCookieLength, | 875 1), |
| 896 1), | 876 MockWrite(ASYNC, kDataHello, kDataHelloLength, 3)}; |
| 897 MockWrite(ASYNC, | |
| 898 kDataHello, | |
| 899 kDataHelloLength, | |
| 900 3) | |
| 901 }; | |
| 902 MockRead reads[] = { | 877 MockRead reads[] = { |
| 903 MockRead(ASYNC, | 878 MockRead(ASYNC, |
| 904 kHandshakeResponseWithoutCookie, | 879 kHandshakeResponseWithoutCookie, |
| 905 kHandshakeResponseWithoutCookieLength, | 880 kHandshakeResponseWithoutCookieLength, |
| 906 2), | 881 2), |
| 907 MockRead(ASYNC, | 882 MockRead(ASYNC, kDataWorld, kDataWorldLength, 4), |
| 908 kDataWorld, | 883 MockRead(SYNCHRONOUS, 0, 5) // EOF |
| 909 kDataWorldLength, | |
| 910 4), | |
| 911 MockRead(SYNCHRONOUS, 0, 5) // EOF | |
| 912 }; | 884 }; |
| 913 data_.reset(new OrderedSocketData( | 885 data_.reset(new OrderedSocketData( |
| 914 reads, arraysize(reads), writes, arraysize(writes))); | 886 reads, arraysize(reads), writes, arraysize(writes))); |
| 915 | 887 |
| 916 GURL url("ws://example.com/demo"); | 888 GURL url("ws://example.com/demo"); |
| 917 MockSocketStreamDelegate delegate; | 889 MockSocketStreamDelegate delegate; |
| 918 WebSocketJobTest* test = this; | 890 WebSocketJobTest* test = this; |
| 919 if (throttling == THROTTLING_ON) | 891 if (throttling == THROTTLING_ON) |
| 920 delegate.SetOnStartOpenConnection( | 892 delegate.SetOnStartOpenConnection( |
| 921 base::Bind(&WebSocketJobTest::DoSync, base::Unretained(test))); | 893 base::Bind(&WebSocketJobTest::DoSync, base::Unretained(test))); |
| 922 delegate.SetOnConnected( | 894 delegate.SetOnConnected( |
| 923 base::Bind(&WebSocketJobTest::DoSendRequest, | 895 base::Bind(&WebSocketJobTest::DoSendRequest, base::Unretained(test))); |
| 924 base::Unretained(test))); | |
| 925 delegate.SetOnReceivedData( | 896 delegate.SetOnReceivedData( |
| 926 base::Bind(&WebSocketJobTest::DoSendData, base::Unretained(test))); | 897 base::Bind(&WebSocketJobTest::DoSendData, base::Unretained(test))); |
| 927 delegate.SetOnClose( | 898 delegate.SetOnClose( |
| 928 base::Bind(&WebSocketJobTest::DoSync, base::Unretained(test))); | 899 base::Bind(&WebSocketJobTest::DoSync, base::Unretained(test))); |
| 929 InitWebSocketJob(url, &delegate, STREAM_SOCKET); | 900 InitWebSocketJob(url, &delegate, STREAM_SOCKET); |
| 930 | 901 |
| 931 scoped_refptr<WebSocketJob> block_websocket; | 902 scoped_refptr<WebSocketJob> block_websocket; |
| 932 if (throttling == THROTTLING_ON) { | 903 if (throttling == THROTTLING_ON) { |
| 933 // Create former WebSocket object which obstructs the latter one. | 904 // Create former WebSocket object which obstructs the latter one. |
| 934 block_websocket = new WebSocketJob(NULL); | 905 block_websocket = new WebSocketJob(NULL); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 949 WebSocketThrottle::GetInstance()->RemoveFromQueue(block_websocket.get()); | 920 WebSocketThrottle::GetInstance()->RemoveFromQueue(block_websocket.get()); |
| 950 block_websocket = NULL; | 921 block_websocket = NULL; |
| 951 } | 922 } |
| 952 | 923 |
| 953 EXPECT_EQ(OK, WaitForResult()); | 924 EXPECT_EQ(OK, WaitForResult()); |
| 954 EXPECT_TRUE(data_->at_read_eof()); | 925 EXPECT_TRUE(data_->at_read_eof()); |
| 955 EXPECT_TRUE(data_->at_write_eof()); | 926 EXPECT_TRUE(data_->at_write_eof()); |
| 956 EXPECT_EQ(WebSocketJob::CLOSED, GetWebSocketJobState()); | 927 EXPECT_EQ(WebSocketJob::CLOSED, GetWebSocketJobState()); |
| 957 } | 928 } |
| 958 | 929 |
| 959 void WebSocketJobTest::TestConnectBySpdy( | 930 void WebSocketJobTest::TestConnectBySpdy(SpdyOption spdy, |
| 960 SpdyOption spdy, ThrottlingOption throttling) { | 931 ThrottlingOption throttling) { |
| 961 // This is a test for verifying cooperation between WebSocketJob and | 932 // This is a test for verifying cooperation between WebSocketJob and |
| 962 // SocketStream in the situation we have SPDY session to the server. If | 933 // SocketStream in the situation we have SPDY session to the server. If |
| 963 // |throttling| was |THROTTLING_ON|, throttling limits the latter connection. | 934 // |throttling| was |THROTTLING_ON|, throttling limits the latter connection. |
| 964 // If you enabled spdy, you should specify |spdy| as |SPDY_ON|. Expected | 935 // If you enabled spdy, you should specify |spdy| as |SPDY_ON|. Expected |
| 965 // results depend on its configuration. | 936 // results depend on its configuration. |
| 966 MockWrite writes_websocket[] = { | 937 MockWrite writes_websocket[] = { |
| 967 MockWrite(ASYNC, | 938 MockWrite(ASYNC, |
| 968 kHandshakeRequestWithoutCookie, | 939 kHandshakeRequestWithoutCookie, |
| 969 kHandshakeRequestWithoutCookieLength, | 940 kHandshakeRequestWithoutCookieLength, |
| 970 1), | 941 1), |
| 971 MockWrite(ASYNC, | 942 MockWrite(ASYNC, kDataHello, kDataHelloLength, 3)}; |
| 972 kDataHello, | |
| 973 kDataHelloLength, | |
| 974 3) | |
| 975 }; | |
| 976 MockRead reads_websocket[] = { | 943 MockRead reads_websocket[] = { |
| 977 MockRead(ASYNC, | 944 MockRead(ASYNC, |
| 978 kHandshakeResponseWithoutCookie, | 945 kHandshakeResponseWithoutCookie, |
| 979 kHandshakeResponseWithoutCookieLength, | 946 kHandshakeResponseWithoutCookieLength, |
| 980 2), | 947 2), |
| 981 MockRead(ASYNC, | 948 MockRead(ASYNC, kDataWorld, kDataWorldLength, 4), |
| 982 kDataWorld, | 949 MockRead(SYNCHRONOUS, 0, 5) // EOF |
| 983 kDataWorldLength, | |
| 984 4), | |
| 985 MockRead(SYNCHRONOUS, 0, 5) // EOF | |
| 986 }; | 950 }; |
| 987 | 951 |
| 988 scoped_ptr<SpdyHeaderBlock> request_headers(new SpdyHeaderBlock()); | 952 scoped_ptr<SpdyHeaderBlock> request_headers(new SpdyHeaderBlock()); |
| 989 spdy_util_.SetHeader("path", "/demo", request_headers.get()); | 953 spdy_util_.SetHeader("path", "/demo", request_headers.get()); |
| 990 spdy_util_.SetHeader("version", "WebSocket/13", request_headers.get()); | 954 spdy_util_.SetHeader("version", "WebSocket/13", request_headers.get()); |
| 991 spdy_util_.SetHeader("scheme", "ws", request_headers.get()); | 955 spdy_util_.SetHeader("scheme", "ws", request_headers.get()); |
| 992 spdy_util_.SetHeader("host", "example.com", request_headers.get()); | 956 spdy_util_.SetHeader("host", "example.com", request_headers.get()); |
| 993 spdy_util_.SetHeader("origin", "http://example.com", request_headers.get()); | 957 spdy_util_.SetHeader("origin", "http://example.com", request_headers.get()); |
| 994 spdy_util_.SetHeader("sec-websocket-protocol", "sample", | 958 spdy_util_.SetHeader( |
| 995 request_headers.get()); | 959 "sec-websocket-protocol", "sample", request_headers.get()); |
| 996 | 960 |
| 997 scoped_ptr<SpdyHeaderBlock> response_headers(new SpdyHeaderBlock()); | 961 scoped_ptr<SpdyHeaderBlock> response_headers(new SpdyHeaderBlock()); |
| 998 spdy_util_.SetHeader("status", "101 Switching Protocols", | 962 spdy_util_.SetHeader( |
| 999 response_headers.get()); | 963 "status", "101 Switching Protocols", response_headers.get()); |
| 1000 spdy_util_.SetHeader("sec-websocket-protocol", "sample", | 964 spdy_util_.SetHeader( |
| 1001 response_headers.get()); | 965 "sec-websocket-protocol", "sample", response_headers.get()); |
| 1002 | 966 |
| 1003 const SpdyStreamId kStreamId = 1; | 967 const SpdyStreamId kStreamId = 1; |
| 1004 scoped_ptr<SpdyFrame> request_frame( | 968 scoped_ptr<SpdyFrame> request_frame( |
| 1005 spdy_util_.ConstructSpdyWebSocketHandshakeRequestFrame( | 969 spdy_util_.ConstructSpdyWebSocketHandshakeRequestFrame( |
| 1006 request_headers.Pass(), | 970 request_headers.Pass(), kStreamId, MEDIUM)); |
| 1007 kStreamId, | |
| 1008 MEDIUM)); | |
| 1009 scoped_ptr<SpdyFrame> response_frame( | 971 scoped_ptr<SpdyFrame> response_frame( |
| 1010 spdy_util_.ConstructSpdyWebSocketHandshakeResponseFrame( | 972 spdy_util_.ConstructSpdyWebSocketHandshakeResponseFrame( |
| 1011 response_headers.Pass(), | 973 response_headers.Pass(), kStreamId, MEDIUM)); |
| 1012 kStreamId, | |
| 1013 MEDIUM)); | |
| 1014 scoped_ptr<SpdyFrame> data_hello_frame( | 974 scoped_ptr<SpdyFrame> data_hello_frame( |
| 1015 spdy_util_.ConstructSpdyWebSocketDataFrame( | 975 spdy_util_.ConstructSpdyWebSocketDataFrame( |
| 1016 kDataHello, | 976 kDataHello, kDataHelloLength, kStreamId, false)); |
| 1017 kDataHelloLength, | |
| 1018 kStreamId, | |
| 1019 false)); | |
| 1020 scoped_ptr<SpdyFrame> data_world_frame( | 977 scoped_ptr<SpdyFrame> data_world_frame( |
| 1021 spdy_util_.ConstructSpdyWebSocketDataFrame( | 978 spdy_util_.ConstructSpdyWebSocketDataFrame( |
| 1022 kDataWorld, | 979 kDataWorld, kDataWorldLength, kStreamId, false)); |
| 1023 kDataWorldLength, | |
| 1024 kStreamId, | |
| 1025 false)); | |
| 1026 MockWrite writes_spdy[] = { | 980 MockWrite writes_spdy[] = { |
| 1027 CreateMockWrite(*request_frame.get(), 1), | 981 CreateMockWrite(*request_frame.get(), 1), |
| 1028 CreateMockWrite(*data_hello_frame.get(), 3), | 982 CreateMockWrite(*data_hello_frame.get(), 3), |
| 1029 }; | 983 }; |
| 1030 MockRead reads_spdy[] = { | 984 MockRead reads_spdy[] = { |
| 1031 CreateMockRead(*response_frame.get(), 2), | 985 CreateMockRead(*response_frame.get(), 2), |
| 1032 CreateMockRead(*data_world_frame.get(), 4), | 986 CreateMockRead(*data_world_frame.get(), 4), |
| 1033 MockRead(SYNCHRONOUS, 0, 5) // EOF | 987 MockRead(SYNCHRONOUS, 0, 5) // EOF |
| 1034 }; | 988 }; |
| 1035 | 989 |
| 1036 if (spdy == SPDY_ON) | 990 if (spdy == SPDY_ON) |
| 1037 data_.reset(new OrderedSocketData( | 991 data_.reset(new OrderedSocketData(reads_spdy, |
| 1038 reads_spdy, arraysize(reads_spdy), | 992 arraysize(reads_spdy), |
| 1039 writes_spdy, arraysize(writes_spdy))); | 993 writes_spdy, |
| 994 arraysize(writes_spdy))); |
| 1040 else | 995 else |
| 1041 data_.reset(new OrderedSocketData( | 996 data_.reset(new OrderedSocketData(reads_websocket, |
| 1042 reads_websocket, arraysize(reads_websocket), | 997 arraysize(reads_websocket), |
| 1043 writes_websocket, arraysize(writes_websocket))); | 998 writes_websocket, |
| 999 arraysize(writes_websocket))); |
| 1044 | 1000 |
| 1045 GURL url("ws://example.com/demo"); | 1001 GURL url("ws://example.com/demo"); |
| 1046 MockSocketStreamDelegate delegate; | 1002 MockSocketStreamDelegate delegate; |
| 1047 WebSocketJobTest* test = this; | 1003 WebSocketJobTest* test = this; |
| 1048 if (throttling == THROTTLING_ON) | 1004 if (throttling == THROTTLING_ON) |
| 1049 delegate.SetOnStartOpenConnection( | 1005 delegate.SetOnStartOpenConnection( |
| 1050 base::Bind(&WebSocketJobTest::DoSync, base::Unretained(test))); | 1006 base::Bind(&WebSocketJobTest::DoSync, base::Unretained(test))); |
| 1051 delegate.SetOnConnected( | 1007 delegate.SetOnConnected( |
| 1052 base::Bind(&WebSocketJobTest::DoSendRequest, | 1008 base::Bind(&WebSocketJobTest::DoSendRequest, base::Unretained(test))); |
| 1053 base::Unretained(test))); | |
| 1054 delegate.SetOnReceivedData( | 1009 delegate.SetOnReceivedData( |
| 1055 base::Bind(&WebSocketJobTest::DoSendData, base::Unretained(test))); | 1010 base::Bind(&WebSocketJobTest::DoSendData, base::Unretained(test))); |
| 1056 delegate.SetOnClose( | 1011 delegate.SetOnClose( |
| 1057 base::Bind(&WebSocketJobTest::DoSync, base::Unretained(test))); | 1012 base::Bind(&WebSocketJobTest::DoSync, base::Unretained(test))); |
| 1058 InitWebSocketJob(url, &delegate, STREAM_SPDY_WEBSOCKET); | 1013 InitWebSocketJob(url, &delegate, STREAM_SPDY_WEBSOCKET); |
| 1059 | 1014 |
| 1060 scoped_refptr<WebSocketJob> block_websocket; | 1015 scoped_refptr<WebSocketJob> block_websocket; |
| 1061 if (throttling == THROTTLING_ON) { | 1016 if (throttling == THROTTLING_ON) { |
| 1062 // Create former WebSocket object which obstructs the latter one. | 1017 // Create former WebSocket object which obstructs the latter one. |
| 1063 block_websocket = new WebSocketJob(NULL); | 1018 block_websocket = new WebSocketJob(NULL); |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1283 job()->Connect(); | 1238 job()->Connect(); |
| 1284 SetDeleteNext(); | 1239 SetDeleteNext(); |
| 1285 job()->OnReceivedData( | 1240 job()->OnReceivedData( |
| 1286 socket_.get(), kMinimalResponse, arraysize(kMinimalResponse) - 1); | 1241 socket_.get(), kMinimalResponse, arraysize(kMinimalResponse) - 1); |
| 1287 EXPECT_FALSE(job()); | 1242 EXPECT_FALSE(job()); |
| 1288 } | 1243 } |
| 1289 | 1244 |
| 1290 // TODO(toyoshim): Add tests to verify throttling, SPDY stream limitation. | 1245 // TODO(toyoshim): Add tests to verify throttling, SPDY stream limitation. |
| 1291 // TODO(toyoshim,yutak): Add tests to verify closing handshake. | 1246 // TODO(toyoshim,yutak): Add tests to verify closing handshake. |
| 1292 } // namespace net | 1247 } // namespace net |
| OLD | NEW |