OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/http/http_stream_factory_impl.h" | 5 #include "net/http/http_stream_factory_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "net/base/net_log.h" | 10 #include "net/base/net_log.h" |
11 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
12 #include "net/cert/mock_cert_verifier.h" | 12 #include "net/cert/mock_cert_verifier.h" |
13 #include "net/dns/mock_host_resolver.h" | 13 #include "net/dns/mock_host_resolver.h" |
14 #include "net/http/http_auth_handler_factory.h" | 14 #include "net/http/http_auth_handler_factory.h" |
15 #include "net/http/http_network_session.h" | 15 #include "net/http/http_network_session.h" |
16 #include "net/http/http_network_session_peer.h" | 16 #include "net/http/http_network_session_peer.h" |
17 #include "net/http/http_request_info.h" | 17 #include "net/http/http_request_info.h" |
18 #include "net/http/http_server_properties_impl.h" | 18 #include "net/http/http_server_properties_impl.h" |
19 #include "net/http/http_stream.h" | 19 #include "net/http/http_stream.h" |
20 #include "net/proxy/proxy_info.h" | 20 #include "net/proxy/proxy_info.h" |
21 #include "net/proxy/proxy_service.h" | 21 #include "net/proxy/proxy_service.h" |
22 #include "net/socket/client_socket_handle.h" | |
22 #include "net/socket/mock_client_socket_pool_manager.h" | 23 #include "net/socket/mock_client_socket_pool_manager.h" |
23 #include "net/socket/socket_test_util.h" | 24 #include "net/socket/socket_test_util.h" |
24 #include "net/spdy/spdy_session.h" | 25 #include "net/spdy/spdy_session.h" |
25 #include "net/spdy/spdy_session_pool.h" | 26 #include "net/spdy/spdy_session_pool.h" |
27 #include "net/spdy/spdy_stream.h" | |
28 #include "net/spdy/spdy_test_util_spdy3.h" | |
29 #include "net/ssl/ssl_config_service.h" | |
26 #include "net/ssl/ssl_config_service_defaults.h" | 30 #include "net/ssl/ssl_config_service_defaults.h" |
31 #include "testing/gmock/include/gmock/gmock.h" | |
27 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
28 | 33 |
29 namespace net { | 34 namespace net { |
30 | 35 |
31 namespace { | 36 namespace { |
tyoshino (SeeGerritForStatus)
2013/05/13 11:04:44
blank line
yhirano
2013/05/13 11:45:50
Done.
| |
37 class HttpStreamFactorySpdyForcer { | |
38 public: | |
39 HttpStreamFactorySpdyForcer() { | |
40 HttpStreamFactory::set_force_spdy_over_ssl(true); | |
41 HttpStreamFactory::set_force_spdy_always(true); | |
42 } | |
43 ~HttpStreamFactorySpdyForcer() { | |
44 HttpStreamFactory::set_force_spdy_over_ssl(false); | |
45 HttpStreamFactory::set_force_spdy_always(false); | |
46 } | |
47 }; | |
48 | |
49 class MockSSLConfigService : public SSLConfigService { | |
50 public: | |
51 explicit MockSSLConfigService(const SSLConfig& config) : config_(config) {} | |
52 | |
53 // SSLConfigService implementation | |
54 virtual void GetSSLConfig(SSLConfig* config) OVERRIDE { | |
55 *config = config_; | |
56 } | |
57 | |
58 // Sets the SSLConfig to be returned by GetSSLConfig and processes any | |
59 // updates. | |
60 void SetSSLConfig(const SSLConfig& config) { | |
61 SSLConfig old_config = config_; | |
62 config_ = config; | |
63 ProcessConfigUpdate(old_config, config_); | |
64 } | |
65 | |
66 private: | |
67 virtual ~MockSSLConfigService() {} | |
68 | |
69 SSLConfig config_; | |
70 }; | |
71 | |
72 class MockSSLConfigServiceObserver : public SSLConfigService::Observer { | |
73 public: | |
74 MockSSLConfigServiceObserver() {} | |
75 virtual ~MockSSLConfigServiceObserver() {} | |
76 | |
77 MOCK_METHOD0(OnSSLConfigChanged, void()); | |
78 }; | |
32 | 79 |
33 class MockHttpStreamFactoryImpl : public HttpStreamFactoryImpl { | 80 class MockHttpStreamFactoryImpl : public HttpStreamFactoryImpl { |
34 public: | 81 public: |
35 MockHttpStreamFactoryImpl(HttpNetworkSession* session) | 82 explicit MockHttpStreamFactoryImpl(HttpNetworkSession* session) |
36 : HttpStreamFactoryImpl(session), | 83 : HttpStreamFactoryImpl(session), |
37 preconnect_done_(false), | 84 preconnect_done_(false), |
38 waiting_for_preconnect_(false) {} | 85 waiting_for_preconnect_(false) {} |
39 | 86 |
40 | 87 |
41 void WaitForPreconnects() { | 88 void WaitForPreconnects() { |
42 while (!preconnect_done_) { | 89 while (!preconnect_done_) { |
43 waiting_for_preconnect_ = true; | 90 waiting_for_preconnect_ = true; |
44 MessageLoop::current()->Run(); | 91 MessageLoop::current()->Run(); |
45 waiting_for_preconnect_ = false; | 92 waiting_for_preconnect_ = false; |
46 } | 93 } |
47 } | 94 } |
48 | 95 |
49 private: | 96 private: |
50 // HttpStreamFactoryImpl methods. | 97 // HttpStreamFactoryImpl methods. |
51 virtual void OnPreconnectsCompleteInternal() OVERRIDE { | 98 virtual void OnPreconnectsCompleteInternal() OVERRIDE { |
52 preconnect_done_ = true; | 99 preconnect_done_ = true; |
53 if (waiting_for_preconnect_) | 100 if (waiting_for_preconnect_) |
54 MessageLoop::current()->Quit(); | 101 MessageLoop::current()->Quit(); |
55 } | 102 } |
56 | 103 |
57 bool preconnect_done_; | 104 bool preconnect_done_; |
58 bool waiting_for_preconnect_; | 105 ; bool waiting_for_preconnect_; |
tyoshino (SeeGerritForStatus)
2013/05/13 11:04:44
typo?
yhirano
2013/05/13 11:45:50
Thanks, done.
| |
59 }; | 106 }; |
60 | 107 |
61 class StreamRequestWaiter : public HttpStreamRequest::Delegate { | 108 class StreamRequestWaiter : public HttpStreamRequest::Delegate { |
62 public: | 109 public: |
63 StreamRequestWaiter() | 110 StreamRequestWaiter() |
64 : waiting_for_stream_(false), | 111 : waiting_for_stream_(false), |
65 stream_done_(false) {} | 112 stream_done_(false) {} |
66 | 113 |
67 // HttpStreamRequest::Delegate | 114 // HttpStreamRequest::Delegate |
68 | 115 |
69 virtual void OnStreamReady( | 116 virtual void OnStreamReady( |
70 const SSLConfig& used_ssl_config, | 117 const SSLConfig& used_ssl_config, |
71 const ProxyInfo& used_proxy_info, | 118 const ProxyInfo& used_proxy_info, |
72 HttpStreamBase* stream) OVERRIDE { | 119 HttpStreamBase* stream) OVERRIDE { |
73 stream_done_ = true; | 120 stream_done_ = true; |
74 if (waiting_for_stream_) | 121 if (waiting_for_stream_) |
75 MessageLoop::current()->Quit(); | 122 MessageLoop::current()->Quit(); |
76 stream_.reset(stream); | 123 stream_.reset(stream); |
77 } | 124 } |
78 | 125 |
126 virtual void OnSocketReady( | |
127 const SSLConfig& used_ssl_config, | |
128 const ProxyInfo& used_proxy_info, | |
129 ClientSocketHandle* connection) OVERRIDE { | |
130 stream_done_ = true; | |
131 if (waiting_for_stream_) | |
132 MessageLoop::current()->Quit(); | |
133 connection_.reset(connection); | |
134 } | |
135 | |
136 virtual void OnSpdySessionReady( | |
137 const SSLConfig& used_ssl_config, | |
138 const ProxyInfo& used_proxy_info, | |
139 SpdySession* session) OVERRIDE { | |
140 stream_done_ = true; | |
141 if (waiting_for_stream_) | |
142 MessageLoop::current()->Quit(); | |
143 spdy_session_ = session; | |
144 } | |
145 | |
79 virtual void OnStreamFailed( | 146 virtual void OnStreamFailed( |
80 int status, | 147 int status, |
81 const SSLConfig& used_ssl_config) OVERRIDE {} | 148 const SSLConfig& used_ssl_config) OVERRIDE {} |
82 | 149 |
83 virtual void OnCertificateError( | 150 virtual void OnCertificateError( |
84 int status, | 151 int status, |
85 const SSLConfig& used_ssl_config, | 152 const SSLConfig& used_ssl_config, |
86 const SSLInfo& ssl_info) OVERRIDE {} | 153 const SSLInfo& ssl_info) OVERRIDE {} |
87 | 154 |
88 virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response, | 155 virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response, |
(...skipping 10 matching lines...) Expand all Loading... | |
99 HttpStreamBase* stream) OVERRIDE {} | 166 HttpStreamBase* stream) OVERRIDE {} |
100 | 167 |
101 void WaitForStream() { | 168 void WaitForStream() { |
102 while (!stream_done_) { | 169 while (!stream_done_) { |
103 waiting_for_stream_ = true; | 170 waiting_for_stream_ = true; |
104 MessageLoop::current()->Run(); | 171 MessageLoop::current()->Run(); |
105 waiting_for_stream_ = false; | 172 waiting_for_stream_ = false; |
106 } | 173 } |
107 } | 174 } |
108 | 175 |
176 public: | |
177 bool stream_done() const { return stream_done_; } | |
178 HttpStreamBase* stream() { return stream_.get(); } | |
179 SpdySession* spdy_session() {return spdy_session_.get(); } | |
180 ClientSocketHandle* connection() { return connection_.get(); } | |
181 | |
109 private: | 182 private: |
110 bool waiting_for_stream_; | 183 bool waiting_for_stream_; |
111 bool stream_done_; | 184 bool stream_done_; |
112 scoped_ptr<HttpStreamBase> stream_; | 185 scoped_ptr<HttpStreamBase> stream_; |
186 scoped_ptr<ClientSocketHandle> connection_; | |
187 scoped_refptr<SpdySession> spdy_session_; | |
113 | 188 |
114 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter); | 189 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter); |
115 }; | 190 }; |
116 | 191 |
117 struct SessionDependencies { | 192 struct SessionDependencies { |
118 // Custom proxy service dependency. | 193 // Custom proxy service dependency. |
119 explicit SessionDependencies(ProxyService* proxy_service) | 194 explicit SessionDependencies(ProxyService* proxy_service) |
120 : host_resolver(new MockHostResolver), | 195 : host_resolver(new MockHostResolver), |
121 cert_verifier(new MockCertVerifier), | 196 cert_verifier(new MockCertVerifier), |
122 proxy_service(proxy_service), | 197 proxy_service(proxy_service), |
123 ssl_config_service(new SSLConfigServiceDefaults), | |
124 http_auth_handler_factory( | 198 http_auth_handler_factory( |
125 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), | 199 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), |
126 net_log(NULL) {} | 200 net_log(NULL) { |
201 SSLConfig initial_config; | |
202 initial_config.rev_checking_enabled = false; | |
203 initial_config.false_start_enabled = false; | |
204 initial_config.unrestricted_ssl3_fallback_enabled = false; | |
205 initial_config.version_min = SSL_PROTOCOL_VERSION_SSL3; | |
206 initial_config.version_max = SSL_PROTOCOL_VERSION_TLS1_1; | |
207 ssl_config_service = new MockSSLConfigService(initial_config); | |
208 ssl_config_service = new SSLConfigServiceDefaults(); | |
209 } | |
127 | 210 |
128 scoped_ptr<MockHostResolverBase> host_resolver; | 211 scoped_ptr<MockHostResolverBase> host_resolver; |
129 scoped_ptr<CertVerifier> cert_verifier; | 212 scoped_ptr<CertVerifier> cert_verifier; |
130 scoped_ptr<ProxyService> proxy_service; | 213 scoped_ptr<ProxyService> proxy_service; |
131 scoped_refptr<SSLConfigService> ssl_config_service; | 214 scoped_refptr<SSLConfigService> ssl_config_service; |
132 MockClientSocketFactory socket_factory; | 215 MockClientSocketFactory socket_factory; |
133 scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; | 216 scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; |
134 HttpServerPropertiesImpl http_server_properties; | 217 HttpServerPropertiesImpl http_server_properties; |
135 NetLog* net_log; | 218 NetLog* net_log; |
136 }; | 219 }; |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
463 waiter.WaitForStream(); | 546 waiter.WaitForStream(); |
464 | 547 |
465 // The proxy that failed should now be known to the proxy_service as bad. | 548 // The proxy that failed should now be known to the proxy_service as bad. |
466 const ProxyRetryInfoMap& retry_info = | 549 const ProxyRetryInfoMap& retry_info = |
467 session->proxy_service()->proxy_retry_info(); | 550 session->proxy_service()->proxy_retry_info(); |
468 EXPECT_EQ(1u, retry_info.size()); | 551 EXPECT_EQ(1u, retry_info.size()); |
469 ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); | 552 ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); |
470 EXPECT_TRUE(iter != retry_info.end()); | 553 EXPECT_TRUE(iter != retry_info.end()); |
471 } | 554 } |
472 | 555 |
556 TEST(HttpStreamFactoryTest, RequestStream) { | |
557 SessionDependencies session_deps(ProxyService::CreateDirect()); | |
558 | |
559 StaticSocketDataProvider socket_data; | |
560 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
561 session_deps.socket_factory.AddSocketDataProvider(&socket_data); | |
562 | |
563 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | |
564 | |
565 // Now request a stream. It should succeed using the second proxy in the | |
566 // list. | |
567 HttpRequestInfo request_info; | |
568 request_info.method = "GET"; | |
569 request_info.url = GURL("http://www.google.com"); | |
570 request_info.load_flags = 0; | |
571 | |
572 SSLConfig ssl_config; | |
573 StreamRequestWaiter waiter; | |
574 scoped_ptr<HttpStreamRequest> request( | |
575 session->http_stream_factory()->RequestStream( | |
576 request_info, | |
577 DEFAULT_PRIORITY, | |
578 ssl_config, | |
579 ssl_config, | |
580 &waiter, | |
581 BoundNetLog())); | |
582 waiter.WaitForStream(); | |
583 EXPECT_TRUE(waiter.stream_done()); | |
584 EXPECT_TRUE(NULL == waiter.spdy_session()); | |
585 EXPECT_TRUE(NULL == waiter.connection()); | |
586 EXPECT_TRUE(NULL != waiter.stream()); | |
587 } | |
588 | |
589 TEST(HttpStreamFactoryTest, RequestSocket) { | |
590 SessionDependencies session_deps(ProxyService::CreateDirect()); | |
591 | |
592 StaticSocketDataProvider socket_data; | |
593 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
594 session_deps.socket_factory.AddSocketDataProvider(&socket_data); | |
595 | |
596 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | |
597 | |
598 // Now request a stream. It should succeed using the second proxy in the | |
599 // list. | |
600 HttpRequestInfo request_info; | |
601 request_info.method = "GET"; | |
602 request_info.url = GURL("ws://www.google.com"); | |
603 request_info.load_flags = 0; | |
604 | |
605 SSLConfig ssl_config; | |
606 StreamRequestWaiter waiter; | |
607 scoped_ptr<HttpStreamRequest> request( | |
608 session->http_stream_factory()->RequestStreamForWebSocket( | |
609 request_info, | |
610 DEFAULT_PRIORITY, | |
611 ssl_config, | |
612 ssl_config, | |
613 &waiter, | |
614 BoundNetLog())); | |
615 waiter.WaitForStream(); | |
616 EXPECT_TRUE(waiter.stream_done()); | |
617 EXPECT_TRUE(NULL == waiter.stream()); | |
618 EXPECT_TRUE(NULL == waiter.spdy_session()); | |
619 EXPECT_TRUE(NULL != waiter.connection()); | |
620 } | |
621 | |
622 TEST(HttpStreamFactoryTest, RequestSpdyHttpStream) { | |
623 HttpStreamFactorySpdyForcer use_spdy; | |
624 SpdySessionDependencies session_deps(kProtoSPDY3, | |
625 ProxyService::CreateDirect()); | |
626 | |
627 MockRead r(ASYNC, OK); | |
628 StaticSocketDataProvider socket_data(&r, 1, NULL, 0); | |
629 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
630 session_deps.socket_factory->AddSocketDataProvider(&socket_data); | |
631 | |
632 SSLSocketDataProvider ssl_socket_data(ASYNC, OK); | |
633 ssl_socket_data.protocol_negotiated = kProtoSPDY3; | |
634 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl_socket_data); | |
635 | |
636 HostPortPair host_port_pair("www.google.com", 80); | |
637 scoped_refptr<HttpNetworkSession> | |
638 session(SpdySessionDependencies::SpdyCreateSession(&session_deps)); | |
639 | |
640 // Now request a stream. It should succeed using the second proxy in the | |
641 // list. | |
642 HttpRequestInfo request_info; | |
643 request_info.method = "GET"; | |
644 request_info.url = GURL("http://www.google.com"); | |
645 request_info.load_flags = 0; | |
646 | |
647 SSLConfig ssl_config; | |
648 StreamRequestWaiter waiter; | |
649 scoped_ptr<HttpStreamRequest> request( | |
650 session->http_stream_factory()->RequestStream( | |
651 request_info, | |
652 DEFAULT_PRIORITY, | |
653 ssl_config, | |
654 ssl_config, | |
655 &waiter, | |
656 BoundNetLog())); | |
657 waiter.WaitForStream(); | |
658 EXPECT_TRUE(waiter.stream_done()); | |
659 EXPECT_TRUE(NULL == waiter.spdy_session()); | |
660 EXPECT_TRUE(NULL == waiter.connection()); | |
661 EXPECT_TRUE(NULL != waiter.stream()); | |
662 EXPECT_TRUE(waiter.stream()->IsSpdyHttpStream()); | |
663 } | |
664 | |
665 TEST(HttpStreamFactoryTest, RequestSpdySession) { | |
666 HttpStreamFactorySpdyForcer use_spdy; | |
667 SpdySessionDependencies session_deps(kProtoSPDY3, | |
668 ProxyService::CreateDirect()); | |
669 | |
670 MockRead r(ASYNC, OK); | |
671 StaticSocketDataProvider socket_data(&r, 1, NULL, 0); | |
672 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
673 session_deps.socket_factory->AddSocketDataProvider(&socket_data); | |
674 | |
675 SSLSocketDataProvider ssl_socket_data(ASYNC, OK); | |
676 ssl_socket_data.protocol_negotiated = kProtoSPDY3; | |
677 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl_socket_data); | |
678 | |
679 HostPortPair host_port_pair("www.google.com", 80); | |
680 scoped_refptr<HttpNetworkSession> | |
681 session(SpdySessionDependencies::SpdyCreateSession(&session_deps)); | |
682 | |
683 // Now request a stream. It should succeed using the second proxy in the | |
684 // list. | |
685 HttpRequestInfo request_info; | |
686 request_info.method = "GET"; | |
687 request_info.url = GURL("wss://www.google.com"); | |
688 request_info.load_flags = 0; | |
689 | |
690 SSLConfig ssl_config; | |
691 StreamRequestWaiter waiter; | |
692 scoped_ptr<HttpStreamRequest> request( | |
693 session->http_stream_factory()->RequestStreamForWebSocket( | |
694 request_info, | |
695 DEFAULT_PRIORITY, | |
696 ssl_config, | |
697 ssl_config, | |
698 &waiter, | |
699 BoundNetLog())); | |
700 waiter.WaitForStream(); | |
701 EXPECT_TRUE(waiter.stream_done()); | |
702 EXPECT_TRUE(NULL != waiter.spdy_session()); | |
703 EXPECT_TRUE(NULL == waiter.connection()); | |
704 EXPECT_TRUE(NULL == waiter.stream()); | |
705 } | |
706 | |
473 } // namespace | 707 } // namespace |
474 | 708 |
475 } // namespace net | 709 } // namespace net |
OLD | NEW |