| 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" |
| 10 #include "net/base/ssl_info.h" | 11 #include "net/base/ssl_info.h" |
| 11 #include "net/socket/socket.h" | 12 #include "net/socket/socket.h" |
| 13 #include "net/socket/ssl_client_socket.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 15 |
| 14 namespace net { | 16 namespace { |
| 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 }; |
| 15 | 99 |
| 16 MockClientSocket::MockClientSocket() | 100 MockClientSocket::MockClientSocket() |
| 17 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 101 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
| 18 callback_(NULL), | 102 callback_(NULL), |
| 19 connected_(false) { | 103 connected_(false) { |
| 20 } | 104 } |
| 21 | 105 |
| 22 void MockClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { | 106 void MockClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { |
| 23 NOTREACHED(); | 107 NOTREACHED(); |
| 24 } | 108 } |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 DCHECK(!callback_); | 294 DCHECK(!callback_); |
| 211 return transport_->Read(buf, buf_len, callback); | 295 return transport_->Read(buf, buf_len, callback); |
| 212 } | 296 } |
| 213 | 297 |
| 214 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 298 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 215 net::CompletionCallback* callback) { | 299 net::CompletionCallback* callback) { |
| 216 DCHECK(!callback_); | 300 DCHECK(!callback_); |
| 217 return transport_->Write(buf, buf_len, callback); | 301 return transport_->Write(buf, buf_len, callback); |
| 218 } | 302 } |
| 219 | 303 |
| 304 } // namespace |
| 305 |
| 306 namespace net { |
| 307 |
| 220 MockRead StaticMockSocket::GetNextRead() { | 308 MockRead StaticMockSocket::GetNextRead() { |
| 221 return reads_[read_index_++]; | 309 return reads_[read_index_++]; |
| 222 } | 310 } |
| 223 | 311 |
| 224 MockWriteResult StaticMockSocket::OnWrite(const std::string& data) { | 312 MockWriteResult StaticMockSocket::OnWrite(const std::string& data) { |
| 225 if (!writes_) { | 313 if (!writes_) { |
| 226 // Not using mock writes; succeed synchronously. | 314 // Not using mock writes; succeed synchronously. |
| 227 return MockWriteResult(false, data.length()); | 315 return MockWriteResult(false, data.length()); |
| 228 } | 316 } |
| 229 | 317 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 385 |
| 298 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( | 386 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( |
| 299 ClientSocket* transport_socket, | 387 ClientSocket* transport_socket, |
| 300 const std::string& hostname, | 388 const std::string& hostname, |
| 301 const SSLConfig& ssl_config) { | 389 const SSLConfig& ssl_config) { |
| 302 return new MockSSLClientSocket(transport_socket, hostname, ssl_config, | 390 return new MockSSLClientSocket(transport_socket, hostname, ssl_config, |
| 303 mock_ssl_sockets_.GetNext()); | 391 mock_ssl_sockets_.GetNext()); |
| 304 } | 392 } |
| 305 | 393 |
| 306 } // namespace net | 394 } // namespace net |
| OLD | NEW |