[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 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. |
| 8 | |
| 9 | #include "net/socket/tcp_client_socket.h" |
| 10 | |
| 11 | #include "net/base/ip_endpoint.h" |
| 12 | #include "net/base/net_errors.h" |
| 13 | #include "net/base/net_util.h" |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 14 | #include "net/base/test_completion_callback.h" |
| 15 | #include "net/socket/tcp_server_socket.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 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)); |
| 27 | |
| 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)); |
| 32 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame^] | 33 | TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source()); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 34 | |
| 35 | EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0))); |
| 36 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 37 | TestCompletionCallback connect_callback; |
| 38 | EXPECT_EQ(ERR_IO_PENDING, socket.Connect(connect_callback.callback())); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 39 | |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 40 | TestCompletionCallback accept_callback; |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 41 | scoped_ptr<StreamSocket> accepted_socket; |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 42 | int result = server.Accept(&accepted_socket, accept_callback.callback()); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 43 | if (result == ERR_IO_PENDING) |
| 44 | result = accept_callback.WaitForResult(); |
| 45 | ASSERT_EQ(OK, result); |
| 46 | |
| 47 | EXPECT_EQ(OK, connect_callback.WaitForResult()); |
| 48 | } |
| 49 | |
| 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()); |
| 57 | |
| 58 | IPAddressNumber lo_address; |
| 59 | ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &lo_address)); |
| 60 | EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0))); |
| 61 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 62 | TestCompletionCallback connect_callback; |
| 63 | int result = socket.Connect(connect_callback.callback()); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 64 | if (result == ERR_IO_PENDING) |
| 65 | result = connect_callback.WaitForResult(); |
| 66 | |
| 67 | // We may get different errors here on different system, but |
| 68 | // connect() is not expected to succeed. |
| 69 | EXPECT_NE(OK, result); |
| 70 | } |
| 71 | |
| 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." |
| 81 | " Skipping the test"; |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | IPEndPoint server_address; |
| 86 | ASSERT_EQ(OK, server.GetLocalAddress(&server_address)); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame^] | 87 | TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source()); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 88 | |
| 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))); |
| 92 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 93 | TestCompletionCallback connect_callback; |
| 94 | int result = socket.Connect(connect_callback.callback()); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 95 | if (result == ERR_IO_PENDING) |
| 96 | result = connect_callback.WaitForResult(); |
| 97 | |
| 98 | EXPECT_NE(OK, result); |
| 99 | } |
| 100 | |
| 101 | } // namespace |
| 102 | |
| 103 | } // namespace net |