Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: net/http/http_network_transaction_unittest.cc

Issue 1808001: Implement a 15 connection per proxy server limit. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Remove extra newline. Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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_network_transaction.h"
6
5 #include <math.h> // ceil 7 #include <math.h> // ceil
6 #include <vector> 8 #include <vector>
7 9
10 #include "base/basictypes.h"
8 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
9 #include "base/file_path.h" 12 #include "base/file_path.h"
10 #include "base/file_util.h" 13 #include "base/file_util.h"
11 #include "base/scoped_ptr.h" 14 #include "base/scoped_ptr.h"
12 #include "net/base/completion_callback.h" 15 #include "net/base/completion_callback.h"
13 #include "net/base/mock_host_resolver.h" 16 #include "net/base/mock_host_resolver.h"
14 #include "net/base/request_priority.h" 17 #include "net/base/request_priority.h"
15 #include "net/base/ssl_config_service_defaults.h" 18 #include "net/base/ssl_config_service_defaults.h"
16 #include "net/base/ssl_info.h" 19 #include "net/base/ssl_info.h"
17 #include "net/base/test_completion_callback.h" 20 #include "net/base/test_completion_callback.h"
18 #include "net/base/upload_data.h" 21 #include "net/base/upload_data.h"
19 #include "net/http/http_auth_handler_ntlm.h" 22 #include "net/http/http_auth_handler_ntlm.h"
20 #include "net/http/http_basic_stream.h" 23 #include "net/http/http_basic_stream.h"
21 #include "net/http/http_network_session.h" 24 #include "net/http/http_network_session.h"
22 #include "net/http/http_network_transaction.h"
23 #include "net/http/http_stream.h" 25 #include "net/http/http_stream.h"
24 #include "net/http/http_transaction_unittest.h" 26 #include "net/http/http_transaction_unittest.h"
25 #include "net/proxy/proxy_config_service_fixed.h" 27 #include "net/proxy/proxy_config_service_fixed.h"
26 #include "net/socket/client_socket_factory.h" 28 #include "net/socket/client_socket_factory.h"
27 #include "net/socket/socket_test_util.h" 29 #include "net/socket/socket_test_util.h"
28 #include "net/socket/ssl_client_socket.h" 30 #include "net/socket/ssl_client_socket.h"
29 #include "net/spdy/spdy_framer.h" 31 #include "net/spdy/spdy_framer.h"
30 #include "net/spdy/spdy_session.h" 32 #include "net/spdy/spdy_session.h"
31 #include "net/spdy/spdy_session_pool.h" 33 #include "net/spdy/spdy_session_pool.h"
32 #include "net/spdy/spdy_test_util.h" 34 #include "net/spdy/spdy_test_util.h"
33 #include "testing/gtest/include/gtest/gtest.h" 35 #include "testing/gtest/include/gtest/gtest.h"
34 #include "testing/platform_test.h" 36 #include "testing/platform_test.h"
35 37
36 //----------------------------------------------------------------------------- 38 //-----------------------------------------------------------------------------
37 39
38 namespace net { 40 namespace net {
39 41
42 class HttpNetworkSessionPeer {
43 public:
44 explicit HttpNetworkSessionPeer(
45 const scoped_refptr<HttpNetworkSession>& session)
46 : session_(session) {}
47
48 void SetTCPSocketPool(const scoped_refptr<TCPClientSocketPool>& pool) {
49 session_->tcp_socket_pool_ = pool;
50 }
51
52 void SetSocketPoolForSOCKSProxy(
53 const HostPortPair& socks_proxy,
54 const scoped_refptr<SOCKSClientSocketPool>& pool) {
55 session_->socks_socket_pool_[socks_proxy] = pool;
56 }
57
58 void SetSocketPoolForHTTPProxy(
59 const HostPortPair& http_proxy,
60 const scoped_refptr<TCPClientSocketPool>& pool) {
61 session_->http_proxy_socket_pool_[http_proxy] = pool;
62 }
63
64 private:
65 const scoped_refptr<HttpNetworkSession> session_;
66
67 DISALLOW_COPY_AND_ASSIGN(HttpNetworkSessionPeer);
68 };
69
40 // Helper to manage the lifetimes of the dependencies for a 70 // Helper to manage the lifetimes of the dependencies for a
41 // HttpNetworkTransaction. 71 // HttpNetworkTransaction.
42 class SessionDependencies { 72 class SessionDependencies {
43 public: 73 public:
44 // Default set of dependencies -- "null" proxy service. 74 // Default set of dependencies -- "null" proxy service.
45 SessionDependencies() 75 SessionDependencies()
46 : host_resolver(new MockHostResolver), 76 : host_resolver(new MockHostResolver),
47 proxy_service(ProxyService::CreateNull()), 77 proxy_service(ProxyService::CreateNull()),
48 ssl_config_service(new SSLConfigServiceDefaults), 78 ssl_config_service(new SSLConfigServiceDefaults),
49 http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()), 79 http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()),
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 for (size_t i = 0; i < n; ++i) { 213 for (size_t i = 0; i < n; ++i) {
184 output[i] = bytes[current_byte++]; 214 output[i] = bytes[current_byte++];
185 current_byte %= arraysize(bytes); 215 current_byte %= arraysize(bytes);
186 } 216 }
187 } 217 }
188 218
189 std::string MockGetHostName() { 219 std::string MockGetHostName() {
190 return "WTC-WIN7"; 220 return "WTC-WIN7";
191 } 221 }
192 222
193 template<typename EmulatedClientSocketPool, typename SocketSourceType> 223 template<typename EmulatedClientSocketPool>
194 class CaptureGroupNameSocketPool : public EmulatedClientSocketPool { 224 class CaptureGroupNameSocketPool : public EmulatedClientSocketPool {
195 public: 225 public:
196 CaptureGroupNameSocketPool(HttpNetworkSession* session, 226 CaptureGroupNameSocketPool(HttpNetworkSession* session)
197 SocketSourceType* socket_source)
198 : EmulatedClientSocketPool(0, 0, "CaptureGroupNameTestPool", 227 : EmulatedClientSocketPool(0, 0, "CaptureGroupNameTestPool",
199 session->host_resolver(), socket_source, 228 session->host_resolver(), NULL,
200 NULL) {} 229 NULL) {}
201 const std::string last_group_name_received() const { 230 const std::string last_group_name_received() const {
202 return last_group_name_; 231 return last_group_name_;
203 } 232 }
204 233
205 virtual int RequestSocket(const std::string& group_name, 234 virtual int RequestSocket(const std::string& group_name,
206 const void* socket_params, 235 const void* socket_params,
207 RequestPriority priority, 236 RequestPriority priority,
208 ClientSocketHandle* handle, 237 ClientSocketHandle* handle,
209 CompletionCallback* callback, 238 CompletionCallback* callback,
(...skipping 20 matching lines...) Expand all
230 return LOAD_STATE_IDLE; 259 return LOAD_STATE_IDLE;
231 } 260 }
232 virtual base::TimeDelta ConnectionTimeout() const { 261 virtual base::TimeDelta ConnectionTimeout() const {
233 return base::TimeDelta(); 262 return base::TimeDelta();
234 } 263 }
235 264
236 private: 265 private:
237 std::string last_group_name_; 266 std::string last_group_name_;
238 }; 267 };
239 268
240 typedef CaptureGroupNameSocketPool<TCPClientSocketPool, ClientSocketFactory> 269 typedef CaptureGroupNameSocketPool<TCPClientSocketPool>
241 CaptureGroupNameTCPSocketPool; 270 CaptureGroupNameTCPSocketPool;
242 typedef CaptureGroupNameSocketPool<SOCKSClientSocketPool, TCPClientSocketPool> 271 typedef CaptureGroupNameSocketPool<SOCKSClientSocketPool>
243 CaptureGroupNameSOCKSSocketPool; 272 CaptureGroupNameSOCKSSocketPool;
244 //----------------------------------------------------------------------------- 273 //-----------------------------------------------------------------------------
245 274
246 TEST_F(HttpNetworkTransactionTest, Basic) { 275 TEST_F(HttpNetworkTransactionTest, Basic) {
247 SessionDependencies session_deps; 276 SessionDependencies session_deps;
248 scoped_ptr<HttpTransaction> trans( 277 scoped_ptr<HttpTransaction> trans(
249 new HttpNetworkTransaction(CreateSession(&session_deps))); 278 new HttpNetworkTransaction(CreateSession(&session_deps)));
250 } 279 }
251 280
252 TEST_F(HttpNetworkTransactionTest, SimpleGET) { 281 TEST_F(HttpNetworkTransactionTest, SimpleGET) {
(...skipping 3413 matching lines...) Expand 10 before | Expand all | Expand 10 after
3666 const HttpResponseInfo* response = trans->GetResponseInfo(); 3695 const HttpResponseInfo* response = trans->GetResponseInfo();
3667 EXPECT_FALSE(response == NULL); 3696 EXPECT_FALSE(response == NULL);
3668 3697
3669 std::string response_text; 3698 std::string response_text;
3670 rv = ReadTransaction(trans.get(), &response_text); 3699 rv = ReadTransaction(trans.get(), &response_text);
3671 EXPECT_EQ(OK, rv); 3700 EXPECT_EQ(OK, rv);
3672 EXPECT_EQ("Payload", response_text); 3701 EXPECT_EQ("Payload", response_text);
3673 } 3702 }
3674 3703
3675 // Tests that for connection endpoints the group names are correctly set. 3704 // Tests that for connection endpoints the group names are correctly set.
3676 TEST_F(HttpNetworkTransactionTest, GroupNameForProxyConnections) { 3705
3677 const struct { 3706 struct GroupNameTest {
3678 const std::string proxy_server; 3707 std::string proxy_server;
3679 const std::string url; 3708 std::string url;
3680 const std::string expected_group_name; 3709 std::string expected_group_name;
3681 } tests[] = { 3710 };
3711
3712 scoped_refptr<HttpNetworkSession> SetupSessionForGroupNameTests(
3713 const std::string& proxy_server) {
3714 SessionDependencies session_deps(CreateFixedProxyService(proxy_server));
3715 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
3716
3717 HttpAlternateProtocols* alternate_protocols =
3718 session->mutable_alternate_protocols();
3719 alternate_protocols->SetAlternateProtocolFor(
3720 HostPortPair("host.with.alternate", 80), 443,
3721 HttpAlternateProtocols::NPN_SPDY_1);
3722
3723 return session;
3724 }
3725
3726 int GroupNameTransactionHelper(
3727 const std::string& url,
3728 const scoped_refptr<HttpNetworkSession>& session) {
3729 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session));
3730
3731 HttpRequestInfo request;
3732 request.method = "GET";
3733 request.url = GURL(url);
3734 request.load_flags = 0;
3735
3736 TestCompletionCallback callback;
3737
3738 // We do not complete this request, the dtor will clean the transaction up.
3739 return trans->Start(&request, &callback, BoundNetLog());
3740 }
3741
3742 TEST_F(HttpNetworkTransactionTest, GroupNameForDirectConnections) {
3743 const GroupNameTest tests[] = {
3682 { 3744 {
3683 "", // no proxy (direct) 3745 "", // unused
3684 "http://www.google.com/direct", 3746 "http://www.google.com/direct",
3685 "www.google.com:80", 3747 "www.google.com:80",
3686 }, 3748 },
3687 { 3749 {
3688 "", // no proxy (direct) 3750 "", // unused
3689 "http://[2001:1418:13:1::25]/direct", 3751 "http://[2001:1418:13:1::25]/direct",
3690 "[2001:1418:13:1::25]:80", 3752 "[2001:1418:13:1::25]:80",
3691 }, 3753 },
3692 {
3693 "http_proxy",
3694 "http://www.google.com/http_proxy_normal",
3695 "proxy/http_proxy:80/",
3696 },
3697 {
3698 "socks4://socks_proxy:1080",
3699 "http://www.google.com/socks4_direct",
3700 "proxy/socks4://socks_proxy:1080/www.google.com:80",
3701 },
3702 3754
3703 // SSL Tests 3755 // SSL Tests
3704 { 3756 {
3705 "", 3757 "", // unused
3706 "https://www.google.com/direct_ssl", 3758 "https://www.google.com/direct_ssl",
3707 "www.google.com:443", 3759 "www.google.com:443",
3708 }, 3760 },
3709 { 3761 {
3710 "http_proxy", 3762 "", // unused
3711 "https://www.google.com/http_connect_ssl", 3763 "https://[2001:1418:13:1::25]/direct",
3712 "proxy/http_proxy:80/www.google.com:443", 3764 "[2001:1418:13:1::25]:443",
3713 }, 3765 },
3714 { 3766 {
3715 "socks4://socks_proxy:1080", 3767 "", // unused
3716 "https://www.google.com/socks4_ssl",
3717 "proxy/socks4://socks_proxy:1080/www.google.com:443",
3718 },
3719 {
3720 "", // no proxy (direct)
3721 "http://host.with.alternate/direct", 3768 "http://host.with.alternate/direct",
3722 "host.with.alternate:443", 3769 "host.with.alternate:443",
3723 }, 3770 },
3771 };
3724 3772
3725 // TODO(willchan): Uncomment these tests when they work. 3773 HttpNetworkTransaction::SetUseAlternateProtocols(true);
3774
3775 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
3776 scoped_refptr<HttpNetworkSession> session(
3777 SetupSessionForGroupNameTests(tests[i].proxy_server));
3778
3779 HttpNetworkSessionPeer peer(session);
3780 scoped_refptr<CaptureGroupNameTCPSocketPool> tcp_conn_pool(
3781 new CaptureGroupNameTCPSocketPool(session.get()));
3782 peer.SetTCPSocketPool(tcp_conn_pool);
3783
3784 EXPECT_EQ(ERR_IO_PENDING,
3785 GroupNameTransactionHelper(tests[i].url, session));
3786 EXPECT_EQ(tests[i].expected_group_name,
3787 tcp_conn_pool->last_group_name_received());
3788 }
3789
3790 HttpNetworkTransaction::SetUseAlternateProtocols(false);
3791 }
3792
3793 TEST_F(HttpNetworkTransactionTest, GroupNameForHTTPProxyConnections) {
3794 const GroupNameTest tests[] = {
3795 {
3796 "http_proxy",
3797 "http://www.google.com/http_proxy_normal",
3798 "www.google.com:80",
3799 },
3800
3801 // SSL Tests
3802 {
3803 "http_proxy",
3804 "https://www.google.com/http_connect_ssl",
3805 "www.google.com:443",
3806 },
3807
3808 // TODO(willchan): Uncomment these tests when they work.
3726 // { 3809 // {
3727 // "http_proxy", 3810 // "http_proxy",
3728 // "http://host.with.alternate/direct", 3811 // "http://host.with.alternate/direct",
3729 // "proxy/http_proxy:80/host.with.alternate:443", 3812 // "proxy/http_proxy:80/host.with.alternate:443",
3730 // }, 3813 // },
3814 };
3815
3816 HttpNetworkTransaction::SetUseAlternateProtocols(true);
3817
3818 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
3819 scoped_refptr<HttpNetworkSession> session(
3820 SetupSessionForGroupNameTests(tests[i].proxy_server));
3821
3822 HttpNetworkSessionPeer peer(session);
3823
3824 scoped_refptr<CaptureGroupNameTCPSocketPool> http_proxy_pool(
3825 new CaptureGroupNameTCPSocketPool(session.get()));
3826 peer.SetSocketPoolForHTTPProxy(
3827 HostPortPair("http_proxy", 80), http_proxy_pool);
3828
3829 EXPECT_EQ(ERR_IO_PENDING,
3830 GroupNameTransactionHelper(tests[i].url, session));
3831 EXPECT_EQ(tests[i].expected_group_name,
3832 http_proxy_pool->last_group_name_received());
3833 }
3834
3835 HttpNetworkTransaction::SetUseAlternateProtocols(false);
3836 }
3837
3838 TEST_F(HttpNetworkTransactionTest, GroupNameForSOCKSConnections) {
3839 const GroupNameTest tests[] = {
3840 {
3841 "socks4://socks_proxy:1080",
3842 "http://www.google.com/socks4_direct",
3843 "socks4/www.google.com:80",
3844 },
3845 {
3846 "socks5://socks_proxy:1080",
3847 "http://www.google.com/socks5_direct",
3848 "socks5/www.google.com:80",
3849 },
3850
3851 // SSL Tests
3852 {
3853 "socks4://socks_proxy:1080",
3854 "https://www.google.com/socks4_ssl",
3855 "socks4/www.google.com:443",
3856 },
3857 {
3858 "socks5://socks_proxy:1080",
3859 "https://www.google.com/socks5_ssl",
3860 "socks5/www.google.com:443",
3861 },
3862
3863 // TODO(willchan): Uncomment these tests when they work.
3731 // { 3864 // {
3732 // "socks4://socks_proxy:1080", 3865 // "socks4://socks_proxy:1080",
3733 // "http://host.with.alternate/direct", 3866 // "http://host.with.alternate/direct",
3734 // "proxy/socks4://socks_proxy:1080/host.with.alternate:443", 3867 // "proxy/socks4://socks_proxy:1080/host.with.alternate:443",
3735 // }, 3868 // },
3736 }; 3869 };
3737 3870
3738 HttpNetworkTransaction::SetUseAlternateProtocols(true); 3871 HttpNetworkTransaction::SetUseAlternateProtocols(true);
3739 3872
3740 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 3873 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
3741 SessionDependencies session_deps( 3874 scoped_refptr<HttpNetworkSession> session(
3742 CreateFixedProxyService(tests[i].proxy_server)); 3875 SetupSessionForGroupNameTests(tests[i].proxy_server));
3876 HttpNetworkSessionPeer peer(session);
3743 3877
3744 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
3745
3746 HttpAlternateProtocols* alternate_protocols =
3747 session->mutable_alternate_protocols();
3748 alternate_protocols->SetAlternateProtocolFor(
3749 HostPortPair("host.with.alternate", 80), 443,
3750 HttpAlternateProtocols::NPN_SPDY_1);
3751
3752 scoped_refptr<CaptureGroupNameTCPSocketPool> tcp_conn_pool(
3753 new CaptureGroupNameTCPSocketPool(session.get(),
3754 session->socket_factory()));
3755 session->tcp_socket_pool_ = tcp_conn_pool.get();
3756 scoped_refptr<CaptureGroupNameSOCKSSocketPool> socks_conn_pool( 3878 scoped_refptr<CaptureGroupNameSOCKSSocketPool> socks_conn_pool(
3757 new CaptureGroupNameSOCKSSocketPool(session.get(), 3879 new CaptureGroupNameSOCKSSocketPool(session.get()));
3758 tcp_conn_pool.get())); 3880 peer.SetSocketPoolForSOCKSProxy(
3759 session->socks_socket_pool_ = socks_conn_pool.get(); 3881 HostPortPair("socks_proxy", 1080), socks_conn_pool);
3760 3882
3761 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); 3883 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session));
3762 3884
3763 HttpRequestInfo request; 3885 EXPECT_EQ(ERR_IO_PENDING,
3764 request.method = "GET"; 3886 GroupNameTransactionHelper(tests[i].url, session));
3765 request.url = GURL(tests[i].url); 3887 EXPECT_EQ(tests[i].expected_group_name,
3766 request.load_flags = 0; 3888 socks_conn_pool->last_group_name_received());
3767
3768 TestCompletionCallback callback;
3769
3770 // We do not complete this request, the dtor will clean the transaction up.
3771 EXPECT_EQ(ERR_IO_PENDING, trans->Start(&request, &callback, BoundNetLog()));
3772 std::string allgroups = tcp_conn_pool->last_group_name_received() +
3773 socks_conn_pool->last_group_name_received();
3774 EXPECT_EQ(tests[i].expected_group_name, allgroups);
3775 } 3889 }
3776 3890
3777 HttpNetworkTransaction::SetUseAlternateProtocols(false); 3891 HttpNetworkTransaction::SetUseAlternateProtocols(false);
3778 } 3892 }
3779 3893
3780 TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { 3894 TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) {
3781 SessionDependencies session_deps( 3895 SessionDependencies session_deps(
3782 CreateFixedProxyService("myproxy:70;foobar:80")); 3896 CreateFixedProxyService("myproxy:70;foobar:80"));
3783 3897
3784 // This simulates failure resolving all hostnames; that means we will fail 3898 // This simulates failure resolving all hostnames; that means we will fail
(...skipping 1426 matching lines...) Expand 10 before | Expand all | Expand 10 after
5211 ASSERT_TRUE(response != NULL); 5325 ASSERT_TRUE(response != NULL);
5212 ASSERT_TRUE(response->headers != NULL); 5326 ASSERT_TRUE(response->headers != NULL);
5213 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); 5327 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
5214 5328
5215 std::string response_data; 5329 std::string response_data;
5216 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); 5330 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data));
5217 EXPECT_EQ("ok.", response_data); 5331 EXPECT_EQ("ok.", response_data);
5218 } 5332 }
5219 5333
5220 } // namespace net 5334 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698