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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 Loading... | |
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 Loading... | |
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 // Always returns true to prevent sockets that are not | |
769 // OpenSSL sockets from trying to use SSLConnectJob waiting. | |
770 return true; | |
Ryan Sleevi
2014/07/25 01:36:34
This does seem really weird to me. It also couples
mshelley
2014/07/26 00:58:27
Perhaps it would just be better to put:
Ryan Sleevi
2014/07/26 02:22:32
I think that will work, but you have to use approp
| |
771 } | |
772 | |
773 void MockClientSocket::SetHandshakeCompletionCallback(const base::Closure& cb) { | |
774 NOTIMPLEMENTED(); | |
775 } | |
776 | |
761 void MockClientSocket::GetSSLCertRequestInfo( | 777 void MockClientSocket::GetSSLCertRequestInfo( |
762 SSLCertRequestInfo* cert_request_info) { | 778 SSLCertRequestInfo* cert_request_info) { |
763 } | 779 } |
764 | 780 |
765 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, | 781 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, |
766 bool has_context, | 782 bool has_context, |
767 const base::StringPiece& context, | 783 const base::StringPiece& context, |
768 unsigned char* out, | 784 unsigned char* out, |
769 unsigned int outlen) { | 785 unsigned int outlen) { |
770 memset(out, 'A', outlen); | 786 memset(out, 'A', outlen); |
771 return OK; | 787 return OK; |
772 } | 788 } |
773 | 789 |
774 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { | 790 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { |
775 out->assign(MockClientSocket::kTlsUnique); | 791 out->assign(MockClientSocket::kTlsUnique); |
776 return OK; | 792 return OK; |
777 } | 793 } |
778 | 794 |
779 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const { | 795 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const { |
780 NOTREACHED(); | 796 NOTREACHED(); |
781 return NULL; | 797 return NULL; |
782 } | 798 } |
783 | 799 |
800 void MockSSLClientSocket::RestartPausedConnect() { | |
801 Connect(connect_callback_); | |
802 } | |
803 | |
784 SSLClientSocket::NextProtoStatus | 804 SSLClientSocket::NextProtoStatus |
785 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { | 805 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { |
786 proto->clear(); | 806 proto->clear(); |
787 server_protos->clear(); | 807 server_protos->clear(); |
788 return SSLClientSocket::kNextProtoUnsupported; | 808 return SSLClientSocket::kNextProtoUnsupported; |
789 } | 809 } |
790 | 810 |
791 scoped_refptr<X509Certificate> | 811 scoped_refptr<X509Certificate> |
792 MockClientSocket::GetUnverifiedServerCertificateChain() const { | 812 MockClientSocket::GetUnverifiedServerCertificateChain() const { |
793 NOTREACHED(); | 813 NOTREACHED(); |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1296 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} | 1316 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} |
1297 | 1317 |
1298 void DeterministicMockTCPClientSocket::OnConnectComplete( | 1318 void DeterministicMockTCPClientSocket::OnConnectComplete( |
1299 const MockConnect& data) {} | 1319 const MockConnect& data) {} |
1300 | 1320 |
1301 // static | 1321 // static |
1302 void MockSSLClientSocket::ConnectCallback( | 1322 void MockSSLClientSocket::ConnectCallback( |
1303 MockSSLClientSocket* ssl_client_socket, | 1323 MockSSLClientSocket* ssl_client_socket, |
1304 const CompletionCallback& callback, | 1324 const CompletionCallback& callback, |
1305 int rv) { | 1325 int rv) { |
1306 if (rv == OK) | 1326 if (rv < OK) { |
1307 ssl_client_socket->connected_ = true; | 1327 callback.Run(rv); |
1308 callback.Run(rv); | 1328 return; |
1329 } | |
1330 ssl_client_socket->Connect(callback); | |
1309 } | 1331 } |
1310 | 1332 |
1311 MockSSLClientSocket::MockSSLClientSocket( | 1333 MockSSLClientSocket::MockSSLClientSocket( |
1312 scoped_ptr<ClientSocketHandle> transport_socket, | 1334 scoped_ptr<ClientSocketHandle> transport_socket, |
1313 const HostPortPair& host_port_pair, | 1335 const HostPortPair& host_port_pair, |
1314 const SSLConfig& ssl_config, | 1336 const SSLConfig& ssl_config, |
1315 SSLSocketDataProvider* data) | 1337 SSLSocketDataProvider* data) |
1316 : MockClientSocket( | 1338 : MockClientSocket( |
1317 // Have to use the right BoundNetLog for LoadTimingInfo regression | 1339 // Have to use the right BoundNetLog for LoadTimingInfo regression |
1318 // tests. | 1340 // tests. |
1319 transport_socket->socket()->NetLog()), | 1341 transport_socket->socket()->NetLog()), |
1320 transport_(transport_socket.Pass()), | 1342 transport_(transport_socket.Pass()), |
1321 data_(data), | 1343 data_(data), |
1322 is_npn_state_set_(false), | 1344 is_npn_state_set_(false), |
1323 new_npn_value_(false), | 1345 new_npn_value_(false), |
1324 is_protocol_negotiated_set_(false), | 1346 is_protocol_negotiated_set_(false), |
1325 protocol_negotiated_(kProtoUnknown) { | 1347 protocol_negotiated_(kProtoUnknown), |
1348 next_connect_state_(STATE_TCP_CONNECT) { | |
1326 DCHECK(data_); | 1349 DCHECK(data_); |
1327 peer_addr_ = data->connect.peer_addr; | 1350 peer_addr_ = data->connect.peer_addr; |
1328 } | 1351 } |
1329 | 1352 |
1330 MockSSLClientSocket::~MockSSLClientSocket() { | 1353 MockSSLClientSocket::~MockSSLClientSocket() { |
1331 Disconnect(); | 1354 Disconnect(); |
1332 } | 1355 } |
1333 | 1356 |
1334 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, | 1357 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, |
1335 const CompletionCallback& callback) { | 1358 const CompletionCallback& callback) { |
1336 return transport_->socket()->Read(buf, buf_len, callback); | 1359 return transport_->socket()->Read(buf, buf_len, callback); |
1337 } | 1360 } |
1338 | 1361 |
1339 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, | 1362 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, |
1340 const CompletionCallback& callback) { | 1363 const CompletionCallback& callback) { |
1341 return transport_->socket()->Write(buf, buf_len, callback); | 1364 return transport_->socket()->Write(buf, buf_len, callback); |
1342 } | 1365 } |
1343 | 1366 |
1344 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { | 1367 int MockSSLClientSocket::DoTCPConnect(const CompletionCallback& callback) { |
1368 next_connect_state_ = STATE_SSL_CONNECT; | |
1345 int rv = transport_->socket()->Connect( | 1369 int rv = transport_->socket()->Connect( |
1346 base::Bind(&ConnectCallback, base::Unretained(this), callback)); | 1370 base::Bind(&ConnectCallback, base::Unretained(this), callback)); |
1347 if (rv == OK) { | 1371 return rv; |
1372 } | |
1373 | |
1374 int MockSSLClientSocket::DoSSLConnect(const CompletionCallback& callback) { | |
1375 next_connect_state_ = STATE_SSL_CONNECT_COMPLETE; | |
1376 | |
1377 if (data_->should_block_on_connect) { | |
1378 connect_callback_ = callback; | |
1379 data_->should_block_on_connect = false; | |
1348 if (data_->connect.result == OK) | 1380 if (data_->connect.result == OK) |
1349 connected_ = true; | 1381 connected_ = true; |
1382 return ERR_IO_PENDING; | |
1383 } | |
1384 return OK; | |
1385 } | |
1386 | |
1387 int MockSSLClientSocket::DoSSLConnectComplete( | |
1388 const CompletionCallback& callback) { | |
1389 if (data_->connect.result == OK) { | |
1390 connected_ = true; | |
1391 if (!handshake_completion_callback_.is_null()) { | |
1392 // The presence of |completion_callback_| indicates that SSL Connect Job | |
1393 // Waiting is enabled. | |
1394 base::ResetAndReturn(&handshake_completion_callback_).Run(); | |
1395 } | |
1350 if (data_->connect.mode == ASYNC) { | 1396 if (data_->connect.mode == ASYNC) { |
1351 RunCallbackAsync(callback, data_->connect.result); | 1397 RunCallbackAsync(callback, data_->connect.result); |
1352 return ERR_IO_PENDING; | 1398 return ERR_IO_PENDING; |
1353 } | 1399 } |
1354 return data_->connect.result; | 1400 } else if (!handshake_completion_callback_.is_null()) { |
1401 base::ResetAndReturn(&handshake_completion_callback_).Run(); | |
1355 } | 1402 } |
1403 return data_->connect.result; | |
1404 } | |
1405 | |
1406 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { | |
1407 int rv; | |
Ryan Sleevi
2014/07/25 01:36:34
So this tripped me up when reviewing, and violated
| |
1408 do { | |
1409 ConnectState state = next_connect_state_; | |
1410 next_connect_state_ = STATE_NONE; | |
1411 switch (state) { | |
1412 case STATE_TCP_CONNECT: | |
1413 rv = DoTCPConnect(callback); | |
1414 case STATE_SSL_CONNECT: | |
1415 rv = DoSSLConnect(callback); | |
1416 break; | |
1417 case STATE_SSL_CONNECT_COMPLETE: | |
1418 rv = DoSSLConnectComplete(callback); | |
1419 break; | |
1420 default: | |
1421 NOTREACHED(); | |
1422 break; | |
1423 } | |
1424 } while (rv == OK && next_connect_state_ != STATE_NONE); | |
1425 | |
1356 return rv; | 1426 return rv; |
1357 } | 1427 } |
1358 | 1428 |
1359 void MockSSLClientSocket::Disconnect() { | 1429 void MockSSLClientSocket::Disconnect() { |
1360 MockClientSocket::Disconnect(); | 1430 MockClientSocket::Disconnect(); |
1361 if (transport_->socket() != NULL) | 1431 if (transport_->socket() != NULL) |
1362 transport_->socket()->Disconnect(); | 1432 transport_->socket()->Disconnect(); |
1363 } | 1433 } |
1364 | 1434 |
1365 bool MockSSLClientSocket::IsConnected() const { | 1435 bool MockSSLClientSocket::IsConnected() const { |
1366 return transport_->socket()->IsConnected(); | 1436 return transport_->socket()->IsConnected() && connected_; |
1367 } | 1437 } |
1368 | 1438 |
1369 bool MockSSLClientSocket::WasEverUsed() const { | 1439 bool MockSSLClientSocket::WasEverUsed() const { |
1370 return transport_->socket()->WasEverUsed(); | 1440 return transport_->socket()->WasEverUsed(); |
1371 } | 1441 } |
1372 | 1442 |
1373 bool MockSSLClientSocket::UsingTCPFastOpen() const { | 1443 bool MockSSLClientSocket::UsingTCPFastOpen() const { |
1374 return transport_->socket()->UsingTCPFastOpen(); | 1444 return transport_->socket()->UsingTCPFastOpen(); |
1375 } | 1445 } |
1376 | 1446 |
(...skipping 15 matching lines...) Expand all Loading... | |
1392 DCHECK(cert_request_info); | 1462 DCHECK(cert_request_info); |
1393 if (data_->cert_request_info) { | 1463 if (data_->cert_request_info) { |
1394 cert_request_info->host_and_port = | 1464 cert_request_info->host_and_port = |
1395 data_->cert_request_info->host_and_port; | 1465 data_->cert_request_info->host_and_port; |
1396 cert_request_info->client_certs = data_->cert_request_info->client_certs; | 1466 cert_request_info->client_certs = data_->cert_request_info->client_certs; |
1397 } else { | 1467 } else { |
1398 cert_request_info->Reset(); | 1468 cert_request_info->Reset(); |
1399 } | 1469 } |
1400 } | 1470 } |
1401 | 1471 |
1472 bool MockSSLClientSocket::InSessionCache() const { | |
1473 return data_->is_in_session_cache; | |
1474 } | |
1475 | |
1476 void MockSSLClientSocket::SetHandshakeCompletionCallback( | |
1477 const base::Closure& cb) { | |
1478 handshake_completion_callback_ = cb; | |
1479 } | |
1480 | |
1402 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( | 1481 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( |
1403 std::string* proto, std::string* server_protos) { | 1482 std::string* proto, std::string* server_protos) { |
1404 *proto = data_->next_proto; | 1483 *proto = data_->next_proto; |
1405 *server_protos = data_->server_protos; | 1484 *server_protos = data_->server_protos; |
1406 return data_->next_proto_status; | 1485 return data_->next_proto_status; |
1407 } | 1486 } |
1408 | 1487 |
1409 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { | 1488 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { |
1410 is_npn_state_set_ = true; | 1489 is_npn_state_set_ = true; |
1411 return new_npn_value_ = negotiated; | 1490 return new_npn_value_ = negotiated; |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1906 | 1985 |
1907 const char kSOCKS5OkRequest[] = | 1986 const char kSOCKS5OkRequest[] = |
1908 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1987 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
1909 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1988 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
1910 | 1989 |
1911 const char kSOCKS5OkResponse[] = | 1990 const char kSOCKS5OkResponse[] = |
1912 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1991 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
1913 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1992 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
1914 | 1993 |
1915 } // namespace net | 1994 } // namespace net |
OLD | NEW |