| 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 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 return true; |
| 769 } |
| 770 |
| 771 void MockClientSocket::SetHandshakeSuccessCallback(const base::Closure& cb) { |
| 772 NOTIMPLEMENTED(); |
| 773 } |
| 774 |
| 775 void MockClientSocket::SetHandshakeFailureCallback(const base::Closure& cb) { |
| 776 NOTIMPLEMENTED(); |
| 777 } |
| 778 |
| 761 void MockClientSocket::GetSSLCertRequestInfo( | 779 void MockClientSocket::GetSSLCertRequestInfo( |
| 762 SSLCertRequestInfo* cert_request_info) { | 780 SSLCertRequestInfo* cert_request_info) { |
| 763 } | 781 } |
| 764 | 782 |
| 765 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, | 783 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, |
| 766 bool has_context, | 784 bool has_context, |
| 767 const base::StringPiece& context, | 785 const base::StringPiece& context, |
| 768 unsigned char* out, | 786 unsigned char* out, |
| 769 unsigned int outlen) { | 787 unsigned int outlen) { |
| 770 memset(out, 'A', outlen); | 788 memset(out, 'A', outlen); |
| 771 return OK; | 789 return OK; |
| 772 } | 790 } |
| 773 | 791 |
| 774 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { | 792 int MockClientSocket::GetTLSUniqueChannelBinding(std::string* out) { |
| 775 out->assign(MockClientSocket::kTlsUnique); | 793 out->assign(MockClientSocket::kTlsUnique); |
| 776 return OK; | 794 return OK; |
| 777 } | 795 } |
| 778 | 796 |
| 779 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const { | 797 ServerBoundCertService* MockClientSocket::GetServerBoundCertService() const { |
| 780 NOTREACHED(); | 798 NOTREACHED(); |
| 781 return NULL; | 799 return NULL; |
| 782 } | 800 } |
| 783 | 801 |
| 802 void MockSSLClientSocket::RestartPausedConnect() { |
| 803 Connect(connect_callback_); |
| 804 } |
| 805 |
| 784 SSLClientSocket::NextProtoStatus | 806 SSLClientSocket::NextProtoStatus |
| 785 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { | 807 MockClientSocket::GetNextProto(std::string* proto, std::string* server_protos) { |
| 786 proto->clear(); | 808 proto->clear(); |
| 787 server_protos->clear(); | 809 server_protos->clear(); |
| 788 return SSLClientSocket::kNextProtoUnsupported; | 810 return SSLClientSocket::kNextProtoUnsupported; |
| 789 } | 811 } |
| 790 | 812 |
| 791 scoped_refptr<X509Certificate> | 813 scoped_refptr<X509Certificate> |
| 792 MockClientSocket::GetUnverifiedServerCertificateChain() const { | 814 MockClientSocket::GetUnverifiedServerCertificateChain() const { |
| 793 NOTREACHED(); | 815 NOTREACHED(); |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1307 ssl_client_socket->connected_ = true; | 1329 ssl_client_socket->connected_ = true; |
| 1308 callback.Run(rv); | 1330 callback.Run(rv); |
| 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_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::DoConnect(const CompletionCallback& callback) { |
| 1345 int rv = transport_->socket()->Connect( | 1368 int rv = transport_->socket()->Connect( |
| 1346 base::Bind(&ConnectCallback, base::Unretained(this), callback)); | 1369 base::Bind(&ConnectCallback, base::Unretained(this), callback)); |
| 1347 if (rv == OK) { | 1370 next_connect_state_ = STATE_CONNECT_COMPLETE; |
| 1371 |
| 1372 if (rv != OK) |
| 1373 return rv; |
| 1374 |
| 1375 if (data_->should_block_in_connect) { |
| 1376 connect_callback_ = callback; |
| 1377 data_->should_block_in_connect = false; |
| 1348 if (data_->connect.result == OK) | 1378 if (data_->connect.result == OK) |
| 1349 connected_ = true; | 1379 connected_ = true; |
| 1380 return ERR_IO_PENDING; |
| 1381 } else { |
| 1382 return OK; |
| 1383 } |
| 1384 } |
| 1385 |
| 1386 int MockSSLClientSocket::DoConnectComplete(const CompletionCallback& callback) { |
| 1387 if (data_->connect.result == OK) { |
| 1388 connected_ = true; |
| 1389 if (!success_callback_.is_null()) { |
| 1390 // The presence of |success_callback_| indicates that SSL Connect Job |
| 1391 // Waiting is enabled. |
| 1392 base::ResetAndReturn(&success_callback_).Run(); |
| 1393 } |
| 1350 if (data_->connect.mode == ASYNC) { | 1394 if (data_->connect.mode == ASYNC) { |
| 1351 RunCallbackAsync(callback, data_->connect.result); | 1395 RunCallbackAsync(callback, data_->connect.result); |
| 1352 return ERR_IO_PENDING; | 1396 return ERR_IO_PENDING; |
| 1353 } | 1397 } |
| 1354 return data_->connect.result; | 1398 } else if (!error_callback_.is_null()) { |
| 1399 base::ResetAndReturn(&error_callback_).Run(); |
| 1355 } | 1400 } |
| 1401 return data_->connect.result; |
| 1402 } |
| 1403 |
| 1404 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { |
| 1405 int rv; |
| 1406 do { |
| 1407 ConnectState state = next_connect_state_; |
| 1408 next_connect_state_ = STATE_NONE; |
| 1409 switch (state) { |
| 1410 case STATE_CONNECT: |
| 1411 rv = DoConnect(callback); |
| 1412 break; |
| 1413 case STATE_CONNECT_COMPLETE: |
| 1414 rv = DoConnectComplete(callback); |
| 1415 break; |
| 1416 default: |
| 1417 NOTREACHED(); |
| 1418 break; |
| 1419 } |
| 1420 } while (rv == OK && next_connect_state_ != STATE_NONE); |
| 1421 |
| 1356 return rv; | 1422 return rv; |
| 1357 } | 1423 } |
| 1358 | 1424 |
| 1359 void MockSSLClientSocket::Disconnect() { | 1425 void MockSSLClientSocket::Disconnect() { |
| 1360 MockClientSocket::Disconnect(); | 1426 MockClientSocket::Disconnect(); |
| 1361 if (transport_->socket() != NULL) | 1427 if (transport_->socket() != NULL) |
| 1362 transport_->socket()->Disconnect(); | 1428 transport_->socket()->Disconnect(); |
| 1363 } | 1429 } |
| 1364 | 1430 |
| 1365 bool MockSSLClientSocket::IsConnected() const { | 1431 bool MockSSLClientSocket::IsConnected() const { |
| 1366 return transport_->socket()->IsConnected(); | 1432 return transport_->socket()->IsConnected() && connected_; |
| 1367 } | 1433 } |
| 1368 | 1434 |
| 1369 bool MockSSLClientSocket::WasEverUsed() const { | 1435 bool MockSSLClientSocket::WasEverUsed() const { |
| 1370 return transport_->socket()->WasEverUsed(); | 1436 return transport_->socket()->WasEverUsed(); |
| 1371 } | 1437 } |
| 1372 | 1438 |
| 1373 bool MockSSLClientSocket::UsingTCPFastOpen() const { | 1439 bool MockSSLClientSocket::UsingTCPFastOpen() const { |
| 1374 return transport_->socket()->UsingTCPFastOpen(); | 1440 return transport_->socket()->UsingTCPFastOpen(); |
| 1375 } | 1441 } |
| 1376 | 1442 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1392 DCHECK(cert_request_info); | 1458 DCHECK(cert_request_info); |
| 1393 if (data_->cert_request_info) { | 1459 if (data_->cert_request_info) { |
| 1394 cert_request_info->host_and_port = | 1460 cert_request_info->host_and_port = |
| 1395 data_->cert_request_info->host_and_port; | 1461 data_->cert_request_info->host_and_port; |
| 1396 cert_request_info->client_certs = data_->cert_request_info->client_certs; | 1462 cert_request_info->client_certs = data_->cert_request_info->client_certs; |
| 1397 } else { | 1463 } else { |
| 1398 cert_request_info->Reset(); | 1464 cert_request_info->Reset(); |
| 1399 } | 1465 } |
| 1400 } | 1466 } |
| 1401 | 1467 |
| 1468 bool MockSSLClientSocket::InSessionCache() const { |
| 1469 return data_->is_in_session_cache; |
| 1470 } |
| 1471 |
| 1472 void MockSSLClientSocket::SetHandshakeSuccessCallback(const base::Closure& cb) { |
| 1473 success_callback_ = cb; |
| 1474 } |
| 1475 |
| 1476 void MockSSLClientSocket::SetHandshakeFailureCallback(const base::Closure& cb) { |
| 1477 error_callback_ = cb; |
| 1478 } |
| 1479 |
| 1402 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( | 1480 SSLClientSocket::NextProtoStatus MockSSLClientSocket::GetNextProto( |
| 1403 std::string* proto, std::string* server_protos) { | 1481 std::string* proto, std::string* server_protos) { |
| 1404 *proto = data_->next_proto; | 1482 *proto = data_->next_proto; |
| 1405 *server_protos = data_->server_protos; | 1483 *server_protos = data_->server_protos; |
| 1406 return data_->next_proto_status; | 1484 return data_->next_proto_status; |
| 1407 } | 1485 } |
| 1408 | 1486 |
| 1409 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { | 1487 bool MockSSLClientSocket::set_was_npn_negotiated(bool negotiated) { |
| 1410 is_npn_state_set_ = true; | 1488 is_npn_state_set_ = true; |
| 1411 return new_npn_value_ = negotiated; | 1489 return new_npn_value_ = negotiated; |
| (...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1906 | 1984 |
| 1907 const char kSOCKS5OkRequest[] = | 1985 const char kSOCKS5OkRequest[] = |
| 1908 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1986 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
| 1909 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1987 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
| 1910 | 1988 |
| 1911 const char kSOCKS5OkResponse[] = | 1989 const char kSOCKS5OkResponse[] = |
| 1912 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1990 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 1913 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1991 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
| 1914 | 1992 |
| 1915 } // namespace net | 1993 } // namespace net |
| OLD | NEW |