Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: net/socket/socket_test_util.cc

Issue 353713005: Implements new, more robust design for communicating between SSLConnectJobs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed error in SSLSessionIsInCache. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 } 271 }
271 272
272 SSLSocketDataProvider::SSLSocketDataProvider(IoMode mode, int result) 273 SSLSocketDataProvider::SSLSocketDataProvider(IoMode mode, int result)
273 : connect(mode, result), 274 : connect(mode, result),
274 next_proto_status(SSLClientSocket::kNextProtoUnsupported), 275 next_proto_status(SSLClientSocket::kNextProtoUnsupported),
275 was_npn_negotiated(false), 276 was_npn_negotiated(false),
276 protocol_negotiated(kProtoUnknown), 277 protocol_negotiated(kProtoUnknown),
277 client_cert_sent(false), 278 client_cert_sent(false),
278 cert_request_info(NULL), 279 cert_request_info(NULL),
279 channel_id_sent(false), 280 channel_id_sent(false),
280 connection_status(0) { 281 connection_status(0),
282 block_in_connect_(false),
283 blocked_in_connect_(false),
284 is_in_session_cache_(true) {
wtc 2014/07/18 01:17:14 Why is |is_in_session_cache_| initialized to true?
mshelley 2014/07/18 21:08:30 Done.
281 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, 285 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
282 &connection_status); 286 &connection_status);
283 // Set to TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 287 // Set to TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305
284 SSLConnectionStatusSetCipherSuite(0xcc14, &connection_status); 288 SSLConnectionStatusSetCipherSuite(0xcc14, &connection_status);
285 } 289 }
286 290
287 SSLSocketDataProvider::~SSLSocketDataProvider() { 291 SSLSocketDataProvider::~SSLSocketDataProvider() {
288 } 292 }
289 293
290 void SSLSocketDataProvider::SetNextProto(NextProto proto) { 294 void SSLSocketDataProvider::SetNextProto(NextProto proto) {
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 new MockTCPClientSocket(addresses, net_log, data_provider)); 696 new MockTCPClientSocket(addresses, net_log, data_provider));
693 data_provider->set_socket(socket.get()); 697 data_provider->set_socket(socket.get());
694 return socket.PassAs<StreamSocket>(); 698 return socket.PassAs<StreamSocket>();
695 } 699 }
696 700
697 scoped_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( 701 scoped_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket(
698 scoped_ptr<ClientSocketHandle> transport_socket, 702 scoped_ptr<ClientSocketHandle> transport_socket,
699 const HostPortPair& host_and_port, 703 const HostPortPair& host_and_port,
700 const SSLConfig& ssl_config, 704 const SSLConfig& ssl_config,
701 const SSLClientSocketContext& context) { 705 const SSLClientSocketContext& context) {
702 return scoped_ptr<SSLClientSocket>( 706 scoped_ptr<MockSSLClientSocket> socket(
703 new MockSSLClientSocket(transport_socket.Pass(), 707 new MockSSLClientSocket(transport_socket.Pass(),
704 host_and_port, ssl_config, 708 host_and_port,
709 ssl_config,
705 mock_ssl_data_.GetNext())); 710 mock_ssl_data_.GetNext()));
711 ssl_client_sockets_.push_back(socket.get());
712 return socket.PassAs<SSLClientSocket>();
706 } 713 }
707 714
708 void MockClientSocketFactory::ClearSSLSessionCache() { 715 void MockClientSocketFactory::ClearSSLSessionCache() {
709 } 716 }
710 717
718 std::vector<MockSSLClientSocket*>
719 MockClientSocketFactory::GetSSLClientSockets() {
720 return ssl_client_sockets_;
721 }
722
711 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; 723 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ";
712 724
713 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) 725 MockClientSocket::MockClientSocket(const BoundNetLog& net_log)
714 : connected_(false), 726 : connected_(false),
715 net_log_(net_log), 727 net_log_(net_log),
716 weak_factory_(this) { 728 weak_factory_(this) {
717 IPAddressNumber ip; 729 IPAddressNumber ip;
718 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); 730 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip));
719 peer_addr_ = IPEndPoint(ip, 0); 731 peer_addr_ = IPEndPoint(ip, 0);
720 } 732 }
721 733
722 int MockClientSocket::SetReceiveBufferSize(int32 size) { 734 int MockClientSocket::SetReceiveBufferSize(int32 size) {
723 return OK; 735 return OK;
724 } 736 }
725 737
726 int MockClientSocket::SetSendBufferSize(int32 size) { 738 int MockClientSocket::SetSendBufferSize(int32 size) {
727 return OK; 739 return OK;
728 } 740 }
729 741
730 void MockClientSocket::Disconnect() { 742 void MockClientSocket::Disconnect() {
731 connected_ = false; 743 connected_ = false;
732 } 744 }
733 745
734 bool MockClientSocket::IsConnected() const { 746 bool MockClientSocket::IsConnected() const {
735 return connected_; 747 return connected_;
736 } 748 }
737 749
750 bool MockClientSocket::IsConnectedSSL() const {
751 return connected_;
752 }
753
738 bool MockClientSocket::IsConnectedAndIdle() const { 754 bool MockClientSocket::IsConnectedAndIdle() const {
739 return connected_; 755 return connected_;
740 } 756 }
741 757
742 int MockClientSocket::GetPeerAddress(IPEndPoint* address) const { 758 int MockClientSocket::GetPeerAddress(IPEndPoint* address) const {
743 if (!IsConnected()) 759 if (!IsConnected())
744 return ERR_SOCKET_NOT_CONNECTED; 760 return ERR_SOCKET_NOT_CONNECTED;
745 *address = peer_addr_; 761 *address = peer_addr_;
746 return OK; 762 return OK;
747 } 763 }
748 764
749 int MockClientSocket::GetLocalAddress(IPEndPoint* address) const { 765 int MockClientSocket::GetLocalAddress(IPEndPoint* address) const {
750 IPAddressNumber ip; 766 IPAddressNumber ip;
751 bool rv = ParseIPLiteralToNumber("192.0.2.33", &ip); 767 bool rv = ParseIPLiteralToNumber("192.0.2.33", &ip);
752 CHECK(rv); 768 CHECK(rv);
753 *address = IPEndPoint(ip, 123); 769 *address = IPEndPoint(ip, 123);
754 return OK; 770 return OK;
755 } 771 }
756 772
757 const BoundNetLog& MockClientSocket::NetLog() const { 773 const BoundNetLog& MockClientSocket::NetLog() const {
758 return net_log_; 774 return net_log_;
759 } 775 }
760 776
777 bool MockClientSocket::InSessionCache() const {
778 return true;
779 }
780
781 void MockClientSocket::SetHandshakeSuccessCallback(const base::Closure& cb) {
782 }
783
784 void MockClientSocket::SetHandshakeFailureCallback(const base::Closure& cb) {
785 }
wtc 2014/07/18 15:39:16 You may want to assert NOTREACHED or NOTIMPLEMENTE
mshelley 2014/07/18 21:08:30 Done.
786
761 void MockClientSocket::GetSSLCertRequestInfo( 787 void MockClientSocket::GetSSLCertRequestInfo(
762 SSLCertRequestInfo* cert_request_info) { 788 SSLCertRequestInfo* cert_request_info) {
763 } 789 }
764 790
765 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, 791 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label,
766 bool has_context, 792 bool has_context,
767 const base::StringPiece& context, 793 const base::StringPiece& context,
768 unsigned char* out, 794 unsigned char* out,
769 unsigned int outlen) { 795 unsigned int outlen) {
770 memset(out, 'A', outlen); 796 memset(out, 'A', outlen);
771 return OK; 797 return OK;
772 } 798 }
773 799
774 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { 800 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) {
775 out->assign(MockClientSocket::kTlsUnique); 801 out->assign(MockClientSocket::kTlsUnique);
776 return OK; 802 return OK;
777 } 803 }
778 804
779 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const { 805 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const {
780 NOTREACHED(); 806 NOTREACHED();
781 return NULL; 807 return NULL;
782 } 808 }
783 809
810 CompletionCallback MockClientSocket::GetResumptionCallback() {
811 return resumption_callback_;
812 }
813
784 SSLClientSocket::NextProtoStatus 814 SSLClientSocket::NextProtoStatus
785 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { 815 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) {
786 proto->clear(); 816 proto->clear();
787 server_protos->clear(); 817 server_protos->clear();
788 return SSLClientSocket::kNextProtoUnsupported; 818 return SSLClientSocket::kNextProtoUnsupported;
789 } 819 }
790 820
791 scoped_refptr<X509Certificate> 821 scoped_refptr<X509Certificate>
792 MockClientSocket::GetUnverifiedServerCertificateChain() const { 822 MockClientSocket::GetUnverifiedServerCertificateChain() const {
793 NOTREACHED(); 823 NOTREACHED();
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
1307 ssl_client_socket->connected_ = true; 1337 ssl_client_socket->connected_ = true;
1308 callback.Run(rv); 1338 callback.Run(rv);
1309 } 1339 }
1310 1340
1311 MockSSLClientSocket::MockSSLClientSocket( 1341 MockSSLClientSocket::MockSSLClientSocket(
1312 scoped_ptr<ClientSocketHandle> transport_socket, 1342 scoped_ptr<ClientSocketHandle> transport_socket,
1313 const HostPortPair& host_port_pair, 1343 const HostPortPair& host_port_pair,
1314 const SSLConfig& ssl_config, 1344 const SSLConfig& ssl_config,
1315 SSLSocketDataProvider* data) 1345 SSLSocketDataProvider* data)
1316 : MockClientSocket( 1346 : MockClientSocket(
1317 // Have to use the right BoundNetLog for LoadTimingInfo regression 1347 // Have to use the right BoundNetLog for LoadTimingInfo regression
1318 // tests. 1348 // tests.
1319 transport_socket->socket()->NetLog()), 1349 transport_socket->socket()->NetLog()),
1320 transport_(transport_socket.Pass()), 1350 transport_(transport_socket.Pass()),
1321 data_(data), 1351 data_(data),
1322 is_npn_state_set_(false), 1352 is_npn_state_set_(false),
1323 new_npn_value_(false), 1353 new_npn_value_(false),
1324 is_protocol_negotiated_set_(false), 1354 is_protocol_negotiated_set_(false),
1325 protocol_negotiated_(kProtoUnknown) { 1355 protocol_negotiated_(kProtoUnknown) {
1326 DCHECK(data_); 1356 DCHECK(data_);
1327 peer_addr_ = data->connect.peer_addr; 1357 peer_addr_ = data->connect.peer_addr;
1328 } 1358 }
1329 1359
1330 MockSSLClientSocket::~MockSSLClientSocket() { 1360 MockSSLClientSocket::~MockSSLClientSocket() {
1331 Disconnect(); 1361 Disconnect();
1332 } 1362 }
1333 1363
1334 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, 1364 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len,
1335 const CompletionCallback& callback) { 1365 const CompletionCallback& callback) {
1336 return transport_->socket()->Read(buf, buf_len, callback); 1366 return transport_->socket()->Read(buf, buf_len, callback);
1337 } 1367 }
1338 1368
1339 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, 1369 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len,
1340 const CompletionCallback& callback) { 1370 const CompletionCallback& callback) {
1341 return transport_->socket()->Write(buf, buf_len, callback); 1371 return transport_->socket()->Write(buf, buf_len, callback);
1342 } 1372 }
1343 1373
1344 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { 1374 int MockSSLClientSocket::Connect(const CompletionCallback& callback) {
1345 int rv = transport_->socket()->Connect( 1375 int rv;
1346 base::Bind(&ConnectCallback, base::Unretained(this), callback)); 1376 if (!data_->blocked_in_connect_)
1377 rv = transport_->socket()->Connect(
1378 base::Bind(&ConnectCallback, base::Unretained(this), callback));
1379 else
1380 rv = OK;
wtc 2014/07/18 01:17:14 Add curly braces to both the "if" and "else" branc
mshelley 2014/07/18 21:08:31 Done.
1381
1347 if (rv == OK) { 1382 if (rv == OK) {
1348 if (data_->connect.result == OK) 1383 if (data_->connect.result == OK) {
1349 connected_ = true; 1384 connected_ = true;
1350 if (data_->connect.mode == ASYNC) { 1385 if (data_->connect.mode == ASYNC) {
wtc 2014/07/18 15:39:16 IMPORTANT: in the original code, the "if (data_->c
mshelley 2014/07/18 21:08:30 That was not intended -- I fixed it.
1351 RunCallbackAsync(callback, data_->connect.result); 1386 if (data_->block_in_connect_) {
1352 return ERR_IO_PENDING; 1387 resumption_callback_ = callback;
1388 data_->block_in_connect_ = false;
1389 data_->blocked_in_connect_ = true;
1390 return ERR_IO_PENDING;
1391 } else {
wtc 2014/07/18 01:17:14 We usually omit "else" after a return statement. T
mshelley 2014/07/18 21:08:30 Done.
1392 if (!success_callback_.is_null()) {
1393 success_callback_.Run();
1394 }
wtc 2014/07/18 01:17:14 Omit curly braces. The local convention in our ne
mshelley 2014/07/18 21:08:30 Done.
1395 RunCallbackAsync(callback, data_->connect.result);
1396 }
1397 return ERR_IO_PENDING;
1398 }
1399
1400 // The presence of |success_callback_| indicates that SSL Connect Job
1401 // Waiting is enabled.
1402 if (!success_callback_.is_null()) {
1403 success_callback_.Run();
1404 }
1405 } else if (!error_callback_.is_null()) {
1406 if (data_->block_in_connect_) {
1407 resumption_callback_ = callback;
1408 data_->block_in_connect_ = false;
1409 data_->blocked_in_connect_ = true;
1410 return ERR_IO_PENDING;
1411 }
1412 error_callback_.Run();
wtc 2014/07/18 15:39:16 BUG: we need to reset a callback when we run it. W
mshelley 2014/07/18 21:08:30 Done.
1353 } 1413 }
1414
1354 return data_->connect.result; 1415 return data_->connect.result;
1355 } 1416 }
1356 return rv; 1417 return rv;
1357 } 1418 }
1358
1359 void MockSSLClientSocket::Disconnect() { 1419 void MockSSLClientSocket::Disconnect() {
wtc 2014/07/18 15:39:16 Resurrect the blank line before this.
mshelley 2014/07/18 21:08:30 Done.
1360 MockClientSocket::Disconnect(); 1420 MockClientSocket::Disconnect();
1361 if (transport_->socket() != NULL) 1421 if (transport_->socket() != NULL)
1362 transport_->socket()->Disconnect(); 1422 transport_->socket()->Disconnect();
1363 } 1423 }
1364 1424
1365 bool MockSSLClientSocket::IsConnected() const { 1425 bool MockSSLClientSocket::IsConnected() const {
1366 return transport_->socket()->IsConnected(); 1426 return transport_->socket()->IsConnected();
1367 } 1427 }
1368 1428
1429 bool MockSSLClientSocket::IsConnectedSSL() const {
1430 return connected_;
wtc 2014/07/18 01:17:14 IMPORTANT: it seems better to just use the existin
mshelley 2014/07/18 21:08:31 That makes sense to me. It seemed strange that SSL
1431 }
1432
1369 bool MockSSLClientSocket::WasEverUsed() const { 1433 bool MockSSLClientSocket::WasEverUsed() const {
1370 return transport_->socket()->WasEverUsed(); 1434 return transport_->socket()->WasEverUsed();
1371 } 1435 }
1372 1436
1373 bool MockSSLClientSocket::UsingTCPFastOpen() const { 1437 bool MockSSLClientSocket::UsingTCPFastOpen() const {
1374 return transport_->socket()->UsingTCPFastOpen(); 1438 return transport_->socket()->UsingTCPFastOpen();
1375 } 1439 }
1376 1440
1377 int MockSSLClientSocket::GetPeerAddress(IPEndPoint* address) const { 1441 int MockSSLClientSocket::GetPeerAddress(IPEndPoint* address) const {
1378 return transport_->socket()->GetPeerAddress(address); 1442 return transport_->socket()->GetPeerAddress(address);
(...skipping 13 matching lines...) Expand all
1392 DCHECK(cert_request_info); 1456 DCHECK(cert_request_info);
1393 if (data_->cert_request_info) { 1457 if (data_->cert_request_info) {
1394 cert_request_info->host_and_port = 1458 cert_request_info->host_and_port =
1395 data_->cert_request_info->host_and_port; 1459 data_->cert_request_info->host_and_port;
1396 cert_request_info->client_certs = data_->cert_request_info->client_certs; 1460 cert_request_info->client_certs = data_->cert_request_info->client_certs;
1397 } else { 1461 } else {
1398 cert_request_info->Reset(); 1462 cert_request_info->Reset();
1399 } 1463 }
1400 } 1464 }
1401 1465
1466 bool MockSSLClientSocket::InSessionCache() const {
1467 return data_->is_in_session_cache_;
1468 }
1469
1470 void MockSSLClientSocket::SetHandshakeSuccessCallback(const base::Closure& cb) {
1471 success_callback_ = cb;
1472 }
1473
1474 void MockSSLClientSocket::SetHandshakeFailureCallback(const base::Closure& cb) {
1475 error_callback_ = cb;
1476 }
1477
1402 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( 1478 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto(
1403 std::string* proto, std::string* server_protos) { 1479 std::string* proto, std::string* server_protos) {
1404 *proto = data_->next_proto; 1480 *proto = data_->next_proto;
1405 *server_protos = data_->server_protos; 1481 *server_protos = data_->server_protos;
1406 return data_->next_proto_status; 1482 return data_->next_proto_status;
1407 } 1483 }
1408 1484
1409 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { 1485 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) {
1410 is_npn_state_set_ = true; 1486 is_npn_state_set_ = true;
1411 return new_npn_value_ = negotiated; 1487 return new_npn_value_ = negotiated;
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after
1906 1982
1907 const char kSOCKS5OkRequest[] = 1983 const char kSOCKS5OkRequest[] =
1908 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; 1984 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 };
1909 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); 1985 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest);
1910 1986
1911 const char kSOCKS5OkResponse[] = 1987 const char kSOCKS5OkResponse[] =
1912 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; 1988 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 };
1913 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); 1989 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse);
1914 1990
1915 } // namespace net 1991 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698