| 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 void RetryRead(int rv); |
| 669 int ReadIfReadyHelper(IOBuffer* buf, |
| 670 int buf_len, |
| 671 const CompletionCallback& callback); |
| 672 void RunReadIfReadyCallback(int result); |
| 655 | 673 |
| 656 AddressList addresses_; | 674 AddressList addresses_; |
| 657 | 675 |
| 658 SocketDataProvider* data_; | 676 SocketDataProvider* data_; |
| 659 int read_offset_; | 677 int read_offset_; |
| 660 MockRead read_data_; | 678 MockRead read_data_; |
| 661 bool need_read_data_; | 679 bool need_read_data_; |
| 662 | 680 |
| 663 // True if the peer has closed the connection. This allows us to simulate | 681 // 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 | 682 // the recv(..., MSG_PEEK) call in the IsConnectedAndIdle method of the real |
| 665 // TCPClientSocket. | 683 // TCPClientSocket. |
| 666 bool peer_closed_connection_; | 684 bool peer_closed_connection_; |
| 667 | 685 |
| 668 // While an asynchronous read is pending, we save our user-buffer state. | 686 // While an asynchronous read is pending, we save our user-buffer state. |
| 669 scoped_refptr<IOBuffer> pending_read_buf_; | 687 scoped_refptr<IOBuffer> pending_read_buf_; |
| 670 int pending_read_buf_len_; | 688 int pending_read_buf_len_; |
| 689 CompletionCallback pending_read_callback_; |
| 690 |
| 691 // Non-null when a ReadIfReady() is pending. |
| 692 CompletionCallback pending_read_if_ready_callback_; |
| 693 |
| 671 CompletionCallback pending_connect_callback_; | 694 CompletionCallback pending_connect_callback_; |
| 672 CompletionCallback pending_read_callback_; | |
| 673 CompletionCallback pending_write_callback_; | 695 CompletionCallback pending_write_callback_; |
| 674 bool was_used_to_convey_data_; | 696 bool was_used_to_convey_data_; |
| 697 bool enable_read_if_ready_; |
| 675 | 698 |
| 676 ConnectionAttempts connection_attempts_; | 699 ConnectionAttempts connection_attempts_; |
| 677 | 700 |
| 701 base::WeakPtrFactory<MockTCPClientSocket> weak_factory_; |
| 702 |
| 678 DISALLOW_COPY_AND_ASSIGN(MockTCPClientSocket); | 703 DISALLOW_COPY_AND_ASSIGN(MockTCPClientSocket); |
| 679 }; | 704 }; |
| 680 | 705 |
| 681 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { | 706 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { |
| 682 public: | 707 public: |
| 683 MockSSLClientSocket(std::unique_ptr<ClientSocketHandle> transport_socket, | 708 MockSSLClientSocket(std::unique_ptr<ClientSocketHandle> transport_socket, |
| 684 const HostPortPair& host_and_port, | 709 const HostPortPair& host_and_port, |
| 685 const SSLConfig& ssl_config, | 710 const SSLConfig& ssl_config, |
| 686 SSLSocketDataProvider* socket); | 711 SSLSocketDataProvider* socket); |
| 687 ~MockSSLClientSocket() override; | 712 ~MockSSLClientSocket() override; |
| 688 | 713 |
| 689 // Socket implementation. | 714 // Socket implementation. |
| 690 int Read(IOBuffer* buf, | 715 int Read(IOBuffer* buf, |
| 691 int buf_len, | 716 int buf_len, |
| 692 const CompletionCallback& callback) override; | 717 const CompletionCallback& callback) override; |
| 718 int ReadIfReady(IOBuffer* buf, |
| 719 int buf_len, |
| 720 const CompletionCallback& callback) override; |
| 693 int Write(IOBuffer* buf, | 721 int Write(IOBuffer* buf, |
| 694 int buf_len, | 722 int buf_len, |
| 695 const CompletionCallback& callback) override; | 723 const CompletionCallback& callback) override; |
| 696 | 724 |
| 697 // StreamSocket implementation. | 725 // StreamSocket implementation. |
| 698 int Connect(const CompletionCallback& callback) override; | 726 int Connect(const CompletionCallback& callback) override; |
| 699 void Disconnect() override; | 727 void Disconnect() override; |
| 700 bool IsConnected() const override; | 728 bool IsConnected() const override; |
| 701 bool IsConnectedAndIdle() const override; | 729 bool IsConnectedAndIdle() const override; |
| 702 bool WasEverUsed() const override; | 730 bool WasEverUsed() const override; |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 | 1043 |
| 1016 // Helper function to get the total data size of the MockReads in |reads|. | 1044 // Helper function to get the total data size of the MockReads in |reads|. |
| 1017 int64_t CountReadBytes(const MockRead reads[], size_t reads_size); | 1045 int64_t CountReadBytes(const MockRead reads[], size_t reads_size); |
| 1018 | 1046 |
| 1019 // Helper function to get the total data size of the MockWrites in |writes|. | 1047 // Helper function to get the total data size of the MockWrites in |writes|. |
| 1020 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size); | 1048 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size); |
| 1021 | 1049 |
| 1022 } // namespace net | 1050 } // namespace net |
| 1023 | 1051 |
| 1024 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 1052 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |