1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "remoting/protocol/fake_datagram_socket.h"
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/single_thread_task_runner.h"
#include "net/base/address_list.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting::protocol {
FakeDatagramSocket::FakeDatagramSocket()
: input_pos_(0),
task_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()) {}
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_ = nullptr;
std::move(read_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::Recv(const scoped_refptr<net::IOBuffer>& buf,
int buf_len,
const net::CompletionRepeatingCallback& 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::Send(const scoped_refptr<net::IOBuffer>& buf,
int buf_len,
const net::CompletionRepeatingCallback& callback) {
EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
EXPECT_FALSE(send_pending_);
if (async_send_) {
send_pending_ = true;
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&FakeDatagramSocket::DoAsyncSend,
weak_factory_.GetWeakPtr(), buf, buf_len, callback));
return net::ERR_IO_PENDING;
} else {
return DoSend(buf, buf_len);
}
}
void FakeDatagramSocket::DoAsyncSend(
const scoped_refptr<net::IOBuffer>& buf,
int buf_len,
const net::CompletionRepeatingCallback& callback) {
EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
EXPECT_TRUE(send_pending_);
send_pending_ = false;
callback.Run(DoSend(buf, buf_len));
}
int FakeDatagramSocket::DoSend(const scoped_refptr<net::IOBuffer>& buf,
int buf_len) {
EXPECT_TRUE(task_runner_->BelongsToCurrentThread());
if (next_send_error_ != net::OK) {
int r = next_send_error_;
next_send_error_ = net::OK;
return r;
}
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::BindOnce(&FakeDatagramSocket::AppendInputPacket, peer_socket_,
std::string(buf->data(), buf->data() + buf_len)));
}
return buf_len;
}
int FakeDatagramSocket::CopyReadData(const scoped_refptr<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::SingleThreadTaskRunner::GetCurrentDefault()),
asynchronous_create_(false),
fail_create_(false) {}
FakeDatagramChannelFactory::~FakeDatagramChannelFactory() {
for (auto it = channels_.begin(); it != channels_.end(); ++it) {
EXPECT_FALSE(it->second);
}
}
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,
ChannelCreatedCallback callback) {
EXPECT_FALSE(channels_[name]);
std::unique_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::BindOnce(&FakeDatagramChannelFactory::NotifyChannelCreated,
weak_factory_.GetWeakPtr(), std::move(channel), name,
std::move(callback)));
} else {
NotifyChannelCreated(std::move(channel), name, std::move(callback));
}
}
void FakeDatagramChannelFactory::NotifyChannelCreated(
std::unique_ptr<FakeDatagramSocket> owned_socket,
const std::string& name,
ChannelCreatedCallback callback) {
if (base::Contains(channels_, name)) {
std::move(callback).Run(std::move(owned_socket));
}
}
void FakeDatagramChannelFactory::CancelChannelCreation(
const std::string& name) {
channels_.erase(name);
}
} // namespace remoting::protocol
|