Chromium Code Reviews| 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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <cstring> | 10 #include <cstring> |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 MockWriteResult(IoMode io_mode, int result) : mode(io_mode), result(result) {} | 183 MockWriteResult(IoMode io_mode, int result) : mode(io_mode), result(result) {} |
| 184 | 184 |
| 185 IoMode mode; | 185 IoMode mode; |
| 186 int result; | 186 int result; |
| 187 }; | 187 }; |
| 188 | 188 |
| 189 // The SocketDataProvider is an interface used by the MockClientSocket | 189 // The SocketDataProvider is an interface used by the MockClientSocket |
| 190 // for getting data about individual reads and writes on the socket. | 190 // for getting data about individual reads and writes on the socket. |
| 191 class SocketDataProvider { | 191 class SocketDataProvider { |
| 192 public: | 192 public: |
| 193 SocketDataProvider() : socket_(NULL) {} | 193 SocketDataProvider(); |
| 194 | 194 virtual ~SocketDataProvider(); |
| 195 virtual ~SocketDataProvider() {} | |
| 196 | 195 |
| 197 // Returns the buffer and result code for the next simulated read. | 196 // Returns the buffer and result code for the next simulated read. |
| 198 // If the |MockRead.result| is ERR_IO_PENDING, it informs the caller | 197 // If the |MockRead.result| is ERR_IO_PENDING, it informs the caller |
| 199 // that it will be called via the AsyncSocket::OnReadComplete() | 198 // that it will be called via the AsyncSocket::OnReadComplete() |
| 200 // function at a later time. | 199 // function at a later time. |
| 201 virtual MockRead OnRead() = 0; | 200 virtual MockRead OnRead() = 0; |
| 202 virtual MockWriteResult OnWrite(const std::string& data) = 0; | 201 virtual MockWriteResult OnWrite(const std::string& data) = 0; |
| 203 virtual void Reset() = 0; | 202 virtual void Reset() = 0; |
| 204 virtual bool AllReadDataConsumed() const = 0; | 203 virtual bool AllReadDataConsumed() const = 0; |
| 205 virtual bool AllWriteDataConsumed() const = 0; | 204 virtual bool AllWriteDataConsumed() const = 0; |
| 206 | 205 |
| 206 // Returns true if the request should be considered idle, for the purposes of | |
| 207 // IsConnectedAndIdle. | |
| 208 virtual bool IsIdle() const; | |
| 209 | |
| 207 // Accessor for the socket which is using the SocketDataProvider. | 210 // Accessor for the socket which is using the SocketDataProvider. |
| 208 AsyncSocket* socket() { return socket_; } | 211 AsyncSocket* socket() { return socket_; } |
| 209 void set_socket(AsyncSocket* socket) { socket_ = socket; } | 212 void set_socket(AsyncSocket* socket) { socket_ = socket; } |
| 210 | 213 |
| 211 MockConnect connect_data() const { return connect_; } | 214 MockConnect connect_data() const { return connect_; } |
| 212 void set_connect_data(const MockConnect& connect) { connect_ = connect; } | 215 void set_connect_data(const MockConnect& connect) { connect_ = connect; } |
| 213 | 216 |
| 214 private: | 217 private: |
| 215 MockConnect connect_; | 218 MockConnect connect_; |
| 216 AsyncSocket* socket_; | 219 AsyncSocket* socket_; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 228 // data.async is ignored, and this read is completed synchronously as | 231 // data.async is ignored, and this read is completed synchronously as |
| 229 // part of this call. | 232 // part of this call. |
| 230 // TODO(rch): this should take a StringPiece since most of the fields | 233 // TODO(rch): this should take a StringPiece since most of the fields |
| 231 // are ignored. | 234 // are ignored. |
| 232 virtual void OnReadComplete(const MockRead& data) = 0; | 235 virtual void OnReadComplete(const MockRead& data) = 0; |
| 233 // If an async IO is pending because the SocketDataProvider returned | 236 // If an async IO is pending because the SocketDataProvider returned |
| 234 // ERR_IO_PENDING, then the AsyncSocket waits until this OnReadComplete | 237 // ERR_IO_PENDING, then the AsyncSocket waits until this OnReadComplete |
| 235 // is called to complete the asynchronous read operation. | 238 // is called to complete the asynchronous read operation. |
| 236 virtual void OnWriteComplete(int rv) = 0; | 239 virtual void OnWriteComplete(int rv) = 0; |
| 237 virtual void OnConnectComplete(const MockConnect& data) = 0; | 240 virtual void OnConnectComplete(const MockConnect& data) = 0; |
| 241 | |
| 242 // Called when the SocketDataProvider associated with the socket is destroyed. | |
| 243 // The socket may continue to be used after the data provider is destroyed, | |
| 244 // so it should be sure not to dereference the provider after this is called. | |
| 245 virtual void OnDataProviderDestroyed() = 0; | |
|
mmenke
2015/12/03 19:37:10
Could use weak ptrs instead, but I think this is a
| |
| 238 }; | 246 }; |
| 239 | 247 |
| 240 // StaticSocketDataHelper manages a list of reads and writes. | 248 // StaticSocketDataHelper manages a list of reads and writes. |
| 241 class StaticSocketDataHelper { | 249 class StaticSocketDataHelper { |
| 242 public: | 250 public: |
| 243 StaticSocketDataHelper(MockRead* reads, | 251 StaticSocketDataHelper(MockRead* reads, |
| 244 size_t reads_count, | 252 size_t reads_count, |
| 245 MockWrite* writes, | 253 MockWrite* writes, |
| 246 size_t writes_count); | 254 size_t writes_count); |
| 247 ~StaticSocketDataHelper(); | 255 ~StaticSocketDataHelper(); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 357 size_t writes_count); | 365 size_t writes_count); |
| 358 | 366 |
| 359 ~SequencedSocketData() override; | 367 ~SequencedSocketData() override; |
| 360 | 368 |
| 361 // SocketDataProviderBase implementation. | 369 // SocketDataProviderBase implementation. |
| 362 MockRead OnRead() override; | 370 MockRead OnRead() override; |
| 363 MockWriteResult OnWrite(const std::string& data) override; | 371 MockWriteResult OnWrite(const std::string& data) override; |
| 364 void Reset() override; | 372 void Reset() override; |
| 365 bool AllReadDataConsumed() const override; | 373 bool AllReadDataConsumed() const override; |
| 366 bool AllWriteDataConsumed() const override; | 374 bool AllWriteDataConsumed() const override; |
| 375 bool IsIdle() const override; | |
| 367 | 376 |
| 368 bool IsReadPaused(); | 377 bool IsReadPaused(); |
| 369 void CompleteRead(); | 378 void CompleteRead(); |
| 370 | 379 |
| 380 // When true, IsConnectedAndIdle() will return false if the next event in the | |
| 381 // sequence is a synchronous. Otherwise, the socket claims to be idle as | |
| 382 // long as it's connected. Defaults to false. | |
| 383 void set_busy_before_sync_reads(bool busy_before_sync_reads) { | |
| 384 busy_before_sync_reads_ = busy_before_sync_reads; | |
| 385 } | |
| 386 | |
| 371 private: | 387 private: |
| 372 // Defines the state for the read or write path. | 388 // Defines the state for the read or write path. |
| 373 enum IoState { | 389 enum IoState { |
| 374 IDLE, // No async operation is in progress. | 390 IDLE, // No async operation is in progress. |
| 375 PENDING, // An async operation in waiting for another opteration to | 391 PENDING, // An async operation in waiting for another opteration to |
| 376 // complete. | 392 // complete. |
| 377 COMPLETING, // A task has been posted to complet an async operation. | 393 COMPLETING, // A task has been posted to complet an async operation. |
| 378 PAUSED, // IO is paused until CompleteRead() is called. | 394 PAUSED, // IO is paused until CompleteRead() is called. |
| 379 }; | 395 }; |
| 380 | 396 |
| 381 void OnReadComplete(); | 397 void OnReadComplete(); |
| 382 void OnWriteComplete(); | 398 void OnWriteComplete(); |
| 383 | 399 |
| 384 void MaybePostReadCompleteTask(); | 400 void MaybePostReadCompleteTask(); |
| 385 void MaybePostWriteCompleteTask(); | 401 void MaybePostWriteCompleteTask(); |
| 386 | 402 |
| 387 StaticSocketDataHelper helper_; | 403 StaticSocketDataHelper helper_; |
| 388 int sequence_number_; | 404 int sequence_number_; |
| 389 IoState read_state_; | 405 IoState read_state_; |
| 390 IoState write_state_; | 406 IoState write_state_; |
| 391 | 407 |
| 408 bool busy_before_sync_reads_; | |
| 409 | |
| 392 base::WeakPtrFactory<SequencedSocketData> weak_factory_; | 410 base::WeakPtrFactory<SequencedSocketData> weak_factory_; |
| 393 | 411 |
| 394 DISALLOW_COPY_AND_ASSIGN(SequencedSocketData); | 412 DISALLOW_COPY_AND_ASSIGN(SequencedSocketData); |
| 395 }; | 413 }; |
| 396 | 414 |
| 397 class DeterministicMockTCPClientSocket; | 415 class DeterministicMockTCPClientSocket; |
| 398 | 416 |
| 399 // This class gives the user full control over the network activity, | 417 // This class gives the user full control over the network activity, |
| 400 // specifically the timing of the COMPLETION of I/O operations. Regardless of | 418 // specifically the timing of the COMPLETION of I/O operations. Regardless of |
| 401 // the order in which I/O operations are initiated, this class ensures that they | 419 // the order in which I/O operations are initiated, this class ensures that they |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 710 bool WasNpnNegotiated() const override; | 728 bool WasNpnNegotiated() const override; |
| 711 bool GetSSLInfo(SSLInfo* ssl_info) override; | 729 bool GetSSLInfo(SSLInfo* ssl_info) override; |
| 712 void GetConnectionAttempts(ConnectionAttempts* out) const override; | 730 void GetConnectionAttempts(ConnectionAttempts* out) const override; |
| 713 void ClearConnectionAttempts() override; | 731 void ClearConnectionAttempts() override; |
| 714 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; | 732 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; |
| 715 | 733 |
| 716 // AsyncSocket: | 734 // AsyncSocket: |
| 717 void OnReadComplete(const MockRead& data) override; | 735 void OnReadComplete(const MockRead& data) override; |
| 718 void OnWriteComplete(int rv) override; | 736 void OnWriteComplete(int rv) override; |
| 719 void OnConnectComplete(const MockConnect& data) override; | 737 void OnConnectComplete(const MockConnect& data) override; |
| 738 void OnDataProviderDestroyed() override; | |
| 720 | 739 |
| 721 private: | 740 private: |
| 722 int CompleteRead(); | 741 int CompleteRead(); |
| 723 | 742 |
| 724 AddressList addresses_; | 743 AddressList addresses_; |
| 725 | 744 |
| 726 SocketDataProvider* data_; | 745 SocketDataProvider* data_; |
| 727 int read_offset_; | 746 int read_offset_; |
| 728 MockRead read_data_; | 747 MockRead read_data_; |
| 729 bool need_read_data_; | 748 bool need_read_data_; |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 891 int buf_len, | 910 int buf_len, |
| 892 const CompletionCallback& callback) override; | 911 const CompletionCallback& callback) override; |
| 893 int Write(IOBuffer* buf, | 912 int Write(IOBuffer* buf, |
| 894 int buf_len, | 913 int buf_len, |
| 895 const CompletionCallback& callback) override; | 914 const CompletionCallback& callback) override; |
| 896 | 915 |
| 897 // StreamSocket implementation. | 916 // StreamSocket implementation. |
| 898 int Connect(const CompletionCallback& callback) override; | 917 int Connect(const CompletionCallback& callback) override; |
| 899 void Disconnect() override; | 918 void Disconnect() override; |
| 900 bool IsConnected() const override; | 919 bool IsConnected() const override; |
| 920 bool IsConnectedAndIdle() const override; | |
| 901 bool WasEverUsed() const override; | 921 bool WasEverUsed() const override; |
| 902 bool UsingTCPFastOpen() const override; | 922 bool UsingTCPFastOpen() const override; |
| 903 int GetPeerAddress(IPEndPoint* address) const override; | 923 int GetPeerAddress(IPEndPoint* address) const override; |
| 904 bool GetSSLInfo(SSLInfo* ssl_info) override; | 924 bool GetSSLInfo(SSLInfo* ssl_info) override; |
| 905 | 925 |
| 906 // SSLClientSocket implementation. | 926 // SSLClientSocket implementation. |
| 907 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override; | 927 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override; |
| 908 NextProtoStatus GetNextProto(std::string* proto) const override; | 928 NextProtoStatus GetNextProto(std::string* proto) const override; |
| 909 | 929 |
| 910 // This MockSocket does not implement the manual async IO feature. | 930 // This MockSocket does not implement the manual async IO feature. |
| 911 void OnReadComplete(const MockRead& data) override; | 931 void OnReadComplete(const MockRead& data) override; |
| 912 void OnWriteComplete(int rv) override; | 932 void OnWriteComplete(int rv) override; |
| 913 void OnConnectComplete(const MockConnect& data) override; | 933 void OnConnectComplete(const MockConnect& data) override; |
| 934 // SSL sockets don't need magic to deal with destruction of their data | |
| 935 // provider. | |
| 936 // TODO(mmenke): Probably a good idea to support it, anyways. | |
| 937 void OnDataProviderDestroyed() override {} | |
| 914 | 938 |
| 915 ChannelIDService* GetChannelIDService() const override; | 939 ChannelIDService* GetChannelIDService() const override; |
| 916 | 940 |
| 917 private: | 941 private: |
| 918 static void ConnectCallback(MockSSLClientSocket* ssl_client_socket, | 942 static void ConnectCallback(MockSSLClientSocket* ssl_client_socket, |
| 919 const CompletionCallback& callback, | 943 const CompletionCallback& callback, |
| 920 int rv); | 944 int rv); |
| 921 | 945 |
| 922 scoped_ptr<ClientSocketHandle> transport_; | 946 scoped_ptr<ClientSocketHandle> transport_; |
| 923 SSLSocketDataProvider* data_; | 947 SSLSocketDataProvider* data_; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 947 const BoundNetLog& NetLog() const override; | 971 const BoundNetLog& NetLog() const override; |
| 948 | 972 |
| 949 // DatagramClientSocket implementation. | 973 // DatagramClientSocket implementation. |
| 950 int BindToNetwork(NetworkChangeNotifier::NetworkHandle network) override; | 974 int BindToNetwork(NetworkChangeNotifier::NetworkHandle network) override; |
| 951 int Connect(const IPEndPoint& address) override; | 975 int Connect(const IPEndPoint& address) override; |
| 952 | 976 |
| 953 // AsyncSocket implementation. | 977 // AsyncSocket implementation. |
| 954 void OnReadComplete(const MockRead& data) override; | 978 void OnReadComplete(const MockRead& data) override; |
| 955 void OnWriteComplete(int rv) override; | 979 void OnWriteComplete(int rv) override; |
| 956 void OnConnectComplete(const MockConnect& data) override; | 980 void OnConnectComplete(const MockConnect& data) override; |
| 981 void OnDataProviderDestroyed() override; | |
| 957 | 982 |
| 958 void set_source_port(uint16 port) { source_port_ = port;} | 983 void set_source_port(uint16 port) { source_port_ = port;} |
| 959 | 984 |
| 960 private: | 985 private: |
| 961 int CompleteRead(); | 986 int CompleteRead(); |
| 962 | 987 |
| 963 void RunCallbackAsync(const CompletionCallback& callback, int result); | 988 void RunCallbackAsync(const CompletionCallback& callback, int result); |
| 964 void RunCallback(const CompletionCallback& callback, int result); | 989 void RunCallback(const CompletionCallback& callback, int result); |
| 965 | 990 |
| 966 bool connected_; | 991 bool connected_; |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1248 | 1273 |
| 1249 // Helper function to get the total data size of the MockReads in |reads|. | 1274 // Helper function to get the total data size of the MockReads in |reads|. |
| 1250 int64_t CountReadBytes(const MockRead reads[], size_t reads_size); | 1275 int64_t CountReadBytes(const MockRead reads[], size_t reads_size); |
| 1251 | 1276 |
| 1252 // Helper function to get the total data size of the MockWrites in |writes|. | 1277 // Helper function to get the total data size of the MockWrites in |writes|. |
| 1253 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size); | 1278 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size); |
| 1254 | 1279 |
| 1255 } // namespace net | 1280 } // namespace net |
| 1256 | 1281 |
| 1257 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 1282 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |