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/ssl_client_socket.h" | 5 #include "net/socket/ssl_client_socket.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
| 14 #include "net/base/net_log_unittest.h" | 14 #include "net/base/net_log_unittest.h" |
| 15 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 16 #include "net/base/test_data_directory.h" | 16 #include "net/base/test_data_directory.h" |
| 17 #include "net/cert/mock_cert_verifier.h" | 17 #include "net/cert/mock_cert_verifier.h" |
| 18 #include "net/cert/test_root_certs.h" | 18 #include "net/cert/test_root_certs.h" |
| 19 #include "net/dns/host_resolver.h" | 19 #include "net/dns/host_resolver.h" |
| 20 #include "net/http/transport_security_state.h" | 20 #include "net/http/transport_security_state.h" |
| 21 #include "net/socket/client_socket_factory.h" | 21 #include "net/socket/client_socket_factory.h" |
| 22 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
| 23 #include "net/socket/socket_test_util.h" | 23 #include "net/socket/socket_test_util.h" |
| 24 #include "net/socket/tcp_client_socket.h" | 24 #include "net/socket/tcp_client_socket.h" |
| 25 #include "net/ssl/default_server_bound_cert_store.h" | |
| 25 #include "net/ssl/ssl_cert_request_info.h" | 26 #include "net/ssl/ssl_cert_request_info.h" |
| 26 #include "net/ssl/ssl_config_service.h" | 27 #include "net/ssl/ssl_config_service.h" |
| 27 #include "net/test/cert_test_util.h" | 28 #include "net/test/cert_test_util.h" |
| 28 #include "net/test/spawned_test_server/spawned_test_server.h" | 29 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #include "testing/platform_test.h" | 31 #include "testing/platform_test.h" |
| 31 | 32 |
| 32 //----------------------------------------------------------------------------- | 33 //----------------------------------------------------------------------------- |
| 33 | 34 |
| 34 namespace net { | 35 namespace net { |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 550 } | 551 } |
| 551 SetResult(result); | 552 SetResult(result); |
| 552 } | 553 } |
| 553 | 554 |
| 554 StreamSocket* socket_; | 555 StreamSocket* socket_; |
| 555 CompletionCallback callback_; | 556 CompletionCallback callback_; |
| 556 | 557 |
| 557 DISALLOW_COPY_AND_ASSIGN(DeleteSocketCallback); | 558 DISALLOW_COPY_AND_ASSIGN(DeleteSocketCallback); |
| 558 }; | 559 }; |
| 559 | 560 |
| 561 // A ServerBoundCertStore that always returns an error when asked for a | |
| 562 // certificate. | |
| 563 class FailingServerBoundCertStore : public ServerBoundCertStore { | |
| 564 virtual int GetServerBoundCert(const std::string& server_identifier, | |
| 565 base::Time* expiration_time, | |
| 566 std::string* private_key_result, | |
| 567 std::string* cert_result, | |
| 568 const GetCertCallback& callback) OVERRIDE { | |
| 569 return ERR_UNEXPECTED; | |
| 570 } | |
| 571 virtual void SetServerBoundCert(const std::string& server_identifier, | |
| 572 base::Time creation_time, | |
| 573 base::Time expiration_time, | |
| 574 const std::string& private_key, | |
| 575 const std::string& cert) OVERRIDE {} | |
| 576 virtual void DeleteServerBoundCert(const std::string& server_identifier, | |
| 577 const base::Closure& completion_callback) | |
| 578 OVERRIDE {} | |
| 579 virtual void DeleteAllCreatedBetween(base::Time delete_begin, | |
| 580 base::Time delete_end, | |
| 581 const base::Closure& completion_callback) | |
| 582 OVERRIDE {} | |
| 583 virtual void DeleteAll(const base::Closure& completion_callback) OVERRIDE {} | |
| 584 virtual void GetAllServerBoundCerts(const GetCertListCallback& callback) | |
| 585 OVERRIDE {} | |
| 586 virtual int GetCertCount() OVERRIDE { return 0; } | |
| 587 virtual void SetForceKeepSessionState() OVERRIDE {} | |
| 588 }; | |
| 589 | |
| 560 class SSLClientSocketTest : public PlatformTest { | 590 class SSLClientSocketTest : public PlatformTest { |
| 561 public: | 591 public: |
| 562 SSLClientSocketTest() | 592 SSLClientSocketTest() |
| 563 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 593 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
| 564 cert_verifier_(new MockCertVerifier), | 594 cert_verifier_(new MockCertVerifier), |
| 565 transport_security_state_(new TransportSecurityState) { | 595 transport_security_state_(new TransportSecurityState) { |
| 566 cert_verifier_->set_default_result(OK); | 596 cert_verifier_->set_default_result(OK); |
| 567 context_.cert_verifier = cert_verifier_.get(); | 597 context_.cert_verifier = cert_verifier_.get(); |
| 568 context_.transport_security_state = transport_security_state_.get(); | 598 context_.transport_security_state = transport_security_state_.get(); |
| 569 } | 599 } |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 706 EXPECT_LT(0, rv); | 736 EXPECT_LT(0, rv); |
| 707 } else { | 737 } else { |
| 708 // False Start is not enabled, so the handshake will not complete because | 738 // False Start is not enabled, so the handshake will not complete because |
| 709 // the server second leg is blocked. | 739 // the server second leg is blocked. |
| 710 base::RunLoop().RunUntilIdle(); | 740 base::RunLoop().RunUntilIdle(); |
| 711 EXPECT_FALSE(callback.have_result()); | 741 EXPECT_FALSE(callback.have_result()); |
| 712 } | 742 } |
| 713 } | 743 } |
| 714 }; | 744 }; |
| 715 | 745 |
| 746 class SSLClientSocketChannelIDTest : public SSLClientSocketTest { | |
|
wtc
2014/05/06 17:56:24
Nit: I think it is a good idea to copy the Connect
haavardm
2014/05/07 13:53:43
I did that to follow the code pattern of the rest
| |
| 747 protected: | |
| 748 void EnabledChannelID() { | |
|
Ryan Sleevi
2014/05/06 18:45:26
these should be "EnableChannelID()" / "EnableFaili
haavardm
2014/05/07 13:53:43
Done.
| |
| 749 cert_service_.reset( | |
| 750 new ServerBoundCertService(new DefaultServerBoundCertStore(NULL), | |
| 751 base::MessageLoopProxy::current())); | |
| 752 context_.server_bound_cert_service = cert_service_.get(); | |
| 753 } | |
| 754 | |
| 755 void EnabledFailingChannelID() { | |
| 756 cert_service_.reset(new ServerBoundCertService( | |
| 757 new FailingServerBoundCertStore(), base::MessageLoopProxy::current())); | |
| 758 context_.server_bound_cert_service = cert_service_.get(); | |
| 759 } | |
| 760 | |
| 761 scoped_ptr<ServerBoundCertService> cert_service_; | |
|
wtc
2014/05/06 17:56:24
The cert_service_ member should be private.
| |
| 762 }; | |
| 763 | |
| 716 //----------------------------------------------------------------------------- | 764 //----------------------------------------------------------------------------- |
| 717 | 765 |
| 718 // LogContainsSSLConnectEndEvent returns true if the given index in the given | 766 // LogContainsSSLConnectEndEvent returns true if the given index in the given |
| 719 // log is an SSL connect end event. The NSS sockets will cork in an attempt to | 767 // log is an SSL connect end event. The NSS sockets will cork in an attempt to |
| 720 // merge the first application data record with the Finished message when false | 768 // merge the first application data record with the Finished message when false |
| 721 // starting. However, in order to avoid the server timing out the handshake, | 769 // starting. However, in order to avoid the server timing out the handshake, |
| 722 // they'll give up waiting for application data and send the Finished after a | 770 // they'll give up waiting for application data and send the Finished after a |
| 723 // timeout. This means that an SSL connect end event may appear as a socket | 771 // timeout. This means that an SSL connect end event may appear as a socket |
| 724 // write. | 772 // write. |
| 725 static bool LogContainsSSLConnectEndEvent( | 773 static bool LogContainsSSLConnectEndEvent( |
| (...skipping 1630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2356 TEST_F(SSLClientSocketFalseStartTest, NoForwardSecrecy) { | 2404 TEST_F(SSLClientSocketFalseStartTest, NoForwardSecrecy) { |
| 2357 SpawnedTestServer::SSLOptions server_options; | 2405 SpawnedTestServer::SSLOptions server_options; |
| 2358 server_options.key_exchanges = | 2406 server_options.key_exchanges = |
| 2359 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_RSA; | 2407 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_RSA; |
| 2360 server_options.enable_npn = true; | 2408 server_options.enable_npn = true; |
| 2361 SSLConfig client_config; | 2409 SSLConfig client_config; |
| 2362 client_config.next_protos.push_back("http/1.1"); | 2410 client_config.next_protos.push_back("http/1.1"); |
| 2363 TestFalseStart(server_options, client_config, false); | 2411 TestFalseStart(server_options, client_config, false); |
| 2364 } | 2412 } |
| 2365 | 2413 |
| 2414 // Connect to a server using channel id. It should allow the connection. | |
| 2415 TEST_F(SSLClientSocketChannelIDTest, SendChannelID) { | |
| 2416 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | |
| 2417 SpawnedTestServer::kLocalhost, | |
| 2418 base::FilePath()); | |
| 2419 | |
| 2420 SpawnedTestServer::SSLOptions ssl_options; | |
|
wtc
2014/05/06 17:56:24
Remove the ssl_options variable in both unit tests
haavardm
2014/05/07 13:53:43
Done.
| |
| 2421 ASSERT_TRUE(test_server.Start()); | |
| 2422 | |
| 2423 AddressList addr; | |
| 2424 ASSERT_TRUE(test_server.GetAddressList(&addr)); | |
| 2425 | |
| 2426 TestCompletionCallback callback; | |
| 2427 scoped_ptr<StreamSocket> transport( | |
| 2428 new TCPClientSocket(addr, NULL, NetLog::Source())); | |
|
wtc
2014/05/06 17:56:24
Just wanted to confirm that you intended to pass N
haavardm
2014/05/07 13:53:43
Yes, that was as intended. Some tests only pass if
| |
| 2429 int rv = transport->Connect(callback.callback()); | |
| 2430 if (rv == ERR_IO_PENDING) | |
| 2431 rv = callback.WaitForResult(); | |
| 2432 EXPECT_EQ(OK, rv); | |
| 2433 | |
| 2434 EnabledChannelID(); | |
| 2435 SSLConfig ssl_config = kDefaultSSLConfig; | |
| 2436 ssl_config.channel_id_enabled = true; | |
| 2437 | |
| 2438 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | |
| 2439 transport.Pass(), test_server.host_port_pair(), kDefaultSSLConfig)); | |
|
wtc
2014/05/06 17:56:24
1. BUG: the variable ssl_config is set but not use
haavardm
2014/05/07 13:53:43
Hm, the evils of copy and paste.. Yes the tests pa
| |
| 2440 | |
| 2441 rv = sock->Connect(callback.callback()); | |
|
wtc
2014/05/06 17:56:24
Just wanted to point out that you omitted them che
| |
| 2442 if (rv == ERR_IO_PENDING) | |
| 2443 rv = callback.WaitForResult(); | |
| 2444 EXPECT_EQ(OK, rv); | |
|
wtc
2014/05/06 17:56:24
Nit: fix indentation.
haavardm
2014/05/07 13:53:43
Done.
| |
| 2445 EXPECT_TRUE(sock->IsConnected()); | |
| 2446 EXPECT_TRUE(sock->WasChannelIDSent()); | |
| 2447 | |
| 2448 sock->Disconnect(); | |
| 2449 EXPECT_FALSE(sock->IsConnected()); | |
| 2450 } | |
| 2451 | |
| 2452 // Connect to a server using channel id but without sending a key. It should | |
| 2453 // fail. | |
| 2454 TEST_F(SSLClientSocketChannelIDTest, FailingChannelID) { | |
| 2455 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | |
| 2456 SpawnedTestServer::kLocalhost, | |
| 2457 base::FilePath()); | |
| 2458 | |
| 2459 SpawnedTestServer::SSLOptions ssl_options; | |
| 2460 ASSERT_TRUE(test_server.Start()); | |
| 2461 | |
| 2462 AddressList addr; | |
| 2463 ASSERT_TRUE(test_server.GetAddressList(&addr)); | |
| 2464 | |
| 2465 TestCompletionCallback callback; | |
| 2466 scoped_ptr<StreamSocket> transport( | |
| 2467 new TCPClientSocket(addr, NULL, NetLog::Source())); | |
| 2468 int rv = transport->Connect(callback.callback()); | |
| 2469 if (rv == ERR_IO_PENDING) | |
| 2470 rv = callback.WaitForResult(); | |
| 2471 EXPECT_EQ(OK, rv); | |
| 2472 | |
| 2473 EnabledFailingChannelID(); | |
| 2474 SSLConfig ssl_config = kDefaultSSLConfig; | |
| 2475 ssl_config.channel_id_enabled = true; | |
| 2476 | |
| 2477 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | |
| 2478 transport.Pass(), test_server.host_port_pair(), kDefaultSSLConfig)); | |
| 2479 | |
| 2480 rv = sock->Connect(callback.callback()); | |
| 2481 if (rv == ERR_IO_PENDING) | |
| 2482 rv = callback.WaitForResult(); | |
| 2483 | |
| 2484 EXPECT_EQ(ERR_UNEXPECTED, rv); | |
| 2485 EXPECT_FALSE(sock->IsConnected()); | |
| 2486 } | |
| 2487 | |
| 2366 } // namespace net | 2488 } // namespace net |
| OLD | NEW |