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
3 // found in the LICENSE file.
5 // This file contains some tests for TCPClientSocket.
6 // transport_client_socket_unittest.cc contans some other tests that
7 // are common for TCP and other types of sockets.
9 #include "net/socket/tcp_client_socket.h"
11 #include "net/base/ip_endpoint.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/net_util.h"
14 #include "net/base/test_completion_callback.h"
15 #include "net/socket/tcp_server_socket.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 // Try binding a socket to loopback interface and verify that we can
23 // still connect to a server on the same interface.
24 TEST(TCPClientSocketTest
, BindLoopbackToLoopback
) {
25 IPAddressNumber lo_address
;
26 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address
));
28 TCPServerSocket
server(NULL
, NetLog::Source());
29 ASSERT_EQ(OK
, server
.Listen(IPEndPoint(lo_address
, 0), 1));
30 IPEndPoint server_address
;
31 ASSERT_EQ(OK
, server
.GetLocalAddress(&server_address
));
33 TCPClientSocket
socket(AddressList(server_address
), NULL
, NetLog::Source());
35 EXPECT_EQ(OK
, socket
.Bind(IPEndPoint(lo_address
, 0)));
37 TestCompletionCallback connect_callback
;
38 EXPECT_EQ(ERR_IO_PENDING
, socket
.Connect(connect_callback
.callback()));
40 TestCompletionCallback accept_callback
;
41 scoped_ptr
<StreamSocket
> accepted_socket
;
42 int result
= server
.Accept(&accepted_socket
, accept_callback
.callback());
43 if (result
== ERR_IO_PENDING
)
44 result
= accept_callback
.WaitForResult();
45 ASSERT_EQ(OK
, result
);
47 EXPECT_EQ(OK
, connect_callback
.WaitForResult());
50 // Try to bind socket to the loopback interface and connect to an
51 // external address, verify that connection fails.
52 TEST(TCPClientSocketTest
, BindLoopbackToExternal
) {
53 IPAddressNumber external_ip
;
54 ASSERT_TRUE(ParseIPLiteralToNumber("72.14.213.105", &external_ip
));
55 TCPClientSocket
socket(AddressList::CreateFromIPAddress(external_ip
, 80),
56 NULL
, NetLog::Source());
58 IPAddressNumber lo_address
;
59 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address
));
60 EXPECT_EQ(OK
, socket
.Bind(IPEndPoint(lo_address
, 0)));
62 TestCompletionCallback connect_callback
;
63 int result
= socket
.Connect(connect_callback
.callback());
64 if (result
== ERR_IO_PENDING
)
65 result
= connect_callback
.WaitForResult();
67 // We may get different errors here on different system, but
68 // connect() is not expected to succeed.
69 EXPECT_NE(OK
, result
);
72 // Bind a socket to the IPv4 loopback interface and try to connect to
73 // the IPv6 loopback interface, verify that connection fails.
74 TEST(TCPClientSocketTest
, BindLoopbackToIPv6
) {
75 IPAddressNumber ipv6_lo_ip
;
76 ASSERT_TRUE(ParseIPLiteralToNumber("::1", &ipv6_lo_ip
));
77 TCPServerSocket
server(NULL
, NetLog::Source());
78 int listen_result
= server
.Listen(IPEndPoint(ipv6_lo_ip
, 0), 1);
79 if (listen_result
!= OK
) {
80 LOG(ERROR
) << "Failed to listen on ::1 - probably because IPv6 is disabled."
85 IPEndPoint server_address
;
86 ASSERT_EQ(OK
, server
.GetLocalAddress(&server_address
));
87 TCPClientSocket
socket(AddressList(server_address
), NULL
, NetLog::Source());
89 IPAddressNumber ipv4_lo_ip
;
90 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &ipv4_lo_ip
));
91 EXPECT_EQ(OK
, socket
.Bind(IPEndPoint(ipv4_lo_ip
, 0)));
93 TestCompletionCallback connect_callback
;
94 int result
= socket
.Connect(connect_callback
.callback());
95 if (result
== ERR_IO_PENDING
)
96 result
= connect_callback
.WaitForResult();
98 EXPECT_NE(OK
, result
);