| 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 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 // complete in this step, but is marked asynchronous. Write() returns | 491 // complete in this step, but is marked asynchronous. Write() returns |
| 492 // ERR_IO_PENDING. The current step is still 1. At this point RunFor(1) is | 492 // ERR_IO_PENDING. The current step is still 1. At this point RunFor(1) is |
| 493 // called which will cause the write callback to be invoked, and will then | 493 // called which will cause the write callback to be invoked, and will then |
| 494 // stop. The current state is now 2. RunFor(1) is called again, which | 494 // stop. The current state is now 2. RunFor(1) is called again, which |
| 495 // causes the read callback to be invoked, and will then stop. Then current | 495 // causes the read callback to be invoked, and will then stop. Then current |
| 496 // step is 2. Write() is called again. Then next available write is | 496 // step is 2. Write() is called again. Then next available write is |
| 497 // synchronous so the call to Write() returns length. | 497 // synchronous so the call to Write() returns length. |
| 498 // | 498 // |
| 499 // For examples of how to use this class, see: | 499 // For examples of how to use this class, see: |
| 500 // deterministic_socket_data_unittests.cc | 500 // deterministic_socket_data_unittests.cc |
| 501 class DeterministicSocketData : public StaticSocketDataProvider { | 501 class DeterministicSocketData { |
| 502 public: | 502 public: |
| 503 // The Delegate is an abstract interface which handles the communication from | 503 // The Delegate is an abstract interface which handles the communication from |
| 504 // the DeterministicSocketData to the Deterministic MockSocket. The | 504 // the DeterministicSocketData to the Deterministic MockSocket. The |
| 505 // MockSockets directly store a pointer to the DeterministicSocketData, | 505 // MockSockets directly store a pointer to the DeterministicSocketData, |
| 506 // whereas the DeterministicSocketData only stores a pointer to the | 506 // whereas the DeterministicSocketData only stores a pointer to the |
| 507 // abstract Delegate interface. | 507 // abstract Delegate interface. |
| 508 class Delegate { | 508 class Delegate { |
| 509 public: | 509 public: |
| 510 // Returns true if there is currently a write pending. That is to say, if | 510 // Returns true if there is currently a write pending. That is to say, if |
| 511 // an asynchronous write has been started but the callback has not been | 511 // an asynchronous write has been started but the callback has not been |
| (...skipping 11 matching lines...) Expand all Loading... |
| 523 protected: | 523 protected: |
| 524 virtual ~Delegate() {} | 524 virtual ~Delegate() {} |
| 525 }; | 525 }; |
| 526 | 526 |
| 527 // |reads| the list of MockRead completions. | 527 // |reads| the list of MockRead completions. |
| 528 // |writes| the list of MockWrite completions. | 528 // |writes| the list of MockWrite completions. |
| 529 DeterministicSocketData(MockRead* reads, | 529 DeterministicSocketData(MockRead* reads, |
| 530 size_t reads_count, | 530 size_t reads_count, |
| 531 MockWrite* writes, | 531 MockWrite* writes, |
| 532 size_t writes_count); | 532 size_t writes_count); |
| 533 ~DeterministicSocketData() override; | 533 ~DeterministicSocketData(); |
| 534 | 534 |
| 535 // Consume all the data up to the give stop point (via SetStop()). | 535 // Consume all the data up to the give stop point (via SetStop()). |
| 536 void Run(); | 536 void Run(); |
| 537 | 537 |
| 538 // Set the stop point to be |steps| from now, and then invoke Run(). | 538 // Set the stop point to be |steps| from now, and then invoke Run(). |
| 539 void RunFor(int steps); | 539 void RunFor(int steps); |
| 540 | 540 |
| 541 // Stop at step |seq|, which must be in the future. | 541 // Stop at step |seq|, which must be in the future. |
| 542 virtual void SetStop(int seq); | 542 void SetStop(int seq); |
| 543 | 543 |
| 544 // Stop |seq| steps after the current step. | 544 // Stop |seq| steps after the current step. |
| 545 virtual void StopAfter(int seq); | 545 void StopAfter(int seq); |
| 546 |
| 546 bool stopped() const { return stopped_; } | 547 bool stopped() const { return stopped_; } |
| 547 void SetStopped(bool val) { stopped_ = val; } | 548 void SetStopped(bool val) { stopped_ = val; } |
| 548 MockRead& current_read() { return current_read_; } | 549 MockRead& current_read() { return current_read_; } |
| 549 MockWrite& current_write() { return current_write_; } | 550 MockWrite& current_write() { return current_write_; } |
| 550 int sequence_number() const { return sequence_number_; } | 551 int sequence_number() const { return sequence_number_; } |
| 551 void set_delegate(base::WeakPtr<Delegate> delegate) { delegate_ = delegate; } | 552 void set_delegate(base::WeakPtr<Delegate> delegate) { delegate_ = delegate; } |
| 552 | 553 MockConnect connect_data() const { return connect_; } |
| 553 // StaticSocketDataProvider: | 554 void set_connect_data(const MockConnect& connect) { connect_ = connect; } |
| 554 | 555 |
| 555 // When the socket calls Read(), that calls OnRead(), and expects either | 556 // When the socket calls Read(), that calls OnRead(), and expects either |
| 556 // ERR_IO_PENDING or data. | 557 // ERR_IO_PENDING or data. |
| 557 MockRead OnRead() override; | 558 MockRead OnRead(); |
| 558 | 559 |
| 559 // When the socket calls Write(), it always completes synchronously. OnWrite() | 560 // When the socket calls Write(), it always completes synchronously. OnWrite() |
| 560 // checks to make sure the written data matches the expected data. The | 561 // checks to make sure the written data matches the expected data. The |
| 561 // callback will not be invoked until its sequence number is reached. | 562 // callback will not be invoked until its sequence number is reached. |
| 562 MockWriteResult OnWrite(const std::string& data) override; | 563 MockWriteResult OnWrite(const std::string& data); |
| 563 void Reset() override; | 564 |
| 564 void CompleteRead() override {} | 565 bool AllReadDataConsumed() const; |
| 566 bool AllWriteDataConsumed() const; |
| 565 | 567 |
| 566 private: | 568 private: |
| 567 // Invoke the read and write callbacks, if the timing is appropriate. | 569 // Invoke the read and write callbacks, if the timing is appropriate. |
| 568 void InvokeCallbacks(); | 570 void InvokeCallbacks(); |
| 569 | 571 |
| 570 void NextStep(); | 572 void NextStep(); |
| 571 | 573 |
| 572 void VerifyCorrectSequenceNumbers(MockRead* reads, | 574 void VerifyCorrectSequenceNumbers(MockRead* reads, |
| 573 size_t reads_count, | 575 size_t reads_count, |
| 574 MockWrite* writes, | 576 MockWrite* writes, |
| 575 size_t writes_count); | 577 size_t writes_count); |
| 576 | 578 StaticSocketDataHelper helper_; |
| 579 MockConnect connect_; |
| 577 int sequence_number_; | 580 int sequence_number_; |
| 578 MockRead current_read_; | 581 MockRead current_read_; |
| 579 MockWrite current_write_; | 582 MockWrite current_write_; |
| 580 int stopping_sequence_number_; | 583 int stopping_sequence_number_; |
| 581 bool stopped_; | 584 bool stopped_; |
| 582 base::WeakPtr<Delegate> delegate_; | 585 base::WeakPtr<Delegate> delegate_; |
| 583 bool print_debug_; | 586 bool print_debug_; |
| 584 bool is_running_; | 587 bool is_running_; |
| 585 }; | 588 }; |
| 586 | 589 |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 829 CompletionCallback read_callback_; | 832 CompletionCallback read_callback_; |
| 830 DeterministicSocketData* data_; | 833 DeterministicSocketData* data_; |
| 831 bool was_used_to_convey_data_; | 834 bool was_used_to_convey_data_; |
| 832 bool peer_closed_connection_; | 835 bool peer_closed_connection_; |
| 833 BoundNetLog net_log_; | 836 BoundNetLog net_log_; |
| 834 }; | 837 }; |
| 835 | 838 |
| 836 // Mock UDP socket to be used in conjunction with DeterministicSocketData. | 839 // Mock UDP socket to be used in conjunction with DeterministicSocketData. |
| 837 class DeterministicMockUDPClientSocket | 840 class DeterministicMockUDPClientSocket |
| 838 : public DatagramClientSocket, | 841 : public DatagramClientSocket, |
| 839 public AsyncSocket, | |
| 840 public DeterministicSocketData::Delegate, | 842 public DeterministicSocketData::Delegate, |
| 841 public base::SupportsWeakPtr<DeterministicMockUDPClientSocket> { | 843 public base::SupportsWeakPtr<DeterministicMockUDPClientSocket> { |
| 842 public: | 844 public: |
| 843 DeterministicMockUDPClientSocket(net::NetLog* net_log, | 845 DeterministicMockUDPClientSocket(net::NetLog* net_log, |
| 844 DeterministicSocketData* data); | 846 DeterministicSocketData* data); |
| 845 ~DeterministicMockUDPClientSocket() override; | 847 ~DeterministicMockUDPClientSocket() override; |
| 846 | 848 |
| 847 // DeterministicSocketData::Delegate: | 849 // DeterministicSocketData::Delegate: |
| 848 bool WritePending() const override; | 850 bool WritePending() const override; |
| 849 bool ReadPending() const override; | 851 bool ReadPending() const override; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 862 | 864 |
| 863 // DatagramSocket implementation. | 865 // DatagramSocket implementation. |
| 864 void Close() override; | 866 void Close() override; |
| 865 int GetPeerAddress(IPEndPoint* address) const override; | 867 int GetPeerAddress(IPEndPoint* address) const override; |
| 866 int GetLocalAddress(IPEndPoint* address) const override; | 868 int GetLocalAddress(IPEndPoint* address) const override; |
| 867 const BoundNetLog& NetLog() const override; | 869 const BoundNetLog& NetLog() const override; |
| 868 | 870 |
| 869 // DatagramClientSocket implementation. | 871 // DatagramClientSocket implementation. |
| 870 int Connect(const IPEndPoint& address) override; | 872 int Connect(const IPEndPoint& address) override; |
| 871 | 873 |
| 872 // AsyncSocket implementation. | |
| 873 void OnReadComplete(const MockRead& data) override; | |
| 874 void OnWriteComplete(int rv) override; | |
| 875 void OnConnectComplete(const MockConnect& data) override; | |
| 876 | |
| 877 void set_source_port(uint16 port) { source_port_ = port; } | 874 void set_source_port(uint16 port) { source_port_ = port; } |
| 878 | 875 |
| 879 private: | 876 private: |
| 880 bool connected_; | 877 bool connected_; |
| 881 IPEndPoint peer_address_; | 878 IPEndPoint peer_address_; |
| 882 DeterministicSocketHelper helper_; | 879 DeterministicSocketHelper helper_; |
| 883 uint16 source_port_; // Ephemeral source port. | 880 uint16 source_port_; // Ephemeral source port. |
| 884 | 881 |
| 885 DISALLOW_COPY_AND_ASSIGN(DeterministicMockUDPClientSocket); | 882 DISALLOW_COPY_AND_ASSIGN(DeterministicMockUDPClientSocket); |
| 886 }; | 883 }; |
| 887 | 884 |
| 888 // Mock TCP socket to be used in conjunction with DeterministicSocketData. | 885 // Mock TCP socket to be used in conjunction with DeterministicSocketData. |
| 889 class DeterministicMockTCPClientSocket | 886 class DeterministicMockTCPClientSocket |
| 890 : public MockClientSocket, | 887 : public MockClientSocket, |
| 891 public AsyncSocket, | |
| 892 public DeterministicSocketData::Delegate, | 888 public DeterministicSocketData::Delegate, |
| 893 public base::SupportsWeakPtr<DeterministicMockTCPClientSocket> { | 889 public base::SupportsWeakPtr<DeterministicMockTCPClientSocket> { |
| 894 public: | 890 public: |
| 895 DeterministicMockTCPClientSocket(net::NetLog* net_log, | 891 DeterministicMockTCPClientSocket(net::NetLog* net_log, |
| 896 DeterministicSocketData* data); | 892 DeterministicSocketData* data); |
| 897 ~DeterministicMockTCPClientSocket() override; | 893 ~DeterministicMockTCPClientSocket() override; |
| 898 | 894 |
| 899 // DeterministicSocketData::Delegate: | 895 // DeterministicSocketData::Delegate: |
| 900 bool WritePending() const override; | 896 bool WritePending() const override; |
| 901 bool ReadPending() const override; | 897 bool ReadPending() const override; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 913 // StreamSocket: | 909 // StreamSocket: |
| 914 int Connect(const CompletionCallback& callback) override; | 910 int Connect(const CompletionCallback& callback) override; |
| 915 void Disconnect() override; | 911 void Disconnect() override; |
| 916 bool IsConnected() const override; | 912 bool IsConnected() const override; |
| 917 bool IsConnectedAndIdle() const override; | 913 bool IsConnectedAndIdle() const override; |
| 918 bool WasEverUsed() const override; | 914 bool WasEverUsed() const override; |
| 919 bool UsingTCPFastOpen() const override; | 915 bool UsingTCPFastOpen() const override; |
| 920 bool WasNpnNegotiated() const override; | 916 bool WasNpnNegotiated() const override; |
| 921 bool GetSSLInfo(SSLInfo* ssl_info) override; | 917 bool GetSSLInfo(SSLInfo* ssl_info) override; |
| 922 | 918 |
| 923 // AsyncSocket: | |
| 924 void OnReadComplete(const MockRead& data) override; | |
| 925 void OnWriteComplete(int rv) override; | |
| 926 void OnConnectComplete(const MockConnect& data) override; | |
| 927 | |
| 928 private: | 919 private: |
| 929 DeterministicSocketHelper helper_; | 920 DeterministicSocketHelper helper_; |
| 930 | 921 |
| 931 DISALLOW_COPY_AND_ASSIGN(DeterministicMockTCPClientSocket); | 922 DISALLOW_COPY_AND_ASSIGN(DeterministicMockTCPClientSocket); |
| 932 }; | 923 }; |
| 933 | 924 |
| 934 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { | 925 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { |
| 935 public: | 926 public: |
| 936 MockSSLClientSocket(scoped_ptr<ClientSocketHandle> transport_socket, | 927 MockSSLClientSocket(scoped_ptr<ClientSocketHandle> transport_socket, |
| 937 const HostPortPair& host_and_port, | 928 const HostPortPair& host_and_port, |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1294 | 1285 |
| 1295 extern const char kSOCKS5OkRequest[]; | 1286 extern const char kSOCKS5OkRequest[]; |
| 1296 extern const int kSOCKS5OkRequestLength; | 1287 extern const int kSOCKS5OkRequestLength; |
| 1297 | 1288 |
| 1298 extern const char kSOCKS5OkResponse[]; | 1289 extern const char kSOCKS5OkResponse[]; |
| 1299 extern const int kSOCKS5OkResponseLength; | 1290 extern const int kSOCKS5OkResponseLength; |
| 1300 | 1291 |
| 1301 } // namespace net | 1292 } // namespace net |
| 1302 | 1293 |
| 1303 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 1294 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |