blob: e170f3b5a5b8fea7e13793fe7fdc8f24f29c014a [file] [log] [blame]
[email protected]21160f02013-09-01 23:04:271// 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
tbansal7b403bcc2016-04-13 22:33:217#include <stddef.h>
[email protected]c9080d82013-09-15 15:14:168#include <string.h>
[email protected]21160f02013-09-01 23:04:279
danakj655b66c2016-04-16 00:51:3810#include <memory>
[email protected]c9080d82013-09-15 15:14:1611#include <string>
12#include <vector>
13
[email protected]c9080d82013-09-15 15:14:1614#include "base/memory/ref_counted.h"
tbansal7b403bcc2016-04-13 22:33:2115#include "base/time/time.h"
[email protected]21160f02013-09-01 23:04:2716#include "net/base/address_list.h"
[email protected]c9080d82013-09-15 15:14:1617#include "net/base/io_buffer.h"
[email protected]21160f02013-09-01 23:04:2718#include "net/base/ip_endpoint.h"
19#include "net/base/net_errors.h"
tfarina3d87d7cd2016-01-13 02:26:5920#include "net/base/sockaddr_storage.h"
[email protected]21160f02013-09-01 23:04:2721#include "net/base/test_completion_callback.h"
mikecironef22f9812016-10-04 03:40:1922#include "net/log/net_log_source.h"
tbansalca83c002016-04-28 20:56:2823#include "net/socket/socket_performance_watcher.h"
[email protected]21160f02013-09-01 23:04:2724#include "net/socket/tcp_client_socket.h"
robpercival214763f2016-07-01 23:27:0125#include "net/test/gtest_util.h"
26#include "testing/gmock/include/gmock/gmock.h"
[email protected]21160f02013-09-01 23:04:2727#include "testing/gtest/include/gtest/gtest.h"
28#include "testing/platform_test.h"
29
Sergey Ulanov7cbcbc52017-07-26 18:29:1330using net::test::IsError;
robpercival214763f2016-07-01 23:27:0131using net::test::IsOk;
32
[email protected]21160f02013-09-01 23:04:2733namespace net {
34
35namespace {
tbansal7b403bcc2016-04-13 22:33:2136
37class 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 Watkins7a41d3552017-12-01 02:13:2743 ~TestSocketPerformanceWatcher() override = default;
tbansal7b403bcc2016-04-13 22:33:2144
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]21160f02013-09-01 23:04:2767const int kListenBacklog = 5;
68
69class TCPSocketTest : public PlatformTest {
70 protected:
Sergey Ulanov7cbcbc52017-07-26 18:29:1371 TCPSocketTest() : socket_(nullptr, nullptr, NetLogSource()) {}
[email protected]21160f02013-09-01 23:04:2772
73 void SetUpListenIPv4() {
robpercival214763f2016-07-01 23:27:0174 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]21160f02013-09-01 23:04:2779 }
80
81 void SetUpListenIPv6(bool* success) {
82 *success = false;
[email protected]21160f02013-09-01 23:04:2783
[email protected]c9080d82013-09-15 15:14:1684 if (socket_.Open(ADDRESS_FAMILY_IPV6) != OK ||
martijna2e83bd2016-03-18 13:10:4585 socket_.Bind(IPEndPoint(IPAddress::IPv6Localhost(), 0)) != OK ||
[email protected]21160f02013-09-01 23:04:2786 socket_.Listen(kListenBacklog) != OK) {
87 LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is "
88 "disabled. Skipping the test";
89 return;
90 }
robpercival214763f2016-07-01 23:27:0191 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
[email protected]21160f02013-09-01 23:04:2792 *success = true;
93 }
94
[email protected]ef2f0022014-04-29 10:24:3595 void TestAcceptAsync() {
96 TestCompletionCallback accept_callback;
danakj655b66c2016-04-16 00:51:3897 std::unique_ptr<TCPSocket> accepted_socket;
[email protected]ef2f0022014-04-29 10:24:3598 IPEndPoint accepted_address;
Sergey Ulanov7cbcbc52017-07-26 18:29:1399 ASSERT_THAT(socket_.Accept(&accepted_socket, &accepted_address,
100 accept_callback.callback()),
101 IsError(ERR_IO_PENDING));
[email protected]ef2f0022014-04-29 10:24:35102
103 TestCompletionCallback connect_callback;
Sergey Ulanov7cbcbc52017-07-26 18:29:13104 TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr,
mikecironef22f9812016-10-04 03:40:19105 NetLogSource());
Sergey Ulanov7cbcbc52017-07-26 18:29:13106 int connect_result = connecting_socket.Connect(connect_callback.callback());
107 EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk());
[email protected]ef2f0022014-04-29 10:24:35108
robpercival214763f2016-07-01 23:27:01109 EXPECT_THAT(accept_callback.WaitForResult(), IsOk());
[email protected]ef2f0022014-04-29 10:24:35110
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
tbansal7b403bcc2016-04-13 22:33:21117#if defined(TCP_INFO) || defined(OS_LINUX)
118 // Tests that notifications to Socket Performance Watcher (SPW) are delivered
tbansal180587c2017-02-16 15:13:23119 // 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,
tbansal7b403bcc2016-04-13 22:33:21128 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
tbansal7b403bcc2016-04-13 22:33:21133 TestCompletionCallback connect_callback;
134
danakj655b66c2016-04-16 00:51:38135 std::unique_ptr<TestSocketPerformanceWatcher> watcher(
tbansal7b403bcc2016-04-13 22:33:21136 new TestSocketPerformanceWatcher(should_notify_updated_rtt));
137 TestSocketPerformanceWatcher* watcher_ptr = watcher.get();
138
Sergey Ulanov7cbcbc52017-07-26 18:29:13139 TCPSocket connecting_socket(std::move(watcher), nullptr, NetLogSource());
tbansal7b403bcc2016-04-13 22:33:21140
141 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4);
robpercival214763f2016-07-01 23:27:01142 ASSERT_THAT(result, IsOk());
Sergey Ulanov7cbcbc52017-07-26 18:29:13143 int connect_result =
144 connecting_socket.Connect(local_address_, connect_callback.callback());
tbansal7b403bcc2016-04-13 22:33:21145
146 TestCompletionCallback accept_callback;
danakj655b66c2016-04-16 00:51:38147 std::unique_ptr<TCPSocket> accepted_socket;
tbansal7b403bcc2016-04-13 22:33:21148 IPEndPoint accepted_address;
149 result = socket_.Accept(&accepted_socket, &accepted_address,
150 accept_callback.callback());
robpercival214763f2016-07-01 23:27:01151 ASSERT_THAT(accept_callback.GetResult(result), IsOk());
tbansal7b403bcc2016-04-13 22:33:21152
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 Ulanov7cbcbc52017-07-26 18:29:13158 ASSERT_THAT(connect_callback.GetResult(connect_result), IsOk());
tbansal7b403bcc2016-04-13 22:33:21159
160 for (size_t i = 0; i < num_messages; ++i) {
tbansal7b403bcc2016-04-13 22:33:21161 // 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]21160f02013-09-01 23:04:27189 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.
198TEST_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 Ulanov7cbcbc52017-07-26 18:29:13204 TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr,
mikecironef22f9812016-10-04 03:40:19205 NetLogSource());
Sergey Ulanov7cbcbc52017-07-26 18:29:13206 int connect_result = connecting_socket.Connect(connect_callback.callback());
[email protected]21160f02013-09-01 23:04:27207
208 TestCompletionCallback accept_callback;
danakj655b66c2016-04-16 00:51:38209 std::unique_ptr<TCPSocket> accepted_socket;
[email protected]21160f02013-09-01 23:04:27210 IPEndPoint accepted_address;
211 int result = socket_.Accept(&accepted_socket, &accepted_address,
212 accept_callback.callback());
Sergey Ulanov7cbcbc52017-07-26 18:29:13213 ASSERT_THAT(accept_callback.GetResult(result), IsOk());
[email protected]21160f02013-09-01 23:04:27214
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 Ulanov7cbcbc52017-07-26 18:29:13220 EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk());
[email protected]21160f02013-09-01 23:04:27221}
222
223// Test Accept() callback.
224TEST_F(TCPSocketTest, AcceptAsync) {
225 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
[email protected]ef2f0022014-04-29 10:24:35226 TestAcceptAsync();
[email protected]21160f02013-09-01 23:04:27227}
228
rvera26f0a1392017-05-02 22:25:44229// Test AdoptConnectedSocket()
230TEST_F(TCPSocketTest, AdoptConnectedSocket) {
Sergey Ulanov7cbcbc52017-07-26 18:29:13231 TCPSocket accepting_socket(nullptr, nullptr, NetLogSource());
rvera26f0a1392017-05-02 22:25:44232 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 Ulanov7cbcbc52017-07-26 18:29:13241 TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr,
rvera26f0a1392017-05-02 22:25:44242 NetLogSource());
Sergey Ulanov7cbcbc52017-07-26 18:29:13243 int connect_result = connecting_socket.Connect(connect_callback.callback());
rvera26f0a1392017-05-02 22:25:44244
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 Ulanov7cbcbc52017-07-26 18:29:13250 ASSERT_THAT(accept_callback.GetResult(result), IsOk());
rvera26f0a1392017-05-02 22:25:44251
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 Ulanov7cbcbc52017-07-26 18:29:13264 EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk());
rvera26f0a1392017-05-02 22:25:44265}
266
267// Test Accept() for AdoptUnconnectedSocket.
268TEST_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]ef2f0022014-04-29 10:24:35272
martijna2e83bd2016-03-18 13:10:45273 IPEndPoint address(IPAddress::IPv4Localhost(), 0);
[email protected]ef2f0022014-04-29 10:24:35274 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
robpercival214763f2016-07-01 23:27:01278 ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk());
279 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
[email protected]ef2f0022014-04-29 10:24:35280
281 TestAcceptAsync();
282}
[email protected]ef2f0022014-04-29 10:24:35283
[email protected]21160f02013-09-01 23:04:27284// Accept two connections simultaneously.
285TEST_F(TCPSocketTest, Accept2Connections) {
286 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
287
288 TestCompletionCallback accept_callback;
danakj655b66c2016-04-16 00:51:38289 std::unique_ptr<TCPSocket> accepted_socket;
[email protected]21160f02013-09-01 23:04:27290 IPEndPoint accepted_address;
291
Sergey Ulanov7cbcbc52017-07-26 18:29:13292 ASSERT_THAT(socket_.Accept(&accepted_socket, &accepted_address,
293 accept_callback.callback()),
294 IsError(ERR_IO_PENDING));
[email protected]21160f02013-09-01 23:04:27295
296 TestCompletionCallback connect_callback;
Sergey Ulanov7cbcbc52017-07-26 18:29:13297 TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr,
mikecironef22f9812016-10-04 03:40:19298 NetLogSource());
Sergey Ulanov7cbcbc52017-07-26 18:29:13299 int connect_result = connecting_socket.Connect(connect_callback.callback());
[email protected]21160f02013-09-01 23:04:27300
301 TestCompletionCallback connect_callback2;
Sergey Ulanov7cbcbc52017-07-26 18:29:13302 TCPClientSocket connecting_socket2(local_address_list(), nullptr, nullptr,
mikecironef22f9812016-10-04 03:40:19303 NetLogSource());
Sergey Ulanov7cbcbc52017-07-26 18:29:13304 int connect_result2 =
305 connecting_socket2.Connect(connect_callback2.callback());
[email protected]21160f02013-09-01 23:04:27306
robpercival214763f2016-07-01 23:27:01307 EXPECT_THAT(accept_callback.WaitForResult(), IsOk());
[email protected]21160f02013-09-01 23:04:27308
309 TestCompletionCallback accept_callback2;
danakj655b66c2016-04-16 00:51:38310 std::unique_ptr<TCPSocket> accepted_socket2;
[email protected]21160f02013-09-01 23:04:27311 IPEndPoint accepted_address2;
312
313 int result = socket_.Accept(&accepted_socket2, &accepted_address2,
314 accept_callback2.callback());
Sergey Ulanov7cbcbc52017-07-26 18:29:13315 ASSERT_THAT(accept_callback2.GetResult(result), IsOk());
[email protected]21160f02013-09-01 23:04:27316
Sergey Ulanov7cbcbc52017-07-26 18:29:13317 EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk());
318 EXPECT_THAT(connect_callback2.GetResult(connect_result2), IsOk());
[email protected]21160f02013-09-01 23:04:27319
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.
329TEST_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 Ulanov7cbcbc52017-07-26 18:29:13336 TCPClientSocket connecting_socket(local_address_list(), nullptr, nullptr,
mikecironef22f9812016-10-04 03:40:19337 NetLogSource());
Sergey Ulanov7cbcbc52017-07-26 18:29:13338 int connect_result = connecting_socket.Connect(connect_callback.callback());
[email protected]21160f02013-09-01 23:04:27339
340 TestCompletionCallback accept_callback;
danakj655b66c2016-04-16 00:51:38341 std::unique_ptr<TCPSocket> accepted_socket;
[email protected]21160f02013-09-01 23:04:27342 IPEndPoint accepted_address;
343 int result = socket_.Accept(&accepted_socket, &accepted_address,
344 accept_callback.callback());
Sergey Ulanov7cbcbc52017-07-26 18:29:13345 ASSERT_THAT(accept_callback.GetResult(result), IsOk());
[email protected]21160f02013-09-01 23:04:27346
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 Ulanov7cbcbc52017-07-26 18:29:13352 EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk());
[email protected]21160f02013-09-01 23:04:27353}
354
[email protected]c9080d82013-09-15 15:14:16355TEST_F(TCPSocketTest, ReadWrite) {
356 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
357
358 TestCompletionCallback connect_callback;
Sergey Ulanov7cbcbc52017-07-26 18:29:13359 TCPSocket connecting_socket(nullptr, nullptr, NetLogSource());
[email protected]c9080d82013-09-15 15:14:16360 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4);
robpercival214763f2016-07-01 23:27:01361 ASSERT_THAT(result, IsOk());
Sergey Ulanov7cbcbc52017-07-26 18:29:13362 int connect_result =
363 connecting_socket.Connect(local_address_, connect_callback.callback());
[email protected]c9080d82013-09-15 15:14:16364
365 TestCompletionCallback accept_callback;
danakj655b66c2016-04-16 00:51:38366 std::unique_ptr<TCPSocket> accepted_socket;
[email protected]c9080d82013-09-15 15:14:16367 IPEndPoint accepted_address;
368 result = socket_.Accept(&accepted_socket, &accepted_address,
369 accept_callback.callback());
robpercival214763f2016-07-01 23:27:01370 ASSERT_THAT(accept_callback.GetResult(result), IsOk());
[email protected]c9080d82013-09-15 15:14:16371
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 Ulanov7cbcbc52017-07-26 18:29:13377 EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk());
[email protected]c9080d82013-09-15 15:14:16378
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
tbansal7b403bcc2016-04-13 22:33:21416// 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.
421TEST_F(TCPSocketTest, SPWNotInterested) {
tbansal180587c2017-02-16 15:13:23422 TestSPWNotifications(false, 2u, 0u, 0u);
tbansal7b403bcc2016-04-13 22:33:21423}
424
425// One notification should be received when the socket connects. One
tbansal180587c2017-02-16 15:13:23426// additional notification should be received for each message read.
427TEST_F(TCPSocketTest, SPWNoAdvance) {
428 TestSPWNotifications(true, 2u, 0u, 3u);
tbansal7b403bcc2016-04-13 22:33:21429}
430#endif // defined(TCP_INFO) || defined(OS_LINUX)
431
[email protected]21160f02013-09-01 23:04:27432} // namespace
433} // namespace net