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, SocketStream::Delegate* delegate, |
45 : SocketStream(url, delegate) {} | 45 URLRequestContext* context, CookieStore* cookie_store) |
| 46 : SocketStream(url, delegate, context, cookie_store) {} |
46 | 47 |
47 virtual void Connect() OVERRIDE {} | 48 virtual void Connect() OVERRIDE {} |
48 virtual bool SendData(const char* data, int len) OVERRIDE { | 49 virtual bool SendData(const char* data, int len) OVERRIDE { |
49 sent_data_ += std::string(data, len); | 50 sent_data_ += std::string(data, len); |
50 return true; | 51 return true; |
51 } | 52 } |
52 | 53 |
53 virtual void Close() OVERRIDE {} | 54 virtual void Close() OVERRIDE {} |
54 virtual void RestartWithAuth( | 55 virtual void RestartWithAuth( |
55 const AuthCredentials& credentials) OVERRIDE { | 56 const AuthCredentials& credentials) OVERRIDE { |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 SPDY_ON, | 358 SPDY_ON, |
358 }; | 359 }; |
359 void InitWebSocketJob(const GURL& url, | 360 void InitWebSocketJob(const GURL& url, |
360 MockSocketStreamDelegate* delegate, | 361 MockSocketStreamDelegate* delegate, |
361 StreamType stream_type) { | 362 StreamType stream_type) { |
362 DCHECK_NE(STREAM_INVALID, stream_type); | 363 DCHECK_NE(STREAM_INVALID, stream_type); |
363 stream_type_ = stream_type; | 364 stream_type_ = stream_type; |
364 websocket_ = new WebSocketJob(delegate); | 365 websocket_ = new WebSocketJob(delegate); |
365 | 366 |
366 if (stream_type == STREAM_MOCK_SOCKET) | 367 if (stream_type == STREAM_MOCK_SOCKET) |
367 socket_ = new MockSocketStream(url, websocket_.get()); | 368 socket_ = new MockSocketStream(url, websocket_.get(), context_.get(), |
| 369 NULL); |
368 | 370 |
369 if (stream_type == STREAM_SOCKET || stream_type == STREAM_SPDY_WEBSOCKET) { | 371 if (stream_type == STREAM_SOCKET || stream_type == STREAM_SPDY_WEBSOCKET) { |
370 if (stream_type == STREAM_SPDY_WEBSOCKET) { | 372 if (stream_type == STREAM_SPDY_WEBSOCKET) { |
371 http_factory_.reset( | 373 http_factory_.reset( |
372 new MockHttpTransactionFactory(GetParam(), data_.get())); | 374 new MockHttpTransactionFactory(GetParam(), data_.get())); |
373 context_->set_http_transaction_factory(http_factory_.get()); | 375 context_->set_http_transaction_factory(http_factory_.get()); |
374 } | 376 } |
375 | 377 |
376 ssl_config_service_ = new MockSSLConfigService(); | 378 ssl_config_service_ = new MockSSLConfigService(); |
377 context_->set_ssl_config_service(ssl_config_service_.get()); | 379 context_->set_ssl_config_service(ssl_config_service_.get()); |
378 proxy_service_.reset(ProxyService::CreateDirect()); | 380 proxy_service_.reset(ProxyService::CreateDirect()); |
379 context_->set_proxy_service(proxy_service_.get()); | 381 context_->set_proxy_service(proxy_service_.get()); |
380 host_resolver_.reset(new MockHostResolver); | 382 host_resolver_.reset(new MockHostResolver); |
381 context_->set_host_resolver(host_resolver_.get()); | 383 context_->set_host_resolver(host_resolver_.get()); |
382 | 384 |
383 socket_ = new SocketStream(url, websocket_.get()); | 385 socket_ = new SocketStream(url, websocket_.get(), context_.get(), NULL); |
384 socket_factory_.reset(new MockClientSocketFactory); | 386 socket_factory_.reset(new MockClientSocketFactory); |
385 DCHECK(data_.get()); | 387 DCHECK(data_.get()); |
386 socket_factory_->AddSocketDataProvider(data_.get()); | 388 socket_factory_->AddSocketDataProvider(data_.get()); |
387 socket_->SetClientSocketFactory(socket_factory_.get()); | 389 socket_->SetClientSocketFactory(socket_factory_.get()); |
388 } | 390 } |
389 | 391 |
390 websocket_->InitSocketStream(socket_.get()); | 392 websocket_->InitSocketStream(socket_.get()); |
391 websocket_->set_context(context_.get()); | |
392 // MockHostResolver resolves all hosts to 127.0.0.1; however, when we create | 393 // MockHostResolver resolves all hosts to 127.0.0.1; however, when we create |
393 // a WebSocketJob purely to block another one in a throttling test, we don't | 394 // a WebSocketJob purely to block another one in a throttling test, we don't |
394 // perform a real connect. In that case, the following address is used | 395 // perform a real connect. In that case, the following address is used |
395 // instead. | 396 // instead. |
396 IPAddressNumber ip; | 397 IPAddressNumber ip; |
397 ParseIPLiteralToNumber("127.0.0.1", &ip); | 398 ParseIPLiteralToNumber("127.0.0.1", &ip); |
398 websocket_->addresses_ = AddressList::CreateFromIPAddress(ip, 80); | 399 websocket_->addresses_ = AddressList::CreateFromIPAddress(ip, 80); |
399 } | 400 } |
400 void SkipToConnecting() { | 401 void SkipToConnecting() { |
401 websocket_->state_ = WebSocketJob::CONNECTING; | 402 websocket_->state_ = WebSocketJob::CONNECTING; |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 | 732 |
732 CloseWebSocketJob(); | 733 CloseWebSocketJob(); |
733 } | 734 } |
734 | 735 |
735 void WebSocketJobTest::TestHSTSUpgrade() { | 736 void WebSocketJobTest::TestHSTSUpgrade() { |
736 GURL url("ws://upgrademe.com/"); | 737 GURL url("ws://upgrademe.com/"); |
737 MockSocketStreamDelegate delegate; | 738 MockSocketStreamDelegate delegate; |
738 scoped_refptr<SocketStreamJob> job = | 739 scoped_refptr<SocketStreamJob> job = |
739 SocketStreamJob::CreateSocketStreamJob( | 740 SocketStreamJob::CreateSocketStreamJob( |
740 url, &delegate, context_->transport_security_state(), | 741 url, &delegate, context_->transport_security_state(), |
741 context_->ssl_config_service()); | 742 context_->ssl_config_service(), NULL, NULL); |
742 EXPECT_TRUE(GetSocket(job.get())->is_secure()); | 743 EXPECT_TRUE(GetSocket(job.get())->is_secure()); |
743 job->DetachDelegate(); | 744 job->DetachDelegate(); |
744 | 745 |
745 url = GURL("ws://donotupgrademe.com/"); | 746 url = GURL("ws://donotupgrademe.com/"); |
746 job = SocketStreamJob::CreateSocketStreamJob( | 747 job = SocketStreamJob::CreateSocketStreamJob( |
747 url, &delegate, context_->transport_security_state(), | 748 url, &delegate, context_->transport_security_state(), |
748 context_->ssl_config_service()); | 749 context_->ssl_config_service(), NULL, NULL); |
749 EXPECT_FALSE(GetSocket(job.get())->is_secure()); | 750 EXPECT_FALSE(GetSocket(job.get())->is_secure()); |
750 job->DetachDelegate(); | 751 job->DetachDelegate(); |
751 } | 752 } |
752 | 753 |
753 void WebSocketJobTest::TestInvalidSendData() { | 754 void WebSocketJobTest::TestInvalidSendData() { |
754 GURL url("ws://example.com/demo"); | 755 GURL url("ws://example.com/demo"); |
755 MockSocketStreamDelegate delegate; | 756 MockSocketStreamDelegate delegate; |
756 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); | 757 InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET); |
757 SkipToConnecting(); | 758 SkipToConnecting(); |
758 | 759 |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1111 } | 1112 } |
1112 | 1113 |
1113 TEST_P(WebSocketJobTest, ThrottlingSpdySpdyEnabled) { | 1114 TEST_P(WebSocketJobTest, ThrottlingSpdySpdyEnabled) { |
1114 WebSocketJob::set_websocket_over_spdy_enabled(true); | 1115 WebSocketJob::set_websocket_over_spdy_enabled(true); |
1115 TestConnectBySpdy(SPDY_ON, THROTTLING_ON); | 1116 TestConnectBySpdy(SPDY_ON, THROTTLING_ON); |
1116 } | 1117 } |
1117 | 1118 |
1118 // TODO(toyoshim): Add tests to verify throttling, SPDY stream limitation. | 1119 // TODO(toyoshim): Add tests to verify throttling, SPDY stream limitation. |
1119 // TODO(toyoshim,yutak): Add tests to verify closing handshake. | 1120 // TODO(toyoshim,yutak): Add tests to verify closing handshake. |
1120 } // namespace net | 1121 } // namespace net |
OLD | NEW |