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