[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 1 | // Copyright 2013 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. |
| 4 | |
| 5 | #include "net/socket/tcp_socket.h" |
| 6 | |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 8 | #include <string.h> |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 9 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 10 | #include <memory> |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 15 | #include "base/time/time.h" |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 16 | #include "net/base/address_list.h" |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 17 | #include "net/base/io_buffer.h" |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 18 | #include "net/base/ip_endpoint.h" |
| 19 | #include "net/base/net_errors.h" |
tfarina | 3d87d7cd | 2016-01-13 02:26:59 | [diff] [blame] | 20 | #include "net/base/sockaddr_storage.h" |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 21 | #include "net/base/test_completion_callback.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 22 | #include "net/log/net_log_source.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 23 | #include "net/socket/socket_performance_watcher.h" |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 24 | #include "net/socket/tcp_client_socket.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 25 | #include "net/test/gtest_util.h" |
| 26 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 27 | #include "testing/gtest/include/gtest/gtest.h" |
| 28 | #include "testing/platform_test.h" |
| 29 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 30 | using net::test::IsError; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 31 | using net::test::IsOk; |
| 32 | |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 33 | namespace net { |
| 34 | |
| 35 | namespace { |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 36 | |
| 37 | class TestSocketPerformanceWatcher : public SocketPerformanceWatcher { |
| 38 | public: |
| 39 | explicit TestSocketPerformanceWatcher(bool should_notify_updated_rtt) |
| 40 | : should_notify_updated_rtt_(should_notify_updated_rtt), |
| 41 | connection_changed_count_(0u), |
| 42 | rtt_notification_count_(0u) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame^] | 43 | ~TestSocketPerformanceWatcher() override = default; |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 44 | |
| 45 | bool ShouldNotifyUpdatedRTT() const override { |
| 46 | return should_notify_updated_rtt_; |
| 47 | } |
| 48 | |
| 49 | void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override { |
| 50 | rtt_notification_count_++; |
| 51 | } |
| 52 | |
| 53 | void OnConnectionChanged() override { connection_changed_count_++; } |
| 54 | |
| 55 | size_t rtt_notification_count() const { return rtt_notification_count_; } |
| 56 | |
| 57 | size_t connection_changed_count() const { return connection_changed_count_; } |
| 58 | |
| 59 | private: |
| 60 | const bool should_notify_updated_rtt_; |
| 61 | size_t connection_changed_count_; |
| 62 | size_t rtt_notification_count_; |
| 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(TestSocketPerformanceWatcher); |
| 65 | }; |
| 66 | |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 67 | const int kListenBacklog = 5; |
| 68 | |
| 69 | class TCPSocketTest : public PlatformTest { |
| 70 | protected: |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 71 | TCPSocketTest() : socket_(nullptr, nullptr, NetLogSource()) {} |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 72 | |
| 73 | void SetUpListenIPv4() { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 74 | ASSERT_THAT(socket_.Open(ADDRESS_FAMILY_IPV4), IsOk()); |
| 75 | ASSERT_THAT(socket_.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), |
| 76 | IsOk()); |
| 77 | ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk()); |
| 78 | ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void SetUpListenIPv6(bool* success) { |
| 82 | *success = false; |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 83 | |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 84 | if (socket_.Open(ADDRESS_FAMILY_IPV6) != OK || |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 85 | socket_.Bind(IPEndPoint(IPAddress::IPv6Localhost(), 0)) != OK || |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 86 | socket_.Listen(kListenBacklog) != OK) { |
| 87 | LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is " |
| 88 | "disabled. Skipping the test"; |
| 89 | return; |
| 90 | } |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 91 | ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 92 | *success = true; |
| 93 | } |
| 94 | |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 95 | void TestAcceptAsync() { |
| 96 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 97 | std::unique_ptr<TCPSocket> accepted_socket; |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 98 | IPEndPoint accepted_address; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 99 | ASSERT_THAT(socket_.Accept(&accepted_socket, &accepted_address, |
| 100 | accept_callback.callback()), |
| 101 | IsError(ERR_IO_PENDING)); |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 102 | |
| 103 | TestCompletionCallback connect_callback; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 104 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 105 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 106 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
| 107 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 108 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 109 | EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 110 | |
| 111 | EXPECT_TRUE(accepted_socket.get()); |
| 112 | |
| 113 | // Both sockets should be on the loopback network interface. |
| 114 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 115 | } |
| 116 | |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 117 | #if defined(TCP_INFO) || defined(OS_LINUX) |
| 118 | // Tests that notifications to Socket Performance Watcher (SPW) are delivered |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 119 | // correctly. |should_notify_updated_rtt| is true if the SPW is interested in |
| 120 | // receiving RTT notifications. |num_messages| is the number of messages that |
| 121 | // are written/read by the sockets. |expect_connection_changed_count| is the |
| 122 | // expected number of connection change notifications received by the SPW. |
| 123 | // |expect_rtt_notification_count| is the expected number of RTT |
| 124 | // notifications received by the SPW. This test works by writing |
| 125 | // |num_messages| to the socket. A different socket (with a SPW attached to |
| 126 | // it) reads the messages. |
| 127 | void TestSPWNotifications(bool should_notify_updated_rtt, |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 128 | size_t num_messages, |
| 129 | size_t expect_connection_changed_count, |
| 130 | size_t expect_rtt_notification_count) { |
| 131 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 132 | |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 133 | TestCompletionCallback connect_callback; |
| 134 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 135 | std::unique_ptr<TestSocketPerformanceWatcher> watcher( |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 136 | new TestSocketPerformanceWatcher(should_notify_updated_rtt)); |
| 137 | TestSocketPerformanceWatcher* watcher_ptr = watcher.get(); |
| 138 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 139 | TCPSocket connecting_socket(std::move(watcher), nullptr, NetLogSource()); |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 140 | |
| 141 | int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 142 | ASSERT_THAT(result, IsOk()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 143 | int connect_result = |
| 144 | connecting_socket.Connect(local_address_, connect_callback.callback()); |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 145 | |
| 146 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 147 | std::unique_ptr<TCPSocket> accepted_socket; |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 148 | IPEndPoint accepted_address; |
| 149 | result = socket_.Accept(&accepted_socket, &accepted_address, |
| 150 | accept_callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 151 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 152 | |
| 153 | ASSERT_TRUE(accepted_socket.get()); |
| 154 | |
| 155 | // Both sockets should be on the loopback network interface. |
| 156 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 157 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 158 | ASSERT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 159 | |
| 160 | for (size_t i = 0; i < num_messages; ++i) { |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 161 | // Use a 1 byte message so that the watcher is notified at most once per |
| 162 | // message. |
| 163 | const std::string message("t"); |
| 164 | |
| 165 | scoped_refptr<IOBufferWithSize> write_buffer( |
| 166 | new IOBufferWithSize(message.size())); |
| 167 | memmove(write_buffer->data(), message.data(), message.size()); |
| 168 | |
| 169 | TestCompletionCallback write_callback; |
| 170 | int write_result = accepted_socket->Write( |
| 171 | write_buffer.get(), write_buffer->size(), write_callback.callback()); |
| 172 | |
| 173 | scoped_refptr<IOBufferWithSize> read_buffer( |
| 174 | new IOBufferWithSize(message.size())); |
| 175 | TestCompletionCallback read_callback; |
| 176 | int read_result = connecting_socket.Read( |
| 177 | read_buffer.get(), read_buffer->size(), read_callback.callback()); |
| 178 | |
| 179 | ASSERT_EQ(1, write_callback.GetResult(write_result)); |
| 180 | ASSERT_EQ(1, read_callback.GetResult(read_result)); |
| 181 | } |
| 182 | EXPECT_EQ(expect_connection_changed_count, |
| 183 | watcher_ptr->connection_changed_count()); |
| 184 | EXPECT_EQ(expect_rtt_notification_count, |
| 185 | watcher_ptr->rtt_notification_count()); |
| 186 | } |
| 187 | #endif // defined(TCP_INFO) || defined(OS_LINUX) |
| 188 | |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 189 | AddressList local_address_list() const { |
| 190 | return AddressList(local_address_); |
| 191 | } |
| 192 | |
| 193 | TCPSocket socket_; |
| 194 | IPEndPoint local_address_; |
| 195 | }; |
| 196 | |
| 197 | // Test listening and accepting with a socket bound to an IPv4 address. |
| 198 | TEST_F(TCPSocketTest, Accept) { |
| 199 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 200 | |
| 201 | TestCompletionCallback connect_callback; |
| 202 | // TODO(yzshen): Switch to use TCPSocket when it supports client socket |
| 203 | // operations. |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 204 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 205 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 206 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 207 | |
| 208 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 209 | std::unique_ptr<TCPSocket> accepted_socket; |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 210 | IPEndPoint accepted_address; |
| 211 | int result = socket_.Accept(&accepted_socket, &accepted_address, |
| 212 | accept_callback.callback()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 213 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 214 | |
| 215 | EXPECT_TRUE(accepted_socket.get()); |
| 216 | |
| 217 | // Both sockets should be on the loopback network interface. |
| 218 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 219 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 220 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Test Accept() callback. |
| 224 | TEST_F(TCPSocketTest, AcceptAsync) { |
| 225 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 226 | TestAcceptAsync(); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 227 | } |
| 228 | |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 229 | // Test AdoptConnectedSocket() |
| 230 | TEST_F(TCPSocketTest, AdoptConnectedSocket) { |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 231 | TCPSocket accepting_socket(nullptr, nullptr, NetLogSource()); |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 232 | ASSERT_THAT(accepting_socket.Open(ADDRESS_FAMILY_IPV4), IsOk()); |
| 233 | ASSERT_THAT(accepting_socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), |
| 234 | IsOk()); |
| 235 | ASSERT_THAT(accepting_socket.GetLocalAddress(&local_address_), IsOk()); |
| 236 | ASSERT_THAT(accepting_socket.Listen(kListenBacklog), IsOk()); |
| 237 | |
| 238 | TestCompletionCallback connect_callback; |
| 239 | // TODO(yzshen): Switch to use TCPSocket when it supports client socket |
| 240 | // operations. |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 241 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 242 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 243 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 244 | |
| 245 | TestCompletionCallback accept_callback; |
| 246 | std::unique_ptr<TCPSocket> accepted_socket; |
| 247 | IPEndPoint accepted_address; |
| 248 | int result = accepting_socket.Accept(&accepted_socket, &accepted_address, |
| 249 | accept_callback.callback()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 250 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 251 | |
| 252 | SocketDescriptor accepted_descriptor = |
| 253 | accepted_socket->ReleaseSocketDescriptorForTesting(); |
| 254 | |
| 255 | ASSERT_THAT( |
| 256 | socket_.AdoptConnectedSocket(accepted_descriptor, accepted_address), |
| 257 | IsOk()); |
| 258 | |
| 259 | // socket_ should now have the local address. |
| 260 | IPEndPoint adopted_address; |
| 261 | ASSERT_THAT(socket_.GetLocalAddress(&adopted_address), IsOk()); |
| 262 | EXPECT_EQ(local_address_.address(), adopted_address.address()); |
| 263 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 264 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Test Accept() for AdoptUnconnectedSocket. |
| 268 | TEST_F(TCPSocketTest, AcceptForAdoptedUnconnectedSocket) { |
| 269 | SocketDescriptor existing_socket = |
| 270 | CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 271 | ASSERT_THAT(socket_.AdoptUnconnectedSocket(existing_socket), IsOk()); |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 272 | |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 273 | IPEndPoint address(IPAddress::IPv4Localhost(), 0); |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 274 | SockaddrStorage storage; |
| 275 | ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len)); |
| 276 | ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len)); |
| 277 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 278 | ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk()); |
| 279 | ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 280 | |
| 281 | TestAcceptAsync(); |
| 282 | } |
[email protected] | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 283 | |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 284 | // Accept two connections simultaneously. |
| 285 | TEST_F(TCPSocketTest, Accept2Connections) { |
| 286 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 287 | |
| 288 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 289 | std::unique_ptr<TCPSocket> accepted_socket; |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 290 | IPEndPoint accepted_address; |
| 291 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 292 | ASSERT_THAT(socket_.Accept(&accepted_socket, &accepted_address, |
| 293 | accept_callback.callback()), |
| 294 | IsError(ERR_IO_PENDING)); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 295 | |
| 296 | TestCompletionCallback connect_callback; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 297 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 298 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 299 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 300 | |
| 301 | TestCompletionCallback connect_callback2; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 302 | TCPClientSocket connecting_socket2(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 303 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 304 | int connect_result2 = |
| 305 | connecting_socket2.Connect(connect_callback2.callback()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 306 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 307 | EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 308 | |
| 309 | TestCompletionCallback accept_callback2; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 310 | std::unique_ptr<TCPSocket> accepted_socket2; |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 311 | IPEndPoint accepted_address2; |
| 312 | |
| 313 | int result = socket_.Accept(&accepted_socket2, &accepted_address2, |
| 314 | accept_callback2.callback()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 315 | ASSERT_THAT(accept_callback2.GetResult(result), IsOk()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 316 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 317 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
| 318 | EXPECT_THAT(connect_callback2.GetResult(connect_result2), IsOk()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 319 | |
| 320 | EXPECT_TRUE(accepted_socket.get()); |
| 321 | EXPECT_TRUE(accepted_socket2.get()); |
| 322 | EXPECT_NE(accepted_socket.get(), accepted_socket2.get()); |
| 323 | |
| 324 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 325 | EXPECT_EQ(accepted_address2.address(), local_address_.address()); |
| 326 | } |
| 327 | |
| 328 | // Test listening and accepting with a socket bound to an IPv6 address. |
| 329 | TEST_F(TCPSocketTest, AcceptIPv6) { |
| 330 | bool initialized = false; |
| 331 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv6(&initialized)); |
| 332 | if (!initialized) |
| 333 | return; |
| 334 | |
| 335 | TestCompletionCallback connect_callback; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 336 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 337 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 338 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 339 | |
| 340 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 341 | std::unique_ptr<TCPSocket> accepted_socket; |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 342 | IPEndPoint accepted_address; |
| 343 | int result = socket_.Accept(&accepted_socket, &accepted_address, |
| 344 | accept_callback.callback()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 345 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 346 | |
| 347 | EXPECT_TRUE(accepted_socket.get()); |
| 348 | |
| 349 | // Both sockets should be on the loopback network interface. |
| 350 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 351 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 352 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 353 | } |
| 354 | |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 355 | TEST_F(TCPSocketTest, ReadWrite) { |
| 356 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 357 | |
| 358 | TestCompletionCallback connect_callback; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 359 | TCPSocket connecting_socket(nullptr, nullptr, NetLogSource()); |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 360 | int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 361 | ASSERT_THAT(result, IsOk()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 362 | int connect_result = |
| 363 | connecting_socket.Connect(local_address_, connect_callback.callback()); |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 364 | |
| 365 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 366 | std::unique_ptr<TCPSocket> accepted_socket; |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 367 | IPEndPoint accepted_address; |
| 368 | result = socket_.Accept(&accepted_socket, &accepted_address, |
| 369 | accept_callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 370 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 371 | |
| 372 | ASSERT_TRUE(accepted_socket.get()); |
| 373 | |
| 374 | // Both sockets should be on the loopback network interface. |
| 375 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 376 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 377 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
[email protected] | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 378 | |
| 379 | const std::string message("test message"); |
| 380 | std::vector<char> buffer(message.size()); |
| 381 | |
| 382 | size_t bytes_written = 0; |
| 383 | while (bytes_written < message.size()) { |
| 384 | scoped_refptr<IOBufferWithSize> write_buffer( |
| 385 | new IOBufferWithSize(message.size() - bytes_written)); |
| 386 | memmove(write_buffer->data(), message.data() + bytes_written, |
| 387 | message.size() - bytes_written); |
| 388 | |
| 389 | TestCompletionCallback write_callback; |
| 390 | int write_result = accepted_socket->Write( |
| 391 | write_buffer.get(), write_buffer->size(), write_callback.callback()); |
| 392 | write_result = write_callback.GetResult(write_result); |
| 393 | ASSERT_TRUE(write_result >= 0); |
| 394 | bytes_written += write_result; |
| 395 | ASSERT_TRUE(bytes_written <= message.size()); |
| 396 | } |
| 397 | |
| 398 | size_t bytes_read = 0; |
| 399 | while (bytes_read < message.size()) { |
| 400 | scoped_refptr<IOBufferWithSize> read_buffer( |
| 401 | new IOBufferWithSize(message.size() - bytes_read)); |
| 402 | TestCompletionCallback read_callback; |
| 403 | int read_result = connecting_socket.Read( |
| 404 | read_buffer.get(), read_buffer->size(), read_callback.callback()); |
| 405 | read_result = read_callback.GetResult(read_result); |
| 406 | ASSERT_TRUE(read_result >= 0); |
| 407 | ASSERT_TRUE(bytes_read + read_result <= message.size()); |
| 408 | memmove(&buffer[bytes_read], read_buffer->data(), read_result); |
| 409 | bytes_read += read_result; |
| 410 | } |
| 411 | |
| 412 | std::string received_message(buffer.begin(), buffer.end()); |
| 413 | ASSERT_EQ(message, received_message); |
| 414 | } |
| 415 | |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 416 | // These tests require kernel support for tcp_info struct, and so they are |
| 417 | // enabled only on certain platforms. |
| 418 | #if defined(TCP_INFO) || defined(OS_LINUX) |
| 419 | // If SocketPerformanceWatcher::ShouldNotifyUpdatedRTT always returns false, |
| 420 | // then the wtatcher should not receive any notifications. |
| 421 | TEST_F(TCPSocketTest, SPWNotInterested) { |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 422 | TestSPWNotifications(false, 2u, 0u, 0u); |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | // One notification should be received when the socket connects. One |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 426 | // additional notification should be received for each message read. |
| 427 | TEST_F(TCPSocketTest, SPWNoAdvance) { |
| 428 | TestSPWNotifications(true, 2u, 0u, 3u); |
tbansal | 7b403bcc | 2016-04-13 22:33:21 | [diff] [blame] | 429 | } |
| 430 | #endif // defined(TCP_INFO) || defined(OS_LINUX) |
| 431 | |
[email protected] | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 432 | } // namespace |
| 433 | } // namespace net |