OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/transport_client_socket_pool_test_util.h" | 5 #include "net/socket/transport_client_socket_pool_test_util.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 weak_factory_(this) {} | 183 weak_factory_(this) {} |
184 | 184 |
185 // Call this method to get a closure which will trigger the connect callback | 185 // Call this method to get a closure which will trigger the connect callback |
186 // when called. The closure can be called even after the socket is deleted; it | 186 // when called. The closure can be called even after the socket is deleted; it |
187 // will safely do nothing. | 187 // will safely do nothing. |
188 base::Closure GetConnectCallback() { | 188 base::Closure GetConnectCallback() { |
189 return base::Bind(&MockTriggerableClientSocket::DoCallback, | 189 return base::Bind(&MockTriggerableClientSocket::DoCallback, |
190 weak_factory_.GetWeakPtr()); | 190 weak_factory_.GetWeakPtr()); |
191 } | 191 } |
192 | 192 |
193 static scoped_ptr<StreamSocket> MakeMockPendingClientSocket( | 193 static std::unique_ptr<StreamSocket> MakeMockPendingClientSocket( |
194 const AddressList& addrlist, | 194 const AddressList& addrlist, |
195 bool should_connect, | 195 bool should_connect, |
196 net::NetLog* net_log) { | 196 net::NetLog* net_log) { |
197 scoped_ptr<MockTriggerableClientSocket> socket( | 197 std::unique_ptr<MockTriggerableClientSocket> socket( |
198 new MockTriggerableClientSocket(addrlist, should_connect, net_log)); | 198 new MockTriggerableClientSocket(addrlist, should_connect, net_log)); |
199 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 199 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
200 socket->GetConnectCallback()); | 200 socket->GetConnectCallback()); |
201 return std::move(socket); | 201 return std::move(socket); |
202 } | 202 } |
203 | 203 |
204 static scoped_ptr<StreamSocket> MakeMockDelayedClientSocket( | 204 static std::unique_ptr<StreamSocket> MakeMockDelayedClientSocket( |
205 const AddressList& addrlist, | 205 const AddressList& addrlist, |
206 bool should_connect, | 206 bool should_connect, |
207 const base::TimeDelta& delay, | 207 const base::TimeDelta& delay, |
208 net::NetLog* net_log) { | 208 net::NetLog* net_log) { |
209 scoped_ptr<MockTriggerableClientSocket> socket( | 209 std::unique_ptr<MockTriggerableClientSocket> socket( |
210 new MockTriggerableClientSocket(addrlist, should_connect, net_log)); | 210 new MockTriggerableClientSocket(addrlist, should_connect, net_log)); |
211 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 211 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
212 FROM_HERE, socket->GetConnectCallback(), delay); | 212 FROM_HERE, socket->GetConnectCallback(), delay); |
213 return std::move(socket); | 213 return std::move(socket); |
214 } | 214 } |
215 | 215 |
216 static scoped_ptr<StreamSocket> MakeMockStalledClientSocket( | 216 static std::unique_ptr<StreamSocket> MakeMockStalledClientSocket( |
217 const AddressList& addrlist, | 217 const AddressList& addrlist, |
218 net::NetLog* net_log, | 218 net::NetLog* net_log, |
219 bool failing) { | 219 bool failing) { |
220 scoped_ptr<MockTriggerableClientSocket> socket( | 220 std::unique_ptr<MockTriggerableClientSocket> socket( |
221 new MockTriggerableClientSocket(addrlist, true, net_log)); | 221 new MockTriggerableClientSocket(addrlist, true, net_log)); |
222 if (failing) { | 222 if (failing) { |
223 DCHECK_LE(1u, addrlist.size()); | 223 DCHECK_LE(1u, addrlist.size()); |
224 ConnectionAttempts attempts; | 224 ConnectionAttempts attempts; |
225 attempts.push_back(ConnectionAttempt(addrlist[0], ERR_CONNECTION_FAILED)); | 225 attempts.push_back(ConnectionAttempt(addrlist[0], ERR_CONNECTION_FAILED)); |
226 socket->AddConnectionAttempts(attempts); | 226 socket->AddConnectionAttempts(attempts); |
227 } | 227 } |
228 return std::move(socket); | 228 return std::move(socket); |
229 } | 229 } |
230 | 230 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 allocation_count_(0), | 352 allocation_count_(0), |
353 client_socket_type_(MOCK_CLIENT_SOCKET), | 353 client_socket_type_(MOCK_CLIENT_SOCKET), |
354 client_socket_types_(NULL), | 354 client_socket_types_(NULL), |
355 client_socket_index_(0), | 355 client_socket_index_(0), |
356 client_socket_index_max_(0), | 356 client_socket_index_max_(0), |
357 delay_(base::TimeDelta::FromMilliseconds( | 357 delay_(base::TimeDelta::FromMilliseconds( |
358 ClientSocketPool::kMaxConnectRetryIntervalMs)) {} | 358 ClientSocketPool::kMaxConnectRetryIntervalMs)) {} |
359 | 359 |
360 MockTransportClientSocketFactory::~MockTransportClientSocketFactory() {} | 360 MockTransportClientSocketFactory::~MockTransportClientSocketFactory() {} |
361 | 361 |
362 scoped_ptr<DatagramClientSocket> | 362 std::unique_ptr<DatagramClientSocket> |
363 MockTransportClientSocketFactory::CreateDatagramClientSocket( | 363 MockTransportClientSocketFactory::CreateDatagramClientSocket( |
364 DatagramSocket::BindType bind_type, | 364 DatagramSocket::BindType bind_type, |
365 const RandIntCallback& rand_int_cb, | 365 const RandIntCallback& rand_int_cb, |
366 NetLog* net_log, | 366 NetLog* net_log, |
367 const NetLog::Source& source) { | 367 const NetLog::Source& source) { |
368 NOTREACHED(); | 368 NOTREACHED(); |
369 return scoped_ptr<DatagramClientSocket>(); | 369 return std::unique_ptr<DatagramClientSocket>(); |
370 } | 370 } |
371 | 371 |
372 scoped_ptr<StreamSocket> | 372 std::unique_ptr<StreamSocket> |
373 MockTransportClientSocketFactory::CreateTransportClientSocket( | 373 MockTransportClientSocketFactory::CreateTransportClientSocket( |
374 const AddressList& addresses, | 374 const AddressList& addresses, |
375 scoped_ptr<SocketPerformanceWatcher> /* socket_performance_watcher */, | 375 std::unique_ptr<SocketPerformanceWatcher> /* socket_performance_watcher */, |
376 NetLog* /* net_log */, | 376 NetLog* /* net_log */, |
377 const NetLog::Source& /* source */) { | 377 const NetLog::Source& /* source */) { |
378 allocation_count_++; | 378 allocation_count_++; |
379 | 379 |
380 ClientSocketType type = client_socket_type_; | 380 ClientSocketType type = client_socket_type_; |
381 if (client_socket_types_ && client_socket_index_ < client_socket_index_max_) { | 381 if (client_socket_types_ && client_socket_index_ < client_socket_index_max_) { |
382 type = client_socket_types_[client_socket_index_++]; | 382 type = client_socket_types_[client_socket_index_++]; |
383 } | 383 } |
384 | 384 |
385 switch (type) { | 385 switch (type) { |
386 case MOCK_CLIENT_SOCKET: | 386 case MOCK_CLIENT_SOCKET: |
387 return scoped_ptr<StreamSocket>( | 387 return std::unique_ptr<StreamSocket>( |
388 new MockConnectClientSocket(addresses, net_log_)); | 388 new MockConnectClientSocket(addresses, net_log_)); |
389 case MOCK_FAILING_CLIENT_SOCKET: | 389 case MOCK_FAILING_CLIENT_SOCKET: |
390 return scoped_ptr<StreamSocket>( | 390 return std::unique_ptr<StreamSocket>( |
391 new MockFailingClientSocket(addresses, net_log_)); | 391 new MockFailingClientSocket(addresses, net_log_)); |
392 case MOCK_PENDING_CLIENT_SOCKET: | 392 case MOCK_PENDING_CLIENT_SOCKET: |
393 return MockTriggerableClientSocket::MakeMockPendingClientSocket( | 393 return MockTriggerableClientSocket::MakeMockPendingClientSocket( |
394 addresses, true, net_log_); | 394 addresses, true, net_log_); |
395 case MOCK_PENDING_FAILING_CLIENT_SOCKET: | 395 case MOCK_PENDING_FAILING_CLIENT_SOCKET: |
396 return MockTriggerableClientSocket::MakeMockPendingClientSocket( | 396 return MockTriggerableClientSocket::MakeMockPendingClientSocket( |
397 addresses, false, net_log_); | 397 addresses, false, net_log_); |
398 case MOCK_DELAYED_CLIENT_SOCKET: | 398 case MOCK_DELAYED_CLIENT_SOCKET: |
399 return MockTriggerableClientSocket::MakeMockDelayedClientSocket( | 399 return MockTriggerableClientSocket::MakeMockDelayedClientSocket( |
400 addresses, true, delay_, net_log_); | 400 addresses, true, delay_, net_log_); |
401 case MOCK_DELAYED_FAILING_CLIENT_SOCKET: | 401 case MOCK_DELAYED_FAILING_CLIENT_SOCKET: |
402 return MockTriggerableClientSocket::MakeMockDelayedClientSocket( | 402 return MockTriggerableClientSocket::MakeMockDelayedClientSocket( |
403 addresses, false, delay_, net_log_); | 403 addresses, false, delay_, net_log_); |
404 case MOCK_STALLED_CLIENT_SOCKET: | 404 case MOCK_STALLED_CLIENT_SOCKET: |
405 return MockTriggerableClientSocket::MakeMockStalledClientSocket( | 405 return MockTriggerableClientSocket::MakeMockStalledClientSocket( |
406 addresses, net_log_, false); | 406 addresses, net_log_, false); |
407 case MOCK_STALLED_FAILING_CLIENT_SOCKET: | 407 case MOCK_STALLED_FAILING_CLIENT_SOCKET: |
408 return MockTriggerableClientSocket::MakeMockStalledClientSocket( | 408 return MockTriggerableClientSocket::MakeMockStalledClientSocket( |
409 addresses, net_log_, true); | 409 addresses, net_log_, true); |
410 case MOCK_TRIGGERABLE_CLIENT_SOCKET: { | 410 case MOCK_TRIGGERABLE_CLIENT_SOCKET: { |
411 scoped_ptr<MockTriggerableClientSocket> rv( | 411 std::unique_ptr<MockTriggerableClientSocket> rv( |
412 new MockTriggerableClientSocket(addresses, true, net_log_)); | 412 new MockTriggerableClientSocket(addresses, true, net_log_)); |
413 triggerable_sockets_.push(rv->GetConnectCallback()); | 413 triggerable_sockets_.push(rv->GetConnectCallback()); |
414 // run_loop_quit_closure_ behaves like a condition variable. It will | 414 // run_loop_quit_closure_ behaves like a condition variable. It will |
415 // wake up WaitForTriggerableSocketCreation() if it is sleeping. We | 415 // wake up WaitForTriggerableSocketCreation() if it is sleeping. We |
416 // don't need to worry about atomicity because this code is | 416 // don't need to worry about atomicity because this code is |
417 // single-threaded. | 417 // single-threaded. |
418 if (!run_loop_quit_closure_.is_null()) | 418 if (!run_loop_quit_closure_.is_null()) |
419 run_loop_quit_closure_.Run(); | 419 run_loop_quit_closure_.Run(); |
420 return std::move(rv); | 420 return std::move(rv); |
421 } | 421 } |
422 default: | 422 default: |
423 NOTREACHED(); | 423 NOTREACHED(); |
424 return scoped_ptr<StreamSocket>( | 424 return std::unique_ptr<StreamSocket>( |
425 new MockConnectClientSocket(addresses, net_log_)); | 425 new MockConnectClientSocket(addresses, net_log_)); |
426 } | 426 } |
427 } | 427 } |
428 | 428 |
429 scoped_ptr<SSLClientSocket> | 429 std::unique_ptr<SSLClientSocket> |
430 MockTransportClientSocketFactory::CreateSSLClientSocket( | 430 MockTransportClientSocketFactory::CreateSSLClientSocket( |
431 scoped_ptr<ClientSocketHandle> transport_socket, | 431 std::unique_ptr<ClientSocketHandle> transport_socket, |
432 const HostPortPair& host_and_port, | 432 const HostPortPair& host_and_port, |
433 const SSLConfig& ssl_config, | 433 const SSLConfig& ssl_config, |
434 const SSLClientSocketContext& context) { | 434 const SSLClientSocketContext& context) { |
435 NOTIMPLEMENTED(); | 435 NOTIMPLEMENTED(); |
436 return scoped_ptr<SSLClientSocket>(); | 436 return std::unique_ptr<SSLClientSocket>(); |
437 } | 437 } |
438 | 438 |
439 void MockTransportClientSocketFactory::ClearSSLSessionCache() { | 439 void MockTransportClientSocketFactory::ClearSSLSessionCache() { |
440 NOTIMPLEMENTED(); | 440 NOTIMPLEMENTED(); |
441 } | 441 } |
442 | 442 |
443 void MockTransportClientSocketFactory::set_client_socket_types( | 443 void MockTransportClientSocketFactory::set_client_socket_types( |
444 ClientSocketType* type_list, | 444 ClientSocketType* type_list, |
445 int num_types) { | 445 int num_types) { |
446 DCHECK_GT(num_types, 0); | 446 DCHECK_GT(num_types, 0); |
447 client_socket_types_ = type_list; | 447 client_socket_types_ = type_list; |
448 client_socket_index_ = 0; | 448 client_socket_index_ = 0; |
449 client_socket_index_max_ = num_types; | 449 client_socket_index_max_ = num_types; |
450 } | 450 } |
451 | 451 |
452 base::Closure | 452 base::Closure |
453 MockTransportClientSocketFactory::WaitForTriggerableSocketCreation() { | 453 MockTransportClientSocketFactory::WaitForTriggerableSocketCreation() { |
454 while (triggerable_sockets_.empty()) { | 454 while (triggerable_sockets_.empty()) { |
455 base::RunLoop run_loop; | 455 base::RunLoop run_loop; |
456 run_loop_quit_closure_ = run_loop.QuitClosure(); | 456 run_loop_quit_closure_ = run_loop.QuitClosure(); |
457 run_loop.Run(); | 457 run_loop.Run(); |
458 run_loop_quit_closure_.Reset(); | 458 run_loop_quit_closure_.Reset(); |
459 } | 459 } |
460 base::Closure trigger = triggerable_sockets_.front(); | 460 base::Closure trigger = triggerable_sockets_.front(); |
461 triggerable_sockets_.pop(); | 461 triggerable_sockets_.pop(); |
462 return trigger; | 462 return trigger; |
463 } | 463 } |
464 | 464 |
465 } // namespace net | 465 } // namespace net |
OLD | NEW |