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

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

Issue 2815993002: Adds a method to TCPServerSocket to adopt a socket. (Closed)
Patch Set: Rebased to fix merge conflict Created 3 years, 8 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/files/file_util.h"
mmenke 2017/04/21 19:20:38 I don't think this is used?
Raul Vera 2017/05/01 05:35:25 It was, but isn't now, so I've removed it.
14 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
16 #include "net/base/address_list.h" 17 #include "net/base/address_list.h"
17 #include "net/base/io_buffer.h" 18 #include "net/base/io_buffer.h"
18 #include "net/base/ip_endpoint.h" 19 #include "net/base/ip_endpoint.h"
19 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
20 #include "net/base/sockaddr_storage.h" 21 #include "net/base/sockaddr_storage.h"
21 #include "net/base/test_completion_callback.h" 22 #include "net/base/test_completion_callback.h"
22 #include "net/log/net_log_source.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"
25 #if defined(OS_POSIX)
26 #include "net/socket/socket_posix.h"
27 #endif
mmenke 2017/04/21 19:20:38 No longer needed, I believe.
Raul Vera 2017/05/01 05:35:25 Good catch. I missed that. Thanks.
24 #include "net/socket/tcp_client_socket.h" 28 #include "net/socket/tcp_client_socket.h"
25 #include "net/test/gtest_util.h" 29 #include "net/test/gtest_util.h"
26 #include "testing/gmock/include/gmock/gmock.h" 30 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/platform_test.h" 32 #include "testing/platform_test.h"
29 33
30 using net::test::IsOk; 34 using net::test::IsOk;
31 35
32 namespace net { 36 namespace net {
33 37
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 223
220 EXPECT_THAT(connect_callback.WaitForResult(), IsOk()); 224 EXPECT_THAT(connect_callback.WaitForResult(), IsOk());
221 } 225 }
222 226
223 // Test Accept() callback. 227 // Test Accept() callback.
224 TEST_F(TCPSocketTest, AcceptAsync) { 228 TEST_F(TCPSocketTest, AcceptAsync) {
225 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); 229 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
226 TestAcceptAsync(); 230 TestAcceptAsync();
227 } 231 }
228 232
229 #if defined(OS_WIN) 233 // Test AdoptConnectedSocket()
230 // Test Accept() for AdoptListenSocket. 234 TEST_F(TCPSocketTest, AdoptConnectedSocket) {
231 TEST_F(TCPSocketTest, AcceptForAdoptedListenSocket) { 235 SocketDescriptor socket_fd =
232 // Create a socket to be used with AdoptListenSocket. 236 CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
233 SOCKET existing_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 237 ASSERT_GE(socket_fd, 0);
234 ASSERT_THAT(socket_.AdoptListenSocket(existing_socket), IsOk()); 238 ASSERT_TRUE(base::SetNonBlocking(socket_fd));
239 IPEndPoint address(IPAddress::IPv4Localhost(), 0);
240 SockaddrStorage storage;
241 ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len));
242 ASSERT_GE(bind(socket_fd, storage.addr, storage.addr_len), 0);
243 ASSERT_GE(listen(socket_fd, kListenBacklog), 0);
mmenke 2017/04/21 19:20:38 This isn't a connected socket, it's a listening so
Raul Vera 2017/05/01 05:35:25 Indeed. This turned out to require exposing GetSoc
244
245 ASSERT_THAT(socket_.AdoptConnectedSocket(socket_fd, IPEndPoint()), IsOk());
246 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
247
248 TestAcceptAsync();
249 }
250
251 // Test Accept() for AdoptUnconnectedSocket.
252 TEST_F(TCPSocketTest, AcceptForAdoptedUnconnectedSocket) {
253 // Create a socket to be used with AdoptUnconnectedSocket.
254 SocketDescriptor existing_socket =
255 CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
256 ASSERT_THAT(socket_.AdoptUnconnectedSocket(existing_socket), IsOk());
235 257
236 IPEndPoint address(IPAddress::IPv4Localhost(), 0); 258 IPEndPoint address(IPAddress::IPv4Localhost(), 0);
237 SockaddrStorage storage; 259 SockaddrStorage storage;
238 ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len)); 260 ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len));
239 ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len)); 261 ASSERT_EQ(bind(existing_socket, storage.addr, storage.addr_len), 0);
mmenke 2017/04/21 19:20:38 Expected should go before actual, for ASSERT_EQ (Y
Raul Vera 2017/05/01 05:35:25 That code is gone now, but thanks.
240 262
241 ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk()); 263 ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk());
242 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); 264 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
243 265
244 TestAcceptAsync(); 266 TestAcceptAsync();
245 } 267 }
246 #endif
247 268
248 // Accept two connections simultaneously. 269 // Accept two connections simultaneously.
249 TEST_F(TCPSocketTest, Accept2Connections) { 270 TEST_F(TCPSocketTest, Accept2Connections) {
250 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); 271 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
251 272
252 TestCompletionCallback accept_callback; 273 TestCompletionCallback accept_callback;
253 std::unique_ptr<TCPSocket> accepted_socket; 274 std::unique_ptr<TCPSocket> accepted_socket;
254 IPEndPoint accepted_address; 275 IPEndPoint accepted_address;
255 276
256 ASSERT_EQ(ERR_IO_PENDING, 277 ASSERT_EQ(ERR_IO_PENDING,
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 411
391 // One notification should be received when the socket connects. One 412 // One notification should be received when the socket connects. One
392 // additional notification should be received for each message read. 413 // additional notification should be received for each message read.
393 TEST_F(TCPSocketTest, SPWNoAdvance) { 414 TEST_F(TCPSocketTest, SPWNoAdvance) {
394 TestSPWNotifications(true, 2u, 0u, 3u); 415 TestSPWNotifications(true, 2u, 0u, 3u);
395 } 416 }
396 #endif // defined(TCP_INFO) || defined(OS_LINUX) 417 #endif // defined(TCP_INFO) || defined(OS_LINUX)
397 418
398 } // namespace 419 } // namespace
399 } // namespace net 420 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698