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 #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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 void set_connect_data(const MockConnect& connect) { connect_ = connect; } | 184 void set_connect_data(const MockConnect& connect) { connect_ = connect; } |
185 | 185 |
186 private: | 186 private: |
187 MockConnect connect_; | 187 MockConnect connect_; |
188 AsyncSocket* socket_; | 188 AsyncSocket* socket_; |
189 | 189 |
190 DISALLOW_COPY_AND_ASSIGN(SocketDataProvider); | 190 DISALLOW_COPY_AND_ASSIGN(SocketDataProvider); |
191 }; | 191 }; |
192 | 192 |
193 // The AsyncSocket is an interface used by the SocketDataProvider to | 193 // The AsyncSocket is an interface used by the SocketDataProvider to |
194 // complete the asynchronous read operation. | 194 // complete the asynchronous read operation. |
Ryan Sleevi
2013/09/03 23:04:08
Comment is out of date
Nicolas Zea
2013/09/07 01:07:52
n/a now
| |
195 class AsyncSocket { | 195 class AsyncSocket { |
196 public: | 196 public: |
197 // If an async IO is pending because the SocketDataProvider returned | 197 // If an async IO is pending because the SocketDataProvider returned |
198 // ERR_IO_PENDING, then the AsyncSocket waits until this OnReadComplete | 198 // ERR_IO_PENDING, then the AsyncSocket waits until this OnReadComplete |
199 // is called to complete the asynchronous read operation. | 199 // is called to complete the asynchronous read operation. |
200 // data.async is ignored, and this read is completed synchronously as | 200 // data.async is ignored, and this read is completed synchronously as |
201 // part of this call. | 201 // part of this call. |
202 virtual void OnReadComplete(const MockRead& data) = 0; | 202 virtual void OnReadComplete(const MockRead& data) = 0; |
203 virtual void OnConnectComplete(const MockConnect& data) = 0; | 203 virtual void OnConnectComplete(const MockConnect& data) = 0; |
204 virtual void OnWriteComplete(const MockWriteResult& result) = 0; | |
Ryan Sleevi
2013/09/03 23:04:08
For review sake, and because socket_test_util itse
Ryan Sleevi
2013/09/03 23:04:08
So, OnReadComplete is meant to be consumed by Dela
Nicolas Zea
2013/09/07 01:07:52
These changes have been reverted now that I don't
| |
204 }; | 205 }; |
205 | 206 |
206 // SocketDataProvider which responds based on static tables of mock reads and | 207 // SocketDataProvider which responds based on static tables of mock reads and |
207 // writes. | 208 // writes. |
208 class StaticSocketDataProvider : public SocketDataProvider { | 209 class StaticSocketDataProvider : public SocketDataProvider { |
209 public: | 210 public: |
210 StaticSocketDataProvider(); | 211 StaticSocketDataProvider(); |
211 StaticSocketDataProvider(MockRead* reads, size_t reads_count, | 212 StaticSocketDataProvider(MockRead* reads, size_t reads_count, |
212 MockWrite* writes, size_t writes_count); | 213 MockWrite* writes, size_t writes_count); |
213 virtual ~StaticSocketDataProvider(); | 214 virtual ~StaticSocketDataProvider(); |
(...skipping 10 matching lines...) Expand all Loading... | |
224 size_t write_count() const { return write_count_; } | 225 size_t write_count() const { return write_count_; } |
225 | 226 |
226 bool at_read_eof() const { return read_index_ >= read_count_; } | 227 bool at_read_eof() const { return read_index_ >= read_count_; } |
227 bool at_write_eof() const { return write_index_ >= write_count_; } | 228 bool at_write_eof() const { return write_index_ >= write_count_; } |
228 | 229 |
229 virtual void CompleteRead() {} | 230 virtual void CompleteRead() {} |
230 | 231 |
231 // SocketDataProvider implementation. | 232 // SocketDataProvider implementation. |
232 virtual MockRead GetNextRead() OVERRIDE; | 233 virtual MockRead GetNextRead() OVERRIDE; |
233 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE; | 234 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE; |
234 ; virtual void Reset() OVERRIDE; | 235 virtual void Reset() OVERRIDE; |
235 | 236 |
236 private: | 237 private: |
237 MockRead* reads_; | 238 MockRead* reads_; |
238 size_t read_index_; | 239 size_t read_index_; |
239 size_t read_count_; | 240 size_t read_count_; |
240 MockWrite* writes_; | 241 MockWrite* writes_; |
241 size_t write_index_; | 242 size_t write_index_; |
242 size_t write_count_; | 243 size_t write_count_; |
243 | 244 |
244 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider); | 245 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider); |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
692 virtual bool IsConnectedAndIdle() const OVERRIDE; | 693 virtual bool IsConnectedAndIdle() const OVERRIDE; |
693 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; | 694 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; |
694 virtual bool WasEverUsed() const OVERRIDE; | 695 virtual bool WasEverUsed() const OVERRIDE; |
695 virtual bool UsingTCPFastOpen() const OVERRIDE; | 696 virtual bool UsingTCPFastOpen() const OVERRIDE; |
696 virtual bool WasNpnNegotiated() const OVERRIDE; | 697 virtual bool WasNpnNegotiated() const OVERRIDE; |
697 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; | 698 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; |
698 | 699 |
699 // AsyncSocket: | 700 // AsyncSocket: |
700 virtual void OnReadComplete(const MockRead& data) OVERRIDE; | 701 virtual void OnReadComplete(const MockRead& data) OVERRIDE; |
701 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; | 702 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; |
703 virtual void OnWriteComplete(const MockWriteResult& result) OVERRIDE; | |
702 | 704 |
703 private: | 705 private: |
704 int CompleteRead(); | 706 int CompleteRead(); |
705 | 707 |
706 AddressList addresses_; | 708 AddressList addresses_; |
707 | 709 |
708 SocketDataProvider* data_; | 710 SocketDataProvider* data_; |
709 int read_offset_; | 711 int read_offset_; |
710 MockRead read_data_; | 712 MockRead read_data_; |
711 bool need_read_data_; | 713 bool need_read_data_; |
712 | 714 |
713 // True if the peer has closed the connection. This allows us to simulate | 715 // True if the peer has closed the connection. This allows us to simulate |
714 // the recv(..., MSG_PEEK) call in the IsConnectedAndIdle method of the real | 716 // the recv(..., MSG_PEEK) call in the IsConnectedAndIdle method of the real |
715 // TCPClientSocket. | 717 // TCPClientSocket. |
716 bool peer_closed_connection_; | 718 bool peer_closed_connection_; |
717 | 719 |
718 // While an asynchronous IO is pending, we save our user-buffer state. | 720 // While an asynchronous IO is pending, we save our user-buffer state. |
719 IOBuffer* pending_buf_; | 721 scoped_refptr<IOBuffer> pending_buf_; |
Ryan Sleevi
2013/09/03 23:04:08
While I agree that this is the right change, in or
Nicolas Zea
2013/09/07 01:07:52
Yep, in particular when handling a timeout, as the
| |
720 int pending_buf_len_; | 722 int pending_buf_len_; |
721 CompletionCallback pending_callback_; | 723 CompletionCallback pending_callback_; |
722 bool was_used_to_convey_data_; | 724 bool was_used_to_convey_data_; |
723 }; | 725 }; |
724 | 726 |
725 // DeterministicSocketHelper is a helper class that can be used | 727 // DeterministicSocketHelper is a helper class that can be used |
726 // to simulate net::Socket::Read() and net::Socket::Write() | 728 // to simulate net::Socket::Read() and net::Socket::Write() |
727 // using deterministic |data|. | 729 // using deterministic |data|. |
728 // Note: This is provided as a common helper class because | 730 // Note: This is provided as a common helper class because |
729 // of the inheritance hierarchy of DeterministicMock[UDP,TCP]ClientSocket and a | 731 // of the inheritance hierarchy of DeterministicMock[UDP,TCP]ClientSocket and a |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
800 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; | 802 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; |
801 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; | 803 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; |
802 virtual const BoundNetLog& NetLog() const OVERRIDE; | 804 virtual const BoundNetLog& NetLog() const OVERRIDE; |
803 | 805 |
804 // DatagramClientSocket implementation. | 806 // DatagramClientSocket implementation. |
805 virtual int Connect(const IPEndPoint& address) OVERRIDE; | 807 virtual int Connect(const IPEndPoint& address) OVERRIDE; |
806 | 808 |
807 // AsyncSocket implementation. | 809 // AsyncSocket implementation. |
808 virtual void OnReadComplete(const MockRead& data) OVERRIDE; | 810 virtual void OnReadComplete(const MockRead& data) OVERRIDE; |
809 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; | 811 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; |
812 virtual void OnWriteComplete(const MockWriteResult& result) OVERRIDE; | |
810 | 813 |
811 private: | 814 private: |
812 bool connected_; | 815 bool connected_; |
813 IPEndPoint peer_address_; | 816 IPEndPoint peer_address_; |
814 DeterministicSocketHelper helper_; | 817 DeterministicSocketHelper helper_; |
815 }; | 818 }; |
816 | 819 |
817 // Mock TCP socket to be used in conjunction with DeterministicSocketData. | 820 // Mock TCP socket to be used in conjunction with DeterministicSocketData. |
818 class DeterministicMockTCPClientSocket | 821 class DeterministicMockTCPClientSocket |
819 : public MockClientSocket, | 822 : public MockClientSocket, |
(...skipping 23 matching lines...) Expand all Loading... | |
843 virtual bool IsConnected() const OVERRIDE; | 846 virtual bool IsConnected() const OVERRIDE; |
844 virtual bool IsConnectedAndIdle() const OVERRIDE; | 847 virtual bool IsConnectedAndIdle() const OVERRIDE; |
845 virtual bool WasEverUsed() const OVERRIDE; | 848 virtual bool WasEverUsed() const OVERRIDE; |
846 virtual bool UsingTCPFastOpen() const OVERRIDE; | 849 virtual bool UsingTCPFastOpen() const OVERRIDE; |
847 virtual bool WasNpnNegotiated() const OVERRIDE; | 850 virtual bool WasNpnNegotiated() const OVERRIDE; |
848 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; | 851 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; |
849 | 852 |
850 // AsyncSocket: | 853 // AsyncSocket: |
851 virtual void OnReadComplete(const MockRead& data) OVERRIDE; | 854 virtual void OnReadComplete(const MockRead& data) OVERRIDE; |
852 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; | 855 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; |
856 virtual void OnWriteComplete(const MockWriteResult& result) OVERRIDE; | |
853 | 857 |
854 private: | 858 private: |
855 DeterministicSocketHelper helper_; | 859 DeterministicSocketHelper helper_; |
856 }; | 860 }; |
857 | 861 |
858 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { | 862 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { |
859 public: | 863 public: |
860 MockSSLClientSocket( | 864 MockSSLClientSocket( |
861 scoped_ptr<ClientSocketHandle> transport_socket, | 865 scoped_ptr<ClientSocketHandle> transport_socket, |
862 const HostPortPair& host_and_port, | 866 const HostPortPair& host_and_port, |
(...skipping 23 matching lines...) Expand all Loading... | |
886 virtual NextProtoStatus GetNextProto(std::string* proto, | 890 virtual NextProtoStatus GetNextProto(std::string* proto, |
887 std::string* server_protos) OVERRIDE; | 891 std::string* server_protos) OVERRIDE; |
888 virtual bool set_was_npn_negotiated(bool negotiated) OVERRIDE; | 892 virtual bool set_was_npn_negotiated(bool negotiated) OVERRIDE; |
889 virtual void set_protocol_negotiated( | 893 virtual void set_protocol_negotiated( |
890 NextProto protocol_negotiated) OVERRIDE; | 894 NextProto protocol_negotiated) OVERRIDE; |
891 virtual NextProto GetNegotiatedProtocol() const OVERRIDE; | 895 virtual NextProto GetNegotiatedProtocol() const OVERRIDE; |
892 | 896 |
893 // This MockSocket does not implement the manual async IO feature. | 897 // This MockSocket does not implement the manual async IO feature. |
894 virtual void OnReadComplete(const MockRead& data) OVERRIDE; | 898 virtual void OnReadComplete(const MockRead& data) OVERRIDE; |
895 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; | 899 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; |
900 virtual void OnWriteComplete(const MockWriteResult& result) OVERRIDE; | |
896 | 901 |
897 virtual bool WasChannelIDSent() const OVERRIDE; | 902 virtual bool WasChannelIDSent() const OVERRIDE; |
898 virtual void set_channel_id_sent(bool channel_id_sent) OVERRIDE; | 903 virtual void set_channel_id_sent(bool channel_id_sent) OVERRIDE; |
899 virtual ServerBoundCertService* GetServerBoundCertService() const OVERRIDE; | 904 virtual ServerBoundCertService* GetServerBoundCertService() const OVERRIDE; |
900 | 905 |
901 private: | 906 private: |
902 static void ConnectCallback(MockSSLClientSocket *ssl_client_socket, | 907 static void ConnectCallback(MockSSLClientSocket *ssl_client_socket, |
903 const CompletionCallback& callback, | 908 const CompletionCallback& callback, |
904 int rv); | 909 int rv); |
905 | 910 |
(...skipping 25 matching lines...) Expand all Loading... | |
931 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; | 936 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; |
932 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; | 937 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; |
933 virtual const BoundNetLog& NetLog() const OVERRIDE; | 938 virtual const BoundNetLog& NetLog() const OVERRIDE; |
934 | 939 |
935 // DatagramClientSocket implementation. | 940 // DatagramClientSocket implementation. |
936 virtual int Connect(const IPEndPoint& address) OVERRIDE; | 941 virtual int Connect(const IPEndPoint& address) OVERRIDE; |
937 | 942 |
938 // AsyncSocket implementation. | 943 // AsyncSocket implementation. |
939 virtual void OnReadComplete(const MockRead& data) OVERRIDE; | 944 virtual void OnReadComplete(const MockRead& data) OVERRIDE; |
940 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; | 945 virtual void OnConnectComplete(const MockConnect& data) OVERRIDE; |
946 virtual void OnWriteComplete(const MockWriteResult& result) OVERRIDE; | |
941 | 947 |
942 private: | 948 private: |
943 int CompleteRead(); | 949 int CompleteRead(); |
944 | 950 |
945 void RunCallbackAsync(const CompletionCallback& callback, int result); | 951 void RunCallbackAsync(const CompletionCallback& callback, int result); |
946 void RunCallback(const CompletionCallback& callback, int result); | 952 void RunCallback(const CompletionCallback& callback, int result); |
947 | 953 |
948 bool connected_; | 954 bool connected_; |
949 SocketDataProvider* data_; | 955 SocketDataProvider* data_; |
950 int read_offset_; | 956 int read_offset_; |
951 MockRead read_data_; | 957 MockRead read_data_; |
952 bool need_read_data_; | 958 bool need_read_data_; |
953 | 959 |
954 // Address of the "remote" peer we're connected to. | 960 // Address of the "remote" peer we're connected to. |
955 IPEndPoint peer_addr_; | 961 IPEndPoint peer_addr_; |
956 | 962 |
957 // While an asynchronous IO is pending, we save our user-buffer state. | 963 // While an asynchronous IO is pending, we save our user-buffer state. |
958 IOBuffer* pending_buf_; | 964 scoped_refptr<IOBuffer> pending_buf_; |
959 int pending_buf_len_; | 965 int pending_buf_len_; |
960 CompletionCallback pending_callback_; | 966 CompletionCallback pending_callback_; |
961 | 967 |
962 BoundNetLog net_log_; | 968 BoundNetLog net_log_; |
963 | 969 |
964 base::WeakPtrFactory<MockUDPClientSocket> weak_factory_; | 970 base::WeakPtrFactory<MockUDPClientSocket> weak_factory_; |
965 | 971 |
966 DISALLOW_COPY_AND_ASSIGN(MockUDPClientSocket); | 972 DISALLOW_COPY_AND_ASSIGN(MockUDPClientSocket); |
967 }; | 973 }; |
968 | 974 |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1204 | 1210 |
1205 extern const char kSOCKS5OkRequest[]; | 1211 extern const char kSOCKS5OkRequest[]; |
1206 extern const int kSOCKS5OkRequestLength; | 1212 extern const int kSOCKS5OkRequestLength; |
1207 | 1213 |
1208 extern const char kSOCKS5OkResponse[]; | 1214 extern const char kSOCKS5OkResponse[]; |
1209 extern const int kSOCKS5OkResponseLength; | 1215 extern const int kSOCKS5OkResponseLength; |
1210 | 1216 |
1211 } // namespace net | 1217 } // namespace net |
1212 | 1218 |
1213 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 1219 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
OLD | NEW |