OLD | NEW |
1 // Copyright (c) 2009 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 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/scoped_ptr.h" | 15 #include "base/scoped_ptr.h" |
16 #include "base/scoped_vector.h" | 16 #include "base/scoped_vector.h" |
17 #include "net/base/address_list.h" | 17 #include "net/base/address_list.h" |
18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_log.h" |
20 #include "net/base/ssl_config_service.h" | 21 #include "net/base/ssl_config_service.h" |
21 #include "net/base/test_completion_callback.h" | 22 #include "net/base/test_completion_callback.h" |
22 #include "net/socket/client_socket_factory.h" | 23 #include "net/socket/client_socket_factory.h" |
23 #include "net/socket/client_socket_handle.h" | 24 #include "net/socket/client_socket_handle.h" |
24 #include "net/socket/ssl_client_socket.h" | 25 #include "net/socket/ssl_client_socket.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 namespace net { | 28 namespace net { |
28 | 29 |
29 enum { | 30 enum { |
30 // A private network error code used by the socket test utility classes. | 31 // A private network error code used by the socket test utility classes. |
31 // If the |result| member of a MockRead is | 32 // If the |result| member of a MockRead is |
32 // ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, that MockRead is just a | 33 // ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, that MockRead is just a |
33 // marker that indicates the peer will close the connection after the next | 34 // marker that indicates the peer will close the connection after the next |
34 // MockRead. The other members of that MockRead are ignored. | 35 // MockRead. The other members of that MockRead are ignored. |
35 ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ = -10000, | 36 ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ = -10000, |
36 }; | 37 }; |
37 | 38 |
38 class BoundNetLog; | |
39 class ClientSocket; | 39 class ClientSocket; |
40 class MockClientSocket; | 40 class MockClientSocket; |
41 class SSLClientSocket; | 41 class SSLClientSocket; |
42 | 42 |
43 struct MockConnect { | 43 struct MockConnect { |
44 // Asynchronous connection success. | 44 // Asynchronous connection success. |
45 MockConnect() : async(true), result(OK) { } | 45 MockConnect() : async(true), result(OK) { } |
46 MockConnect(bool a, int r) : async(a), result(r) { } | 46 MockConnect(bool a, int r) : async(a), result(r) { } |
47 | 47 |
48 bool async; | 48 bool async; |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 282 |
283 // Return |index|-th MockTCPClientSocket (starting from 0) that the factory | 283 // Return |index|-th MockTCPClientSocket (starting from 0) that the factory |
284 // created. | 284 // created. |
285 MockTCPClientSocket* GetMockTCPClientSocket(size_t index) const; | 285 MockTCPClientSocket* GetMockTCPClientSocket(size_t index) const; |
286 | 286 |
287 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory | 287 // Return |index|-th MockSSLClientSocket (starting from 0) that the factory |
288 // created. | 288 // created. |
289 MockSSLClientSocket* GetMockSSLClientSocket(size_t index) const; | 289 MockSSLClientSocket* GetMockSSLClientSocket(size_t index) const; |
290 | 290 |
291 // ClientSocketFactory | 291 // ClientSocketFactory |
292 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses); | 292 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses, |
| 293 NetLog* net_log); |
293 virtual SSLClientSocket* CreateSSLClientSocket( | 294 virtual SSLClientSocket* CreateSSLClientSocket( |
294 ClientSocket* transport_socket, | 295 ClientSocket* transport_socket, |
295 const std::string& hostname, | 296 const std::string& hostname, |
296 const SSLConfig& ssl_config); | 297 const SSLConfig& ssl_config); |
297 | 298 |
298 private: | 299 private: |
299 SocketDataProviderArray<SocketDataProvider> mock_data_; | 300 SocketDataProviderArray<SocketDataProvider> mock_data_; |
300 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; | 301 SocketDataProviderArray<SSLSocketDataProvider> mock_ssl_data_; |
301 | 302 |
302 // Store pointers to handed out sockets in case the test wants to get them. | 303 // Store pointers to handed out sockets in case the test wants to get them. |
303 std::vector<MockTCPClientSocket*> tcp_client_sockets_; | 304 std::vector<MockTCPClientSocket*> tcp_client_sockets_; |
304 std::vector<MockSSLClientSocket*> ssl_client_sockets_; | 305 std::vector<MockSSLClientSocket*> ssl_client_sockets_; |
305 }; | 306 }; |
306 | 307 |
307 class MockClientSocket : public net::SSLClientSocket { | 308 class MockClientSocket : public net::SSLClientSocket { |
308 public: | 309 public: |
309 MockClientSocket(); | 310 explicit MockClientSocket(net::NetLog* net_log); |
310 | 311 |
311 // ClientSocket methods: | 312 // ClientSocket methods: |
312 virtual int Connect(net::CompletionCallback* callback, | 313 virtual int Connect(net::CompletionCallback* callback) = 0; |
313 const BoundNetLog& net_log) = 0; | |
314 virtual void Disconnect(); | 314 virtual void Disconnect(); |
315 virtual bool IsConnected() const; | 315 virtual bool IsConnected() const; |
316 virtual bool IsConnectedAndIdle() const; | 316 virtual bool IsConnectedAndIdle() const; |
317 virtual int GetPeerAddress(AddressList* address) const; | 317 virtual int GetPeerAddress(AddressList* address) const; |
| 318 virtual const BoundNetLog& NetLog() const { return net_log_;} |
318 | 319 |
319 // SSLClientSocket methods: | 320 // SSLClientSocket methods: |
320 virtual void GetSSLInfo(net::SSLInfo* ssl_info); | 321 virtual void GetSSLInfo(net::SSLInfo* ssl_info); |
321 virtual void GetSSLCertRequestInfo( | 322 virtual void GetSSLCertRequestInfo( |
322 net::SSLCertRequestInfo* cert_request_info); | 323 net::SSLCertRequestInfo* cert_request_info); |
323 virtual NextProtoStatus GetNextProto(std::string* proto); | 324 virtual NextProtoStatus GetNextProto(std::string* proto); |
324 | 325 |
325 // Socket methods: | 326 // Socket methods: |
326 virtual int Read(net::IOBuffer* buf, int buf_len, | 327 virtual int Read(net::IOBuffer* buf, int buf_len, |
327 net::CompletionCallback* callback) = 0; | 328 net::CompletionCallback* callback) = 0; |
(...skipping 10 matching lines...) Expand all Loading... |
338 virtual void OnReadComplete(const MockRead& data) = 0; | 339 virtual void OnReadComplete(const MockRead& data) = 0; |
339 | 340 |
340 protected: | 341 protected: |
341 void RunCallbackAsync(net::CompletionCallback* callback, int result); | 342 void RunCallbackAsync(net::CompletionCallback* callback, int result); |
342 void RunCallback(net::CompletionCallback*, int result); | 343 void RunCallback(net::CompletionCallback*, int result); |
343 | 344 |
344 ScopedRunnableMethodFactory<MockClientSocket> method_factory_; | 345 ScopedRunnableMethodFactory<MockClientSocket> method_factory_; |
345 | 346 |
346 // True if Connect completed successfully and Disconnect hasn't been called. | 347 // True if Connect completed successfully and Disconnect hasn't been called. |
347 bool connected_; | 348 bool connected_; |
| 349 |
| 350 net::BoundNetLog net_log_; |
348 }; | 351 }; |
349 | 352 |
350 class MockTCPClientSocket : public MockClientSocket { | 353 class MockTCPClientSocket : public MockClientSocket { |
351 public: | 354 public: |
352 MockTCPClientSocket(const net::AddressList& addresses, | 355 MockTCPClientSocket(const net::AddressList& addresses, net::NetLog* net_log, |
353 net::SocketDataProvider* socket); | 356 net::SocketDataProvider* socket); |
354 | 357 |
355 // ClientSocket methods: | 358 // ClientSocket methods: |
356 virtual int Connect(net::CompletionCallback* callback, | 359 virtual int Connect(net::CompletionCallback* callback); |
357 const BoundNetLog& net_log); | |
358 virtual void Disconnect(); | 360 virtual void Disconnect(); |
359 virtual bool IsConnected() const; | 361 virtual bool IsConnected() const; |
360 virtual bool IsConnectedAndIdle() const { return IsConnected(); } | 362 virtual bool IsConnectedAndIdle() const { return IsConnected(); } |
361 | 363 |
362 // Socket methods: | 364 // Socket methods: |
363 virtual int Read(net::IOBuffer* buf, int buf_len, | 365 virtual int Read(net::IOBuffer* buf, int buf_len, |
364 net::CompletionCallback* callback); | 366 net::CompletionCallback* callback); |
365 virtual int Write(net::IOBuffer* buf, int buf_len, | 367 virtual int Write(net::IOBuffer* buf, int buf_len, |
366 net::CompletionCallback* callback); | 368 net::CompletionCallback* callback); |
367 | 369 |
(...skipping 26 matching lines...) Expand all Loading... |
394 public: | 396 public: |
395 MockSSLClientSocket( | 397 MockSSLClientSocket( |
396 net::ClientSocket* transport_socket, | 398 net::ClientSocket* transport_socket, |
397 const std::string& hostname, | 399 const std::string& hostname, |
398 const net::SSLConfig& ssl_config, | 400 const net::SSLConfig& ssl_config, |
399 net::SSLSocketDataProvider* socket); | 401 net::SSLSocketDataProvider* socket); |
400 ~MockSSLClientSocket(); | 402 ~MockSSLClientSocket(); |
401 | 403 |
402 virtual void GetSSLInfo(net::SSLInfo* ssl_info); | 404 virtual void GetSSLInfo(net::SSLInfo* ssl_info); |
403 | 405 |
404 virtual int Connect(net::CompletionCallback* callback, | 406 virtual int Connect(net::CompletionCallback* callback); |
405 const BoundNetLog& net_log); | |
406 virtual void Disconnect(); | 407 virtual void Disconnect(); |
407 | 408 |
408 // Socket methods: | 409 // Socket methods: |
409 virtual int Read(net::IOBuffer* buf, int buf_len, | 410 virtual int Read(net::IOBuffer* buf, int buf_len, |
410 net::CompletionCallback* callback); | 411 net::CompletionCallback* callback); |
411 virtual int Write(net::IOBuffer* buf, int buf_len, | 412 virtual int Write(net::IOBuffer* buf, int buf_len, |
412 net::CompletionCallback* callback); | 413 net::CompletionCallback* callback); |
413 | 414 |
414 // This MockSocket does not implement the manual async IO feature. | 415 // This MockSocket does not implement the manual async IO feature. |
415 virtual void OnReadComplete(const MockRead& data) { NOTIMPLEMENTED(); } | 416 virtual void OnReadComplete(const MockRead& data) { NOTIMPLEMENTED(); } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 | 504 |
504 extern const char kSOCKS5OkRequest[]; | 505 extern const char kSOCKS5OkRequest[]; |
505 extern const int kSOCKS5OkRequestLength; | 506 extern const int kSOCKS5OkRequestLength; |
506 | 507 |
507 extern const char kSOCKS5OkResponse[]; | 508 extern const char kSOCKS5OkResponse[]; |
508 extern const int kSOCKS5OkResponseLength; | 509 extern const int kSOCKS5OkResponseLength; |
509 | 510 |
510 } // namespace net | 511 } // namespace net |
511 | 512 |
512 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 513 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
OLD | NEW |