| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
| 15 #include "base/scoped_vector.h" | 15 #include "base/scoped_vector.h" |
| 16 #include "net/base/address_list.h" | 16 #include "net/base/address_list.h" |
| 17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/ssl_config_service.h" | 19 #include "net/base/ssl_config_service.h" |
| 20 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" |
| 21 #include "net/socket/client_socket_factory.h" | 21 #include "net/socket/client_socket_factory.h" |
| 22 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
| 23 #include "net/socket/ssl_client_socket.h" | 23 #include "net/socket/ssl_client_socket.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 namespace net { | 26 namespace net { |
| 27 | 27 |
| 28 enum { |
| 29 // A private network error code used by the socket test utility classes. |
| 30 // If the |result| member of a MockRead is |
| 31 // ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, that MockRead is just a |
| 32 // marker that indicates the peer will close the connection after the next |
| 33 // MockRead. The other members of that MockRead are ignored. |
| 34 ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ = -10000, |
| 35 }; |
| 36 |
| 28 class ClientSocket; | 37 class ClientSocket; |
| 29 class LoadLog; | 38 class LoadLog; |
| 30 class MockClientSocket; | 39 class MockClientSocket; |
| 31 class SSLClientSocket; | 40 class SSLClientSocket; |
| 32 | 41 |
| 33 struct MockConnect { | 42 struct MockConnect { |
| 34 // Asynchronous connection success. | 43 // Asynchronous connection success. |
| 35 MockConnect() : async(true), result(OK) { } | 44 MockConnect() : async(true), result(OK) { } |
| 36 MockConnect(bool a, int r) : async(a), result(r) { } | 45 MockConnect(bool a, int r) : async(a), result(r) { } |
| 37 | 46 |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 // If an async IO is pending because the SocketDataProvider returned | 287 // If an async IO is pending because the SocketDataProvider returned |
| 279 // ERR_IO_PENDING, then the MockClientSocket waits until this OnReadComplete | 288 // ERR_IO_PENDING, then the MockClientSocket waits until this OnReadComplete |
| 280 // is called to complete the asynchronous read operation. | 289 // is called to complete the asynchronous read operation. |
| 281 virtual void OnReadComplete(const MockRead& data) = 0; | 290 virtual void OnReadComplete(const MockRead& data) = 0; |
| 282 | 291 |
| 283 protected: | 292 protected: |
| 284 void RunCallbackAsync(net::CompletionCallback* callback, int result); | 293 void RunCallbackAsync(net::CompletionCallback* callback, int result); |
| 285 void RunCallback(net::CompletionCallback*, int result); | 294 void RunCallback(net::CompletionCallback*, int result); |
| 286 | 295 |
| 287 ScopedRunnableMethodFactory<MockClientSocket> method_factory_; | 296 ScopedRunnableMethodFactory<MockClientSocket> method_factory_; |
| 297 |
| 298 // True if Connect completed successfully and Disconnect hasn't been called. |
| 288 bool connected_; | 299 bool connected_; |
| 289 }; | 300 }; |
| 290 | 301 |
| 291 class MockTCPClientSocket : public MockClientSocket { | 302 class MockTCPClientSocket : public MockClientSocket { |
| 292 public: | 303 public: |
| 293 MockTCPClientSocket(const net::AddressList& addresses, | 304 MockTCPClientSocket(const net::AddressList& addresses, |
| 294 net::SocketDataProvider* socket); | 305 net::SocketDataProvider* socket); |
| 295 | 306 |
| 296 // ClientSocket methods: | 307 // ClientSocket methods: |
| 297 virtual int Connect(net::CompletionCallback* callback, | 308 virtual int Connect(net::CompletionCallback* callback, |
| 298 LoadLog* load_log); | 309 LoadLog* load_log); |
| 310 virtual bool IsConnected() const; |
| 311 virtual bool IsConnectedAndIdle() const { return IsConnected(); } |
| 299 | 312 |
| 300 // Socket methods: | 313 // Socket methods: |
| 301 virtual int Read(net::IOBuffer* buf, int buf_len, | 314 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 302 net::CompletionCallback* callback); | 315 net::CompletionCallback* callback); |
| 303 virtual int Write(net::IOBuffer* buf, int buf_len, | 316 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 304 net::CompletionCallback* callback); | 317 net::CompletionCallback* callback); |
| 305 | 318 |
| 306 virtual void OnReadComplete(const MockRead& data); | 319 virtual void OnReadComplete(const MockRead& data); |
| 307 | 320 |
| 308 net::AddressList addresses() const { return addresses_; } | 321 net::AddressList addresses() const { return addresses_; } |
| 309 | 322 |
| 310 private: | 323 private: |
| 311 int CompleteRead(); | 324 int CompleteRead(); |
| 312 | 325 |
| 313 net::AddressList addresses_; | 326 net::AddressList addresses_; |
| 314 | 327 |
| 315 net::SocketDataProvider* data_; | 328 net::SocketDataProvider* data_; |
| 316 int read_offset_; | 329 int read_offset_; |
| 317 net::MockRead read_data_; | 330 net::MockRead read_data_; |
| 318 bool need_read_data_; | 331 bool need_read_data_; |
| 319 | 332 |
| 333 // True if the peer has closed the connection. This allows us to simulate |
| 334 // the recv(..., MSG_PEEK) call in the IsConnectedAndIdle method of the real |
| 335 // TCPClientSocket. |
| 336 bool peer_closed_connection_; |
| 337 |
| 320 // While an asynchronous IO is pending, we save our user-buffer state. | 338 // While an asynchronous IO is pending, we save our user-buffer state. |
| 321 net::IOBuffer* pending_buf_; | 339 net::IOBuffer* pending_buf_; |
| 322 int pending_buf_len_; | 340 int pending_buf_len_; |
| 323 net::CompletionCallback* pending_callback_; | 341 net::CompletionCallback* pending_callback_; |
| 324 }; | 342 }; |
| 325 | 343 |
| 326 class MockSSLClientSocket : public MockClientSocket { | 344 class MockSSLClientSocket : public MockClientSocket { |
| 327 public: | 345 public: |
| 328 MockSSLClientSocket( | 346 MockSSLClientSocket( |
| 329 net::ClientSocket* transport_socket, | 347 net::ClientSocket* transport_socket, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 void ReleaseAllConnections(KeepAlive keep_alive); | 440 void ReleaseAllConnections(KeepAlive keep_alive); |
| 423 | 441 |
| 424 ScopedVector<TestSocketRequest> requests_; | 442 ScopedVector<TestSocketRequest> requests_; |
| 425 std::vector<TestSocketRequest*> request_order_; | 443 std::vector<TestSocketRequest*> request_order_; |
| 426 size_t completion_count_; | 444 size_t completion_count_; |
| 427 }; | 445 }; |
| 428 | 446 |
| 429 } // namespace net | 447 } // namespace net |
| 430 | 448 |
| 431 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ | 449 #endif // NET_SOCKET_SOCKET_TEST_UTIL_H_ |
| OLD | NEW |