This source file includes following definitions.
- TEST
- TEST
- TEST
#include "net/socket/tcp_client_socket.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/base/test_completion_callback.h"
#include "net/socket/tcp_server_socket.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
TEST(TCPClientSocketTest, BindLoopbackToLoopback) {
IPAddressNumber lo_address;
ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address));
TCPServerSocket server(NULL, NetLog::Source());
ASSERT_EQ(OK, server.Listen(IPEndPoint(lo_address, 0), 1));
IPEndPoint server_address;
ASSERT_EQ(OK, server.GetLocalAddress(&server_address));
TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source());
EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0)));
IPEndPoint local_address_result;
EXPECT_EQ(OK, socket.GetLocalAddress(&local_address_result));
EXPECT_EQ(lo_address, local_address_result.address());
TestCompletionCallback connect_callback;
EXPECT_EQ(ERR_IO_PENDING, socket.Connect(connect_callback.callback()));
TestCompletionCallback accept_callback;
scoped_ptr<StreamSocket> accepted_socket;
int result = server.Accept(&accepted_socket, accept_callback.callback());
if (result == ERR_IO_PENDING)
result = accept_callback.WaitForResult();
ASSERT_EQ(OK, result);
EXPECT_EQ(OK, connect_callback.WaitForResult());
EXPECT_TRUE(socket.IsConnected());
socket.Disconnect();
EXPECT_FALSE(socket.IsConnected());
EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED,
socket.GetLocalAddress(&local_address_result));
}
TEST(TCPClientSocketTest, BindLoopbackToExternal) {
IPAddressNumber external_ip;
ASSERT_TRUE(ParseIPLiteralToNumber("72.14.213.105", &external_ip));
TCPClientSocket socket(AddressList::CreateFromIPAddress(external_ip, 80),
NULL, NetLog::Source());
IPAddressNumber lo_address;
ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address));
EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0)));
TestCompletionCallback connect_callback;
int result = socket.Connect(connect_callback.callback());
if (result == ERR_IO_PENDING)
result = connect_callback.WaitForResult();
EXPECT_NE(OK, result);
}
TEST(TCPClientSocketTest, BindLoopbackToIPv6) {
IPAddressNumber ipv6_lo_ip;
ASSERT_TRUE(ParseIPLiteralToNumber("::1", &ipv6_lo_ip));
TCPServerSocket server(NULL, NetLog::Source());
int listen_result = server.Listen(IPEndPoint(ipv6_lo_ip, 0), 1);
if (listen_result != OK) {
LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is disabled."
" Skipping the test";
return;
}
IPEndPoint server_address;
ASSERT_EQ(OK, server.GetLocalAddress(&server_address));
TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source());
IPAddressNumber ipv4_lo_ip;
ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &ipv4_lo_ip));
EXPECT_EQ(OK, socket.Bind(IPEndPoint(ipv4_lo_ip, 0)));
TestCompletionCallback connect_callback;
int result = socket.Connect(connect_callback.callback());
if (result == ERR_IO_PENDING)
result = connect_callback.WaitForResult();
EXPECT_NE(OK, result);
}
}
}