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