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

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 comment I missed in the last patch. 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 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 return true;
wtc 2014/07/23 22:53:32 Add a comment to explain why this returns true.
mshelley 2014/07/24 20:37:46 Done.
769 }
770
771 void MockClientSocket::SetHandshakeCompletionCallback(const base::Closure& cb) {
772 NOTIMPLEMENTED();
773 }
774
761 void MockClientSocket::GetSSLCertRequestInfo( 775 void MockClientSocket::GetSSLCertRequestInfo(
762 SSLCertRequestInfo* cert_request_info) { 776 SSLCertRequestInfo* cert_request_info) {
763 } 777 }
764 778
765 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, 779 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label,
766 bool has_context, 780 bool has_context,
767 const base::StringPiece& context, 781 const base::StringPiece& context,
768 unsigned char* out, 782 unsigned char* out,
769 unsigned int outlen) { 783 unsigned int outlen) {
770 memset(out, 'A', outlen); 784 memset(out, 'A', outlen);
771 return OK; 785 return OK;
772 } 786 }
773 787
774 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { 788 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) {
775 out->assign(MockClientSocket::kTlsUnique); 789 out->assign(MockClientSocket::kTlsUnique);
776 return OK; 790 return OK;
777 } 791 }
778 792
779 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const { 793 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const {
780 NOTREACHED(); 794 NOTREACHED();
781 return NULL; 795 return NULL;
782 } 796 }
783 797
798 void MockSSLClientSocket::RestartPausedConnect() {
799 Connect(connect_callback_);
800 }
801
784 SSLClientSocket::NextProtoStatus 802 SSLClientSocket::NextProtoStatus
785 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { 803 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) {
786 proto->clear(); 804 proto->clear();
787 server_protos->clear(); 805 server_protos->clear();
788 return SSLClientSocket::kNextProtoUnsupported; 806 return SSLClientSocket::kNextProtoUnsupported;
789 } 807 }
790 808
791 scoped_refptr<X509Certificate> 809 scoped_refptr<X509Certificate>
792 MockClientSocket::GetUnverifiedServerCertificateChain() const { 810 MockClientSocket::GetUnverifiedServerCertificateChain() const {
793 NOTREACHED(); 811 NOTREACHED();
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after
1292 bool DeterministicMockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { 1310 bool DeterministicMockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
1293 return false; 1311 return false;
1294 } 1312 }
1295 1313
1296 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} 1314 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {}
1297 1315
1298 void DeterministicMockTCPClientSocket::OnConnectComplete( 1316 void DeterministicMockTCPClientSocket::OnConnectComplete(
1299 const MockConnect& data) {} 1317 const MockConnect& data) {}
1300 1318
1301 // static 1319 // static
1302 void MockSSLClientSocket::ConnectCallback( 1320 void MockSSLClientSocket::ConnectCallback(
wtc 2014/07/23 22:53:32 This function seems wrong. This is a pre-existing
mshelley 2014/07/24 20:37:46 Done.
1303 MockSSLClientSocket* ssl_client_socket, 1321 MockSSLClientSocket* ssl_client_socket,
1304 const CompletionCallback& callback, 1322 const CompletionCallback& callback,
1305 int rv) { 1323 int rv) {
1306 if (rv == OK) 1324 if (rv == OK)
1307 ssl_client_socket->connected_ = true; 1325 ssl_client_socket->connected_ = true;
1308 callback.Run(rv); 1326 callback.Run(rv);
1309 } 1327 }
1310 1328
1311 MockSSLClientSocket::MockSSLClientSocket( 1329 MockSSLClientSocket::MockSSLClientSocket(
1312 scoped_ptr<ClientSocketHandle> transport_socket, 1330 scoped_ptr<ClientSocketHandle> transport_socket,
1313 const HostPortPair& host_port_pair, 1331 const HostPortPair& host_port_pair,
1314 const SSLConfig& ssl_config, 1332 const SSLConfig& ssl_config,
1315 SSLSocketDataProvider* data) 1333 SSLSocketDataProvider* data)
1316 : MockClientSocket( 1334 : MockClientSocket(
1317 // Have to use the right BoundNetLog for LoadTimingInfo regression 1335 // Have to use the right BoundNetLog for LoadTimingInfo regression
1318 // tests. 1336 // tests.
1319 transport_socket->socket()->NetLog()), 1337 transport_socket->socket()->NetLog()),
1320 transport_(transport_socket.Pass()), 1338 transport_(transport_socket.Pass()),
1321 data_(data), 1339 data_(data),
1322 is_npn_state_set_(false), 1340 is_npn_state_set_(false),
1323 new_npn_value_(false), 1341 new_npn_value_(false),
1324 is_protocol_negotiated_set_(false), 1342 is_protocol_negotiated_set_(false),
1325 protocol_negotiated_(kProtoUnknown) { 1343 protocol_negotiated_(kProtoUnknown),
1344 next_connect_state_(STATE_CONNECT) {
1326 DCHECK(data_); 1345 DCHECK(data_);
1327 peer_addr_ = data->connect.peer_addr; 1346 peer_addr_ = data->connect.peer_addr;
1328 } 1347 }
1329 1348
1330 MockSSLClientSocket::~MockSSLClientSocket() { 1349 MockSSLClientSocket::~MockSSLClientSocket() {
1331 Disconnect(); 1350 Disconnect();
1332 } 1351 }
1333 1352
1334 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, 1353 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len,
1335 const CompletionCallback& callback) { 1354 const CompletionCallback& callback) {
1336 return transport_->socket()->Read(buf, buf_len, callback); 1355 return transport_->socket()->Read(buf, buf_len, callback);
1337 } 1356 }
1338 1357
1339 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, 1358 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len,
1340 const CompletionCallback& callback) { 1359 const CompletionCallback& callback) {
1341 return transport_->socket()->Write(buf, buf_len, callback); 1360 return transport_->socket()->Write(buf, buf_len, callback);
1342 } 1361 }
1343 1362
1344 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { 1363 int MockSSLClientSocket::DoConnect(const CompletionCallback& callback) {
1345 int rv = transport_->socket()->Connect( 1364 int rv = transport_->socket()->Connect(
1346 base::Bind(&ConnectCallback, base::Unretained(this), callback)); 1365 base::Bind(&ConnectCallback, base::Unretained(this), callback));
1347 if (rv == OK) { 1366 next_connect_state_ = STATE_CONNECT_COMPLETE;
wtc 2014/07/23 22:53:32 Move this line after line 1369.
mshelley 2014/07/24 20:37:46 Done.
1367
1368 if (rv != OK)
1369 return rv;
1370
1371 if (data_->should_block_on_connect) {
wtc 2014/07/23 22:53:32 It seems that data_->should_block_on_connect is th
1372 connect_callback_ = callback;
1373 data_->should_block_on_connect = false;
1348 if (data_->connect.result == OK) 1374 if (data_->connect.result == OK)
1349 connected_ = true; 1375 connected_ = true;
wtc 2014/07/23 22:53:32 I think these two lines should be deleted. We shou
mshelley 2014/07/24 20:37:46 The reason I left this here was so that I'd be abl
1376 return ERR_IO_PENDING;
1377 } else {
wtc 2014/07/23 22:53:32 Omit "else" after a return statement.
mshelley 2014/07/24 20:37:46 Done.
1378 return OK;
1379 }
1380 }
1381
1382 int MockSSLClientSocket::DoConnectComplete(const CompletionCallback& callback) {
1383 if (data_->connect.result == OK) {
1384 connected_ = true;
1385 if (!completion_callback_.is_null()) {
1386 // The presence of |success_callback_| indicates that SSL Connect Job
wtc 2014/07/23 22:53:32 success_callback_ => completion_callback_
mshelley 2014/07/24 20:37:46 Done.
1387 // Waiting is enabled.
1388 base::ResetAndReturn(&completion_callback_).Run();
1389 }
1350 if (data_->connect.mode == ASYNC) { 1390 if (data_->connect.mode == ASYNC) {
1351 RunCallbackAsync(callback, data_->connect.result); 1391 RunCallbackAsync(callback, data_->connect.result);
1352 return ERR_IO_PENDING; 1392 return ERR_IO_PENDING;
1353 } 1393 }
1354 return data_->connect.result; 1394 } else if (!completion_callback_.is_null()) {
1395 base::ResetAndReturn(&completion_callback_).Run();
1355 } 1396 }
1397 return data_->connect.result;
1398 }
1399
1400 int MockSSLClientSocket::Connect(const CompletionCallback& callback) {
1401 int rv;
1402 do {
1403 ConnectState state = next_connect_state_;
1404 next_connect_state_ = STATE_NONE;
1405 switch (state) {
1406 case STATE_CONNECT:
1407 rv = DoConnect(callback);
1408 break;
1409 case STATE_CONNECT_COMPLETE:
1410 rv = DoConnectComplete(callback);
1411 break;
1412 default:
1413 NOTREACHED();
1414 break;
1415 }
1416 } while (rv == OK && next_connect_state_ != STATE_NONE);
1417
1356 return rv; 1418 return rv;
1357 } 1419 }
1358 1420
1359 void MockSSLClientSocket::Disconnect() { 1421 void MockSSLClientSocket::Disconnect() {
1360 MockClientSocket::Disconnect(); 1422 MockClientSocket::Disconnect();
1361 if (transport_->socket() != NULL) 1423 if (transport_->socket() != NULL)
1362 transport_->socket()->Disconnect(); 1424 transport_->socket()->Disconnect();
1363 } 1425 }
1364 1426
1365 bool MockSSLClientSocket::IsConnected() const { 1427 bool MockSSLClientSocket::IsConnected() const {
1366 return transport_->socket()->IsConnected(); 1428 return transport_->socket()->IsConnected() && connected_;
1367 } 1429 }
1368 1430
1369 bool MockSSLClientSocket::WasEverUsed() const { 1431 bool MockSSLClientSocket::WasEverUsed() const {
1370 return transport_->socket()->WasEverUsed(); 1432 return transport_->socket()->WasEverUsed();
1371 } 1433 }
1372 1434
1373 bool MockSSLClientSocket::UsingTCPFastOpen() const { 1435 bool MockSSLClientSocket::UsingTCPFastOpen() const {
1374 return transport_->socket()->UsingTCPFastOpen(); 1436 return transport_->socket()->UsingTCPFastOpen();
1375 } 1437 }
1376 1438
(...skipping 15 matching lines...) Expand all
1392 DCHECK(cert_request_info); 1454 DCHECK(cert_request_info);
1393 if (data_->cert_request_info) { 1455 if (data_->cert_request_info) {
1394 cert_request_info->host_and_port = 1456 cert_request_info->host_and_port =
1395 data_->cert_request_info->host_and_port; 1457 data_->cert_request_info->host_and_port;
1396 cert_request_info->client_certs = data_->cert_request_info->client_certs; 1458 cert_request_info->client_certs = data_->cert_request_info->client_certs;
1397 } else { 1459 } else {
1398 cert_request_info->Reset(); 1460 cert_request_info->Reset();
1399 } 1461 }
1400 } 1462 }
1401 1463
1464 bool MockSSLClientSocket::InSessionCache() const {
1465 return data_->is_in_session_cache;
1466 }
1467
1468 void MockSSLClientSocket::SetHandshakeCompletionCallback(
1469 const base::Closure& cb) {
1470 completion_callback_ = cb;
1471 }
1472
1402 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( 1473 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto(
1403 std::string* proto, std::string* server_protos) { 1474 std::string* proto, std::string* server_protos) {
1404 *proto = data_->next_proto; 1475 *proto = data_->next_proto;
1405 *server_protos = data_->server_protos; 1476 *server_protos = data_->server_protos;
1406 return data_->next_proto_status; 1477 return data_->next_proto_status;
1407 } 1478 }
1408 1479
1409 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { 1480 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) {
1410 is_npn_state_set_ = true; 1481 is_npn_state_set_ = true;
1411 return new_npn_value_ = negotiated; 1482 return new_npn_value_ = negotiated;
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after
1906 1977
1907 const char kSOCKS5OkRequest[] = 1978 const char kSOCKS5OkRequest[] =
1908 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; 1979 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 };
1909 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); 1980 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest);
1910 1981
1911 const char kSOCKS5OkResponse[] = 1982 const char kSOCKS5OkResponse[] =
1912 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; 1983 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 };
1913 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); 1984 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse);
1914 1985
1915 } // namespace net 1986 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698