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" | |
14 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
15 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
17 #include "base/time/time.h" | 16 #include "base/time/time.h" |
18 #include "net/base/address_family.h" | 17 #include "net/base/address_family.h" |
19 #include "net/base/address_list.h" | 18 #include "net/base/address_list.h" |
20 #include "net/base/auth.h" | 19 #include "net/base/auth.h" |
21 #include "net/base/load_timing_info.h" | 20 #include "net/base/load_timing_info.h" |
22 #include "net/http/http_network_session.h" | 21 #include "net/http/http_network_session.h" |
23 #include "net/http/http_request_headers.h" | 22 #include "net/http/http_request_headers.h" |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 } | 271 } |
273 | 272 |
274 SSLSocketDataProvider::SSLSocketDataProvider(IoMode mode, int result) | 273 SSLSocketDataProvider::SSLSocketDataProvider(IoMode mode, int result) |
275 : connect(mode, result), | 274 : connect(mode, result), |
276 next_proto_status(SSLClientSocket::kNextProtoUnsupported), | 275 next_proto_status(SSLClientSocket::kNextProtoUnsupported), |
277 was_npn_negotiated(false), | 276 was_npn_negotiated(false), |
278 protocol_negotiated(kProtoUnknown), | 277 protocol_negotiated(kProtoUnknown), |
279 client_cert_sent(false), | 278 client_cert_sent(false), |
280 cert_request_info(NULL), | 279 cert_request_info(NULL), |
281 channel_id_sent(false), | 280 channel_id_sent(false), |
282 connection_status(0), | 281 connection_status(0) { |
283 should_pause_on_connect(false), | |
284 is_in_session_cache(false) { | |
285 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, | 282 SSLConnectionStatusSetVersion(SSL_CONNECTION_VERSION_TLS1_2, |
286 &connection_status); | 283 &connection_status); |
287 // Set to TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 | 284 // Set to TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 |
288 SSLConnectionStatusSetCipherSuite(0xcc14, &connection_status); | 285 SSLConnectionStatusSetCipherSuite(0xcc14, &connection_status); |
289 } | 286 } |
290 | 287 |
291 SSLSocketDataProvider::~SSLSocketDataProvider() { | 288 SSLSocketDataProvider::~SSLSocketDataProvider() { |
292 } | 289 } |
293 | 290 |
294 void SSLSocketDataProvider::SetNextProto(NextProto proto) { | 291 void SSLSocketDataProvider::SetNextProto(NextProto proto) { |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
705 const SSLClientSocketContext& context) { | 702 const SSLClientSocketContext& context) { |
706 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); | 703 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); |
707 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { | 704 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { |
708 EXPECT_EQ(next_ssl_data->next_protos_expected_in_ssl_config.size(), | 705 EXPECT_EQ(next_ssl_data->next_protos_expected_in_ssl_config.size(), |
709 ssl_config.next_protos.size()); | 706 ssl_config.next_protos.size()); |
710 EXPECT_TRUE( | 707 EXPECT_TRUE( |
711 std::equal(next_ssl_data->next_protos_expected_in_ssl_config.begin(), | 708 std::equal(next_ssl_data->next_protos_expected_in_ssl_config.begin(), |
712 next_ssl_data->next_protos_expected_in_ssl_config.end(), | 709 next_ssl_data->next_protos_expected_in_ssl_config.end(), |
713 ssl_config.next_protos.begin())); | 710 ssl_config.next_protos.begin())); |
714 } | 711 } |
715 scoped_ptr<MockSSLClientSocket> socket(new MockSSLClientSocket( | 712 return scoped_ptr<SSLClientSocket>(new MockSSLClientSocket( |
716 transport_socket.Pass(), host_and_port, ssl_config, next_ssl_data)); | 713 transport_socket.Pass(), host_and_port, ssl_config, next_ssl_data)); |
717 ssl_client_sockets_.push_back(socket.get()); | |
718 return socket.Pass(); | |
719 } | 714 } |
720 | 715 |
721 void MockClientSocketFactory::ClearSSLSessionCache() { | 716 void MockClientSocketFactory::ClearSSLSessionCache() { |
722 } | 717 } |
723 | 718 |
724 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; | 719 const char MockClientSocket::kTlsUnique[] = "MOCK_TLSUNIQ"; |
725 | 720 |
726 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) | 721 MockClientSocket::MockClientSocket(const BoundNetLog& net_log) |
727 : connected_(false), | 722 : connected_(false), |
728 net_log_(net_log), | 723 net_log_(net_log), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
764 bool rv = ParseIPLiteralToNumber("192.0.2.33", &ip); | 759 bool rv = ParseIPLiteralToNumber("192.0.2.33", &ip); |
765 CHECK(rv); | 760 CHECK(rv); |
766 *address = IPEndPoint(ip, 123); | 761 *address = IPEndPoint(ip, 123); |
767 return OK; | 762 return OK; |
768 } | 763 } |
769 | 764 |
770 const BoundNetLog& MockClientSocket::NetLog() const { | 765 const BoundNetLog& MockClientSocket::NetLog() const { |
771 return net_log_; | 766 return net_log_; |
772 } | 767 } |
773 | 768 |
774 std::string MockClientSocket::GetSessionCacheKey() const { | |
775 NOTIMPLEMENTED(); | |
776 return std::string(); | |
777 } | |
778 | |
779 bool MockClientSocket::InSessionCache() const { | |
780 NOTIMPLEMENTED(); | |
781 return false; | |
782 } | |
783 | |
784 void MockClientSocket::SetHandshakeCompletionCallback(const base::Closure& cb) { | |
785 NOTIMPLEMENTED(); | |
786 } | |
787 | |
788 void MockClientSocket::GetSSLCertRequestInfo( | 769 void MockClientSocket::GetSSLCertRequestInfo( |
789 SSLCertRequestInfo* cert_request_info) { | 770 SSLCertRequestInfo* cert_request_info) { |
790 } | 771 } |
791 | 772 |
792 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, | 773 int MockClientSocket::ExportKeyingMaterial(const base::StringPiece& label, |
793 bool has_context, | 774 bool has_context, |
794 const base::StringPiece& context, | 775 const base::StringPiece& context, |
795 unsigned char* out, | 776 unsigned char* out, |
796 unsigned int outlen) { | 777 unsigned int outlen) { |
797 memset(out, 'A', outlen); | 778 memset(out, 'A', outlen); |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1317 | 1298 |
1318 bool DeterministicMockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { | 1299 bool DeterministicMockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
1319 return false; | 1300 return false; |
1320 } | 1301 } |
1321 | 1302 |
1322 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} | 1303 void DeterministicMockTCPClientSocket::OnReadComplete(const MockRead& data) {} |
1323 | 1304 |
1324 void DeterministicMockTCPClientSocket::OnConnectComplete( | 1305 void DeterministicMockTCPClientSocket::OnConnectComplete( |
1325 const MockConnect& data) {} | 1306 const MockConnect& data) {} |
1326 | 1307 |
1308 // static | |
1309 void MockSSLClientSocket::ConnectCallback( | |
1310 MockSSLClientSocket* ssl_client_socket, | |
1311 const CompletionCallback& callback, | |
1312 int rv) { | |
1313 if (rv == OK) | |
1314 ssl_client_socket->connected_ = true; | |
1315 callback.Run(rv); | |
1316 } | |
1317 | |
1327 MockSSLClientSocket::MockSSLClientSocket( | 1318 MockSSLClientSocket::MockSSLClientSocket( |
1328 scoped_ptr<ClientSocketHandle> transport_socket, | 1319 scoped_ptr<ClientSocketHandle> transport_socket, |
1329 const HostPortPair& host_port_pair, | 1320 const HostPortPair& host_port_pair, |
1330 const SSLConfig& ssl_config, | 1321 const SSLConfig& ssl_config, |
1331 SSLSocketDataProvider* data) | 1322 SSLSocketDataProvider* data) |
1332 : MockClientSocket( | 1323 : MockClientSocket( |
1333 // Have to use the right BoundNetLog for LoadTimingInfo regression | 1324 // Have to use the right BoundNetLog for LoadTimingInfo regression |
1334 // tests. | 1325 // tests. |
1335 transport_socket->socket()->NetLog()), | 1326 transport_socket->socket()->NetLog()), |
davidben
2015/03/09 18:02:36
I think the RHS is correct, right? (Eh, git cl for
| |
1336 transport_(transport_socket.Pass()), | 1327 transport_(transport_socket.Pass()), |
1337 host_port_pair_(host_port_pair), | |
1338 data_(data), | 1328 data_(data), |
1339 is_npn_state_set_(false), | 1329 is_npn_state_set_(false), |
1340 new_npn_value_(false), | 1330 new_npn_value_(false), |
1341 is_protocol_negotiated_set_(false), | 1331 is_protocol_negotiated_set_(false), |
1342 protocol_negotiated_(kProtoUnknown), | 1332 protocol_negotiated_(kProtoUnknown) { |
1343 next_connect_state_(STATE_NONE), | |
1344 reached_connect_(false), | |
1345 weak_factory_(this) { | |
1346 DCHECK(data_); | 1333 DCHECK(data_); |
1347 peer_addr_ = data->connect.peer_addr; | 1334 peer_addr_ = data->connect.peer_addr; |
1348 } | 1335 } |
1349 | 1336 |
1350 MockSSLClientSocket::~MockSSLClientSocket() { | 1337 MockSSLClientSocket::~MockSSLClientSocket() { |
1351 Disconnect(); | 1338 Disconnect(); |
1352 } | 1339 } |
1353 | 1340 |
1354 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, | 1341 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, |
1355 const CompletionCallback& callback) { | 1342 const CompletionCallback& callback) { |
1356 return transport_->socket()->Read(buf, buf_len, callback); | 1343 return transport_->socket()->Read(buf, buf_len, callback); |
1357 } | 1344 } |
1358 | 1345 |
1359 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, | 1346 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, |
1360 const CompletionCallback& callback) { | 1347 const CompletionCallback& callback) { |
1361 return transport_->socket()->Write(buf, buf_len, callback); | 1348 return transport_->socket()->Write(buf, buf_len, callback); |
1362 } | 1349 } |
1363 | 1350 |
1364 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { | 1351 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { |
1365 next_connect_state_ = STATE_SSL_CONNECT; | 1352 int rv = transport_->socket()->Connect( |
1366 reached_connect_ = true; | 1353 base::Bind(&ConnectCallback, base::Unretained(this), callback)); |
davidben
2015/03/09 18:02:36
I'm slightly confused about what happened here. Th
Ryan Sleevi
2015/03/09 21:23:56
If it does, then the underlying transport isn't ad
davidben
2015/03/11 01:05:13
Acknowledged.
| |
1367 int rv = DoConnectLoop(OK); | 1354 if (rv == OK) { |
1368 if (rv == ERR_IO_PENDING) | 1355 if (data_->connect.result == OK) |
1369 connect_callback_ = callback; | 1356 connected_ = true; |
1357 if (data_->connect.mode == ASYNC) { | |
1358 RunCallbackAsync(callback, data_->connect.result); | |
1359 return ERR_IO_PENDING; | |
1360 } | |
1361 return data_->connect.result; | |
1362 } | |
1370 return rv; | 1363 return rv; |
1371 } | 1364 } |
1372 | 1365 |
1373 void MockSSLClientSocket::Disconnect() { | 1366 void MockSSLClientSocket::Disconnect() { |
1374 weak_factory_.InvalidateWeakPtrs(); | |
1375 MockClientSocket::Disconnect(); | 1367 MockClientSocket::Disconnect(); |
1376 if (transport_->socket() != NULL) | 1368 if (transport_->socket() != NULL) |
1377 transport_->socket()->Disconnect(); | 1369 transport_->socket()->Disconnect(); |
1378 } | 1370 } |
1379 | 1371 |
1380 bool MockSSLClientSocket::IsConnected() const { | 1372 bool MockSSLClientSocket::IsConnected() const { |
1381 return transport_->socket()->IsConnected() && connected_; | 1373 return transport_->socket()->IsConnected(); |
1382 } | 1374 } |
1383 | 1375 |
1384 bool MockSSLClientSocket::WasEverUsed() const { | 1376 bool MockSSLClientSocket::WasEverUsed() const { |
1385 return transport_->socket()->WasEverUsed(); | 1377 return transport_->socket()->WasEverUsed(); |
1386 } | 1378 } |
1387 | 1379 |
1388 bool MockSSLClientSocket::UsingTCPFastOpen() const { | 1380 bool MockSSLClientSocket::UsingTCPFastOpen() const { |
1389 return transport_->socket()->UsingTCPFastOpen(); | 1381 return transport_->socket()->UsingTCPFastOpen(); |
1390 } | 1382 } |
1391 | 1383 |
1392 int MockSSLClientSocket::GetPeerAddress(IPEndPoint* address) const { | 1384 int MockSSLClientSocket::GetPeerAddress(IPEndPoint* address) const { |
1393 return transport_->socket()->GetPeerAddress(address); | 1385 return transport_->socket()->GetPeerAddress(address); |
1394 } | 1386 } |
1395 | 1387 |
1396 bool MockSSLClientSocket::GetSSLInfo(SSLInfo* ssl_info) { | 1388 bool MockSSLClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
1397 ssl_info->Reset(); | 1389 ssl_info->Reset(); |
1398 ssl_info->cert = data_->cert; | 1390 ssl_info->cert = data_->cert; |
1399 ssl_info->client_cert_sent = data_->client_cert_sent; | 1391 ssl_info->client_cert_sent = data_->client_cert_sent; |
1400 ssl_info->channel_id_sent = data_->channel_id_sent; | 1392 ssl_info->channel_id_sent = data_->channel_id_sent; |
1401 ssl_info->connection_status = data_->connection_status; | 1393 ssl_info->connection_status = data_->connection_status; |
1402 return true; | 1394 return true; |
1403 } | 1395 } |
1404 | 1396 |
1405 std::string MockSSLClientSocket::GetSessionCacheKey() const { | |
1406 // For the purposes of these tests, |host_and_port| will serve as the | |
1407 // cache key. | |
1408 return host_port_pair_.ToString(); | |
1409 } | |
1410 | |
1411 bool MockSSLClientSocket::InSessionCache() const { | |
1412 return data_->is_in_session_cache; | |
1413 } | |
1414 | |
1415 void MockSSLClientSocket::SetHandshakeCompletionCallback( | |
1416 const base::Closure& cb) { | |
1417 handshake_completion_callback_ = cb; | |
1418 } | |
1419 | |
1420 void MockSSLClientSocket::GetSSLCertRequestInfo( | 1397 void MockSSLClientSocket::GetSSLCertRequestInfo( |
1421 SSLCertRequestInfo* cert_request_info) { | 1398 SSLCertRequestInfo* cert_request_info) { |
1422 DCHECK(cert_request_info); | 1399 DCHECK(cert_request_info); |
1423 if (data_->cert_request_info) { | 1400 if (data_->cert_request_info) { |
1424 cert_request_info->host_and_port = | 1401 cert_request_info->host_and_port = |
1425 data_->cert_request_info->host_and_port; | 1402 data_->cert_request_info->host_and_port; |
1426 cert_request_info->client_certs = data_->cert_request_info->client_certs; | 1403 cert_request_info->client_certs = data_->cert_request_info->client_certs; |
1427 } else { | 1404 } else { |
1428 cert_request_info->Reset(); | 1405 cert_request_info->Reset(); |
1429 } | 1406 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1471 } | 1448 } |
1472 | 1449 |
1473 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { | 1450 void MockSSLClientSocket::OnReadComplete(const MockRead& data) { |
1474 NOTIMPLEMENTED(); | 1451 NOTIMPLEMENTED(); |
1475 } | 1452 } |
1476 | 1453 |
1477 void MockSSLClientSocket::OnConnectComplete(const MockConnect& data) { | 1454 void MockSSLClientSocket::OnConnectComplete(const MockConnect& data) { |
1478 NOTIMPLEMENTED(); | 1455 NOTIMPLEMENTED(); |
1479 } | 1456 } |
1480 | 1457 |
1481 void MockSSLClientSocket::RestartPausedConnect() { | |
1482 DCHECK(data_->should_pause_on_connect); | |
1483 DCHECK_EQ(next_connect_state_, STATE_SSL_CONNECT_COMPLETE); | |
1484 OnIOComplete(data_->connect.result); | |
1485 } | |
1486 | |
1487 void MockSSLClientSocket::OnIOComplete(int result) { | |
1488 int rv = DoConnectLoop(result); | |
1489 if (rv != ERR_IO_PENDING) | |
1490 base::ResetAndReturn(&connect_callback_).Run(rv); | |
1491 } | |
1492 | |
1493 int MockSSLClientSocket::DoConnectLoop(int result) { | |
1494 DCHECK_NE(next_connect_state_, STATE_NONE); | |
1495 | |
1496 int rv = result; | |
1497 do { | |
1498 ConnectState state = next_connect_state_; | |
1499 next_connect_state_ = STATE_NONE; | |
1500 switch (state) { | |
1501 case STATE_SSL_CONNECT: | |
1502 rv = DoSSLConnect(); | |
1503 break; | |
1504 case STATE_SSL_CONNECT_COMPLETE: | |
1505 rv = DoSSLConnectComplete(rv); | |
1506 break; | |
1507 default: | |
1508 NOTREACHED() << "bad state"; | |
1509 rv = ERR_UNEXPECTED; | |
1510 break; | |
1511 } | |
1512 } while (rv != ERR_IO_PENDING && next_connect_state_ != STATE_NONE); | |
1513 | |
1514 return rv; | |
1515 } | |
1516 | |
1517 int MockSSLClientSocket::DoSSLConnect() { | |
1518 next_connect_state_ = STATE_SSL_CONNECT_COMPLETE; | |
1519 | |
1520 if (data_->should_pause_on_connect) | |
1521 return ERR_IO_PENDING; | |
1522 | |
1523 if (data_->connect.mode == ASYNC) { | |
1524 base::MessageLoop::current()->PostTask( | |
1525 FROM_HERE, | |
1526 base::Bind(&MockSSLClientSocket::OnIOComplete, | |
1527 weak_factory_.GetWeakPtr(), | |
1528 data_->connect.result)); | |
1529 return ERR_IO_PENDING; | |
1530 } | |
1531 | |
1532 return data_->connect.result; | |
1533 } | |
1534 | |
1535 int MockSSLClientSocket::DoSSLConnectComplete(int result) { | |
1536 if (result == OK) | |
1537 connected_ = true; | |
1538 | |
1539 if (!handshake_completion_callback_.is_null()) | |
1540 base::ResetAndReturn(&handshake_completion_callback_).Run(); | |
1541 return result; | |
1542 } | |
1543 | |
1544 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, | 1458 MockUDPClientSocket::MockUDPClientSocket(SocketDataProvider* data, |
1545 net::NetLog* net_log) | 1459 net::NetLog* net_log) |
1546 : connected_(false), | 1460 : connected_(false), |
1547 data_(data), | 1461 data_(data), |
1548 read_offset_(0), | 1462 read_offset_(0), |
1549 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), | 1463 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), |
1550 need_read_data_(true), | 1464 need_read_data_(true), |
1551 source_port_(123), | 1465 source_port_(123), |
1552 pending_buf_(NULL), | 1466 pending_buf_(NULL), |
1553 pending_buf_len_(0), | 1467 pending_buf_len_(0), |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2013 | 1927 |
2014 const char kSOCKS5OkRequest[] = | 1928 const char kSOCKS5OkRequest[] = |
2015 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; | 1929 { 0x05, 0x01, 0x00, 0x03, 0x04, 'h', 'o', 's', 't', 0x00, 0x50 }; |
2016 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); | 1930 const int kSOCKS5OkRequestLength = arraysize(kSOCKS5OkRequest); |
2017 | 1931 |
2018 const char kSOCKS5OkResponse[] = | 1932 const char kSOCKS5OkResponse[] = |
2019 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | 1933 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
2020 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); | 1934 const int kSOCKS5OkResponseLength = arraysize(kSOCKS5OkResponse); |
2021 | 1935 |
2022 } // namespace net | 1936 } // namespace net |
OLD | NEW |