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

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

Issue 8801004: base::Bind: Convert StreamSocket::Connect. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « net/socket/client_socket_pool_base_unittest.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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #pragma once 7 #pragma once
8 8
9 #include <cstring> 9 #include <cstring>
10 #include <deque> 10 #include <deque>
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 size_t read_index() const { return read_index_; } 191 size_t read_index() const { return read_index_; }
192 size_t write_index() const { return write_index_; } 192 size_t write_index() const { return write_index_; }
193 size_t read_count() const { return read_count_; } 193 size_t read_count() const { return read_count_; }
194 size_t write_count() const { return write_count_; } 194 size_t write_count() const { return write_count_; }
195 195
196 bool at_read_eof() const { return read_index_ >= read_count_; } 196 bool at_read_eof() const { return read_index_ >= read_count_; }
197 bool at_write_eof() const { return write_index_ >= write_count_; } 197 bool at_write_eof() const { return write_index_ >= write_count_; }
198 198
199 virtual void CompleteRead() {} 199 virtual void CompleteRead() {}
200 200
201 // SocketDataProvider methods: 201 // SocketDataProvider implementation.
202 virtual MockRead GetNextRead() OVERRIDE; 202 virtual MockRead GetNextRead() OVERRIDE;
203 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE; 203 virtual MockWriteResult OnWrite(const std::string& data) OVERRIDE;
204 virtual void Reset() OVERRIDE; 204 virtual void Reset() OVERRIDE;
205 205
206 private: 206 private:
207 MockRead* reads_; 207 MockRead* reads_;
208 size_t read_index_; 208 size_t read_index_;
209 size_t read_count_; 209 size_t read_count_;
210 MockWrite* writes_; 210 MockWrite* writes_;
211 size_t write_index_; 211 size_t write_index_;
212 size_t write_count_; 212 size_t write_count_;
213 213
214 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider); 214 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider);
215 }; 215 };
216 216
217 // SocketDataProvider which can make decisions about next mock reads based on 217 // SocketDataProvider which can make decisions about next mock reads based on
218 // received writes. It can also be used to enforce order of operations, for 218 // received writes. It can also be used to enforce order of operations, for
219 // example that tested code must send the "Hello!" message before receiving 219 // example that tested code must send the "Hello!" message before receiving
220 // response. This is useful for testing conversation-like protocols like FTP. 220 // response. This is useful for testing conversation-like protocols like FTP.
221 class DynamicSocketDataProvider : public SocketDataProvider { 221 class DynamicSocketDataProvider : public SocketDataProvider {
222 public: 222 public:
223 DynamicSocketDataProvider(); 223 DynamicSocketDataProvider();
224 virtual ~DynamicSocketDataProvider(); 224 virtual ~DynamicSocketDataProvider();
225 225
226 int short_read_limit() const { return short_read_limit_; } 226 int short_read_limit() const { return short_read_limit_; }
227 void set_short_read_limit(int limit) { short_read_limit_ = limit; } 227 void set_short_read_limit(int limit) { short_read_limit_ = limit; }
228 228
229 void allow_unconsumed_reads(bool allow) { allow_unconsumed_reads_ = allow; } 229 void allow_unconsumed_reads(bool allow) { allow_unconsumed_reads_ = allow; }
230 230
231 // SocketDataProvider methods: 231 // SocketDataProvider implementation.
232 virtual MockRead GetNextRead() OVERRIDE; 232 virtual MockRead GetNextRead() OVERRIDE;
233 virtual MockWriteResult OnWrite(const std::string& data) = 0; 233 virtual MockWriteResult OnWrite(const std::string& data) = 0;
234 virtual void Reset() OVERRIDE; 234 virtual void Reset() OVERRIDE;
235 235
236 protected: 236 protected:
237 // The next time there is a read from this socket, it will return |data|. 237 // The next time there is a read from this socket, it will return |data|.
238 // Before calling SimulateRead next time, the previous data must be consumed. 238 // Before calling SimulateRead next time, the previous data must be consumed.
239 void SimulateRead(const char* data, size_t length); 239 void SimulateRead(const char* data, size_t length);
240 void SimulateRead(const char* data) { 240 void SimulateRead(const char* data) {
241 SimulateRead(data, std::strlen(data)); 241 SimulateRead(data, std::strlen(data));
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 // Store pointers to handed out sockets in case the test wants to get them. 576 // Store pointers to handed out sockets in case the test wants to get them.
577 std::vector<MockUDPClientSocket*> udp_client_sockets_; 577 std::vector<MockUDPClientSocket*> udp_client_sockets_;
578 std::vector<MockTCPClientSocket*> tcp_client_sockets_; 578 std::vector<MockTCPClientSocket*> tcp_client_sockets_;
579 std::vector<MockSSLClientSocket*> ssl_client_sockets_; 579 std::vector<MockSSLClientSocket*> ssl_client_sockets_;
580 }; 580 };
581 581
582 class MockClientSocket : public net::SSLClientSocket { 582 class MockClientSocket : public net::SSLClientSocket {
583 public: 583 public:
584 explicit MockClientSocket(net::NetLog* net_log); 584 explicit MockClientSocket(net::NetLog* net_log);
585 585
586 // Socket methods: 586 // Socket implementation.
587 virtual int Read(net::IOBuffer* buf, int buf_len, 587 virtual int Read(net::IOBuffer* buf, int buf_len,
588 net::OldCompletionCallback* callback) = 0; 588 net::OldCompletionCallback* callback) = 0;
589 virtual int Write(net::IOBuffer* buf, int buf_len, 589 virtual int Write(net::IOBuffer* buf, int buf_len,
590 net::OldCompletionCallback* callback) = 0; 590 net::OldCompletionCallback* callback) = 0;
591 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; 591 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
592 virtual bool SetSendBufferSize(int32 size) OVERRIDE; 592 virtual bool SetSendBufferSize(int32 size) OVERRIDE;
593 593
594 // StreamSocket methods: 594 // StreamSocket implementation.
595 virtual int Connect(net::OldCompletionCallback* callback) = 0; 595 virtual int Connect(net::OldCompletionCallback* callback) = 0;
596 virtual int Connect(const net::CompletionCallback& callback) = 0;
596 virtual void Disconnect() OVERRIDE; 597 virtual void Disconnect() OVERRIDE;
597 virtual bool IsConnected() const OVERRIDE; 598 virtual bool IsConnected() const OVERRIDE;
598 virtual bool IsConnectedAndIdle() const OVERRIDE; 599 virtual bool IsConnectedAndIdle() const OVERRIDE;
599 virtual int GetPeerAddress(AddressList* address) const OVERRIDE; 600 virtual int GetPeerAddress(AddressList* address) const OVERRIDE;
600 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; 601 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
601 virtual const BoundNetLog& NetLog() const OVERRIDE; 602 virtual const BoundNetLog& NetLog() const OVERRIDE;
602 virtual void SetSubresourceSpeculation() OVERRIDE {} 603 virtual void SetSubresourceSpeculation() OVERRIDE {}
603 virtual void SetOmniboxSpeculation() OVERRIDE {} 604 virtual void SetOmniboxSpeculation() OVERRIDE {}
604 605
605 // SSLClientSocket methods: 606 // SSLClientSocket implementation.
606 virtual void GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE; 607 virtual void GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE;
607 virtual void GetSSLCertRequestInfo( 608 virtual void GetSSLCertRequestInfo(
608 net::SSLCertRequestInfo* cert_request_info) OVERRIDE; 609 net::SSLCertRequestInfo* cert_request_info) OVERRIDE;
609 virtual int ExportKeyingMaterial(const base::StringPiece& label, 610 virtual int ExportKeyingMaterial(const base::StringPiece& label,
610 const base::StringPiece& context, 611 const base::StringPiece& context,
611 unsigned char *out, 612 unsigned char *out,
612 unsigned int outlen) OVERRIDE; 613 unsigned int outlen) OVERRIDE;
613 virtual NextProtoStatus GetNextProto(std::string* proto) OVERRIDE; 614 virtual NextProtoStatus GetNextProto(std::string* proto) OVERRIDE;
614 615
615 protected: 616 protected:
616 virtual ~MockClientSocket(); 617 virtual ~MockClientSocket();
617 void RunCallbackAsync(net::OldCompletionCallback* callback, int result); 618 void RunCallbackAsync(net::OldCompletionCallback* callback, int result);
618 void RunCallback(net::OldCompletionCallback*, int result); 619 void RunCallbackAsync(const net::CompletionCallback& callback, int result);
620 void RunOldCallback(net::OldCompletionCallback*, int result);
621 void RunCallback(const net::CompletionCallback&, int result);
619 622
620 ScopedRunnableMethodFactory<MockClientSocket> method_factory_; 623 base::WeakPtrFactory<MockClientSocket> weak_factory_;
621 624
622 // True if Connect completed successfully and Disconnect hasn't been called. 625 // True if Connect completed successfully and Disconnect hasn't been called.
623 bool connected_; 626 bool connected_;
624 627
625 net::BoundNetLog net_log_; 628 net::BoundNetLog net_log_;
626 }; 629 };
627 630
628 class MockTCPClientSocket : public MockClientSocket, public AsyncSocket { 631 class MockTCPClientSocket : public MockClientSocket, public AsyncSocket {
629 public: 632 public:
630 MockTCPClientSocket(const net::AddressList& addresses, net::NetLog* net_log, 633 MockTCPClientSocket(const net::AddressList& addresses, net::NetLog* net_log,
631 net::SocketDataProvider* socket); 634 net::SocketDataProvider* socket);
632 635
633 net::AddressList addresses() const { return addresses_; } 636 net::AddressList addresses() const { return addresses_; }
634 637
635 // Socket methods: 638 // Socket implementation.
636 virtual int Read(net::IOBuffer* buf, int buf_len, 639 virtual int Read(net::IOBuffer* buf, int buf_len,
637 net::OldCompletionCallback* callback) OVERRIDE; 640 net::OldCompletionCallback* callback) OVERRIDE;
638 virtual int Write(net::IOBuffer* buf, int buf_len, 641 virtual int Write(net::IOBuffer* buf, int buf_len,
639 net::OldCompletionCallback* callback) OVERRIDE; 642 net::OldCompletionCallback* callback) OVERRIDE;
640 643
641 // StreamSocket methods: 644 // StreamSocket implementation.
642 virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; 645 virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE;
646 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE;
643 virtual void Disconnect() OVERRIDE; 647 virtual void Disconnect() OVERRIDE;
644 virtual bool IsConnected() const OVERRIDE; 648 virtual bool IsConnected() const OVERRIDE;
645 virtual bool IsConnectedAndIdle() const OVERRIDE; 649 virtual bool IsConnectedAndIdle() const OVERRIDE;
646 virtual int GetPeerAddress(AddressList* address) const OVERRIDE; 650 virtual int GetPeerAddress(AddressList* address) const OVERRIDE;
647 virtual bool WasEverUsed() const OVERRIDE; 651 virtual bool WasEverUsed() const OVERRIDE;
648 virtual bool UsingTCPFastOpen() const OVERRIDE; 652 virtual bool UsingTCPFastOpen() const OVERRIDE;
649 virtual int64 NumBytesRead() const OVERRIDE; 653 virtual int64 NumBytesRead() const OVERRIDE;
650 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; 654 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE;
651 655
652 // AsyncSocket: 656 // AsyncSocket:
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 692
689 void CompleteWrite(); 693 void CompleteWrite();
690 int CompleteRead(); 694 int CompleteRead();
691 695
692 // Socket: 696 // Socket:
693 virtual int Write(net::IOBuffer* buf, int buf_len, 697 virtual int Write(net::IOBuffer* buf, int buf_len,
694 net::OldCompletionCallback* callback) OVERRIDE; 698 net::OldCompletionCallback* callback) OVERRIDE;
695 virtual int Read(net::IOBuffer* buf, int buf_len, 699 virtual int Read(net::IOBuffer* buf, int buf_len,
696 net::OldCompletionCallback* callback) OVERRIDE; 700 net::OldCompletionCallback* callback) OVERRIDE;
697 701
698 // StreamSocket: 702 // StreamSocket implementation.
699 virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; 703 virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE;
704 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE;
700 virtual void Disconnect() OVERRIDE; 705 virtual void Disconnect() OVERRIDE;
701 virtual bool IsConnected() const OVERRIDE; 706 virtual bool IsConnected() const OVERRIDE;
702 virtual bool IsConnectedAndIdle() const OVERRIDE; 707 virtual bool IsConnectedAndIdle() const OVERRIDE;
703 virtual bool WasEverUsed() const OVERRIDE; 708 virtual bool WasEverUsed() const OVERRIDE;
704 virtual bool UsingTCPFastOpen() const OVERRIDE; 709 virtual bool UsingTCPFastOpen() const OVERRIDE;
705 virtual int64 NumBytesRead() const OVERRIDE; 710 virtual int64 NumBytesRead() const OVERRIDE;
706 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; 711 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE;
707 712
708 // AsyncSocket: 713 // AsyncSocket:
709 virtual void OnReadComplete(const MockRead& data) OVERRIDE; 714 virtual void OnReadComplete(const MockRead& data) OVERRIDE;
(...skipping 16 matching lines...) Expand all
726 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket { 731 class MockSSLClientSocket : public MockClientSocket, public AsyncSocket {
727 public: 732 public:
728 MockSSLClientSocket( 733 MockSSLClientSocket(
729 net::ClientSocketHandle* transport_socket, 734 net::ClientSocketHandle* transport_socket,
730 const HostPortPair& host_and_port, 735 const HostPortPair& host_and_port,
731 const net::SSLConfig& ssl_config, 736 const net::SSLConfig& ssl_config,
732 SSLHostInfo* ssl_host_info, 737 SSLHostInfo* ssl_host_info,
733 net::SSLSocketDataProvider* socket); 738 net::SSLSocketDataProvider* socket);
734 virtual ~MockSSLClientSocket(); 739 virtual ~MockSSLClientSocket();
735 740
736 // Socket methods: 741 // Socket implementation.
737 virtual int Read(net::IOBuffer* buf, int buf_len, 742 virtual int Read(net::IOBuffer* buf, int buf_len,
738 net::OldCompletionCallback* callback) OVERRIDE; 743 net::OldCompletionCallback* callback) OVERRIDE;
739 virtual int Write(net::IOBuffer* buf, int buf_len, 744 virtual int Write(net::IOBuffer* buf, int buf_len,
740 net::OldCompletionCallback* callback) OVERRIDE; 745 net::OldCompletionCallback* callback) OVERRIDE;
741 746
742 // StreamSocket methods: 747 // StreamSocket implementation.
743 virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE; 748 virtual int Connect(net::OldCompletionCallback* callback) OVERRIDE;
749 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE;
744 virtual void Disconnect() OVERRIDE; 750 virtual void Disconnect() OVERRIDE;
745 virtual bool IsConnected() const OVERRIDE; 751 virtual bool IsConnected() const OVERRIDE;
746 virtual bool WasEverUsed() const OVERRIDE; 752 virtual bool WasEverUsed() const OVERRIDE;
747 virtual bool UsingTCPFastOpen() const OVERRIDE; 753 virtual bool UsingTCPFastOpen() const OVERRIDE;
748 virtual int64 NumBytesRead() const OVERRIDE; 754 virtual int64 NumBytesRead() const OVERRIDE;
749 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; 755 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE;
750 756
751 // SSLClientSocket methods: 757 // SSLClientSocket implementation.
752 virtual void GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE; 758 virtual void GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE;
753 virtual void GetSSLCertRequestInfo( 759 virtual void GetSSLCertRequestInfo(
754 net::SSLCertRequestInfo* cert_request_info) OVERRIDE; 760 net::SSLCertRequestInfo* cert_request_info) OVERRIDE;
755 virtual NextProtoStatus GetNextProto(std::string* proto) OVERRIDE; 761 virtual NextProtoStatus GetNextProto(std::string* proto) OVERRIDE;
756 virtual bool was_npn_negotiated() const OVERRIDE; 762 virtual bool was_npn_negotiated() const OVERRIDE;
757 virtual bool set_was_npn_negotiated(bool negotiated) OVERRIDE; 763 virtual bool set_was_npn_negotiated(bool negotiated) OVERRIDE;
758 764
759 // This MockSocket does not implement the manual async IO feature. 765 // This MockSocket does not implement the manual async IO feature.
760 virtual void OnReadComplete(const MockRead& data) OVERRIDE; 766 virtual void OnReadComplete(const MockRead& data) OVERRIDE;
761 767
762 private: 768 private:
769 class OldConnectCallback;
763 class ConnectCallback; 770 class ConnectCallback;
764 771
765 scoped_ptr<ClientSocketHandle> transport_; 772 scoped_ptr<ClientSocketHandle> transport_;
766 net::SSLSocketDataProvider* data_; 773 net::SSLSocketDataProvider* data_;
767 bool is_npn_state_set_; 774 bool is_npn_state_set_;
768 bool new_npn_value_; 775 bool new_npn_value_;
769 bool was_used_to_convey_data_; 776 bool was_used_to_convey_data_;
770 }; 777 };
771 778
772 class MockUDPClientSocket : public DatagramClientSocket, 779 class MockUDPClientSocket : public DatagramClientSocket,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
807 net::MockRead read_data_; 814 net::MockRead read_data_;
808 bool need_read_data_; 815 bool need_read_data_;
809 816
810 // While an asynchronous IO is pending, we save our user-buffer state. 817 // While an asynchronous IO is pending, we save our user-buffer state.
811 net::IOBuffer* pending_buf_; 818 net::IOBuffer* pending_buf_;
812 int pending_buf_len_; 819 int pending_buf_len_;
813 net::OldCompletionCallback* pending_callback_; 820 net::OldCompletionCallback* pending_callback_;
814 821
815 BoundNetLog net_log_; 822 BoundNetLog net_log_;
816 823
817 ScopedRunnableMethodFactory<MockUDPClientSocket> method_factory_; 824 base::WeakPtrFactory<MockUDPClientSocket> weak_factory_;
818 825
819 DISALLOW_COPY_AND_ASSIGN(MockUDPClientSocket); 826 DISALLOW_COPY_AND_ASSIGN(MockUDPClientSocket);
820 }; 827 };
821 828
822 class TestSocketRequest : public CallbackRunner< Tuple1<int> > { 829 class TestSocketRequest : public CallbackRunner< Tuple1<int> > {
823 public: 830 public:
824 TestSocketRequest( 831 TestSocketRequest(
825 std::vector<TestSocketRequest*>* request_order, 832 std::vector<TestSocketRequest*>* request_order,
826 size_t* completion_count); 833 size_t* completion_count);
827 virtual ~TestSocketRequest(); 834 virtual ~TestSocketRequest();
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 1041
1035 extern const char kSOCKS5OkRequest[]; 1042 extern const char kSOCKS5OkRequest[];
1036 extern const int kSOCKS5OkRequestLength; 1043 extern const int kSOCKS5OkRequestLength;
1037 1044
1038 extern const char kSOCKS5OkResponse[]; 1045 extern const char kSOCKS5OkResponse[];
1039 extern const int kSOCKS5OkResponseLength; 1046 extern const int kSOCKS5OkResponseLength;
1040 1047
1041 } // namespace net 1048 } // namespace net
1042 1049
1043 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ 1050 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_
OLDNEW
« no previous file with comments | « net/socket/client_socket_pool_base_unittest.cc ('k') | net/socket/socket_test_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698