summaryrefslogtreecommitdiffstats
path: root/net/base
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-15 09:47:37 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-06-15 09:47:37 +0000
commit69b43fe355fcbd401fe02d6e9c3028516d192b9c (patch)
treebc8a146e371ce7ef6fabe26170136e4dc8f365f4 /net/base
parent842fb6e67517350c67869d63d6958b1d8ae3c926 (diff)
downloadchromium_src-69b43fe355fcbd401fe02d6e9c3028516d192b9c.zip
chromium_src-69b43fe355fcbd401fe02d6e9c3028516d192b9c.tar.gz
chromium_src-69b43fe355fcbd401fe02d6e9c3028516d192b9c.tar.bz2
Add unit tests for new FTP network transaction.
TEST=Covered by net_unittests. http://crbug.com/12409 Review URL: http://codereview.chromium.org/115779 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18375 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r--net/base/socket_test_util.cc113
-rw-r--r--net/base/socket_test_util.h81
2 files changed, 153 insertions, 41 deletions
diff --git a/net/base/socket_test_util.cc b/net/base/socket_test_util.cc
index cd9bd77..414cc2a 100644
--- a/net/base/socket_test_util.cc
+++ b/net/base/socket_test_util.cc
@@ -65,9 +65,9 @@ class MockTCPClientSocket : public MockClientSocket {
private:
net::MockSocket* data_;
- int read_index_;
int read_offset_;
- int write_index_;
+ net::MockRead* read_data_;
+ bool need_read_data_;
};
class MockSSLClientSocket : public MockClientSocket {
@@ -150,10 +150,11 @@ void MockClientSocket::RunCallback(int result) {
MockTCPClientSocket::MockTCPClientSocket(const net::AddressList& addresses,
net::MockSocket* socket)
: data_(socket),
- read_index_(0),
read_offset_(0),
- write_index_(0) {
+ read_data_(NULL),
+ need_read_data_(true) {
DCHECK(data_);
+ data_->Reset();
}
int MockTCPClientSocket::Connect(net::CompletionCallback* callback) {
@@ -161,32 +162,35 @@ int MockTCPClientSocket::Connect(net::CompletionCallback* callback) {
if (connected_)
return net::OK;
connected_ = true;
- if (data_->connect.async) {
- RunCallbackAsync(callback, data_->connect.result);
+ if (data_->connect_data().async) {
+ RunCallbackAsync(callback, data_->connect_data().result);
return net::ERR_IO_PENDING;
}
- return data_->connect.result;
+ return data_->connect_data().result;
}
int MockTCPClientSocket::Read(net::IOBuffer* buf, int buf_len,
net::CompletionCallback* callback) {
DCHECK(!callback_);
- net::MockRead& r = data_->reads[read_index_];
- int result = r.result;
- if (r.data) {
- if (r.data_len - read_offset_ > 0) {
- result = std::min(buf_len, r.data_len - read_offset_);
- memcpy(buf->data(), r.data + read_offset_, result);
+ 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);
read_offset_ += result;
- if (read_offset_ == r.data_len) {
- read_index_++;
+ if (read_offset_ == read_data_->data_len) {
+ need_read_data_ = true;
read_offset_ = 0;
}
} else {
result = 0; // EOF
}
}
- if (r.async) {
+ if (read_data_->async) {
RunCallbackAsync(callback, result);
return net::ERR_IO_PENDING;
}
@@ -198,28 +202,15 @@ int MockTCPClientSocket::Write(net::IOBuffer* buf, int buf_len,
DCHECK(buf);
DCHECK(buf_len > 0);
DCHECK(!callback_);
- // Not using mock writes; succeed synchronously.
- if (!data_->writes)
- return buf_len;
- // Check that what we are writing matches the expectation.
- // Then give the mocked return value.
- net::MockWrite& w = data_->writes[write_index_++];
- int result = w.result;
- if (w.data) {
- std::string expected_data(w.data, w.data_len);
- std::string actual_data(buf->data(), buf_len);
- EXPECT_EQ(expected_data, actual_data);
- if (expected_data != actual_data)
- return net::ERR_UNEXPECTED;
- if (result == net::OK)
- result = w.data_len;
- }
- if (w.async) {
- RunCallbackAsync(callback, result);
+ std::string data(buf->data(), buf_len);
+ net::MockWriteResult write_result = data_->OnWrite(data);
+
+ if (write_result.async) {
+ RunCallbackAsync(callback, write_result.result);
return net::ERR_IO_PENDING;
}
- return result;
+ return write_result.result;
}
class MockSSLClientSocket::ConnectCallback :
@@ -307,6 +298,58 @@ int MockSSLClientSocket::Write(net::IOBuffer* buf, int buf_len,
namespace net {
+MockRead* StaticMockSocket::GetNextRead() {
+ return &reads_[read_index_++];
+}
+
+MockWriteResult StaticMockSocket::OnWrite(const std::string& data) {
+ if (!writes_) {
+ // Not using mock writes; succeed synchronously.
+ return MockWriteResult(false, data.length());
+ }
+
+ // Check that what we are writing matches the expectation.
+ // Then give the mocked return value.
+ net::MockWrite* w = &writes_[write_index_++];
+ int result = w->result;
+ if (w->data) {
+ std::string expected_data(w->data, w->data_len);
+ EXPECT_EQ(expected_data, data);
+ if (expected_data != data)
+ return MockWriteResult(false, net::ERR_UNEXPECTED);
+ if (result == net::OK)
+ result = w->data_len;
+ }
+ return MockWriteResult(w->async, result);
+}
+
+void StaticMockSocket::Reset() {
+ read_index_ = 0;
+ write_index_ = 0;
+}
+
+DynamicMockSocket::DynamicMockSocket()
+ : read_(false, ERR_UNEXPECTED),
+ has_read_(false) {
+}
+
+MockRead* DynamicMockSocket::GetNextRead() {
+ if (!has_read_)
+ return unexpected_read();
+ has_read_ = false;
+ return &read_;
+}
+
+void DynamicMockSocket::Reset() {
+ has_read_ = false;
+}
+
+void DynamicMockSocket::SimulateRead(const char* data) {
+ EXPECT_FALSE(has_read_) << "Unconsumed read: " << read_.data;
+ read_ = MockRead(data);
+ has_read_ = true;
+}
+
void MockClientSocketFactory::AddMockSocket(MockSocket* socket) {
mock_sockets_.Add(socket);
}
diff --git a/net/base/socket_test_util.h b/net/base/socket_test_util.h
index 4f54b76..952e97a 100644
--- a/net/base/socket_test_util.h
+++ b/net/base/socket_test_util.h
@@ -58,13 +58,82 @@ struct MockRead {
// {async, result}.
typedef MockRead MockWrite;
-struct MockSocket {
- MockSocket() : reads(NULL), writes(NULL) { }
- MockSocket(MockRead* r, MockWrite* w) : reads(r), writes(w) { }
+struct MockWriteResult {
+ MockWriteResult(bool async, int result) : async(async), result(result) {}
- MockConnect connect;
- MockRead* reads;
- MockWrite* writes;
+ bool async;
+ int result;
+};
+
+class MockSocket {
+ public:
+ MockSocket() : unexpected_read_(true, ERR_UNEXPECTED) {
+ }
+
+ virtual ~MockSocket() {}
+ 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);
+};
+
+// MockSocket which responds based on static tables of mock reads and writes.
+class StaticMockSocket : public MockSocket {
+ public:
+ StaticMockSocket() : reads_(NULL), read_index_(0),
+ writes_(NULL), write_index_(0) {}
+ StaticMockSocket(MockRead* r, MockWrite* w) : reads_(r), read_index_(0),
+ writes_(w), write_index_(0) {}
+
+ // MockSocket methods:
+ virtual MockRead* GetNextRead();
+ virtual MockWriteResult OnWrite(const std::string& data);
+ virtual void Reset();
+
+ private:
+ MockRead* reads_;
+ int read_index_;
+ MockWrite* writes_;
+ int write_index_;
+
+ DISALLOW_COPY_AND_ASSIGN(StaticMockSocket);
+};
+
+// MockSocket which can make decisions about next mock reads based on
+// received writes. It can also be used to enforce order of operations,
+// for example that tested code must send the "Hello!" message before
+// receiving response. This is useful for testing conversation-like
+// protocols like FTP.
+class DynamicMockSocket : public MockSocket {
+ public:
+ DynamicMockSocket();
+
+ // MockSocket methods:
+ virtual MockRead* GetNextRead();
+ virtual MockWriteResult OnWrite(const std::string& data) = 0;
+ virtual void Reset();
+
+ 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.
+ void SimulateRead(const char* data);
+
+ private:
+ MockRead read_;
+ bool has_read_;
+ bool consumed_read_;
+
+ DISALLOW_COPY_AND_ASSIGN(DynamicMockSocket);
};
// MockSSLSockets only need to keep track of the return code from calls to