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/socket/transport_client_socket_pool.h" | 5 #include "net/socket/transport_client_socket_pool.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 const int kMaxSockets = 32; | 33 const int kMaxSockets = 32; |
34 const int kMaxSocketsPerGroup = 6; | 34 const int kMaxSocketsPerGroup = 6; |
35 const RequestPriority kDefaultPriority = LOW; | 35 const RequestPriority kDefaultPriority = LOW; |
36 | 36 |
37 class TransportClientSocketPoolTest : public testing::Test { | 37 class TransportClientSocketPoolTest : public testing::Test { |
38 protected: | 38 protected: |
39 TransportClientSocketPoolTest() | 39 TransportClientSocketPoolTest() |
40 : connect_backup_jobs_enabled_( | 40 : connect_backup_jobs_enabled_( |
41 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true)), | 41 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true)), |
42 params_( | 42 params_( |
43 new TransportSocketParams(HostPortPair("www.google.com", 80), | 43 new TransportSocketParams( |
44 false, false, | 44 HostPortPair("www.google.com", 80), |
45 OnHostResolutionCallback())), | 45 false, |
| 46 false, |
| 47 OnHostResolutionCallback(), |
| 48 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)), |
46 histograms_(new ClientSocketPoolHistograms("TCPUnitTest")), | 49 histograms_(new ClientSocketPoolHistograms("TCPUnitTest")), |
47 host_resolver_(new MockHostResolver), | 50 host_resolver_(new MockHostResolver), |
48 client_socket_factory_(&net_log_), | 51 client_socket_factory_(&net_log_), |
49 pool_(kMaxSockets, | 52 pool_(kMaxSockets, |
50 kMaxSocketsPerGroup, | 53 kMaxSocketsPerGroup, |
51 histograms_.get(), | 54 histograms_.get(), |
52 host_resolver_.get(), | 55 host_resolver_.get(), |
53 &client_socket_factory_, | 56 &client_socket_factory_, |
54 NULL) { | 57 NULL) { |
55 } | 58 } |
56 | 59 |
57 virtual ~TransportClientSocketPoolTest() { | 60 virtual ~TransportClientSocketPoolTest() { |
58 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( | 61 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( |
59 connect_backup_jobs_enabled_); | 62 connect_backup_jobs_enabled_); |
60 } | 63 } |
61 | 64 |
| 65 scoped_refptr<TransportSocketParams> CreateParamsForTCPFastOpen() { |
| 66 return new TransportSocketParams(HostPortPair("www.google.com", 80), |
| 67 false, false, OnHostResolutionCallback(), |
| 68 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED); |
| 69 } |
| 70 |
62 int StartRequest(const std::string& group_name, RequestPriority priority) { | 71 int StartRequest(const std::string& group_name, RequestPriority priority) { |
63 scoped_refptr<TransportSocketParams> params(new TransportSocketParams( | 72 scoped_refptr<TransportSocketParams> params(new TransportSocketParams( |
64 HostPortPair("www.google.com", 80), false, false, | 73 HostPortPair("www.google.com", 80), false, false, |
65 OnHostResolutionCallback())); | 74 OnHostResolutionCallback(), |
| 75 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
66 return test_base_.StartRequestUsingPool( | 76 return test_base_.StartRequestUsingPool( |
67 &pool_, group_name, priority, params); | 77 &pool_, group_name, priority, params); |
68 } | 78 } |
69 | 79 |
70 int GetOrderOfRequest(size_t index) { | 80 int GetOrderOfRequest(size_t index) { |
71 return test_base_.GetOrderOfRequest(index); | 81 return test_base_.GetOrderOfRequest(index); |
72 } | 82 } |
73 | 83 |
74 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { | 84 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
75 return test_base_.ReleaseOneConnection(keep_alive); | 85 return test_base_.ReleaseOneConnection(keep_alive); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 EXPECT_EQ(priority, host_resolver_->last_request_priority()); | 204 EXPECT_EQ(priority, host_resolver_->last_request_priority()); |
195 } | 205 } |
196 } | 206 } |
197 | 207 |
198 TEST_F(TransportClientSocketPoolTest, InitHostResolutionFailure) { | 208 TEST_F(TransportClientSocketPoolTest, InitHostResolutionFailure) { |
199 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); | 209 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); |
200 TestCompletionCallback callback; | 210 TestCompletionCallback callback; |
201 ClientSocketHandle handle; | 211 ClientSocketHandle handle; |
202 HostPortPair host_port_pair("unresolvable.host.name", 80); | 212 HostPortPair host_port_pair("unresolvable.host.name", 80); |
203 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 213 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
204 host_port_pair, false, false, | 214 host_port_pair, false, false, OnHostResolutionCallback(), |
205 OnHostResolutionCallback())); | 215 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
206 EXPECT_EQ(ERR_IO_PENDING, | 216 EXPECT_EQ(ERR_IO_PENDING, |
207 handle.Init("a", dest, kDefaultPriority, callback.callback(), | 217 handle.Init("a", dest, kDefaultPriority, callback.callback(), |
208 &pool_, BoundNetLog())); | 218 &pool_, BoundNetLog())); |
209 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); | 219 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); |
210 } | 220 } |
211 | 221 |
212 TEST_F(TransportClientSocketPoolTest, InitConnectionFailure) { | 222 TEST_F(TransportClientSocketPoolTest, InitConnectionFailure) { |
213 client_socket_factory_.set_default_client_socket_type( | 223 client_socket_factory_.set_default_client_socket_type( |
214 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); | 224 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); |
215 TestCompletionCallback callback; | 225 TestCompletionCallback callback; |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 handle_->socket()->Disconnect(); | 480 handle_->socket()->Disconnect(); |
471 handle_->Reset(); | 481 handle_->Reset(); |
472 { | 482 { |
473 base::MessageLoop::ScopedNestableTaskAllower allow( | 483 base::MessageLoop::ScopedNestableTaskAllower allow( |
474 base::MessageLoop::current()); | 484 base::MessageLoop::current()); |
475 base::MessageLoop::current()->RunUntilIdle(); | 485 base::MessageLoop::current()->RunUntilIdle(); |
476 } | 486 } |
477 within_callback_ = true; | 487 within_callback_ = true; |
478 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 488 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
479 HostPortPair("www.google.com", 80), false, false, | 489 HostPortPair("www.google.com", 80), false, false, |
480 OnHostResolutionCallback())); | 490 OnHostResolutionCallback(), |
| 491 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
481 int rv = handle_->Init("a", dest, LOWEST, callback(), pool_, | 492 int rv = handle_->Init("a", dest, LOWEST, callback(), pool_, |
482 BoundNetLog()); | 493 BoundNetLog()); |
483 EXPECT_EQ(OK, rv); | 494 EXPECT_EQ(OK, rv); |
484 } | 495 } |
485 } | 496 } |
486 | 497 |
487 ClientSocketHandle* const handle_; | 498 ClientSocketHandle* const handle_; |
488 TransportClientSocketPool* const pool_; | 499 TransportClientSocketPool* const pool_; |
489 bool within_callback_; | 500 bool within_callback_; |
490 CompletionCallback callback_; | 501 CompletionCallback callback_; |
491 | 502 |
492 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); | 503 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); |
493 }; | 504 }; |
494 | 505 |
495 TEST_F(TransportClientSocketPoolTest, RequestTwice) { | 506 TEST_F(TransportClientSocketPoolTest, RequestTwice) { |
496 ClientSocketHandle handle; | 507 ClientSocketHandle handle; |
497 RequestSocketCallback callback(&handle, &pool_); | 508 RequestSocketCallback callback(&handle, &pool_); |
498 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 509 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
499 HostPortPair("www.google.com", 80), false, false, | 510 HostPortPair("www.google.com", 80), false, false, |
500 OnHostResolutionCallback())); | 511 OnHostResolutionCallback(), |
| 512 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
501 int rv = handle.Init("a", dest, LOWEST, callback.callback(), &pool_, | 513 int rv = handle.Init("a", dest, LOWEST, callback.callback(), &pool_, |
502 BoundNetLog()); | 514 BoundNetLog()); |
503 ASSERT_EQ(ERR_IO_PENDING, rv); | 515 ASSERT_EQ(ERR_IO_PENDING, rv); |
504 | 516 |
505 // The callback is going to request "www.google.com". We want it to complete | 517 // The callback is going to request "www.google.com". We want it to complete |
506 // synchronously this time. | 518 // synchronously this time. |
507 host_resolver_->set_synchronous_mode(true); | 519 host_resolver_->set_synchronous_mode(true); |
508 | 520 |
509 EXPECT_EQ(OK, callback.WaitForResult()); | 521 EXPECT_EQ(OK, callback.WaitForResult()); |
510 | 522 |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 | 969 |
958 EXPECT_EQ(OK, callback.WaitForResult()); | 970 EXPECT_EQ(OK, callback.WaitForResult()); |
959 EXPECT_TRUE(handle.is_initialized()); | 971 EXPECT_TRUE(handle.is_initialized()); |
960 EXPECT_TRUE(handle.socket()); | 972 EXPECT_TRUE(handle.socket()); |
961 IPEndPoint endpoint; | 973 IPEndPoint endpoint; |
962 handle.socket()->GetLocalAddress(&endpoint); | 974 handle.socket()->GetLocalAddress(&endpoint); |
963 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size()); | 975 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size()); |
964 EXPECT_EQ(1, client_socket_factory_.allocation_count()); | 976 EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
965 } | 977 } |
966 | 978 |
| 979 // Test that if TCP FastOpen is enabled, it is set on the socket |
| 980 // when we have only an IPv4 address. |
| 981 TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv4WithNoFallback) { |
| 982 // Create a pool without backup jobs. |
| 983 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); |
| 984 TransportClientSocketPool pool(kMaxSockets, |
| 985 kMaxSocketsPerGroup, |
| 986 histograms_.get(), |
| 987 host_resolver_.get(), |
| 988 &client_socket_factory_, |
| 989 NULL); |
| 990 client_socket_factory_.set_default_client_socket_type( |
| 991 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); |
| 992 // Resolve an AddressList with only IPv4 addresses. |
| 993 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string()); |
| 994 |
| 995 TestCompletionCallback callback; |
| 996 ClientSocketHandle handle; |
| 997 // Enable TCP FastOpen in TransportSocketParams. |
| 998 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); |
| 999 handle.Init("a", params, LOW, callback.callback(), &pool, BoundNetLog()); |
| 1000 EXPECT_EQ(OK, callback.WaitForResult()); |
| 1001 EXPECT_TRUE(handle.socket()->UsingTCPFastOpen()); |
| 1002 } |
| 1003 |
| 1004 // Test that if TCP FastOpen is enabled, it is set on the socket |
| 1005 // when we have only IPv6 addresses. |
| 1006 TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv6WithNoFallback) { |
| 1007 // Create a pool without backup jobs. |
| 1008 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); |
| 1009 TransportClientSocketPool pool(kMaxSockets, |
| 1010 kMaxSocketsPerGroup, |
| 1011 histograms_.get(), |
| 1012 host_resolver_.get(), |
| 1013 &client_socket_factory_, |
| 1014 NULL); |
| 1015 client_socket_factory_.set_default_client_socket_type( |
| 1016 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); |
| 1017 // Resolve an AddressList with only IPv6 addresses. |
| 1018 host_resolver_->rules() |
| 1019 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string()); |
| 1020 |
| 1021 TestCompletionCallback callback; |
| 1022 ClientSocketHandle handle; |
| 1023 // Enable TCP FastOpen in TransportSocketParams. |
| 1024 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); |
| 1025 handle.Init("a", params, LOW, callback.callback(), &pool, BoundNetLog()); |
| 1026 EXPECT_EQ(OK, callback.WaitForResult()); |
| 1027 EXPECT_TRUE(handle.socket()->UsingTCPFastOpen()); |
| 1028 } |
| 1029 |
| 1030 // Test that if TCP FastOpen is enabled, it does not do anything when there |
| 1031 // is a IPv6 address with fallback to an IPv4 address. This test tests the case |
| 1032 // when the IPv6 connect fails and the IPv4 one succeeds. |
| 1033 TEST_F(TransportClientSocketPoolTest, |
| 1034 NoTCPFastOpenOnIPv6FailureWithIPv4Fallback) { |
| 1035 // Create a pool without backup jobs. |
| 1036 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); |
| 1037 TransportClientSocketPool pool(kMaxSockets, |
| 1038 kMaxSocketsPerGroup, |
| 1039 histograms_.get(), |
| 1040 host_resolver_.get(), |
| 1041 &client_socket_factory_, |
| 1042 NULL); |
| 1043 |
| 1044 MockTransportClientSocketFactory::ClientSocketType case_types[] = { |
| 1045 // This is the IPv6 socket. |
| 1046 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
| 1047 // This is the IPv4 socket. |
| 1048 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET |
| 1049 }; |
| 1050 client_socket_factory_.set_client_socket_types(case_types, 2); |
| 1051 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. |
| 1052 host_resolver_->rules() |
| 1053 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
| 1054 |
| 1055 TestCompletionCallback callback; |
| 1056 ClientSocketHandle handle; |
| 1057 // Enable TCP FastOpen in TransportSocketParams. |
| 1058 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); |
| 1059 handle.Init("a", params, LOW, callback.callback(), &pool, BoundNetLog()); |
| 1060 EXPECT_EQ(OK, callback.WaitForResult()); |
| 1061 // Verify that the socket used is connected to the fallback IPv4 address. |
| 1062 IPEndPoint endpoint; |
| 1063 handle.socket()->GetLocalAddress(&endpoint); |
| 1064 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size()); |
| 1065 EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1066 // Verify that TCP FastOpen was not turned on for the socket. |
| 1067 EXPECT_FALSE(handle.socket()->UsingTCPFastOpen()); |
| 1068 } |
| 1069 |
| 1070 // Test that if TCP FastOpen is enabled, it does not do anything when there |
| 1071 // is a IPv6 address with fallback to an IPv4 address. This test tests the case |
| 1072 // when the IPv6 connect succeeds. |
| 1073 TEST_F(TransportClientSocketPoolTest, |
| 1074 NoTCPFastOpenOnIPv6SuccessWithIPv4Fallback) { |
| 1075 // Create a pool without backup jobs. |
| 1076 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); |
| 1077 TransportClientSocketPool pool(kMaxSockets, |
| 1078 kMaxSocketsPerGroup, |
| 1079 histograms_.get(), |
| 1080 host_resolver_.get(), |
| 1081 &client_socket_factory_, |
| 1082 NULL); |
| 1083 |
| 1084 MockTransportClientSocketFactory::ClientSocketType case_types[] = { |
| 1085 // This is the IPv6 socket. |
| 1086 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET, |
| 1087 // This is the IPv4 socket. |
| 1088 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET |
| 1089 }; |
| 1090 client_socket_factory_.set_client_socket_types(case_types, 2); |
| 1091 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. |
| 1092 host_resolver_->rules() |
| 1093 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
| 1094 |
| 1095 TestCompletionCallback callback; |
| 1096 ClientSocketHandle handle; |
| 1097 // Enable TCP FastOpen in TransportSocketParams. |
| 1098 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); |
| 1099 handle.Init("a", params, LOW, callback.callback(), &pool, BoundNetLog()); |
| 1100 EXPECT_EQ(OK, callback.WaitForResult()); |
| 1101 // Verify that the socket used is connected to the IPv6 address. |
| 1102 IPEndPoint endpoint; |
| 1103 handle.socket()->GetLocalAddress(&endpoint); |
| 1104 EXPECT_EQ(kIPv6AddressSize, endpoint.address().size()); |
| 1105 EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 1106 // Verify that TCP FastOpen was not turned on for the socket. |
| 1107 EXPECT_FALSE(handle.socket()->UsingTCPFastOpen()); |
| 1108 } |
| 1109 |
967 } // namespace | 1110 } // namespace |
968 | 1111 |
969 } // namespace net | 1112 } // namespace net |
OLD | NEW |