Chromium Code Reviews| Index: net/socket/tcp_socket_unittest.cc |
| diff --git a/net/socket/tcp_socket_unittest.cc b/net/socket/tcp_socket_unittest.cc |
| index d9da13c660cc5f7910de744af5932ff324f575d0..60f332b08236876d43afea19b7f6ed833fbe886c 100644 |
| --- a/net/socket/tcp_socket_unittest.cc |
| +++ b/net/socket/tcp_socket_unittest.cc |
| @@ -226,12 +226,61 @@ TEST_F(TCPSocketTest, AcceptAsync) { |
| TestAcceptAsync(); |
| } |
| -#if defined(OS_WIN) |
| -// Test Accept() for AdoptListenSocket. |
| -TEST_F(TCPSocketTest, AcceptForAdoptedListenSocket) { |
| - // Create a socket to be used with AdoptListenSocket. |
| - SOCKET existing_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| - ASSERT_THAT(socket_.AdoptListenSocket(existing_socket), IsOk()); |
| +// Test AdoptConnectedSocket() |
| +TEST_F(TCPSocketTest, AdoptConnectedSocket) { |
| + TCPSocket accepting_socket(NULL, NULL, NetLogSource()); |
| + ASSERT_THAT(accepting_socket.Open(ADDRESS_FAMILY_IPV4), IsOk()); |
| + ASSERT_THAT(accepting_socket.Bind(IPEndPoint(IPAddress::IPv4Localhost(), 0)), |
| + IsOk()); |
| + ASSERT_THAT(accepting_socket.GetLocalAddress(&local_address_), IsOk()); |
| + ASSERT_THAT(accepting_socket.Listen(kListenBacklog), IsOk()); |
| + |
| + TestCompletionCallback connect_callback; |
| + // TODO(yzshen): Switch to use TCPSocket when it supports client socket |
| + // operations. |
| + TCPClientSocket connecting_socket(local_address_list(), NULL, NULL, |
| + NetLogSource()); |
| + connecting_socket.Connect(connect_callback.callback()); |
| + |
| + TestCompletionCallback accept_callback; |
| + std::unique_ptr<TCPSocket> accepted_socket; |
| + IPEndPoint accepted_address; |
| + int result = accepting_socket.Accept(&accepted_socket, &accepted_address, |
| + accept_callback.callback()); |
| + if (result == ERR_IO_PENDING) |
| + result = accept_callback.WaitForResult(); |
| + ASSERT_THAT(result, IsOk()); |
| + |
| + SocketDescriptor accepted_descriptor = accepted_socket->GetSocketDescriptor(); |
| + |
| + // Note that this takes owenership of the descriptor, which causes the socket |
| + // to be closed twice at the os level, once for socket_ and once for |
| + // accepted_socket, due to the unique_ptrs involved. The test passes, but this |
| + // generates a run-time error message. |
| + // The double close is difficult to avoid, as it the would require avoiding |
| + // the unique_prts by using lower-level calls to set up the accepting socket |
| + // and do the accept. Doing the above accept asynchronously involves a fair |
| + // amount of platform-specific code. Doing the connect asynchronously and the |
| + // accept synchronously deadlocks, presumably because it's racing the connect |
| + // somehow. In the end keeping this code simpler and accepting a benign error |
| + // 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.
|
| + ASSERT_THAT( |
| + socket_.AdoptConnectedSocket(accepted_descriptor, accepted_address), |
| + IsOk()); |
| + |
| + // socket_ should now have the local address. |
| + IPEndPoint adopted_address; |
| + ASSERT_THAT(socket_.GetLocalAddress(&adopted_address), IsOk()); |
| + EXPECT_EQ(local_address_.address(), adopted_address.address()); |
| + |
| + EXPECT_THAT(connect_callback.WaitForResult(), IsOk()); |
| +} |
| + |
| +// Test Accept() for AdoptUnconnectedSocket. |
| +TEST_F(TCPSocketTest, AcceptForAdoptedUnconnectedSocket) { |
| + SocketDescriptor existing_socket = |
| + CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| + ASSERT_THAT(socket_.AdoptUnconnectedSocket(existing_socket), IsOk()); |
| IPEndPoint address(IPAddress::IPv4Localhost(), 0); |
| SockaddrStorage storage; |
| @@ -243,7 +292,6 @@ TEST_F(TCPSocketTest, AcceptForAdoptedListenSocket) { |
| TestAcceptAsync(); |
| } |
| -#endif |
| // Accept two connections simultaneously. |
| TEST_F(TCPSocketTest, Accept2Connections) { |