Chromium Code Reviews| 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_in_connect(false), | |
| 283 blocked_in_connect(false), | |
| 284 is_in_session_cache(false) { | |
| 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 Loading... | |
| 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 |
| 711 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; | 718 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; |
| 712 | 719 |
| 713 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) | 720 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) |
| 714 : connected_(false), | 721 : connected_(false), |
| 715 net_log_(net_log), | 722 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); | 758 bool rv = ParseIPLiteralToNumber("192.0.2.33", &ip); |
| 752 CHECK(rv); | 759 CHECK(rv); |
| 753 *address = IPEndPoint(ip, 123); | 760 *address = IPEndPoint(ip, 123); |
| 754 return OK; | 761 return OK; |
| 755 } | 762 } |
| 756 | 763 |
| 757 const BoundNetLog& MockClientSocket::NetLog() const { | 764 const BoundNetLog& MockClientSocket::NetLog() const { |
| 758 return net_log_; | 765 return net_log_; |
| 759 } | 766 } |
| 760 | 767 |
| 768 bool MockClientSocket::InSessionCache() const { | |
| 769 return true; | |
| 770 } | |
| 771 | |
| 772 void MockClientSocket::SetHandshakeSuccessCallback(const base::Closure& cb) { | |
| 773 NOTIMPLEMENTED(); | |
| 774 } | |
| 775 | |
| 776 void MockClientSocket::SetHandshakeFailureCallback(const base::Closure& cb) { | |
| 777 NOTIMPLEMENTED(); | |
| 778 } | |
| 779 | |
| 761 void MockClientSocket::GetSSLCertRequestInfo( | 780 void MockClientSocket::GetSSLCertRequestInfo( |
| 762 SSLCertRequestInfo* cert_request_info) { | 781 SSLCertRequestInfo* cert_request_info) { |
| 763 } | 782 } |
| 764 | 783 |
| 765 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, | 784 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, |
| 766 bool has_context, | 785 bool has_context, |
| 767 const base::StringPiece& context, | 786 const base::StringPiece& context, |
| 768 unsigned char* out, | 787 unsigned char* out, |
| 769 unsigned int outlen) { | 788 unsigned int outlen) { |
| 770 memset(out, 'A', outlen); | 789 memset(out, 'A', outlen); |
| 771 return OK; | 790 return OK; |
| 772 } | 791 } |
| 773 | 792 |
| 774 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { | 793 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { |
| 775 out->assign(MockClientSocket::kTlsUnique); | 794 out->assign(MockClientSocket::kTlsUnique); |
| 776 return OK; | 795 return OK; |
| 777 } | 796 } |
| 778 | 797 |
| 779 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const { | 798 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const { |
| 780 NOTREACHED(); | 799 NOTREACHED(); |
| 781 return NULL; | 800 return NULL; |
| 782 } | 801 } |
| 783 | 802 |
| 803 CompletionCallback MockClientSocket::connect_callback() { | |
| 804 return connect_callback_; | |
| 805 } | |
| 806 | |
| 784 SSLClientSocket::NextProtoStatus | 807 SSLClientSocket::NextProtoStatus |
| 785 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { | 808 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { |
| 786 proto->clear(); | 809 proto->clear(); |
| 787 server_protos->clear(); | 810 server_protos->clear(); |
| 788 return SSLClientSocket::kNextProtoUnsupported; | 811 return SSLClientSocket::kNextProtoUnsupported; |
| 789 } | 812 } |
| 790 | 813 |
| 791 scoped_refptr<X509Certificate> | 814 scoped_refptr<X509Certificate> |
| 792 MockClientSocket::GetUnverifiedServerCertificateChain() const { | 815 MockClientSocket::GetUnverifiedServerCertificateChain() const { |
| 793 NOTREACHED(); | 816 NOTREACHED(); |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1307 ssl_client_socket->connected_ = true; | 1330 ssl_client_socket->connected_ = true; |
| 1308 callback.Run(rv); | 1331 callback.Run(rv); |
| 1309 } | 1332 } |
| 1310 | 1333 |
| 1311 MockSSLClientSocket::MockSSLClientSocket( | 1334 MockSSLClientSocket::MockSSLClientSocket( |
| 1312 scoped_ptr<ClientSocketHandle> transport_socket, | 1335 scoped_ptr<ClientSocketHandle> transport_socket, |
| 1313 const HostPortPair& host_port_pair, | 1336 const HostPortPair& host_port_pair, |
| 1314 const SSLConfig& ssl_config, | 1337 const SSLConfig& ssl_config, |
| 1315 SSLSocketDataProvider* data) | 1338 SSLSocketDataProvider* data) |
| 1316 : MockClientSocket( | 1339 : MockClientSocket( |
| 1317 // Have to use the right BoundNetLog for LoadTimingInfo regression | 1340 // Have to use the right BoundNetLog for LoadTimingInfo regression |
| 1318 // tests. | 1341 // tests. |
| 1319 transport_socket->socket()->NetLog()), | 1342 transport_socket->socket()->NetLog()), |
| 1320 transport_(transport_socket.Pass()), | 1343 transport_(transport_socket.Pass()), |
| 1321 data_(data), | 1344 data_(data), |
| 1322 is_npn_state_set_(false), | 1345 is_npn_state_set_(false), |
| 1323 new_npn_value_(false), | 1346 new_npn_value_(false), |
| 1324 is_protocol_negotiated_set_(false), | 1347 is_protocol_negotiated_set_(false), |
| 1325 protocol_negotiated_(kProtoUnknown) { | 1348 protocol_negotiated_(kProtoUnknown) { |
| 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::Connect(const CompletionCallback& callback) { |
| 1345 int rv = transport_->socket()->Connect( | 1368 int rv; |
| 1346 base::Bind(&ConnectCallback, base::Unretained(this), callback)); | 1369 if (!data_->blocked_in_connect) { |
|
Ryan Sleevi
2014/07/18 22:01:16
When would this ever be true in a well-behaved cli
| |
| 1370 rv = transport_->socket()->Connect( | |
| 1371 base::Bind(&ConnectCallback, base::Unretained(this), callback)); | |
| 1372 } else { | |
| 1373 rv = OK; | |
| 1374 } | |
| 1375 | |
| 1347 if (rv == OK) { | 1376 if (rv == OK) { |
| 1348 if (data_->connect.result == OK) | 1377 if (data_->connect.result == OK) { |
| 1349 connected_ = true; | 1378 connected_ = true; |
| 1350 if (data_->connect.mode == ASYNC) { | 1379 if (data_->should_block_in_connect) { |
| 1351 RunCallbackAsync(callback, data_->connect.result); | 1380 connect_callback_ = callback; |
| 1352 return ERR_IO_PENDING; | 1381 data_->should_block_in_connect = false; |
| 1382 data_->blocked_in_connect = true; | |
| 1383 return ERR_IO_PENDING; | |
|
Ryan Sleevi
2014/07/18 22:01:16
This doesn't look right to me.
We don't need to e
mshelley
2014/07/21 23:00:07
Done.
| |
| 1384 } else if (!success_callback_.is_null()) { | |
| 1385 // The presence of |success_callback_| indicates that SSL Connect Job | |
| 1386 // Waiting is enabled. | |
| 1387 base::ResetAndReturn(&success_callback_).Run(); | |
| 1388 } | |
| 1389 if (data_->connect.mode == ASYNC) { | |
| 1390 RunCallbackAsync(callback, data_->connect.result); | |
| 1391 return ERR_IO_PENDING; | |
| 1392 } | |
| 1393 } else if (!error_callback_.is_null()) { | |
| 1394 if (data_->should_block_in_connect) { | |
| 1395 connect_callback_ = callback; | |
| 1396 data_->should_block_in_connect = false; | |
| 1397 data_->blocked_in_connect = true; | |
| 1398 return ERR_IO_PENDING; | |
| 1399 } | |
| 1400 base::ResetAndReturn(&error_callback_).Run(); | |
|
Ryan Sleevi
2014/07/18 22:01:16
We may want to restructure this all into a more de
mshelley
2014/07/21 23:00:07
Done.
| |
| 1353 } | 1401 } |
| 1354 return data_->connect.result; | 1402 return data_->connect.result; |
| 1355 } | 1403 } |
| 1356 return rv; | 1404 return rv; |
| 1357 } | 1405 } |
| 1358 | 1406 |
| 1359 void MockSSLClientSocket::Disconnect() { | 1407 void MockSSLClientSocket::Disconnect() { |
| 1360 MockClientSocket::Disconnect(); | 1408 MockClientSocket::Disconnect(); |
| 1361 if (transport_->socket() != NULL) | 1409 if (transport_->socket() != NULL) |
| 1362 transport_->socket()->Disconnect(); | 1410 transport_->socket()->Disconnect(); |
| 1363 } | 1411 } |
| 1364 | 1412 |
| 1365 bool MockSSLClientSocket::IsConnected() const { | 1413 bool MockSSLClientSocket::IsConnected() const { |
| 1366 return transport_->socket()->IsConnected(); | 1414 return transport_->socket()->IsConnected() && connected_; |
| 1367 } | 1415 } |
| 1368 | 1416 |
| 1369 bool MockSSLClientSocket::WasEverUsed() const { | 1417 bool MockSSLClientSocket::WasEverUsed() const { |
| 1370 return transport_->socket()->WasEverUsed(); | 1418 return transport_->socket()->WasEverUsed(); |
| 1371 } | 1419 } |
| 1372 | 1420 |
| 1373 bool MockSSLClientSocket::UsingTCPFastOpen() const { | 1421 bool MockSSLClientSocket::UsingTCPFastOpen() const { |
| 1374 return transport_->socket()->UsingTCPFastOpen(); | 1422 return transport_->socket()->UsingTCPFastOpen(); |
| 1375 } | 1423 } |
| 1376 | 1424 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 1392 DCHECK(cert_request_info); | 1440 DCHECK(cert_request_info); |
| 1393 if (data_->cert_request_info) { | 1441 if (data_->cert_request_info) { |
| 1394 cert_request_info->host_and_port = | 1442 cert_request_info->host_and_port = |
| 1395 data_->cert_request_info->host_and_port; | 1443 data_->cert_request_info->host_and_port; |
| 1396 cert_request_info->client_certs = data_->cert_request_info->client_certs; | 1444 cert_request_info->client_certs = data_->cert_request_info->client_certs; |
| 1397 } else { | 1445 } else { |
| 1398 cert_request_info->Reset(); | 1446 cert_request_info->Reset(); |
| 1399 } | 1447 } |
| 1400 } | 1448 } |
| 1401 | 1449 |
| 1450 bool MockSSLClientSocket::InSessionCache() const { | |
| 1451 return data_->is_in_session_cache; | |
| 1452 } | |
| 1453 | |
| 1454 void MockSSLClientSocket::SetHandshakeSuccessCallback(const base::Closure& cb) { | |
| 1455 success_callback_ = cb; | |
| 1456 } | |
| 1457 | |
| 1458 void MockSSLClientSocket::SetHandshakeFailureCallback(const base::Closure& cb) { | |
| 1459 error_callback_ = cb; | |
| 1460 } | |
| 1461 | |
| 1402 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( | 1462 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( |
| 1403 std::string* proto, std::string* server_protos) { | 1463 std::string* proto, std::string* server_protos) { |
| 1404 *proto = data_->next_proto; | 1464 *proto = data_->next_proto; |
| 1405 *server_protos = data_->server_protos; | 1465 *server_protos = data_->server_protos; |
| 1406 return data_->next_proto_status; | 1466 return data_->next_proto_status; |
| 1407 } | 1467 } |
| 1408 | 1468 |
| 1409 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { | 1469 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { |
| 1410 is_npn_state_set_ = true; | 1470 is_npn_state_set_ = true; |
| 1411 return new_npn_value_ = negotiated; | 1471 return new_npn_value_ = negotiated; |
| (...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1906 | 1966 |
| 1907 const char kSOCKS5OkRequest[] = | 1967 const char kSOCKS5OkRequest[] = |
| 1908 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1968 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
| 1909 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1969 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
| 1910 | 1970 |
| 1911 const char kSOCKS5OkResponse[] = | 1971 const char kSOCKS5OkResponse[] = |
| 1912 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1972 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 1913 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1973 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
| 1914 | 1974 |
| 1915 } // namespace net | 1975 } // namespace net |
| OLD | NEW |