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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
723 // itself was a success. | 726 // itself was a success. |
724 bool CreateAndConnectSSLClientSocket(SSLConfig& ssl_config, int* result) { | 727 bool CreateAndConnectSSLClientSocket(SSLConfig& ssl_config, int* result) { |
725 sock_ = CreateSSLClientSocket( | 728 sock_ = CreateSSLClientSocket( |
726 transport_.Pass(), test_server_->host_port_pair(), ssl_config); | 729 transport_.Pass(), test_server_->host_port_pair(), ssl_config); |
727 | 730 |
728 if (sock_->IsConnected()) { | 731 if (sock_->IsConnected()) { |
729 LOG(ERROR) << "SSL Socket prematurely connected"; | 732 LOG(ERROR) << "SSL Socket prematurely connected"; |
730 return false; | 733 return false; |
731 } | 734 } |
732 | 735 |
736 #if defined(USE_OPENSSL) | |
737 sock_->SetHandshakeCompletionCallback( | |
738 base::Bind(&SSLClientSocketTest::RecordCompletedHandshake, | |
739 base::Unretained(this))); | |
740 #endif | |
wtc
2014/08/03 01:49:10
Please remove this. In the CompletionCallbackIsRun
mshelley
2014/08/03 23:37:10
Done.
| |
741 | |
733 *result = callback_.GetResult(sock_->Connect(callback_.callback())); | 742 *result = callback_.GetResult(sock_->Connect(callback_.callback())); |
734 return true; | 743 return true; |
735 } | 744 } |
736 | 745 |
737 ClientSocketFactory* socket_factory_; | 746 ClientSocketFactory* socket_factory_; |
738 scoped_ptr<MockCertVerifier> cert_verifier_; | 747 scoped_ptr<MockCertVerifier> cert_verifier_; |
739 scoped_ptr<TransportSecurityState> transport_security_state_; | 748 scoped_ptr<TransportSecurityState> transport_security_state_; |
740 SSLClientSocketContext context_; | 749 SSLClientSocketContext context_; |
741 scoped_ptr<SSLClientSocket> sock_; | 750 scoped_ptr<SSLClientSocket> sock_; |
742 CapturingNetLog log_; | 751 CapturingNetLog log_; |
752 bool ran_handshake_completion_callback_; | |
743 | 753 |
744 private: | 754 private: |
745 scoped_ptr<StreamSocket> transport_; | 755 scoped_ptr<StreamSocket> transport_; |
746 scoped_ptr<SpawnedTestServer> test_server_; | 756 scoped_ptr<SpawnedTestServer> test_server_; |
747 TestCompletionCallback callback_; | 757 TestCompletionCallback callback_; |
748 AddressList addr_; | 758 AddressList addr_; |
749 }; | 759 }; |
750 | 760 |
751 // Verifies the correctness of GetSSLCertRequestInfo. | 761 // Verifies the correctness of GetSSLCertRequestInfo. |
752 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { | 762 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { |
(...skipping 1884 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2637 | 2647 |
2638 // The socket has now been used. | 2648 // The socket has now been used. |
2639 EXPECT_TRUE(sock->WasEverUsed()); | 2649 EXPECT_TRUE(sock->WasEverUsed()); |
2640 | 2650 |
2641 // TODO(davidben): Read one byte to ensure the test server has responded and | 2651 // TODO(davidben): Read one byte to ensure the test server has responded and |
2642 // then assert IsConnectedAndIdle is false. This currently doesn't work | 2652 // then assert IsConnectedAndIdle is false. This currently doesn't work |
2643 // because neither SSLClientSocketNSS nor SSLClientSocketOpenSSL check their | 2653 // because neither SSLClientSocketNSS nor SSLClientSocketOpenSSL check their |
2644 // SSL implementation's internal buffers. Either call PR_Available and | 2654 // SSL implementation's internal buffers. Either call PR_Available and |
2645 // SSL_pending, although the former isn't actually implemented or perhaps | 2655 // SSL_pending, although the former isn't actually implemented or perhaps |
2646 // attempt to read one byte extra. | 2656 // attempt to read one byte extra. |
2647 } | 2657 } |
wtc
2014/08/03 01:49:10
This is the end of the last SSLClientSocketTest.xx
mshelley
2014/08/03 23:37:10
Done.
| |
2648 | 2658 |
2649 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { | 2659 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { |
2650 // False Start requires NPN and a forward-secret cipher suite. | 2660 // False Start requires NPN and a forward-secret cipher suite. |
2651 SpawnedTestServer::SSLOptions server_options; | 2661 SpawnedTestServer::SSLOptions server_options; |
2652 server_options.key_exchanges = | 2662 server_options.key_exchanges = |
2653 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; | 2663 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; |
2654 server_options.enable_npn = true; | 2664 server_options.enable_npn = true; |
2655 SSLConfig client_config; | 2665 SSLConfig client_config; |
2656 client_config.next_protos.push_back("http/1.1"); | 2666 client_config.next_protos.push_back("http/1.1"); |
2657 ASSERT_NO_FATAL_FAILURE( | 2667 ASSERT_NO_FATAL_FAILURE( |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2807 SSLConfig ssl_config = kDefaultSSLConfig; | 2817 SSLConfig ssl_config = kDefaultSSLConfig; |
2808 ssl_config.channel_id_enabled = true; | 2818 ssl_config.channel_id_enabled = true; |
2809 | 2819 |
2810 int rv; | 2820 int rv; |
2811 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | 2821 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); |
2812 | 2822 |
2813 EXPECT_EQ(ERR_UNEXPECTED, rv); | 2823 EXPECT_EQ(ERR_UNEXPECTED, rv); |
2814 EXPECT_FALSE(sock_->IsConnected()); | 2824 EXPECT_FALSE(sock_->IsConnected()); |
2815 } | 2825 } |
2816 | 2826 |
2827 #if defined(USE_OPENSSL) | |
2828 | |
2829 TEST_F(SSLClientSocketTest, CompletionCallbackIsRun_WithFailure) { | |
wtc
2014/08/03 01:49:10
In your test names, change "CompletionCallback" to
mshelley
2014/08/03 23:37:10
Done.
| |
2830 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | |
2831 SpawnedTestServer::kLocalhost, | |
2832 base::FilePath()); | |
2833 ASSERT_TRUE(test_server.Start()); | |
2834 | |
2835 AddressList addr; | |
2836 ASSERT_TRUE(test_server.GetAddressList(&addr)); | |
2837 | |
2838 TestCompletionCallback callback; | |
2839 scoped_ptr<StreamSocket> real_transport( | |
2840 new TCPClientSocket(addr, NULL, NetLog::Source())); | |
2841 scoped_ptr<SynchronousErrorStreamSocket> transport( | |
2842 new SynchronousErrorStreamSocket(real_transport.Pass())); | |
2843 int rv = callback.GetResult(transport->Connect(callback.callback())); | |
2844 EXPECT_EQ(OK, rv); | |
2845 | |
2846 // Disable TLS False Start to avoid handshake non-determinism. | |
2847 SSLConfig ssl_config; | |
2848 ssl_config.false_start_enabled = false; | |
2849 | |
2850 SynchronousErrorStreamSocket* raw_transport = transport.get(); | |
2851 scoped_ptr<SSLClientSocket> sock( | |
2852 CreateSSLClientSocket(transport.PassAs<StreamSocket>(), | |
2853 test_server.host_port_pair(), | |
2854 ssl_config)); | |
2855 | |
2856 sock->SetHandshakeCompletionCallback(base::Bind( | |
2857 &SSLClientSocketTest::RecordCompletedHandshake, base::Unretained(this))); | |
2858 | |
2859 rv = callback.GetResult(sock->Connect(callback.callback())); | |
wtc
2014/08/03 01:49:10
IMPORTANT: please base this unit test on the Conne
mshelley
2014/08/03 23:37:10
Done.
| |
2860 EXPECT_EQ(OK, rv); | |
2861 EXPECT_TRUE(sock->IsConnected()); | |
2862 | |
2863 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | |
2864 static const int kRequestTextSize = | |
2865 static_cast<int>(arraysize(request_text) - 1); | |
2866 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kRequestTextSize)); | |
2867 memcpy(request_buffer->data(), request_text, kRequestTextSize); | |
2868 | |
2869 rv = callback.GetResult( | |
2870 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); | |
2871 EXPECT_EQ(kRequestTextSize, rv); | |
2872 | |
2873 // Simulate an unclean/forcible shutdown. | |
2874 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); | |
2875 | |
2876 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | |
2877 | |
2878 // Note: This test will hang if this bug has regressed. Simply checking that | |
2879 // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate | |
2880 // result when using a dedicated task runner for NSS. | |
2881 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | |
2882 | |
2883 EXPECT_TRUE(ran_handshake_completion_callback_); | |
2884 } | |
2885 | |
2886 TEST_F(SSLClientSocketTest, CompletionCallbackIsRun_WithFalseStartFailure) { | |
wtc
2014/08/03 01:49:11
IMPORTANT: did you verify that the handshake is ac
| |
2887 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | |
2888 SpawnedTestServer::kLocalhost, | |
2889 base::FilePath()); | |
2890 ASSERT_TRUE(test_server.Start()); | |
2891 | |
2892 AddressList addr; | |
2893 ASSERT_TRUE(test_server.GetAddressList(&addr)); | |
2894 | |
2895 TestCompletionCallback callback; | |
2896 scoped_ptr<StreamSocket> real_transport( | |
2897 new TCPClientSocket(addr, NULL, NetLog::Source())); | |
2898 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer | |
2899 // is retained in order to configure additional errors. | |
2900 scoped_ptr<SynchronousErrorStreamSocket> error_socket( | |
2901 new SynchronousErrorStreamSocket(real_transport.Pass())); | |
2902 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | |
2903 scoped_ptr<FakeBlockingStreamSocket> transport( | |
2904 new FakeBlockingStreamSocket(error_socket.PassAs<StreamSocket>())); | |
2905 FakeBlockingStreamSocket* raw_transport = transport.get(); | |
2906 int rv = callback.GetResult(transport->Connect(callback.callback())); | |
2907 EXPECT_EQ(OK, rv); | |
2908 | |
2909 SSLConfig ssl_config; | |
2910 ssl_config.false_start_enabled = true; | |
2911 | |
2912 scoped_ptr<SSLClientSocket> sock( | |
2913 CreateSSLClientSocket(transport.PassAs<StreamSocket>(), | |
2914 test_server.host_port_pair(), | |
2915 ssl_config)); | |
2916 | |
2917 sock->SetHandshakeCompletionCallback(base::Bind( | |
2918 &SSLClientSocketTest::RecordCompletedHandshake, base::Unretained(this))); | |
2919 | |
2920 rv = callback.GetResult(sock->Connect(callback.callback())); | |
2921 EXPECT_EQ(OK, rv); | |
2922 EXPECT_TRUE(sock->IsConnected()); | |
2923 | |
2924 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | |
2925 static const int kRequestTextSize = | |
2926 static_cast<int>(arraysize(request_text) - 1); | |
2927 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kRequestTextSize)); | |
2928 memcpy(request_buffer->data(), request_text, kRequestTextSize); | |
2929 | |
2930 // Simulate an unclean/forcible shutdown on the underlying socket. | |
2931 // However, simulate this error asynchronously. | |
2932 raw_error_socket->SetNextWriteError(ERR_CONNECTION_RESET); | |
2933 raw_transport->BlockWrite(); | |
2934 | |
2935 // This write should complete synchronously, because the TLS ciphertext | |
2936 // can be created and placed into the outgoing buffers independent of the | |
2937 // underlying transport. | |
2938 rv = callback.GetResult( | |
2939 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); | |
2940 EXPECT_EQ(kRequestTextSize, rv); | |
2941 | |
2942 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | |
2943 | |
2944 rv = sock->Read(buf.get(), 4096, callback.callback()); | |
2945 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2946 | |
2947 // Now unblock the outgoing request, having it fail with the connection | |
2948 // being reset. | |
2949 raw_transport->UnblockWrite(); | |
2950 | |
2951 // Note: This will cause an inifite loop if this bug has regressed. Simply | |
2952 // checking that rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING | |
2953 // is a legitimate result when using a dedicated task runner for NSS. | |
wtc
2014/08/03 01:49:10
1. Remove this comment block. It makes sense in th
| |
2954 rv = callback.GetResult(rv); | |
2955 | |
2956 EXPECT_TRUE(ran_handshake_completion_callback_); | |
2957 } | |
2958 | |
2959 // Tests that the completion callback is run when an SSL connection | |
2960 // completes successfully. | |
2961 TEST_F(SSLClientSocketTest, CompletionCallbackIsRun_WithSuccess) { | |
2962 SpawnedTestServer::SSLOptions ssl_options; | |
2963 | |
2964 ASSERT_TRUE(ConnectToTestServer(ssl_options)); | |
2965 | |
2966 base::FilePath certs_dir = GetTestCertsDirectory(); | |
2967 SSLConfig ssl_config = kDefaultSSLConfig; | |
2968 ssl_config.send_client_cert = true; | |
2969 ssl_config.client_cert = ImportCertFromFile(certs_dir, "client_1.pem"); | |
wtc
2014/08/03 01:49:10
Please delete these four lines and just use kDefau
mshelley
2014/08/03 23:37:09
Done.
| |
2970 | |
2971 int rv; | |
2972 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | |
2973 EXPECT_EQ(OK, rv); | |
2974 | |
2975 EXPECT_TRUE(sock_->IsConnected()); | |
2976 | |
2977 EXPECT_TRUE(ran_handshake_completion_callback_); | |
2978 } | |
2979 | |
2980 #endif // defined(USE_OPENSSL) | |
2981 | |
2817 } // namespace net | 2982 } // namespace net |
OLD | NEW |