Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: net/socket/socket_test_util.h

Issue 2593063003: Add Socket::ReadIfReady() (Closed)
Patch Set: Rebased Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/socket/socket_posix.cc ('k') | net/socket/socket_test_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 void set_enable_read_if_ready(bool enable_read_if_ready) {
522 enable_read_if_ready_ = enable_read_if_ready;
523 }
524
521 // ClientSocketFactory 525 // ClientSocketFactory
522 std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( 526 std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket(
523 DatagramSocket::BindType bind_type, 527 DatagramSocket::BindType bind_type,
524 const RandIntCallback& rand_int_cb, 528 const RandIntCallback& rand_int_cb,
525 NetLog* net_log, 529 NetLog* net_log,
526 const NetLogSource& source) override; 530 const NetLogSource& source) override;
527 std::unique_ptr<StreamSocket> CreateTransportClientSocket( 531 std::unique_ptr<StreamSocket> CreateTransportClientSocket(
528 const AddressList& addresses, 532 const AddressList& addresses,
529 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, 533 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
530 NetLog* net_log, 534 NetLog* net_log,
531 const NetLogSource& source) override; 535 const NetLogSource& source) override;
532 std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( 536 std::unique_ptr<SSLClientSocket> CreateSSLClientSocket(
533 std::unique_ptr<ClientSocketHandle> transport_socket, 537 std::unique_ptr<ClientSocketHandle> transport_socket,
534 const HostPortPair& host_and_port, 538 const HostPortPair& host_and_port,
535 const SSLConfig& ssl_config, 539 const SSLConfig& ssl_config,
536 const SSLClientSocketContext& context) override; 540 const SSLClientSocketContext& context) override;
537 void ClearSSLSessionCache() override; 541 void ClearSSLSessionCache() override;
538 542
539 const std::vector<uint16_t>& udp_client_socket_ports() const { 543 const std::vector<uint16_t>& udp_client_socket_ports() const {
540 return udp_client_socket_ports_; 544 return udp_client_socket_ports_;
541 } 545 }
542 546
543 private: 547 private:
544 SocketDataProviderArray<SocketDataProvider> mock_data_; 548 SocketDataProviderArray<SocketDataProvider> mock_data_;
545 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; 549 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_;
546 std::vector<uint16_t> udp_client_socket_ports_; 550 std::vector<uint16_t> udp_client_socket_ports_;
547 551
552 // If true, ReadIfReady() is enabled; otherwise ReadIfReady() returns
553 // ERR_READ_IF_READY_NOT_IMPLEMENTED.
554 bool enable_read_if_ready_;
555
548 DISALLOW_COPY_AND_ASSIGN(MockClientSocketFactory); 556 DISALLOW_COPY_AND_ASSIGN(MockClientSocketFactory);
549 }; 557 };
550 558
551 class MockClientSocket : public SSLClientSocket { 559 class MockClientSocket : public SSLClientSocket {
552 public: 560 public:
553 // The NetLogWithSource is needed to test LoadTimingInfo, which uses NetLog 561 // The NetLogWithSource is needed to test LoadTimingInfo, which uses NetLog
554 // IDs as 562 // IDs as
555 // unique socket IDs. 563 // unique socket IDs.
556 explicit MockClientSocket(const NetLogWithSource& net_log); 564 explicit MockClientSocket(const NetLogWithSource& net_log);
557 565
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 net::NetLog* net_log, 628 net::NetLog* net_log,
621 SocketDataProvider* socket); 629 SocketDataProvider* socket);
622 ~MockTCPClientSocket() override; 630 ~MockTCPClientSocket() override;
623 631
624 const AddressList& addresses() const { return addresses_; } 632 const AddressList& addresses() const { return addresses_; }
625 633
626 // Socket implementation. 634 // Socket implementation.
627 int Read(IOBuffer* buf, 635 int Read(IOBuffer* buf,
628 int buf_len, 636 int buf_len,
629 const CompletionCallback& callback) override; 637 const CompletionCallback& callback) override;
638 int ReadIfReady(IOBuffer* buf,
639 int buf_len,
640 const CompletionCallback& callback) override;
630 int Write(IOBuffer* buf, 641 int Write(IOBuffer* buf,
631 int buf_len, 642 int buf_len,
632 const CompletionCallback& callback) override; 643 const CompletionCallback& callback) override;
633 644
634 // StreamSocket implementation. 645 // StreamSocket implementation.
635 int Connect(const CompletionCallback& callback) override; 646 int Connect(const CompletionCallback& callback) override;
636 void Disconnect() override; 647 void Disconnect() override;
637 bool IsConnected() const override; 648 bool IsConnected() const override;
638 bool IsConnectedAndIdle() const override; 649 bool IsConnectedAndIdle() const override;
639 int GetPeerAddress(IPEndPoint* address) const override; 650 int GetPeerAddress(IPEndPoint* address) const override;
640 bool WasEverUsed() const override; 651 bool WasEverUsed() const override;
641 void EnableTCPFastOpenIfSupported() override; 652 void EnableTCPFastOpenIfSupported() override;
642 bool GetSSLInfo(SSLInfo* ssl_info) override; 653 bool GetSSLInfo(SSLInfo* ssl_info) override;
643 void GetConnectionAttempts(ConnectionAttempts* out) const override; 654 void GetConnectionAttempts(ConnectionAttempts* out) const override;
644 void ClearConnectionAttempts() override; 655 void ClearConnectionAttempts() override;
645 void AddConnectionAttempts(const ConnectionAttempts& attempts) override; 656 void AddConnectionAttempts(const ConnectionAttempts& attempts) override;
646 657
647 // AsyncSocket: 658 // AsyncSocket:
648 void OnReadComplete(const MockRead& data) override; 659 void OnReadComplete(const MockRead& data) override;
649 void OnWriteComplete(int rv) override; 660 void OnWriteComplete(int rv) override;
650 void OnConnectComplete(const MockConnect& data) override; 661 void OnConnectComplete(const MockConnect& data) override;
651 void OnDataProviderDestroyed() override; 662 void OnDataProviderDestroyed() override;
652 663
664 void set_enable_read_if_ready(bool enable_read_if_ready) {
665 enable_read_if_ready_ = enable_read_if_ready;
666 }
667
653 private: 668 private:
654 int CompleteRead(); 669 void RetryRead(int rv);
655 670 int ReadIfReadyImpl(IOBuffer* buf,
671 int buf_len,
672 const CompletionCallback& callback);
656 AddressList addresses_; 673 AddressList addresses_;
657 674
658 SocketDataProvider* data_; 675 SocketDataProvider* data_;
659 int read_offset_; 676 int read_offset_;
660 MockRead read_data_; 677 MockRead read_data_;
661 bool need_read_data_; 678 bool need_read_data_;
662 679
663 // True if the peer has closed the connection. This allows us to simulate 680 // 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 681 // the recv(..., MSG_PEEK) call in the IsConnectedAndIdle method of the real
665 // TCPClientSocket. 682 // TCPClientSocket.
666 bool peer_closed_connection_; 683 bool peer_closed_connection_;
667 684
668 // While an asynchronous read is pending, we save our user-buffer state. 685 // While an asynchronous read is pending, we save our user-buffer state.
669 scoped_refptr<IOBuffer> pending_read_buf_; 686 scoped_refptr<IOBuffer> pending_read_buf_;
670 int pending_read_buf_len_; 687 int pending_read_buf_len_;
688 CompletionCallback pending_read_callback_;
689
690 // Non-null when a ReadIfReady() is pending.
691 CompletionCallback pending_read_if_ready_callback_;
692
671 CompletionCallback pending_connect_callback_; 693 CompletionCallback pending_connect_callback_;
672 CompletionCallback pending_read_callback_;
673 CompletionCallback pending_write_callback_; 694 CompletionCallback pending_write_callback_;
674 bool was_used_to_convey_data_; 695 bool was_used_to_convey_data_;
675 696
697 // If true, ReadIfReady() is enabled; otherwise ReadIfReady() returns
698 // ERR_READ_IF_READY_NOT_IMPLEMENTED.
699 bool enable_read_if_ready_;
700
676 ConnectionAttempts connection_attempts_; 701 ConnectionAttempts connection_attempts_;
677 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 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 1047
1020 // Helper function to get the total data size of the MockReads in |reads|. 1048 // Helper function to get the total data size of the MockReads in |reads|.
1021 int64_t CountReadBytes(const MockRead reads[], size_t reads_size); 1049 int64_t CountReadBytes(const MockRead reads[], size_t reads_size);
1022 1050
1023 // Helper function to get the total data size of the MockWrites in |writes|. 1051 // Helper function to get the total data size of the MockWrites in |writes|.
1024 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size); 1052 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size);
1025 1053
1026 } // namespace net 1054 } // namespace net
1027 1055
1028 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ 1056 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_
OLDNEW
« no previous file with comments | « net/socket/socket_posix.cc ('k') | net/socket/socket_test_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698