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

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

Issue 2815993002: Adds a method to TCPServerSocket to adopt a socket. (Closed)
Patch Set: Refined GetSocketDescriptor per review. 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
« no previous file with comments | « net/socket/tcp_socket_posix.cc ('k') | net/socket/tcp_socket_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 =
255 accepted_socket->ReleaseSocketDescriptorForTesting();
256
257 ASSERT_THAT(
258 socket_.AdoptConnectedSocket(accepted_descriptor, accepted_address),
259 IsOk());
260
261 // socket_ should now have the local address.
262 IPEndPoint adopted_address;
263 ASSERT_THAT(socket_.GetLocalAddress(&adopted_address), IsOk());
264 EXPECT_EQ(local_address_.address(), adopted_address.address());
265
266 EXPECT_THAT(connect_callback.WaitForResult(), IsOk());
267 }
268
269 // Test Accept() for AdoptUnconnectedSocket.
270 TEST_F(TCPSocketTest, AcceptForAdoptedUnconnectedSocket) {
271 SocketDescriptor existing_socket =
272 CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
273 ASSERT_THAT(socket_.AdoptUnconnectedSocket(existing_socket), IsOk());
235 274
236 IPEndPoint address(IPAddress::IPv4Localhost(), 0); 275 IPEndPoint address(IPAddress::IPv4Localhost(), 0);
237 SockaddrStorage storage; 276 SockaddrStorage storage;
238 ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len)); 277 ASSERT_TRUE(address.ToSockAddr(storage.addr, &storage.addr_len));
239 ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len)); 278 ASSERT_EQ(0, bind(existing_socket, storage.addr, storage.addr_len));
240 279
241 ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk()); 280 ASSERT_THAT(socket_.Listen(kListenBacklog), IsOk());
242 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk()); 281 ASSERT_THAT(socket_.GetLocalAddress(&local_address_), IsOk());
243 282
244 TestAcceptAsync(); 283 TestAcceptAsync();
245 } 284 }
246 #endif
247 285
248 // Accept two connections simultaneously. 286 // Accept two connections simultaneously.
249 TEST_F(TCPSocketTest, Accept2Connections) { 287 TEST_F(TCPSocketTest, Accept2Connections) {
250 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); 288 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
251 289
252 TestCompletionCallback accept_callback; 290 TestCompletionCallback accept_callback;
253 std::unique_ptr<TCPSocket> accepted_socket; 291 std::unique_ptr<TCPSocket> accepted_socket;
254 IPEndPoint accepted_address; 292 IPEndPoint accepted_address;
255 293
256 ASSERT_EQ(ERR_IO_PENDING, 294 ASSERT_EQ(ERR_IO_PENDING,
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 428
391 // One notification should be received when the socket connects. One 429 // One notification should be received when the socket connects. One
392 // additional notification should be received for each message read. 430 // additional notification should be received for each message read.
393 TEST_F(TCPSocketTest, SPWNoAdvance) { 431 TEST_F(TCPSocketTest, SPWNoAdvance) {
394 TestSPWNotifications(true, 2u, 0u, 3u); 432 TestSPWNotifications(true, 2u, 0u, 3u);
395 } 433 }
396 #endif // defined(TCP_INFO) || defined(OS_LINUX) 434 #endif // defined(TCP_INFO) || defined(OS_LINUX)
397 435
398 } // namespace 436 } // namespace
399 } // namespace net 437 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/tcp_socket_posix.cc ('k') | net/socket/tcp_socket_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698