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 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); |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1290 | 1305 |
1291 bool DeterministicMockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { | 1306 bool DeterministicMockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
1292 return false; | 1307 return false; |
1293 } | 1308 } |
1294 | 1309 |
1295 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} | 1310 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} |
1296 | 1311 |
1297 void DeterministicMockTCPClientSocket::OnConnectComplete( | 1312 void DeterministicMockTCPClientSocket::OnConnectComplete( |
1298 const MockConnect& data) {} | 1313 const MockConnect& data) {} |
1299 | 1314 |
1300 // static | |
1301 void MockSSLClientSocket::ConnectCallback( | |
1302 MockSSLClientSocket* ssl_client_socket, | |
1303 const CompletionCallback& callback, | |
1304 int rv) { | |
1305 if (rv == OK) | |
1306 ssl_client_socket->connected_ = true; | |
1307 callback.Run(rv); | |
1308 } | |
1309 | |
1310 MockSSLClientSocket::MockSSLClientSocket( | 1315 MockSSLClientSocket::MockSSLClientSocket( |
1311 scoped_ptr<ClientSocketHandle> transport_socket, | 1316 scoped_ptr<ClientSocketHandle> transport_socket, |
1312 const HostPortPair& host_port_pair, | 1317 const HostPortPair& host_port_pair, |
1313 const SSLConfig& ssl_config, | 1318 const SSLConfig& ssl_config, |
1314 SSLSocketDataProvider* data) | 1319 SSLSocketDataProvider* data) |
1315 : MockClientSocket( | 1320 : MockClientSocket( |
1316 // Have to use the right BoundNetLog for LoadTimingInfo regression | 1321 // Have to use the right BoundNetLog for LoadTimingInfo regression |
1317 // tests. | 1322 // tests. |
1318 transport_socket->socket()->NetLog()), | 1323 transport_socket->socket()->NetLog()), |
1319 transport_(transport_socket.Pass()), | 1324 transport_(transport_socket.Pass()), |
1320 data_(data), | 1325 data_(data), |
1321 is_npn_state_set_(false), | 1326 is_npn_state_set_(false), |
1322 new_npn_value_(false), | 1327 new_npn_value_(false), |
1323 is_protocol_negotiated_set_(false), | 1328 is_protocol_negotiated_set_(false), |
1324 protocol_negotiated_(kProtoUnknown) { | 1329 protocol_negotiated_(kProtoUnknown), |
| 1330 next_connect_state_(STATE_NONE), |
| 1331 weak_factory_(this), |
| 1332 reached_connect_(false) { |
1325 DCHECK(data_); | 1333 DCHECK(data_); |
1326 peer_addr_ = data->connect.peer_addr; | 1334 peer_addr_ = data->connect.peer_addr; |
1327 } | 1335 } |
1328 | 1336 |
1329 MockSSLClientSocket::~MockSSLClientSocket() { | 1337 MockSSLClientSocket::~MockSSLClientSocket() { |
1330 Disconnect(); | 1338 Disconnect(); |
1331 } | 1339 } |
1332 | 1340 |
1333 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, | 1341 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, |
1334 const CompletionCallback& callback) { | 1342 const CompletionCallback& callback) { |
1335 return transport_->socket()->Read(buf, buf_len, callback); | 1343 return transport_->socket()->Read(buf, buf_len, callback); |
1336 } | 1344 } |
1337 | 1345 |
1338 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, | 1346 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, |
1339 const CompletionCallback& callback) { | 1347 const CompletionCallback& callback) { |
1340 return transport_->socket()->Write(buf, buf_len, callback); | 1348 return transport_->socket()->Write(buf, buf_len, callback); |
1341 } | 1349 } |
1342 | 1350 |
1343 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { | 1351 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { |
1344 int rv = transport_->socket()->Connect( | 1352 next_connect_state_ = STATE_TRANSPORT_CONNECT; |
1345 base::Bind(&ConnectCallback, base::Unretained(this), callback)); | 1353 reached_connect_ = true; |
1346 if (rv == OK) { | 1354 int rv = DoConnectLoop(OK); |
1347 if (data_->connect.result == OK) | 1355 if (rv == ERR_IO_PENDING) |
1348 connected_ = true; | 1356 connect_callback_ = callback; |
1349 if (data_->connect.mode == ASYNC) { | |
1350 RunCallbackAsync(callback, data_->connect.result); | |
1351 return ERR_IO_PENDING; | |
1352 } | |
1353 return data_->connect.result; | |
1354 } | |
1355 return rv; | 1357 return rv; |
1356 } | 1358 } |
1357 | 1359 |
1358 void MockSSLClientSocket::Disconnect() { | 1360 void MockSSLClientSocket::Disconnect() { |
| 1361 weak_factory_.InvalidateWeakPtrs(); |
1359 MockClientSocket::Disconnect(); | 1362 MockClientSocket::Disconnect(); |
1360 if (transport_->socket() != NULL) | 1363 if (transport_->socket() != NULL) |
1361 transport_->socket()->Disconnect(); | 1364 transport_->socket()->Disconnect(); |
1362 } | 1365 } |
1363 | 1366 |
1364 bool MockSSLClientSocket::IsConnected() const { | 1367 bool MockSSLClientSocket::IsConnected() const { |
1365 return transport_->socket()->IsConnected(); | 1368 return transport_->socket()->IsConnected() && connected_; |
1366 } | 1369 } |
1367 | 1370 |
1368 bool MockSSLClientSocket::WasEverUsed() const { | 1371 bool MockSSLClientSocket::WasEverUsed() const { |
1369 return transport_->socket()->WasEverUsed(); | 1372 return transport_->socket()->WasEverUsed(); |
1370 } | 1373 } |
1371 | 1374 |
1372 bool MockSSLClientSocket::UsingTCPFastOpen() const { | 1375 bool MockSSLClientSocket::UsingTCPFastOpen() const { |
1373 return transport_->socket()->UsingTCPFastOpen(); | 1376 return transport_->socket()->UsingTCPFastOpen(); |
1374 } | 1377 } |
1375 | 1378 |
1376 int MockSSLClientSocket::GetPeerAddress(IPEndPoint* address) const { | 1379 int MockSSLClientSocket::GetPeerAddress(IPEndPoint* address) const { |
1377 return transport_->socket()->GetPeerAddress(address); | 1380 return transport_->socket()->GetPeerAddress(address); |
1378 } | 1381 } |
1379 | 1382 |
1380 bool MockSSLClientSocket::GetSSLInfo(SSLInfo* ssl_info) { | 1383 bool MockSSLClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
1381 ssl_info->Reset(); | 1384 ssl_info->Reset(); |
1382 ssl_info->cert = data_->cert; | 1385 ssl_info->cert = data_->cert; |
1383 ssl_info->client_cert_sent = data_->client_cert_sent; | 1386 ssl_info->client_cert_sent = data_->client_cert_sent; |
1384 ssl_info->channel_id_sent = data_->channel_id_sent; | 1387 ssl_info->channel_id_sent = data_->channel_id_sent; |
1385 ssl_info->connection_status = data_->connection_status; | 1388 ssl_info->connection_status = data_->connection_status; |
1386 return true; | 1389 return true; |
1387 } | 1390 } |
1388 | 1391 |
| 1392 bool MockSSLClientSocket::InSessionCache() const { |
| 1393 return data_->is_in_session_cache; |
| 1394 } |
| 1395 |
| 1396 void MockSSLClientSocket::SetHandshakeCompletionCallback( |
| 1397 const base::Closure& cb) { |
| 1398 handshake_completion_callback_ = cb; |
| 1399 } |
| 1400 |
1389 void MockSSLClientSocket::GetSSLCertRequestInfo( | 1401 void MockSSLClientSocket::GetSSLCertRequestInfo( |
1390 SSLCertRequestInfo* cert_request_info) { | 1402 SSLCertRequestInfo* cert_request_info) { |
1391 DCHECK(cert_request_info); | 1403 DCHECK(cert_request_info); |
1392 if (data_->cert_request_info) { | 1404 if (data_->cert_request_info) { |
1393 cert_request_info->host_and_port = | 1405 cert_request_info->host_and_port = |
1394 data_->cert_request_info->host_and_port; | 1406 data_->cert_request_info->host_and_port; |
1395 cert_request_info->client_certs = data_->cert_request_info->client_certs; | 1407 cert_request_info->client_certs = data_->cert_request_info->client_certs; |
1396 } else { | 1408 } else { |
1397 cert_request_info->Reset(); | 1409 cert_request_info->Reset(); |
1398 } | 1410 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1440 } | 1452 } |
1441 | 1453 |
1442 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { | 1454 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { |
1443 NOTIMPLEMENTED(); | 1455 NOTIMPLEMENTED(); |
1444 } | 1456 } |
1445 | 1457 |
1446 void MockSSLClientSocket::OnConnectComplete(const MockConnect& data) { | 1458 void MockSSLClientSocket::OnConnectComplete(const MockConnect& data) { |
1447 NOTIMPLEMENTED(); | 1459 NOTIMPLEMENTED(); |
1448 } | 1460 } |
1449 | 1461 |
| 1462 void MockSSLClientSocket::RestartPausedConnect() { |
| 1463 DCHECK(data_->should_block_on_connect); |
| 1464 DCHECK_EQ(next_connect_state_, STATE_SSL_CONNECT_COMPLETE); |
| 1465 OnIOComplete(data_->connect.result); |
| 1466 } |
| 1467 |
| 1468 void MockSSLClientSocket::OnIOComplete(int result) { |
| 1469 int rv = DoConnectLoop(result); |
| 1470 if (rv != ERR_IO_PENDING) |
| 1471 base::ResetAndReturn(&connect_callback_).Run(rv); |
| 1472 } |
| 1473 |
| 1474 int MockSSLClientSocket::DoConnectLoop(int result) { |
| 1475 DCHECK_NE(next_connect_state_, STATE_NONE); |
| 1476 |
| 1477 int rv = result; |
| 1478 do { |
| 1479 ConnectState state = next_connect_state_; |
| 1480 next_connect_state_ = STATE_NONE; |
| 1481 switch (state) { |
| 1482 case STATE_TRANSPORT_CONNECT: |
| 1483 rv = DoTransportConnect(); |
| 1484 break; |
| 1485 case STATE_TRANSPORT_CONNECT_COMPLETE: |
| 1486 rv = DoTransportConnectComplete(rv); |
| 1487 break; |
| 1488 case STATE_SSL_CONNECT: |
| 1489 rv = DoSSLConnect(); |
| 1490 break; |
| 1491 case STATE_SSL_CONNECT_COMPLETE: |
| 1492 rv = DoSSLConnectComplete(rv); |
| 1493 break; |
| 1494 default: |
| 1495 NOTREACHED() << "bad state"; |
| 1496 rv = ERR_UNEXPECTED; |
| 1497 break; |
| 1498 } |
| 1499 } while (rv != ERR_IO_PENDING && next_connect_state_ != STATE_NONE); |
| 1500 |
| 1501 return rv; |
| 1502 } |
| 1503 |
| 1504 int MockSSLClientSocket::DoTransportConnect() { |
| 1505 next_connect_state_ = STATE_TRANSPORT_CONNECT_COMPLETE; |
| 1506 return transport_->socket()->Connect( |
| 1507 base::Bind(&MockSSLClientSocket::OnIOComplete, base::Unretained(this))); |
| 1508 } |
| 1509 |
| 1510 int MockSSLClientSocket::DoTransportConnectComplete(int result) { |
| 1511 if (result == OK) |
| 1512 next_connect_state_ = STATE_SSL_CONNECT; |
| 1513 return result; |
| 1514 } |
| 1515 |
| 1516 int MockSSLClientSocket::DoSSLConnect() { |
| 1517 next_connect_state_ = STATE_SSL_CONNECT_COMPLETE; |
| 1518 |
| 1519 if (data_->should_block_on_connect) |
| 1520 return ERR_IO_PENDING; |
| 1521 |
| 1522 if (data_->connect.mode == ASYNC) { |
| 1523 base::MessageLoop::current()->PostTask( |
| 1524 FROM_HERE, |
| 1525 base::Bind(&MockSSLClientSocket::OnIOComplete, |
| 1526 weak_factory_.GetWeakPtr(), |
| 1527 data_->connect.result)); |
| 1528 return ERR_IO_PENDING; |
| 1529 } |
| 1530 |
| 1531 return data_->connect.result; |
| 1532 } |
| 1533 |
| 1534 int MockSSLClientSocket::DoSSLConnectComplete(int result) { |
| 1535 if (result == OK) |
| 1536 connected_ = true; |
| 1537 |
| 1538 if (!handshake_completion_callback_.is_null()) |
| 1539 base::ResetAndReturn(&handshake_completion_callback_).Run(); |
| 1540 return result; |
| 1541 } |
| 1542 |
1450 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, | 1543 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, |
1451 net::NetLog* net_log) | 1544 net::NetLog* net_log) |
1452 : connected_(false), | 1545 : connected_(false), |
1453 data_(data), | 1546 data_(data), |
1454 read_offset_(0), | 1547 read_offset_(0), |
1455 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), | 1548 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), |
1456 need_read_data_(true), | 1549 need_read_data_(true), |
1457 source_port_(123), | 1550 source_port_(123), |
1458 pending_buf_(NULL), | 1551 pending_buf_(NULL), |
1459 pending_buf_len_(0), | 1552 pending_buf_len_(0), |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1904 | 1997 |
1905 const char kSOCKS5OkRequest[] = | 1998 const char kSOCKS5OkRequest[] = |
1906 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1999 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
1907 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 2000 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
1908 | 2001 |
1909 const char kSOCKS5OkResponse[] = | 2002 const char kSOCKS5OkResponse[] = |
1910 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 2003 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
1911 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 2004 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
1912 | 2005 |
1913 } // namespace net | 2006 } // namespace net |
OLD | NEW |