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 "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 651 OVERRIDE {} | 651 OVERRIDE {} |
| 652 virtual int GetChannelIDCount() OVERRIDE { return 0; } | 652 virtual int GetChannelIDCount() OVERRIDE { return 0; } |
| 653 virtual void SetForceKeepSessionState() OVERRIDE {} | 653 virtual void SetForceKeepSessionState() OVERRIDE {} |
| 654 }; | 654 }; |
| 655 | 655 |
| 656 class SSLClientSocketTest : public PlatformTest { | 656 class SSLClientSocketTest : public PlatformTest { |
| 657 public: | 657 public: |
| 658 SSLClientSocketTest() | 658 SSLClientSocketTest() |
| 659 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 659 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
| 660 cert_verifier_(new MockCertVerifier), | 660 cert_verifier_(new MockCertVerifier), |
| 661 transport_security_state_(new TransportSecurityState) { | 661 transport_security_state_(new TransportSecurityState), |
| 662 ran_handshake_completion_callback_(false) { | |
| 662 cert_verifier_->set_default_result(OK); | 663 cert_verifier_->set_default_result(OK); |
| 663 context_.cert_verifier = cert_verifier_.get(); | 664 context_.cert_verifier = cert_verifier_.get(); |
| 664 context_.transport_security_state = transport_security_state_.get(); | 665 context_.transport_security_state = transport_security_state_.get(); |
| 665 } | 666 } |
| 666 | 667 |
| 668 void RecordCompletedHandshake() { ran_handshake_completion_callback_ = true; } | |
| 669 | |
| 667 protected: | 670 protected: |
| 668 // The address of the spawned test server, after calling StartTestServer(). | 671 // The address of the spawned test server, after calling StartTestServer(). |
| 669 const AddressList& addr() const { return addr_; } | 672 const AddressList& addr() const { return addr_; } |
| 670 | 673 |
| 671 // The SpawnedTestServer object, after calling StartTestServer(). | 674 // The SpawnedTestServer object, after calling StartTestServer(). |
| 672 const SpawnedTestServer* test_server() const { return test_server_.get(); } | 675 const SpawnedTestServer* test_server() const { return test_server_.get(); } |
| 673 | 676 |
| 674 // Starts the test server with SSL configuration |ssl_options|. Returns true | 677 // Starts the test server with SSL configuration |ssl_options|. Returns true |
| 675 // on success. | 678 // on success. |
| 676 bool StartTestServer(const SpawnedTestServer::SSLOptions& ssl_options) { | 679 bool StartTestServer(const SpawnedTestServer::SSLOptions& ssl_options) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 733 *result = callback_.GetResult(sock_->Connect(callback_.callback())); | 736 *result = callback_.GetResult(sock_->Connect(callback_.callback())); |
| 734 return true; | 737 return true; |
| 735 } | 738 } |
| 736 | 739 |
| 737 ClientSocketFactory* socket_factory_; | 740 ClientSocketFactory* socket_factory_; |
| 738 scoped_ptr<MockCertVerifier> cert_verifier_; | 741 scoped_ptr<MockCertVerifier> cert_verifier_; |
| 739 scoped_ptr<TransportSecurityState> transport_security_state_; | 742 scoped_ptr<TransportSecurityState> transport_security_state_; |
| 740 SSLClientSocketContext context_; | 743 SSLClientSocketContext context_; |
| 741 scoped_ptr<SSLClientSocket> sock_; | 744 scoped_ptr<SSLClientSocket> sock_; |
| 742 CapturingNetLog log_; | 745 CapturingNetLog log_; |
| 746 bool ran_handshake_completion_callback_; | |
| 743 | 747 |
| 744 private: | 748 private: |
| 745 scoped_ptr<StreamSocket> transport_; | 749 scoped_ptr<StreamSocket> transport_; |
| 746 scoped_ptr<SpawnedTestServer> test_server_; | 750 scoped_ptr<SpawnedTestServer> test_server_; |
| 747 TestCompletionCallback callback_; | 751 TestCompletionCallback callback_; |
| 748 AddressList addr_; | 752 AddressList addr_; |
| 749 }; | 753 }; |
| 750 | 754 |
| 751 // Verifies the correctness of GetSSLCertRequestInfo. | 755 // Verifies the correctness of GetSSLCertRequestInfo. |
| 752 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { | 756 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { |
| (...skipping 1886 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2639 EXPECT_TRUE(sock->WasEverUsed()); | 2643 EXPECT_TRUE(sock->WasEverUsed()); |
| 2640 | 2644 |
| 2641 // TODO(davidben): Read one byte to ensure the test server has responded and | 2645 // TODO(davidben): Read one byte to ensure the test server has responded and |
| 2642 // then assert IsConnectedAndIdle is false. This currently doesn't work | 2646 // then assert IsConnectedAndIdle is false. This currently doesn't work |
| 2643 // because neither SSLClientSocketNSS nor SSLClientSocketOpenSSL check their | 2647 // because neither SSLClientSocketNSS nor SSLClientSocketOpenSSL check their |
| 2644 // SSL implementation's internal buffers. Either call PR_Available and | 2648 // SSL implementation's internal buffers. Either call PR_Available and |
| 2645 // SSL_pending, although the former isn't actually implemented or perhaps | 2649 // SSL_pending, although the former isn't actually implemented or perhaps |
| 2646 // attempt to read one byte extra. | 2650 // attempt to read one byte extra. |
| 2647 } | 2651 } |
| 2648 | 2652 |
| 2653 #if defined(USE_OPENSSL) | |
| 2654 | |
| 2655 TEST_F(SSLClientSocketTest, HandshakeCallbackIsRun_WithFailure) { | |
| 2656 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | |
| 2657 SpawnedTestServer::kLocalhost, | |
| 2658 base::FilePath()); | |
| 2659 ASSERT_TRUE(test_server.Start()); | |
| 2660 | |
| 2661 AddressList addr; | |
| 2662 ASSERT_TRUE(test_server.GetAddressList(&addr)); | |
| 2663 | |
| 2664 TestCompletionCallback callback; | |
| 2665 scoped_ptr<StreamSocket> real_transport( | |
| 2666 new TCPClientSocket(addr, NULL, NetLog::Source())); | |
| 2667 scoped_ptr<SynchronousErrorStreamSocket> transport( | |
| 2668 new SynchronousErrorStreamSocket(real_transport.Pass())); | |
| 2669 int rv = callback.GetResult(transport->Connect(callback.callback())); | |
| 2670 EXPECT_EQ(OK, rv); | |
| 2671 | |
| 2672 // Disable TLS False Start to avoid handshake non-determinism. | |
| 2673 SSLConfig ssl_config; | |
| 2674 ssl_config.false_start_enabled = false; | |
| 2675 | |
| 2676 SynchronousErrorStreamSocket* raw_transport = transport.get(); | |
| 2677 scoped_ptr<SSLClientSocket> sock( | |
| 2678 CreateSSLClientSocket(transport.PassAs<StreamSocket>(), | |
| 2679 test_server.host_port_pair(), | |
| 2680 ssl_config)); | |
| 2681 | |
| 2682 sock->SetHandshakeCompletionCallback(base::Bind( | |
| 2683 &SSLClientSocketTest::RecordCompletedHandshake, base::Unretained(this))); | |
| 2684 | |
| 2685 raw_transport->SetNextWriteError(ERR_CONNECTION_RESET); | |
| 2686 | |
| 2687 rv = callback.GetResult(sock->Connect(callback.callback())); | |
| 2688 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | |
| 2689 EXPECT_FALSE(sock->IsConnected()); | |
| 2690 | |
| 2691 EXPECT_TRUE(ran_handshake_completion_callback_); | |
| 2692 } | |
| 2693 | |
| 2694 // Tests that the completion callback is run when an SSL connection | |
| 2695 // completes successfully. | |
| 2696 TEST_F(SSLClientSocketTest, HandshakeCallbackIsRun_WithSuccess) { | |
| 2697 SpawnedTestServer::SSLOptions ssl_options; | |
| 2698 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | |
| 2699 SpawnedTestServer::kLocalhost, | |
| 2700 base::FilePath()); | |
| 2701 ASSERT_TRUE(test_server.Start()); | |
| 2702 | |
| 2703 AddressList addr; | |
| 2704 ASSERT_TRUE(test_server.GetAddressList(&addr)); | |
| 2705 | |
| 2706 CapturingNetLog log; | |
|
wtc
2014/08/04 00:04:17
This test doesn't seem to need a CapturingNetLog.
mshelley
2014/08/04 16:41:59
Ok -- I added a TODO comment.
| |
| 2707 scoped_ptr<StreamSocket> transport( | |
| 2708 new TCPClientSocket(addr, &log, NetLog::Source())); | |
| 2709 | |
| 2710 TestCompletionCallback callback; | |
| 2711 int rv = transport->Connect(callback.callback()); | |
| 2712 if (rv == ERR_IO_PENDING) | |
| 2713 rv = callback.WaitForResult(); | |
| 2714 EXPECT_EQ(OK, rv); | |
| 2715 | |
| 2716 SSLConfig ssl_config = kDefaultSSLConfig; | |
| 2717 ssl_config.false_start_enabled = false; | |
| 2718 | |
| 2719 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | |
| 2720 transport.Pass(), test_server.host_port_pair(), ssl_config)); | |
| 2721 | |
| 2722 sock->SetHandshakeCompletionCallback(base::Bind( | |
| 2723 &SSLClientSocketTest::RecordCompletedHandshake, base::Unretained(this))); | |
| 2724 | |
| 2725 if (sock->IsConnected()) { | |
| 2726 LOG(ERROR) << "SSL Socket prematurely connected"; | |
| 2727 } | |
|
wtc
2014/08/04 00:04:17
Nit: omit the curly braces.
mshelley
2014/08/04 16:41:59
Done.
| |
| 2728 | |
| 2729 rv = callback.GetResult(sock->Connect(callback.callback())); | |
| 2730 | |
| 2731 EXPECT_EQ(OK, rv); | |
| 2732 EXPECT_TRUE(sock->IsConnected()); | |
| 2733 EXPECT_TRUE(ran_handshake_completion_callback_); | |
| 2734 } | |
| 2735 | |
| 2736 #endif // defined(USE_OPENSSL) | |
| 2737 | |
| 2649 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { | 2738 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { |
| 2650 // False Start requires NPN and a forward-secret cipher suite. | 2739 // False Start requires NPN and a forward-secret cipher suite. |
| 2651 SpawnedTestServer::SSLOptions server_options; | 2740 SpawnedTestServer::SSLOptions server_options; |
| 2652 server_options.key_exchanges = | 2741 server_options.key_exchanges = |
| 2653 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; | 2742 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; |
| 2654 server_options.enable_npn = true; | 2743 server_options.enable_npn = true; |
| 2655 SSLConfig client_config; | 2744 SSLConfig client_config; |
| 2656 client_config.next_protos.push_back("http/1.1"); | 2745 client_config.next_protos.push_back("http/1.1"); |
| 2657 ASSERT_NO_FATAL_FAILURE( | 2746 ASSERT_NO_FATAL_FAILURE( |
| 2658 TestFalseStart(server_options, client_config, true)); | 2747 TestFalseStart(server_options, client_config, true)); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2808 ssl_config.channel_id_enabled = true; | 2897 ssl_config.channel_id_enabled = true; |
| 2809 | 2898 |
| 2810 int rv; | 2899 int rv; |
| 2811 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | 2900 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); |
| 2812 | 2901 |
| 2813 EXPECT_EQ(ERR_UNEXPECTED, rv); | 2902 EXPECT_EQ(ERR_UNEXPECTED, rv); |
| 2814 EXPECT_FALSE(sock_->IsConnected()); | 2903 EXPECT_FALSE(sock_->IsConnected()); |
| 2815 } | 2904 } |
| 2816 | 2905 |
| 2817 } // namespace net | 2906 } // namespace net |
| OLD | NEW |