| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 ~MockClientSocketFactory() override; | 511 ~MockClientSocketFactory() override; |
| 512 | 512 |
| 513 void AddSocketDataProvider(SocketDataProvider* socket); | 513 void AddSocketDataProvider(SocketDataProvider* socket); |
| 514 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); | 514 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); |
| 515 void ResetNextMockIndexes(); | 515 void ResetNextMockIndexes(); |
| 516 | 516 |
| 517 SocketDataProviderArray<SocketDataProvider>& mock_data() { | 517 SocketDataProviderArray<SocketDataProvider>& mock_data() { |
| 518 return mock_data_; | 518 return mock_data_; |
| 519 } | 519 } |
| 520 | 520 |
| 521 // If |enable_read_if_ready|, ReadIfReady() will be implemented. |
| 522 void set_enable_read_if_ready(bool enable_read_if_ready) { |
| 523 enable_read_if_ready_ = enable_read_if_ready; |
| 524 } |
| 525 |
| 521 // ClientSocketFactory | 526 // ClientSocketFactory |
| 522 std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( | 527 std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
| 523 DatagramSocket::BindType bind_type, | 528 DatagramSocket::BindType bind_type, |
| 524 const RandIntCallback& rand_int_cb, | 529 const RandIntCallback& rand_int_cb, |
| 525 NetLog* net_log, | 530 NetLog* net_log, |
| 526 const NetLogSource& source) override; | 531 const NetLogSource& source) override; |
| 527 std::unique_ptr<StreamSocket> CreateTransportClientSocket( | 532 std::unique_ptr<StreamSocket> CreateTransportClientSocket( |
| 528 const AddressList& addresses, | 533 const AddressList& addresses, |
| 529 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, | 534 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, |
| 530 NetLog* net_log, | 535 NetLog* net_log, |
| 531 const NetLogSource& source) override; | 536 const NetLogSource& source) override; |
| 532 std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( | 537 std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
| 533 std::unique_ptr<ClientSocketHandle> transport_socket, | 538 std::unique_ptr<ClientSocketHandle> transport_socket, |
| 534 const HostPortPair& host_and_port, | 539 const HostPortPair& host_and_port, |
| 535 const SSLConfig& ssl_config, | 540 const SSLConfig& ssl_config, |
| 536 const SSLClientSocketContext& context) override; | 541 const SSLClientSocketContext& context) override; |
| 537 void ClearSSLSessionCache() override; | 542 void ClearSSLSessionCache() override; |
| 538 | 543 |
| 539 const std::vector<uint16_t>& udp_client_socket_ports() const { | 544 const std::vector<uint16_t>& udp_client_socket_ports() const { |
| 540 return udp_client_socket_ports_; | 545 return udp_client_socket_ports_; |
| 541 } | 546 } |
| 542 | 547 |
| 543 private: | 548 private: |
| 544 SocketDataProviderArray<SocketDataProvider> mock_data_; | 549 SocketDataProviderArray<SocketDataProvider> mock_data_; |
| 545 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; | 550 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; |
| 546 std::vector<uint16_t> udp_client_socket_ports_; | 551 std::vector<uint16_t> udp_client_socket_ports_; |
| 547 | 552 |
| 553 bool enable_read_if_ready_; |
| 548 DISALLOW_COPY_AND_ASSIGN(MockClientSocketFactory); | 554 DISALLOW_COPY_AND_ASSIGN(MockClientSocketFactory); |
| 549 }; | 555 }; |
| 550 | 556 |
| 551 class MockClientSocket : public SSLClientSocket { | 557 class MockClientSocket : public SSLClientSocket { |
| 552 public: | 558 public: |
| 553 // The NetLogWithSource is needed to test LoadTimingInfo, which uses NetLog | 559 // The NetLogWithSource is needed to test LoadTimingInfo, which uses NetLog |
| 554 // IDs as | 560 // IDs as |
| 555 // unique socket IDs. | 561 // unique socket IDs. |
| 556 explicit MockClientSocket(const NetLogWithSource& net_log); | 562 explicit MockClientSocket(const NetLogWithSource& net_log); |
| 557 | 563 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 net::NetLog* net_log, | 626 net::NetLog* net_log, |
| 621 SocketDataProvider* socket); | 627 SocketDataProvider* socket); |
| 622 ~MockTCPClientSocket() override; | 628 ~MockTCPClientSocket() override; |
| 623 | 629 |
| 624 const AddressList& addresses() const { return addresses_; } | 630 const AddressList& addresses() const { return addresses_; } |
| 625 | 631 |
| 626 // Socket implementation. | 632 // Socket implementation. |
| 627 int Read(IOBuffer* buf, | 633 int Read(IOBuffer* buf, |
| 628 int buf_len, | 634 int buf_len, |
| 629 const CompletionCallback& callback) override; | 635 const CompletionCallback& callback) override; |
| 636 int ReadIfReady(IOBuffer* buf, |
| 637 int buf_len, |
| 638 const CompletionCallback& callback) override; |
| 630 int Write(IOBuffer* buf, | 639 int Write(IOBuffer* buf, |
| 631 int buf_len, | 640 int buf_len, |
| 632 const CompletionCallback& callback) override; | 641 const CompletionCallback& callback) override; |
| 633 | 642 |
| 634 // StreamSocket implementation. | 643 // StreamSocket implementation. |
| 635 int Connect(const CompletionCallback& callback) override; | 644 int Connect(const CompletionCallback& callback) override; |
| 636 void Disconnect() override; | 645 void Disconnect() override; |
| 637 bool IsConnected() const override; | 646 bool IsConnected() const override; |
| 638 bool IsConnectedAndIdle() const override; | 647 bool IsConnectedAndIdle() const override; |
| 639 int GetPeerAddress(IPEndPoint* address) const override; | 648 int GetPeerAddress(IPEndPoint* address) const override; |
| 640 bool WasEverUsed() const override; | 649 bool WasEverUsed() const override; |
| 641 void EnableTCPFastOpenIfSupported() override; | 650 void EnableTCPFastOpenIfSupported() override; |
| 642 bool GetSSLInfo(SSLInfo* ssl_info) override; | 651 bool GetSSLInfo(SSLInfo* ssl_info) override; |
| 643 void GetConnectionAttempts(ConnectionAttempts* out) const override; | 652 void GetConnectionAttempts(ConnectionAttempts* out) const override; |
| 644 void ClearConnectionAttempts() override; | 653 void ClearConnectionAttempts() override; |
| 645 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; | 654 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; |
| 646 | 655 |
| 647 // AsyncSocket: | 656 // AsyncSocket: |
| 648 void OnReadComplete(const MockRead& data) override; | 657 void OnReadComplete(const MockRead& data) override; |
| 649 void OnWriteComplete(int rv) override; | 658 void OnWriteComplete(int rv) override; |
| 650 void OnConnectComplete(const MockConnect& data) override; | 659 void OnConnectComplete(const MockConnect& data) override; |
| 651 void OnDataProviderDestroyed() override; | 660 void OnDataProviderDestroyed() override; |
| 652 | 661 |
| 662 // If |enable_read_if_ready|, ReadIfReady() will be implemented. |
| 663 void set_enable_read_if_ready(bool enable_read_if_ready) { |
| 664 enable_read_if_ready_ = enable_read_if_ready; |
| 665 } |
| 666 |
| 653 private: | 667 private: |
| 654 int CompleteRead(); | 668 enum PendingReadType { |
| 669 NONE, |
| 670 READ, |
| 671 READ_IF_READY, |
| 672 }; |
| 673 // Helper method shared between Read() and ReadIfReady(). |
| 674 int ReadCommon(IOBuffer* buf, |
| 675 int buf_len, |
| 676 const CompletionCallback& callback, |
| 677 bool use_read_if_ready); |
| 678 |
| 679 int CompleteRead(bool read_if_ready_used); |
| 680 void RunReadCallback(const CompletionCallback& callback, int result); |
| 681 void RunReadCallbackAsync(const CompletionCallback& callback, int result); |
| 655 | 682 |
| 656 AddressList addresses_; | 683 AddressList addresses_; |
| 657 | 684 |
| 658 SocketDataProvider* data_; | 685 SocketDataProvider* data_; |
| 659 int read_offset_; | 686 int read_offset_; |
| 660 MockRead read_data_; | 687 MockRead read_data_; |
| 661 bool need_read_data_; | 688 bool need_read_data_; |
| 662 | 689 |
| 663 // True if the peer has closed the connection. This allows us to simulate | 690 // True if the peer has closed the connection. This allows us to simulate |
| 664 // the recv(..., MSG_PEEK) call in the IsConnectedAndIdle method of the real | 691 // the recv(..., MSG_PEEK) call in the IsConnectedAndIdle method of the real |
| 665 // TCPClientSocket. | 692 // TCPClientSocket. |
| 666 bool peer_closed_connection_; | 693 bool peer_closed_connection_; |
| 667 | 694 |
| 668 // While an asynchronous read is pending, we save our user-buffer state. | 695 // While an asynchronous read is pending, we save our user-buffer state. |
| 669 scoped_refptr<IOBuffer> pending_read_buf_; | 696 scoped_refptr<IOBuffer> pending_read_buf_; |
| 670 int pending_read_buf_len_; | 697 int pending_read_buf_len_; |
| 671 CompletionCallback pending_connect_callback_; | 698 CompletionCallback pending_connect_callback_; |
| 672 CompletionCallback pending_read_callback_; | 699 CompletionCallback pending_read_callback_; |
| 673 CompletionCallback pending_write_callback_; | 700 CompletionCallback pending_write_callback_; |
| 674 bool was_used_to_convey_data_; | 701 bool was_used_to_convey_data_; |
| 702 bool enable_read_if_ready_; |
| 703 PendingReadType pending_read_type_; |
| 675 | 704 |
| 676 ConnectionAttempts connection_attempts_; | 705 ConnectionAttempts connection_attempts_; |
| 677 | 706 |
| 707 base::WeakPtrFactory<MockTCPClientSocket> weak_factory_; |
| 708 |
| 678 DISALLOW_COPY_AND_ASSIGN(MockTCPClientSocket); | 709 DISALLOW_COPY_AND_ASSIGN(MockTCPClientSocket); |
| 679 }; | 710 }; |
| 680 | 711 |
| 681 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { | 712 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { |
| 682 public: | 713 public: |
| 683 MockSSLClientSocket(std::unique_ptr<ClientSocketHandle> transport_socket, | 714 MockSSLClientSocket(std::unique_ptr<ClientSocketHandle> transport_socket, |
| 684 const HostPortPair& host_and_port, | 715 const HostPortPair& host_and_port, |
| 685 const SSLConfig& ssl_config, | 716 const SSLConfig& ssl_config, |
| 686 SSLSocketDataProvider* socket); | 717 SSLSocketDataProvider* socket); |
| 687 ~MockSSLClientSocket() override; | 718 ~MockSSLClientSocket() override; |
| 688 | 719 |
| 689 // Socket implementation. | 720 // Socket implementation. |
| 690 int Read(IOBuffer* buf, | 721 int Read(IOBuffer* buf, |
| 691 int buf_len, | 722 int buf_len, |
| 692 const CompletionCallback& callback) override; | 723 const CompletionCallback& callback) override; |
| 724 int ReadIfReady(IOBuffer* buf, |
| 725 int buf_len, |
| 726 const CompletionCallback& callback) override; |
| 693 int Write(IOBuffer* buf, | 727 int Write(IOBuffer* buf, |
| 694 int buf_len, | 728 int buf_len, |
| 695 const CompletionCallback& callback) override; | 729 const CompletionCallback& callback) override; |
| 696 | 730 |
| 697 // StreamSocket implementation. | 731 // StreamSocket implementation. |
| 698 int Connect(const CompletionCallback& callback) override; | 732 int Connect(const CompletionCallback& callback) override; |
| 699 void Disconnect() override; | 733 void Disconnect() override; |
| 700 bool IsConnected() const override; | 734 bool IsConnected() const override; |
| 701 bool IsConnectedAndIdle() const override; | 735 bool IsConnectedAndIdle() const override; |
| 702 bool WasEverUsed() const override; | 736 bool WasEverUsed() const override; |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 | 1049 |
| 1016 // Helper function to get the total data size of the MockReads in |reads|. | 1050 // Helper function to get the total data size of the MockReads in |reads|. |
| 1017 int64_t CountReadBytes(const MockRead reads[], size_t reads_size); | 1051 int64_t CountReadBytes(const MockRead reads[], size_t reads_size); |
| 1018 | 1052 |
| 1019 // Helper function to get the total data size of the MockWrites in |writes|. | 1053 // Helper function to get the total data size of the MockWrites in |writes|. |
| 1020 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size); | 1054 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size); |
| 1021 | 1055 |
| 1022 } // namespace net | 1056 } // namespace net |
| 1023 | 1057 |
| 1024 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 1058 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |