| 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 <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 18 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 20 #include "net/base/address_family.h" | 21 #include "net/base/address_family.h" |
| 21 #include "net/base/address_list.h" | 22 #include "net/base/address_list.h" |
| 22 #include "net/base/auth.h" | 23 #include "net/base/auth.h" |
| 23 #include "net/base/ip_address.h" | 24 #include "net/base/ip_address.h" |
| 24 #include "net/base/load_timing_info.h" | 25 #include "net/base/load_timing_info.h" |
| 25 #include "net/http/http_network_session.h" | 26 #include "net/http/http_network_session.h" |
| (...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 void MockClientSocketFactory::AddSSLSocketDataProvider( | 704 void MockClientSocketFactory::AddSSLSocketDataProvider( |
| 704 SSLSocketDataProvider* data) { | 705 SSLSocketDataProvider* data) { |
| 705 mock_ssl_data_.Add(data); | 706 mock_ssl_data_.Add(data); |
| 706 } | 707 } |
| 707 | 708 |
| 708 void MockClientSocketFactory::ResetNextMockIndexes() { | 709 void MockClientSocketFactory::ResetNextMockIndexes() { |
| 709 mock_data_.ResetNextIndex(); | 710 mock_data_.ResetNextIndex(); |
| 710 mock_ssl_data_.ResetNextIndex(); | 711 mock_ssl_data_.ResetNextIndex(); |
| 711 } | 712 } |
| 712 | 713 |
| 713 scoped_ptr<DatagramClientSocket> | 714 std::unique_ptr<DatagramClientSocket> |
| 714 MockClientSocketFactory::CreateDatagramClientSocket( | 715 MockClientSocketFactory::CreateDatagramClientSocket( |
| 715 DatagramSocket::BindType bind_type, | 716 DatagramSocket::BindType bind_type, |
| 716 const RandIntCallback& rand_int_cb, | 717 const RandIntCallback& rand_int_cb, |
| 717 NetLog* net_log, | 718 NetLog* net_log, |
| 718 const NetLog::Source& source) { | 719 const NetLog::Source& source) { |
| 719 SocketDataProvider* data_provider = mock_data_.GetNext(); | 720 SocketDataProvider* data_provider = mock_data_.GetNext(); |
| 720 scoped_ptr<MockUDPClientSocket> socket( | 721 std::unique_ptr<MockUDPClientSocket> socket( |
| 721 new MockUDPClientSocket(data_provider, net_log)); | 722 new MockUDPClientSocket(data_provider, net_log)); |
| 722 if (bind_type == DatagramSocket::RANDOM_BIND) | 723 if (bind_type == DatagramSocket::RANDOM_BIND) |
| 723 socket->set_source_port( | 724 socket->set_source_port( |
| 724 static_cast<uint16_t>(rand_int_cb.Run(1025, 65535))); | 725 static_cast<uint16_t>(rand_int_cb.Run(1025, 65535))); |
| 725 udp_client_socket_ports_.push_back(socket->source_port()); | 726 udp_client_socket_ports_.push_back(socket->source_port()); |
| 726 return std::move(socket); | 727 return std::move(socket); |
| 727 } | 728 } |
| 728 | 729 |
| 729 scoped_ptr<StreamSocket> MockClientSocketFactory::CreateTransportClientSocket( | 730 std::unique_ptr<StreamSocket> |
| 731 MockClientSocketFactory::CreateTransportClientSocket( |
| 730 const AddressList& addresses, | 732 const AddressList& addresses, |
| 731 scoped_ptr<SocketPerformanceWatcher> socket_performance_watcher, | 733 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, |
| 732 NetLog* net_log, | 734 NetLog* net_log, |
| 733 const NetLog::Source& source) { | 735 const NetLog::Source& source) { |
| 734 SocketDataProvider* data_provider = mock_data_.GetNext(); | 736 SocketDataProvider* data_provider = mock_data_.GetNext(); |
| 735 scoped_ptr<MockTCPClientSocket> socket( | 737 std::unique_ptr<MockTCPClientSocket> socket( |
| 736 new MockTCPClientSocket(addresses, net_log, data_provider)); | 738 new MockTCPClientSocket(addresses, net_log, data_provider)); |
| 737 return std::move(socket); | 739 return std::move(socket); |
| 738 } | 740 } |
| 739 | 741 |
| 740 scoped_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( | 742 std::unique_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( |
| 741 scoped_ptr<ClientSocketHandle> transport_socket, | 743 std::unique_ptr<ClientSocketHandle> transport_socket, |
| 742 const HostPortPair& host_and_port, | 744 const HostPortPair& host_and_port, |
| 743 const SSLConfig& ssl_config, | 745 const SSLConfig& ssl_config, |
| 744 const SSLClientSocketContext& context) { | 746 const SSLClientSocketContext& context) { |
| 745 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); | 747 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); |
| 746 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { | 748 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { |
| 747 EXPECT_EQ(next_ssl_data->next_protos_expected_in_ssl_config.size(), | 749 EXPECT_EQ(next_ssl_data->next_protos_expected_in_ssl_config.size(), |
| 748 ssl_config.alpn_protos.size()); | 750 ssl_config.alpn_protos.size()); |
| 749 EXPECT_TRUE( | 751 EXPECT_TRUE( |
| 750 std::equal(next_ssl_data->next_protos_expected_in_ssl_config.begin(), | 752 std::equal(next_ssl_data->next_protos_expected_in_ssl_config.begin(), |
| 751 next_ssl_data->next_protos_expected_in_ssl_config.end(), | 753 next_ssl_data->next_protos_expected_in_ssl_config.end(), |
| 752 ssl_config.alpn_protos.begin())); | 754 ssl_config.alpn_protos.begin())); |
| 753 } | 755 } |
| 754 return scoped_ptr<SSLClientSocket>(new MockSSLClientSocket( | 756 return std::unique_ptr<SSLClientSocket>(new MockSSLClientSocket( |
| 755 std::move(transport_socket), host_and_port, ssl_config, next_ssl_data)); | 757 std::move(transport_socket), host_and_port, ssl_config, next_ssl_data)); |
| 756 } | 758 } |
| 757 | 759 |
| 758 void MockClientSocketFactory::ClearSSLSessionCache() { | 760 void MockClientSocketFactory::ClearSSLSessionCache() { |
| 759 } | 761 } |
| 760 | 762 |
| 761 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) | 763 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) |
| 762 : connected_(false), | 764 : connected_(false), |
| 763 net_log_(net_log), | 765 net_log_(net_log), |
| 764 weak_factory_(this) { | 766 weak_factory_(this) { |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1131 void MockSSLClientSocket::ConnectCallback( | 1133 void MockSSLClientSocket::ConnectCallback( |
| 1132 MockSSLClientSocket* ssl_client_socket, | 1134 MockSSLClientSocket* ssl_client_socket, |
| 1133 const CompletionCallback& callback, | 1135 const CompletionCallback& callback, |
| 1134 int rv) { | 1136 int rv) { |
| 1135 if (rv == OK) | 1137 if (rv == OK) |
| 1136 ssl_client_socket->connected_ = true; | 1138 ssl_client_socket->connected_ = true; |
| 1137 callback.Run(rv); | 1139 callback.Run(rv); |
| 1138 } | 1140 } |
| 1139 | 1141 |
| 1140 MockSSLClientSocket::MockSSLClientSocket( | 1142 MockSSLClientSocket::MockSSLClientSocket( |
| 1141 scoped_ptr<ClientSocketHandle> transport_socket, | 1143 std::unique_ptr<ClientSocketHandle> transport_socket, |
| 1142 const HostPortPair& host_port_pair, | 1144 const HostPortPair& host_port_pair, |
| 1143 const SSLConfig& ssl_config, | 1145 const SSLConfig& ssl_config, |
| 1144 SSLSocketDataProvider* data) | 1146 SSLSocketDataProvider* data) |
| 1145 : MockClientSocket( | 1147 : MockClientSocket( |
| 1146 // Have to use the right BoundNetLog for LoadTimingInfo regression | 1148 // Have to use the right BoundNetLog for LoadTimingInfo regression |
| 1147 // tests. | 1149 // tests. |
| 1148 transport_socket->socket() | 1150 transport_socket->socket()->NetLog()), |
| 1149 ->NetLog()), | |
| 1150 transport_(std::move(transport_socket)), | 1151 transport_(std::move(transport_socket)), |
| 1151 data_(data) { | 1152 data_(data) { |
| 1152 DCHECK(data_); | 1153 DCHECK(data_); |
| 1153 peer_addr_ = data->connect.peer_addr; | 1154 peer_addr_ = data->connect.peer_addr; |
| 1154 } | 1155 } |
| 1155 | 1156 |
| 1156 MockSSLClientSocket::~MockSSLClientSocket() { | 1157 MockSSLClientSocket::~MockSSLClientSocket() { |
| 1157 Disconnect(); | 1158 Disconnect(); |
| 1158 } | 1159 } |
| 1159 | 1160 |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1518 return kIndexOutOfBounds; | 1519 return kIndexOutOfBounds; |
| 1519 | 1520 |
| 1520 for (size_t i = 0; i < request_order_.size(); i++) | 1521 for (size_t i = 0; i < request_order_.size(); i++) |
| 1521 if (requests_[index].get() == request_order_[i]) | 1522 if (requests_[index].get() == request_order_[i]) |
| 1522 return i + 1; | 1523 return i + 1; |
| 1523 | 1524 |
| 1524 return kRequestNotFound; | 1525 return kRequestNotFound; |
| 1525 } | 1526 } |
| 1526 | 1527 |
| 1527 bool ClientSocketPoolTest::ReleaseOneConnection(KeepAlive keep_alive) { | 1528 bool ClientSocketPoolTest::ReleaseOneConnection(KeepAlive keep_alive) { |
| 1528 for (scoped_ptr<TestSocketRequest>& it : requests_) { | 1529 for (std::unique_ptr<TestSocketRequest>& it : requests_) { |
| 1529 if (it->handle()->is_initialized()) { | 1530 if (it->handle()->is_initialized()) { |
| 1530 if (keep_alive == NO_KEEP_ALIVE) | 1531 if (keep_alive == NO_KEEP_ALIVE) |
| 1531 it->handle()->socket()->Disconnect(); | 1532 it->handle()->socket()->Disconnect(); |
| 1532 it->handle()->Reset(); | 1533 it->handle()->Reset(); |
| 1533 base::RunLoop().RunUntilIdle(); | 1534 base::RunLoop().RunUntilIdle(); |
| 1534 return true; | 1535 return true; |
| 1535 } | 1536 } |
| 1536 } | 1537 } |
| 1537 return false; | 1538 return false; |
| 1538 } | 1539 } |
| 1539 | 1540 |
| 1540 void ClientSocketPoolTest::ReleaseAllConnections(KeepAlive keep_alive) { | 1541 void ClientSocketPoolTest::ReleaseAllConnections(KeepAlive keep_alive) { |
| 1541 bool released_one; | 1542 bool released_one; |
| 1542 do { | 1543 do { |
| 1543 released_one = ReleaseOneConnection(keep_alive); | 1544 released_one = ReleaseOneConnection(keep_alive); |
| 1544 } while (released_one); | 1545 } while (released_one); |
| 1545 } | 1546 } |
| 1546 | 1547 |
| 1547 MockTransportClientSocketPool::MockConnectJob::MockConnectJob( | 1548 MockTransportClientSocketPool::MockConnectJob::MockConnectJob( |
| 1548 scoped_ptr<StreamSocket> socket, | 1549 std::unique_ptr<StreamSocket> socket, |
| 1549 ClientSocketHandle* handle, | 1550 ClientSocketHandle* handle, |
| 1550 const CompletionCallback& callback) | 1551 const CompletionCallback& callback) |
| 1551 : socket_(std::move(socket)), handle_(handle), user_callback_(callback) {} | 1552 : socket_(std::move(socket)), handle_(handle), user_callback_(callback) {} |
| 1552 | 1553 |
| 1553 MockTransportClientSocketPool::MockConnectJob::~MockConnectJob() {} | 1554 MockTransportClientSocketPool::MockConnectJob::~MockConnectJob() {} |
| 1554 | 1555 |
| 1555 int MockTransportClientSocketPool::MockConnectJob::Connect() { | 1556 int MockTransportClientSocketPool::MockConnectJob::Connect() { |
| 1556 int rv = socket_->Connect(base::Bind(&MockConnectJob::OnConnect, | 1557 int rv = socket_->Connect(base::Bind(&MockConnectJob::OnConnect, |
| 1557 base::Unretained(this))); | 1558 base::Unretained(this))); |
| 1558 if (rv != ERR_IO_PENDING) { | 1559 if (rv != ERR_IO_PENDING) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1624 | 1625 |
| 1625 int MockTransportClientSocketPool::RequestSocket( | 1626 int MockTransportClientSocketPool::RequestSocket( |
| 1626 const std::string& group_name, | 1627 const std::string& group_name, |
| 1627 const void* socket_params, | 1628 const void* socket_params, |
| 1628 RequestPriority priority, | 1629 RequestPriority priority, |
| 1629 RespectLimits respect_limits, | 1630 RespectLimits respect_limits, |
| 1630 ClientSocketHandle* handle, | 1631 ClientSocketHandle* handle, |
| 1631 const CompletionCallback& callback, | 1632 const CompletionCallback& callback, |
| 1632 const BoundNetLog& net_log) { | 1633 const BoundNetLog& net_log) { |
| 1633 last_request_priority_ = priority; | 1634 last_request_priority_ = priority; |
| 1634 scoped_ptr<StreamSocket> socket = | 1635 std::unique_ptr<StreamSocket> socket = |
| 1635 client_socket_factory_->CreateTransportClientSocket( | 1636 client_socket_factory_->CreateTransportClientSocket( |
| 1636 AddressList(), NULL, net_log.net_log(), NetLog::Source()); | 1637 AddressList(), NULL, net_log.net_log(), NetLog::Source()); |
| 1637 MockConnectJob* job = new MockConnectJob(std::move(socket), handle, callback); | 1638 MockConnectJob* job = new MockConnectJob(std::move(socket), handle, callback); |
| 1638 job_list_.push_back(make_scoped_ptr(job)); | 1639 job_list_.push_back(base::WrapUnique(job)); |
| 1639 handle->set_pool_id(1); | 1640 handle->set_pool_id(1); |
| 1640 return job->Connect(); | 1641 return job->Connect(); |
| 1641 } | 1642 } |
| 1642 | 1643 |
| 1643 void MockTransportClientSocketPool::CancelRequest(const std::string& group_name, | 1644 void MockTransportClientSocketPool::CancelRequest(const std::string& group_name, |
| 1644 ClientSocketHandle* handle) { | 1645 ClientSocketHandle* handle) { |
| 1645 for (scoped_ptr<MockConnectJob>& it : job_list_) { | 1646 for (std::unique_ptr<MockConnectJob>& it : job_list_) { |
| 1646 if (it->CancelHandle(handle)) { | 1647 if (it->CancelHandle(handle)) { |
| 1647 cancel_count_++; | 1648 cancel_count_++; |
| 1648 break; | 1649 break; |
| 1649 } | 1650 } |
| 1650 } | 1651 } |
| 1651 } | 1652 } |
| 1652 | 1653 |
| 1653 void MockTransportClientSocketPool::ReleaseSocket( | 1654 void MockTransportClientSocketPool::ReleaseSocket( |
| 1654 const std::string& group_name, | 1655 const std::string& group_name, |
| 1655 scoped_ptr<StreamSocket> socket, | 1656 std::unique_ptr<StreamSocket> socket, |
| 1656 int id) { | 1657 int id) { |
| 1657 EXPECT_EQ(1, id); | 1658 EXPECT_EQ(1, id); |
| 1658 release_count_++; | 1659 release_count_++; |
| 1659 } | 1660 } |
| 1660 | 1661 |
| 1661 MockSOCKSClientSocketPool::MockSOCKSClientSocketPool( | 1662 MockSOCKSClientSocketPool::MockSOCKSClientSocketPool( |
| 1662 int max_sockets, | 1663 int max_sockets, |
| 1663 int max_sockets_per_group, | 1664 int max_sockets_per_group, |
| 1664 TransportClientSocketPool* transport_pool) | 1665 TransportClientSocketPool* transport_pool) |
| 1665 : SOCKSClientSocketPool(max_sockets, | 1666 : SOCKSClientSocketPool(max_sockets, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1683 respect_limits, handle, callback, | 1684 respect_limits, handle, callback, |
| 1684 net_log); | 1685 net_log); |
| 1685 } | 1686 } |
| 1686 | 1687 |
| 1687 void MockSOCKSClientSocketPool::CancelRequest( | 1688 void MockSOCKSClientSocketPool::CancelRequest( |
| 1688 const std::string& group_name, | 1689 const std::string& group_name, |
| 1689 ClientSocketHandle* handle) { | 1690 ClientSocketHandle* handle) { |
| 1690 return transport_pool_->CancelRequest(group_name, handle); | 1691 return transport_pool_->CancelRequest(group_name, handle); |
| 1691 } | 1692 } |
| 1692 | 1693 |
| 1693 void MockSOCKSClientSocketPool::ReleaseSocket(const std::string& group_name, | 1694 void MockSOCKSClientSocketPool::ReleaseSocket( |
| 1694 scoped_ptr<StreamSocket> socket, | 1695 const std::string& group_name, |
| 1695 int id) { | 1696 std::unique_ptr<StreamSocket> socket, |
| 1697 int id) { |
| 1696 return transport_pool_->ReleaseSocket(group_name, std::move(socket), id); | 1698 return transport_pool_->ReleaseSocket(group_name, std::move(socket), id); |
| 1697 } | 1699 } |
| 1698 | 1700 |
| 1699 ScopedWebSocketEndpointZeroUnlockDelay:: | 1701 ScopedWebSocketEndpointZeroUnlockDelay:: |
| 1700 ScopedWebSocketEndpointZeroUnlockDelay() { | 1702 ScopedWebSocketEndpointZeroUnlockDelay() { |
| 1701 old_delay_ = | 1703 old_delay_ = |
| 1702 WebSocketEndpointLockManager::GetInstance()->SetUnlockDelayForTesting( | 1704 WebSocketEndpointLockManager::GetInstance()->SetUnlockDelayForTesting( |
| 1703 base::TimeDelta()); | 1705 base::TimeDelta()); |
| 1704 } | 1706 } |
| 1705 | 1707 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1733 } | 1735 } |
| 1734 | 1736 |
| 1735 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { | 1737 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { |
| 1736 int64_t total = 0; | 1738 int64_t total = 0; |
| 1737 for (const MockWrite* write = writes; write != writes + writes_size; ++write) | 1739 for (const MockWrite* write = writes; write != writes + writes_size; ++write) |
| 1738 total += write->data_len; | 1740 total += write->data_len; |
| 1739 return total; | 1741 return total; |
| 1740 } | 1742 } |
| 1741 | 1743 |
| 1742 } // namespace net | 1744 } // namespace net |
| OLD | NEW |