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