yzshen@chromium.org | 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 | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 7 | #include <stddef.h> |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 8 | #include <string.h> |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 9 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 10 | #include <memory> |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "base/memory/ref_counted.h" |
Matt Menke | b92dda2dd | 2018-09-18 17:04:42 | [diff] [blame] | 15 | #include "base/test/bind_test_util.h" |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 16 | #include "base/time/time.h" |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 17 | #include "build/build_config.h" |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 18 | #include "net/base/address_list.h" |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 19 | #include "net/base/io_buffer.h" |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 20 | #include "net/base/ip_endpoint.h" |
| 21 | #include "net/base/net_errors.h" |
tfarina | 3d87d7cd | 2016-01-13 02:26:59 | [diff] [blame] | 22 | #include "net/base/sockaddr_storage.h" |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 23 | #include "net/base/test_completion_callback.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 24 | #include "net/log/net_log_source.h" |
Wez | d174213 | 2019-01-18 02:50:46 | [diff] [blame] | 25 | #include "net/socket/socket_descriptor.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 26 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 27 | #include "net/socket/socket_test_util.h" |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 28 | #include "net/socket/tcp_client_socket.h" |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 29 | #include "net/test/embedded_test_server/embedded_test_server.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 30 | #include "net/test/gtest_util.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 31 | #include "net/test/test_with_scoped_task_environment.h" |
rhalavati@google.com | 690bbea | 2017-12-08 09:05:46 | [diff] [blame] | 32 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 33 | #include "testing/gmock/include/gmock/gmock.h" |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 34 | #include "testing/gtest/include/gtest/gtest.h" |
| 35 | #include "testing/platform_test.h" |
| 36 | |
Matt Menke | b92dda2dd | 2018-09-18 17:04:42 | [diff] [blame] | 37 | // For getsockopt() call. |
| 38 | #if defined(OS_WIN) |
| 39 | #include <winsock2.h> |
| 40 | #else // !defined(OS_WIN) |
| 41 | #include <sys/socket.h> |
| 42 | #endif // !defined(OS_WIN) |
| 43 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 44 | using net::test::IsError; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 45 | using net::test::IsOk; |
| 46 | |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 47 | namespace net { |
| 48 | |
| 49 | namespace { |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 50 | |
Matt Menke | e0673201 | 2018-04-06 21:41:24 | [diff] [blame] | 51 | // IOBuffer with the ability to invoke a callback when destroyed. Useful for |
| 52 | // checking for leaks. |
| 53 | class IOBufferWithDestructionCallback : public IOBufferWithSize { |
| 54 | public: |
| 55 | explicit IOBufferWithDestructionCallback(base::OnceClosure on_destroy_closure) |
| 56 | : IOBufferWithSize(1024), |
| 57 | on_destroy_closure_(std::move(on_destroy_closure)) { |
| 58 | DCHECK(on_destroy_closure_); |
| 59 | } |
| 60 | |
| 61 | protected: |
| 62 | ~IOBufferWithDestructionCallback() override { |
| 63 | std::move(on_destroy_closure_).Run(); |
| 64 | } |
| 65 | |
| 66 | base::OnceClosure on_destroy_closure_; |
| 67 | }; |
| 68 | |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 69 | class TestSocketPerformanceWatcher : public SocketPerformanceWatcher { |
| 70 | public: |
| 71 | explicit TestSocketPerformanceWatcher(bool should_notify_updated_rtt) |
| 72 | : should_notify_updated_rtt_(should_notify_updated_rtt), |
| 73 | connection_changed_count_(0u), |
| 74 | rtt_notification_count_(0u) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 75 | ~TestSocketPerformanceWatcher() override = default; |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 76 | |
| 77 | bool ShouldNotifyUpdatedRTT() const override { |
| 78 | return should_notify_updated_rtt_; |
| 79 | } |
| 80 | |
| 81 | void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override { |
| 82 | rtt_notification_count_++; |
| 83 | } |
| 84 | |
| 85 | void OnConnectionChanged() override { connection_changed_count_++; } |
| 86 | |
| 87 | size_t rtt_notification_count() const { return rtt_notification_count_; } |
| 88 | |
| 89 | size_t connection_changed_count() const { return connection_changed_count_; } |
| 90 | |
| 91 | private: |
| 92 | const bool should_notify_updated_rtt_; |
| 93 | size_t connection_changed_count_; |
| 94 | size_t rtt_notification_count_; |
| 95 | |
| 96 | DISALLOW_COPY_AND_ASSIGN(TestSocketPerformanceWatcher); |
| 97 | }; |
| 98 | |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 99 | const int kListenBacklog = 5; |
| 100 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 101 | class TCPSocketTest : public PlatformTest, public WithScopedTaskEnvironment { |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 102 | protected: |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 103 | TCPSocketTest() : socket_(nullptr, nullptr, NetLogSource()) {} |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 104 | |
| 105 | void SetUpListenIPv4() { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 106 | ASSERT_THAT(socket_.Open(ADDRESS_FAMILY_IPV4), IsOk()); |
| 107 | ASSERT_THAT(socket_.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), |
| 108 | IsOk()); |
| 109 | ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk()); |
| 110 | ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void SetUpListenIPv6(bool* success) { |
| 114 | *success = false; |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 115 | |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 116 | if (socket_.Open(ADDRESS_FAMILY_IPV6) != OK || |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 117 | socket_.Bind(IPEndPoint(IPAddress::IPv6Localhost(), 0)) != OK || |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 118 | socket_.Listen(kListenBacklog) != OK) { |
| 119 | LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is " |
| 120 | "disabled. Skipping the test"; |
| 121 | return; |
| 122 | } |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 123 | ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 124 | *success = true; |
| 125 | } |
| 126 | |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 127 | void TestAcceptAsync() { |
| 128 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 129 | std::unique_ptr<TCPSocket> accepted_socket; |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 130 | IPEndPoint accepted_address; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 131 | ASSERT_THAT(socket_.Accept(&accepted_socket, &accepted_address, |
| 132 | accept_callback.callback()), |
| 133 | IsError(ERR_IO_PENDING)); |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 134 | |
| 135 | TestCompletionCallback connect_callback; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 136 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 137 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 138 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
| 139 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 140 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 141 | EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 142 | |
| 143 | EXPECT_TRUE(accepted_socket.get()); |
| 144 | |
| 145 | // Both sockets should be on the loopback network interface. |
| 146 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 147 | } |
| 148 | |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 149 | #if defined(TCP_INFO) || defined(OS_LINUX) |
| 150 | // Tests that notifications to Socket Performance Watcher (SPW) are delivered |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 151 | // correctly. |should_notify_updated_rtt| is true if the SPW is interested in |
| 152 | // receiving RTT notifications. |num_messages| is the number of messages that |
| 153 | // are written/read by the sockets. |expect_connection_changed_count| is the |
| 154 | // expected number of connection change notifications received by the SPW. |
| 155 | // |expect_rtt_notification_count| is the expected number of RTT |
| 156 | // notifications received by the SPW. This test works by writing |
| 157 | // |num_messages| to the socket. A different socket (with a SPW attached to |
| 158 | // it) reads the messages. |
| 159 | void TestSPWNotifications(bool should_notify_updated_rtt, |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 160 | size_t num_messages, |
| 161 | size_t expect_connection_changed_count, |
| 162 | size_t expect_rtt_notification_count) { |
| 163 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 164 | |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 165 | TestCompletionCallback connect_callback; |
| 166 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 167 | std::unique_ptr<TestSocketPerformanceWatcher> watcher( |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 168 | new TestSocketPerformanceWatcher(should_notify_updated_rtt)); |
| 169 | TestSocketPerformanceWatcher* watcher_ptr = watcher.get(); |
| 170 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 171 | TCPSocket connecting_socket(std::move(watcher), nullptr, NetLogSource()); |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 172 | |
| 173 | int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 174 | ASSERT_THAT(result, IsOk()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 175 | int connect_result = |
| 176 | connecting_socket.Connect(local_address_, connect_callback.callback()); |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 177 | |
| 178 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 179 | std::unique_ptr<TCPSocket> accepted_socket; |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 180 | IPEndPoint accepted_address; |
| 181 | result = socket_.Accept(&accepted_socket, &accepted_address, |
| 182 | accept_callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 183 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 184 | |
| 185 | ASSERT_TRUE(accepted_socket.get()); |
| 186 | |
| 187 | // Both sockets should be on the loopback network interface. |
| 188 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 189 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 190 | ASSERT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 191 | |
| 192 | for (size_t i = 0; i < num_messages; ++i) { |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 193 | // Use a 1 byte message so that the watcher is notified at most once per |
| 194 | // message. |
| 195 | const std::string message("t"); |
| 196 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 197 | scoped_refptr<IOBufferWithSize> write_buffer = |
| 198 | base::MakeRefCounted<IOBufferWithSize>(message.size()); |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 199 | memmove(write_buffer->data(), message.data(), message.size()); |
| 200 | |
| 201 | TestCompletionCallback write_callback; |
| 202 | int write_result = accepted_socket->Write( |
rhalavati@google.com | 690bbea | 2017-12-08 09:05:46 | [diff] [blame] | 203 | write_buffer.get(), write_buffer->size(), write_callback.callback(), |
| 204 | TRAFFIC_ANNOTATION_FOR_TESTS); |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 205 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 206 | scoped_refptr<IOBufferWithSize> read_buffer = |
| 207 | base::MakeRefCounted<IOBufferWithSize>(message.size()); |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 208 | TestCompletionCallback read_callback; |
| 209 | int read_result = connecting_socket.Read( |
| 210 | read_buffer.get(), read_buffer->size(), read_callback.callback()); |
| 211 | |
| 212 | ASSERT_EQ(1, write_callback.GetResult(write_result)); |
| 213 | ASSERT_EQ(1, read_callback.GetResult(read_result)); |
| 214 | } |
| 215 | EXPECT_EQ(expect_connection_changed_count, |
| 216 | watcher_ptr->connection_changed_count()); |
| 217 | EXPECT_EQ(expect_rtt_notification_count, |
| 218 | watcher_ptr->rtt_notification_count()); |
| 219 | } |
| 220 | #endif // defined(TCP_INFO) || defined(OS_LINUX) |
| 221 | |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 222 | AddressList local_address_list() const { |
| 223 | return AddressList(local_address_); |
| 224 | } |
| 225 | |
| 226 | TCPSocket socket_; |
| 227 | IPEndPoint local_address_; |
| 228 | }; |
| 229 | |
| 230 | // Test listening and accepting with a socket bound to an IPv4 address. |
| 231 | TEST_F(TCPSocketTest, Accept) { |
| 232 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 233 | |
| 234 | TestCompletionCallback connect_callback; |
| 235 | // TODO(yzshen): Switch to use TCPSocket when it supports client socket |
| 236 | // operations. |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 237 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 238 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 239 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 240 | |
| 241 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 242 | std::unique_ptr<TCPSocket> accepted_socket; |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 243 | IPEndPoint accepted_address; |
| 244 | int result = socket_.Accept(&accepted_socket, &accepted_address, |
| 245 | accept_callback.callback()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 246 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 247 | |
| 248 | EXPECT_TRUE(accepted_socket.get()); |
| 249 | |
| 250 | // Both sockets should be on the loopback network interface. |
| 251 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 252 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 253 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Test Accept() callback. |
| 257 | TEST_F(TCPSocketTest, AcceptAsync) { |
| 258 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 259 | TestAcceptAsync(); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 260 | } |
| 261 | |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 262 | // Test AdoptConnectedSocket() |
| 263 | TEST_F(TCPSocketTest, AdoptConnectedSocket) { |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 264 | TCPSocket accepting_socket(nullptr, nullptr, NetLogSource()); |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 265 | ASSERT_THAT(accepting_socket.Open(ADDRESS_FAMILY_IPV4), IsOk()); |
| 266 | ASSERT_THAT(accepting_socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), |
| 267 | IsOk()); |
| 268 | ASSERT_THAT(accepting_socket.GetLocalAddress(&local_address_), IsOk()); |
| 269 | ASSERT_THAT(accepting_socket.Listen(kListenBacklog), IsOk()); |
| 270 | |
| 271 | TestCompletionCallback connect_callback; |
| 272 | // TODO(yzshen): Switch to use TCPSocket when it supports client socket |
| 273 | // operations. |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 274 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 275 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 276 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 277 | |
| 278 | TestCompletionCallback accept_callback; |
| 279 | std::unique_ptr<TCPSocket> accepted_socket; |
| 280 | IPEndPoint accepted_address; |
| 281 | int result = accepting_socket.Accept(&accepted_socket, &accepted_address, |
| 282 | accept_callback.callback()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 283 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 284 | |
| 285 | SocketDescriptor accepted_descriptor = |
| 286 | accepted_socket->ReleaseSocketDescriptorForTesting(); |
| 287 | |
| 288 | ASSERT_THAT( |
| 289 | socket_.AdoptConnectedSocket(accepted_descriptor, accepted_address), |
| 290 | IsOk()); |
| 291 | |
| 292 | // socket_ should now have the local address. |
| 293 | IPEndPoint adopted_address; |
| 294 | ASSERT_THAT(socket_.GetLocalAddress(&adopted_address), IsOk()); |
| 295 | EXPECT_EQ(local_address_.address(), adopted_address.address()); |
| 296 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 297 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
rvera | 26f0a139 | 2017-05-02 22:25:44 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Test Accept() for AdoptUnconnectedSocket. |
| 301 | TEST_F(TCPSocketTest, AcceptForAdoptedUnconnectedSocket) { |
| 302 | SocketDescriptor existing_socket = |
| 303 | CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 304 | ASSERT_THAT(socket_.AdoptUnconnectedSocket(existing_socket), IsOk()); |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 305 | |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 306 | IPEndPoint address(IPAddress::IPv4Localhost(), 0); |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 307 | SockaddrStorage storage; |
| 308 | ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len)); |
| 309 | ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len)); |
| 310 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 311 | ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk()); |
| 312 | ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 313 | |
| 314 | TestAcceptAsync(); |
| 315 | } |
xiyuan@chromium.org | ef2f002 | 2014-04-29 10:24:35 | [diff] [blame] | 316 | |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 317 | // Accept two connections simultaneously. |
| 318 | TEST_F(TCPSocketTest, Accept2Connections) { |
| 319 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 320 | |
| 321 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 322 | std::unique_ptr<TCPSocket> accepted_socket; |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 323 | IPEndPoint accepted_address; |
| 324 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 325 | ASSERT_THAT(socket_.Accept(&accepted_socket, &accepted_address, |
| 326 | accept_callback.callback()), |
| 327 | IsError(ERR_IO_PENDING)); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 328 | |
| 329 | TestCompletionCallback connect_callback; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 330 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 331 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 332 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 333 | |
| 334 | TestCompletionCallback connect_callback2; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 335 | TCPClientSocket connecting_socket2(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 336 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 337 | int connect_result2 = |
| 338 | connecting_socket2.Connect(connect_callback2.callback()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 339 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 340 | EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 341 | |
| 342 | TestCompletionCallback accept_callback2; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 343 | std::unique_ptr<TCPSocket> accepted_socket2; |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 344 | IPEndPoint accepted_address2; |
| 345 | |
| 346 | int result = socket_.Accept(&accepted_socket2, &accepted_address2, |
| 347 | accept_callback2.callback()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 348 | ASSERT_THAT(accept_callback2.GetResult(result), IsOk()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 349 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 350 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
| 351 | EXPECT_THAT(connect_callback2.GetResult(connect_result2), IsOk()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 352 | |
| 353 | EXPECT_TRUE(accepted_socket.get()); |
| 354 | EXPECT_TRUE(accepted_socket2.get()); |
| 355 | EXPECT_NE(accepted_socket.get(), accepted_socket2.get()); |
| 356 | |
| 357 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 358 | EXPECT_EQ(accepted_address2.address(), local_address_.address()); |
| 359 | } |
| 360 | |
| 361 | // Test listening and accepting with a socket bound to an IPv6 address. |
| 362 | TEST_F(TCPSocketTest, AcceptIPv6) { |
| 363 | bool initialized = false; |
| 364 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv6(&initialized)); |
| 365 | if (!initialized) |
| 366 | return; |
| 367 | |
| 368 | TestCompletionCallback connect_callback; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 369 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 370 | NetLogSource()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 371 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 372 | |
| 373 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 374 | std::unique_ptr<TCPSocket> accepted_socket; |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 375 | IPEndPoint accepted_address; |
| 376 | int result = socket_.Accept(&accepted_socket, &accepted_address, |
| 377 | accept_callback.callback()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 378 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 379 | |
| 380 | EXPECT_TRUE(accepted_socket.get()); |
| 381 | |
| 382 | // Both sockets should be on the loopback network interface. |
| 383 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 384 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 385 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 386 | } |
| 387 | |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 388 | TEST_F(TCPSocketTest, ReadWrite) { |
| 389 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 390 | |
| 391 | TestCompletionCallback connect_callback; |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 392 | TCPSocket connecting_socket(nullptr, nullptr, NetLogSource()); |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 393 | int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 394 | ASSERT_THAT(result, IsOk()); |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 395 | int connect_result = |
| 396 | connecting_socket.Connect(local_address_, connect_callback.callback()); |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 397 | |
| 398 | TestCompletionCallback accept_callback; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 399 | std::unique_ptr<TCPSocket> accepted_socket; |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 400 | IPEndPoint accepted_address; |
| 401 | result = socket_.Accept(&accepted_socket, &accepted_address, |
| 402 | accept_callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 403 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 404 | |
| 405 | ASSERT_TRUE(accepted_socket.get()); |
| 406 | |
| 407 | // Both sockets should be on the loopback network interface. |
| 408 | EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 409 | |
Sergey Ulanov | 7cbcbc5 | 2017-07-26 18:29:13 | [diff] [blame] | 410 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 411 | |
| 412 | const std::string message("test message"); |
| 413 | std::vector<char> buffer(message.size()); |
| 414 | |
| 415 | size_t bytes_written = 0; |
| 416 | while (bytes_written < message.size()) { |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 417 | scoped_refptr<IOBufferWithSize> write_buffer = |
| 418 | base::MakeRefCounted<IOBufferWithSize>(message.size() - bytes_written); |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 419 | memmove(write_buffer->data(), message.data() + bytes_written, |
| 420 | message.size() - bytes_written); |
| 421 | |
| 422 | TestCompletionCallback write_callback; |
| 423 | int write_result = accepted_socket->Write( |
rhalavati@google.com | 690bbea | 2017-12-08 09:05:46 | [diff] [blame] | 424 | write_buffer.get(), write_buffer->size(), write_callback.callback(), |
| 425 | TRAFFIC_ANNOTATION_FOR_TESTS); |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 426 | write_result = write_callback.GetResult(write_result); |
| 427 | ASSERT_TRUE(write_result >= 0); |
| 428 | bytes_written += write_result; |
| 429 | ASSERT_TRUE(bytes_written <= message.size()); |
| 430 | } |
| 431 | |
| 432 | size_t bytes_read = 0; |
| 433 | while (bytes_read < message.size()) { |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 434 | scoped_refptr<IOBufferWithSize> read_buffer = |
| 435 | base::MakeRefCounted<IOBufferWithSize>(message.size() - bytes_read); |
yzshen@chromium.org | c9080d8 | 2013-09-15 15:14:16 | [diff] [blame] | 436 | TestCompletionCallback read_callback; |
| 437 | int read_result = connecting_socket.Read( |
| 438 | read_buffer.get(), read_buffer->size(), read_callback.callback()); |
| 439 | read_result = read_callback.GetResult(read_result); |
| 440 | ASSERT_TRUE(read_result >= 0); |
| 441 | ASSERT_TRUE(bytes_read + read_result <= message.size()); |
| 442 | memmove(&buffer[bytes_read], read_buffer->data(), read_result); |
| 443 | bytes_read += read_result; |
| 444 | } |
| 445 | |
| 446 | std::string received_message(buffer.begin(), buffer.end()); |
| 447 | ASSERT_EQ(message, received_message); |
| 448 | } |
| 449 | |
Matt Menke | e0673201 | 2018-04-06 21:41:24 | [diff] [blame] | 450 | // Destroy a TCPSocket while there's a pending read, and make sure the read |
| 451 | // IOBuffer that the socket was holding on to is destroyed. |
| 452 | // See https://crbug.com/804868. |
| 453 | TEST_F(TCPSocketTest, DestroyWithPendingRead) { |
| 454 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 455 | |
| 456 | // Create a connected socket. |
| 457 | |
| 458 | TestCompletionCallback connect_callback; |
| 459 | std::unique_ptr<TCPSocket> connecting_socket = |
| 460 | std::make_unique<TCPSocket>(nullptr, nullptr, NetLogSource()); |
| 461 | int result = connecting_socket->Open(ADDRESS_FAMILY_IPV4); |
| 462 | ASSERT_THAT(result, IsOk()); |
| 463 | int connect_result = |
| 464 | connecting_socket->Connect(local_address_, connect_callback.callback()); |
| 465 | |
| 466 | TestCompletionCallback accept_callback; |
| 467 | std::unique_ptr<TCPSocket> accepted_socket; |
| 468 | IPEndPoint accepted_address; |
| 469 | result = socket_.Accept(&accepted_socket, &accepted_address, |
| 470 | accept_callback.callback()); |
| 471 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
| 472 | ASSERT_TRUE(accepted_socket.get()); |
| 473 | ASSERT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
| 474 | |
| 475 | // Try to read from the socket, but never write anything to the other end. |
| 476 | base::RunLoop run_loop; |
| 477 | scoped_refptr<IOBufferWithDestructionCallback> read_buffer( |
| 478 | base::MakeRefCounted<IOBufferWithDestructionCallback>( |
| 479 | run_loop.QuitClosure())); |
| 480 | TestCompletionCallback read_callback; |
| 481 | EXPECT_EQ(ERR_IO_PENDING, |
| 482 | connecting_socket->Read(read_buffer.get(), read_buffer->size(), |
| 483 | read_callback.callback())); |
| 484 | |
| 485 | // Release the handle to the read buffer and destroy the socket. Make sure the |
| 486 | // read buffer is destroyed. |
| 487 | read_buffer = nullptr; |
| 488 | connecting_socket.reset(); |
| 489 | run_loop.Run(); |
| 490 | } |
| 491 | |
| 492 | // Destroy a TCPSocket while there's a pending write, and make sure the write |
| 493 | // IOBuffer that the socket was holding on to is destroyed. |
| 494 | TEST_F(TCPSocketTest, DestroyWithPendingWrite) { |
| 495 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 496 | |
| 497 | // Create a connected socket. |
| 498 | |
| 499 | TestCompletionCallback connect_callback; |
| 500 | std::unique_ptr<TCPSocket> connecting_socket = |
| 501 | std::make_unique<TCPSocket>(nullptr, nullptr, NetLogSource()); |
| 502 | int result = connecting_socket->Open(ADDRESS_FAMILY_IPV4); |
| 503 | ASSERT_THAT(result, IsOk()); |
| 504 | int connect_result = |
| 505 | connecting_socket->Connect(local_address_, connect_callback.callback()); |
| 506 | |
| 507 | TestCompletionCallback accept_callback; |
| 508 | std::unique_ptr<TCPSocket> accepted_socket; |
| 509 | IPEndPoint accepted_address; |
| 510 | result = socket_.Accept(&accepted_socket, &accepted_address, |
| 511 | accept_callback.callback()); |
| 512 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
| 513 | ASSERT_TRUE(accepted_socket.get()); |
| 514 | ASSERT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
| 515 | |
| 516 | // Repeatedly write to the socket until an operation does not complete |
| 517 | // synchronously. |
| 518 | base::RunLoop run_loop; |
| 519 | scoped_refptr<IOBufferWithDestructionCallback> write_buffer( |
| 520 | base::MakeRefCounted<IOBufferWithDestructionCallback>( |
| 521 | run_loop.QuitClosure())); |
| 522 | memset(write_buffer->data(), '1', write_buffer->size()); |
| 523 | TestCompletionCallback write_callback; |
| 524 | while (true) { |
| 525 | int result = connecting_socket->Write( |
| 526 | write_buffer.get(), write_buffer->size(), write_callback.callback(), |
| 527 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 528 | if (result == ERR_IO_PENDING) |
| 529 | break; |
| 530 | ASSERT_LT(0, result); |
| 531 | } |
| 532 | |
| 533 | // Release the handle to the read buffer and destroy the socket. Make sure the |
| 534 | // write buffer is destroyed. |
| 535 | write_buffer = nullptr; |
| 536 | connecting_socket.reset(); |
| 537 | run_loop.Run(); |
| 538 | } |
| 539 | |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 540 | // If a ReadIfReady is pending, it's legal to cancel it and start reading later. |
| 541 | TEST_F(TCPSocketTest, CancelPendingReadIfReady) { |
| 542 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 543 | |
| 544 | // Create a connected socket. |
| 545 | TestCompletionCallback connect_callback; |
| 546 | std::unique_ptr<TCPSocket> connecting_socket = |
| 547 | std::make_unique<TCPSocket>(nullptr, nullptr, NetLogSource()); |
| 548 | int result = connecting_socket->Open(ADDRESS_FAMILY_IPV4); |
| 549 | ASSERT_THAT(result, IsOk()); |
| 550 | int connect_result = |
| 551 | connecting_socket->Connect(local_address_, connect_callback.callback()); |
| 552 | |
| 553 | TestCompletionCallback accept_callback; |
| 554 | std::unique_ptr<TCPSocket> accepted_socket; |
| 555 | IPEndPoint accepted_address; |
| 556 | result = socket_.Accept(&accepted_socket, &accepted_address, |
| 557 | accept_callback.callback()); |
| 558 | ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
| 559 | ASSERT_TRUE(accepted_socket.get()); |
| 560 | ASSERT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
| 561 | |
| 562 | // Try to read from the socket, but never write anything to the other end. |
| 563 | base::RunLoop run_loop; |
| 564 | scoped_refptr<IOBufferWithDestructionCallback> read_buffer( |
| 565 | base::MakeRefCounted<IOBufferWithDestructionCallback>( |
| 566 | run_loop.QuitClosure())); |
| 567 | TestCompletionCallback read_callback; |
| 568 | EXPECT_EQ(ERR_IO_PENDING, connecting_socket->ReadIfReady( |
| 569 | read_buffer.get(), read_buffer->size(), |
| 570 | read_callback.callback())); |
| 571 | |
| 572 | // Now cancel the pending ReadIfReady(). |
| 573 | connecting_socket->CancelReadIfReady(); |
| 574 | |
| 575 | // Send data to |connecting_socket|. |
| 576 | const char kMsg[] = "hello!"; |
| 577 | scoped_refptr<StringIOBuffer> write_buffer = |
| 578 | base::MakeRefCounted<StringIOBuffer>(kMsg); |
| 579 | |
| 580 | TestCompletionCallback write_callback; |
| 581 | int write_result = accepted_socket->Write(write_buffer.get(), strlen(kMsg), |
| 582 | write_callback.callback(), |
| 583 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 584 | const int msg_size = strlen(kMsg); |
| 585 | ASSERT_EQ(msg_size, write_result); |
| 586 | |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 587 | TestCompletionCallback read_callback2; |
| 588 | int read_result = connecting_socket->ReadIfReady( |
| 589 | read_buffer.get(), read_buffer->size(), read_callback2.callback()); |
Wez | 1c67dcbe | 2018-05-09 10:48:34 | [diff] [blame] | 590 | if (read_result == ERR_IO_PENDING) { |
| 591 | ASSERT_EQ(OK, read_callback2.GetResult(read_result)); |
| 592 | read_result = connecting_socket->ReadIfReady( |
| 593 | read_buffer.get(), read_buffer->size(), read_callback2.callback()); |
| 594 | } |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 595 | |
Wez | 1c67dcbe | 2018-05-09 10:48:34 | [diff] [blame] | 596 | ASSERT_EQ(msg_size, read_result); |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 597 | ASSERT_EQ(0, memcmp(&kMsg, read_buffer->data(), msg_size)); |
| 598 | } |
| 599 | |
Wez | d174213 | 2019-01-18 02:50:46 | [diff] [blame] | 600 | TEST_F(TCPSocketTest, IsConnected) { |
Wez | 372b0342 | 2019-01-12 00:09:47 | [diff] [blame] | 601 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 602 | |
| 603 | TestCompletionCallback accept_callback; |
| 604 | std::unique_ptr<TCPSocket> accepted_socket; |
| 605 | IPEndPoint accepted_address; |
| 606 | EXPECT_THAT(socket_.Accept(&accepted_socket, &accepted_address, |
| 607 | accept_callback.callback()), |
| 608 | IsError(ERR_IO_PENDING)); |
| 609 | |
| 610 | TestCompletionCallback connect_callback; |
| 611 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
| 612 | NetLogSource()); |
| 613 | |
| 614 | // Immediately after creation, the socket should not be connected. |
| 615 | EXPECT_FALSE(connecting_socket.IsConnected()); |
| 616 | EXPECT_FALSE(connecting_socket.IsConnectedAndIdle()); |
| 617 | |
| 618 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
| 619 | EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); |
| 620 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
| 621 | |
| 622 | // |connecting_socket| and |accepted_socket| should now both be reported as |
| 623 | // connected, and idle |
| 624 | EXPECT_TRUE(accepted_socket->IsConnected()); |
| 625 | EXPECT_TRUE(accepted_socket->IsConnectedAndIdle()); |
| 626 | EXPECT_TRUE(connecting_socket.IsConnected()); |
| 627 | EXPECT_TRUE(connecting_socket.IsConnectedAndIdle()); |
| 628 | |
| 629 | // Write one byte to the |accepted_socket|, then close it. |
| 630 | const char kSomeData[] = "!"; |
| 631 | scoped_refptr<IOBuffer> some_data_buffer = |
| 632 | base::MakeRefCounted<StringIOBuffer>(kSomeData); |
| 633 | TestCompletionCallback write_callback; |
| 634 | EXPECT_THAT(write_callback.GetResult(accepted_socket->Write( |
| 635 | some_data_buffer.get(), 1, write_callback.callback(), |
| 636 | TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 637 | 1); |
| 638 | accepted_socket.reset(); |
| 639 | |
Wez | d174213 | 2019-01-18 02:50:46 | [diff] [blame] | 640 | // Wait until |connecting_socket| is signalled as having data to read. |
| 641 | fd_set read_fds; |
| 642 | FD_ZERO(&read_fds); |
| 643 | SocketDescriptor connecting_fd = |
| 644 | connecting_socket.SocketDescriptorForTesting(); |
| 645 | FD_SET(connecting_fd, &read_fds); |
| 646 | ASSERT_EQ(select(FD_SETSIZE, &read_fds, nullptr, nullptr, nullptr), 1); |
| 647 | ASSERT_TRUE(FD_ISSET(connecting_fd, &read_fds)); |
| 648 | |
| 649 | // It should now be reported as connected, but not as idle. |
Wez | 372b0342 | 2019-01-12 00:09:47 | [diff] [blame] | 650 | EXPECT_TRUE(connecting_socket.IsConnected()); |
| 651 | EXPECT_FALSE(connecting_socket.IsConnectedAndIdle()); |
| 652 | |
| 653 | // Read the message from |connecting_socket_|, then read the end-of-stream. |
| 654 | scoped_refptr<IOBufferWithSize> read_buffer = |
| 655 | base::MakeRefCounted<IOBufferWithSize>(2); |
| 656 | TestCompletionCallback read_callback; |
| 657 | EXPECT_THAT( |
| 658 | read_callback.GetResult(connecting_socket.Read( |
| 659 | read_buffer.get(), read_buffer->size(), read_callback.callback())), |
| 660 | 1); |
| 661 | EXPECT_THAT( |
| 662 | read_callback.GetResult(connecting_socket.Read( |
| 663 | read_buffer.get(), read_buffer->size(), read_callback.callback())), |
| 664 | 0); |
| 665 | |
| 666 | // |connecting_socket| has no more data to read, so should noe be reported |
| 667 | // as disconnected. |
| 668 | EXPECT_FALSE(connecting_socket.IsConnected()); |
| 669 | EXPECT_FALSE(connecting_socket.IsConnectedAndIdle()); |
| 670 | } |
| 671 | |
Matt Menke | b92dda2dd | 2018-09-18 17:04:42 | [diff] [blame] | 672 | // Tests that setting a socket option in the BeforeConnectCallback works. With |
| 673 | // real sockets, socket options often have to be set before the connect() call, |
| 674 | // and the BeforeConnectCallback is the only way to do that, with a |
| 675 | // TCPClientSocket. |
| 676 | TEST_F(TCPSocketTest, BeforeConnectCallback) { |
| 677 | // A receive buffer size that is between max and minimum buffer size limits, |
| 678 | // and weird enough to likely not be a default value. |
| 679 | const int kReceiveBufferSize = 32 * 1024 + 1117; |
| 680 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 681 | |
| 682 | TestCompletionCallback accept_callback; |
| 683 | std::unique_ptr<TCPSocket> accepted_socket; |
| 684 | IPEndPoint accepted_address; |
| 685 | EXPECT_THAT(socket_.Accept(&accepted_socket, &accepted_address, |
| 686 | accept_callback.callback()), |
| 687 | IsError(ERR_IO_PENDING)); |
| 688 | |
| 689 | TestCompletionCallback connect_callback; |
| 690 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
| 691 | NetLogSource()); |
| 692 | |
| 693 | connecting_socket.SetBeforeConnectCallback(base::BindLambdaForTesting([&] { |
| 694 | EXPECT_FALSE(connecting_socket.IsConnected()); |
| 695 | int result = connecting_socket.SetReceiveBufferSize(kReceiveBufferSize); |
| 696 | EXPECT_THAT(result, IsOk()); |
| 697 | return result; |
| 698 | })); |
| 699 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
| 700 | |
| 701 | EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); |
| 702 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
| 703 | |
| 704 | int actual_size = 0; |
| 705 | socklen_t actual_size_len = sizeof(actual_size); |
| 706 | int os_result = getsockopt( |
| 707 | connecting_socket.SocketDescriptorForTesting(), SOL_SOCKET, SO_RCVBUF, |
| 708 | reinterpret_cast<char*>(&actual_size), &actual_size_len); |
| 709 | ASSERT_EQ(0, os_result); |
| 710 | // Linux platforms generally allocate twice as much buffer size is requested to |
| 711 | // account for internal kernel data structures. |
| 712 | #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 713 | EXPECT_EQ(2 * kReceiveBufferSize, actual_size); |
| 714 | // Unfortunately, Apple platform behavior doesn't seem to be documented, and |
| 715 | // doesn't match behavior on any other platforms. |
Wez | 372b0342 | 2019-01-12 00:09:47 | [diff] [blame] | 716 | // Fuchsia doesn't currently implement SO_RCVBUF. |
| 717 | #elif !defined(OS_IOS) && !defined(OS_MACOSX) && !defined(OS_FUCHSIA) |
Matt Menke | b92dda2dd | 2018-09-18 17:04:42 | [diff] [blame] | 718 | EXPECT_EQ(kReceiveBufferSize, actual_size); |
| 719 | #endif |
| 720 | } |
| 721 | |
| 722 | TEST_F(TCPSocketTest, BeforeConnectCallbackFails) { |
| 723 | // Setting up a server isn't strictly necessary, but it does allow checking |
| 724 | // the server was never connected to. |
| 725 | ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 726 | |
| 727 | TestCompletionCallback accept_callback; |
| 728 | std::unique_ptr<TCPSocket> accepted_socket; |
| 729 | IPEndPoint accepted_address; |
| 730 | EXPECT_THAT(socket_.Accept(&accepted_socket, &accepted_address, |
| 731 | accept_callback.callback()), |
| 732 | IsError(ERR_IO_PENDING)); |
| 733 | |
| 734 | TestCompletionCallback connect_callback; |
| 735 | TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr, |
| 736 | NetLogSource()); |
| 737 | |
| 738 | // Set a callback that returns a nonsensical error, and make sure it's |
| 739 | // returned. |
| 740 | connecting_socket.SetBeforeConnectCallback(base::BindRepeating( |
| 741 | [] { return static_cast<int>(net::ERR_NAME_NOT_RESOLVED); })); |
| 742 | int connect_result = connecting_socket.Connect(connect_callback.callback()); |
| 743 | EXPECT_THAT(connect_callback.GetResult(connect_result), |
| 744 | IsError(net::ERR_NAME_NOT_RESOLVED)); |
| 745 | |
| 746 | // Best effort check that the socket wasn't accepted - may flakily pass on |
| 747 | // regression, unfortunately. |
| 748 | base::RunLoop().RunUntilIdle(); |
| 749 | EXPECT_FALSE(accept_callback.have_result()); |
| 750 | } |
| 751 | |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 752 | // These tests require kernel support for tcp_info struct, and so they are |
| 753 | // enabled only on certain platforms. |
| 754 | #if defined(TCP_INFO) || defined(OS_LINUX) |
| 755 | // If SocketPerformanceWatcher::ShouldNotifyUpdatedRTT always returns false, |
| 756 | // then the wtatcher should not receive any notifications. |
| 757 | TEST_F(TCPSocketTest, SPWNotInterested) { |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 758 | TestSPWNotifications(false, 2u, 0u, 0u); |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | // One notification should be received when the socket connects. One |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 762 | // additional notification should be received for each message read. |
Asanka Herath | 3773b61 | 2018-10-25 19:46:22 | [diff] [blame] | 763 | TEST_F(TCPSocketTest, SPWNoAdvance) { |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 764 | TestSPWNotifications(true, 2u, 0u, 3u); |
tbansal | 7b403bc | 2016-04-13 22:33:21 | [diff] [blame] | 765 | } |
| 766 | #endif // defined(TCP_INFO) || defined(OS_LINUX) |
| 767 | |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 768 | // On Android, where socket tagging is supported, verify that TCPSocket::Tag |
| 769 | // works as expected. |
| 770 | #if defined(OS_ANDROID) |
| 771 | TEST_F(TCPSocketTest, Tag) { |
| 772 | // Start test server. |
| 773 | EmbeddedTestServer test_server; |
| 774 | test_server.AddDefaultHandlers(base::FilePath()); |
| 775 | ASSERT_TRUE(test_server.Start()); |
| 776 | |
| 777 | AddressList addr_list; |
| 778 | ASSERT_TRUE(test_server.GetAddressList(&addr_list)); |
| 779 | EXPECT_EQ(socket_.Open(addr_list[0].GetFamily()), OK); |
| 780 | |
| 781 | // Verify TCP connect packets are tagged and counted properly. |
| 782 | int32_t tag_val1 = 0x12345678; |
| 783 | uint64_t old_traffic = GetTaggedBytes(tag_val1); |
| 784 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 785 | socket_.ApplySocketTag(tag1); |
| 786 | TestCompletionCallback connect_callback; |
| 787 | int connect_result = |
| 788 | socket_.Connect(addr_list[0], connect_callback.callback()); |
| 789 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
| 790 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 791 | |
| 792 | // Verify socket can be retagged with a new value and the current process's |
| 793 | // UID. |
| 794 | int32_t tag_val2 = 0x87654321; |
| 795 | old_traffic = GetTaggedBytes(tag_val2); |
| 796 | SocketTag tag2(getuid(), tag_val2); |
| 797 | socket_.ApplySocketTag(tag2); |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 798 | const char kRequest1[] = "GET / HTTP/1.0"; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 799 | scoped_refptr<IOBuffer> write_buffer1 = |
| 800 | base::MakeRefCounted<StringIOBuffer>(kRequest1); |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 801 | TestCompletionCallback write_callback1; |
| 802 | EXPECT_EQ( |
| 803 | socket_.Write(write_buffer1.get(), strlen(kRequest1), |
| 804 | write_callback1.callback(), TRAFFIC_ANNOTATION_FOR_TESTS), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 805 | static_cast<int>(strlen(kRequest1))); |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 806 | EXPECT_GT(GetTaggedBytes(tag_val2), old_traffic); |
| 807 | |
| 808 | // Verify socket can be retagged with a new value and the current process's |
| 809 | // UID. |
| 810 | old_traffic = GetTaggedBytes(tag_val1); |
| 811 | socket_.ApplySocketTag(tag1); |
| 812 | const char kRequest2[] = "\n\n"; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 813 | scoped_refptr<IOBuffer> write_buffer2 = |
| 814 | base::MakeRefCounted<StringIOBuffer>(kRequest2); |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 815 | TestCompletionCallback write_callback2; |
| 816 | EXPECT_EQ( |
| 817 | socket_.Write(write_buffer2.get(), strlen(kRequest2), |
| 818 | write_callback2.callback(), TRAFFIC_ANNOTATION_FOR_TESTS), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 819 | static_cast<int>(strlen(kRequest2))); |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 820 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 821 | |
| 822 | socket_.Close(); |
| 823 | } |
| 824 | |
| 825 | TEST_F(TCPSocketTest, TagAfterConnect) { |
| 826 | // Start test server. |
| 827 | EmbeddedTestServer test_server; |
| 828 | test_server.AddDefaultHandlers(base::FilePath()); |
| 829 | ASSERT_TRUE(test_server.Start()); |
| 830 | |
| 831 | AddressList addr_list; |
| 832 | ASSERT_TRUE(test_server.GetAddressList(&addr_list)); |
| 833 | EXPECT_EQ(socket_.Open(addr_list[0].GetFamily()), OK); |
| 834 | |
| 835 | // Connect socket. |
| 836 | TestCompletionCallback connect_callback; |
| 837 | int connect_result = |
| 838 | socket_.Connect(addr_list[0], connect_callback.callback()); |
| 839 | EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk()); |
| 840 | |
| 841 | // Verify socket can be tagged with a new value and the current process's |
| 842 | // UID. |
| 843 | int32_t tag_val2 = 0x87654321; |
| 844 | uint64_t old_traffic = GetTaggedBytes(tag_val2); |
| 845 | SocketTag tag2(getuid(), tag_val2); |
| 846 | socket_.ApplySocketTag(tag2); |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 847 | const char kRequest1[] = "GET / HTTP/1.0"; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 848 | scoped_refptr<IOBuffer> write_buffer1 = |
| 849 | base::MakeRefCounted<StringIOBuffer>(kRequest1); |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 850 | TestCompletionCallback write_callback1; |
| 851 | EXPECT_EQ( |
| 852 | socket_.Write(write_buffer1.get(), strlen(kRequest1), |
| 853 | write_callback1.callback(), TRAFFIC_ANNOTATION_FOR_TESTS), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 854 | static_cast<int>(strlen(kRequest1))); |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 855 | EXPECT_GT(GetTaggedBytes(tag_val2), old_traffic); |
| 856 | |
| 857 | // Verify socket can be retagged with a new value and the current process's |
| 858 | // UID. |
| 859 | int32_t tag_val1 = 0x12345678; |
| 860 | old_traffic = GetTaggedBytes(tag_val1); |
| 861 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 862 | socket_.ApplySocketTag(tag1); |
| 863 | const char kRequest2[] = "\n\n"; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 864 | scoped_refptr<IOBuffer> write_buffer2 = |
| 865 | base::MakeRefCounted<StringIOBuffer>(kRequest2); |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 866 | TestCompletionCallback write_callback2; |
| 867 | EXPECT_EQ( |
| 868 | socket_.Write(write_buffer2.get(), strlen(kRequest2), |
| 869 | write_callback2.callback(), TRAFFIC_ANNOTATION_FOR_TESTS), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 870 | static_cast<int>(strlen(kRequest2))); |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 871 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 872 | |
| 873 | socket_.Close(); |
| 874 | } |
| 875 | #endif |
| 876 | |
yzshen@chromium.org | 21160f0 | 2013-09-01 23:04:27 | [diff] [blame] | 877 | } // namespace |
| 878 | } // namespace net |