| 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/io_buffer.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 59 |
| 60 // Socket methods: | 60 // Socket methods: |
| 61 virtual int Read(net::IOBuffer* buf, int buf_len, | 61 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 62 net::CompletionCallback* callback); | 62 net::CompletionCallback* callback); |
| 63 virtual int Write(net::IOBuffer* buf, int buf_len, | 63 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 64 net::CompletionCallback* callback); | 64 net::CompletionCallback* callback); |
| 65 | 65 |
| 66 private: | 66 private: |
| 67 net::MockSocket* data_; | 67 net::MockSocket* data_; |
| 68 int read_offset_; | 68 int read_offset_; |
| 69 net::MockRead* read_data_; | 69 net::MockRead read_data_; |
| 70 bool need_read_data_; | 70 bool need_read_data_; |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 class MockSSLClientSocket : public MockClientSocket { | 73 class MockSSLClientSocket : public MockClientSocket { |
| 74 public: | 74 public: |
| 75 MockSSLClientSocket( | 75 MockSSLClientSocket( |
| 76 net::ClientSocket* transport_socket, | 76 net::ClientSocket* transport_socket, |
| 77 const std::string& hostname, | 77 const std::string& hostname, |
| 78 const net::SSLConfig& ssl_config, | 78 const net::SSLConfig& ssl_config, |
| 79 net::MockSSLSocket* socket); | 79 net::MockSSLSocket* socket); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 net::CompletionCallback* c = callback_; | 144 net::CompletionCallback* c = callback_; |
| 145 callback_ = NULL; | 145 callback_ = NULL; |
| 146 if (c) | 146 if (c) |
| 147 c->Run(result); | 147 c->Run(result); |
| 148 } | 148 } |
| 149 | 149 |
| 150 MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, | 150 MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, |
| 151 net::MockSocket* socket) | 151 net::MockSocket* socket) |
| 152 : data_(socket), | 152 : data_(socket), |
| 153 read_offset_(0), | 153 read_offset_(0), |
| 154 read_data_(NULL), | 154 read_data_(true, net::ERR_UNEXPECTED), |
| 155 need_read_data_(true) { | 155 need_read_data_(true) { |
| 156 DCHECK(data_); | 156 DCHECK(data_); |
| 157 data_->Reset(); | 157 data_->Reset(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 int MockTCPClientSocket::Connect(net::CompletionCallback* callback) { | 160 int MockTCPClientSocket::Connect(net::CompletionCallback* callback) { |
| 161 DCHECK(!callback_); | 161 DCHECK(!callback_); |
| 162 if (connected_) | 162 if (connected_) |
| 163 return net::OK; | 163 return net::OK; |
| 164 connected_ = true; | 164 connected_ = true; |
| 165 if (data_->connect_data().async) { | 165 if (data_->connect_data().async) { |
| 166 RunCallbackAsync(callback, data_->connect_data().result); | 166 RunCallbackAsync(callback, data_->connect_data().result); |
| 167 return net::ERR_IO_PENDING; | 167 return net::ERR_IO_PENDING; |
| 168 } | 168 } |
| 169 return data_->connect_data().result; | 169 return data_->connect_data().result; |
| 170 } | 170 } |
| 171 | 171 |
| 172 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, | 172 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 173 net::CompletionCallback* callback) { | 173 net::CompletionCallback* callback) { |
| 174 DCHECK(!callback_); | 174 DCHECK(!callback_); |
| 175 |
| 176 if (!IsConnected()) |
| 177 return net::ERR_UNEXPECTED; |
| 178 |
| 175 if (need_read_data_) { | 179 if (need_read_data_) { |
| 176 read_data_ = data_->GetNextRead(); | 180 read_data_ = data_->GetNextRead(); |
| 177 need_read_data_ = false; | 181 need_read_data_ = false; |
| 178 } | 182 } |
| 179 int result = read_data_->result; | 183 int result = read_data_.result; |
| 180 if (read_data_->data) { | 184 if (read_data_.data) { |
| 181 if (read_data_->data_len - read_offset_ > 0) { | 185 if (read_data_.data_len - read_offset_ > 0) { |
| 182 result = std::min(buf_len, read_data_->data_len - read_offset_); | 186 result = std::min(buf_len, read_data_.data_len - read_offset_); |
| 183 memcpy(buf->data(), read_data_->data + read_offset_, result); | 187 memcpy(buf->data(), read_data_.data + read_offset_, result); |
| 184 read_offset_ += result; | 188 read_offset_ += result; |
| 185 if (read_offset_ == read_data_->data_len) { | 189 if (read_offset_ == read_data_.data_len) { |
| 186 need_read_data_ = true; | 190 need_read_data_ = true; |
| 187 read_offset_ = 0; | 191 read_offset_ = 0; |
| 188 } | 192 } |
| 189 } else { | 193 } else { |
| 190 result = 0; // EOF | 194 result = 0; // EOF |
| 191 } | 195 } |
| 192 } | 196 } |
| 193 if (read_data_->async) { | 197 if (read_data_.async) { |
| 194 RunCallbackAsync(callback, result); | 198 RunCallbackAsync(callback, result); |
| 195 return net::ERR_IO_PENDING; | 199 return net::ERR_IO_PENDING; |
| 196 } | 200 } |
| 197 return result; | 201 return result; |
| 198 } | 202 } |
| 199 | 203 |
| 200 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, | 204 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 201 net::CompletionCallback* callback) { | 205 net::CompletionCallback* callback) { |
| 202 DCHECK(buf); | 206 DCHECK(buf); |
| 203 DCHECK(buf_len > 0); | 207 DCHECK(buf_len > 0); |
| 204 DCHECK(!callback_); | 208 DCHECK(!callback_); |
| 205 | 209 |
| 210 if (!IsConnected()) |
| 211 return net::ERR_UNEXPECTED; |
| 212 |
| 206 std::string data(buf->data(), buf_len); | 213 std::string data(buf->data(), buf_len); |
| 207 net::MockWriteResult write_result = data_->OnWrite(data); | 214 net::MockWriteResult write_result = data_->OnWrite(data); |
| 208 | 215 |
| 209 if (write_result.async) { | 216 if (write_result.async) { |
| 210 RunCallbackAsync(callback, write_result.result); | 217 RunCallbackAsync(callback, write_result.result); |
| 211 return net::ERR_IO_PENDING; | 218 return net::ERR_IO_PENDING; |
| 212 } | 219 } |
| 213 return write_result.result; | 220 return write_result.result; |
| 214 } | 221 } |
| 215 | 222 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, | 298 int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 292 net::CompletionCallback* callback) { | 299 net::CompletionCallback* callback) { |
| 293 DCHECK(!callback_); | 300 DCHECK(!callback_); |
| 294 return transport_->Write(buf, buf_len, callback); | 301 return transport_->Write(buf, buf_len, callback); |
| 295 } | 302 } |
| 296 | 303 |
| 297 } // namespace | 304 } // namespace |
| 298 | 305 |
| 299 namespace net { | 306 namespace net { |
| 300 | 307 |
| 301 MockRead* StaticMockSocket::GetNextRead() { | 308 MockRead StaticMockSocket::GetNextRead() { |
| 302 return &reads_[read_index_++]; | 309 return reads_[read_index_++]; |
| 303 } | 310 } |
| 304 | 311 |
| 305 MockWriteResult StaticMockSocket::OnWrite(const std::string& data) { | 312 MockWriteResult StaticMockSocket::OnWrite(const std::string& data) { |
| 306 if (!writes_) { | 313 if (!writes_) { |
| 307 // Not using mock writes; succeed synchronously. | 314 // Not using mock writes; succeed synchronously. |
| 308 return MockWriteResult(false, data.length()); | 315 return MockWriteResult(false, data.length()); |
| 309 } | 316 } |
| 310 | 317 |
| 311 // Check that what we are writing matches the expectation. | 318 // Check that what we are writing matches the expectation. |
| 312 // Then give the mocked return value. | 319 // Then give the mocked return value. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 323 return MockWriteResult(w->async, result); | 330 return MockWriteResult(w->async, result); |
| 324 } | 331 } |
| 325 | 332 |
| 326 void StaticMockSocket::Reset() { | 333 void StaticMockSocket::Reset() { |
| 327 read_index_ = 0; | 334 read_index_ = 0; |
| 328 write_index_ = 0; | 335 write_index_ = 0; |
| 329 } | 336 } |
| 330 | 337 |
| 331 DynamicMockSocket::DynamicMockSocket() | 338 DynamicMockSocket::DynamicMockSocket() |
| 332 : read_(false, ERR_UNEXPECTED), | 339 : read_(false, ERR_UNEXPECTED), |
| 333 has_read_(false) { | 340 has_read_(false), |
| 341 short_read_limit_(0) { |
| 334 } | 342 } |
| 335 | 343 |
| 336 MockRead* DynamicMockSocket::GetNextRead() { | 344 MockRead DynamicMockSocket::GetNextRead() { |
| 337 if (!has_read_) | 345 if (!has_read_) |
| 338 return unexpected_read(); | 346 return MockRead(true, ERR_UNEXPECTED); |
| 339 has_read_ = false; | 347 MockRead result = read_; |
| 340 return &read_; | 348 if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) { |
| 349 has_read_ = false; |
| 350 } else { |
| 351 result.data_len = short_read_limit_; |
| 352 read_.data += result.data_len; |
| 353 read_.data_len -= result.data_len; |
| 354 } |
| 355 return result; |
| 341 } | 356 } |
| 342 | 357 |
| 343 void DynamicMockSocket::Reset() { | 358 void DynamicMockSocket::Reset() { |
| 344 has_read_ = false; | 359 has_read_ = false; |
| 345 } | 360 } |
| 346 | 361 |
| 347 void DynamicMockSocket::SimulateRead(const char* data) { | 362 void DynamicMockSocket::SimulateRead(const char* data) { |
| 348 EXPECT_FALSE(has_read_) << "Unconsumed read: " << read_.data; | 363 EXPECT_FALSE(has_read_) << "Unconsumed read: " << read_.data; |
| 349 read_ = MockRead(data); | 364 read_ = MockRead(data); |
| 350 has_read_ = true; | 365 has_read_ = true; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 370 | 385 |
| 371 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( | 386 SSLClientSocket* MockClientSocketFactory::CreateSSLClientSocket( |
| 372 ClientSocket* transport_socket, | 387 ClientSocket* transport_socket, |
| 373 const std::string& hostname, | 388 const std::string& hostname, |
| 374 const SSLConfig& ssl_config) { | 389 const SSLConfig& ssl_config) { |
| 375 return new MockSSLClientSocket(transport_socket, hostname, ssl_config, | 390 return new MockSSLClientSocket(transport_socket, hostname, ssl_config, |
| 376 mock_ssl_sockets_.GetNext()); | 391 mock_ssl_sockets_.GetNext()); |
| 377 } | 392 } |
| 378 | 393 |
| 379 } // namespace net | 394 } // namespace net |
| OLD | NEW |