Commit 98d8fe93 authored by sergeyu's avatar sergeyu Committed by Commit bot

Cleanup Fake* classes in remoting/protocol

1. Renamed FakeSocket to FakeStreamSocket and FakeUdpSocket to
   FakeDatagramSocket,
2. Added factories for both stream and datagram fake
   sockets,
3. Move fake sockets to separate files out of fake_session.h

BUG=402993

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

Cr-Commit-Position: refs/heads/master@{#296270}
parent 8a610264
......@@ -15,7 +15,7 @@
#include "remoting/base/rsa_key_pair.h"
#include "remoting/protocol/authenticator.h"
#include "remoting/protocol/channel_authenticator.h"
#include "remoting/protocol/fake_session.h"
#include "remoting/protocol/fake_stream_socket.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
......@@ -111,8 +111,8 @@ void AuthenticatorTestBase::ContinueAuthExchangeWith(Authenticator* sender,
}
void AuthenticatorTestBase::RunChannelAuth(bool expected_fail) {
client_fake_socket_.reset(new FakeSocket());
host_fake_socket_.reset(new FakeSocket());
client_fake_socket_.reset(new FakeStreamSocket());
host_fake_socket_.reset(new FakeStreamSocket());
client_fake_socket_->PairWith(host_fake_socket_.get());
client_auth_->SecureAndAuthenticate(
......
......@@ -24,7 +24,7 @@ namespace protocol {
class Authenticator;
class ChannelAuthenticator;
class FakeSocket;
class FakeStreamSocket;
class AuthenticatorTestBase : public testing::Test {
public:
......@@ -60,8 +60,8 @@ class AuthenticatorTestBase : public testing::Test {
std::string host_cert_;
scoped_ptr<Authenticator> host_;
scoped_ptr<Authenticator> client_;
scoped_ptr<FakeSocket> client_fake_socket_;
scoped_ptr<FakeSocket> host_fake_socket_;
scoped_ptr<FakeStreamSocket> client_fake_socket_;
scoped_ptr<FakeStreamSocket> host_fake_socket_;
scoped_ptr<ChannelAuthenticator> client_auth_;
scoped_ptr<ChannelAuthenticator> host_auth_;
MockChannelDoneCallback client_callback_;
......
......@@ -48,7 +48,7 @@ class BufferedSocketWriterTest : public testing::Test {
protected:
virtual void SetUp() OVERRIDE {
socket_.reset(new FakeSocket());
socket_.reset(new FakeStreamSocket());
writer_.reset(new BufferedSocketWriter());
writer_->Init(socket_.get(), base::Bind(
&BufferedSocketWriterTest::OnWriteFailed, base::Unretained(this)));
......@@ -98,7 +98,7 @@ class BufferedSocketWriterTest : public testing::Test {
}
base::MessageLoop message_loop_;
scoped_ptr<FakeSocket> socket_;
scoped_ptr<FakeStreamSocket> socket_;
scoped_ptr<BufferedSocketWriter> writer_;
scoped_refptr<net::IOBufferWithSize> test_buffer_;
scoped_refptr<net::IOBufferWithSize> test_buffer_2_;
......
......@@ -72,18 +72,21 @@ class ChannelMultiplexerTest : public testing::Test {
protected:
virtual void SetUp() OVERRIDE {
// Create pair of multiplexers and connect them to each other.
host_mux_.reset(new ChannelMultiplexer(&host_session_, kMuxChannelName));
client_mux_.reset(new ChannelMultiplexer(&client_session_,
kMuxChannelName));
host_mux_.reset(new ChannelMultiplexer(
host_session_.GetTransportChannelFactory(), kMuxChannelName));
client_mux_.reset(new ChannelMultiplexer(
client_session_.GetTransportChannelFactory(), kMuxChannelName));
}
// Connect sockets to each other. Must be called after we've created at least
// one channel with each multiplexer.
void ConnectSockets() {
FakeSocket* host_socket =
host_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName);
FakeSocket* client_socket =
client_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName);
FakeStreamSocket* host_socket =
host_session_.fake_channel_factory().GetFakeChannel(
ChannelMultiplexer::kMuxChannelName);
FakeStreamSocket* client_socket =
client_session_.fake_channel_factory().GetFakeChannel(
ChannelMultiplexer::kMuxChannelName);
host_socket->PairWith(client_socket);
// Make writes asynchronous in one direction.
......@@ -242,9 +245,9 @@ TEST_F(ChannelMultiplexerTest, WriteFailSync) {
ConnectSockets();
host_session_.GetStreamChannel(kMuxChannelName)->
host_session_.fake_channel_factory().GetFakeChannel(kMuxChannelName)->
set_next_write_error(net::ERR_FAILED);
host_session_.GetStreamChannel(kMuxChannelName)->
host_session_.fake_channel_factory().GetFakeChannel(kMuxChannelName)->
set_async_write(false);
scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100);
......@@ -280,9 +283,9 @@ TEST_F(ChannelMultiplexerTest, WriteFailAsync) {
ConnectSockets();
host_session_.GetStreamChannel(kMuxChannelName)->
host_session_.fake_channel_factory().GetFakeChannel(kMuxChannelName)->
set_next_write_error(net::ERR_FAILED);
host_session_.GetStreamChannel(kMuxChannelName)->
host_session_.fake_channel_factory().GetFakeChannel(kMuxChannelName)->
set_async_write(true);
scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100);
......@@ -314,9 +317,9 @@ TEST_F(ChannelMultiplexerTest, DeleteWhenFailed) {
ConnectSockets();
host_session_.GetStreamChannel(kMuxChannelName)->
host_session_.fake_channel_factory().GetFakeChannel(kMuxChannelName)->
set_next_write_error(net::ERR_FAILED);
host_session_.GetStreamChannel(kMuxChannelName)->
host_session_.fake_channel_factory().GetFakeChannel(kMuxChannelName)->
set_async_write(true);
scoped_refptr<net::IOBufferWithSize> buf = CreateTestBuffer(100);
......@@ -349,8 +352,8 @@ TEST_F(ChannelMultiplexerTest, DeleteWhenFailed) {
}
TEST_F(ChannelMultiplexerTest, SessionFail) {
host_session_.set_async_creation(true);
host_session_.set_error(AUTHENTICATION_FAILED);
host_session_.fake_channel_factory().set_asynchronous_create(true);
host_session_.fake_channel_factory().set_fail_create(true);
MockConnectCallback cb1;
MockConnectCallback cb2;
......
......@@ -69,9 +69,10 @@ TEST_F(ConnectionToClientTest, SendUpdateStream) {
// Verify that something has been written.
// TODO(sergeyu): Verify that the correct data has been written.
ASSERT_TRUE(session_->GetStreamChannel(kVideoChannelName));
EXPECT_GT(session_->GetStreamChannel(kVideoChannelName)->
written_data().size(), 0u);
ASSERT_TRUE(
session_->fake_channel_factory().GetFakeChannel(kVideoChannelName));
EXPECT_FALSE(session_->fake_channel_factory()
.GetFakeChannel(kVideoChannelName)->written_data().empty());
// And then close the connection to ConnectionToClient.
viewer_->Disconnect();
......
// Copyright 2014 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 "remoting/protocol/fake_datagram_socket.h"
#include "base/bind.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "net/base/address_list.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
namespace protocol {
FakeDatagramSocket::FakeDatagramSocket()
: input_pos_(0),
task_runner_(base::ThreadTaskRunnerHandle::Get()),
weak_factory_(this) {
}
FakeDatagramSocket::~FakeDatagramSocket() {
EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
}
void FakeDatagramSocket::AppendInputPacket(const std::string& data) {
EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
input_packets_.push_back(data);
// Complete pending read if any.
if (!read_callback_.is_null()) {
DCHECK_EQ(input_pos_, static_cast<int>(input_packets_.size()) - 1);
int result = CopyReadData(read_buffer_.get(), read_buffer_size_);
read_buffer_ = NULL;
net::CompletionCallback callback = read_callback_;
read_callback_.Reset();
callback.Run(result);
}
}
void FakeDatagramSocket::PairWith(FakeDatagramSocket* peer_socket) {
EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
peer_socket_ = peer_socket->GetWeakPtr();
peer_socket->peer_socket_ = GetWeakPtr();
}
base::WeakPtr<FakeDatagramSocket> FakeDatagramSocket::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
int FakeDatagramSocket::Read(net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) {
EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
if (input_pos_ < static_cast<int>(input_packets_.size())) {
return CopyReadData(buf, buf_len);
} else {
read_buffer_ = buf;
read_buffer_size_ = buf_len;
read_callback_ = callback;
return net::ERR_IO_PENDING;
}
}
int FakeDatagramSocket::Write(net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) {
EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
written_packets_.push_back(std::string());
written_packets_.back().assign(buf->data(), buf->data() + buf_len);
if (peer_socket_.get()) {
task_runner_->PostTask(
FROM_HERE,
base::Bind(&FakeDatagramSocket::AppendInputPacket,
peer_socket_,
std::string(buf->data(), buf->data() + buf_len)));
}
return buf_len;
}
int FakeDatagramSocket::SetReceiveBufferSize(int32 size) {
NOTIMPLEMENTED();
return net::ERR_NOT_IMPLEMENTED;
}
int FakeDatagramSocket::SetSendBufferSize(int32 size) {
NOTIMPLEMENTED();
return net::ERR_NOT_IMPLEMENTED;
}
int FakeDatagramSocket::CopyReadData(net::IOBuffer* buf, int buf_len) {
int size = std::min(
buf_len, static_cast<int>(input_packets_[input_pos_].size()));
memcpy(buf->data(), &(*input_packets_[input_pos_].begin()), size);
++input_pos_;
return size;
}
FakeDatagramChannelFactory::FakeDatagramChannelFactory()
: task_runner_(base::ThreadTaskRunnerHandle::Get()),
asynchronous_create_(false),
fail_create_(false),
weak_factory_(this) {
}
FakeDatagramChannelFactory::~FakeDatagramChannelFactory() {
for (ChannelsMap::iterator it = channels_.begin(); it != channels_.end();
++it) {
EXPECT_TRUE(it->second == NULL);
}
}
void FakeDatagramChannelFactory::PairWith(
FakeDatagramChannelFactory* peer_factory) {
peer_factory_ = peer_factory->weak_factory_.GetWeakPtr();
peer_factory_->peer_factory_ = weak_factory_.GetWeakPtr();
}
FakeDatagramSocket* FakeDatagramChannelFactory::GetFakeChannel(
const std::string& name) {
return channels_[name].get();
}
void FakeDatagramChannelFactory::CreateChannel(
const std::string& name,
const ChannelCreatedCallback& callback) {
EXPECT_TRUE(channels_[name] == NULL);
scoped_ptr<FakeDatagramSocket> channel(new FakeDatagramSocket());
channels_[name] = channel->GetWeakPtr();
if (peer_factory_) {
FakeDatagramSocket* peer_socket = peer_factory_->GetFakeChannel(name);
if (peer_socket)
channel->PairWith(peer_socket);
}
if (fail_create_)
channel.reset();
if (asynchronous_create_) {
task_runner_->PostTask(
FROM_HERE,
base::Bind(&FakeDatagramChannelFactory::NotifyChannelCreated,
weak_factory_.GetWeakPtr(), base::Passed(&channel),
name, callback));
} else {
NotifyChannelCreated(channel.Pass(), name, callback);
}
}
void FakeDatagramChannelFactory::NotifyChannelCreated(
scoped_ptr<FakeDatagramSocket> owned_socket,
const std::string& name,
const ChannelCreatedCallback& callback) {
if (channels_.find(name) != channels_.end())
callback.Run(owned_socket.PassAs<net::Socket>());
}
void FakeDatagramChannelFactory::CancelChannelCreation(
const std::string& name) {
channels_.erase(name);
}
} // namespace protocol
} // namespace remoting
// Copyright 2014 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.
#ifndef REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_
#define REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_
#include <map>
#include <string>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "net/base/completion_callback.h"
#include "net/socket/socket.h"
#include "remoting/protocol/datagram_channel_factory.h"
namespace base {
class SingleThreadTaskRunner;
}
namespace remoting {
namespace protocol {
// FakeDatagramSocket implement net::StreamSocket interface. All data written to
// FakeDatagramSocket is stored in a buffer returned by written_packets().
// Read() reads data from another buffer that can be set with
// AppendInputPacket(). Pending reads are supported, so if there is a pending
// read AppendInputPacket() calls the read callback.
//
// Two fake sockets can be connected to each other using the
// PairWith() method, e.g.: a->PairWith(b). After this all data
// written to |a| can be read from |b| and vice versa. Two connected
// sockets |a| and |b| must be created and used on the same thread.
class FakeDatagramSocket : public net::Socket {
public:
FakeDatagramSocket();
virtual ~FakeDatagramSocket();
const std::vector<std::string>& written_packets() const {
return written_packets_;
}
void AppendInputPacket(const std::string& data);
// Current position in the input in number of packets, i.e. number of finished
// Read() calls.
int input_pos() const { return input_pos_; }
// Pairs the socket with |peer_socket|. Deleting either of the paired sockets
// unpairs them.
void PairWith(FakeDatagramSocket* peer_socket);
base::WeakPtr<FakeDatagramSocket> GetWeakPtr();
// net::Socket implementation.
virtual int Read(net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) OVERRIDE;
virtual int Write(net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) OVERRIDE;
virtual int SetReceiveBufferSize(int32 size) OVERRIDE;
virtual int SetSendBufferSize(int32 size) OVERRIDE;
private:
int CopyReadData(net::IOBuffer* buf, int buf_len);
base::WeakPtr<FakeDatagramSocket> peer_socket_;
scoped_refptr<net::IOBuffer> read_buffer_;
int read_buffer_size_;
net::CompletionCallback read_callback_;
std::vector<std::string> written_packets_;
std::vector<std::string> input_packets_;
int input_pos_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
base::WeakPtrFactory<FakeDatagramSocket> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(FakeDatagramSocket);
};
class FakeDatagramChannelFactory : public DatagramChannelFactory {
public:
FakeDatagramChannelFactory();
virtual ~FakeDatagramChannelFactory();
void set_asynchronous_create(bool asynchronous_create) {
asynchronous_create_ = asynchronous_create;
}
void set_fail_create(bool fail_create) { fail_create_ = fail_create; }
// Pair with |peer_factory|. Once paired the factory will be automatically
// pairing created sockets with the sockets with the same name from the peer
// factory.
void PairWith(FakeDatagramChannelFactory* peer_factory);
// Can be used to retrieve FakeDatagramSocket created by this factory, e.g. to
// feed data into it. The caller doesn't get ownership of the result. Returns
// NULL if the socket doesn't exist.
FakeDatagramSocket* GetFakeChannel(const std::string& name);
// DatagramChannelFactory interface.
virtual void CreateChannel(const std::string& name,
const ChannelCreatedCallback& callback) OVERRIDE;
virtual void CancelChannelCreation(const std::string& name) OVERRIDE;
private:
typedef std::map<std::string, base::WeakPtr<FakeDatagramSocket> > ChannelsMap;
void NotifyChannelCreated(scoped_ptr<FakeDatagramSocket> owned_socket,
const std::string& name,
const ChannelCreatedCallback& callback);
base::WeakPtr<FakeDatagramChannelFactory> peer_factory_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
bool asynchronous_create_;
ChannelsMap channels_;
bool fail_create_;
base::WeakPtrFactory<FakeDatagramChannelFactory> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(FakeDatagramChannelFactory);
};
} // namespace protocol
} // namespace remoting
#endif // REMOTING_PROTOCOL_FAKE_DATAGRAM_SOCKET_H_
......@@ -4,298 +4,21 @@
#include "remoting/protocol/fake_session.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "net/base/address_list.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
namespace protocol {
const char kTestJid[] = "host1@gmail.com/chromoting123";
FakeSocket::FakeSocket()
: async_write_(false),
write_pending_(false),
write_limit_(0),
next_write_error_(net::OK),
next_read_error_(net::OK),
read_pending_(false),
read_buffer_size_(0),
input_pos_(0),
message_loop_(base::MessageLoop::current()),
weak_factory_(this) {
}
FakeSocket::~FakeSocket() {
EXPECT_EQ(message_loop_, base::MessageLoop::current());
}
void FakeSocket::AppendInputData(const std::vector<char>& data) {
EXPECT_EQ(message_loop_, base::MessageLoop::current());
input_data_.insert(input_data_.end(), data.begin(), data.end());
// Complete pending read if any.
if (read_pending_) {
read_pending_ = false;
int result = std::min(read_buffer_size_,
static_cast<int>(input_data_.size() - input_pos_));
CHECK(result > 0);
memcpy(read_buffer_->data(),
&(*input_data_.begin()) + input_pos_, result);
input_pos_ += result;
read_buffer_ = NULL;
read_callback_.Run(result);
}
}
void FakeSocket::PairWith(FakeSocket* peer_socket) {
EXPECT_EQ(message_loop_, base::MessageLoop::current());
peer_socket_ = peer_socket->weak_factory_.GetWeakPtr();
peer_socket->peer_socket_ = weak_factory_.GetWeakPtr();
}
int FakeSocket::Read(net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) {
EXPECT_EQ(message_loop_, base::MessageLoop::current());
if (next_read_error_ != net::OK) {
int r = next_read_error_;
next_read_error_ = net::OK;
return r;
}
if (input_pos_ < static_cast<int>(input_data_.size())) {
int result = std::min(buf_len,
static_cast<int>(input_data_.size()) - input_pos_);
memcpy(buf->data(), &(*input_data_.begin()) + input_pos_, result);
input_pos_ += result;
return result;
} else {
read_pending_ = true;
read_buffer_ = buf;
read_buffer_size_ = buf_len;
read_callback_ = callback;
return net::ERR_IO_PENDING;
}
}
int FakeSocket::Write(net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) {
EXPECT_EQ(message_loop_, base::MessageLoop::current());
EXPECT_FALSE(write_pending_);
if (write_limit_ > 0)
buf_len = std::min(write_limit_, buf_len);
if (async_write_) {
message_loop_->PostTask(FROM_HERE, base::Bind(
&FakeSocket::DoAsyncWrite, weak_factory_.GetWeakPtr(),
scoped_refptr<net::IOBuffer>(buf), buf_len, callback));
write_pending_ = true;
return net::ERR_IO_PENDING;
} else {
if (next_write_error_ != net::OK) {
int r = next_write_error_;
next_write_error_ = net::OK;
return r;
}
DoWrite(buf, buf_len);
return buf_len;
}
}
void FakeSocket::DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len,
const net::CompletionCallback& callback) {
write_pending_ = false;
if (next_write_error_ != net::OK) {
int r = next_write_error_;
next_write_error_ = net::OK;
callback.Run(r);
return;
}
DoWrite(buf.get(), buf_len);
callback.Run(buf_len);
}
void FakeSocket::DoWrite(net::IOBuffer* buf, int buf_len) {
written_data_.insert(written_data_.end(),
buf->data(), buf->data() + buf_len);
if (peer_socket_.get()) {
message_loop_->PostTask(
FROM_HERE,
base::Bind(&FakeSocket::AppendInputData,
peer_socket_,
std::vector<char>(buf->data(), buf->data() + buf_len)));
}
}
int FakeSocket::SetReceiveBufferSize(int32 size) {
NOTIMPLEMENTED();
return net::ERR_NOT_IMPLEMENTED;
}
int FakeSocket::SetSendBufferSize(int32 size) {
NOTIMPLEMENTED();
return net::ERR_NOT_IMPLEMENTED;
}
int FakeSocket::Connect(const net::CompletionCallback& callback) {
EXPECT_EQ(message_loop_, base::MessageLoop::current());
return net::OK;
}
void FakeSocket::Disconnect() {
peer_socket_.reset();
}
bool FakeSocket::IsConnected() const {
EXPECT_EQ(message_loop_, base::MessageLoop::current());
return true;
}
bool FakeSocket::IsConnectedAndIdle() const {
NOTIMPLEMENTED();
return false;
}
int FakeSocket::GetPeerAddress(net::IPEndPoint* address) const {
net::IPAddressNumber ip(net::kIPv4AddressSize);
*address = net::IPEndPoint(ip, 0);
return net::OK;
}
int FakeSocket::GetLocalAddress(net::IPEndPoint* address) const {
NOTIMPLEMENTED();
return net::ERR_NOT_IMPLEMENTED;
}
const net::BoundNetLog& FakeSocket::NetLog() const {
EXPECT_EQ(message_loop_, base::MessageLoop::current());
return net_log_;
}