diff options
-rw-r--r-- | net/base/socket_test_util.cc | 113 | ||||
-rw-r--r-- | net/base/socket_test_util.h | 81 | ||||
-rw-r--r-- | net/ftp/ftp_network_transaction_unittest.cc | 498 | ||||
-rw-r--r-- | net/http/http_network_transaction_unittest.cc | 199 | ||||
-rw-r--r-- | net/net.gyp | 1 |
5 files changed, 706 insertions, 186 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 diff --git a/net/ftp/ftp_network_transaction_unittest.cc b/net/ftp/ftp_network_transaction_unittest.cc new file mode 100644 index 0000000..77b8c6a --- /dev/null +++ b/net/ftp/ftp_network_transaction_unittest.cc @@ -0,0 +1,498 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/ftp/ftp_network_transaction.h" + +#include "base/ref_counted.h" +#include "net/base/host_resolver.h" +#include "net/base/host_resolver_unittest.h" +#include "net/base/io_buffer.h" +#include "net/base/socket_test_util.h" +#include "net/base/test_completion_callback.h" +#include "net/ftp/ftp_network_session.h" +#include "net/ftp/ftp_request_info.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" + +namespace net { + +class FtpMockControlSocket : public DynamicMockSocket { + public: + enum State { + NONE, + PRE_USER, + PRE_PASSWD, + PRE_SYST, + PRE_PWD, + PRE_TYPE, + PRE_PASV, + PRE_SIZE, + PRE_MDTM, + PRE_LIST, + PRE_RETR, + PRE_PASV2, + PRE_CWD, + PRE_QUIT, + QUIT + }; + + FtpMockControlSocket() : failure_injection_state_(NONE) { + Init(); + } + + virtual MockWriteResult OnWrite(const std::string& data) { + if (InjectFault()) + return MockWriteResult(true, OK); + switch (state()) { + case PRE_USER: + return Verify("USER anonymous\r\n", data, PRE_PASSWD, + "331 Password needed\r\n"); + case PRE_PASSWD: + return Verify("PASS chrome@example.com\r\n", data, PRE_SYST, + "230 Welcome\r\n"); + case PRE_SYST: + return Verify("SYST\r\n", data, PRE_PWD, "215 UNIX\r\n"); + case PRE_PWD: + return Verify("PWD\r\n", data, PRE_TYPE, + "257 \"/\" is your current location\r\n"); + case PRE_TYPE: + return Verify("TYPE I\r\n", data, PRE_PASV, + "200 TYPE is now 8-bit binary\r\n"); + case PRE_PASV: + return Verify("PASV\r\n", data, PRE_SIZE, + "227 Entering Passive Mode (127,0,0,1,123,456)\r\n"); + case PRE_QUIT: + return Verify("QUIT\r\n", data, QUIT, "221 Goodbye.\r\n"); + default: + return MockWriteResult(true, ERR_UNEXPECTED); + } + } + + void InjectFailure(State state, State next_state, const char* response) { + DCHECK_EQ(NONE, failure_injection_state_); + DCHECK_NE(NONE, state); + DCHECK_NE(NONE, next_state); + DCHECK_NE(state, next_state); + failure_injection_state_ = state; + failure_injection_next_state_ = next_state; + fault_response_ = response; + } + + State state() const { + return state_; + } + + virtual void Reset() { + DynamicMockSocket::Reset(); + Init(); + } + + protected: + void Init() { + state_ = PRE_USER; + SimulateRead("220 host TestFTPd\r\n"); + } + + // If protocol fault injection has been requested, adjusts state and mocked + // read and returns true. + bool InjectFault() { + if (state_ != failure_injection_state_) + return false; + SimulateRead(fault_response_); + state_ = failure_injection_next_state_; + return true; + } + + MockWriteResult Verify(const std::string& expected, + const std::string& data, + State next_state, + const char* next_read) { + EXPECT_EQ(expected, data); + if (expected == data) { + state_ = next_state; + SimulateRead(next_read); + return MockWriteResult(true, OK); + } + return MockWriteResult(true, ERR_UNEXPECTED); + } + + private: + State state_; + State failure_injection_state_; + State failure_injection_next_state_; + const char* fault_response_; + + DISALLOW_COPY_AND_ASSIGN(FtpMockControlSocket); +}; + +class FtpMockControlSocketDirectoryListing : public FtpMockControlSocket { + public: + FtpMockControlSocketDirectoryListing() { + } + + virtual MockWriteResult OnWrite(const std::string& data) { + if (InjectFault()) + return MockWriteResult(true, OK); + switch (state()) { + case PRE_SIZE: + return Verify("SIZE /\r\n", data, PRE_MDTM, + "550 I can only retrieve regular files\r\n"); + case PRE_MDTM: + return Verify("MDTM /\r\n", data, PRE_RETR, + "213 20070221112533\r\n"); + case PRE_RETR: + return Verify("RETR /\r\n", data, PRE_PASV2, + "550 Can't download directory\r\n"); + case PRE_PASV2: + // Parser should also accept format without parentheses. + return Verify("PASV\r\n", data, PRE_CWD, + "227 Entering Passive Mode 127,0,0,1,123,456\r\n"); + case PRE_CWD: + return Verify("CWD /\r\n", data, PRE_LIST, "200 OK\r\n"); + case PRE_LIST: + // TODO(phajdan.jr): Also test with "150 Accepted Data Connection". + return Verify("LIST\r\n", data, PRE_QUIT, "200 OK\r\n"); + default: + return FtpMockControlSocket::OnWrite(data); + } + } + + private: + DISALLOW_COPY_AND_ASSIGN(FtpMockControlSocketDirectoryListing); +}; + +class FtpMockControlSocketFileDownload : public FtpMockControlSocket { + public: + FtpMockControlSocketFileDownload() { + } + + virtual MockWriteResult OnWrite(const std::string& data) { + if (InjectFault()) + return MockWriteResult(true, OK); + switch (state()) { + case PRE_SIZE: + return Verify("SIZE /file\r\n", data, PRE_MDTM, + "213 18\r\n"); + case PRE_MDTM: + return Verify("MDTM /file\r\n", data, PRE_RETR, + "213 20070221112533\r\n"); + case PRE_RETR: + // TODO(phajdan.jr): Also test with "150 Accepted Data Connection". + return Verify("RETR /file\r\n", data, PRE_QUIT, "200 OK\r\n"); + default: + return FtpMockControlSocket::OnWrite(data); + } + } + + private: + DISALLOW_COPY_AND_ASSIGN(FtpMockControlSocketFileDownload); +}; + +class FtpMockControlSocketFileDownloadRetrFail + : public FtpMockControlSocketFileDownload { + public: + FtpMockControlSocketFileDownloadRetrFail() { + } + + virtual MockWriteResult OnWrite(const std::string& data) { + if (InjectFault()) + return MockWriteResult(true, OK); + switch (state()) { + case PRE_PASV2: + return Verify("PASV\r\n", data, PRE_CWD, + "227 Entering Passive Mode (127,0,0,1,123,456)\r\n"); + case PRE_CWD: + return Verify("CWD /file\r\n", data, PRE_QUIT, + "500 file is a directory\r\n"); + default: + return FtpMockControlSocketFileDownload::OnWrite(data); + } + } + + private: + DISALLOW_COPY_AND_ASSIGN(FtpMockControlSocketFileDownloadRetrFail); +}; + +class FtpNetworkTransactionTest : public PlatformTest { + public: + FtpNetworkTransactionTest() + : session_(new FtpNetworkSession(&resolver_)), + transaction_(session_.get(), &mock_socket_factory_) { + } + + protected: + FtpRequestInfo GetRequestInfo(const std::string& url) { + FtpRequestInfo info; + info.url = GURL(url); + return info; + } + + void TransactionFailHelper(FtpMockControlSocket* ctrl_socket, + const char* request, + FtpMockControlSocket::State state, + FtpMockControlSocket::State next_state, + const char* response, + int expected_result) { + ctrl_socket->InjectFailure(state, next_state, response); + StaticMockSocket data_socket1; + StaticMockSocket data_socket2; + mock_socket_factory_.AddMockSocket(ctrl_socket); + mock_socket_factory_.AddMockSocket(&data_socket1); + mock_socket_factory_.AddMockSocket(&data_socket2); + FtpRequestInfo request_info = GetRequestInfo(request); + ASSERT_EQ(ERR_IO_PENDING, transaction_.Start(&request_info, &callback_)); + EXPECT_EQ(expected_result, callback_.WaitForResult()); + EXPECT_EQ(FtpMockControlSocket::QUIT, ctrl_socket->state()); + } + + HostResolver resolver_; + scoped_refptr<FtpNetworkSession> session_; + MockClientSocketFactory mock_socket_factory_; + FtpNetworkTransaction transaction_; + TestCompletionCallback callback_; +}; + +TEST_F(FtpNetworkTransactionTest, FailedLookup) { + FtpRequestInfo request_info = GetRequestInfo("ftp://badhost"); + scoped_refptr<RuleBasedHostMapper> mapper(new RuleBasedHostMapper()); + mapper->AddSimulatedFailure("badhost"); + ScopedHostMapper scoped_mapper(mapper.get()); + ASSERT_EQ(ERR_IO_PENDING, transaction_.Start(&request_info, &callback_)); + EXPECT_EQ(ERR_FAILED, callback_.WaitForResult()); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransaction) { + FtpMockControlSocketDirectoryListing ctrl_socket; + std::string test_string("mock-directory-listing"); + MockRead data_reads[] = { + MockRead(test_string.c_str()), + }; + // TODO(phajdan.jr): FTP transaction should not open two data sockets. + StaticMockSocket data_socket1; + StaticMockSocket data_socket2(data_reads, NULL); + mock_socket_factory_.AddMockSocket(&ctrl_socket); + mock_socket_factory_.AddMockSocket(&data_socket1); + mock_socket_factory_.AddMockSocket(&data_socket2); + FtpRequestInfo request_info = GetRequestInfo("ftp://host"); + ASSERT_EQ(ERR_IO_PENDING, transaction_.Start(&request_info, &callback_)); + EXPECT_EQ(OK, callback_.WaitForResult()); + EXPECT_EQ(FtpMockControlSocket::QUIT, ctrl_socket.state()); + scoped_refptr<IOBuffer> io_buffer(new IOBuffer(128)); + EXPECT_EQ(ERR_IO_PENDING, + transaction_.Read(io_buffer.get(), 128, &callback_)); + EXPECT_EQ(static_cast<int>(test_string.length()), callback_.WaitForResult()); + EXPECT_EQ(test_string, std::string(io_buffer->data(), test_string.length())); +} + +TEST_F(FtpNetworkTransactionTest, DownloadTransaction) { + FtpMockControlSocketFileDownload ctrl_socket; + std::string test_string("mock-file-contents"); + MockRead data_reads[] = { + MockRead(test_string.c_str()), + }; + StaticMockSocket data_socket(data_reads, NULL); + mock_socket_factory_.AddMockSocket(&ctrl_socket); + mock_socket_factory_.AddMockSocket(&data_socket); + FtpRequestInfo request_info = GetRequestInfo("ftp://host/file"); + ASSERT_EQ(ERR_IO_PENDING, transaction_.Start(&request_info, &callback_)); + EXPECT_EQ(OK, callback_.WaitForResult()); + EXPECT_EQ(FtpMockControlSocket::QUIT, ctrl_socket.state()); + scoped_refptr<IOBuffer> io_buffer(new IOBuffer(128)); + EXPECT_EQ(ERR_IO_PENDING, + transaction_.Read(io_buffer.get(), 128, &callback_)); + EXPECT_EQ(static_cast<int>(test_string.length()), callback_.WaitForResult()); + EXPECT_EQ(test_string, std::string(io_buffer->data(), test_string.length())); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailUser) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_USER, + FtpMockControlSocket::PRE_QUIT, + "500 no such user\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailPass) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_PASSWD, + FtpMockControlSocket::PRE_QUIT, + "530 Login authentication failed\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailSyst) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_SYST, + FtpMockControlSocket::PRE_PWD, + "500 failed syst\r\n", + OK); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailPwd) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_PWD, + FtpMockControlSocket::PRE_QUIT, + "500 failed pwd\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailType) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_TYPE, + FtpMockControlSocket::PRE_QUIT, + "500 failed type\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailPasv) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_PASV, + FtpMockControlSocket::PRE_QUIT, + "500 failed pasv\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionMalformedMdtm) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_MDTM, + FtpMockControlSocket::PRE_RETR, + "213 foobar\r\n", + OK); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailMdtm) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_MDTM, + FtpMockControlSocket::PRE_RETR, + "500 failed mdtm\r\n", + OK); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailPasv2) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_PASV2, + FtpMockControlSocket::PRE_QUIT, + "500 failed pasv\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailCwd) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_CWD, + FtpMockControlSocket::PRE_QUIT, + "500 failed cwd\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DirectoryTransactionFailList) { + FtpMockControlSocketDirectoryListing ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host", + FtpMockControlSocket::PRE_LIST, + FtpMockControlSocket::PRE_QUIT, + "500 failed list\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailUser) { + FtpMockControlSocketFileDownload ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host/file", + FtpMockControlSocket::PRE_USER, + FtpMockControlSocket::PRE_QUIT, + "500 no such user\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailPass) { + FtpMockControlSocketFileDownload ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host/file", + FtpMockControlSocket::PRE_PASSWD, + FtpMockControlSocket::PRE_QUIT, + "530 Login authentication failed\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailSyst) { + FtpMockControlSocketFileDownload ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host/file", + FtpMockControlSocket::PRE_SYST, + FtpMockControlSocket::PRE_PWD, + "500 failed syst\r\n", + OK); +} + +TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailPwd) { + FtpMockControlSocketFileDownload ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host/file", + FtpMockControlSocket::PRE_PWD, + FtpMockControlSocket::PRE_QUIT, + "500 failed pwd\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailType) { + FtpMockControlSocketFileDownload ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host/file", + FtpMockControlSocket::PRE_TYPE, + FtpMockControlSocket::PRE_QUIT, + "500 failed type\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailPasv) { + FtpMockControlSocketFileDownload ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host/file", + FtpMockControlSocket::PRE_PASV, + FtpMockControlSocket::PRE_QUIT, + "500 failed pasv\r\n", + ERR_FAILED); +} + +TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailMdtm) { + FtpMockControlSocketFileDownload ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host/file", + FtpMockControlSocket::PRE_MDTM, + FtpMockControlSocket::PRE_RETR, + "500 failed mdtm\r\n", + OK); +} + +TEST_F(FtpNetworkTransactionTest, DownloadTransactionFailRetr) { + FtpMockControlSocketFileDownloadRetrFail ctrl_socket; + TransactionFailHelper(&ctrl_socket, + "ftp://host/file", + FtpMockControlSocket::PRE_RETR, + FtpMockControlSocket::PRE_PASV2, + "500 failed retr\r\n", + ERR_FAILED); +} + +} // namespace net diff --git a/net/http/http_network_transaction_unittest.cc b/net/http/http_network_transaction_unittest.cc index e5c4ccc..b42b693 100644 --- a/net/http/http_network_transaction_unittest.cc +++ b/net/http/http_network_transaction_unittest.cc @@ -90,8 +90,7 @@ class HttpNetworkTransactionTest : public PlatformTest { request.url = GURL("http://www.google.com/"); request.load_flags = 0; - MockSocket data; - data.reads = data_reads; + StaticMockSocket data(data_reads, NULL); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -307,9 +306,7 @@ TEST_F(HttpNetworkTransactionTest, Head) { MockRead(false, ERR_UNEXPECTED), // Should not be reached. }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; + StaticMockSocket data1(data_reads1, data_writes1); session_deps.socket_factory.AddMockSocket(&data1); TestCompletionCallback callback1; @@ -354,8 +351,7 @@ TEST_F(HttpNetworkTransactionTest, ReuseConnection) { MockRead("world"), MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; + StaticMockSocket data(data_reads, NULL); session_deps.socket_factory.AddMockSocket(&data); const char* kExpectedResponseData[] = { @@ -412,8 +408,7 @@ TEST_F(HttpNetworkTransactionTest, Ignores100) { MockRead("hello world"), MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; + StaticMockSocket data(data_reads, NULL); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -457,8 +452,7 @@ TEST_F(HttpNetworkTransactionTest, Ignores1xx) { MockRead("hello world"), MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; + StaticMockSocket data(data_reads, NULL); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -498,8 +492,7 @@ void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( MockRead("hello"), read_failure, // Now, we reuse the connection and fail the first read. }; - MockSocket data1; - data1.reads = data1_reads; + StaticMockSocket data1(data1_reads, NULL); session_deps.socket_factory.AddMockSocket(&data1); MockRead data2_reads[] = { @@ -507,8 +500,7 @@ void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( MockRead("world"), MockRead(true, OK), }; - MockSocket data2; - data2.reads = data2_reads; + StaticMockSocket data2(data2_reads, NULL); session_deps.socket_factory.AddMockSocket(&data2); const char* kExpectedResponseData[] = { @@ -568,8 +560,7 @@ TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { MockRead("hello world"), MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; + StaticMockSocket data(data_reads, NULL); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -654,12 +645,8 @@ TEST_F(HttpNetworkTransactionTest, BasicAuth) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; + StaticMockSocket data1(data_reads1, data_writes1); + StaticMockSocket data2(data_reads2, data_writes2); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); @@ -736,9 +723,7 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; + StaticMockSocket data1(data_reads1, data_writes1); session_deps.socket_factory.AddMockSocket(&data1); TestCompletionCallback callback1; @@ -817,9 +802,7 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; + StaticMockSocket data1(data_reads1, data_writes1); session_deps.socket_factory.AddMockSocket(&data1); TestCompletionCallback callback1; @@ -901,9 +884,7 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; + StaticMockSocket data1(data_reads1, data_writes1); session_deps.socket_factory.AddMockSocket(&data1); TestCompletionCallback callback1; @@ -984,9 +965,7 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) { MockRead(false, ERR_UNEXPECTED), // Should not be reached. }; - MockSocket data1; - data1.writes = data_writes1; - data1.reads = data_reads1; + StaticMockSocket data1(data_reads1, data_writes1); session_deps.socket_factory.AddMockSocket(&data1); TestCompletionCallback callback1; @@ -1068,9 +1047,7 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) { MockRead(false, ERR_UNEXPECTED), // Should not be reached. }; - MockSocket data; - data.writes = data_writes; - data.reads = data_reads; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -1123,9 +1100,7 @@ void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus( MockRead(false, ERR_UNEXPECTED), // Should not be reached. }; - MockSocket data; - data.writes = data_writes; - data.reads = data_reads; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -1381,15 +1356,9 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; - MockSocket data3; - data3.reads = data_reads3; - data3.writes = data_writes3; + StaticMockSocket data1(data_reads1, data_writes1); + StaticMockSocket data2(data_reads2, data_writes2); + StaticMockSocket data3(data_reads3, data_writes3); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); session_deps.socket_factory.AddMockSocket(&data3); @@ -1524,12 +1493,8 @@ TEST_F(HttpNetworkTransactionTest, NTLMAuth1) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; + StaticMockSocket data1(data_reads1, data_writes1); + StaticMockSocket data2(data_reads2, data_writes2); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); @@ -1700,15 +1665,9 @@ TEST_F(HttpNetworkTransactionTest, NTLMAuth2) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; - MockSocket data3; - data3.reads = data_reads3; - data3.writes = data_writes3; + StaticMockSocket data1(data_reads1, data_writes1); + StaticMockSocket data2(data_reads2, data_writes2); + StaticMockSocket data3(data_reads3, data_writes3); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); session_deps.socket_factory.AddMockSocket(&data3); @@ -1805,8 +1764,7 @@ TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { MockRead("\r\nBODY"), MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; + StaticMockSocket data(data_reads, NULL); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -1854,9 +1812,7 @@ TEST_F(HttpNetworkTransactionTest, DontRecycleTCPSocketForSSLTunnel) { MockRead(false, ERR_UNEXPECTED), // Should not be reached. }; - MockSocket data1; - data1.writes = data_writes1; - data1.reads = data_reads1; + StaticMockSocket data1(data_reads1, data_writes1); session_deps.socket_factory.AddMockSocket(&data1); TestCompletionCallback callback1; @@ -1906,8 +1862,7 @@ TEST_F(HttpNetworkTransactionTest, RecycleSocket) { MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; + StaticMockSocket data(data_reads, NULL); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -1965,8 +1920,7 @@ TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; + StaticMockSocket data(data_reads, NULL); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -2035,9 +1989,7 @@ TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { MockWrite(false, 93), // POST MockWrite(false, ERR_CONNECTION_ABORTED), // POST data }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; + StaticMockSocket data1(data_reads1, data_writes1); // The second socket is used for the second attempt of transaction 2. @@ -2052,9 +2004,7 @@ TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { MockWrite(false, 93), // POST MockWrite(false, 3), // POST data }; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; + StaticMockSocket data2(data_reads2, data_writes2); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); @@ -2132,12 +2082,8 @@ TEST_F(HttpNetworkTransactionTest, AuthIdentityInUrl) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; + StaticMockSocket data1(data_reads1, data_writes1); + StaticMockSocket data2(data_reads2, data_writes2); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); @@ -2212,12 +2158,8 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; + StaticMockSocket data1(data_reads1, data_writes1); + StaticMockSocket data2(data_reads2, data_writes2); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); @@ -2300,13 +2242,8 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; - + StaticMockSocket data1(data_reads1, data_writes1); + StaticMockSocket data2(data_reads2, data_writes2); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); @@ -2372,9 +2309,7 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; + StaticMockSocket data1(data_reads1, data_writes1); session_deps.socket_factory.AddMockSocket(&data1); TestCompletionCallback callback1; @@ -2433,12 +2368,8 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; + StaticMockSocket data1(data_reads1, data_writes1); + StaticMockSocket data2(data_reads2, data_writes2); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); @@ -2522,15 +2453,9 @@ TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { MockRead(false, OK), }; - MockSocket data1; - data1.reads = data_reads1; - data1.writes = data_writes1; - MockSocket data2; - data2.reads = data_reads2; - data2.writes = data_writes2; - MockSocket data3; - data3.reads = data_reads3; - data3.writes = data_writes3; + StaticMockSocket data1(data_reads1, data_writes1); + StaticMockSocket data2(data_reads2, data_writes2); + StaticMockSocket data3(data_reads3, data_writes3); session_deps.socket_factory.AddMockSocket(&data1); session_deps.socket_factory.AddMockSocket(&data2); session_deps.socket_factory.AddMockSocket(&data3); @@ -2662,8 +2587,8 @@ TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) { MockRead(false, OK), }; - MockSocket ssl_bad_certificate; - MockSocket data(data_reads, data_writes); + StaticMockSocket ssl_bad_certificate; + StaticMockSocket data(data_reads, data_writes); MockSSLSocket ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); MockSSLSocket ssl(true, OK); @@ -2730,8 +2655,8 @@ TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaProxy) { MockRead(false, OK), }; - MockSocket ssl_bad_certificate(proxy_reads, proxy_writes); - MockSocket data(data_reads, data_writes); + StaticMockSocket ssl_bad_certificate(proxy_reads, proxy_writes); + StaticMockSocket data(data_reads, data_writes); MockSSLSocket ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); MockSSLSocket ssl(true, OK); @@ -2796,9 +2721,7 @@ TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) { MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; - data.writes = data_writes; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -2838,9 +2761,7 @@ TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) { MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; - data.writes = data_writes; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -2878,9 +2799,7 @@ TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) { MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; - data.writes = data_writes; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -2918,9 +2837,7 @@ TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) { MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; - data.writes = data_writes; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -2958,9 +2875,7 @@ TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) { MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; - data.writes = data_writes; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -3000,9 +2915,7 @@ TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) { MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; - data.writes = data_writes; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -3042,9 +2955,7 @@ TEST_F(HttpNetworkTransactionTest, MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; - data.writes = data_writes; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; @@ -3083,9 +2994,7 @@ TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) { MockRead(false, OK), }; - MockSocket data; - data.reads = data_reads; - data.writes = data_writes; + StaticMockSocket data(data_reads, data_writes); session_deps.socket_factory.AddMockSocket(&data); TestCompletionCallback callback; diff --git a/net/net.gyp b/net/net.gyp index 536f1de..14aca88 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -438,6 +438,7 @@ 'disk_cache/mapped_file_unittest.cc', 'disk_cache/storage_block_unittest.cc', 'ftp/ftp_auth_cache_unittest.cc', + 'ftp/ftp_network_transaction_unittest.cc', 'http/des_unittest.cc', 'http/http_auth_cache_unittest.cc', 'http/http_auth_handler_basic_unittest.cc', |