| 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 &MockClientSocket::RunCallback, callback, result)); | 55 &MockClientSocket::RunCallback, callback, result)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void MockClientSocket::RunCallback(net::CompletionCallback* callback, | 58 void MockClientSocket::RunCallback(net::CompletionCallback* callback, |
| 59 int result) { | 59 int result) { |
| 60 if (callback) | 60 if (callback) |
| 61 callback->Run(result); | 61 callback->Run(result); |
| 62 } | 62 } |
| 63 | 63 |
| 64 MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, | 64 MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, |
| 65 net::MockSocket* socket) | 65 net::SocketDataProvider* data) |
| 66 : addresses_(addresses), | 66 : addresses_(addresses), |
| 67 data_(socket), | 67 data_(data), |
| 68 read_offset_(0), | 68 read_offset_(0), |
| 69 read_data_(true, net::ERR_UNEXPECTED), | 69 read_data_(true, net::ERR_UNEXPECTED), |
| 70 need_read_data_(true) { | 70 need_read_data_(true) { |
| 71 DCHECK(data_); | 71 DCHECK(data_); |
| 72 data_->Reset(); | 72 data_->Reset(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 int MockTCPClientSocket::Connect(net::CompletionCallback* callback, | 75 int MockTCPClientSocket::Connect(net::CompletionCallback* callback, |
| 76 LoadLog* load_log) { | 76 LoadLog* load_log) { |
| 77 if (connected_) | 77 if (connected_) |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 156 |
| 157 MockSSLClientSocket* ssl_client_socket_; | 157 MockSSLClientSocket* ssl_client_socket_; |
| 158 net::CompletionCallback* user_callback_; | 158 net::CompletionCallback* user_callback_; |
| 159 int rv_; | 159 int rv_; |
| 160 }; | 160 }; |
| 161 | 161 |
| 162 MockSSLClientSocket::MockSSLClientSocket( | 162 MockSSLClientSocket::MockSSLClientSocket( |
| 163 net::ClientSocket* transport_socket, | 163 net::ClientSocket* transport_socket, |
| 164 const std::string& hostname, | 164 const std::string& hostname, |
| 165 const net::SSLConfig& ssl_config, | 165 const net::SSLConfig& ssl_config, |
| 166 net::MockSSLSocket* socket) | 166 net::SSLSocketDataProvider* data) |
| 167 : transport_(transport_socket), | 167 : transport_(transport_socket), |
| 168 data_(socket) { | 168 data_(data) { |
| 169 DCHECK(data_); | 169 DCHECK(data_); |
| 170 } | 170 } |
| 171 | 171 |
| 172 MockSSLClientSocket::~MockSSLClientSocket() { | 172 MockSSLClientSocket::~MockSSLClientSocket() { |
| 173 Disconnect(); | 173 Disconnect(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 void MockSSLClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { | 176 void MockSSLClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) { |
| 177 ssl_info->Reset(); | 177 ssl_info->Reset(); |
| 178 } | 178 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 204 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, | 204 int MockSSLClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 205 net::CompletionCallback* callback) { | 205 net::CompletionCallback* callback) { |
| 206 return transport_->Read(buf, buf_len, callback); | 206 return transport_->Read(buf, buf_len, callback); |
| 207 } | 207 } |
| 208 | 208 |
| 209 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 209 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 210 net::CompletionCallback* callback) { | 210 net::CompletionCallback* callback) { |
| 211 return transport_->Write(buf, buf_len, callback); | 211 return transport_->Write(buf, buf_len, callback); |
| 212 } | 212 } |
| 213 | 213 |
| 214 MockRead StaticMockSocket::GetNextRead() { | 214 MockRead StaticSocketDataProvider::GetNextRead() { |
| 215 return reads_[read_index_++]; | 215 return reads_[read_index_++]; |
| 216 } | 216 } |
| 217 | 217 |
| 218 MockWriteResult StaticMockSocket::OnWrite(const std::string& data) { | 218 MockWriteResult StaticSocketDataProvider::OnWrite(const std::string& data) { |
| 219 if (!writes_) { | 219 if (!writes_) { |
| 220 // Not using mock writes; succeed synchronously. | 220 // Not using mock writes; succeed synchronously. |
| 221 return MockWriteResult(false, data.length()); | 221 return MockWriteResult(false, data.length()); |
| 222 } | 222 } |
| 223 | 223 |
| 224 // Check that what we are writing matches the expectation. | 224 // Check that what we are writing matches the expectation. |
| 225 // Then give the mocked return value. | 225 // Then give the mocked return value. |
| 226 net::MockWrite* w = &writes_[write_index_++]; | 226 net::MockWrite* w = &writes_[write_index_++]; |
| 227 int result = w->result; | 227 int result = w->result; |
| 228 if (w->data) { | 228 if (w->data) { |
| 229 std::string expected_data(w->data, w->data_len); | 229 std::string expected_data(w->data, w->data_len); |
| 230 EXPECT_EQ(expected_data, data); | 230 EXPECT_EQ(expected_data, data); |
| 231 if (expected_data != data) | 231 if (expected_data != data) |
| 232 return MockWriteResult(false, net::ERR_UNEXPECTED); | 232 return MockWriteResult(false, net::ERR_UNEXPECTED); |
| 233 if (result == net::OK) | 233 if (result == net::OK) |
| 234 result = w->data_len; | 234 result = w->data_len; |
| 235 } | 235 } |
| 236 return MockWriteResult(w->async, result); | 236 return MockWriteResult(w->async, result); |
| 237 } | 237 } |
| 238 | 238 |
| 239 void StaticMockSocket::Reset() { | 239 void StaticSocketDataProvider::Reset() { |
| 240 read_index_ = 0; | 240 read_index_ = 0; |
| 241 write_index_ = 0; | 241 write_index_ = 0; |
| 242 } | 242 } |
| 243 | 243 |
| 244 DynamicMockSocket::DynamicMockSocket() | 244 DynamicSocketDataProvider::DynamicSocketDataProvider() |
| 245 : short_read_limit_(0), | 245 : short_read_limit_(0), |
| 246 allow_unconsumed_reads_(false) { | 246 allow_unconsumed_reads_(false) { |
| 247 } | 247 } |
| 248 | 248 |
| 249 MockRead DynamicMockSocket::GetNextRead() { | 249 MockRead DynamicSocketDataProvider::GetNextRead() { |
| 250 if (reads_.empty()) | 250 if (reads_.empty()) |
| 251 return MockRead(true, ERR_UNEXPECTED); | 251 return MockRead(true, ERR_UNEXPECTED); |
| 252 MockRead result = reads_.front(); | 252 MockRead result = reads_.front(); |
| 253 if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) { | 253 if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) { |
| 254 reads_.pop_front(); | 254 reads_.pop_front(); |
| 255 } else { | 255 } else { |
| 256 result.data_len = short_read_limit_; | 256 result.data_len = short_read_limit_; |
| 257 reads_.front().data += result.data_len; | 257 reads_.front().data += result.data_len; |
| 258 reads_.front().data_len -= result.data_len; | 258 reads_.front().data_len -= result.data_len; |
| 259 } | 259 } |
| 260 return result; | 260 return result; |
| 261 } | 261 } |
| 262 | 262 |
| 263 void DynamicMockSocket::Reset() { | 263 void DynamicSocketDataProvider::Reset() { |
| 264 reads_.clear(); | 264 reads_.clear(); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void DynamicMockSocket::SimulateRead(const char* data) { | 267 void DynamicSocketDataProvider::SimulateRead(const char* data) { |
| 268 if (!allow_unconsumed_reads_) { | 268 if (!allow_unconsumed_reads_) { |
| 269 EXPECT_TRUE(reads_.empty()) << "Unconsumed read: " << reads_.front().data; | 269 EXPECT_TRUE(reads_.empty()) << "Unconsumed read: " << reads_.front().data; |
| 270 } | 270 } |
| 271 reads_.push_back(MockRead(data)); | 271 reads_.push_back(MockRead(data)); |
| 272 } | 272 } |
| 273 | 273 |
| 274 void MockClientSocketFactory::AddMockSocket(MockSocket* socket) { | 274 void MockClientSocketFactory::AddSocketDataProvider( |
| 275 mock_sockets_.Add(socket); | 275 SocketDataProvider* data) { |
| 276 mock_data_.Add(data); |
| 276 } | 277 } |
| 277 | 278 |
| 278 void MockClientSocketFactory::AddMockSSLSocket(MockSSLSocket* socket) { | 279 void MockClientSocketFactory::AddSSLSocketDataProvider( |
| 279 mock_ssl_sockets_.Add(socket); | 280 SSLSocketDataProvider* data) { |
| 281 mock_ssl_data_.Add(data); |
| 280 } | 282 } |
| 281 | 283 |
| 282 void MockClientSocketFactory::ResetNextMockIndexes() { | 284 void MockClientSocketFactory::ResetNextMockIndexes() { |
| 283 mock_sockets_.ResetNextIndex(); | 285 mock_data_.ResetNextIndex(); |
| 284 mock_ssl_sockets_.ResetNextIndex(); | 286 mock_ssl_data_.ResetNextIndex(); |
| 285 } | 287 } |
| 286 | 288 |
| 287 MockTCPClientSocket* MockClientSocketFactory::GetMockTCPClientSocket( | 289 MockTCPClientSocket* MockClientSocketFactory::GetMockTCPClientSocket( |
| 288 int index) const { | 290 int index) const { |
| 289 return tcp_client_sockets_[index]; | 291 return tcp_client_sockets_[index]; |
| 290 } | 292 } |
| 291 | 293 |
| 292 MockSSLClientSocket* MockClientSocketFactory::GetMockSSLClientSocket( | 294 MockSSLClientSocket* MockClientSocketFactory::GetMockSSLClientSocket( |
| 293 int index) const { | 295 int index) const { |
| 294 return ssl_client_sockets_[index]; | 296 return ssl_client_sockets_[index]; |
| 295 } | 297 } |
| 296 | 298 |
| 297 ClientSocket* MockClientSocketFactory::CreateTCPClientSocket( | 299 ClientSocket* MockClientSocketFactory::CreateTCPClientSocket( |
| 298 const AddressList& addresses) { | 300 const AddressList& addresses) { |
| 299 MockTCPClientSocket* socket = | 301 MockTCPClientSocket* socket = |
| 300 new MockTCPClientSocket(addresses, mock_sockets_.GetNext()); | 302 new MockTCPClientSocket(addresses, mock_data_.GetNext()); |
| 301 tcp_client_sockets_.push_back(socket); | 303 tcp_client_sockets_.push_back(socket); |
| 302 return socket; | 304 return socket; |
| 303 } | 305 } |
| 304 | 306 |
| 305 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( | 307 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( |
| 306 ClientSocket* transport_socket, | 308 ClientSocket* transport_socket, |
| 307 const std::string& hostname, | 309 const std::string& hostname, |
| 308 const SSLConfig& ssl_config) { | 310 const SSLConfig& ssl_config) { |
| 309 MockSSLClientSocket* socket = | 311 MockSSLClientSocket* socket = |
| 310 new MockSSLClientSocket(transport_socket, hostname, ssl_config, | 312 new MockSSLClientSocket(transport_socket, hostname, ssl_config, |
| 311 mock_ssl_sockets_.GetNext()); | 313 mock_ssl_data_.GetNext()); |
| 312 ssl_client_sockets_.push_back(socket); | 314 ssl_client_sockets_.push_back(socket); |
| 313 return socket; | 315 return socket; |
| 314 } | 316 } |
| 315 | 317 |
| 316 int TestSocketRequest::WaitForResult() { | 318 int TestSocketRequest::WaitForResult() { |
| 317 return callback_.WaitForResult(); | 319 return callback_.WaitForResult(); |
| 318 } | 320 } |
| 319 | 321 |
| 320 void TestSocketRequest::RunWithParams(const Tuple1<int>& params) { | 322 void TestSocketRequest::RunWithParams(const Tuple1<int>& params) { |
| 321 callback_.RunWithParams(params); | 323 callback_.RunWithParams(params); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 } | 377 } |
| 376 | 378 |
| 377 void ClientSocketPoolTest::ReleaseAllConnections(KeepAlive keep_alive) { | 379 void ClientSocketPoolTest::ReleaseAllConnections(KeepAlive keep_alive) { |
| 378 bool released_one; | 380 bool released_one; |
| 379 do { | 381 do { |
| 380 released_one = ReleaseOneConnection(keep_alive); | 382 released_one = ReleaseOneConnection(keep_alive); |
| 381 } while (released_one); | 383 } while (released_one); |
| 382 } | 384 } |
| 383 | 385 |
| 384 } // namespace net | 386 } // namespace net |
| OLD | NEW |