OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 MockConnect connect_; | 155 MockConnect connect_; |
156 MockClientSocket* socket_; | 156 MockClientSocket* socket_; |
157 | 157 |
158 DISALLOW_COPY_AND_ASSIGN(SocketDataProvider); | 158 DISALLOW_COPY_AND_ASSIGN(SocketDataProvider); |
159 }; | 159 }; |
160 | 160 |
161 // SocketDataProvider which responds based on static tables of mock reads and | 161 // SocketDataProvider which responds based on static tables of mock reads and |
162 // writes. | 162 // writes. |
163 class StaticSocketDataProvider : public SocketDataProvider { | 163 class StaticSocketDataProvider : public SocketDataProvider { |
164 public: | 164 public: |
165 StaticSocketDataProvider() : reads_(NULL), read_index_(0), read_count_(0), | 165 StaticSocketDataProvider(); |
166 writes_(NULL), write_index_(0), write_count_(0) {} | |
167 StaticSocketDataProvider(MockRead* reads, size_t reads_count, | 166 StaticSocketDataProvider(MockRead* reads, size_t reads_count, |
168 MockWrite* writes, size_t writes_count) | 167 MockWrite* writes, size_t writes_count); |
169 : reads_(reads), | 168 virtual ~StaticSocketDataProvider(); |
170 read_index_(0), | |
171 read_count_(reads_count), | |
172 writes_(writes), | |
173 write_index_(0), | |
174 write_count_(writes_count) { | |
175 } | |
176 virtual ~StaticSocketDataProvider() {} | |
177 | 169 |
178 // SocketDataProvider methods: | 170 // SocketDataProvider methods: |
179 virtual MockRead GetNextRead(); | 171 virtual MockRead GetNextRead(); |
180 virtual MockWriteResult OnWrite(const std::string& data); | 172 virtual MockWriteResult OnWrite(const std::string& data); |
181 virtual void Reset(); | 173 virtual void Reset(); |
182 | 174 |
183 // These functions get access to the next available read and write data. | 175 // These functions get access to the next available read and write data. |
184 const MockRead& PeekRead() const; | 176 const MockRead& PeekRead() const; |
185 const MockWrite& PeekWrite() const; | 177 const MockWrite& PeekWrite() const; |
186 // These functions get random access to the read and write data, for timing. | 178 // These functions get random access to the read and write data, for timing. |
(...skipping 18 matching lines...) Expand all Loading... |
205 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider); | 197 DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider); |
206 }; | 198 }; |
207 | 199 |
208 // SocketDataProvider which can make decisions about next mock reads based on | 200 // SocketDataProvider which can make decisions about next mock reads based on |
209 // received writes. It can also be used to enforce order of operations, for | 201 // received writes. It can also be used to enforce order of operations, for |
210 // example that tested code must send the "Hello!" message before receiving | 202 // example that tested code must send the "Hello!" message before receiving |
211 // response. This is useful for testing conversation-like protocols like FTP. | 203 // response. This is useful for testing conversation-like protocols like FTP. |
212 class DynamicSocketDataProvider : public SocketDataProvider { | 204 class DynamicSocketDataProvider : public SocketDataProvider { |
213 public: | 205 public: |
214 DynamicSocketDataProvider(); | 206 DynamicSocketDataProvider(); |
| 207 virtual ~DynamicSocketDataProvider(); |
215 | 208 |
216 // SocketDataProvider methods: | 209 // SocketDataProvider methods: |
217 virtual MockRead GetNextRead(); | 210 virtual MockRead GetNextRead(); |
218 virtual MockWriteResult OnWrite(const std::string& data) = 0; | 211 virtual MockWriteResult OnWrite(const std::string& data) = 0; |
219 virtual void Reset(); | 212 virtual void Reset(); |
220 | 213 |
221 int short_read_limit() const { return short_read_limit_; } | 214 int short_read_limit() const { return short_read_limit_; } |
222 void set_short_read_limit(int limit) { short_read_limit_ = limit; } | 215 void set_short_read_limit(int limit) { short_read_limit_ = limit; } |
223 | 216 |
224 void allow_unconsumed_reads(bool allow) { allow_unconsumed_reads_ = allow; } | 217 void allow_unconsumed_reads(bool allow) { allow_unconsumed_reads_ = allow; } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 void SetCompletionCallback(CompletionCallback* callback) { | 329 void SetCompletionCallback(CompletionCallback* callback) { |
337 callback_ = callback; | 330 callback_ = callback; |
338 } | 331 } |
339 | 332 |
340 // Posts a quit message to the current message loop, if one is running. | 333 // Posts a quit message to the current message loop, if one is running. |
341 void EndLoop(); | 334 void EndLoop(); |
342 | 335 |
343 void CompleteRead(); | 336 void CompleteRead(); |
344 | 337 |
345 private: | 338 private: |
| 339 friend class base::RefCounted<OrderedSocketData>; |
| 340 virtual ~OrderedSocketData(); |
| 341 |
346 int sequence_number_; | 342 int sequence_number_; |
347 int loop_stop_stage_; | 343 int loop_stop_stage_; |
348 CompletionCallback* callback_; | 344 CompletionCallback* callback_; |
349 bool blocked_; | 345 bool blocked_; |
350 ScopedRunnableMethodFactory<OrderedSocketData> factory_; | 346 ScopedRunnableMethodFactory<OrderedSocketData> factory_; |
351 }; | 347 }; |
352 | 348 |
353 class DeterministicMockTCPClientSocket; | 349 class DeterministicMockTCPClientSocket; |
354 | 350 |
355 // This class gives the user full control over the network activity, | 351 // This class gives the user full control over the network activity, |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 class MockTCPClientSocket; | 502 class MockTCPClientSocket; |
507 class MockSSLClientSocket; | 503 class MockSSLClientSocket; |
508 | 504 |
509 // ClientSocketFactory which contains arrays of sockets of each type. | 505 // ClientSocketFactory which contains arrays of sockets of each type. |
510 // You should first fill the arrays using AddMock{SSL,}Socket. When the factory | 506 // You should first fill the arrays using AddMock{SSL,}Socket. When the factory |
511 // is asked to create a socket, it takes next entry from appropriate array. | 507 // is asked to create a socket, it takes next entry from appropriate array. |
512 // You can use ResetNextMockIndexes to reset that next entry index for all mock | 508 // You can use ResetNextMockIndexes to reset that next entry index for all mock |
513 // socket types. | 509 // socket types. |
514 class MockClientSocketFactory : public ClientSocketFactory { | 510 class MockClientSocketFactory : public ClientSocketFactory { |
515 public: | 511 public: |
| 512 MockClientSocketFactory(); |
| 513 virtual ~MockClientSocketFactory(); |
| 514 |
516 void AddSocketDataProvider(SocketDataProvider* socket); | 515 void AddSocketDataProvider(SocketDataProvider* socket); |
517 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); | 516 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); |
518 void ResetNextMockIndexes(); | 517 void ResetNextMockIndexes(); |
519 | 518 |
520 // Return |index|-th MockTCPClientSocket (starting from 0) that the factory | 519 // Return |index|-th MockTCPClientSocket (starting from 0) that the factory |
521 // created. | 520 // created. |
522 MockTCPClientSocket* GetMockTCPClientSocket(size_t index) const; | 521 MockTCPClientSocket* GetMockTCPClientSocket(size_t index) const; |
523 | 522 |
524 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory | 523 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory |
525 // created. | 524 // created. |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 std::vector<TestSocketRequest*> request_order_; | 795 std::vector<TestSocketRequest*> request_order_; |
797 size_t completion_count_; | 796 size_t completion_count_; |
798 }; | 797 }; |
799 | 798 |
800 class MockTCPClientSocketPool : public TCPClientSocketPool { | 799 class MockTCPClientSocketPool : public TCPClientSocketPool { |
801 public: | 800 public: |
802 class MockConnectJob { | 801 class MockConnectJob { |
803 public: | 802 public: |
804 MockConnectJob(ClientSocket* socket, ClientSocketHandle* handle, | 803 MockConnectJob(ClientSocket* socket, ClientSocketHandle* handle, |
805 CompletionCallback* callback); | 804 CompletionCallback* callback); |
| 805 ~MockConnectJob(); |
806 | 806 |
807 int Connect(); | 807 int Connect(); |
808 bool CancelHandle(const ClientSocketHandle* handle); | 808 bool CancelHandle(const ClientSocketHandle* handle); |
809 | 809 |
810 private: | 810 private: |
811 void OnConnect(int rv); | 811 void OnConnect(int rv); |
812 | 812 |
813 scoped_ptr<ClientSocket> socket_; | 813 scoped_ptr<ClientSocket> socket_; |
814 ClientSocketHandle* handle_; | 814 ClientSocketHandle* handle_; |
815 CompletionCallback* user_callback_; | 815 CompletionCallback* user_callback_; |
(...skipping 30 matching lines...) Expand all Loading... |
846 ClientSocketFactory* client_socket_factory_; | 846 ClientSocketFactory* client_socket_factory_; |
847 ScopedVector<MockConnectJob> job_list_; | 847 ScopedVector<MockConnectJob> job_list_; |
848 int release_count_; | 848 int release_count_; |
849 int cancel_count_; | 849 int cancel_count_; |
850 | 850 |
851 DISALLOW_COPY_AND_ASSIGN(MockTCPClientSocketPool); | 851 DISALLOW_COPY_AND_ASSIGN(MockTCPClientSocketPool); |
852 }; | 852 }; |
853 | 853 |
854 class DeterministicMockClientSocketFactory : public ClientSocketFactory { | 854 class DeterministicMockClientSocketFactory : public ClientSocketFactory { |
855 public: | 855 public: |
| 856 DeterministicMockClientSocketFactory(); |
| 857 virtual ~DeterministicMockClientSocketFactory(); |
| 858 |
856 void AddSocketDataProvider(DeterministicSocketData* socket); | 859 void AddSocketDataProvider(DeterministicSocketData* socket); |
857 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); | 860 void AddSSLSocketDataProvider(SSLSocketDataProvider* socket); |
858 void ResetNextMockIndexes(); | 861 void ResetNextMockIndexes(); |
859 | 862 |
860 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory | 863 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory |
861 // created. | 864 // created. |
862 MockSSLClientSocket* GetMockSSLClientSocket(size_t index) const; | 865 MockSSLClientSocket* GetMockSSLClientSocket(size_t index) const; |
863 | 866 |
864 // ClientSocketFactory | 867 // ClientSocketFactory |
865 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses, | 868 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
924 | 927 |
925 extern const char kSOCKS5OkRequest[]; | 928 extern const char kSOCKS5OkRequest[]; |
926 extern const int kSOCKS5OkRequestLength; | 929 extern const int kSOCKS5OkRequestLength; |
927 | 930 |
928 extern const char kSOCKS5OkResponse[]; | 931 extern const char kSOCKS5OkResponse[]; |
929 extern const int kSOCKS5OkResponseLength; | 932 extern const int kSOCKS5OkResponseLength; |
930 | 933 |
931 } // namespace net | 934 } // namespace net |
932 | 935 |
933 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 936 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
OLD | NEW |