Commit faa1928d authored by rch's avatar rch Committed by Commit bot

DynamicSocketDataProvider only has one consumer, FtpSocketDataProvider. This...

DynamicSocketDataProvider only has one consumer, FtpSocketDataProvider. This CL moves the functionality of DynamicSocketDataProvider directly into FtpSocketDataProvider.

Review URL: https://codereview.chromium.org/1137183005

Cr-Commit-Position: refs/heads/master@{#330317}
parent ff8ec298
......@@ -31,7 +31,7 @@ const int kBufferSize = 128;
namespace net {
class FtpSocketDataProvider : public DynamicSocketDataProvider {
class FtpSocketDataProvider : public SocketDataProvider {
public:
enum State {
NONE,
......@@ -54,13 +54,30 @@ class FtpSocketDataProvider : public DynamicSocketDataProvider {
};
FtpSocketDataProvider()
: failure_injection_state_(NONE),
: short_read_limit_(0),
allow_unconsumed_reads_(false),
failure_injection_state_(NONE),
multiline_welcome_(false),
use_epsv_(true),
data_type_('I') {
Init();
}
// SocketDataProvider implementation.
MockRead OnRead() override {
if (reads_.empty())
return MockRead(SYNCHRONOUS, ERR_UNEXPECTED);
MockRead result = reads_.front();
if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) {
reads_.pop_front();
} else {
result.data_len = short_read_limit_;
reads_.front().data += result.data_len;
reads_.front().data_len -= result.data_len;
}
return result;
}
MockWriteResult OnWrite(const std::string& data) override {
if (InjectFault())
return MockWriteResult(ASYNC, data.length());
......@@ -124,7 +141,7 @@ class FtpSocketDataProvider : public DynamicSocketDataProvider {
}
void Reset() override {
DynamicSocketDataProvider::Reset();
reads_.clear();
Init();
}
......@@ -139,6 +156,13 @@ class FtpSocketDataProvider : public DynamicSocketDataProvider {
void set_data_type(char data_type) { data_type_ = data_type; }
int short_read_limit() const { return short_read_limit_; }
void set_short_read_limit(int limit) { short_read_limit_ = limit; }
void set_allow_unconsumed_reads(bool allow) {
allow_unconsumed_reads_ = allow;
}
protected:
void Init() {
state_ = PRE_USER;
......@@ -177,8 +201,27 @@ class FtpSocketDataProvider : public DynamicSocketDataProvider {
next_read, std::strlen(next_read));
}
// 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, size_t length) {
if (!allow_unconsumed_reads_) {
EXPECT_TRUE(reads_.empty()) << "Unconsumed read: " << reads_.front().data;
}
reads_.push_back(MockRead(ASYNC, data, length));
}
void SimulateRead(const char* data) { SimulateRead(data, std::strlen(data)); }
private:
// List of reads to be consumed.
std::deque<MockRead> reads_;
// Max number of bytes we will read at a time. 0 means no limit.
int short_read_limit_;
// If true, we'll not require the client to consume all data before we
// mock the next read.
bool allow_unconsumed_reads_;
State state_;
State failure_injection_state_;
State failure_injection_next_state_;
......@@ -950,7 +993,7 @@ TEST_P(FtpNetworkTransactionTest, DirectoryTransactionMultilineWelcomeShort) {
FtpSocketDataProviderDirectoryListing ctrl_socket;
// The client will not consume all three 230 lines. That's good, we want to
// test that scenario.
ctrl_socket.allow_unconsumed_reads(true);
ctrl_socket.set_allow_unconsumed_reads(true);
ctrl_socket.set_multiline_welcome(true);
ctrl_socket.set_short_read_limit(5);
ExecuteTransaction(&ctrl_socket, "ftp://host", OK);
......
......@@ -257,39 +257,6 @@ bool StaticSocketDataProvider::AllWriteDataConsumed() const {
return helper_.AllWriteDataConsumed();
}
DynamicSocketDataProvider::DynamicSocketDataProvider()
: short_read_limit_(0),
allow_unconsumed_reads_(false) {
}
DynamicSocketDataProvider::~DynamicSocketDataProvider() {}
MockRead DynamicSocketDataProvider::OnRead() {
if (reads_.empty())
return MockRead(SYNCHRONOUS, ERR_UNEXPECTED);
MockRead result = reads_.front();
if (short_read_limit_ == 0 || result.data_len <= short_read_limit_) {
reads_.pop_front();
} else {
result.data_len = short_read_limit_;
reads_.front().data += result.data_len;
reads_.front().data_len -= result.data_len;
}
return result;
}
void DynamicSocketDataProvider::Reset() {
reads_.clear();
}
void DynamicSocketDataProvider::SimulateRead(const char* data,
const size_t length) {
if (!allow_unconsumed_reads_) {
EXPECT_TRUE(reads_.empty()) << "Unconsumed read: " << reads_.front().data;
}
reads_.push_back(MockRead(ASYNC, data, length));
}
SSLSocketDataProvider::SSLSocketDataProvider(IoMode mode, int result)
: connect(mode, result),
next_proto_status(SSLClientSocket::kNextProtoUnsupported),
......
......@@ -313,44 +313,6 @@ class StaticSocketDataProvider : public SocketDataProvider {
DISALLOW_COPY_AND_ASSIGN(StaticSocketDataProvider);
};
// SocketDataProvider 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 DynamicSocketDataProvider : public SocketDataProvider {
public:
DynamicSocketDataProvider();
~DynamicSocketDataProvider() override;
int short_read_limit() const { return short_read_limit_; }
void set_short_read_limit(int limit) { short_read_limit_ = limit; }
void allow_unconsumed_reads(bool allow) { allow_unconsumed_reads_ = allow; }
// SocketDataProvider implementation.
MockRead OnRead() override;
MockWriteResult OnWrite(const std::string& data) override = 0;
void Reset() override;
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, size_t length);
void SimulateRead(const char* data) { SimulateRead(data, std::strlen(data)); }
private:
std::deque<MockRead> reads_;
// Max number of bytes we will read at a time. 0 means no limit.
int short_read_limit_;
// If true, we'll not require the client to consume all data before we
// mock the next read.
bool allow_unconsumed_reads_;
DISALLOW_COPY_AND_ASSIGN(DynamicSocketDataProvider);
};
// SSLSocketDataProviders only need to keep track of the return code from calls
// to Connect().
struct SSLSocketDataProvider {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment