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" |
11 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "net/base/net_log.h" | 14 #include "net/base/net_log.h" |
15 #include "net/base/net_log_unittest.h" | 15 #include "net/base/net_log_unittest.h" |
16 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
17 #include "net/base/test_data_directory.h" | 17 #include "net/base/test_data_directory.h" |
18 #include "net/cert/mock_cert_verifier.h" | 18 #include "net/cert/mock_cert_verifier.h" |
19 #include "net/cert/test_root_certs.h" | 19 #include "net/cert/test_root_certs.h" |
20 #include "net/dns/host_resolver.h" | 20 #include "net/dns/host_resolver.h" |
21 #include "net/http/transport_security_state.h" | 21 #include "net/http/transport_security_state.h" |
22 #include "net/socket/client_socket_factory.h" | 22 #include "net/socket/client_socket_factory.h" |
23 #include "net/socket/client_socket_handle.h" | 23 #include "net/socket/client_socket_handle.h" |
24 #include "net/socket/socket_test_util.h" | 24 #include "net/socket/socket_test_util.h" |
25 #include "net/socket/tcp_client_socket.h" | 25 #include "net/socket/tcp_client_socket.h" |
26 #include "net/ssl/channel_id_service.h" | 26 #include "net/ssl/channel_id_service.h" |
27 #include "net/ssl/default_channel_id_store.h" | 27 #include "net/ssl/default_channel_id_store.h" |
28 #include "net/ssl/openssl_client_key_store.h" | |
wtc
2014/07/31 23:05:25
IMPORTANT: is this part of the CL?
mshelley
2014/08/02 23:59:15
Done.
| |
28 #include "net/ssl/ssl_cert_request_info.h" | 29 #include "net/ssl/ssl_cert_request_info.h" |
29 #include "net/ssl/ssl_config_service.h" | 30 #include "net/ssl/ssl_config_service.h" |
30 #include "net/test/cert_test_util.h" | 31 #include "net/test/cert_test_util.h" |
31 #include "net/test/spawned_test_server/spawned_test_server.h" | 32 #include "net/test/spawned_test_server/spawned_test_server.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
33 #include "testing/platform_test.h" | 34 #include "testing/platform_test.h" |
34 | 35 |
35 //----------------------------------------------------------------------------- | 36 //----------------------------------------------------------------------------- |
36 | 37 |
37 namespace net { | 38 namespace net { |
(...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
651 OVERRIDE {} | 652 OVERRIDE {} |
652 virtual int GetChannelIDCount() OVERRIDE { return 0; } | 653 virtual int GetChannelIDCount() OVERRIDE { return 0; } |
653 virtual void SetForceKeepSessionState() OVERRIDE {} | 654 virtual void SetForceKeepSessionState() OVERRIDE {} |
654 }; | 655 }; |
655 | 656 |
656 class SSLClientSocketTest : public PlatformTest { | 657 class SSLClientSocketTest : public PlatformTest { |
657 public: | 658 public: |
658 SSLClientSocketTest() | 659 SSLClientSocketTest() |
659 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 660 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
660 cert_verifier_(new MockCertVerifier), | 661 cert_verifier_(new MockCertVerifier), |
661 transport_security_state_(new TransportSecurityState) { | 662 transport_security_state_(new TransportSecurityState) { |
wtc
2014/07/31 23:05:25
BUG: initialize ran_handshake_completion_callback_
mshelley
2014/08/02 23:59:15
Done.
| |
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/07/31 23:05:25
IMPORTANT: should we delete this? Ah, I see the Co
| |
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 2054 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) { | |
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())); | |
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) { | |
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. | |
2954 rv = callback.GetResult(rv); | |
2955 | |
2956 EXPECT_TRUE(ran_handshake_completion_callback_); | |
2957 } | |
2958 | |
2959 // Connect to a server requesting client authentication. Send it a | |
2960 // matching certificate. It should allow the connection. | |
wtc
2014/07/31 23:05:25
Please update this comment because this test is te
mshelley
2014/08/02 23:59:15
Done.
| |
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"); | |
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 |