Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: net/socket/ssl_client_socket_unittest.cc

Issue 353713005: Implements new, more robust design for communicating between SSLConnectJobs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed typo & removed CapturingNetLog arg. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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 scoped_ptr<StreamSocket> transport(
2707 new TCPClientSocket(addr, NULL, NetLog::Source()));
2708
2709 TestCompletionCallback callback;
2710 int rv = transport->Connect(callback.callback());
2711 if (rv == ERR_IO_PENDING)
2712 rv = callback.WaitForResult();
2713 EXPECT_EQ(OK, rv);
2714
2715 SSLConfig ssl_config = kDefaultSSLConfig;
2716 ssl_config.false_start_enabled = false;
2717
2718 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
2719 transport.Pass(), test_server.host_port_pair(), ssl_config));
2720
2721 sock->SetHandshakeCompletionCallback(base::Bind(
2722 &SSLClientSocketTest::RecordCompletedHandshake, base::Unretained(this)));
2723
2724 if (sock->IsConnected())
2725 LOG(ERROR) << "SSL Socket prematurely connected";
2726
2727 rv = callback.GetResult(sock->Connect(callback.callback()));
2728
2729 EXPECT_EQ(OK, rv);
2730 EXPECT_TRUE(sock->IsConnected());
2731 EXPECT_TRUE(ran_handshake_completion_callback_);
2732 }
2733
2734 #endif // defined(USE_OPENSSL)
2735
2649 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { 2736 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) {
2650 // False Start requires NPN and a forward-secret cipher suite. 2737 // False Start requires NPN and a forward-secret cipher suite.
2651 SpawnedTestServer::SSLOptions server_options; 2738 SpawnedTestServer::SSLOptions server_options;
2652 server_options.key_exchanges = 2739 server_options.key_exchanges =
2653 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; 2740 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA;
2654 server_options.enable_npn = true; 2741 server_options.enable_npn = true;
2655 SSLConfig client_config; 2742 SSLConfig client_config;
2656 client_config.next_protos.push_back("http/1.1"); 2743 client_config.next_protos.push_back("http/1.1");
2657 ASSERT_NO_FATAL_FAILURE( 2744 ASSERT_NO_FATAL_FAILURE(
2658 TestFalseStart(server_options, client_config, true)); 2745 TestFalseStart(server_options, client_config, true));
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
2808 ssl_config.channel_id_enabled = true; 2895 ssl_config.channel_id_enabled = true;
2809 2896
2810 int rv; 2897 int rv;
2811 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); 2898 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv));
2812 2899
2813 EXPECT_EQ(ERR_UNEXPECTED, rv); 2900 EXPECT_EQ(ERR_UNEXPECTED, rv);
2814 EXPECT_FALSE(sock_->IsConnected()); 2901 EXPECT_FALSE(sock_->IsConnected());
2815 } 2902 }
2816 2903
2817 } // namespace net 2904 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698