| 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 // This file contains some tests for TCPClientSocket. | 5 // This file contains some tests for TCPClientSocket. |
| 6 // transport_client_socket_unittest.cc contans some other tests that | 6 // transport_client_socket_unittest.cc contans some other tests that |
| 7 // are common for TCP and other types of sockets. | 7 // are common for TCP and other types of sockets. |
| 8 | 8 |
| 9 #include "net/socket/tcp_client_socket.h" | 9 #include "net/socket/tcp_client_socket.h" |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // Try binding a socket to loopback interface and verify that we can | 22 // Try binding a socket to loopback interface and verify that we can |
| 23 // still connect to a server on the same interface. | 23 // still connect to a server on the same interface. |
| 24 TEST(TCPClientSocketTest, BindLoopbackToLoopback) { | 24 TEST(TCPClientSocketTest, BindLoopbackToLoopback) { |
| 25 IPAddress lo_address = IPAddress::IPv4Localhost(); | 25 IPAddress lo_address = IPAddress::IPv4Localhost(); |
| 26 | 26 |
| 27 TCPServerSocket server(NULL, NetLog::Source()); | 27 TCPServerSocket server(NULL, NetLog::Source()); |
| 28 ASSERT_EQ(OK, server.Listen(IPEndPoint(lo_address, 0), 1)); | 28 ASSERT_EQ(OK, server.Listen(IPEndPoint(lo_address, 0), 1)); |
| 29 IPEndPoint server_address; | 29 IPEndPoint server_address; |
| 30 ASSERT_EQ(OK, server.GetLocalAddress(&server_address)); | 30 ASSERT_EQ(OK, server.GetLocalAddress(&server_address)); |
| 31 | 31 |
| 32 TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source()); | 32 TCPClientSocket socket(AddressList(server_address), NULL, NULL, |
| 33 NetLog::Source()); |
| 33 | 34 |
| 34 EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0))); | 35 EXPECT_EQ(OK, socket.Bind(IPEndPoint(lo_address, 0))); |
| 35 | 36 |
| 36 IPEndPoint local_address_result; | 37 IPEndPoint local_address_result; |
| 37 EXPECT_EQ(OK, socket.GetLocalAddress(&local_address_result)); | 38 EXPECT_EQ(OK, socket.GetLocalAddress(&local_address_result)); |
| 38 EXPECT_EQ(lo_address, local_address_result.address()); | 39 EXPECT_EQ(lo_address, local_address_result.address()); |
| 39 | 40 |
| 40 TestCompletionCallback connect_callback; | 41 TestCompletionCallback connect_callback; |
| 41 EXPECT_EQ(ERR_IO_PENDING, socket.Connect(connect_callback.callback())); | 42 EXPECT_EQ(ERR_IO_PENDING, socket.Connect(connect_callback.callback())); |
| 42 | 43 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 54 EXPECT_FALSE(socket.IsConnected()); | 55 EXPECT_FALSE(socket.IsConnected()); |
| 55 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, | 56 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, |
| 56 socket.GetLocalAddress(&local_address_result)); | 57 socket.GetLocalAddress(&local_address_result)); |
| 57 } | 58 } |
| 58 | 59 |
| 59 // Try to bind socket to the loopback interface and connect to an | 60 // Try to bind socket to the loopback interface and connect to an |
| 60 // external address, verify that connection fails. | 61 // external address, verify that connection fails. |
| 61 TEST(TCPClientSocketTest, BindLoopbackToExternal) { | 62 TEST(TCPClientSocketTest, BindLoopbackToExternal) { |
| 62 IPAddress external_ip(72, 14, 213, 105); | 63 IPAddress external_ip(72, 14, 213, 105); |
| 63 TCPClientSocket socket(AddressList::CreateFromIPAddress(external_ip, 80), | 64 TCPClientSocket socket(AddressList::CreateFromIPAddress(external_ip, 80), |
| 64 NULL, NetLog::Source()); | 65 NULL, NULL, NetLog::Source()); |
| 65 | 66 |
| 66 EXPECT_EQ(OK, socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0))); | 67 EXPECT_EQ(OK, socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0))); |
| 67 | 68 |
| 68 TestCompletionCallback connect_callback; | 69 TestCompletionCallback connect_callback; |
| 69 int result = socket.Connect(connect_callback.callback()); | 70 int result = socket.Connect(connect_callback.callback()); |
| 70 if (result == ERR_IO_PENDING) | 71 if (result == ERR_IO_PENDING) |
| 71 result = connect_callback.WaitForResult(); | 72 result = connect_callback.WaitForResult(); |
| 72 | 73 |
| 73 // We may get different errors here on different system, but | 74 // We may get different errors here on different system, but |
| 74 // connect() is not expected to succeed. | 75 // connect() is not expected to succeed. |
| 75 EXPECT_NE(OK, result); | 76 EXPECT_NE(OK, result); |
| 76 } | 77 } |
| 77 | 78 |
| 78 // Bind a socket to the IPv4 loopback interface and try to connect to | 79 // Bind a socket to the IPv4 loopback interface and try to connect to |
| 79 // the IPv6 loopback interface, verify that connection fails. | 80 // the IPv6 loopback interface, verify that connection fails. |
| 80 TEST(TCPClientSocketTest, BindLoopbackToIPv6) { | 81 TEST(TCPClientSocketTest, BindLoopbackToIPv6) { |
| 81 TCPServerSocket server(NULL, NetLog::Source()); | 82 TCPServerSocket server(NULL, NetLog::Source()); |
| 82 int listen_result = | 83 int listen_result = |
| 83 server.Listen(IPEndPoint(IPAddress::IPv6Localhost(), 0), 1); | 84 server.Listen(IPEndPoint(IPAddress::IPv6Localhost(), 0), 1); |
| 84 if (listen_result != OK) { | 85 if (listen_result != OK) { |
| 85 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is disabled." | 86 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is disabled." |
| 86 " Skipping the test"; | 87 " Skipping the test"; |
| 87 return; | 88 return; |
| 88 } | 89 } |
| 89 | 90 |
| 90 IPEndPoint server_address; | 91 IPEndPoint server_address; |
| 91 ASSERT_EQ(OK, server.GetLocalAddress(&server_address)); | 92 ASSERT_EQ(OK, server.GetLocalAddress(&server_address)); |
| 92 TCPClientSocket socket(AddressList(server_address), NULL, NetLog::Source()); | 93 TCPClientSocket socket(AddressList(server_address), NULL, NULL, |
| 94 NetLog::Source()); |
| 93 | 95 |
| 94 EXPECT_EQ(OK, socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0))); | 96 EXPECT_EQ(OK, socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0))); |
| 95 | 97 |
| 96 TestCompletionCallback connect_callback; | 98 TestCompletionCallback connect_callback; |
| 97 int result = socket.Connect(connect_callback.callback()); | 99 int result = socket.Connect(connect_callback.callback()); |
| 98 if (result == ERR_IO_PENDING) | 100 if (result == ERR_IO_PENDING) |
| 99 result = connect_callback.WaitForResult(); | 101 result = connect_callback.WaitForResult(); |
| 100 | 102 |
| 101 EXPECT_NE(OK, result); | 103 EXPECT_NE(OK, result); |
| 102 } | 104 } |
| 103 | 105 |
| 104 } // namespace | 106 } // namespace |
| 105 | 107 |
| 106 } // namespace net | 108 } // namespace net |
| OLD | NEW |