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 <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
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/callback_helpers.h" | |
13 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "net/base/address_family.h" | 18 #include "net/base/address_family.h" |
18 #include "net/base/address_list.h" | 19 #include "net/base/address_list.h" |
19 #include "net/base/auth.h" | 20 #include "net/base/auth.h" |
20 #include "net/base/load_timing_info.h" | 21 #include "net/base/load_timing_info.h" |
21 #include "net/http/http_network_session.h" | 22 #include "net/http/http_network_session.h" |
22 #include "net/http/http_request_headers.h" | 23 #include "net/http/http_request_headers.h" |
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
646 return; | 647 return; |
647 } | 648 } |
648 DCHECK_EQ(read, reads_count); | 649 DCHECK_EQ(read, reads_count); |
649 DCHECK_EQ(write, writes_count); | 650 DCHECK_EQ(write, writes_count); |
650 } | 651 } |
651 | 652 |
652 MockClientSocketFactory::MockClientSocketFactory() {} | 653 MockClientSocketFactory::MockClientSocketFactory() {} |
653 | 654 |
654 MockClientSocketFactory::~MockClientSocketFactory() {} | 655 MockClientSocketFactory::~MockClientSocketFactory() {} |
655 | 656 |
657 bool MockClientSocketFactory::leader_connected_ = false; | |
658 | |
656 void MockClientSocketFactory::AddSocketDataProvider( | 659 void MockClientSocketFactory::AddSocketDataProvider( |
657 SocketDataProvider* data) { | 660 SocketDataProvider* data) { |
658 mock_data_.Add(data); | 661 mock_data_.Add(data); |
659 } | 662 } |
660 | 663 |
661 void MockClientSocketFactory::AddSSLSocketDataProvider( | 664 void MockClientSocketFactory::AddSSLSocketDataProvider( |
662 SSLSocketDataProvider* data) { | 665 SSLSocketDataProvider* data) { |
663 mock_ssl_data_.Add(data); | 666 mock_ssl_data_.Add(data); |
664 } | 667 } |
665 | 668 |
666 void MockClientSocketFactory::ResetNextMockIndexes() { | 669 void MockClientSocketFactory::ResetNextMockIndexes() { |
667 mock_data_.ResetNextIndex(); | 670 mock_data_.ResetNextIndex(); |
668 mock_ssl_data_.ResetNextIndex(); | 671 mock_ssl_data_.ResetNextIndex(); |
669 } | 672 } |
670 | 673 |
674 void MockClientSocketFactory::SetLeaderConnected() { | |
675 leader_connected_ = true; | |
676 } | |
677 | |
678 bool MockClientSocketFactory::IsLeaderConnected() { | |
679 return leader_connected_; | |
680 } | |
681 | |
671 scoped_ptr<DatagramClientSocket> | 682 scoped_ptr<DatagramClientSocket> |
672 MockClientSocketFactory::CreateDatagramClientSocket( | 683 MockClientSocketFactory::CreateDatagramClientSocket( |
673 DatagramSocket::BindType bind_type, | 684 DatagramSocket::BindType bind_type, |
674 const RandIntCallback& rand_int_cb, | 685 const RandIntCallback& rand_int_cb, |
675 net::NetLog* net_log, | 686 net::NetLog* net_log, |
676 const net::NetLog::Source& source) { | 687 const net::NetLog::Source& source) { |
677 SocketDataProvider* data_provider = mock_data_.GetNext(); | 688 SocketDataProvider* data_provider = mock_data_.GetNext(); |
678 scoped_ptr<MockUDPClientSocket> socket( | 689 scoped_ptr<MockUDPClientSocket> socket( |
679 new MockUDPClientSocket(data_provider, net_log)); | 690 new MockUDPClientSocket(data_provider, net_log)); |
680 data_provider->set_socket(socket.get()); | 691 data_provider->set_socket(socket.get()); |
(...skipping 11 matching lines...) Expand all Loading... | |
692 new MockTCPClientSocket(addresses, net_log, data_provider)); | 703 new MockTCPClientSocket(addresses, net_log, data_provider)); |
693 data_provider->set_socket(socket.get()); | 704 data_provider->set_socket(socket.get()); |
694 return socket.PassAs<StreamSocket>(); | 705 return socket.PassAs<StreamSocket>(); |
695 } | 706 } |
696 | 707 |
697 scoped_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( | 708 scoped_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( |
698 scoped_ptr<ClientSocketHandle> transport_socket, | 709 scoped_ptr<ClientSocketHandle> transport_socket, |
699 const HostPortPair& host_and_port, | 710 const HostPortPair& host_and_port, |
700 const SSLConfig& ssl_config, | 711 const SSLConfig& ssl_config, |
701 const SSLClientSocketContext& context) { | 712 const SSLClientSocketContext& context) { |
702 return scoped_ptr<SSLClientSocket>( | 713 scoped_ptr<MockSSLClientSocket> socket( |
703 new MockSSLClientSocket(transport_socket.Pass(), | 714 new MockSSLClientSocket(transport_socket.Pass(), |
704 host_and_port, ssl_config, | 715 host_and_port, |
716 ssl_config, | |
705 mock_ssl_data_.GetNext())); | 717 mock_ssl_data_.GetNext())); |
718 ssl_client_sockets_.push_back(socket.get()); | |
719 return socket.PassAs<SSLClientSocket>(); | |
706 } | 720 } |
707 | 721 |
708 void MockClientSocketFactory::ClearSSLSessionCache() { | 722 void MockClientSocketFactory::ClearSSLSessionCache() { |
709 } | 723 } |
710 | 724 |
725 std::vector<MockSSLClientSocket*> | |
726 MockClientSocketFactory::GetSSLClientSockets() { | |
727 return ssl_client_sockets_; | |
728 } | |
729 | |
711 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; | 730 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; |
712 | 731 |
713 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) | 732 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) |
714 : connected_(false), | 733 : connected_(false), |
715 net_log_(net_log), | 734 net_log_(net_log), |
716 weak_factory_(this) { | 735 weak_factory_(this) { |
717 IPAddressNumber ip; | 736 IPAddressNumber ip; |
718 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); | 737 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
719 peer_addr_ = IPEndPoint(ip, 0); | 738 peer_addr_ = IPEndPoint(ip, 0); |
720 } | 739 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
755 } | 774 } |
756 | 775 |
757 const BoundNetLog& MockClientSocket::NetLog() const { | 776 const BoundNetLog& MockClientSocket::NetLog() const { |
758 return net_log_; | 777 return net_log_; |
759 } | 778 } |
760 | 779 |
761 void MockClientSocket::GetSSLCertRequestInfo( | 780 void MockClientSocket::GetSSLCertRequestInfo( |
762 SSLCertRequestInfo* cert_request_info) { | 781 SSLCertRequestInfo* cert_request_info) { |
763 } | 782 } |
764 | 783 |
784 bool MockClientSocket::InSessionCache() const { | |
785 return true; | |
786 } | |
787 | |
788 void MockClientSocket::SetHandshakeSuccessCallback(const base::Closure& cb) { | |
789 } | |
790 | |
791 void MockClientSocket::SetHandshakeFailureCallback(const base::Closure& cb) { | |
792 } | |
793 | |
794 void MockClientSocket::OnSocketFailure() { | |
795 } | |
796 | |
797 void MockClientSocket::SetIsLeader() { | |
798 } | |
799 | |
765 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, | 800 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, |
766 bool has_context, | 801 bool has_context, |
767 const base::StringPiece& context, | 802 const base::StringPiece& context, |
768 unsigned char* out, | 803 unsigned char* out, |
769 unsigned int outlen) { | 804 unsigned int outlen) { |
770 memset(out, 'A', outlen); | 805 memset(out, 'A', outlen); |
771 return OK; | 806 return OK; |
772 } | 807 } |
773 | 808 |
774 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { | 809 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1307 ssl_client_socket->connected_ = true; | 1342 ssl_client_socket->connected_ = true; |
1308 callback.Run(rv); | 1343 callback.Run(rv); |
1309 } | 1344 } |
1310 | 1345 |
1311 MockSSLClientSocket::MockSSLClientSocket( | 1346 MockSSLClientSocket::MockSSLClientSocket( |
1312 scoped_ptr<ClientSocketHandle> transport_socket, | 1347 scoped_ptr<ClientSocketHandle> transport_socket, |
1313 const HostPortPair& host_port_pair, | 1348 const HostPortPair& host_port_pair, |
1314 const SSLConfig& ssl_config, | 1349 const SSLConfig& ssl_config, |
1315 SSLSocketDataProvider* data) | 1350 SSLSocketDataProvider* data) |
1316 : MockClientSocket( | 1351 : MockClientSocket( |
1317 // Have to use the right BoundNetLog for LoadTimingInfo regression | 1352 // Have to use the right BoundNetLog for LoadTimingInfo regression |
1318 // tests. | 1353 // tests. |
1319 transport_socket->socket()->NetLog()), | 1354 transport_socket->socket()->NetLog()), |
1320 transport_(transport_socket.Pass()), | 1355 transport_(transport_socket.Pass()), |
1321 data_(data), | 1356 data_(data), |
1322 is_npn_state_set_(false), | 1357 is_npn_state_set_(false), |
1323 new_npn_value_(false), | 1358 new_npn_value_(false), |
1324 is_protocol_negotiated_set_(false), | 1359 is_protocol_negotiated_set_(false), |
1325 protocol_negotiated_(kProtoUnknown) { | 1360 protocol_negotiated_(kProtoUnknown) { |
1326 DCHECK(data_); | 1361 DCHECK(data_); |
1327 peer_addr_ = data->connect.peer_addr; | 1362 peer_addr_ = data->connect.peer_addr; |
1328 } | 1363 } |
1329 | 1364 |
1330 MockSSLClientSocket::~MockSSLClientSocket() { | 1365 MockSSLClientSocket::~MockSSLClientSocket() { |
1331 Disconnect(); | 1366 Disconnect(); |
1332 } | 1367 } |
1333 | 1368 |
1334 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, | 1369 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, |
1335 const CompletionCallback& callback) { | 1370 const CompletionCallback& callback) { |
1336 return transport_->socket()->Read(buf, buf_len, callback); | 1371 return transport_->socket()->Read(buf, buf_len, callback); |
1337 } | 1372 } |
1338 | 1373 |
1339 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, | 1374 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, |
1340 const CompletionCallback& callback) { | 1375 const CompletionCallback& callback) { |
1341 return transport_->socket()->Write(buf, buf_len, callback); | 1376 return transport_->socket()->Write(buf, buf_len, callback); |
1342 } | 1377 } |
1343 | 1378 |
1344 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { | 1379 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { |
1380 if (SSLClientSocketPool::get_enable_connect_job_waiting()) { | |
1381 // The socket should only be starting to connect if the leader has already | |
1382 // connected -- unless the socket is the leader. | |
1383 if (!MockClientSocketFactory::IsLeaderConnected()) | |
1384 good_ordering_ = false; | |
1385 else | |
1386 good_ordering_ = true; | |
1387 } | |
1345 int rv = transport_->socket()->Connect( | 1388 int rv = transport_->socket()->Connect( |
1346 base::Bind(&ConnectCallback, base::Unretained(this), callback)); | 1389 base::Bind(&ConnectCallback, base::Unretained(this), callback)); |
1347 if (rv == OK) { | 1390 if (rv == OK) { |
1348 if (data_->connect.result == OK) | 1391 if (data_->connect.result == OK) { |
1349 connected_ = true; | 1392 connected_ = true; |
1393 if (SSLClientSocketPool::get_enable_connect_job_waiting()) { | |
1394 if (data_->is_leader_) { | |
1395 good_ordering_ = true; | |
1396 MockClientSocketFactory::SetLeaderConnected(); | |
1397 } | |
1398 if (!success_callback_.is_null()) | |
1399 success_callback_.Run(); | |
1400 } | |
1401 } else if (SSLClientSocketPool::get_enable_connect_job_waiting() && | |
1402 data_->is_leader_) { | |
1403 error_callback_.Run(); | |
Ryan Sleevi
2014/07/12 00:12:15
This doesn't seem right.
Is the behaviour of a no
| |
1404 } | |
1405 | |
1350 if (data_->connect.mode == ASYNC) { | 1406 if (data_->connect.mode == ASYNC) { |
1351 RunCallbackAsync(callback, data_->connect.result); | 1407 RunCallbackAsync(callback, data_->connect.result); |
1352 return ERR_IO_PENDING; | 1408 return ERR_IO_PENDING; |
1353 } | 1409 } |
1354 return data_->connect.result; | 1410 return data_->connect.result; |
1355 } | 1411 } |
1356 return rv; | 1412 return rv; |
1357 } | 1413 } |
1358 | 1414 |
1359 void MockSSLClientSocket::Disconnect() { | 1415 void MockSSLClientSocket::Disconnect() { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1392 DCHECK(cert_request_info); | 1448 DCHECK(cert_request_info); |
1393 if (data_->cert_request_info) { | 1449 if (data_->cert_request_info) { |
1394 cert_request_info->host_and_port = | 1450 cert_request_info->host_and_port = |
1395 data_->cert_request_info->host_and_port; | 1451 data_->cert_request_info->host_and_port; |
1396 cert_request_info->client_certs = data_->cert_request_info->client_certs; | 1452 cert_request_info->client_certs = data_->cert_request_info->client_certs; |
1397 } else { | 1453 } else { |
1398 cert_request_info->Reset(); | 1454 cert_request_info->Reset(); |
1399 } | 1455 } |
1400 } | 1456 } |
1401 | 1457 |
1458 bool MockSSLClientSocket::InSessionCache() const { | |
1459 return data_->is_in_session_cache_; | |
1460 } | |
1461 | |
1462 void MockSSLClientSocket::SetHandshakeSuccessCallback(const base::Closure& cb) { | |
1463 success_callback_ = cb; | |
1464 } | |
1465 | |
1466 void MockSSLClientSocket::SetHandshakeFailureCallback(const base::Closure& cb) { | |
1467 error_callback_ = cb; | |
1468 } | |
1469 | |
1470 void MockSSLClientSocket::OnHandshakeFailure() { | |
1471 if (!error_callback_.is_null()) | |
1472 base::ResetAndReturn(&error_callback_).Run(); | |
1473 } | |
1474 | |
1402 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( | 1475 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( |
1403 std::string* proto, std::string* server_protos) { | 1476 std::string* proto, std::string* server_protos) { |
1404 *proto = data_->next_proto; | 1477 *proto = data_->next_proto; |
1405 *server_protos = data_->server_protos; | 1478 *server_protos = data_->server_protos; |
1406 return data_->next_proto_status; | 1479 return data_->next_proto_status; |
1407 } | 1480 } |
1408 | 1481 |
1409 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { | 1482 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { |
1410 is_npn_state_set_ = true; | 1483 is_npn_state_set_ = true; |
1411 return new_npn_value_ = negotiated; | 1484 return new_npn_value_ = negotiated; |
(...skipping 22 matching lines...) Expand all Loading... | |
1434 } | 1507 } |
1435 | 1508 |
1436 void MockSSLClientSocket::set_channel_id_sent(bool channel_id_sent) { | 1509 void MockSSLClientSocket::set_channel_id_sent(bool channel_id_sent) { |
1437 data_->channel_id_sent = channel_id_sent; | 1510 data_->channel_id_sent = channel_id_sent; |
1438 } | 1511 } |
1439 | 1512 |
1440 ServerBoundCertService* MockSSLClientSocket::GetServerBoundCertService() const { | 1513 ServerBoundCertService* MockSSLClientSocket::GetServerBoundCertService() const { |
1441 return data_->server_bound_cert_service; | 1514 return data_->server_bound_cert_service; |
1442 } | 1515 } |
1443 | 1516 |
1517 bool MockSSLClientSocket::IsGoodOrdering() const { | |
1518 return good_ordering_; | |
1519 } | |
1520 | |
1444 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { | 1521 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { |
1445 NOTIMPLEMENTED(); | 1522 NOTIMPLEMENTED(); |
1446 } | 1523 } |
1447 | 1524 |
1448 void MockSSLClientSocket::OnConnectComplete(const MockConnect& data) { | 1525 void MockSSLClientSocket::OnConnectComplete(const MockConnect& data) { |
1449 NOTIMPLEMENTED(); | 1526 NOTIMPLEMENTED(); |
1450 } | 1527 } |
1451 | 1528 |
1452 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, | 1529 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, |
1453 net::NetLog* net_log) | 1530 net::NetLog* net_log) |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1906 | 1983 |
1907 const char kSOCKS5OkRequest[] = | 1984 const char kSOCKS5OkRequest[] = |
1908 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1985 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
1909 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1986 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
1910 | 1987 |
1911 const char kSOCKS5OkResponse[] = | 1988 const char kSOCKS5OkResponse[] = |
1912 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1989 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
1913 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1990 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
1914 | 1991 |
1915 } // namespace net | 1992 } // namespace net |
OLD | NEW |