| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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::SocketDataProvider* data) | 65 net::SocketDataProvider* data) |
| 66 : addresses_(addresses), | 66 : addresses_(addresses), |
| 67 data_(data), | 67 data_(data), |
| 68 read_offset_(0), | 68 read_offset_(0), |
| 69 read_data_(true, net::ERR_UNEXPECTED), | 69 read_data_(false, net::ERR_UNEXPECTED), |
| 70 need_read_data_(true), | 70 need_read_data_(true), |
| 71 peer_closed_connection_(false), |
| 71 pending_buf_(NULL), | 72 pending_buf_(NULL), |
| 72 pending_buf_len_(0), | 73 pending_buf_len_(0), |
| 73 pending_callback_(NULL) { | 74 pending_callback_(NULL) { |
| 74 DCHECK(data_); | 75 DCHECK(data_); |
| 75 data_->Reset(); | 76 data_->Reset(); |
| 76 } | 77 } |
| 77 | 78 |
| 78 int MockTCPClientSocket::Connect(net::CompletionCallback* callback, | 79 int MockTCPClientSocket::Connect(net::CompletionCallback* callback, |
| 79 LoadLog* load_log) { | 80 LoadLog* load_log) { |
| 80 if (connected_) | 81 if (connected_) |
| 81 return net::OK; | 82 return net::OK; |
| 82 connected_ = true; | 83 connected_ = true; |
| 83 if (data_->connect_data().async) { | 84 if (data_->connect_data().async) { |
| 84 RunCallbackAsync(callback, data_->connect_data().result); | 85 RunCallbackAsync(callback, data_->connect_data().result); |
| 85 return net::ERR_IO_PENDING; | 86 return net::ERR_IO_PENDING; |
| 86 } | 87 } |
| 87 return data_->connect_data().result; | 88 return data_->connect_data().result; |
| 88 } | 89 } |
| 89 | 90 |
| 91 bool MockTCPClientSocket::IsConnected() const { |
| 92 return connected_ && !peer_closed_connection_; |
| 93 } |
| 94 |
| 90 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, | 95 int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 91 net::CompletionCallback* callback) { | 96 net::CompletionCallback* callback) { |
| 92 if (!IsConnected()) | 97 if (!connected_) |
| 93 return net::ERR_UNEXPECTED; | 98 return net::ERR_UNEXPECTED; |
| 94 | 99 |
| 95 // If the buffer is already in use, a read is already in progress! | 100 // If the buffer is already in use, a read is already in progress! |
| 96 DCHECK(pending_buf_ == NULL); | 101 DCHECK(pending_buf_ == NULL); |
| 97 | 102 |
| 98 // Store our async IO data. | 103 // Store our async IO data. |
| 99 pending_buf_ = buf; | 104 pending_buf_ = buf; |
| 100 pending_buf_len_ = buf_len; | 105 pending_buf_len_ = buf_len; |
| 101 pending_callback_ = callback; | 106 pending_callback_ = callback; |
| 102 | 107 |
| 103 if (need_read_data_) { | 108 if (need_read_data_) { |
| 104 read_data_ = data_->GetNextRead(); | 109 read_data_ = data_->GetNextRead(); |
| 110 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) { |
| 111 // This MockRead is just a marker to instruct us to set |
| 112 // peer_closed_connection_. Skip it and get the next one. |
| 113 read_data_ = data_->GetNextRead(); |
| 114 peer_closed_connection_ = true; |
| 115 } |
| 105 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility | 116 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility |
| 106 // to complete the async IO manually later (via OnReadComplete). | 117 // to complete the async IO manually later (via OnReadComplete). |
| 107 if (read_data_.result == ERR_IO_PENDING) { | 118 if (read_data_.result == ERR_IO_PENDING) { |
| 108 DCHECK(callback); // We need to be using async IO in this case. | 119 DCHECK(callback); // We need to be using async IO in this case. |
| 109 return ERR_IO_PENDING; | 120 return ERR_IO_PENDING; |
| 110 } | 121 } |
| 111 need_read_data_ = false; | 122 need_read_data_ = false; |
| 112 } | 123 } |
| 113 | 124 |
| 114 return CompleteRead(); | 125 return CompleteRead(); |
| 115 } | 126 } |
| 116 | 127 |
| 117 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, | 128 int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 118 net::CompletionCallback* callback) { | 129 net::CompletionCallback* callback) { |
| 119 DCHECK(buf); | 130 DCHECK(buf); |
| 120 DCHECK_GT(buf_len, 0); | 131 DCHECK_GT(buf_len, 0); |
| 121 | 132 |
| 122 if (!IsConnected()) | 133 if (!connected_) |
| 123 return net::ERR_UNEXPECTED; | 134 return net::ERR_UNEXPECTED; |
| 124 | 135 |
| 125 std::string data(buf->data(), buf_len); | 136 std::string data(buf->data(), buf_len); |
| 126 net::MockWriteResult write_result = data_->OnWrite(data); | 137 net::MockWriteResult write_result = data_->OnWrite(data); |
| 127 | 138 |
| 128 if (write_result.async) { | 139 if (write_result.async) { |
| 129 RunCallbackAsync(callback, write_result.result); | 140 RunCallbackAsync(callback, write_result.result); |
| 130 return net::ERR_IO_PENDING; | 141 return net::ERR_IO_PENDING; |
| 131 } | 142 } |
| 132 return write_result.result; | 143 return write_result.result; |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 write_index_ = 0; | 308 write_index_ = 0; |
| 298 } | 309 } |
| 299 | 310 |
| 300 DynamicSocketDataProvider::DynamicSocketDataProvider() | 311 DynamicSocketDataProvider::DynamicSocketDataProvider() |
| 301 : short_read_limit_(0), | 312 : short_read_limit_(0), |
| 302 allow_unconsumed_reads_(false) { | 313 allow_unconsumed_reads_(false) { |
| 303 } | 314 } |
| 304 | 315 |
| 305 MockRead DynamicSocketDataProvider::GetNextRead() { | 316 MockRead DynamicSocketDataProvider::GetNextRead() { |
| 306 if (reads_.empty()) | 317 if (reads_.empty()) |
| 307 return MockRead(true, ERR_UNEXPECTED); | 318 return MockRead(false, ERR_UNEXPECTED); |
| 308 MockRead result = reads_.front(); | 319 MockRead result = reads_.front(); |
| 309 if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) { | 320 if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) { |
| 310 reads_.pop_front(); | 321 reads_.pop_front(); |
| 311 } else { | 322 } else { |
| 312 result.data_len = short_read_limit_; | 323 result.data_len = short_read_limit_; |
| 313 reads_.front().data += result.data_len; | 324 reads_.front().data += result.data_len; |
| 314 reads_.front().data_len -= result.data_len; | 325 reads_.front().data_len -= result.data_len; |
| 315 } | 326 } |
| 316 return result; | 327 return result; |
| 317 } | 328 } |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 } | 446 } |
| 436 | 447 |
| 437 void ClientSocketPoolTest::ReleaseAllConnections(KeepAlive keep_alive) { | 448 void ClientSocketPoolTest::ReleaseAllConnections(KeepAlive keep_alive) { |
| 438 bool released_one; | 449 bool released_one; |
| 439 do { | 450 do { |
| 440 released_one = ReleaseOneConnection(keep_alive); | 451 released_one = ReleaseOneConnection(keep_alive); |
| 441 } while (released_one); | 452 } while (released_one); |
| 442 } | 453 } |
| 443 | 454 |
| 444 } // namespace net | 455 } // namespace net |
| OLD | NEW |