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

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

Issue 2815993002: Adds a method to TCPServerSocket to adopt a socket. (Closed)
Patch Set: Fixed typo in Windows code. Created 3 years, 7 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>
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 219
220 EXPECT_THAT(connect_callback.WaitForResult(), IsOk()); 220 EXPECT_THAT(connect_callback.WaitForResult(), IsOk());
221 } 221 }
222 222
223 // Test Accept() callback. 223 // Test Accept() callback.
224 TEST_F(TCPSocketTest, AcceptAsync) { 224 TEST_F(TCPSocketTest, AcceptAsync) {
225 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); 225 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
226 TestAcceptAsync(); 226 TestAcceptAsync();
227 } 227 }
228 228
229 #if defined(OS_WIN) 229 // Test AdoptConnectedSocket()
230 // Test Accept() for AdoptListenSocket. 230 TEST_F(TCPSocketTest, AdoptConnectedSocket) {
231 TEST_F(TCPSocketTest, AcceptForAdoptedListenSocket) { 231 TCPSocket accepting_socket(NULL, NULL, NetLogSource());
232 // Create a socket to be used with AdoptListenSocket. 232 ASSERT_THAT(accepting_socket.Open(ADDRESS_FAMILY_IPV4), IsOk());
233 SOCKET existing_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 233 ASSERT_THAT(accepting_socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)),
234 ASSERT_THAT(socket_.AdoptListenSocket(existing_socket), IsOk()); 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.
241 TCPClientSocket connecting_socket(local_address_list(), NULL, NULL,
242 NetLogSource());
243 connecting_socket.Connect(connect_callback.callback());
244
245 TestCompletionCallback accept_callback;
246 std::unique_ptr<TCPSocket> accepted_socket;
247 IPEndPoint accepted_address;
248 int result = accepting_socket.Accept(&accepted_socket, &accepted_address,
249 accept_callback.callback());
250 if (result == ERR_IO_PENDING)
251 result = accept_callback.WaitForResult();
252 ASSERT_THAT(result, IsOk());
253
254 SocketDescriptor accepted_descriptor = accepted_socket->GetSocketDescriptor();
255
256 // Note that this takes owenership of the descriptor, which causes the socket
257 // to be closed twice at the os level, once for socket_ and once for
258 // accepted_socket, due to the unique_ptrs involved. The test passes, but this
259 // generates a run-time error message.
260 // The double close is difficult to avoid, as it the would require avoiding
261 // the unique_prts by using lower-level calls to set up the accepting socket
262 // and do the accept. Doing the above accept asynchronously involves a fair
263 // amount of platform-specific code. Doing the connect asynchronously and the
264 // accept synchronously deadlocks, presumably because it's racing the connect
265 // somehow. In the end keeping this code simpler and accepting a benign error
266 // message seems the best tradeoff.
mmenke 2017/05/01 18:44:19 Can we do better? Shouldn't be hard to make GetSo
Raul Vera 2017/05/02 00:08:53 Excellent. All done. Thank you.
267 ASSERT_THAT(
268 socket_.AdoptConnectedSocket(accepted_descriptor, accepted_address),
269 IsOk());
270
271 // socket_ should now have the local address.
272 IPEndPoint adopted_address;
273 ASSERT_THAT(socket_.GetLocalAddress(&adopted_address), IsOk());
274 EXPECT_EQ(local_address_.address(), adopted_address.address());
275
276 EXPECT_THAT(connect_callback.WaitForResult(), IsOk());
277 }
278
279 // Test Accept() for AdoptUnconnectedSocket.
280 TEST_F(TCPSocketTest, AcceptForAdoptedUnconnectedSocket) {
281 SocketDescriptor existing_socket =
282 CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
283 ASSERT_THAT(socket_.AdoptUnconnectedSocket(existing_socket), IsOk());
235 284
236 IPEndPoint address(IPAddress::IPv4Localhost(), 0); 285 IPEndPoint address(IPAddress::IPv4Localhost(), 0);
237 SockaddrStorage storage; 286 SockaddrStorage storage;
238 ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len)); 287 ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len));
239 ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len)); 288 ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len));
240 289
241 ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk()); 290 ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk());
242 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); 291 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
243 292
244 TestAcceptAsync(); 293 TestAcceptAsync();
245 } 294 }
246 #endif
247 295
248 // Accept two connections simultaneously. 296 // Accept two connections simultaneously.
249 TEST_F(TCPSocketTest, Accept2Connections) { 297 TEST_F(TCPSocketTest, Accept2Connections) {
250 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); 298 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
251 299
252 TestCompletionCallback accept_callback; 300 TestCompletionCallback accept_callback;
253 std::unique_ptr<TCPSocket> accepted_socket; 301 std::unique_ptr<TCPSocket> accepted_socket;
254 IPEndPoint accepted_address; 302 IPEndPoint accepted_address;
255 303
256 ASSERT_EQ(ERR_IO_PENDING, 304 ASSERT_EQ(ERR_IO_PENDING,
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 438
391 // One notification should be received when the socket connects. One 439 // One notification should be received when the socket connects. One
392 // additional notification should be received for each message read. 440 // additional notification should be received for each message read.
393 TEST_F(TCPSocketTest, SPWNoAdvance) { 441 TEST_F(TCPSocketTest, SPWNoAdvance) {
394 TestSPWNotifications(true, 2u, 0u, 3u); 442 TestSPWNotifications(true, 2u, 0u, 3u);
395 } 443 }
396 #endif // defined(TCP_INFO) || defined(OS_LINUX) 444 #endif // defined(TCP_INFO) || defined(OS_LINUX)
397 445
398 } // namespace 446 } // namespace
399 } // namespace net 447 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698