| 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 |
| 11 #include <stddef.h> | 11 #include <stddef.h> |
| 12 | 12 |
| 13 #include "net/base/ip_address.h" | 13 #include "net/base/ip_address.h" |
| 14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
| 17 #include "net/log/net_log_source.h" |
| 17 #include "net/socket/socket_performance_watcher.h" | 18 #include "net/socket/socket_performance_watcher.h" |
| 18 #include "net/socket/tcp_server_socket.h" | 19 #include "net/socket/tcp_server_socket.h" |
| 19 #include "net/test/gtest_util.h" | 20 #include "net/test/gtest_util.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using net::test::IsError; | 24 using net::test::IsError; |
| 24 using net::test::IsOk; | 25 using net::test::IsOk; |
| 25 | 26 |
| 26 namespace base { | 27 namespace base { |
| 27 class TimeDelta; | 28 class TimeDelta; |
| 28 } | 29 } |
| 29 | 30 |
| 30 namespace net { | 31 namespace net { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 // Try binding a socket to loopback interface and verify that we can | 35 // Try binding a socket to loopback interface and verify that we can |
| 35 // still connect to a server on the same interface. | 36 // still connect to a server on the same interface. |
| 36 TEST(TCPClientSocketTest, BindLoopbackToLoopback) { | 37 TEST(TCPClientSocketTest, BindLoopbackToLoopback) { |
| 37 IPAddress lo_address = IPAddress::IPv4Localhost(); | 38 IPAddress lo_address = IPAddress::IPv4Localhost(); |
| 38 | 39 |
| 39 TCPServerSocket server(NULL, NetLog::Source()); | 40 TCPServerSocket server(NULL, NetLogSource()); |
| 40 ASSERT_THAT(server.Listen(IPEndPoint(lo_address, 0), 1), IsOk()); | 41 ASSERT_THAT(server.Listen(IPEndPoint(lo_address, 0), 1), IsOk()); |
| 41 IPEndPoint server_address; | 42 IPEndPoint server_address; |
| 42 ASSERT_THAT(server.GetLocalAddress(&server_address), IsOk()); | 43 ASSERT_THAT(server.GetLocalAddress(&server_address), IsOk()); |
| 43 | 44 |
| 44 TCPClientSocket socket(AddressList(server_address), NULL, NULL, | 45 TCPClientSocket socket(AddressList(server_address), NULL, NULL, |
| 45 NetLog::Source()); | 46 NetLogSource()); |
| 46 | 47 |
| 47 EXPECT_THAT(socket.Bind(IPEndPoint(lo_address, 0)), IsOk()); | 48 EXPECT_THAT(socket.Bind(IPEndPoint(lo_address, 0)), IsOk()); |
| 48 | 49 |
| 49 IPEndPoint local_address_result; | 50 IPEndPoint local_address_result; |
| 50 EXPECT_THAT(socket.GetLocalAddress(&local_address_result), IsOk()); | 51 EXPECT_THAT(socket.GetLocalAddress(&local_address_result), IsOk()); |
| 51 EXPECT_EQ(lo_address, local_address_result.address()); | 52 EXPECT_EQ(lo_address, local_address_result.address()); |
| 52 | 53 |
| 53 TestCompletionCallback connect_callback; | 54 TestCompletionCallback connect_callback; |
| 54 EXPECT_THAT(socket.Connect(connect_callback.callback()), | 55 EXPECT_THAT(socket.Connect(connect_callback.callback()), |
| 55 IsError(ERR_IO_PENDING)); | 56 IsError(ERR_IO_PENDING)); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 68 EXPECT_FALSE(socket.IsConnected()); | 69 EXPECT_FALSE(socket.IsConnected()); |
| 69 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, | 70 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, |
| 70 socket.GetLocalAddress(&local_address_result)); | 71 socket.GetLocalAddress(&local_address_result)); |
| 71 } | 72 } |
| 72 | 73 |
| 73 // Try to bind socket to the loopback interface and connect to an | 74 // Try to bind socket to the loopback interface and connect to an |
| 74 // external address, verify that connection fails. | 75 // external address, verify that connection fails. |
| 75 TEST(TCPClientSocketTest, BindLoopbackToExternal) { | 76 TEST(TCPClientSocketTest, BindLoopbackToExternal) { |
| 76 IPAddress external_ip(72, 14, 213, 105); | 77 IPAddress external_ip(72, 14, 213, 105); |
| 77 TCPClientSocket socket(AddressList::CreateFromIPAddress(external_ip, 80), | 78 TCPClientSocket socket(AddressList::CreateFromIPAddress(external_ip, 80), |
| 78 NULL, NULL, NetLog::Source()); | 79 NULL, NULL, NetLogSource()); |
| 79 | 80 |
| 80 EXPECT_THAT(socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), IsOk()); | 81 EXPECT_THAT(socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), IsOk()); |
| 81 | 82 |
| 82 TestCompletionCallback connect_callback; | 83 TestCompletionCallback connect_callback; |
| 83 int result = socket.Connect(connect_callback.callback()); | 84 int result = socket.Connect(connect_callback.callback()); |
| 84 if (result == ERR_IO_PENDING) | 85 if (result == ERR_IO_PENDING) |
| 85 result = connect_callback.WaitForResult(); | 86 result = connect_callback.WaitForResult(); |
| 86 | 87 |
| 87 // We may get different errors here on different system, but | 88 // We may get different errors here on different system, but |
| 88 // connect() is not expected to succeed. | 89 // connect() is not expected to succeed. |
| 89 EXPECT_NE(OK, result); | 90 EXPECT_NE(OK, result); |
| 90 } | 91 } |
| 91 | 92 |
| 92 // Bind a socket to the IPv4 loopback interface and try to connect to | 93 // Bind a socket to the IPv4 loopback interface and try to connect to |
| 93 // the IPv6 loopback interface, verify that connection fails. | 94 // the IPv6 loopback interface, verify that connection fails. |
| 94 TEST(TCPClientSocketTest, BindLoopbackToIPv6) { | 95 TEST(TCPClientSocketTest, BindLoopbackToIPv6) { |
| 95 TCPServerSocket server(NULL, NetLog::Source()); | 96 TCPServerSocket server(NULL, NetLogSource()); |
| 96 int listen_result = | 97 int listen_result = |
| 97 server.Listen(IPEndPoint(IPAddress::IPv6Localhost(), 0), 1); | 98 server.Listen(IPEndPoint(IPAddress::IPv6Localhost(), 0), 1); |
| 98 if (listen_result != OK) { | 99 if (listen_result != OK) { |
| 99 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is disabled." | 100 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is disabled." |
| 100 " Skipping the test"; | 101 " Skipping the test"; |
| 101 return; | 102 return; |
| 102 } | 103 } |
| 103 | 104 |
| 104 IPEndPoint server_address; | 105 IPEndPoint server_address; |
| 105 ASSERT_THAT(server.GetLocalAddress(&server_address), IsOk()); | 106 ASSERT_THAT(server.GetLocalAddress(&server_address), IsOk()); |
| 106 TCPClientSocket socket(AddressList(server_address), NULL, NULL, | 107 TCPClientSocket socket(AddressList(server_address), NULL, NULL, |
| 107 NetLog::Source()); | 108 NetLogSource()); |
| 108 | 109 |
| 109 EXPECT_THAT(socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), IsOk()); | 110 EXPECT_THAT(socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), IsOk()); |
| 110 | 111 |
| 111 TestCompletionCallback connect_callback; | 112 TestCompletionCallback connect_callback; |
| 112 int result = socket.Connect(connect_callback.callback()); | 113 int result = socket.Connect(connect_callback.callback()); |
| 113 if (result == ERR_IO_PENDING) | 114 if (result == ERR_IO_PENDING) |
| 114 result = connect_callback.WaitForResult(); | 115 result = connect_callback.WaitForResult(); |
| 115 | 116 |
| 116 EXPECT_NE(OK, result); | 117 EXPECT_NE(OK, result); |
| 117 } | 118 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 IPAddressList ip_list; | 150 IPAddressList ip_list; |
| 150 for (size_t i = 0; i < kNumIPs; ++i) | 151 for (size_t i = 0; i < kNumIPs; ++i) |
| 151 ip_list.push_back(IPAddress(72, 14, 213, i)); | 152 ip_list.push_back(IPAddress(72, 14, 213, i)); |
| 152 | 153 |
| 153 std::unique_ptr<TestSocketPerformanceWatcher> watcher( | 154 std::unique_ptr<TestSocketPerformanceWatcher> watcher( |
| 154 new TestSocketPerformanceWatcher()); | 155 new TestSocketPerformanceWatcher()); |
| 155 TestSocketPerformanceWatcher* watcher_ptr = watcher.get(); | 156 TestSocketPerformanceWatcher* watcher_ptr = watcher.get(); |
| 156 | 157 |
| 157 TCPClientSocket socket( | 158 TCPClientSocket socket( |
| 158 AddressList::CreateFromIPAddressList(ip_list, "example.com"), | 159 AddressList::CreateFromIPAddressList(ip_list, "example.com"), |
| 159 std::move(watcher), NULL, NetLog::Source()); | 160 std::move(watcher), NULL, NetLogSource()); |
| 160 | 161 |
| 161 EXPECT_THAT(socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), IsOk()); | 162 EXPECT_THAT(socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), IsOk()); |
| 162 | 163 |
| 163 TestCompletionCallback connect_callback; | 164 TestCompletionCallback connect_callback; |
| 164 | 165 |
| 165 ASSERT_NE(OK, connect_callback.GetResult( | 166 ASSERT_NE(OK, connect_callback.GetResult( |
| 166 socket.Connect(connect_callback.callback()))); | 167 socket.Connect(connect_callback.callback()))); |
| 167 | 168 |
| 168 EXPECT_EQ(kNumIPs - 1, watcher_ptr->connection_changed_count()); | 169 EXPECT_EQ(kNumIPs - 1, watcher_ptr->connection_changed_count()); |
| 169 } | 170 } |
| 170 | 171 |
| 171 } // namespace | 172 } // namespace |
| 172 | 173 |
| 173 } // namespace net | 174 } // namespace net |
| OLD | NEW |