diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-25 02:43:49 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-25 02:43:49 +0000 |
commit | 4bea556e9700932d734c6cf68028515701bff027 (patch) | |
tree | f8d3e7432cb7d6fc74e3ccbb1a9e0b85915967b4 | |
parent | 47dacf542e275a350b751825c06399870a7c7061 (diff) | |
download | chromium_src-4bea556e9700932d734c6cf68028515701bff027.zip chromium_src-4bea556e9700932d734c6cf68028515701bff027.tar.gz chromium_src-4bea556e9700932d734c6cf68028515701bff027.tar.bz2 |
Make more things possible with mock sockets:
- check connection state in mock read and write
- make possible simulating short reads in DynamicMockSocket
TEST=none
http://crbug.com/15259
Review URL: http://codereview.chromium.org/147117
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19222 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/socket/socket_test_util.cc | 47 | ||||
-rw-r--r-- | net/socket/socket_test_util.h | 19 |
2 files changed, 41 insertions, 25 deletions
diff --git a/net/socket/socket_test_util.cc b/net/socket/socket_test_util.cc index be98865..8e0cb46 100644 --- a/net/socket/socket_test_util.cc +++ b/net/socket/socket_test_util.cc @@ -66,7 +66,7 @@ class MockTCPClientSocket : public MockClientSocket { private: net::MockSocket* data_; int read_offset_; - net::MockRead* read_data_; + net::MockRead read_data_; bool need_read_data_; }; @@ -151,7 +151,7 @@ MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses, net::MockSocket* socket) : data_(socket), read_offset_(0), - read_data_(NULL), + read_data_(true, net::ERR_UNEXPECTED), need_read_data_(true) { DCHECK(data_); data_->Reset(); @@ -172,17 +172,21 @@ int MockTCPClientSocket::Connect(net::CompletionCallback* callback) { int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, net::CompletionCallback* callback) { DCHECK(!callback_); + + if (!IsConnected()) + return net::ERR_UNEXPECTED; + if (need_read_data_) { read_data_ = data_->GetNextRead(); need_read_data_ = false; } - int result = read_data_->result; - if (read_data_->data) { - if (read_data_->data_len - read_offset_ > 0) { - result = std::min(buf_len, read_data_->data_len - read_offset_); - memcpy(buf->data(), read_data_->data + read_offset_, result); + int result = read_data_.result; + if (read_data_.data) { + if (read_data_.data_len - read_offset_ > 0) { + result = std::min(buf_len, read_data_.data_len - read_offset_); + memcpy(buf->data(), read_data_.data + read_offset_, result); read_offset_ += result; - if (read_offset_ == read_data_->data_len) { + if (read_offset_ == read_data_.data_len) { need_read_data_ = true; read_offset_ = 0; } @@ -190,7 +194,7 @@ int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len, result = 0; // EOF } } - if (read_data_->async) { + if (read_data_.async) { RunCallbackAsync(callback, result); return net::ERR_IO_PENDING; } @@ -203,6 +207,9 @@ int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len, DCHECK(buf_len > 0); DCHECK(!callback_); + if (!IsConnected()) + return net::ERR_UNEXPECTED; + std::string data(buf->data(), buf_len); net::MockWriteResult write_result = data_->OnWrite(data); @@ -298,8 +305,8 @@ int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len, namespace net { -MockRead* StaticMockSocket::GetNextRead() { - return &reads_[read_index_++]; +MockRead StaticMockSocket::GetNextRead() { + return reads_[read_index_++]; } MockWriteResult StaticMockSocket::OnWrite(const std::string& data) { @@ -330,14 +337,22 @@ void StaticMockSocket::Reset() { DynamicMockSocket::DynamicMockSocket() : read_(false, ERR_UNEXPECTED), - has_read_(false) { + has_read_(false), + short_read_limit_(0) { } -MockRead* DynamicMockSocket::GetNextRead() { +MockRead DynamicMockSocket::GetNextRead() { if (!has_read_) - return unexpected_read(); - has_read_ = false; - return &read_; + return MockRead(true, ERR_UNEXPECTED); + MockRead result = read_; + if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) { + has_read_ = false; + } else { + result.data_len = short_read_limit_; + read_.data += result.data_len; + read_.data_len -= result.data_len; + } + return result; } void DynamicMockSocket::Reset() { diff --git a/net/socket/socket_test_util.h b/net/socket/socket_test_util.h index 4d3b569..bd7c437 100644 --- a/net/socket/socket_test_util.h +++ b/net/socket/socket_test_util.h @@ -67,21 +67,16 @@ struct MockWriteResult { class MockSocket { public: - MockSocket() : unexpected_read_(true, ERR_UNEXPECTED) { - } + MockSocket() {} virtual ~MockSocket() {} - virtual MockRead* GetNextRead() = 0; + virtual MockRead GetNextRead() = 0; virtual MockWriteResult OnWrite(const std::string& data) = 0; virtual void Reset() = 0; MockConnect connect_data() const { return connect_; } - protected: - MockRead* unexpected_read() { return &unexpected_read_; } - private: - MockRead unexpected_read_; MockConnect connect_; DISALLOW_COPY_AND_ASSIGN(MockSocket); @@ -96,7 +91,7 @@ class StaticMockSocket : public MockSocket { writes_(w), write_index_(0) {} // MockSocket methods: - virtual MockRead* GetNextRead(); + virtual MockRead GetNextRead(); virtual MockWriteResult OnWrite(const std::string& data); virtual void Reset(); @@ -119,10 +114,13 @@ class DynamicMockSocket : public MockSocket { DynamicMockSocket(); // MockSocket methods: - virtual MockRead* GetNextRead(); + virtual MockRead GetNextRead(); virtual MockWriteResult OnWrite(const std::string& data) = 0; virtual void Reset(); + int short_read_limit() const { return short_read_limit_; } + void set_short_read_limit(int limit) { short_read_limit_ = limit; } + protected: // The next time there is a read from this socket, it will return |data|. // Before calling SimulateRead next time, the previous data must be consumed. @@ -133,6 +131,9 @@ class DynamicMockSocket : public MockSocket { bool has_read_; bool consumed_read_; + // Max number of bytes we will read at a time. 0 means no limit. + int short_read_limit_; + DISALLOW_COPY_AND_ASSIGN(DynamicMockSocket); }; |