| 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 #include "net/socket/socket_test_util.h" | 5 #include "net/socket/socket_test_util.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "net/base/io_buffer.h" | |
| 11 #include "net/base/ssl_info.h" | 10 #include "net/base/ssl_info.h" |
| 12 #include "net/socket/socket.h" | 11 #include "net/socket/socket.h" |
| 13 #include "net/socket/ssl_client_socket.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 13 |
| 16 namespace { | 14 namespace net { |
| 17 | |
| 18 class MockClientSocket : public net::SSLClientSocket { | |
| 19 public: | |
| 20 MockClientSocket(); | |
| 21 | |
| 22 // ClientSocket methods: | |
| 23 virtual int Connect(net::CompletionCallback* callback) = 0; | |
| 24 | |
| 25 // SSLClientSocket methods: | |
| 26 virtual void GetSSLInfo(net::SSLInfo* ssl_info); | |
| 27 virtual void GetSSLCertRequestInfo( | |
| 28 net::SSLCertRequestInfo* cert_request_info); | |
| 29 virtual void Disconnect(); | |
| 30 virtual bool IsConnected() const; | |
| 31 virtual bool IsConnectedAndIdle() const; | |
| 32 | |
| 33 // Socket methods: | |
| 34 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 35 net::CompletionCallback* callback) = 0; | |
| 36 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 37 net::CompletionCallback* callback) = 0; | |
| 38 | |
| 39 #if defined(OS_LINUX) | |
| 40 virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen); | |
| 41 #endif | |
| 42 | |
| 43 protected: | |
| 44 void RunCallbackAsync(net::CompletionCallback* callback, int result); | |
| 45 void RunCallback(int result); | |
| 46 | |
| 47 ScopedRunnableMethodFactory<MockClientSocket> method_factory_; | |
| 48 net::CompletionCallback* callback_; | |
| 49 bool connected_; | |
| 50 }; | |
| 51 | |
| 52 class MockTCPClientSocket : public MockClientSocket { | |
| 53 public: | |
| 54 MockTCPClientSocket(const net::AddressList& addresses, | |
| 55 net::MockSocket* socket); | |
| 56 | |
| 57 // ClientSocket methods: | |
| 58 virtual int Connect(net::CompletionCallback* callback); | |
| 59 | |
| 60 // Socket methods: | |
| 61 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 62 net::CompletionCallback* callback); | |
| 63 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 64 net::CompletionCallback* callback); | |
| 65 | |
| 66 private: | |
| 67 net::MockSocket* data_; | |
| 68 int read_offset_; | |
| 69 net::MockRead read_data_; | |
| 70 bool need_read_data_; | |
| 71 }; | |
| 72 | |
| 73 class MockSSLClientSocket : public MockClientSocket { | |
| 74 public: | |
| 75 MockSSLClientSocket( | |
| 76 net::ClientSocket* transport_socket, | |
| 77 const std::string& hostname, | |
| 78 const net::SSLConfig& ssl_config, | |
| 79 net::MockSSLSocket* socket); | |
| 80 ~MockSSLClientSocket(); | |
| 81 | |
| 82 virtual void GetSSLInfo(net::SSLInfo* ssl_info); | |
| 83 | |
| 84 virtual int Connect(net::CompletionCallback* callback); | |
| 85 virtual void Disconnect(); | |
| 86 | |
| 87 // Socket methods: | |
| 88 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 89 net::CompletionCallback* callback); | |
| 90 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 91 net::CompletionCallback* callback); | |
| 92 | |
| 93 private: | |
| 94 class ConnectCallback; | |
| 95 | |
| 96 scoped_ptr<ClientSocket> transport_; | |
| 97 net::MockSSLSocket* data_; | |
| 98 }; | |
| 99 | 15 |
| 100 MockClientSocket::MockClientSocket() | 16 MockClientSocket::MockClientSocket() |
| 101 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 17 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
| 102 callback_(NULL), | 18 callback_(NULL), |
| 103 connected_(false) { | 19 connected_(false) { |
| 104 } | 20 } |
| 105 | 21 |
| 106 void MockClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { | 22 void MockClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { |
| 107 NOTREACHED(); | 23 NOTREACHED(); |
| 108 } | 24 } |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 DCHECK(!callback_); | 210 DCHECK(!callback_); |
| 295 return transport_->Read(buf, buf_len, callback); | 211 return transport_->Read(buf, buf_len, callback); |
| 296 } | 212 } |
| 297 | 213 |
| 298 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 214 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 299 net::CompletionCallback* callback) { | 215 net::CompletionCallback* callback) { |
| 300 DCHECK(!callback_); | 216 DCHECK(!callback_); |
| 301 return transport_->Write(buf, buf_len, callback); | 217 return transport_->Write(buf, buf_len, callback); |
| 302 } | 218 } |
| 303 | 219 |
| 304 } // namespace | |
| 305 | |
| 306 namespace net { | |
| 307 | |
| 308 MockRead StaticMockSocket::GetNextRead() { | 220 MockRead StaticMockSocket::GetNextRead() { |
| 309 return reads_[read_index_++]; | 221 return reads_[read_index_++]; |
| 310 } | 222 } |
| 311 | 223 |
| 312 MockWriteResult StaticMockSocket::OnWrite(const std::string& data) { | 224 MockWriteResult StaticMockSocket::OnWrite(const std::string& data) { |
| 313 if (!writes_) { | 225 if (!writes_) { |
| 314 // Not using mock writes; succeed synchronously. | 226 // Not using mock writes; succeed synchronously. |
| 315 return MockWriteResult(false, data.length()); | 227 return MockWriteResult(false, data.length()); |
| 316 } | 228 } |
| 317 | 229 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 | 297 |
| 386 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( | 298 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( |
| 387 ClientSocket* transport_socket, | 299 ClientSocket* transport_socket, |
| 388 const std::string& hostname, | 300 const std::string& hostname, |
| 389 const SSLConfig& ssl_config) { | 301 const SSLConfig& ssl_config) { |
| 390 return new MockSSLClientSocket(transport_socket, hostname, ssl_config, | 302 return new MockSSLClientSocket(transport_socket, hostname, ssl_config, |
| 391 mock_ssl_sockets_.GetNext()); | 303 mock_ssl_sockets_.GetNext()); |
| 392 } | 304 } |
| 393 | 305 |
| 394 } // namespace net | 306 } // namespace net |
| OLD | NEW |