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