OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/socket_test_util.h" | 5 #include "net/socket/socket_test_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
13 #include "base/location.h" | 14 #include "base/location.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
17 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
703 const RandIntCallback& rand_int_cb, | 704 const RandIntCallback& rand_int_cb, |
704 NetLog* net_log, | 705 NetLog* net_log, |
705 const NetLog::Source& source) { | 706 const NetLog::Source& source) { |
706 SocketDataProvider* data_provider = mock_data_.GetNext(); | 707 SocketDataProvider* data_provider = mock_data_.GetNext(); |
707 scoped_ptr<MockUDPClientSocket> socket( | 708 scoped_ptr<MockUDPClientSocket> socket( |
708 new MockUDPClientSocket(data_provider, net_log)); | 709 new MockUDPClientSocket(data_provider, net_log)); |
709 if (bind_type == DatagramSocket::RANDOM_BIND) | 710 if (bind_type == DatagramSocket::RANDOM_BIND) |
710 socket->set_source_port( | 711 socket->set_source_port( |
711 static_cast<uint16_t>(rand_int_cb.Run(1025, 65535))); | 712 static_cast<uint16_t>(rand_int_cb.Run(1025, 65535))); |
712 udp_client_socket_ports_.push_back(socket->source_port()); | 713 udp_client_socket_ports_.push_back(socket->source_port()); |
713 return socket.Pass(); | 714 return std::move(socket); |
714 } | 715 } |
715 | 716 |
716 scoped_ptr<StreamSocket> MockClientSocketFactory::CreateTransportClientSocket( | 717 scoped_ptr<StreamSocket> MockClientSocketFactory::CreateTransportClientSocket( |
717 const AddressList& addresses, | 718 const AddressList& addresses, |
718 NetLog* net_log, | 719 NetLog* net_log, |
719 const NetLog::Source& source) { | 720 const NetLog::Source& source) { |
720 SocketDataProvider* data_provider = mock_data_.GetNext(); | 721 SocketDataProvider* data_provider = mock_data_.GetNext(); |
721 scoped_ptr<MockTCPClientSocket> socket( | 722 scoped_ptr<MockTCPClientSocket> socket( |
722 new MockTCPClientSocket(addresses, net_log, data_provider)); | 723 new MockTCPClientSocket(addresses, net_log, data_provider)); |
723 return socket.Pass(); | 724 return std::move(socket); |
724 } | 725 } |
725 | 726 |
726 scoped_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( | 727 scoped_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( |
727 scoped_ptr<ClientSocketHandle> transport_socket, | 728 scoped_ptr<ClientSocketHandle> transport_socket, |
728 const HostPortPair& host_and_port, | 729 const HostPortPair& host_and_port, |
729 const SSLConfig& ssl_config, | 730 const SSLConfig& ssl_config, |
730 const SSLClientSocketContext& context) { | 731 const SSLClientSocketContext& context) { |
731 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); | 732 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); |
732 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { | 733 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { |
733 EXPECT_EQ(next_ssl_data->next_protos_expected_in_ssl_config.size(), | 734 EXPECT_EQ(next_ssl_data->next_protos_expected_in_ssl_config.size(), |
734 ssl_config.alpn_protos.size()); | 735 ssl_config.alpn_protos.size()); |
735 EXPECT_TRUE( | 736 EXPECT_TRUE( |
736 std::equal(next_ssl_data->next_protos_expected_in_ssl_config.begin(), | 737 std::equal(next_ssl_data->next_protos_expected_in_ssl_config.begin(), |
737 next_ssl_data->next_protos_expected_in_ssl_config.end(), | 738 next_ssl_data->next_protos_expected_in_ssl_config.end(), |
738 ssl_config.alpn_protos.begin())); | 739 ssl_config.alpn_protos.begin())); |
739 } | 740 } |
740 return scoped_ptr<SSLClientSocket>(new MockSSLClientSocket( | 741 return scoped_ptr<SSLClientSocket>(new MockSSLClientSocket( |
741 transport_socket.Pass(), host_and_port, ssl_config, next_ssl_data)); | 742 std::move(transport_socket), host_and_port, ssl_config, next_ssl_data)); |
742 } | 743 } |
743 | 744 |
744 void MockClientSocketFactory::ClearSSLSessionCache() { | 745 void MockClientSocketFactory::ClearSSLSessionCache() { |
745 } | 746 } |
746 | 747 |
747 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; | 748 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; |
748 | 749 |
749 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) | 750 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) |
750 : connected_(false), | 751 : connected_(false), |
751 net_log_(net_log), | 752 net_log_(net_log), |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1123 } | 1124 } |
1124 | 1125 |
1125 MockSSLClientSocket::MockSSLClientSocket( | 1126 MockSSLClientSocket::MockSSLClientSocket( |
1126 scoped_ptr<ClientSocketHandle> transport_socket, | 1127 scoped_ptr<ClientSocketHandle> transport_socket, |
1127 const HostPortPair& host_port_pair, | 1128 const HostPortPair& host_port_pair, |
1128 const SSLConfig& ssl_config, | 1129 const SSLConfig& ssl_config, |
1129 SSLSocketDataProvider* data) | 1130 SSLSocketDataProvider* data) |
1130 : MockClientSocket( | 1131 : MockClientSocket( |
1131 // Have to use the right BoundNetLog for LoadTimingInfo regression | 1132 // Have to use the right BoundNetLog for LoadTimingInfo regression |
1132 // tests. | 1133 // tests. |
1133 transport_socket->socket()->NetLog()), | 1134 transport_socket->socket() |
1134 transport_(transport_socket.Pass()), | 1135 ->NetLog()), |
| 1136 transport_(std::move(transport_socket)), |
1135 data_(data) { | 1137 data_(data) { |
1136 DCHECK(data_); | 1138 DCHECK(data_); |
1137 peer_addr_ = data->connect.peer_addr; | 1139 peer_addr_ = data->connect.peer_addr; |
1138 } | 1140 } |
1139 | 1141 |
1140 MockSSLClientSocket::~MockSSLClientSocket() { | 1142 MockSSLClientSocket::~MockSSLClientSocket() { |
1141 Disconnect(); | 1143 Disconnect(); |
1142 } | 1144 } |
1143 | 1145 |
1144 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, | 1146 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1508 bool released_one; | 1510 bool released_one; |
1509 do { | 1511 do { |
1510 released_one = ReleaseOneConnection(keep_alive); | 1512 released_one = ReleaseOneConnection(keep_alive); |
1511 } while (released_one); | 1513 } while (released_one); |
1512 } | 1514 } |
1513 | 1515 |
1514 MockTransportClientSocketPool::MockConnectJob::MockConnectJob( | 1516 MockTransportClientSocketPool::MockConnectJob::MockConnectJob( |
1515 scoped_ptr<StreamSocket> socket, | 1517 scoped_ptr<StreamSocket> socket, |
1516 ClientSocketHandle* handle, | 1518 ClientSocketHandle* handle, |
1517 const CompletionCallback& callback) | 1519 const CompletionCallback& callback) |
1518 : socket_(socket.Pass()), | 1520 : socket_(std::move(socket)), handle_(handle), user_callback_(callback) {} |
1519 handle_(handle), | |
1520 user_callback_(callback) { | |
1521 } | |
1522 | 1521 |
1523 MockTransportClientSocketPool::MockConnectJob::~MockConnectJob() {} | 1522 MockTransportClientSocketPool::MockConnectJob::~MockConnectJob() {} |
1524 | 1523 |
1525 int MockTransportClientSocketPool::MockConnectJob::Connect() { | 1524 int MockTransportClientSocketPool::MockConnectJob::Connect() { |
1526 int rv = socket_->Connect(base::Bind(&MockConnectJob::OnConnect, | 1525 int rv = socket_->Connect(base::Bind(&MockConnectJob::OnConnect, |
1527 base::Unretained(this))); | 1526 base::Unretained(this))); |
1528 if (rv != ERR_IO_PENDING) { | 1527 if (rv != ERR_IO_PENDING) { |
1529 user_callback_.Reset(); | 1528 user_callback_.Reset(); |
1530 OnConnect(rv); | 1529 OnConnect(rv); |
1531 } | 1530 } |
1532 return rv; | 1531 return rv; |
1533 } | 1532 } |
1534 | 1533 |
1535 bool MockTransportClientSocketPool::MockConnectJob::CancelHandle( | 1534 bool MockTransportClientSocketPool::MockConnectJob::CancelHandle( |
1536 const ClientSocketHandle* handle) { | 1535 const ClientSocketHandle* handle) { |
1537 if (handle != handle_) | 1536 if (handle != handle_) |
1538 return false; | 1537 return false; |
1539 socket_.reset(); | 1538 socket_.reset(); |
1540 handle_ = NULL; | 1539 handle_ = NULL; |
1541 user_callback_.Reset(); | 1540 user_callback_.Reset(); |
1542 return true; | 1541 return true; |
1543 } | 1542 } |
1544 | 1543 |
1545 void MockTransportClientSocketPool::MockConnectJob::OnConnect(int rv) { | 1544 void MockTransportClientSocketPool::MockConnectJob::OnConnect(int rv) { |
1546 if (!socket_.get()) | 1545 if (!socket_.get()) |
1547 return; | 1546 return; |
1548 if (rv == OK) { | 1547 if (rv == OK) { |
1549 handle_->SetSocket(socket_.Pass()); | 1548 handle_->SetSocket(std::move(socket_)); |
1550 | 1549 |
1551 // Needed for socket pool tests that layer other sockets on top of mock | 1550 // Needed for socket pool tests that layer other sockets on top of mock |
1552 // sockets. | 1551 // sockets. |
1553 LoadTimingInfo::ConnectTiming connect_timing; | 1552 LoadTimingInfo::ConnectTiming connect_timing; |
1554 base::TimeTicks now = base::TimeTicks::Now(); | 1553 base::TimeTicks now = base::TimeTicks::Now(); |
1555 connect_timing.dns_start = now; | 1554 connect_timing.dns_start = now; |
1556 connect_timing.dns_end = now; | 1555 connect_timing.dns_end = now; |
1557 connect_timing.connect_start = now; | 1556 connect_timing.connect_start = now; |
1558 connect_timing.connect_end = now; | 1557 connect_timing.connect_end = now; |
1559 handle_->set_connect_timing(connect_timing); | 1558 handle_->set_connect_timing(connect_timing); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1593 MockTransportClientSocketPool::~MockTransportClientSocketPool() {} | 1592 MockTransportClientSocketPool::~MockTransportClientSocketPool() {} |
1594 | 1593 |
1595 int MockTransportClientSocketPool::RequestSocket( | 1594 int MockTransportClientSocketPool::RequestSocket( |
1596 const std::string& group_name, const void* socket_params, | 1595 const std::string& group_name, const void* socket_params, |
1597 RequestPriority priority, ClientSocketHandle* handle, | 1596 RequestPriority priority, ClientSocketHandle* handle, |
1598 const CompletionCallback& callback, const BoundNetLog& net_log) { | 1597 const CompletionCallback& callback, const BoundNetLog& net_log) { |
1599 last_request_priority_ = priority; | 1598 last_request_priority_ = priority; |
1600 scoped_ptr<StreamSocket> socket = | 1599 scoped_ptr<StreamSocket> socket = |
1601 client_socket_factory_->CreateTransportClientSocket( | 1600 client_socket_factory_->CreateTransportClientSocket( |
1602 AddressList(), net_log.net_log(), NetLog::Source()); | 1601 AddressList(), net_log.net_log(), NetLog::Source()); |
1603 MockConnectJob* job = new MockConnectJob(socket.Pass(), handle, callback); | 1602 MockConnectJob* job = new MockConnectJob(std::move(socket), handle, callback); |
1604 job_list_.push_back(make_scoped_ptr(job)); | 1603 job_list_.push_back(make_scoped_ptr(job)); |
1605 handle->set_pool_id(1); | 1604 handle->set_pool_id(1); |
1606 return job->Connect(); | 1605 return job->Connect(); |
1607 } | 1606 } |
1608 | 1607 |
1609 void MockTransportClientSocketPool::CancelRequest(const std::string& group_name, | 1608 void MockTransportClientSocketPool::CancelRequest(const std::string& group_name, |
1610 ClientSocketHandle* handle) { | 1609 ClientSocketHandle* handle) { |
1611 for (scoped_ptr<MockConnectJob>& it : job_list_) { | 1610 for (scoped_ptr<MockConnectJob>& it : job_list_) { |
1612 if (it->CancelHandle(handle)) { | 1611 if (it->CancelHandle(handle)) { |
1613 cancel_count_++; | 1612 cancel_count_++; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1648 | 1647 |
1649 void MockSOCKSClientSocketPool::CancelRequest( | 1648 void MockSOCKSClientSocketPool::CancelRequest( |
1650 const std::string& group_name, | 1649 const std::string& group_name, |
1651 ClientSocketHandle* handle) { | 1650 ClientSocketHandle* handle) { |
1652 return transport_pool_->CancelRequest(group_name, handle); | 1651 return transport_pool_->CancelRequest(group_name, handle); |
1653 } | 1652 } |
1654 | 1653 |
1655 void MockSOCKSClientSocketPool::ReleaseSocket(const std::string& group_name, | 1654 void MockSOCKSClientSocketPool::ReleaseSocket(const std::string& group_name, |
1656 scoped_ptr<StreamSocket> socket, | 1655 scoped_ptr<StreamSocket> socket, |
1657 int id) { | 1656 int id) { |
1658 return transport_pool_->ReleaseSocket(group_name, socket.Pass(), id); | 1657 return transport_pool_->ReleaseSocket(group_name, std::move(socket), id); |
1659 } | 1658 } |
1660 | 1659 |
1661 ScopedWebSocketEndpointZeroUnlockDelay:: | 1660 ScopedWebSocketEndpointZeroUnlockDelay:: |
1662 ScopedWebSocketEndpointZeroUnlockDelay() { | 1661 ScopedWebSocketEndpointZeroUnlockDelay() { |
1663 old_delay_ = | 1662 old_delay_ = |
1664 WebSocketEndpointLockManager::GetInstance()->SetUnlockDelayForTesting( | 1663 WebSocketEndpointLockManager::GetInstance()->SetUnlockDelayForTesting( |
1665 base::TimeDelta()); | 1664 base::TimeDelta()); |
1666 } | 1665 } |
1667 | 1666 |
1668 ScopedWebSocketEndpointZeroUnlockDelay:: | 1667 ScopedWebSocketEndpointZeroUnlockDelay:: |
(...skipping 26 matching lines...) Expand all Loading... |
1695 } | 1694 } |
1696 | 1695 |
1697 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { | 1696 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { |
1698 int64_t total = 0; | 1697 int64_t total = 0; |
1699 for (const MockWrite* write = writes; write != writes + writes_size; ++write) | 1698 for (const MockWrite* write = writes; write != writes + writes_size; ++write) |
1700 total += write->data_len; | 1699 total += write->data_len; |
1701 return total; | 1700 return total; |
1702 } | 1701 } |
1703 | 1702 |
1704 } // namespace net | 1703 } // namespace net |
OLD | NEW |