This source file includes following definitions.
- TestLoadTimingInfoConnectedReused
- TestLoadTimingInfoConnectedNotReused
- SetIPv4Address
- SetIPv6Address
- net_log_
- Connect
- Disconnect
- IsConnected
- IsConnectedAndIdle
- GetPeerAddress
- GetLocalAddress
- SetSubresourceSpeculation
- SetOmniboxSpeculation
- WasEverUsed
- UsingTCPFastOpen
- WasNpnNegotiated
- GetNegotiatedProtocol
- GetSSLInfo
- Read
- Write
- SetReceiveBufferSize
- SetSendBufferSize
- net_log_
- Connect
- Disconnect
- IsConnected
- IsConnectedAndIdle
- GetPeerAddress
- GetLocalAddress
- SetSubresourceSpeculation
- SetOmniboxSpeculation
- WasEverUsed
- UsingTCPFastOpen
- WasNpnNegotiated
- GetNegotiatedProtocol
- GetSSLInfo
- Read
- Write
- SetReceiveBufferSize
- SetSendBufferSize
- weak_factory_
- Connect
- Disconnect
- IsConnected
- IsConnectedAndIdle
- GetPeerAddress
- GetLocalAddress
- SetSubresourceSpeculation
- SetOmniboxSpeculation
- WasEverUsed
- UsingTCPFastOpen
- WasNpnNegotiated
- GetNegotiatedProtocol
- GetSSLInfo
- Read
- Write
- SetReceiveBufferSize
- SetSendBufferSize
- DoCallback
- delay_
- CreateDatagramClientSocket
- CreateTransportClientSocket
- CreateSSLClientSocket
- ClearSSLSessionCache
- allocation_count
- set_client_socket_type
- set_client_socket_types
- set_delay
- pool_
- StartRequest
- GetOrderOfRequest
- ReleaseOneConnection
- ReleaseAllConnections
- requests
- completion_count
- TEST
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- callback_
- callback
- OnComplete
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "net/socket/transport_client_socket_pool.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/platform_thread.h"
#include "net/base/capturing_net_log.h"
#include "net/base/ip_endpoint.h"
#include "net/base/load_timing_info.h"
#include "net/base/load_timing_info_test_util.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/base/test_completion_callback.h"
#include "net/dns/mock_host_resolver.h"
#include "net/socket/client_socket_factory.h"
#include "net/socket/client_socket_handle.h"
#include "net/socket/client_socket_pool_histograms.h"
#include "net/socket/socket_test_util.h"
#include "net/socket/ssl_client_socket.h"
#include "net/socket/stream_socket.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
using internal::ClientSocketPoolBaseHelper;
namespace {
const int kMaxSockets = 32;
const int kMaxSocketsPerGroup = 6;
const net::RequestPriority kDefaultPriority = LOW;
void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) {
LoadTimingInfo load_timing_info;
EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info));
EXPECT_TRUE(load_timing_info.socket_reused);
EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info);
}
void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) {
EXPECT_FALSE(handle.is_reused());
LoadTimingInfo load_timing_info;
EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info));
EXPECT_FALSE(load_timing_info.socket_reused);
EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
CONNECT_TIMING_HAS_DNS_TIMES);
ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info);
TestLoadTimingInfoConnectedReused(handle);
}
void SetIPv4Address(IPEndPoint* address) {
IPAddressNumber number;
CHECK(ParseIPLiteralToNumber("1.1.1.1", &number));
*address = IPEndPoint(number, 80);
}
void SetIPv6Address(IPEndPoint* address) {
IPAddressNumber number;
CHECK(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number));
*address = IPEndPoint(number, 80);
}
class MockClientSocket : public StreamSocket {
public:
MockClientSocket(const AddressList& addrlist, net::NetLog* net_log)
: connected_(false),
addrlist_(addrlist),
net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
}
virtual int Connect(const CompletionCallback& callback) OVERRIDE {
connected_ = true;
return OK;
}
virtual void Disconnect() OVERRIDE {
connected_ = false;
}
virtual bool IsConnected() const OVERRIDE {
return connected_;
}
virtual bool IsConnectedAndIdle() const OVERRIDE {
return connected_;
}
virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
return ERR_UNEXPECTED;
}
virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
if (!connected_)
return ERR_SOCKET_NOT_CONNECTED;
if (addrlist_.front().GetFamily() == ADDRESS_FAMILY_IPV4)
SetIPv4Address(address);
else
SetIPv6Address(address);
return OK;
}
virtual const BoundNetLog& NetLog() const OVERRIDE {
return net_log_;
}
virtual void SetSubresourceSpeculation() OVERRIDE {}
virtual void SetOmniboxSpeculation() OVERRIDE {}
virtual bool WasEverUsed() const OVERRIDE { return false; }
virtual bool UsingTCPFastOpen() const OVERRIDE { return false; }
virtual bool WasNpnNegotiated() const OVERRIDE {
return false;
}
virtual NextProto GetNegotiatedProtocol() const OVERRIDE {
return kProtoUnknown;
}
virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE {
return false;
}
virtual int Read(IOBuffer* buf, int buf_len,
const CompletionCallback& callback) OVERRIDE {
return ERR_FAILED;
}
virtual int Write(IOBuffer* buf, int buf_len,
const CompletionCallback& callback) OVERRIDE {
return ERR_FAILED;
}
virtual int SetReceiveBufferSize(int32 size) OVERRIDE { return OK; }
virtual int SetSendBufferSize(int32 size) OVERRIDE { return OK; }
private:
bool connected_;
const AddressList addrlist_;
BoundNetLog net_log_;
DISALLOW_COPY_AND_ASSIGN(MockClientSocket);
};
class MockFailingClientSocket : public StreamSocket {
public:
MockFailingClientSocket(const AddressList& addrlist, net::NetLog* net_log)
: addrlist_(addrlist),
net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
}
virtual int Connect(const CompletionCallback& callback) OVERRIDE {
return ERR_CONNECTION_FAILED;
}
virtual void Disconnect() OVERRIDE {}
virtual bool IsConnected() const OVERRIDE {
return false;
}
virtual bool IsConnectedAndIdle() const OVERRIDE {
return false;
}
virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
return ERR_UNEXPECTED;
}
virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
return ERR_UNEXPECTED;
}
virtual const BoundNetLog& NetLog() const OVERRIDE {
return net_log_;
}
virtual void SetSubresourceSpeculation() OVERRIDE {}
virtual void SetOmniboxSpeculation() OVERRIDE {}
virtual bool WasEverUsed() const OVERRIDE { return false; }
virtual bool UsingTCPFastOpen() const OVERRIDE { return false; }
virtual bool WasNpnNegotiated() const OVERRIDE {
return false;
}
virtual NextProto GetNegotiatedProtocol() const OVERRIDE {
return kProtoUnknown;
}
virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE {
return false;
}
virtual int Read(IOBuffer* buf, int buf_len,
const CompletionCallback& callback) OVERRIDE {
return ERR_FAILED;
}
virtual int Write(IOBuffer* buf, int buf_len,
const CompletionCallback& callback) OVERRIDE {
return ERR_FAILED;
}
virtual int SetReceiveBufferSize(int32 size) OVERRIDE { return OK; }
virtual int SetSendBufferSize(int32 size) OVERRIDE { return OK; }
private:
const AddressList addrlist_;
BoundNetLog net_log_;
DISALLOW_COPY_AND_ASSIGN(MockFailingClientSocket);
};
class MockPendingClientSocket : public StreamSocket {
public:
MockPendingClientSocket(
const AddressList& addrlist,
bool should_connect,
bool should_stall,
base::TimeDelta delay,
net::NetLog* net_log)
: should_connect_(should_connect),
should_stall_(should_stall),
delay_(delay),
is_connected_(false),
addrlist_(addrlist),
net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)),
weak_factory_(this) {
}
virtual int Connect(const CompletionCallback& callback) OVERRIDE {
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&MockPendingClientSocket::DoCallback,
weak_factory_.GetWeakPtr(), callback),
delay_);
return ERR_IO_PENDING;
}
virtual void Disconnect() OVERRIDE {}
virtual bool IsConnected() const OVERRIDE {
return is_connected_;
}
virtual bool IsConnectedAndIdle() const OVERRIDE {
return is_connected_;
}
virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
return ERR_UNEXPECTED;
}
virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
if (!is_connected_)
return ERR_SOCKET_NOT_CONNECTED;
if (addrlist_.front().GetFamily() == ADDRESS_FAMILY_IPV4)
SetIPv4Address(address);
else
SetIPv6Address(address);
return OK;
}
virtual const BoundNetLog& NetLog() const OVERRIDE {
return net_log_;
}
virtual void SetSubresourceSpeculation() OVERRIDE {}
virtual void SetOmniboxSpeculation() OVERRIDE {}
virtual bool WasEverUsed() const OVERRIDE { return false; }
virtual bool UsingTCPFastOpen() const OVERRIDE { return false; }
virtual bool WasNpnNegotiated() const OVERRIDE {
return false;
}
virtual NextProto GetNegotiatedProtocol() const OVERRIDE {
return kProtoUnknown;
}
virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE {
return false;
}
virtual int Read(IOBuffer* buf, int buf_len,
const CompletionCallback& callback) OVERRIDE {
return ERR_FAILED;
}
virtual int Write(IOBuffer* buf, int buf_len,
const CompletionCallback& callback) OVERRIDE {
return ERR_FAILED;
}
virtual int SetReceiveBufferSize(int32 size) OVERRIDE { return OK; }
virtual int SetSendBufferSize(int32 size) OVERRIDE { return OK; }
private:
void DoCallback(const CompletionCallback& callback) {
if (should_stall_)
return;
if (should_connect_) {
is_connected_ = true;
callback.Run(OK);
} else {
is_connected_ = false;
callback.Run(ERR_CONNECTION_FAILED);
}
}
bool should_connect_;
bool should_stall_;
base::TimeDelta delay_;
bool is_connected_;
const AddressList addrlist_;
BoundNetLog net_log_;
base::WeakPtrFactory<MockPendingClientSocket> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(MockPendingClientSocket);
};
class MockClientSocketFactory : public ClientSocketFactory {
public:
enum ClientSocketType {
MOCK_CLIENT_SOCKET,
MOCK_FAILING_CLIENT_SOCKET,
MOCK_PENDING_CLIENT_SOCKET,
MOCK_PENDING_FAILING_CLIENT_SOCKET,
MOCK_DELAYED_CLIENT_SOCKET,
MOCK_STALLED_CLIENT_SOCKET,
};
explicit MockClientSocketFactory(NetLog* net_log)
: net_log_(net_log), allocation_count_(0),
client_socket_type_(MOCK_CLIENT_SOCKET), client_socket_types_(NULL),
client_socket_index_(0), client_socket_index_max_(0),
delay_(base::TimeDelta::FromMilliseconds(
ClientSocketPool::kMaxConnectRetryIntervalMs)) {}
virtual scoped_ptr<DatagramClientSocket> CreateDatagramClientSocket(
DatagramSocket::BindType bind_type,
const RandIntCallback& rand_int_cb,
NetLog* net_log,
const NetLog::Source& source) OVERRIDE {
NOTREACHED();
return scoped_ptr<DatagramClientSocket>();
}
virtual scoped_ptr<StreamSocket> CreateTransportClientSocket(
const AddressList& addresses,
NetLog* ,
const NetLog::Source& ) OVERRIDE {
allocation_count_++;
ClientSocketType type = client_socket_type_;
if (client_socket_types_ &&
client_socket_index_ < client_socket_index_max_) {
type = client_socket_types_[client_socket_index_++];
}
switch (type) {
case MOCK_CLIENT_SOCKET:
return scoped_ptr<StreamSocket>(
new MockClientSocket(addresses, net_log_));
case MOCK_FAILING_CLIENT_SOCKET:
return scoped_ptr<StreamSocket>(
new MockFailingClientSocket(addresses, net_log_));
case MOCK_PENDING_CLIENT_SOCKET:
return scoped_ptr<StreamSocket>(
new MockPendingClientSocket(
addresses, true, false, base::TimeDelta(), net_log_));
case MOCK_PENDING_FAILING_CLIENT_SOCKET:
return scoped_ptr<StreamSocket>(
new MockPendingClientSocket(
addresses, false, false, base::TimeDelta(), net_log_));
case MOCK_DELAYED_CLIENT_SOCKET:
return scoped_ptr<StreamSocket>(
new MockPendingClientSocket(
addresses, true, false, delay_, net_log_));
case MOCK_STALLED_CLIENT_SOCKET:
return scoped_ptr<StreamSocket>(
new MockPendingClientSocket(
addresses, true, true, base::TimeDelta(), net_log_));
default:
NOTREACHED();
return scoped_ptr<StreamSocket>(
new MockClientSocket(addresses, net_log_));
}
}
virtual scoped_ptr<SSLClientSocket> CreateSSLClientSocket(
scoped_ptr<ClientSocketHandle> transport_socket,
const HostPortPair& host_and_port,
const SSLConfig& ssl_config,
const SSLClientSocketContext& context) OVERRIDE {
NOTIMPLEMENTED();
return scoped_ptr<SSLClientSocket>();
}
virtual void ClearSSLSessionCache() OVERRIDE {
NOTIMPLEMENTED();
}
int allocation_count() const { return allocation_count_; }
void set_client_socket_type(ClientSocketType type) {
client_socket_type_ = type;
}
void set_client_socket_types(ClientSocketType* type_list, int num_types) {
DCHECK_GT(num_types, 0);
client_socket_types_ = type_list;
client_socket_index_ = 0;
client_socket_index_max_ = num_types;
}
void set_delay(base::TimeDelta delay) { delay_ = delay; }
private:
NetLog* net_log_;
int allocation_count_;
ClientSocketType client_socket_type_;
ClientSocketType* client_socket_types_;
int client_socket_index_;
int client_socket_index_max_;
base::TimeDelta delay_;
DISALLOW_COPY_AND_ASSIGN(MockClientSocketFactory);
};
class TransportClientSocketPoolTest : public testing::Test {
protected:
TransportClientSocketPoolTest()
: connect_backup_jobs_enabled_(
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true)),
params_(
new TransportSocketParams(HostPortPair("www.google.com", 80),
false, false,
OnHostResolutionCallback())),
histograms_(new ClientSocketPoolHistograms("TCPUnitTest")),
host_resolver_(new MockHostResolver),
client_socket_factory_(&net_log_),
pool_(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL) {
}
virtual ~TransportClientSocketPoolTest() {
internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
connect_backup_jobs_enabled_);
}
int StartRequest(const std::string& group_name, RequestPriority priority) {
scoped_refptr<TransportSocketParams> params(new TransportSocketParams(
HostPortPair("www.google.com", 80), false, false,
OnHostResolutionCallback()));
return test_base_.StartRequestUsingPool(
&pool_, group_name, priority, params);
}
int GetOrderOfRequest(size_t index) {
return test_base_.GetOrderOfRequest(index);
}
bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) {
return test_base_.ReleaseOneConnection(keep_alive);
}
void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) {
test_base_.ReleaseAllConnections(keep_alive);
}
ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); }
size_t completion_count() const { return test_base_.completion_count(); }
bool connect_backup_jobs_enabled_;
CapturingNetLog net_log_;
scoped_refptr<TransportSocketParams> params_;
scoped_ptr<ClientSocketPoolHistograms> histograms_;
scoped_ptr<MockHostResolver> host_resolver_;
MockClientSocketFactory client_socket_factory_;
TransportClientSocketPool pool_;
ClientSocketPoolTest test_base_;
DISALLOW_COPY_AND_ASSIGN(TransportClientSocketPoolTest);
};
TEST(TransportConnectJobTest, MakeAddrListStartWithIPv4) {
IPAddressNumber ip_number;
ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.1", &ip_number));
IPEndPoint addrlist_v4_1(ip_number, 80);
ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.2", &ip_number));
IPEndPoint addrlist_v4_2(ip_number, 80);
ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::64", &ip_number));
IPEndPoint addrlist_v6_1(ip_number, 80);
ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::66", &ip_number));
IPEndPoint addrlist_v6_2(ip_number, 80);
AddressList addrlist;
addrlist.clear();
addrlist.push_back(addrlist_v4_1);
addrlist.push_back(addrlist_v4_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(2u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[1].GetFamily());
addrlist.clear();
addrlist.push_back(addrlist_v6_1);
addrlist.push_back(addrlist_v6_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(2u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[1].GetFamily());
addrlist.clear();
addrlist.push_back(addrlist_v4_1);
addrlist.push_back(addrlist_v4_2);
addrlist.push_back(addrlist_v6_1);
addrlist.push_back(addrlist_v6_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(4u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[1].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[2].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily());
addrlist.clear();
addrlist.push_back(addrlist_v6_1);
addrlist.push_back(addrlist_v4_1);
addrlist.push_back(addrlist_v6_2);
addrlist.push_back(addrlist_v4_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(4u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[1].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[2].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily());
addrlist.clear();
addrlist.push_back(addrlist_v6_1);
addrlist.push_back(addrlist_v6_2);
addrlist.push_back(addrlist_v4_1);
addrlist.push_back(addrlist_v4_2);
TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
ASSERT_EQ(4u, addrlist.size());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[1].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[2].GetFamily());
EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily());
}
TEST_F(TransportClientSocketPoolTest, Basic) {
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
TestLoadTimingInfoConnectedNotReused(handle);
}
TEST_F(TransportClientSocketPoolTest, SetResolvePriorityOnInit) {
for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) {
RequestPriority priority = static_cast<RequestPriority>(i);
TestCompletionCallback callback;
ClientSocketHandle handle;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, priority, callback.callback(), &pool_,
BoundNetLog()));
EXPECT_EQ(priority, host_resolver_->last_request_priority());
}
}
TEST_F(TransportClientSocketPoolTest, InitHostResolutionFailure) {
host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name");
TestCompletionCallback callback;
ClientSocketHandle handle;
HostPortPair host_port_pair("unresolvable.host.name", 80);
scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
host_port_pair, false, false,
OnHostResolutionCallback()));
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", dest, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult());
}
TEST_F(TransportClientSocketPoolTest, InitConnectionFailure) {
client_socket_factory_.set_client_socket_type(
MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET);
TestCompletionCallback callback;
ClientSocketHandle handle;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
host_resolver_->set_synchronous_mode(true);
EXPECT_EQ(ERR_CONNECTION_FAILED,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
}
TEST_F(TransportClientSocketPoolTest, PendingRequests) {
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
host_resolver_->set_synchronous_mode(true);
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
EXPECT_EQ(kMaxSocketsPerGroup, client_socket_factory_.allocation_count());
EXPECT_EQ(11U, completion_count());
EXPECT_EQ(1, GetOrderOfRequest(1));
EXPECT_EQ(2, GetOrderOfRequest(2));
EXPECT_EQ(3, GetOrderOfRequest(3));
EXPECT_EQ(4, GetOrderOfRequest(4));
EXPECT_EQ(5, GetOrderOfRequest(5));
EXPECT_EQ(6, GetOrderOfRequest(6));
EXPECT_EQ(7, GetOrderOfRequest(7));
EXPECT_EQ(14, GetOrderOfRequest(8));
EXPECT_EQ(15, GetOrderOfRequest(9));
EXPECT_EQ(10, GetOrderOfRequest(10));
EXPECT_EQ(13, GetOrderOfRequest(11));
EXPECT_EQ(8, GetOrderOfRequest(12));
EXPECT_EQ(16, GetOrderOfRequest(13));
EXPECT_EQ(11, GetOrderOfRequest(14));
EXPECT_EQ(12, GetOrderOfRequest(15));
EXPECT_EQ(9, GetOrderOfRequest(16));
EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17));
}
TEST_F(TransportClientSocketPoolTest, PendingRequests_NoKeepAlive) {
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
host_resolver_->set_synchronous_mode(true);
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
EXPECT_EQ(OK, (*requests())[6]->WaitForResult());
EXPECT_EQ(OK, (*requests())[7]->WaitForResult());
EXPECT_EQ(OK, (*requests())[8]->WaitForResult());
EXPECT_EQ(OK, (*requests())[9]->WaitForResult());
EXPECT_EQ(OK, (*requests())[10]->WaitForResult());
EXPECT_EQ(static_cast<int>(requests()->size()),
client_socket_factory_.allocation_count());
EXPECT_EQ(6U, completion_count());
}
TEST_F(TransportClientSocketPoolTest, CancelRequestClearGroup) {
TestCompletionCallback callback;
ClientSocketHandle handle;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
handle.Reset();
}
TEST_F(TransportClientSocketPoolTest, TwoRequestsCancelOne) {
ClientSocketHandle handle;
TestCompletionCallback callback;
ClientSocketHandle handle2;
TestCompletionCallback callback2;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
EXPECT_EQ(ERR_IO_PENDING,
handle2.Init("a", params_, kDefaultPriority, callback2.callback(),
&pool_, BoundNetLog()));
handle.Reset();
EXPECT_EQ(OK, callback2.WaitForResult());
handle2.Reset();
}
TEST_F(TransportClientSocketPoolTest, ConnectCancelConnect) {
client_socket_factory_.set_client_socket_type(
MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET);
ClientSocketHandle handle;
TestCompletionCallback callback;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback.callback(),
&pool_, BoundNetLog()));
handle.Reset();
TestCompletionCallback callback2;
EXPECT_EQ(ERR_IO_PENDING,
handle.Init("a", params_, kDefaultPriority, callback2.callback(),
&pool_, BoundNetLog()));
host_resolver_->set_synchronous_mode(true);
EXPECT_EQ(OK, callback2.WaitForResult());
EXPECT_FALSE(callback.have_result());
handle.Reset();
}
TEST_F(TransportClientSocketPoolTest, CancelRequest) {
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
host_resolver_->set_synchronous_mode(true);
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
size_t index_to_cancel = kMaxSocketsPerGroup + 2;
EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized());
(*requests())[index_to_cancel]->handle()->Reset();
ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
EXPECT_EQ(kMaxSocketsPerGroup,
client_socket_factory_.allocation_count());
EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count());
EXPECT_EQ(1, GetOrderOfRequest(1));
EXPECT_EQ(2, GetOrderOfRequest(2));
EXPECT_EQ(3, GetOrderOfRequest(3));
EXPECT_EQ(4, GetOrderOfRequest(4));
EXPECT_EQ(5, GetOrderOfRequest(5));
EXPECT_EQ(6, GetOrderOfRequest(6));
EXPECT_EQ(14, GetOrderOfRequest(7));
EXPECT_EQ(7, GetOrderOfRequest(8));
EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound,
GetOrderOfRequest(9));
EXPECT_EQ(9, GetOrderOfRequest(10));
EXPECT_EQ(10, GetOrderOfRequest(11));
EXPECT_EQ(11, GetOrderOfRequest(12));
EXPECT_EQ(8, GetOrderOfRequest(13));
EXPECT_EQ(12, GetOrderOfRequest(14));
EXPECT_EQ(13, GetOrderOfRequest(15));
EXPECT_EQ(15, GetOrderOfRequest(16));
EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17));
}
class RequestSocketCallback : public TestCompletionCallbackBase {
public:
RequestSocketCallback(ClientSocketHandle* handle,
TransportClientSocketPool* pool)
: handle_(handle),
pool_(pool),
within_callback_(false),
callback_(base::Bind(&RequestSocketCallback::OnComplete,
base::Unretained(this))) {
}
virtual ~RequestSocketCallback() {}
const CompletionCallback& callback() const { return callback_; }
private:
void OnComplete(int result) {
SetResult(result);
ASSERT_EQ(OK, result);
if (!within_callback_) {
handle_->socket()->Disconnect();
handle_->Reset();
{
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current());
base::MessageLoop::current()->RunUntilIdle();
}
within_callback_ = true;
scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
HostPortPair("www.google.com", 80), false, false,
OnHostResolutionCallback()));
int rv = handle_->Init("a", dest, LOWEST, callback(), pool_,
BoundNetLog());
EXPECT_EQ(OK, rv);
}
}
ClientSocketHandle* const handle_;
TransportClientSocketPool* const pool_;
bool within_callback_;
CompletionCallback callback_;
DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback);
};
TEST_F(TransportClientSocketPoolTest, RequestTwice) {
ClientSocketHandle handle;
RequestSocketCallback callback(&handle, &pool_);
scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
HostPortPair("www.google.com", 80), false, false,
OnHostResolutionCallback()));
int rv = handle.Init("a", dest, LOWEST, callback.callback(), &pool_,
BoundNetLog());
ASSERT_EQ(ERR_IO_PENDING, rv);
host_resolver_->set_synchronous_mode(true);
EXPECT_EQ(OK, callback.WaitForResult());
handle.Reset();
}
TEST_F(TransportClientSocketPoolTest, CancelActiveRequestWithPendingRequests) {
client_socket_factory_.set_client_socket_type(
MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET);
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size()));
for (int i = 0; i < kMaxSocketsPerGroup; i++)
(*requests())[i]->handle()->Reset();
for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) {
EXPECT_EQ(OK, (*requests())[i]->WaitForResult());
(*requests())[i]->handle()->Reset();
}
EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count());
}
TEST_F(TransportClientSocketPoolTest, FailingActiveRequestWithPendingRequests) {
client_socket_factory_.set_client_socket_type(
MockClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET);
const int kNumRequests = 2 * kMaxSocketsPerGroup + 1;
ASSERT_LE(kNumRequests, kMaxSockets);
for (int i = 0; i < kNumRequests; i++)
EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
for (int i = 0; i < kNumRequests; i++)
EXPECT_EQ(ERR_CONNECTION_FAILED, (*requests())[i]->WaitForResult());
}
TEST_F(TransportClientSocketPoolTest, IdleSocketLoadTiming) {
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
TestLoadTimingInfoConnectedNotReused(handle);
handle.Reset();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(1, pool_.IdleSocketCount());
rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_EQ(0, pool_.IdleSocketCount());
TestLoadTimingInfoConnectedReused(handle);
}
TEST_F(TransportClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) {
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
handle.Reset();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(1, pool_.IdleSocketCount());
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, pool_.IdleSocketCount());
}
TEST_F(TransportClientSocketPoolTest, BackupSocketConnect) {
MockClientSocketFactory::ClientSocketType case1_types[] = {
MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
MockClientSocketFactory::MOCK_CLIENT_SOCKET
};
MockClientSocketFactory::ClientSocketType case2_types[] = {
MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
};
MockClientSocketFactory::ClientSocketType* cases[2] = {
case1_types,
case2_types
};
for (size_t index = 0; index < arraysize(cases); ++index) {
client_socket_factory_.set_client_socket_types(cases[index], 2);
EXPECT_EQ(0, pool_.IdleSocketCount());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("b", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
base::MessageLoop::current()->RunUntilIdle();
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
ClientSocketPool::kMaxConnectRetryIntervalMs + 50));
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
EXPECT_EQ(0, pool_.IdleSocketCount());
handle.Reset();
pool_.FlushWithError(ERR_NETWORK_CHANGED);
}
}
TEST_F(TransportClientSocketPoolTest, BackupSocketCancel) {
client_socket_factory_.set_client_socket_type(
MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET);
enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT };
for (int index = CANCEL_BEFORE_WAIT; index < CANCEL_AFTER_WAIT; ++index) {
EXPECT_EQ(0, pool_.IdleSocketCount());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("c", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
base::MessageLoop::current()->RunUntilIdle();
if (index == CANCEL_AFTER_WAIT) {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
ClientSocketPool::kMaxConnectRetryIntervalMs));
}
base::MessageLoop::current()->RunUntilIdle();
handle.Reset();
EXPECT_FALSE(callback.have_result());
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(0, pool_.IdleSocketCount());
}
}
TEST_F(TransportClientSocketPoolTest, BackupSocketFailAfterStall) {
MockClientSocketFactory::ClientSocketType case_types[] = {
MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
EXPECT_EQ(0, pool_.IdleSocketCount());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("b", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
base::MessageLoop::current()->RunUntilIdle();
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
ClientSocketPool::kMaxConnectRetryIntervalMs));
host_resolver_->set_synchronous_mode(true);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(0, pool_.IdleSocketCount());
handle.Reset();
host_resolver_->set_synchronous_mode(false);
}
TEST_F(TransportClientSocketPoolTest, BackupSocketFailAfterDelay) {
MockClientSocketFactory::ClientSocketType case_types[] = {
MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
client_socket_factory_.set_delay(base::TimeDelta::FromSeconds(5));
EXPECT_EQ(0, pool_.IdleSocketCount());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("b", params_, LOW, callback.callback(), &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
base::MessageLoop::current()->RunUntilIdle();
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
ClientSocketPool::kMaxConnectRetryIntervalMs));
host_resolver_->set_synchronous_mode(true);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
handle.Reset();
host_resolver_->set_synchronous_mode(false);
}
TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv4FinishesFirst) {
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
MockClientSocketFactory::ClientSocketType case_types[] = {
MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
host_resolver_->rules()
->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
EXPECT_EQ(2, client_socket_factory_.allocation_count());
}
TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv6FinishesFirst) {
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
MockClientSocketFactory::ClientSocketType case_types[] = {
MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
};
client_socket_factory_.set_client_socket_types(case_types, 2);
client_socket_factory_.set_delay(base::TimeDelta::FromMilliseconds(
TransportConnectJob::kIPv6FallbackTimerInMs + 50));
host_resolver_->rules()
->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
EXPECT_EQ(2, client_socket_factory_.allocation_count());
}
TEST_F(TransportClientSocketPoolTest, IPv6NoIPv4AddressesToFallbackTo) {
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
client_socket_factory_.set_client_socket_type(
MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
host_resolver_->rules()
->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
EXPECT_EQ(1, client_socket_factory_.allocation_count());
}
TEST_F(TransportClientSocketPoolTest, IPv4HasNoFallback) {
ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
TransportClientSocketPool pool(kMaxSockets,
kMaxSocketsPerGroup,
histograms_.get(),
host_resolver_.get(),
&client_socket_factory_,
NULL);
client_socket_factory_.set_client_socket_type(
MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
TestCompletionCallback callback;
ClientSocketHandle handle;
int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
IPEndPoint endpoint;
handle.socket()->GetLocalAddress(&endpoint);
EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
EXPECT_EQ(1, client_socket_factory_.allocation_count());
}
}
}