Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: net/socket/tcp_socket_unittest.cc

Issue 2333923004: Extracting NetLog inner classes into their own classes. (Closed)
Patch Set: Some nit fixes and better, impl-agnostic naming of net_log_parameters_callback_typedef.h -> net/log… Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/socket/tcp_socket.h" 5 #include "net/socket/tcp_socket.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/test/simple_test_tick_clock.h" 15 #include "base/test/simple_test_tick_clock.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "net/base/address_list.h" 17 #include "net/base/address_list.h"
18 #include "net/base/io_buffer.h" 18 #include "net/base/io_buffer.h"
19 #include "net/base/ip_endpoint.h" 19 #include "net/base/ip_endpoint.h"
20 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
21 #include "net/base/sockaddr_storage.h" 21 #include "net/base/sockaddr_storage.h"
22 #include "net/base/test_completion_callback.h" 22 #include "net/base/test_completion_callback.h"
23 #include "net/log/net_log_source.h"
23 #include "net/socket/socket_performance_watcher.h" 24 #include "net/socket/socket_performance_watcher.h"
24 #include "net/socket/tcp_client_socket.h" 25 #include "net/socket/tcp_client_socket.h"
25 #include "net/test/gtest_util.h" 26 #include "net/test/gtest_util.h"
26 #include "testing/gmock/include/gmock/gmock.h" 27 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/platform_test.h" 29 #include "testing/platform_test.h"
29 30
30 using net::test::IsOk; 31 using net::test::IsOk;
31 32
32 namespace net { 33 namespace net {
(...skipping 27 matching lines...) Expand all
60 size_t connection_changed_count_; 61 size_t connection_changed_count_;
61 size_t rtt_notification_count_; 62 size_t rtt_notification_count_;
62 63
63 DISALLOW_COPY_AND_ASSIGN(TestSocketPerformanceWatcher); 64 DISALLOW_COPY_AND_ASSIGN(TestSocketPerformanceWatcher);
64 }; 65 };
65 66
66 const int kListenBacklog = 5; 67 const int kListenBacklog = 5;
67 68
68 class TCPSocketTest : public PlatformTest { 69 class TCPSocketTest : public PlatformTest {
69 protected: 70 protected:
70 TCPSocketTest() : socket_(NULL, NULL, NetLog::Source()) {} 71 TCPSocketTest() : socket_(NULL, NULL, NetLogSource()) {}
71 72
72 void SetUpListenIPv4() { 73 void SetUpListenIPv4() {
73 ASSERT_THAT(socket_.Open(ADDRESS_FAMILY_IPV4), IsOk()); 74 ASSERT_THAT(socket_.Open(ADDRESS_FAMILY_IPV4), IsOk());
74 ASSERT_THAT(socket_.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), 75 ASSERT_THAT(socket_.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)),
75 IsOk()); 76 IsOk());
76 ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk()); 77 ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk());
77 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); 78 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
78 } 79 }
79 80
80 void SetUpListenIPv6(bool* success) { 81 void SetUpListenIPv6(bool* success) {
(...skipping 13 matching lines...) Expand all
94 void TestAcceptAsync() { 95 void TestAcceptAsync() {
95 TestCompletionCallback accept_callback; 96 TestCompletionCallback accept_callback;
96 std::unique_ptr<TCPSocket> accepted_socket; 97 std::unique_ptr<TCPSocket> accepted_socket;
97 IPEndPoint accepted_address; 98 IPEndPoint accepted_address;
98 ASSERT_EQ(ERR_IO_PENDING, 99 ASSERT_EQ(ERR_IO_PENDING,
99 socket_.Accept(&accepted_socket, &accepted_address, 100 socket_.Accept(&accepted_socket, &accepted_address,
100 accept_callback.callback())); 101 accept_callback.callback()));
101 102
102 TestCompletionCallback connect_callback; 103 TestCompletionCallback connect_callback;
103 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, 104 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
104 NetLog::Source()); 105 NetLogSource());
105 connecting_socket.Connect(connect_callback.callback()); 106 connecting_socket.Connect(connect_callback.callback());
106 107
107 EXPECT_THAT(connect_callback.WaitForResult(), IsOk()); 108 EXPECT_THAT(connect_callback.WaitForResult(), IsOk());
108 EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); 109 EXPECT_THAT(accept_callback.WaitForResult(), IsOk());
109 110
110 EXPECT_TRUE(accepted_socket.get()); 111 EXPECT_TRUE(accepted_socket.get());
111 112
112 // Both sockets should be on the loopback network interface. 113 // Both sockets should be on the loopback network interface.
113 EXPECT_EQ(accepted_address.address(), local_address_.address()); 114 EXPECT_EQ(accepted_address.address(), local_address_.address());
114 } 115 }
(...skipping 20 matching lines...) Expand all
135 new base::SimpleTestTickClock()); 136 new base::SimpleTestTickClock());
136 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); 137 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get();
137 tick_clock_ptr->SetNowTicks(base::TimeTicks::Now()); 138 tick_clock_ptr->SetNowTicks(base::TimeTicks::Now());
138 139
139 TestCompletionCallback connect_callback; 140 TestCompletionCallback connect_callback;
140 141
141 std::unique_ptr<TestSocketPerformanceWatcher> watcher( 142 std::unique_ptr<TestSocketPerformanceWatcher> watcher(
142 new TestSocketPerformanceWatcher(should_notify_updated_rtt)); 143 new TestSocketPerformanceWatcher(should_notify_updated_rtt));
143 TestSocketPerformanceWatcher* watcher_ptr = watcher.get(); 144 TestSocketPerformanceWatcher* watcher_ptr = watcher.get();
144 145
145 TCPSocket connecting_socket(std::move(watcher), NULL, NetLog::Source()); 146 TCPSocket connecting_socket(std::move(watcher), NULL, NetLogSource());
146 connecting_socket.SetTickClockForTesting(std::move(tick_clock)); 147 connecting_socket.SetTickClockForTesting(std::move(tick_clock));
147 148
148 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4); 149 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4);
149 ASSERT_THAT(result, IsOk()); 150 ASSERT_THAT(result, IsOk());
150 connecting_socket.Connect(local_address_, connect_callback.callback()); 151 connecting_socket.Connect(local_address_, connect_callback.callback());
151 152
152 TestCompletionCallback accept_callback; 153 TestCompletionCallback accept_callback;
153 std::unique_ptr<TCPSocket> accepted_socket; 154 std::unique_ptr<TCPSocket> accepted_socket;
154 IPEndPoint accepted_address; 155 IPEndPoint accepted_address;
155 result = socket_.Accept(&accepted_socket, &accepted_address, 156 result = socket_.Accept(&accepted_socket, &accepted_address,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 }; 204 };
204 205
205 // Test listening and accepting with a socket bound to an IPv4 address. 206 // Test listening and accepting with a socket bound to an IPv4 address.
206 TEST_F(TCPSocketTest, Accept) { 207 TEST_F(TCPSocketTest, Accept) {
207 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); 208 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
208 209
209 TestCompletionCallback connect_callback; 210 TestCompletionCallback connect_callback;
210 // TODO(yzshen): Switch to use TCPSocket when it supports client socket 211 // TODO(yzshen): Switch to use TCPSocket when it supports client socket
211 // operations. 212 // operations.
212 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, 213 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
213 NetLog::Source()); 214 NetLogSource());
214 connecting_socket.Connect(connect_callback.callback()); 215 connecting_socket.Connect(connect_callback.callback());
215 216
216 TestCompletionCallback accept_callback; 217 TestCompletionCallback accept_callback;
217 std::unique_ptr<TCPSocket> accepted_socket; 218 std::unique_ptr<TCPSocket> accepted_socket;
218 IPEndPoint accepted_address; 219 IPEndPoint accepted_address;
219 int result = socket_.Accept(&accepted_socket, &accepted_address, 220 int result = socket_.Accept(&accepted_socket, &accepted_address,
220 accept_callback.callback()); 221 accept_callback.callback());
221 if (result == ERR_IO_PENDING) 222 if (result == ERR_IO_PENDING)
222 result = accept_callback.WaitForResult(); 223 result = accept_callback.WaitForResult();
223 ASSERT_THAT(result, IsOk()); 224 ASSERT_THAT(result, IsOk());
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 TestCompletionCallback accept_callback; 263 TestCompletionCallback accept_callback;
263 std::unique_ptr<TCPSocket> accepted_socket; 264 std::unique_ptr<TCPSocket> accepted_socket;
264 IPEndPoint accepted_address; 265 IPEndPoint accepted_address;
265 266
266 ASSERT_EQ(ERR_IO_PENDING, 267 ASSERT_EQ(ERR_IO_PENDING,
267 socket_.Accept(&accepted_socket, &accepted_address, 268 socket_.Accept(&accepted_socket, &accepted_address,
268 accept_callback.callback())); 269 accept_callback.callback()));
269 270
270 TestCompletionCallback connect_callback; 271 TestCompletionCallback connect_callback;
271 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, 272 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
272 NetLog::Source()); 273 NetLogSource());
273 connecting_socket.Connect(connect_callback.callback()); 274 connecting_socket.Connect(connect_callback.callback());
274 275
275 TestCompletionCallback connect_callback2; 276 TestCompletionCallback connect_callback2;
276 TCPClientSocket connecting_socket2(local_address_list(), NULL, NULL, 277 TCPClientSocket connecting_socket2(local_address_list(), NULL, NULL,
277 NetLog::Source()); 278 NetLogSource());
278 connecting_socket2.Connect(connect_callback2.callback()); 279 connecting_socket2.Connect(connect_callback2.callback());
279 280
280 EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); 281 EXPECT_THAT(accept_callback.WaitForResult(), IsOk());
281 282
282 TestCompletionCallback accept_callback2; 283 TestCompletionCallback accept_callback2;
283 std::unique_ptr<TCPSocket> accepted_socket2; 284 std::unique_ptr<TCPSocket> accepted_socket2;
284 IPEndPoint accepted_address2; 285 IPEndPoint accepted_address2;
285 286
286 int result = socket_.Accept(&accepted_socket2, &accepted_address2, 287 int result = socket_.Accept(&accepted_socket2, &accepted_address2,
287 accept_callback2.callback()); 288 accept_callback2.callback());
(...skipping 14 matching lines...) Expand all
302 303
303 // Test listening and accepting with a socket bound to an IPv6 address. 304 // Test listening and accepting with a socket bound to an IPv6 address.
304 TEST_F(TCPSocketTest, AcceptIPv6) { 305 TEST_F(TCPSocketTest, AcceptIPv6) {
305 bool initialized = false; 306 bool initialized = false;
306 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv6(&initialized)); 307 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv6(&initialized));
307 if (!initialized) 308 if (!initialized)
308 return; 309 return;
309 310
310 TestCompletionCallback connect_callback; 311 TestCompletionCallback connect_callback;
311 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, 312 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
312 NetLog::Source()); 313 NetLogSource());
313 connecting_socket.Connect(connect_callback.callback()); 314 connecting_socket.Connect(connect_callback.callback());
314 315
315 TestCompletionCallback accept_callback; 316 TestCompletionCallback accept_callback;
316 std::unique_ptr<TCPSocket> accepted_socket; 317 std::unique_ptr<TCPSocket> accepted_socket;
317 IPEndPoint accepted_address; 318 IPEndPoint accepted_address;
318 int result = socket_.Accept(&accepted_socket, &accepted_address, 319 int result = socket_.Accept(&accepted_socket, &accepted_address,
319 accept_callback.callback()); 320 accept_callback.callback());
320 if (result == ERR_IO_PENDING) 321 if (result == ERR_IO_PENDING)
321 result = accept_callback.WaitForResult(); 322 result = accept_callback.WaitForResult();
322 ASSERT_THAT(result, IsOk()); 323 ASSERT_THAT(result, IsOk());
323 324
324 EXPECT_TRUE(accepted_socket.get()); 325 EXPECT_TRUE(accepted_socket.get());
325 326
326 // Both sockets should be on the loopback network interface. 327 // Both sockets should be on the loopback network interface.
327 EXPECT_EQ(accepted_address.address(), local_address_.address()); 328 EXPECT_EQ(accepted_address.address(), local_address_.address());
328 329
329 EXPECT_THAT(connect_callback.WaitForResult(), IsOk()); 330 EXPECT_THAT(connect_callback.WaitForResult(), IsOk());
330 } 331 }
331 332
332 TEST_F(TCPSocketTest, ReadWrite) { 333 TEST_F(TCPSocketTest, ReadWrite) {
333 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); 334 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
334 335
335 TestCompletionCallback connect_callback; 336 TestCompletionCallback connect_callback;
336 TCPSocket connecting_socket(NULL, NULL, NetLog::Source()); 337 TCPSocket connecting_socket(NULL, NULL, NetLogSource());
337 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4); 338 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4);
338 ASSERT_THAT(result, IsOk()); 339 ASSERT_THAT(result, IsOk());
339 connecting_socket.Connect(local_address_, connect_callback.callback()); 340 connecting_socket.Connect(local_address_, connect_callback.callback());
340 341
341 TestCompletionCallback accept_callback; 342 TestCompletionCallback accept_callback;
342 std::unique_ptr<TCPSocket> accepted_socket; 343 std::unique_ptr<TCPSocket> accepted_socket;
343 IPEndPoint accepted_address; 344 IPEndPoint accepted_address;
344 result = socket_.Accept(&accepted_socket, &accepted_address, 345 result = socket_.Accept(&accepted_socket, &accepted_address,
345 accept_callback.callback()); 346 accept_callback.callback());
346 ASSERT_THAT(accept_callback.GetResult(result), IsOk()); 347 ASSERT_THAT(accept_callback.GetResult(result), IsOk());
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 // additional notification should be received for each message read since this 410 // additional notification should be received for each message read since this
410 // test advances clock by 2 seconds (which is longer than the minimum interval 411 // test advances clock by 2 seconds (which is longer than the minimum interval
411 // between consecutive notifications) before every read. 412 // between consecutive notifications) before every read.
412 TEST_F(TCPSocketTest, SPWAdvance) { 413 TEST_F(TCPSocketTest, SPWAdvance) {
413 TestSPWNotifications(base::TimeDelta::FromSeconds(2), true, 2u, 0u, 3u); 414 TestSPWNotifications(base::TimeDelta::FromSeconds(2), true, 2u, 0u, 3u);
414 } 415 }
415 #endif // defined(TCP_INFO) || defined(OS_LINUX) 416 #endif // defined(TCP_INFO) || defined(OS_LINUX)
416 417
417 } // namespace 418 } // namespace
418 } // namespace net 419 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698