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

Side by Side Diff: webrtc/base/socket_unittest.cc

Issue 1920043002: Replace scoped_ptr with unique_ptr in webrtc/base/ (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebased Created 4 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 | « webrtc/base/signalthread_unittest.cc ('k') | webrtc/base/ssladapter_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2007 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2007 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <memory>
12
11 #include "webrtc/base/socket_unittest.h" 13 #include "webrtc/base/socket_unittest.h"
12 14
13 #include "webrtc/base/arraysize.h" 15 #include "webrtc/base/arraysize.h"
14 #include "webrtc/base/buffer.h" 16 #include "webrtc/base/buffer.h"
15 #include "webrtc/base/asyncudpsocket.h" 17 #include "webrtc/base/asyncudpsocket.h"
16 #include "webrtc/base/gunit.h" 18 #include "webrtc/base/gunit.h"
17 #include "webrtc/base/nethelpers.h" 19 #include "webrtc/base/nethelpers.h"
18 #include "webrtc/base/socketserver.h" 20 #include "webrtc/base/socketserver.h"
19 #include "webrtc/base/testclient.h" 21 #include "webrtc/base/testclient.h"
20 #include "webrtc/base/testutils.h" 22 #include "webrtc/base/testutils.h"
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 #else 193 #else
192 return address.family() == AF_UNSPEC; 194 return address.family() == AF_UNSPEC;
193 #endif 195 #endif
194 } 196 }
195 197
196 void SocketTest::ConnectInternal(const IPAddress& loopback) { 198 void SocketTest::ConnectInternal(const IPAddress& loopback) {
197 testing::StreamSink sink; 199 testing::StreamSink sink;
198 SocketAddress accept_addr; 200 SocketAddress accept_addr;
199 201
200 // Create client. 202 // Create client.
201 scoped_ptr<AsyncSocket> client(ss_->CreateAsyncSocket(loopback.family(), 203 std::unique_ptr<AsyncSocket> client(
202 SOCK_STREAM)); 204 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
203 sink.Monitor(client.get()); 205 sink.Monitor(client.get());
204 EXPECT_EQ(AsyncSocket::CS_CLOSED, client->GetState()); 206 EXPECT_EQ(AsyncSocket::CS_CLOSED, client->GetState());
205 EXPECT_PRED1(IsUnspecOrEmptyIP, client->GetLocalAddress().ipaddr()); 207 EXPECT_PRED1(IsUnspecOrEmptyIP, client->GetLocalAddress().ipaddr());
206 208
207 // Create server and listen. 209 // Create server and listen.
208 scoped_ptr<AsyncSocket> server( 210 std::unique_ptr<AsyncSocket> server(
209 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 211 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
210 sink.Monitor(server.get()); 212 sink.Monitor(server.get());
211 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 213 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
212 EXPECT_EQ(0, server->Listen(5)); 214 EXPECT_EQ(0, server->Listen(5));
213 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState()); 215 EXPECT_EQ(AsyncSocket::CS_CONNECTING, server->GetState());
214 216
215 // Ensure no pending server connections, since we haven't done anything yet. 217 // Ensure no pending server connections, since we haven't done anything yet.
216 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ)); 218 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ));
217 EXPECT_TRUE(NULL == server->Accept(&accept_addr)); 219 EXPECT_TRUE(NULL == server->Accept(&accept_addr));
218 EXPECT_TRUE(accept_addr.IsNil()); 220 EXPECT_TRUE(accept_addr.IsNil());
219 221
220 // Attempt connect to listening socket. 222 // Attempt connect to listening socket.
221 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 223 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
222 EXPECT_FALSE(client->GetLocalAddress().IsNil()); 224 EXPECT_FALSE(client->GetLocalAddress().IsNil());
223 EXPECT_NE(server->GetLocalAddress(), client->GetLocalAddress()); 225 EXPECT_NE(server->GetLocalAddress(), client->GetLocalAddress());
224 226
225 // Client is connecting, outcome not yet determined. 227 // Client is connecting, outcome not yet determined.
226 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client->GetState()); 228 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client->GetState());
227 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_OPEN)); 229 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_OPEN));
228 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE)); 230 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE));
229 231
230 // Server has pending connection, accept it. 232 // Server has pending connection, accept it.
231 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); 233 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
232 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); 234 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
233 ASSERT_TRUE(accepted); 235 ASSERT_TRUE(accepted);
234 EXPECT_FALSE(accept_addr.IsNil()); 236 EXPECT_FALSE(accept_addr.IsNil());
235 EXPECT_EQ(accepted->GetRemoteAddress(), accept_addr); 237 EXPECT_EQ(accepted->GetRemoteAddress(), accept_addr);
236 238
237 // Connected from server perspective, check the addresses are correct. 239 // Connected from server perspective, check the addresses are correct.
238 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState()); 240 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState());
239 EXPECT_EQ(server->GetLocalAddress(), accepted->GetLocalAddress()); 241 EXPECT_EQ(server->GetLocalAddress(), accepted->GetLocalAddress());
240 EXPECT_EQ(client->GetLocalAddress(), accepted->GetRemoteAddress()); 242 EXPECT_EQ(client->GetLocalAddress(), accepted->GetRemoteAddress());
241 243
242 // Connected from client perspective, check the addresses are correct. 244 // Connected from client perspective, check the addresses are correct.
243 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout); 245 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout);
244 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN)); 246 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN));
245 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE)); 247 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE));
246 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress()); 248 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress());
247 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress()); 249 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress());
248 } 250 }
249 251
250 void SocketTest::ConnectWithDnsLookupInternal(const IPAddress& loopback, 252 void SocketTest::ConnectWithDnsLookupInternal(const IPAddress& loopback,
251 const std::string& host) { 253 const std::string& host) {
252 testing::StreamSink sink; 254 testing::StreamSink sink;
253 SocketAddress accept_addr; 255 SocketAddress accept_addr;
254 256
255 // Create client. 257 // Create client.
256 scoped_ptr<AsyncSocket> client( 258 std::unique_ptr<AsyncSocket> client(
257 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 259 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
258 sink.Monitor(client.get()); 260 sink.Monitor(client.get());
259 261
260 // Create server and listen. 262 // Create server and listen.
261 scoped_ptr<AsyncSocket> server( 263 std::unique_ptr<AsyncSocket> server(
262 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 264 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
263 sink.Monitor(server.get()); 265 sink.Monitor(server.get());
264 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 266 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
265 EXPECT_EQ(0, server->Listen(5)); 267 EXPECT_EQ(0, server->Listen(5));
266 268
267 // Attempt connect to listening socket. 269 // Attempt connect to listening socket.
268 SocketAddress dns_addr(server->GetLocalAddress()); 270 SocketAddress dns_addr(server->GetLocalAddress());
269 dns_addr.SetIP(host); 271 dns_addr.SetIP(host);
270 EXPECT_EQ(0, client->Connect(dns_addr)); 272 EXPECT_EQ(0, client->Connect(dns_addr));
271 // TODO: Bind when doing DNS lookup. 273 // TODO: Bind when doing DNS lookup.
272 //EXPECT_NE(kEmptyAddr, client->GetLocalAddress()); // Implicit Bind 274 //EXPECT_NE(kEmptyAddr, client->GetLocalAddress()); // Implicit Bind
273 275
274 // Client is connecting, outcome not yet determined. 276 // Client is connecting, outcome not yet determined.
275 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client->GetState()); 277 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client->GetState());
276 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_OPEN)); 278 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_OPEN));
277 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE)); 279 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE));
278 280
279 // Server has pending connection, accept it. 281 // Server has pending connection, accept it.
280 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); 282 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
281 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); 283 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
282 ASSERT_TRUE(accepted); 284 ASSERT_TRUE(accepted);
283 EXPECT_FALSE(accept_addr.IsNil()); 285 EXPECT_FALSE(accept_addr.IsNil());
284 EXPECT_EQ(accepted->GetRemoteAddress(), accept_addr); 286 EXPECT_EQ(accepted->GetRemoteAddress(), accept_addr);
285 287
286 // Connected from server perspective, check the addresses are correct. 288 // Connected from server perspective, check the addresses are correct.
287 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState()); 289 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState());
288 EXPECT_EQ(server->GetLocalAddress(), accepted->GetLocalAddress()); 290 EXPECT_EQ(server->GetLocalAddress(), accepted->GetLocalAddress());
289 EXPECT_EQ(client->GetLocalAddress(), accepted->GetRemoteAddress()); 291 EXPECT_EQ(client->GetLocalAddress(), accepted->GetRemoteAddress());
290 292
291 // Connected from client perspective, check the addresses are correct. 293 // Connected from client perspective, check the addresses are correct.
292 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout); 294 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout);
293 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN)); 295 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN));
294 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE)); 296 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE));
295 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress()); 297 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress());
296 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress()); 298 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress());
297 } 299 }
298 300
299 void SocketTest::ConnectFailInternal(const IPAddress& loopback) { 301 void SocketTest::ConnectFailInternal(const IPAddress& loopback) {
300 testing::StreamSink sink; 302 testing::StreamSink sink;
301 SocketAddress accept_addr; 303 SocketAddress accept_addr;
302 304
303 // Create client. 305 // Create client.
304 scoped_ptr<AsyncSocket> client( 306 std::unique_ptr<AsyncSocket> client(
305 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 307 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
306 sink.Monitor(client.get()); 308 sink.Monitor(client.get());
307 309
308 // Create server, but don't listen yet. 310 // Create server, but don't listen yet.
309 scoped_ptr<AsyncSocket> server( 311 std::unique_ptr<AsyncSocket> server(
310 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 312 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
311 sink.Monitor(server.get()); 313 sink.Monitor(server.get());
312 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 314 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
313 315
314 // Attempt connect to a non-existent socket. 316 // Attempt connect to a non-existent socket.
315 // We don't connect to the server socket created above, since on 317 // We don't connect to the server socket created above, since on
316 // MacOS it takes about 75 seconds to get back an error! 318 // MacOS it takes about 75 seconds to get back an error!
317 SocketAddress bogus_addr(loopback, 65535); 319 SocketAddress bogus_addr(loopback, 65535);
318 EXPECT_EQ(0, client->Connect(bogus_addr)); 320 EXPECT_EQ(0, client->Connect(bogus_addr));
319 321
320 // Wait for connection to fail (ECONNREFUSED). 322 // Wait for connection to fail (ECONNREFUSED).
321 EXPECT_EQ_WAIT(AsyncSocket::CS_CLOSED, client->GetState(), kTimeout); 323 EXPECT_EQ_WAIT(AsyncSocket::CS_CLOSED, client->GetState(), kTimeout);
322 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_OPEN)); 324 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_OPEN));
323 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_ERROR)); 325 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_ERROR));
324 EXPECT_TRUE(client->GetRemoteAddress().IsNil()); 326 EXPECT_TRUE(client->GetRemoteAddress().IsNil());
325 327
326 // Should be no pending server connections. 328 // Should be no pending server connections.
327 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ)); 329 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ));
328 EXPECT_TRUE(NULL == server->Accept(&accept_addr)); 330 EXPECT_TRUE(NULL == server->Accept(&accept_addr));
329 EXPECT_EQ(IPAddress(), accept_addr.ipaddr()); 331 EXPECT_EQ(IPAddress(), accept_addr.ipaddr());
330 } 332 }
331 333
332 void SocketTest::ConnectWithDnsLookupFailInternal(const IPAddress& loopback) { 334 void SocketTest::ConnectWithDnsLookupFailInternal(const IPAddress& loopback) {
333 testing::StreamSink sink; 335 testing::StreamSink sink;
334 SocketAddress accept_addr; 336 SocketAddress accept_addr;
335 337
336 // Create client. 338 // Create client.
337 scoped_ptr<AsyncSocket> client( 339 std::unique_ptr<AsyncSocket> client(
338 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 340 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
339 sink.Monitor(client.get()); 341 sink.Monitor(client.get());
340 342
341 // Create server, but don't listen yet. 343 // Create server, but don't listen yet.
342 scoped_ptr<AsyncSocket> server( 344 std::unique_ptr<AsyncSocket> server(
343 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 345 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
344 sink.Monitor(server.get()); 346 sink.Monitor(server.get());
345 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 347 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
346 348
347 // Attempt connect to a non-existent host. 349 // Attempt connect to a non-existent host.
348 // We don't connect to the server socket created above, since on 350 // We don't connect to the server socket created above, since on
349 // MacOS it takes about 75 seconds to get back an error! 351 // MacOS it takes about 75 seconds to get back an error!
350 SocketAddress bogus_dns_addr("not-a-real-hostname", 65535); 352 SocketAddress bogus_dns_addr("not-a-real-hostname", 65535);
351 EXPECT_EQ(0, client->Connect(bogus_dns_addr)); 353 EXPECT_EQ(0, client->Connect(bogus_dns_addr));
352 354
(...skipping 12 matching lines...) Expand all
365 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_ERROR)); 367 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_ERROR));
366 EXPECT_TRUE(client->GetRemoteAddress().IsNil()); 368 EXPECT_TRUE(client->GetRemoteAddress().IsNil());
367 // Should be no pending server connections. 369 // Should be no pending server connections.
368 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ)); 370 EXPECT_FALSE(sink.Check(server.get(), testing::SSE_READ));
369 EXPECT_TRUE(NULL == server->Accept(&accept_addr)); 371 EXPECT_TRUE(NULL == server->Accept(&accept_addr));
370 EXPECT_TRUE(accept_addr.IsNil()); 372 EXPECT_TRUE(accept_addr.IsNil());
371 } 373 }
372 374
373 void SocketTest::ConnectWithClosedSocketInternal(const IPAddress& loopback) { 375 void SocketTest::ConnectWithClosedSocketInternal(const IPAddress& loopback) {
374 // Create server and listen. 376 // Create server and listen.
375 scoped_ptr<AsyncSocket> server( 377 std::unique_ptr<AsyncSocket> server(
376 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 378 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
377 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 379 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
378 EXPECT_EQ(0, server->Listen(5)); 380 EXPECT_EQ(0, server->Listen(5));
379 381
380 // Create a client and put in to CS_CLOSED state. 382 // Create a client and put in to CS_CLOSED state.
381 scoped_ptr<AsyncSocket> client( 383 std::unique_ptr<AsyncSocket> client(
382 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 384 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
383 EXPECT_EQ(0, client->Close()); 385 EXPECT_EQ(0, client->Close());
384 EXPECT_EQ(AsyncSocket::CS_CLOSED, client->GetState()); 386 EXPECT_EQ(AsyncSocket::CS_CLOSED, client->GetState());
385 387
386 // Connect() should reinitialize the socket, and put it in to CS_CONNECTING. 388 // Connect() should reinitialize the socket, and put it in to CS_CONNECTING.
387 EXPECT_EQ(0, client->Connect(SocketAddress(server->GetLocalAddress()))); 389 EXPECT_EQ(0, client->Connect(SocketAddress(server->GetLocalAddress())));
388 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client->GetState()); 390 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client->GetState());
389 } 391 }
390 392
391 void SocketTest::ConnectWhileNotClosedInternal(const IPAddress& loopback) { 393 void SocketTest::ConnectWhileNotClosedInternal(const IPAddress& loopback) {
392 // Create server and listen. 394 // Create server and listen.
393 testing::StreamSink sink; 395 testing::StreamSink sink;
394 scoped_ptr<AsyncSocket> server( 396 std::unique_ptr<AsyncSocket> server(
395 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 397 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
396 sink.Monitor(server.get()); 398 sink.Monitor(server.get());
397 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 399 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
398 EXPECT_EQ(0, server->Listen(5)); 400 EXPECT_EQ(0, server->Listen(5));
399 // Create client, connect. 401 // Create client, connect.
400 scoped_ptr<AsyncSocket> client( 402 std::unique_ptr<AsyncSocket> client(
401 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 403 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
402 EXPECT_EQ(0, client->Connect(SocketAddress(server->GetLocalAddress()))); 404 EXPECT_EQ(0, client->Connect(SocketAddress(server->GetLocalAddress())));
403 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client->GetState()); 405 EXPECT_EQ(AsyncSocket::CS_CONNECTING, client->GetState());
404 // Try to connect again. Should fail, but not interfere with original attempt. 406 // Try to connect again. Should fail, but not interfere with original attempt.
405 EXPECT_EQ(SOCKET_ERROR, 407 EXPECT_EQ(SOCKET_ERROR,
406 client->Connect(SocketAddress(server->GetLocalAddress()))); 408 client->Connect(SocketAddress(server->GetLocalAddress())));
407 409
408 // Accept the original connection. 410 // Accept the original connection.
409 SocketAddress accept_addr; 411 SocketAddress accept_addr;
410 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); 412 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
411 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); 413 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
412 ASSERT_TRUE(accepted); 414 ASSERT_TRUE(accepted);
413 EXPECT_FALSE(accept_addr.IsNil()); 415 EXPECT_FALSE(accept_addr.IsNil());
414 416
415 // Check the states and addresses. 417 // Check the states and addresses.
416 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState()); 418 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState());
417 EXPECT_EQ(server->GetLocalAddress(), accepted->GetLocalAddress()); 419 EXPECT_EQ(server->GetLocalAddress(), accepted->GetLocalAddress());
418 EXPECT_EQ(client->GetLocalAddress(), accepted->GetRemoteAddress()); 420 EXPECT_EQ(client->GetLocalAddress(), accepted->GetRemoteAddress());
419 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout); 421 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout);
420 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress()); 422 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress());
421 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress()); 423 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress());
422 424
423 // Try to connect again, to an unresolved hostname. 425 // Try to connect again, to an unresolved hostname.
424 // Shouldn't break anything. 426 // Shouldn't break anything.
425 EXPECT_EQ(SOCKET_ERROR, 427 EXPECT_EQ(SOCKET_ERROR,
426 client->Connect(SocketAddress("localhost", 428 client->Connect(SocketAddress("localhost",
427 server->GetLocalAddress().port()))); 429 server->GetLocalAddress().port())));
428 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState()); 430 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState());
429 EXPECT_EQ(AsyncSocket::CS_CONNECTED, client->GetState()); 431 EXPECT_EQ(AsyncSocket::CS_CONNECTED, client->GetState());
430 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress()); 432 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress());
431 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress()); 433 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress());
432 } 434 }
433 435
434 void SocketTest::ServerCloseDuringConnectInternal(const IPAddress& loopback) { 436 void SocketTest::ServerCloseDuringConnectInternal(const IPAddress& loopback) {
435 testing::StreamSink sink; 437 testing::StreamSink sink;
436 438
437 // Create client. 439 // Create client.
438 scoped_ptr<AsyncSocket> client( 440 std::unique_ptr<AsyncSocket> client(
439 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 441 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
440 sink.Monitor(client.get()); 442 sink.Monitor(client.get());
441 443
442 // Create server and listen. 444 // Create server and listen.
443 scoped_ptr<AsyncSocket> server( 445 std::unique_ptr<AsyncSocket> server(
444 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 446 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
445 sink.Monitor(server.get()); 447 sink.Monitor(server.get());
446 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 448 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
447 EXPECT_EQ(0, server->Listen(5)); 449 EXPECT_EQ(0, server->Listen(5));
448 450
449 // Attempt connect to listening socket. 451 // Attempt connect to listening socket.
450 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 452 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
451 453
452 // Close down the server while the socket is in the accept queue. 454 // Close down the server while the socket is in the accept queue.
453 EXPECT_TRUE_WAIT(sink.Check(server.get(), testing::SSE_READ), kTimeout); 455 EXPECT_TRUE_WAIT(sink.Check(server.get(), testing::SSE_READ), kTimeout);
454 server->Close(); 456 server->Close();
455 457
456 // This should fail the connection for the client. Clean up. 458 // This should fail the connection for the client. Clean up.
457 EXPECT_EQ_WAIT(AsyncSocket::CS_CLOSED, client->GetState(), kTimeout); 459 EXPECT_EQ_WAIT(AsyncSocket::CS_CLOSED, client->GetState(), kTimeout);
458 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_ERROR)); 460 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_ERROR));
459 client->Close(); 461 client->Close();
460 } 462 }
461 463
462 void SocketTest::ClientCloseDuringConnectInternal(const IPAddress& loopback) { 464 void SocketTest::ClientCloseDuringConnectInternal(const IPAddress& loopback) {
463 testing::StreamSink sink; 465 testing::StreamSink sink;
464 SocketAddress accept_addr; 466 SocketAddress accept_addr;
465 467
466 // Create client. 468 // Create client.
467 scoped_ptr<AsyncSocket> client( 469 std::unique_ptr<AsyncSocket> client(
468 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 470 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
469 sink.Monitor(client.get()); 471 sink.Monitor(client.get());
470 472
471 // Create server and listen. 473 // Create server and listen.
472 scoped_ptr<AsyncSocket> server( 474 std::unique_ptr<AsyncSocket> server(
473 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 475 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
474 sink.Monitor(server.get()); 476 sink.Monitor(server.get());
475 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 477 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
476 EXPECT_EQ(0, server->Listen(5)); 478 EXPECT_EQ(0, server->Listen(5));
477 479
478 // Attempt connect to listening socket. 480 // Attempt connect to listening socket.
479 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 481 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
480 482
481 // Close down the client while the socket is in the accept queue. 483 // Close down the client while the socket is in the accept queue.
482 EXPECT_TRUE_WAIT(sink.Check(server.get(), testing::SSE_READ), kTimeout); 484 EXPECT_TRUE_WAIT(sink.Check(server.get(), testing::SSE_READ), kTimeout);
483 client->Close(); 485 client->Close();
484 486
485 // The connection should still be able to be accepted. 487 // The connection should still be able to be accepted.
486 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); 488 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
487 ASSERT_TRUE(accepted); 489 ASSERT_TRUE(accepted);
488 sink.Monitor(accepted.get()); 490 sink.Monitor(accepted.get());
489 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState()); 491 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState());
490 492
491 // The accepted socket should then close (possibly with err, timing-related) 493 // The accepted socket should then close (possibly with err, timing-related)
492 EXPECT_EQ_WAIT(AsyncSocket::CS_CLOSED, accepted->GetState(), kTimeout); 494 EXPECT_EQ_WAIT(AsyncSocket::CS_CLOSED, accepted->GetState(), kTimeout);
493 EXPECT_TRUE(sink.Check(accepted.get(), testing::SSE_CLOSE) || 495 EXPECT_TRUE(sink.Check(accepted.get(), testing::SSE_CLOSE) ||
494 sink.Check(accepted.get(), testing::SSE_ERROR)); 496 sink.Check(accepted.get(), testing::SSE_ERROR));
495 497
496 // The client should not get a close event. 498 // The client should not get a close event.
497 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE)); 499 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE));
498 } 500 }
499 501
500 void SocketTest::ServerCloseInternal(const IPAddress& loopback) { 502 void SocketTest::ServerCloseInternal(const IPAddress& loopback) {
501 testing::StreamSink sink; 503 testing::StreamSink sink;
502 SocketAddress accept_addr; 504 SocketAddress accept_addr;
503 505
504 // Create client. 506 // Create client.
505 scoped_ptr<AsyncSocket> client( 507 std::unique_ptr<AsyncSocket> client(
506 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 508 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
507 sink.Monitor(client.get()); 509 sink.Monitor(client.get());
508 510
509 // Create server and listen. 511 // Create server and listen.
510 scoped_ptr<AsyncSocket> server( 512 std::unique_ptr<AsyncSocket> server(
511 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 513 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
512 sink.Monitor(server.get()); 514 sink.Monitor(server.get());
513 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 515 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
514 EXPECT_EQ(0, server->Listen(5)); 516 EXPECT_EQ(0, server->Listen(5));
515 517
516 // Attempt connection. 518 // Attempt connection.
517 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 519 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
518 520
519 // Accept connection. 521 // Accept connection.
520 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); 522 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
521 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); 523 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
522 ASSERT_TRUE(accepted); 524 ASSERT_TRUE(accepted);
523 sink.Monitor(accepted.get()); 525 sink.Monitor(accepted.get());
524 526
525 // Both sides are now connected. 527 // Both sides are now connected.
526 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout); 528 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout);
527 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN)); 529 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN));
528 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress()); 530 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress());
529 EXPECT_EQ(accepted->GetRemoteAddress(), client->GetLocalAddress()); 531 EXPECT_EQ(accepted->GetRemoteAddress(), client->GetLocalAddress());
530 532
531 // Send data to the client, and then close the connection. 533 // Send data to the client, and then close the connection.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 // for the socket's signal. 571 // for the socket's signal.
570 } 572 }
571 }; 573 };
572 574
573 void SocketTest::CloseInClosedCallbackInternal(const IPAddress& loopback) { 575 void SocketTest::CloseInClosedCallbackInternal(const IPAddress& loopback) {
574 testing::StreamSink sink; 576 testing::StreamSink sink;
575 SocketCloser closer; 577 SocketCloser closer;
576 SocketAddress accept_addr; 578 SocketAddress accept_addr;
577 579
578 // Create client. 580 // Create client.
579 scoped_ptr<AsyncSocket> client( 581 std::unique_ptr<AsyncSocket> client(
580 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 582 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
581 sink.Monitor(client.get()); 583 sink.Monitor(client.get());
582 client->SignalCloseEvent.connect(&closer, &SocketCloser::OnClose); 584 client->SignalCloseEvent.connect(&closer, &SocketCloser::OnClose);
583 585
584 // Create server and listen. 586 // Create server and listen.
585 scoped_ptr<AsyncSocket> server( 587 std::unique_ptr<AsyncSocket> server(
586 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 588 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
587 sink.Monitor(server.get()); 589 sink.Monitor(server.get());
588 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 590 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
589 EXPECT_EQ(0, server->Listen(5)); 591 EXPECT_EQ(0, server->Listen(5));
590 592
591 // Attempt connection. 593 // Attempt connection.
592 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 594 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
593 595
594 // Accept connection. 596 // Accept connection.
595 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); 597 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
596 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); 598 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
597 ASSERT_TRUE(accepted); 599 ASSERT_TRUE(accepted);
598 sink.Monitor(accepted.get()); 600 sink.Monitor(accepted.get());
599 601
600 // Both sides are now connected. 602 // Both sides are now connected.
601 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout); 603 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout);
602 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN)); 604 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN));
603 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress()); 605 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress());
604 EXPECT_EQ(accepted->GetRemoteAddress(), client->GetLocalAddress()); 606 EXPECT_EQ(accepted->GetRemoteAddress(), client->GetLocalAddress());
605 607
606 // Send data to the client, and then close the connection. 608 // Send data to the client, and then close the connection.
(...skipping 16 matching lines...) Expand all
623 void OnMessage(Message* msg) { 625 void OnMessage(Message* msg) {
624 Thread::Current()->SleepMs(500); 626 Thread::Current()->SleepMs(500);
625 } 627 }
626 }; 628 };
627 629
628 void SocketTest::SocketServerWaitInternal(const IPAddress& loopback) { 630 void SocketTest::SocketServerWaitInternal(const IPAddress& loopback) {
629 testing::StreamSink sink; 631 testing::StreamSink sink;
630 SocketAddress accept_addr; 632 SocketAddress accept_addr;
631 633
632 // Create & connect server and client sockets. 634 // Create & connect server and client sockets.
633 scoped_ptr<AsyncSocket> client( 635 std::unique_ptr<AsyncSocket> client(
634 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 636 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
635 scoped_ptr<AsyncSocket> server( 637 std::unique_ptr<AsyncSocket> server(
636 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 638 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
637 sink.Monitor(client.get()); 639 sink.Monitor(client.get());
638 sink.Monitor(server.get()); 640 sink.Monitor(server.get());
639 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 641 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
640 EXPECT_EQ(0, server->Listen(5)); 642 EXPECT_EQ(0, server->Listen(5));
641 643
642 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 644 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
643 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); 645 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
644 646
645 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); 647 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
646 ASSERT_TRUE(accepted); 648 ASSERT_TRUE(accepted);
647 sink.Monitor(accepted.get()); 649 sink.Monitor(accepted.get());
648 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState()); 650 EXPECT_EQ(AsyncSocket::CS_CONNECTED, accepted->GetState());
649 EXPECT_EQ(server->GetLocalAddress(), accepted->GetLocalAddress()); 651 EXPECT_EQ(server->GetLocalAddress(), accepted->GetLocalAddress());
650 EXPECT_EQ(client->GetLocalAddress(), accepted->GetRemoteAddress()); 652 EXPECT_EQ(client->GetLocalAddress(), accepted->GetRemoteAddress());
651 653
652 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout); 654 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout);
653 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN)); 655 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN));
654 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE)); 656 EXPECT_FALSE(sink.Check(client.get(), testing::SSE_CLOSE));
655 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress()); 657 EXPECT_EQ(client->GetRemoteAddress(), server->GetLocalAddress());
656 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress()); 658 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress());
657 659
658 // Do an i/o operation, triggering an eventual callback. 660 // Do an i/o operation, triggering an eventual callback.
659 EXPECT_FALSE(sink.Check(accepted.get(), testing::SSE_READ)); 661 EXPECT_FALSE(sink.Check(accepted.get(), testing::SSE_READ));
660 char buf[1024] = {0}; 662 char buf[1024] = {0};
661 663
662 EXPECT_EQ(1024, client->Send(buf, 1024)); 664 EXPECT_EQ(1024, client->Send(buf, 1024));
663 EXPECT_FALSE(sink.Check(accepted.get(), testing::SSE_READ)); 665 EXPECT_FALSE(sink.Check(accepted.get(), testing::SSE_READ));
664 666
665 // Shouldn't signal when blocked in a thread Send, where process_io is false. 667 // Shouldn't signal when blocked in a thread Send, where process_io is false.
666 scoped_ptr<Thread> thread(new Thread()); 668 std::unique_ptr<Thread> thread(new Thread());
667 thread->Start(); 669 thread->Start();
668 Sleeper sleeper; 670 Sleeper sleeper;
669 TypedMessageData<AsyncSocket*> data(client.get()); 671 TypedMessageData<AsyncSocket*> data(client.get());
670 thread->Send(&sleeper, 0, &data); 672 thread->Send(&sleeper, 0, &data);
671 EXPECT_FALSE(sink.Check(accepted.get(), testing::SSE_READ)); 673 EXPECT_FALSE(sink.Check(accepted.get(), testing::SSE_READ));
672 674
673 // But should signal when process_io is true. 675 // But should signal when process_io is true.
674 EXPECT_TRUE_WAIT((sink.Check(accepted.get(), testing::SSE_READ)), kTimeout); 676 EXPECT_TRUE_WAIT((sink.Check(accepted.get(), testing::SSE_READ)), kTimeout);
675 EXPECT_LT(0, accepted->Recv(buf, 1024)); 677 EXPECT_LT(0, accepted->Recv(buf, 1024));
676 } 678 }
677 679
678 void SocketTest::TcpInternal(const IPAddress& loopback, size_t data_size, 680 void SocketTest::TcpInternal(const IPAddress& loopback, size_t data_size,
679 ssize_t max_send_size) { 681 ssize_t max_send_size) {
680 testing::StreamSink sink; 682 testing::StreamSink sink;
681 SocketAddress accept_addr; 683 SocketAddress accept_addr;
682 684
683 // Create receiving client. 685 // Create receiving client.
684 scoped_ptr<AsyncSocket> receiver( 686 std::unique_ptr<AsyncSocket> receiver(
685 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 687 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
686 sink.Monitor(receiver.get()); 688 sink.Monitor(receiver.get());
687 689
688 // Create server and listen. 690 // Create server and listen.
689 scoped_ptr<AsyncSocket> server( 691 std::unique_ptr<AsyncSocket> server(
690 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 692 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
691 sink.Monitor(server.get()); 693 sink.Monitor(server.get());
692 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 694 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
693 EXPECT_EQ(0, server->Listen(5)); 695 EXPECT_EQ(0, server->Listen(5));
694 696
695 // Attempt connection. 697 // Attempt connection.
696 EXPECT_EQ(0, receiver->Connect(server->GetLocalAddress())); 698 EXPECT_EQ(0, receiver->Connect(server->GetLocalAddress()));
697 699
698 // Accept connection which will be used for sending. 700 // Accept connection which will be used for sending.
699 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); 701 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
700 scoped_ptr<AsyncSocket> sender(server->Accept(&accept_addr)); 702 std::unique_ptr<AsyncSocket> sender(server->Accept(&accept_addr));
701 ASSERT_TRUE(sender); 703 ASSERT_TRUE(sender);
702 sink.Monitor(sender.get()); 704 sink.Monitor(sender.get());
703 705
704 // Both sides are now connected. 706 // Both sides are now connected.
705 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, receiver->GetState(), kTimeout); 707 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, receiver->GetState(), kTimeout);
706 EXPECT_TRUE(sink.Check(receiver.get(), testing::SSE_OPEN)); 708 EXPECT_TRUE(sink.Check(receiver.get(), testing::SSE_OPEN));
707 EXPECT_EQ(receiver->GetRemoteAddress(), sender->GetLocalAddress()); 709 EXPECT_EQ(receiver->GetRemoteAddress(), sender->GetLocalAddress());
708 EXPECT_EQ(sender->GetRemoteAddress(), receiver->GetLocalAddress()); 710 EXPECT_EQ(sender->GetRemoteAddress(), receiver->GetLocalAddress());
709 711
710 // Create test data. 712 // Create test data.
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 EXPECT_EQ_WAIT(AsyncSocket::CS_CLOSED, receiver->GetState(), kTimeout); 804 EXPECT_EQ_WAIT(AsyncSocket::CS_CLOSED, receiver->GetState(), kTimeout);
803 EXPECT_TRUE(sink.Check(receiver.get(), testing::SSE_CLOSE)); 805 EXPECT_TRUE(sink.Check(receiver.get(), testing::SSE_CLOSE));
804 receiver->Close(); 806 receiver->Close();
805 } 807 }
806 808
807 void SocketTest::SingleFlowControlCallbackInternal(const IPAddress& loopback) { 809 void SocketTest::SingleFlowControlCallbackInternal(const IPAddress& loopback) {
808 testing::StreamSink sink; 810 testing::StreamSink sink;
809 SocketAddress accept_addr; 811 SocketAddress accept_addr;
810 812
811 // Create client. 813 // Create client.
812 scoped_ptr<AsyncSocket> client( 814 std::unique_ptr<AsyncSocket> client(
813 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 815 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
814 sink.Monitor(client.get()); 816 sink.Monitor(client.get());
815 817
816 // Create server and listen. 818 // Create server and listen.
817 scoped_ptr<AsyncSocket> server( 819 std::unique_ptr<AsyncSocket> server(
818 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM)); 820 ss_->CreateAsyncSocket(loopback.family(), SOCK_STREAM));
819 sink.Monitor(server.get()); 821 sink.Monitor(server.get());
820 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0))); 822 EXPECT_EQ(0, server->Bind(SocketAddress(loopback, 0)));
821 EXPECT_EQ(0, server->Listen(5)); 823 EXPECT_EQ(0, server->Listen(5));
822 824
823 // Attempt connection. 825 // Attempt connection.
824 EXPECT_EQ(0, client->Connect(server->GetLocalAddress())); 826 EXPECT_EQ(0, client->Connect(server->GetLocalAddress()));
825 827
826 // Accept connection. 828 // Accept connection.
827 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout); 829 EXPECT_TRUE_WAIT((sink.Check(server.get(), testing::SSE_READ)), kTimeout);
828 scoped_ptr<AsyncSocket> accepted(server->Accept(&accept_addr)); 830 std::unique_ptr<AsyncSocket> accepted(server->Accept(&accept_addr));
829 ASSERT_TRUE(accepted); 831 ASSERT_TRUE(accepted);
830 sink.Monitor(accepted.get()); 832 sink.Monitor(accepted.get());
831 833
832 // Both sides are now connected. 834 // Both sides are now connected.
833 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout); 835 EXPECT_EQ_WAIT(AsyncSocket::CS_CONNECTED, client->GetState(), kTimeout);
834 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN)); 836 EXPECT_TRUE(sink.Check(client.get(), testing::SSE_OPEN));
835 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress()); 837 EXPECT_EQ(client->GetRemoteAddress(), accepted->GetLocalAddress());
836 EXPECT_EQ(accepted->GetRemoteAddress(), client->GetLocalAddress()); 838 EXPECT_EQ(accepted->GetRemoteAddress(), client->GetLocalAddress());
837 839
838 // Expect a writable callback from the connect. 840 // Expect a writable callback from the connect.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 EXPECT_EQ(AsyncSocket::CS_CLOSED, socket->GetState()); 882 EXPECT_EQ(AsyncSocket::CS_CLOSED, socket->GetState());
881 EXPECT_EQ(0, socket->Bind(SocketAddress(loopback, 0))); 883 EXPECT_EQ(0, socket->Bind(SocketAddress(loopback, 0)));
882 SocketAddress addr1 = socket->GetLocalAddress(); 884 SocketAddress addr1 = socket->GetLocalAddress();
883 EXPECT_EQ(0, socket->Connect(addr1)); 885 EXPECT_EQ(0, socket->Connect(addr1));
884 EXPECT_EQ(AsyncSocket::CS_CONNECTED, socket->GetState()); 886 EXPECT_EQ(AsyncSocket::CS_CONNECTED, socket->GetState());
885 socket->Close(); 887 socket->Close();
886 EXPECT_EQ(AsyncSocket::CS_CLOSED, socket->GetState()); 888 EXPECT_EQ(AsyncSocket::CS_CLOSED, socket->GetState());
887 delete socket; 889 delete socket;
888 890
889 // Test send/receive behavior. 891 // Test send/receive behavior.
890 scoped_ptr<TestClient> client1( 892 std::unique_ptr<TestClient> client1(
891 new TestClient(AsyncUDPSocket::Create(ss_, addr1))); 893 new TestClient(AsyncUDPSocket::Create(ss_, addr1)));
892 scoped_ptr<TestClient> client2( 894 std::unique_ptr<TestClient> client2(
893 new TestClient(AsyncUDPSocket::Create(ss_, empty))); 895 new TestClient(AsyncUDPSocket::Create(ss_, empty)));
894 896
895 SocketAddress addr2; 897 SocketAddress addr2;
896 EXPECT_EQ(3, client2->SendTo("foo", 3, addr1)); 898 EXPECT_EQ(3, client2->SendTo("foo", 3, addr1));
897 EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &addr2)); 899 EXPECT_TRUE(client1->CheckNextPacket("foo", 3, &addr2));
898 900
899 SocketAddress addr3; 901 SocketAddress addr3;
900 EXPECT_EQ(6, client1->SendTo("bizbaz", 6, addr2)); 902 EXPECT_EQ(6, client1->SendTo("bizbaz", 6, addr2));
901 EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &addr3)); 903 EXPECT_TRUE(client2->CheckNextPacket("bizbaz", 6, &addr3));
902 EXPECT_EQ(addr3, addr1); 904 EXPECT_EQ(addr3, addr1);
(...skipping 18 matching lines...) Expand all
921 void SocketTest::UdpReadyToSend(const IPAddress& loopback) { 923 void SocketTest::UdpReadyToSend(const IPAddress& loopback) {
922 SocketAddress empty = EmptySocketAddressWithFamily(loopback.family()); 924 SocketAddress empty = EmptySocketAddressWithFamily(loopback.family());
923 // RFC 5737 - The blocks 192.0.2.0/24 (TEST-NET-1) ... are provided for use in 925 // RFC 5737 - The blocks 192.0.2.0/24 (TEST-NET-1) ... are provided for use in
924 // documentation. 926 // documentation.
925 // RFC 3849 - 2001:DB8::/32 as a documentation-only prefix. 927 // RFC 3849 - 2001:DB8::/32 as a documentation-only prefix.
926 std::string dest = (loopback.family() == AF_INET6) ? 928 std::string dest = (loopback.family() == AF_INET6) ?
927 "2001:db8::1" : "192.0.2.0"; 929 "2001:db8::1" : "192.0.2.0";
928 SocketAddress test_addr(dest, 2345); 930 SocketAddress test_addr(dest, 2345);
929 931
930 // Test send 932 // Test send
931 scoped_ptr<TestClient> client( 933 std::unique_ptr<TestClient> client(
932 new TestClient(AsyncUDPSocket::Create(ss_, empty))); 934 new TestClient(AsyncUDPSocket::Create(ss_, empty)));
933 int test_packet_size = 1200; 935 int test_packet_size = 1200;
934 rtc::scoped_ptr<char[]> test_packet(new char[test_packet_size]); 936 std::unique_ptr<char[]> test_packet(new char[test_packet_size]);
935 // Init the test packet just to avoid memcheck warning. 937 // Init the test packet just to avoid memcheck warning.
936 memset(test_packet.get(), 0, test_packet_size); 938 memset(test_packet.get(), 0, test_packet_size);
937 // Set the send buffer size to the same size as the test packet to have a 939 // Set the send buffer size to the same size as the test packet to have a
938 // better chance to get EWOULDBLOCK. 940 // better chance to get EWOULDBLOCK.
939 int send_buffer_size = test_packet_size; 941 int send_buffer_size = test_packet_size;
940 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) 942 #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
941 send_buffer_size /= 2; 943 send_buffer_size /= 2;
942 #endif 944 #endif
943 client->SetOption(rtc::Socket::OPT_SNDBUF, send_buffer_size); 945 client->SetOption(rtc::Socket::OPT_SNDBUF, send_buffer_size);
944 946
(...skipping 13 matching lines...) Expand all
958 } 960 }
959 } 961 }
960 } 962 }
961 EXPECT_EQ(expected_error, error); 963 EXPECT_EQ(expected_error, error);
962 EXPECT_FALSE(client->ready_to_send()); 964 EXPECT_FALSE(client->ready_to_send());
963 EXPECT_TRUE_WAIT(client->ready_to_send(), kTimeout); 965 EXPECT_TRUE_WAIT(client->ready_to_send(), kTimeout);
964 LOG(LS_INFO) << "Got SignalReadyToSend"; 966 LOG(LS_INFO) << "Got SignalReadyToSend";
965 } 967 }
966 968
967 void SocketTest::GetSetOptionsInternal(const IPAddress& loopback) { 969 void SocketTest::GetSetOptionsInternal(const IPAddress& loopback) {
968 rtc::scoped_ptr<AsyncSocket> socket( 970 std::unique_ptr<AsyncSocket> socket(
969 ss_->CreateAsyncSocket(loopback.family(), SOCK_DGRAM)); 971 ss_->CreateAsyncSocket(loopback.family(), SOCK_DGRAM));
970 socket->Bind(SocketAddress(loopback, 0)); 972 socket->Bind(SocketAddress(loopback, 0));
971 973
972 // Check SNDBUF/RCVBUF. 974 // Check SNDBUF/RCVBUF.
973 const int desired_size = 12345; 975 const int desired_size = 12345;
974 #if defined(WEBRTC_LINUX) 976 #if defined(WEBRTC_LINUX)
975 // Yes, really. It's in the kernel source. 977 // Yes, really. It's in the kernel source.
976 const int expected_size = desired_size * 2; 978 const int expected_size = desired_size * 2;
977 #else // !WEBRTC_LINUX 979 #else // !WEBRTC_LINUX
978 const int expected_size = desired_size; 980 const int expected_size = desired_size;
(...skipping 14 matching lines...) Expand all
993 ASSERT_EQ(expected_size, send_size); 995 ASSERT_EQ(expected_size, send_size);
994 996
995 // Check that we can't set NODELAY on a UDP socket. 997 // Check that we can't set NODELAY on a UDP socket.
996 int current_nd, desired_nd = 1; 998 int current_nd, desired_nd = 1;
997 ASSERT_EQ(-1, socket->GetOption(Socket::OPT_NODELAY, &current_nd)); 999 ASSERT_EQ(-1, socket->GetOption(Socket::OPT_NODELAY, &current_nd));
998 ASSERT_EQ(-1, socket->SetOption(Socket::OPT_NODELAY, desired_nd)); 1000 ASSERT_EQ(-1, socket->SetOption(Socket::OPT_NODELAY, desired_nd));
999 1001
1000 // Skip the esimate MTU test for IPv6 for now. 1002 // Skip the esimate MTU test for IPv6 for now.
1001 if (loopback.family() != AF_INET6) { 1003 if (loopback.family() != AF_INET6) {
1002 // Try estimating MTU. 1004 // Try estimating MTU.
1003 rtc::scoped_ptr<AsyncSocket> 1005 std::unique_ptr<AsyncSocket> mtu_socket(
1004 mtu_socket( 1006 ss_->CreateAsyncSocket(loopback.family(), SOCK_DGRAM));
1005 ss_->CreateAsyncSocket(loopback.family(), SOCK_DGRAM));
1006 mtu_socket->Bind(SocketAddress(loopback, 0)); 1007 mtu_socket->Bind(SocketAddress(loopback, 0));
1007 uint16_t mtu; 1008 uint16_t mtu;
1008 // should fail until we connect 1009 // should fail until we connect
1009 ASSERT_EQ(-1, mtu_socket->EstimateMTU(&mtu)); 1010 ASSERT_EQ(-1, mtu_socket->EstimateMTU(&mtu));
1010 mtu_socket->Connect(SocketAddress(loopback, 0)); 1011 mtu_socket->Connect(SocketAddress(loopback, 0));
1011 #if defined(WEBRTC_WIN) 1012 #if defined(WEBRTC_WIN)
1012 // now it should succeed 1013 // now it should succeed
1013 ASSERT_NE(-1, mtu_socket->EstimateMTU(&mtu)); 1014 ASSERT_NE(-1, mtu_socket->EstimateMTU(&mtu));
1014 ASSERT_GE(mtu, 1492); // should be at least the 1492 "plateau" on localhost 1015 ASSERT_GE(mtu, 1492); // should be at least the 1492 "plateau" on localhost
1015 #elif defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) 1016 #elif defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
1016 // except on WEBRTC_MAC && !WEBRTC_IOS, where it's not yet implemented 1017 // except on WEBRTC_MAC && !WEBRTC_IOS, where it's not yet implemented
1017 ASSERT_EQ(-1, mtu_socket->EstimateMTU(&mtu)); 1018 ASSERT_EQ(-1, mtu_socket->EstimateMTU(&mtu));
1018 #else 1019 #else
1019 // and the behavior seems unpredictable on Linux, 1020 // and the behavior seems unpredictable on Linux,
1020 // failing on the build machine 1021 // failing on the build machine
1021 // but succeeding on my Ubiquity instance. 1022 // but succeeding on my Ubiquity instance.
1022 #endif 1023 #endif
1023 } 1024 }
1024 } 1025 }
1025 1026
1026 } // namespace rtc 1027 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/signalthread_unittest.cc ('k') | webrtc/base/ssladapter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698