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

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: Rewrote the MockSSLClientSocket connect state loop to use the typical Chrome structure & fixed bugs… Created 6 years, 4 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 should_block_on_connect(false),
283 is_in_session_cache(false) {
281 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, 284 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2,
282 &connection_status); 285 &connection_status);
283 // Set to TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 286 // Set to TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305
284 SSLConnectionStatusSetCipherSuite(0xcc14, &connection_status); 287 SSLConnectionStatusSetCipherSuite(0xcc14, &connection_status);
285 } 288 }
286 289
287 SSLSocketDataProvider::~SSLSocketDataProvider() { 290 SSLSocketDataProvider::~SSLSocketDataProvider() {
288 } 291 }
289 292
290 void SSLSocketDataProvider::SetNextProto(NextProto proto) { 293 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)); 695 new MockTCPClientSocket(addresses, net_log, data_provider));
693 data_provider->set_socket(socket.get()); 696 data_provider->set_socket(socket.get());
694 return socket.PassAs<StreamSocket>(); 697 return socket.PassAs<StreamSocket>();
695 } 698 }
696 699
697 scoped_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( 700 scoped_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket(
698 scoped_ptr<ClientSocketHandle> transport_socket, 701 scoped_ptr<ClientSocketHandle> transport_socket,
699 const HostPortPair& host_and_port, 702 const HostPortPair& host_and_port,
700 const SSLConfig& ssl_config, 703 const SSLConfig& ssl_config,
701 const SSLClientSocketContext& context) { 704 const SSLClientSocketContext& context) {
702 return scoped_ptr<SSLClientSocket>( 705 scoped_ptr<MockSSLClientSocket> socket(
703 new MockSSLClientSocket(transport_socket.Pass(), 706 new MockSSLClientSocket(transport_socket.Pass(),
704 host_and_port, ssl_config, 707 host_and_port,
708 ssl_config,
705 mock_ssl_data_.GetNext())); 709 mock_ssl_data_.GetNext()));
710 ssl_client_sockets_.push_back(socket.get());
711 return socket.PassAs<SSLClientSocket>();
706 } 712 }
707 713
708 void MockClientSocketFactory::ClearSSLSessionCache() { 714 void MockClientSocketFactory::ClearSSLSessionCache() {
709 } 715 }
710 716
711 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; 717 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ";
712 718
713 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) 719 MockClientSocket::MockClientSocket(const BoundNetLog& net_log)
714 : connected_(false), 720 : connected_(false),
715 net_log_(net_log), 721 net_log_(net_log),
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 bool rv = ParseIPLiteralToNumber("192.0.2.33", &ip); 757 bool rv = ParseIPLiteralToNumber("192.0.2.33", &ip);
752 CHECK(rv); 758 CHECK(rv);
753 *address = IPEndPoint(ip, 123); 759 *address = IPEndPoint(ip, 123);
754 return OK; 760 return OK;
755 } 761 }
756 762
757 const BoundNetLog& MockClientSocket::NetLog() const { 763 const BoundNetLog& MockClientSocket::NetLog() const {
758 return net_log_; 764 return net_log_;
759 } 765 }
760 766
767 bool MockClientSocket::InSessionCache() const {
768 NOTIMPLEMENTED();
769 return false;
770 }
771
772 void MockClientSocket::SetHandshakeCompletionCallback(const base::Closure& cb) {
773 NOTIMPLEMENTED();
774 }
775
761 void MockClientSocket::GetSSLCertRequestInfo( 776 void MockClientSocket::GetSSLCertRequestInfo(
762 SSLCertRequestInfo* cert_request_info) { 777 SSLCertRequestInfo* cert_request_info) {
763 } 778 }
764 779
765 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, 780 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label,
766 bool has_context, 781 bool has_context,
767 const base::StringPiece& context, 782 const base::StringPiece& context,
768 unsigned char* out, 783 unsigned char* out,
769 unsigned int outlen) { 784 unsigned int outlen) {
770 memset(out, 'A', outlen); 785 memset(out, 'A', outlen);
771 return OK; 786 return OK;
772 } 787 }
773 788
774 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { 789 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) {
775 out->assign(MockClientSocket::kTlsUnique); 790 out->assign(MockClientSocket::kTlsUnique);
776 return OK; 791 return OK;
777 } 792 }
778 793
779 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const { 794 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const {
780 NOTREACHED(); 795 NOTREACHED();
781 return NULL; 796 return NULL;
782 } 797 }
783 798
799 void MockSSLClientSocket::RestartPausedConnect() {
wtc 2014/07/29 20:59:33 Move this method to the MockSSLClientSocket sectio
mshelley 2014/07/29 21:40:12 Done.
800 DCHECK(data_->should_block_on_connect);
801 DCHECK_EQ(next_connect_state_, STATE_SSL_CONNECT_COMPLETE);
802 OnIOComplete(data_->connect.result);
803 }
804
784 SSLClientSocket::NextProtoStatus 805 SSLClientSocket::NextProtoStatus
785 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { 806 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) {
786 proto->clear(); 807 proto->clear();
787 server_protos->clear(); 808 server_protos->clear();
788 return SSLClientSocket::kNextProtoUnsupported; 809 return SSLClientSocket::kNextProtoUnsupported;
789 } 810 }
790 811
791 scoped_refptr<X509Certificate> 812 scoped_refptr<X509Certificate>
792 MockClientSocket::GetUnverifiedServerCertificateChain() const { 813 MockClientSocket::GetUnverifiedServerCertificateChain() const {
793 NOTREACHED(); 814 NOTREACHED();
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after
1291 1312
1292 bool DeterministicMockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { 1313 bool DeterministicMockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
1293 return false; 1314 return false;
1294 } 1315 }
1295 1316
1296 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} 1317 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {}
1297 1318
1298 void DeterministicMockTCPClientSocket::OnConnectComplete( 1319 void DeterministicMockTCPClientSocket::OnConnectComplete(
1299 const MockConnect& data) {} 1320 const MockConnect& data) {}
1300 1321
1301 // static
1302 void MockSSLClientSocket::ConnectCallback(
1303 MockSSLClientSocket* ssl_client_socket,
1304 const CompletionCallback& callback,
1305 int rv) {
1306 if (rv == OK)
1307 ssl_client_socket->connected_ = true;
1308 callback.Run(rv);
1309 }
1310
1311 MockSSLClientSocket::MockSSLClientSocket( 1322 MockSSLClientSocket::MockSSLClientSocket(
1312 scoped_ptr<ClientSocketHandle> transport_socket, 1323 scoped_ptr<ClientSocketHandle> transport_socket,
1313 const HostPortPair& host_port_pair, 1324 const HostPortPair& host_port_pair,
1314 const SSLConfig& ssl_config, 1325 const SSLConfig& ssl_config,
1315 SSLSocketDataProvider* data) 1326 SSLSocketDataProvider* data)
1316 : MockClientSocket( 1327 : MockClientSocket(
1317 // Have to use the right BoundNetLog for LoadTimingInfo regression 1328 // Have to use the right BoundNetLog for LoadTimingInfo regression
1318 // tests. 1329 // tests.
1319 transport_socket->socket()->NetLog()), 1330 transport_socket->socket()->NetLog()),
1320 transport_(transport_socket.Pass()), 1331 transport_(transport_socket.Pass()),
1321 data_(data), 1332 data_(data),
1322 is_npn_state_set_(false), 1333 is_npn_state_set_(false),
1323 new_npn_value_(false), 1334 new_npn_value_(false),
1324 is_protocol_negotiated_set_(false), 1335 is_protocol_negotiated_set_(false),
1325 protocol_negotiated_(kProtoUnknown) { 1336 protocol_negotiated_(kProtoUnknown),
1337 next_connect_state_(STATE_NONE),
1338 weak_factory_(this) {
1326 DCHECK(data_); 1339 DCHECK(data_);
1327 peer_addr_ = data->connect.peer_addr; 1340 peer_addr_ = data->connect.peer_addr;
1328 } 1341 }
1329 1342
1330 MockSSLClientSocket::~MockSSLClientSocket() { 1343 MockSSLClientSocket::~MockSSLClientSocket() {
1331 Disconnect(); 1344 Disconnect();
1332 } 1345 }
1333 1346
1334 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, 1347 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len,
1335 const CompletionCallback& callback) { 1348 const CompletionCallback& callback) {
1336 return transport_->socket()->Read(buf, buf_len, callback); 1349 return transport_->socket()->Read(buf, buf_len, callback);
1337 } 1350 }
1338 1351
1339 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, 1352 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len,
1340 const CompletionCallback& callback) { 1353 const CompletionCallback& callback) {
1341 return transport_->socket()->Write(buf, buf_len, callback); 1354 return transport_->socket()->Write(buf, buf_len, callback);
1342 } 1355 }
1343 1356
1357 int MockSSLClientSocket::DoTCPConnect() {
1358 next_connect_state_ = STATE_TCP_CONNECT_COMPLETE;
1359 return transport_->socket()->Connect(
1360 base::Bind(&MockSSLClientSocket::OnIOComplete, base::Unretained(this)));
1361 }
1362
1363 int MockSSLClientSocket::DoTCPConnectComplete(int result) {
1364 if (result == OK)
1365 next_connect_state_ = STATE_SSL_CONNECT;
1366 return result;
1367 }
1368
1369 int MockSSLClientSocket::DoSSLConnect() {
1370 next_connect_state_ = STATE_SSL_CONNECT_COMPLETE;
1371
1372 // TODO(mshelley) Move this to DoSSLConnectComplete and add a seperate member
1373 // var to check that the connection has started during testing.
1374 if (data_->connect.result == OK)
1375 connected_ = true;
1376
1377 if (data_->should_block_on_connect) {
1378 return ERR_IO_PENDING;
1379 }
wtc 2014/07/29 20:59:34 Omit curly braces.
mshelley 2014/07/29 21:40:12 Done.
1380
1381 if (data_->connect.mode == ASYNC) {
1382 base::MessageLoop::current()->PostTask(
1383 FROM_HERE,
1384 base::Bind(&MockSSLClientSocket::OnIOComplete,
1385 weak_factory_.GetWeakPtr(),
1386 data_->connect.result));
1387 return ERR_IO_PENDING;
1388 }
1389
1390 return data_->connect.result;
1391 }
1392
1393 int MockSSLClientSocket::DoSSLConnectComplete(int result) {
1394 if (!handshake_completion_callback_.is_null())
1395 base::ResetAndReturn(&handshake_completion_callback_).Run();
1396 return result;
1397 }
1398
1344 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { 1399 int MockSSLClientSocket::Connect(const CompletionCallback& callback) {
1345 int rv = transport_->socket()->Connect( 1400 next_connect_state_ = STATE_TCP_CONNECT;
1346 base::Bind(&ConnectCallback, base::Unretained(this), callback)); 1401 int rv = DoConnectLoop(OK);
1347 if (rv == OK) { 1402 if (rv == ERR_IO_PENDING)
1348 if (data_->connect.result == OK) 1403 connect_callback_ = callback;
1349 connected_ = true;
1350 if (data_->connect.mode == ASYNC) {
1351 RunCallbackAsync(callback, data_->connect.result);
1352 return ERR_IO_PENDING;
1353 }
1354 return data_->connect.result;
1355 }
1356 return rv; 1404 return rv;
1357 } 1405 }
1358 1406
1407 void MockSSLClientSocket::OnIOComplete(int result) {
1408 int rv = DoConnectLoop(result);
1409 if (rv != ERR_IO_PENDING) {
1410 DCHECK(!connect_callback_.is_null());
1411 base::ResetAndReturn(&connect_callback_).Run(rv);
1412 }
1413 }
1414
1415 int MockSSLClientSocket::DoConnectLoop(int result) {
1416 DCHECK_NE(next_connect_state_, STATE_NONE);
1417
1418 int rv = result;
1419 do {
1420 ConnectState state = next_connect_state_;
1421 next_connect_state_ = STATE_NONE;
1422 switch (state) {
1423 case STATE_TCP_CONNECT:
1424 DCHECK_EQ(OK, rv);
1425 rv = DoTCPConnect();
1426 break;
1427 case STATE_TCP_CONNECT_COMPLETE:
1428 rv = DoTCPConnectComplete(rv);
1429 case STATE_SSL_CONNECT:
1430 DCHECK_EQ(OK, rv);
1431 rv = DoSSLConnect();
1432 break;
1433 case STATE_SSL_CONNECT_COMPLETE:
1434 rv = DoSSLConnectComplete(rv);
1435 break;
1436 default:
1437 NOTREACHED() << "bad state";
1438 rv = ERR_UNEXPECTED;
1439 break;
1440 }
1441 } while (rv != ERR_IO_PENDING && next_connect_state_ != STATE_NONE);
1442
1443 return rv;
1444 }
1445
1359 void MockSSLClientSocket::Disconnect() { 1446 void MockSSLClientSocket::Disconnect() {
wtc 2014/07/29 20:59:33 Please invalidate the weak pointers here: weak_
mshelley 2014/07/29 21:40:12 Done.
1360 MockClientSocket::Disconnect(); 1447 MockClientSocket::Disconnect();
1361 if (transport_->socket() != NULL) 1448 if (transport_->socket() != NULL)
1362 transport_->socket()->Disconnect(); 1449 transport_->socket()->Disconnect();
1363 } 1450 }
1364 1451
1365 bool MockSSLClientSocket::IsConnected() const { 1452 bool MockSSLClientSocket::IsConnected() const {
1366 return transport_->socket()->IsConnected(); 1453 return transport_->socket()->IsConnected() && connected_;
1367 } 1454 }
1368 1455
1369 bool MockSSLClientSocket::WasEverUsed() const { 1456 bool MockSSLClientSocket::WasEverUsed() const {
1370 return transport_->socket()->WasEverUsed(); 1457 return transport_->socket()->WasEverUsed();
1371 } 1458 }
1372 1459
1373 bool MockSSLClientSocket::UsingTCPFastOpen() const { 1460 bool MockSSLClientSocket::UsingTCPFastOpen() const {
1374 return transport_->socket()->UsingTCPFastOpen(); 1461 return transport_->socket()->UsingTCPFastOpen();
1375 } 1462 }
1376 1463
(...skipping 15 matching lines...) Expand all
1392 DCHECK(cert_request_info); 1479 DCHECK(cert_request_info);
1393 if (data_->cert_request_info) { 1480 if (data_->cert_request_info) {
1394 cert_request_info->host_and_port = 1481 cert_request_info->host_and_port =
1395 data_->cert_request_info->host_and_port; 1482 data_->cert_request_info->host_and_port;
1396 cert_request_info->client_certs = data_->cert_request_info->client_certs; 1483 cert_request_info->client_certs = data_->cert_request_info->client_certs;
1397 } else { 1484 } else {
1398 cert_request_info->Reset(); 1485 cert_request_info->Reset();
1399 } 1486 }
1400 } 1487 }
1401 1488
1489 bool MockSSLClientSocket::InSessionCache() const {
1490 return data_->is_in_session_cache;
1491 }
1492
1493 void MockSSLClientSocket::SetHandshakeCompletionCallback(
1494 const base::Closure& cb) {
1495 handshake_completion_callback_ = cb;
1496 }
wtc 2014/07/29 20:59:34 Move these two methods before the MockSSLClientSoc
mshelley 2014/07/29 21:40:12 Done.
1497
1402 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( 1498 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto(
1403 std::string* proto, std::string* server_protos) { 1499 std::string* proto, std::string* server_protos) {
1404 *proto = data_->next_proto; 1500 *proto = data_->next_proto;
1405 *server_protos = data_->server_protos; 1501 *server_protos = data_->server_protos;
1406 return data_->next_proto_status; 1502 return data_->next_proto_status;
1407 } 1503 }
1408 1504
1409 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { 1505 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) {
1410 is_npn_state_set_ = true; 1506 is_npn_state_set_ = true;
1411 return new_npn_value_ = negotiated; 1507 return new_npn_value_ = negotiated;
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after
1906 2002
1907 const char kSOCKS5OkRequest[] = 2003 const char kSOCKS5OkRequest[] =
1908 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; 2004 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 };
1909 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); 2005 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest);
1910 2006
1911 const char kSOCKS5OkResponse[] = 2007 const char kSOCKS5OkResponse[] =
1912 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; 2008 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 };
1913 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); 2009 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse);
1914 2010
1915 } // namespace net 2011 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698