Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 #include "net/socket/client_socket_pool_histograms.h" | 21 #include "net/socket/client_socket_pool_histograms.h" |
| 22 #include "net/socket/socket_test_util.h" | 22 #include "net/socket/socket_test_util.h" |
| 23 #include "net/socket/stream_socket.h" | 23 #include "net/socket/stream_socket.h" |
| 24 #include "net/socket/transport_client_socket_pool_test_util.h" | 24 #include "net/socket/transport_client_socket_pool_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 26 |
| 27 namespace net { | 27 namespace net { |
| 28 | 28 |
| 29 using internal::ClientSocketPoolBaseHelper; | 29 using internal::ClientSocketPoolBaseHelper; |
| 30 | 30 |
| 31 class TransportSocketParamsPeer { | |
| 32 public: | |
| 33 static void SetCombineConnectAndWritePolicy(TransportSocketParams* params, | |
| 34 TransportSocketParams::CombineConnectAndWritePolicy new_policy) { | |
| 35 params->combine_connect_and_write_ = new_policy; | |
| 36 } | |
| 37 }; | |
| 38 | |
| 31 namespace { | 39 namespace { |
| 32 | 40 |
| 33 const int kMaxSockets = 32; | 41 const int kMaxSockets = 32; |
| 34 const int kMaxSocketsPerGroup = 6; | 42 const int kMaxSocketsPerGroup = 6; |
| 35 const RequestPriority kDefaultPriority = LOW; | 43 const RequestPriority kDefaultPriority = LOW; |
| 36 | 44 |
| 37 class TransportClientSocketPoolTest : public testing::Test { | 45 class TransportClientSocketPoolTest : public testing::Test { |
| 38 protected: | 46 protected: |
| 39 TransportClientSocketPoolTest() | 47 TransportClientSocketPoolTest() |
| 40 : connect_backup_jobs_enabled_( | 48 : connect_backup_jobs_enabled_( |
| 41 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true)), | 49 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true)), |
| 42 params_( | 50 params_( |
| 43 new TransportSocketParams(HostPortPair("www.google.com", 80), | 51 new TransportSocketParams( |
| 44 false, false, | 52 HostPortPair("www.google.com", 80), |
| 45 OnHostResolutionCallback())), | 53 false, |
| 54 false, | |
| 55 OnHostResolutionCallback(), | |
| 56 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)), | |
| 46 histograms_(new ClientSocketPoolHistograms("TCPUnitTest")), | 57 histograms_(new ClientSocketPoolHistograms("TCPUnitTest")), |
| 47 host_resolver_(new MockHostResolver), | 58 host_resolver_(new MockHostResolver), |
| 48 client_socket_factory_(&net_log_), | 59 client_socket_factory_(&net_log_), |
| 49 pool_(kMaxSockets, | 60 pool_(kMaxSockets, |
| 50 kMaxSocketsPerGroup, | 61 kMaxSocketsPerGroup, |
| 51 histograms_.get(), | 62 histograms_.get(), |
| 52 host_resolver_.get(), | 63 host_resolver_.get(), |
| 53 &client_socket_factory_, | 64 &client_socket_factory_, |
| 54 NULL) { | 65 NULL) { |
| 55 } | 66 } |
| 56 | 67 |
| 57 virtual ~TransportClientSocketPoolTest() { | 68 virtual ~TransportClientSocketPoolTest() { |
| 58 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( | 69 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( |
| 59 connect_backup_jobs_enabled_); | 70 connect_backup_jobs_enabled_); |
| 60 } | 71 } |
| 61 | 72 |
| 62 int StartRequest(const std::string& group_name, RequestPriority priority) { | 73 int StartRequest(const std::string& group_name, RequestPriority priority) { |
| 63 scoped_refptr<TransportSocketParams> params(new TransportSocketParams( | 74 scoped_refptr<TransportSocketParams> params(new TransportSocketParams( |
| 64 HostPortPair("www.google.com", 80), false, false, | 75 HostPortPair("www.google.com", 80), false, false, |
| 65 OnHostResolutionCallback())); | 76 OnHostResolutionCallback(), |
| 77 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | |
| 66 return test_base_.StartRequestUsingPool( | 78 return test_base_.StartRequestUsingPool( |
| 67 &pool_, group_name, priority, params); | 79 &pool_, group_name, priority, params); |
| 68 } | 80 } |
| 69 | 81 |
| 70 int GetOrderOfRequest(size_t index) { | 82 int GetOrderOfRequest(size_t index) { |
| 71 return test_base_.GetOrderOfRequest(index); | 83 return test_base_.GetOrderOfRequest(index); |
| 72 } | 84 } |
| 73 | 85 |
| 74 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { | 86 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 75 return test_base_.ReleaseOneConnection(keep_alive); | 87 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()); | 206 EXPECT_EQ(priority, host_resolver_->last_request_priority()); |
| 195 } | 207 } |
| 196 } | 208 } |
| 197 | 209 |
| 198 TEST_F(TransportClientSocketPoolTest, InitHostResolutionFailure) { | 210 TEST_F(TransportClientSocketPoolTest, InitHostResolutionFailure) { |
| 199 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); | 211 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); |
| 200 TestCompletionCallback callback; | 212 TestCompletionCallback callback; |
| 201 ClientSocketHandle handle; | 213 ClientSocketHandle handle; |
| 202 HostPortPair host_port_pair("unresolvable.host.name", 80); | 214 HostPortPair host_port_pair("unresolvable.host.name", 80); |
| 203 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 215 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
| 204 host_port_pair, false, false, | 216 host_port_pair, false, false, OnHostResolutionCallback(), |
| 205 OnHostResolutionCallback())); | 217 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 206 EXPECT_EQ(ERR_IO_PENDING, | 218 EXPECT_EQ(ERR_IO_PENDING, |
| 207 handle.Init("a", dest, kDefaultPriority, callback.callback(), | 219 handle.Init("a", dest, kDefaultPriority, callback.callback(), |
| 208 &pool_, BoundNetLog())); | 220 &pool_, BoundNetLog())); |
| 209 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); | 221 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); |
| 210 } | 222 } |
| 211 | 223 |
| 212 TEST_F(TransportClientSocketPoolTest, InitConnectionFailure) { | 224 TEST_F(TransportClientSocketPoolTest, InitConnectionFailure) { |
| 213 client_socket_factory_.set_default_client_socket_type( | 225 client_socket_factory_.set_default_client_socket_type( |
| 214 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); | 226 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); |
| 215 TestCompletionCallback callback; | 227 TestCompletionCallback callback; |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 handle_->socket()->Disconnect(); | 482 handle_->socket()->Disconnect(); |
| 471 handle_->Reset(); | 483 handle_->Reset(); |
| 472 { | 484 { |
| 473 base::MessageLoop::ScopedNestableTaskAllower allow( | 485 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 474 base::MessageLoop::current()); | 486 base::MessageLoop::current()); |
| 475 base::MessageLoop::current()->RunUntilIdle(); | 487 base::MessageLoop::current()->RunUntilIdle(); |
| 476 } | 488 } |
| 477 within_callback_ = true; | 489 within_callback_ = true; |
| 478 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 490 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
| 479 HostPortPair("www.google.com", 80), false, false, | 491 HostPortPair("www.google.com", 80), false, false, |
| 480 OnHostResolutionCallback())); | 492 OnHostResolutionCallback(), |
| 493 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | |
| 481 int rv = handle_->Init("a", dest, LOWEST, callback(), pool_, | 494 int rv = handle_->Init("a", dest, LOWEST, callback(), pool_, |
| 482 BoundNetLog()); | 495 BoundNetLog()); |
| 483 EXPECT_EQ(OK, rv); | 496 EXPECT_EQ(OK, rv); |
| 484 } | 497 } |
| 485 } | 498 } |
| 486 | 499 |
| 487 ClientSocketHandle* const handle_; | 500 ClientSocketHandle* const handle_; |
| 488 TransportClientSocketPool* const pool_; | 501 TransportClientSocketPool* const pool_; |
| 489 bool within_callback_; | 502 bool within_callback_; |
| 490 CompletionCallback callback_; | 503 CompletionCallback callback_; |
| 491 | 504 |
| 492 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); | 505 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); |
| 493 }; | 506 }; |
| 494 | 507 |
| 495 TEST_F(TransportClientSocketPoolTest, RequestTwice) { | 508 TEST_F(TransportClientSocketPoolTest, RequestTwice) { |
| 496 ClientSocketHandle handle; | 509 ClientSocketHandle handle; |
| 497 RequestSocketCallback callback(&handle, &pool_); | 510 RequestSocketCallback callback(&handle, &pool_); |
| 498 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 511 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
| 499 HostPortPair("www.google.com", 80), false, false, | 512 HostPortPair("www.google.com", 80), false, false, |
| 500 OnHostResolutionCallback())); | 513 OnHostResolutionCallback(), |
| 514 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | |
| 501 int rv = handle.Init("a", dest, LOWEST, callback.callback(), &pool_, | 515 int rv = handle.Init("a", dest, LOWEST, callback.callback(), &pool_, |
| 502 BoundNetLog()); | 516 BoundNetLog()); |
| 503 ASSERT_EQ(ERR_IO_PENDING, rv); | 517 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 504 | 518 |
| 505 // The callback is going to request "www.google.com". We want it to complete | 519 // The callback is going to request "www.google.com". We want it to complete |
| 506 // synchronously this time. | 520 // synchronously this time. |
| 507 host_resolver_->set_synchronous_mode(true); | 521 host_resolver_->set_synchronous_mode(true); |
| 508 | 522 |
| 509 EXPECT_EQ(OK, callback.WaitForResult()); | 523 EXPECT_EQ(OK, callback.WaitForResult()); |
| 510 | 524 |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 957 | 971 |
| 958 EXPECT_EQ(OK, callback.WaitForResult()); | 972 EXPECT_EQ(OK, callback.WaitForResult()); |
| 959 EXPECT_TRUE(handle.is_initialized()); | 973 EXPECT_TRUE(handle.is_initialized()); |
| 960 EXPECT_TRUE(handle.socket()); | 974 EXPECT_TRUE(handle.socket()); |
| 961 IPEndPoint endpoint; | 975 IPEndPoint endpoint; |
| 962 handle.socket()->GetLocalAddress(&endpoint); | 976 handle.socket()->GetLocalAddress(&endpoint); |
| 963 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size()); | 977 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size()); |
| 964 EXPECT_EQ(1, client_socket_factory_.allocation_count()); | 978 EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 965 } | 979 } |
| 966 | 980 |
| 981 // Test that if TCP FastOpen is enabled, it is set on the socket | |
| 982 // when we have only an IPv4 address. | |
| 983 TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv4WithNoFallback) { | |
| 984 // Create a pool without backup jobs. | |
| 985 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); | |
| 986 TransportClientSocketPool pool(kMaxSockets, | |
| 987 kMaxSocketsPerGroup, | |
| 988 histograms_.get(), | |
| 989 host_resolver_.get(), | |
| 990 &client_socket_factory_, | |
| 991 NULL); | |
| 992 client_socket_factory_.set_default_client_socket_type( | |
| 993 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); | |
| 994 // Resolve an AddressList with only IPv4 addresses. | |
| 995 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string()); | |
| 996 | |
| 997 TestCompletionCallback callback; | |
| 998 ClientSocketHandle handle; | |
| 999 // Enable TCP FastOpen in TransportSocketParams. | |
| 1000 TransportSocketParamsPeer::SetCombineConnectAndWritePolicy( | |
|
mmenke
2014/09/11 18:14:35
Rather than creating a peer for this purpose, sugg
mmenke
2014/09/11 18:20:37
Oops...left out my "or". Or making a function to
Jana
2014/09/12 01:04:39
Done.
| |
| 1001 params_.get(), | |
| 1002 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED); | |
| 1003 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); | |
| 1004 EXPECT_EQ(OK, callback.WaitForResult()); | |
| 1005 EXPECT_TRUE(handle.socket()->UsingTCPFastOpen()); | |
| 1006 } | |
| 1007 | |
| 1008 // Test that if TCP FastOpen is enabled, it is set on the socket | |
| 1009 // when we have only IPv6 addresses. | |
| 1010 TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv6WithNoFallback) { | |
| 1011 // Create a pool without backup jobs. | |
| 1012 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); | |
| 1013 TransportClientSocketPool pool(kMaxSockets, | |
| 1014 kMaxSocketsPerGroup, | |
| 1015 histograms_.get(), | |
| 1016 host_resolver_.get(), | |
| 1017 &client_socket_factory_, | |
| 1018 NULL); | |
| 1019 client_socket_factory_.set_default_client_socket_type( | |
| 1020 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); | |
| 1021 // Resolve an AddressList with only IPv6 addresses. | |
| 1022 host_resolver_->rules() | |
| 1023 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string()); | |
| 1024 | |
| 1025 TestCompletionCallback callback; | |
| 1026 ClientSocketHandle handle; | |
| 1027 // Enable TCP FastOpen in TransportSocketParams. | |
| 1028 TransportSocketParamsPeer::SetCombineConnectAndWritePolicy( | |
| 1029 params_.get(), | |
| 1030 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED); | |
| 1031 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); | |
| 1032 EXPECT_EQ(OK, callback.WaitForResult()); | |
| 1033 EXPECT_TRUE(handle.socket()->UsingTCPFastOpen()); | |
| 1034 } | |
| 1035 | |
| 1036 // Test that if TCP FastOpen is enabled, it does not do anything when there | |
| 1037 // is a IPv6 address with fallback to an IPv4 address. | |
| 1038 TEST_F(TransportClientSocketPoolTest, NoTCPFastOpenOnIPv6WithIPv4Fallback) { | |
|
mmenke
2014/09/11 18:14:35
Suggest a test just like this, except the IPv6 soc
Jana
2014/09/12 01:04:39
Added test, PTAL.
| |
| 1039 // Create a pool without backup jobs. | |
| 1040 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); | |
| 1041 TransportClientSocketPool pool(kMaxSockets, | |
| 1042 kMaxSocketsPerGroup, | |
| 1043 histograms_.get(), | |
| 1044 host_resolver_.get(), | |
| 1045 &client_socket_factory_, | |
| 1046 NULL); | |
| 1047 | |
| 1048 MockTransportClientSocketFactory::ClientSocketType case_types[] = { | |
| 1049 // This is the IPv6 socket. | |
| 1050 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, | |
| 1051 // This is the IPv4 socket. | |
| 1052 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET | |
| 1053 }; | |
| 1054 client_socket_factory_.set_client_socket_types(case_types, 2); | |
| 1055 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. | |
| 1056 host_resolver_->rules() | |
| 1057 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); | |
| 1058 | |
| 1059 TestCompletionCallback callback; | |
| 1060 ClientSocketHandle handle; | |
| 1061 // Enable TCP FastOpen in TransportSocketParams. | |
| 1062 TransportSocketParamsPeer::SetCombineConnectAndWritePolicy( | |
| 1063 params_.get(), | |
| 1064 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED); | |
| 1065 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog()); | |
| 1066 EXPECT_EQ(OK, callback.WaitForResult()); | |
| 1067 EXPECT_FALSE(handle.socket()->UsingTCPFastOpen()); | |
|
mmenke
2014/09/11 18:14:35
As a sanity check, suggest you check that the conn
Jana
2014/09/12 01:04:39
In general, I like to not duplicate test coverage.
| |
| 1068 } | |
| 1069 | |
| 967 } // namespace | 1070 } // namespace |
| 968 | 1071 |
| 969 } // namespace net | 1072 } // namespace net |
| OLD | NEW |