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

Side by Side Diff: net/socket/socket_test_util.h

Issue 353713005: Implements new, more robust design for communicating between SSLConnectJobs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated SSLClientSocket tests & fixed bug in SSLSessionCacheOpenSSL 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 #ifndef NET_SOCKET_SOCKET_TEST_UTIL_H_ 5 #ifndef NET_SOCKET_SOCKET_TEST_UTIL_H_
6 #define NET_SOCKET_SOCKET_TEST_UTIL_H_ 6 #define NET_SOCKET_SOCKET_TEST_UTIL_H_
7 7
8 #include <cstring> 8 #include <cstring>
9 #include <deque> 9 #include <deque>
10 #include <string> 10 #include <string>
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 std::string next_proto; 328 std::string next_proto;
329 std::string server_protos; 329 std::string server_protos;
330 bool was_npn_negotiated; 330 bool was_npn_negotiated;
331 NextProto protocol_negotiated; 331 NextProto protocol_negotiated;
332 bool client_cert_sent; 332 bool client_cert_sent;
333 SSLCertRequestInfo* cert_request_info; 333 SSLCertRequestInfo* cert_request_info;
334 scoped_refptr<X509Certificate> cert; 334 scoped_refptr<X509Certificate> cert;
335 bool channel_id_sent; 335 bool channel_id_sent;
336 ServerBoundCertService* server_bound_cert_service; 336 ServerBoundCertService* server_bound_cert_service;
337 int connection_status; 337 int connection_status;
338 // Indicates that the socket should block in the Connect method.
339 bool should_block_on_connect;
340 // Whether or not the Socket should behave like there is a pre-existing
341 // session to resume. Whether or not such a session is reported as
342 // resumed is controlled by |connection_status|.
343 bool is_in_session_cache;
338 }; 344 };
339 345
340 // A DataProvider where the client must write a request before the reads (e.g. 346 // A DataProvider where the client must write a request before the reads (e.g.
341 // the response) will complete. 347 // the response) will complete.
342 class DelayedSocketData : public StaticSocketDataProvider { 348 class DelayedSocketData : public StaticSocketDataProvider {
343 public: 349 public:
344 // |write_delay| the number of MockWrites to complete before allowing 350 // |write_delay| the number of MockWrites to complete before allowing
345 // a MockRead to complete. 351 // a MockRead to complete.
346 // |reads| the list of MockRead completions. 352 // |reads| the list of MockRead completions.
347 // |writes| the list of MockWrite completions. 353 // |writes| the list of MockWrite completions.
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 virtual ~MockClientSocketFactory(); 637 virtual ~MockClientSocketFactory();
632 638
633 void AddSocketDataProvider(SocketDataProvider* socket); 639 void AddSocketDataProvider(SocketDataProvider* socket);
634 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); 640 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket);
635 void ResetNextMockIndexes(); 641 void ResetNextMockIndexes();
636 642
637 SocketDataProviderArray<SocketDataProvider>& mock_data() { 643 SocketDataProviderArray<SocketDataProvider>& mock_data() {
638 return mock_data_; 644 return mock_data_;
639 } 645 }
640 646
647 // Note: this method is unsafe; the elements of the returned vector
648 // are not necessarily valid.
649 const std::vector<MockSSLClientSocket*>& ssl_client_sockets() const {
650 return ssl_client_sockets_;
651 }
652
641 // ClientSocketFactory 653 // ClientSocketFactory
642 virtual scoped_ptr<DatagramClientSocket> CreateDatagramClientSocket( 654 virtual scoped_ptr<DatagramClientSocket> CreateDatagramClientSocket(
643 DatagramSocket::BindType bind_type, 655 DatagramSocket::BindType bind_type,
644 const RandIntCallback& rand_int_cb, 656 const RandIntCallback& rand_int_cb,
645 NetLog* net_log, 657 NetLog* net_log,
646 const NetLog::Source& source) OVERRIDE; 658 const NetLog::Source& source) OVERRIDE;
647 virtual scoped_ptr<StreamSocket> CreateTransportClientSocket( 659 virtual scoped_ptr<StreamSocket> CreateTransportClientSocket(
648 const AddressList& addresses, 660 const AddressList& addresses,
649 NetLog* net_log, 661 NetLog* net_log,
650 const NetLog::Source& source) OVERRIDE; 662 const NetLog::Source& source) OVERRIDE;
651 virtual scoped_ptr<SSLClientSocket> CreateSSLClientSocket( 663 virtual scoped_ptr<SSLClientSocket> CreateSSLClientSocket(
652 scoped_ptr<ClientSocketHandle> transport_socket, 664 scoped_ptr<ClientSocketHandle> transport_socket,
653 const HostPortPair& host_and_port, 665 const HostPortPair& host_and_port,
654 const SSLConfig& ssl_config, 666 const SSLConfig& ssl_config,
655 const SSLClientSocketContext& context) OVERRIDE; 667 const SSLClientSocketContext& context) OVERRIDE;
656 virtual void ClearSSLSessionCache() OVERRIDE; 668 virtual void ClearSSLSessionCache() OVERRIDE;
657 669
658 private: 670 private:
659 SocketDataProviderArray<SocketDataProvider> mock_data_; 671 SocketDataProviderArray<SocketDataProvider> mock_data_;
660 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; 672 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_;
673 std::vector<MockSSLClientSocket*> ssl_client_sockets_;
661 }; 674 };
662 675
663 class MockClientSocket : public SSLClientSocket { 676 class MockClientSocket : public SSLClientSocket {
664 public: 677 public:
665 // Value returned by GetTLSUniqueChannelBinding(). 678 // Value returned by GetTLSUniqueChannelBinding().
666 static const char kTlsUnique[]; 679 static const char kTlsUnique[];
667 680
668 // The BoundNetLog is needed to test LoadTimingInfo, which uses NetLog IDs as 681 // The BoundNetLog is needed to test LoadTimingInfo, which uses NetLog IDs as
669 // unique socket IDs. 682 // unique socket IDs.
670 explicit MockClientSocket(const BoundNetLog& net_log); 683 explicit MockClientSocket(const BoundNetLog& net_log);
(...skipping 13 matching lines...) Expand all
684 virtual void Disconnect() OVERRIDE; 697 virtual void Disconnect() OVERRIDE;
685 virtual bool IsConnected() const OVERRIDE; 698 virtual bool IsConnected() const OVERRIDE;
686 virtual bool IsConnectedAndIdle() const OVERRIDE; 699 virtual bool IsConnectedAndIdle() const OVERRIDE;
687 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; 700 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
688 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; 701 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
689 virtual const BoundNetLog& NetLog() const OVERRIDE; 702 virtual const BoundNetLog& NetLog() const OVERRIDE;
690 virtual void SetSubresourceSpeculation() OVERRIDE {} 703 virtual void SetSubresourceSpeculation() OVERRIDE {}
691 virtual void SetOmniboxSpeculation() OVERRIDE {} 704 virtual void SetOmniboxSpeculation() OVERRIDE {}
692 705
693 // SSLClientSocket implementation. 706 // SSLClientSocket implementation.
707 virtual bool InSessionCache() const OVERRIDE;
708 virtual void SetHandshakeCompletionCallback(const base::Closure& cb) OVERRIDE;
694 virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) 709 virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info)
695 OVERRIDE; 710 OVERRIDE;
696 virtual int ExportKeyingMaterial(const base::StringPiece& label, 711 virtual int ExportKeyingMaterial(const base::StringPiece& label,
697 bool has_context, 712 bool has_context,
698 const base::StringPiece& context, 713 const base::StringPiece& context,
699 unsigned char* out, 714 unsigned char* out,
700 unsigned int outlen) OVERRIDE; 715 unsigned int outlen) OVERRIDE;
701 virtual int GetTLSUniqueChannelBinding(std::string* out) OVERRIDE; 716 virtual int GetTLSUniqueChannelBinding(std::string* out) OVERRIDE;
702 virtual NextProtoStatus GetNextProto(std::string* proto, 717 virtual NextProtoStatus GetNextProto(std::string* proto,
703 std::string* server_protos) OVERRIDE; 718 std::string* server_protos) OVERRIDE;
704 virtual ServerBoundCertService* GetServerBoundCertService() const OVERRIDE; 719 virtual ServerBoundCertService* GetServerBoundCertService() const OVERRIDE;
720 bool DidReachConnect() const;
wtc 2014/07/31 02:02:24 1. Add a blank line to separate this method from t
mshelley 2014/07/31 20:11:37 Done.
705 721
706 protected: 722 protected:
707 virtual ~MockClientSocket(); 723 virtual ~MockClientSocket();
708 void RunCallbackAsync(const CompletionCallback& callback, int result); 724 void RunCallbackAsync(const CompletionCallback& callback, int result);
709 void RunCallback(const CompletionCallback& callback, int result); 725 void RunCallback(const CompletionCallback& callback, int result);
710 726
711 // SSLClientSocket implementation. 727 // SSLClientSocket implementation.
712 virtual scoped_refptr<X509Certificate> GetUnverifiedServerCertificateChain() 728 virtual scoped_refptr<X509Certificate> GetUnverifiedServerCertificateChain()
713 const OVERRIDE; 729 const OVERRIDE;
714 730
715 // True if Connect completed successfully and Disconnect hasn't been called. 731 // True if Connect completed successfully and Disconnect hasn't been called.
716 bool connected_; 732 bool connected_;
717 733
734 // True if the Connect method has been called on the socket.
735 bool reached_connect_;
736
718 // Address of the "remote" peer we're connected to. 737 // Address of the "remote" peer we're connected to.
719 IPEndPoint peer_addr_; 738 IPEndPoint peer_addr_;
720 739
721 BoundNetLog net_log_; 740 BoundNetLog net_log_;
722 741
742 private:
723 base::WeakPtrFactory<MockClientSocket> weak_factory_; 743 base::WeakPtrFactory<MockClientSocket> weak_factory_;
724 744
725 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); 745 DISALLOW_COPY_AND_ASSIGN(MockClientSocket);
726 }; 746 };
727 747
728 class MockTCPClientSocket : public MockClientSocket, public AsyncSocket { 748 class MockTCPClientSocket : public MockClientSocket, public AsyncSocket {
729 public: 749 public:
730 MockTCPClientSocket(const AddressList& addresses, 750 MockTCPClientSocket(const AddressList& addresses,
731 net::NetLog* net_log, 751 net::NetLog* net_log,
732 SocketDataProvider* socket); 752 SocketDataProvider* socket);
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 virtual int Connect(const CompletionCallback& callback) OVERRIDE; 963 virtual int Connect(const CompletionCallback& callback) OVERRIDE;
944 virtual void Disconnect() OVERRIDE; 964 virtual void Disconnect() OVERRIDE;
945 virtual bool IsConnected() const OVERRIDE; 965 virtual bool IsConnected() const OVERRIDE;
946 virtual bool WasEverUsed() const OVERRIDE; 966 virtual bool WasEverUsed() const OVERRIDE;
947 virtual bool UsingTCPFastOpen() const OVERRIDE; 967 virtual bool UsingTCPFastOpen() const OVERRIDE;
948 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; 968 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
949 virtual bool WasNpnNegotiated() const OVERRIDE; 969 virtual bool WasNpnNegotiated() const OVERRIDE;
950 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; 970 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
951 971
952 // SSLClientSocket implementation. 972 // SSLClientSocket implementation.
973 virtual bool InSessionCache() const OVERRIDE;
974 virtual void SetHandshakeCompletionCallback(const base::Closure& cb) OVERRIDE;
953 virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) 975 virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info)
954 OVERRIDE; 976 OVERRIDE;
955 virtual NextProtoStatus GetNextProto(std::string* proto, 977 virtual NextProtoStatus GetNextProto(std::string* proto,
956 std::string* server_protos) OVERRIDE; 978 std::string* server_protos) OVERRIDE;
957 virtual bool set_was_npn_negotiated(bool negotiated) OVERRIDE; 979 virtual bool set_was_npn_negotiated(bool negotiated) OVERRIDE;
958 virtual void set_protocol_negotiated(NextProto protocol_negotiated) OVERRIDE; 980 virtual void set_protocol_negotiated(NextProto protocol_negotiated) OVERRIDE;
959 virtual NextProto GetNegotiatedProtocol() const OVERRIDE; 981 virtual NextProto GetNegotiatedProtocol() const OVERRIDE;
960 982
961 // This MockSocket does not implement the manual async IO feature. 983 // This MockSocket does not implement the manual async IO feature.
962 virtual void OnReadComplete(const MockRead& data) OVERRIDE; 984 virtual void OnReadComplete(const MockRead& data) OVERRIDE;
963 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; 985 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE;
964 986
965 virtual bool WasChannelIDSent() const OVERRIDE; 987 virtual bool WasChannelIDSent() const OVERRIDE;
966 virtual void set_channel_id_sent(bool channel_id_sent) OVERRIDE; 988 virtual void set_channel_id_sent(bool channel_id_sent) OVERRIDE;
967 virtual ServerBoundCertService* GetServerBoundCertService() const OVERRIDE; 989 virtual ServerBoundCertService* GetServerBoundCertService() const OVERRIDE;
968 990
991 // Resumes the connection of a socket that was paused for testing.
992 // |connect_callback_| should be set before invoking this method.
993 void RestartPausedConnect();
994
969 private: 995 private:
970 static void ConnectCallback(MockSSLClientSocket* ssl_client_socket, 996 enum ConnectState {
971 const CompletionCallback& callback, 997 STATE_NONE,
972 int rv); 998 STATE_TRANSPORT_CONNECT,
999 STATE_TRANSPORT_CONNECT_COMPLETE,
1000 STATE_SSL_CONNECT,
1001 STATE_SSL_CONNECT_COMPLETE,
1002 };
1003
1004 void OnIOComplete(int result);
1005
1006 // Runs the state transistion loop.
1007 int DoConnectLoop(int result);
1008
1009 int DoTransportConnect();
1010 int DoTransportConnectComplete(int result);
1011 int DoSSLConnect();
1012 int DoSSLConnectComplete(int result);
973 1013
974 scoped_ptr<ClientSocketHandle> transport_; 1014 scoped_ptr<ClientSocketHandle> transport_;
975 SSLSocketDataProvider* data_; 1015 SSLSocketDataProvider* data_;
976 bool is_npn_state_set_; 1016 bool is_npn_state_set_;
977 bool new_npn_value_; 1017 bool new_npn_value_;
978 bool is_protocol_negotiated_set_; 1018 bool is_protocol_negotiated_set_;
979 NextProto protocol_negotiated_; 1019 NextProto protocol_negotiated_;
980 1020
1021 CompletionCallback connect_callback_;
1022 // Indicates what state of Connect the socket should enter.
1023 ConnectState next_connect_state_;
1024
1025 base::Closure handshake_completion_callback_;
1026
1027 base::WeakPtrFactory<MockSSLClientSocket> weak_factory_;
1028
981 DISALLOW_COPY_AND_ASSIGN(MockSSLClientSocket); 1029 DISALLOW_COPY_AND_ASSIGN(MockSSLClientSocket);
982 }; 1030 };
983 1031
984 class MockUDPClientSocket : public DatagramClientSocket, public AsyncSocket { 1032 class MockUDPClientSocket : public DatagramClientSocket, public AsyncSocket {
985 public: 1033 public:
986 MockUDPClientSocket(SocketDataProvider* data, net::NetLog* net_log); 1034 MockUDPClientSocket(SocketDataProvider* data, net::NetLog* net_log);
987 virtual ~MockUDPClientSocket(); 1035 virtual ~MockUDPClientSocket();
988 1036
989 // Socket implementation. 1037 // Socket implementation.
990 virtual int Read(IOBuffer* buf, 1038 virtual int Read(IOBuffer* buf,
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 1333
1286 extern const char kSOCKS5OkRequest[]; 1334 extern const char kSOCKS5OkRequest[];
1287 extern const int kSOCKS5OkRequestLength; 1335 extern const int kSOCKS5OkRequestLength;
1288 1336
1289 extern const char kSOCKS5OkResponse[]; 1337 extern const char kSOCKS5OkResponse[];
1290 extern const int kSOCKS5OkResponseLength; 1338 extern const int kSOCKS5OkResponseLength;
1291 1339
1292 } // namespace net 1340 } // namespace net
1293 1341
1294 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ 1342 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698