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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/file_transfer/file_transfer_message_handler.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/containers/queue.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/test/task_environment.h"
#include "net/base/io_buffer.h"
#include "remoting/base/compound_buffer.h"
#include "remoting/host/file_transfer/fake_file_operations.h"
#include "remoting/host/file_transfer/test_byte_vector_utils.h"
#include "remoting/protocol/fake_message_pipe.h"
#include "remoting/protocol/fake_message_pipe_wrapper.h"
#include "remoting/protocol/file_transfer_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr char kTestDatachannelName[] = "filetransfer-test";
constexpr char kTestFilename[] = "test-file.txt";
std::unique_ptr<remoting::CompoundBuffer> StringToBuffer(
const std::string& data) {
std::unique_ptr<remoting::CompoundBuffer> buffer =
std::make_unique<remoting::CompoundBuffer>();
buffer->Append(base::MakeRefCounted<net::StringIOBuffer>(data.data()),
data.size());
return buffer;
}
std::unique_ptr<remoting::CompoundBuffer> MessageToBuffer(
const remoting::protocol::FileTransfer& message) {
return StringToBuffer(message.SerializeAsString());
}
std::unique_ptr<remoting::CompoundBuffer> DataToBuffer(
const std::vector<std::uint8_t>& data) {
remoting::protocol::FileTransfer message;
message.mutable_data()->set_data(std::string(data.begin(), data.end()));
return MessageToBuffer(message);
}
// base::queue doesn't provide operator==.
template <typename T>
bool QueuesEqual(const base::queue<T>& a, const base::queue<T>& b) {
if (a.size() != b.size()) {
return false;
}
auto a_copy = a;
auto b_copy = b;
while (!a_copy.empty()) {
if (a_copy.front() != b_copy.front()) {
return false;
}
a_copy.pop();
b_copy.pop();
}
return true;
}
} // namespace
namespace remoting {
class FileTransferMessageHandlerTest : public testing::Test {
public:
FileTransferMessageHandlerTest();
~FileTransferMessageHandlerTest() override;
// testing::Test implementation.
void SetUp() override;
void TearDown() override;
protected:
const std::vector<std::uint8_t> kTestDataOne =
ByteArrayFrom("this is the first test string");
const std::vector<std::uint8_t> kTestDataTwo =
ByteArrayFrom("this is the second test string");
const std::vector<std::uint8_t> kTestDataThree =
ByteArrayFrom("this is the third test string");
base::test::TaskEnvironment task_environment_;
std::unique_ptr<protocol::FakeMessagePipe> fake_pipe_;
protocol::FileTransfer fake_metadata_;
protocol::FileTransfer fake_end_;
protocol::FileTransfer fake_request_transfer_;
protocol::FileTransfer fake_success_;
};
FileTransferMessageHandlerTest::FileTransferMessageHandlerTest()
: task_environment_(
base::test::TaskEnvironment::MainThreadType::DEFAULT,
base::test::TaskEnvironment::ThreadPoolExecutionMode::QUEUED) {}
FileTransferMessageHandlerTest::~FileTransferMessageHandlerTest() = default;
void FileTransferMessageHandlerTest::SetUp() {
fake_pipe_ =
base::WrapUnique(new protocol::FakeMessagePipe(false /* asynchronous */));
fake_metadata_.mutable_metadata()->set_filename(kTestFilename);
fake_metadata_.mutable_metadata()->set_size(
kTestDataOne.size() + kTestDataTwo.size() + kTestDataThree.size());
fake_end_.mutable_end();
fake_request_transfer_.mutable_request_transfer();
fake_success_.mutable_success();
}
void FileTransferMessageHandlerTest::TearDown() {}
// Upload tests.
// Verifies that the message handler creates, writes to, and closes a
// FileOperations::Writer without errors when given valid input.
TEST_F(FileTransferMessageHandlerTest, WritesThreeChunks) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
fake_pipe_->OpenPipe();
fake_pipe_->Receive(MessageToBuffer(fake_metadata_));
fake_pipe_->Receive(DataToBuffer(kTestDataOne));
fake_pipe_->Receive(DataToBuffer(kTestDataTwo));
fake_pipe_->Receive(DataToBuffer(kTestDataThree));
fake_pipe_->Receive(MessageToBuffer(fake_end_));
task_environment_.RunUntilIdle();
fake_pipe_->ClosePipe();
ASSERT_EQ(1ul, test_io.files_written.size());
ASSERT_EQ(false, test_io.files_written[0].failed);
std::vector<std::vector<std::uint8_t>> expected_chunks = {
kTestDataOne, kTestDataTwo, kTestDataThree};
ASSERT_EQ(expected_chunks, test_io.files_written[0].chunks);
const base::queue<std::string>& actual_sent_messages =
fake_pipe_->sent_messages();
base::queue<std::string> expected_sent_messages;
expected_sent_messages.push(fake_success_.SerializeAsString());
ASSERT_TRUE(QueuesEqual(expected_sent_messages, actual_sent_messages));
}
// Verifies that the message handler sends an error protobuf when
// FileOperations::Writer returns an error.
TEST_F(FileTransferMessageHandlerTest, HandlesWriteError) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
protocol::FileTransfer_Error fake_error = protocol::MakeFileTransferError(
FROM_HERE, protocol::FileTransfer_Error_Type_IO_ERROR);
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
fake_pipe_->OpenPipe();
fake_pipe_->Receive(MessageToBuffer(fake_metadata_));
fake_pipe_->Receive(DataToBuffer(kTestDataOne));
fake_pipe_->Receive(DataToBuffer(kTestDataTwo));
task_environment_.RunUntilIdle();
test_io.io_error = fake_error;
fake_pipe_->Receive(DataToBuffer(kTestDataTwo));
fake_pipe_->Receive(MessageToBuffer(fake_end_));
task_environment_.RunUntilIdle();
fake_pipe_->ClosePipe();
const base::queue<std::string>& actual_sent_messages =
fake_pipe_->sent_messages();
protocol::FileTransfer expected_response;
*expected_response.mutable_error() = fake_error;
base::queue<std::string> expected_sent_messages;
expected_sent_messages.push(expected_response.SerializeAsString());
ASSERT_TRUE(QueuesEqual(expected_sent_messages, actual_sent_messages));
}
// Verifies that the message handler cancels the write if an error is received
// from the sender.
TEST_F(FileTransferMessageHandlerTest, HandlesErrorMessage) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
protocol::FileTransfer fake_error_message;
*fake_error_message.mutable_error() = protocol::MakeFileTransferError(
FROM_HERE, protocol::FileTransfer_Error_Type_IO_ERROR);
fake_pipe_->OpenPipe();
fake_pipe_->Receive(MessageToBuffer(fake_metadata_));
fake_pipe_->Receive(DataToBuffer(kTestDataOne));
fake_pipe_->Receive(DataToBuffer(kTestDataTwo));
task_environment_.RunUntilIdle();
fake_pipe_->Receive(MessageToBuffer(fake_error_message));
task_environment_.RunUntilIdle();
fake_pipe_->ClosePipe();
ASSERT_EQ(1ul, test_io.files_written.size());
ASSERT_EQ(true, test_io.files_written[0].failed);
std::vector<std::vector<std::uint8_t>> expected_chunks = {kTestDataOne,
kTestDataTwo};
ASSERT_EQ(expected_chunks, test_io.files_written[0].chunks);
const base::queue<std::string>& actual_sent_messages =
fake_pipe_->sent_messages();
// No messages
base::queue<std::string> expected_sent_messages;
ASSERT_TRUE(QueuesEqual(expected_sent_messages, actual_sent_messages));
}
// Verifies that the message handler cancels the write if the connection is
// closed prematurely.
TEST_F(FileTransferMessageHandlerTest, HandlesPrematureClose) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
fake_pipe_->OpenPipe();
fake_pipe_->Receive(MessageToBuffer(fake_metadata_));
fake_pipe_->Receive(DataToBuffer(kTestDataOne));
fake_pipe_->Receive(DataToBuffer(kTestDataTwo));
task_environment_.RunUntilIdle();
fake_pipe_->ClosePipe();
ASSERT_EQ(1ul, test_io.files_written.size());
ASSERT_EQ(true, test_io.files_written[0].failed);
std::vector<std::vector<std::uint8_t>> expected_chunks = {kTestDataOne,
kTestDataTwo};
ASSERT_EQ(expected_chunks, test_io.files_written[0].chunks);
}
// Verifies that an error is sent if data is sent before/without metadata.
TEST_F(FileTransferMessageHandlerTest, ErrorsOnMissingMetadata) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
fake_pipe_->OpenPipe();
fake_pipe_->Receive(DataToBuffer(kTestDataOne));
fake_pipe_->Receive(DataToBuffer(kTestDataTwo));
fake_pipe_->Receive(DataToBuffer(kTestDataThree));
fake_pipe_->Receive(MessageToBuffer(fake_end_));
task_environment_.RunUntilIdle();
fake_pipe_->ClosePipe();
ASSERT_EQ(0ul, test_io.files_written.size());
const base::queue<std::string>& sent_messages = fake_pipe_->sent_messages();
ASSERT_EQ(1ul, sent_messages.size());
protocol::FileTransfer response;
response.ParseFromString(sent_messages.front());
ASSERT_EQ(protocol::FileTransfer::kError, response.message_case());
ASSERT_EQ(protocol::FileTransfer_Error_Type_PROTOCOL_ERROR,
response.error().type());
}
// Verifies that an error is sent if another metadata message is sent.
TEST_F(FileTransferMessageHandlerTest, ErrorsOnNewMetadata) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
fake_pipe_->OpenPipe();
fake_pipe_->Receive(MessageToBuffer(fake_metadata_));
fake_pipe_->Receive(DataToBuffer(kTestDataOne));
fake_pipe_->Receive(DataToBuffer(kTestDataTwo));
fake_pipe_->Receive(DataToBuffer(kTestDataThree));
fake_pipe_->Receive(MessageToBuffer(fake_end_));
task_environment_.RunUntilIdle();
fake_pipe_->Receive(MessageToBuffer(fake_metadata_));
fake_pipe_->ClosePipe();
const base::queue<std::string>& sent_messages = fake_pipe_->sent_messages();
// First is the success message, second should be a protocol error.
ASSERT_EQ(2ul, sent_messages.size());
protocol::FileTransfer response;
response.ParseFromString(sent_messages.back());
ASSERT_EQ(protocol::FileTransfer::kError, response.message_case());
ASSERT_EQ(protocol::FileTransfer_Error_Type_PROTOCOL_ERROR,
response.error().type());
}
// Verifies that an error is sent if more data is sent after Close.
TEST_F(FileTransferMessageHandlerTest, ErrorsOnDataAfterClose) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
fake_pipe_->OpenPipe();
fake_pipe_->Receive(MessageToBuffer(fake_metadata_));
fake_pipe_->Receive(DataToBuffer(kTestDataOne));
fake_pipe_->Receive(DataToBuffer(kTestDataTwo));
fake_pipe_->Receive(DataToBuffer(kTestDataThree));
fake_pipe_->Receive(MessageToBuffer(fake_end_));
fake_pipe_->Receive(DataToBuffer(kTestDataOne));
task_environment_.RunUntilIdle();
fake_pipe_->ClosePipe();
ASSERT_EQ(1ul, test_io.files_written.size());
ASSERT_EQ(true, test_io.files_written[0].failed);
const base::queue<std::string>& sent_messages = fake_pipe_->sent_messages();
// Because the error is triggered before RunUntilIdle is called, there should
// be no complete message this time.
ASSERT_EQ(1ul, sent_messages.size());
protocol::FileTransfer response;
response.ParseFromString(sent_messages.front());
ASSERT_EQ(protocol::FileTransfer::kError, response.message_case());
ASSERT_EQ(protocol::FileTransfer_Error_Type_PROTOCOL_ERROR,
response.error().type());
}
// Download tests.
// Verifies that the message handler will read and respond with a file when a
// RequestTransfer message is received.
TEST_F(FileTransferMessageHandlerTest, ReadsFile) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
test_io.input_file = FakeFileOperations::InputFile(
base::FilePath::FromASCII(kTestFilename),
ByteArrayFrom(kTestDataOne, kTestDataTwo, kTestDataThree), std::nullopt);
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
fake_pipe_->OpenPipe();
fake_pipe_->Receive(MessageToBuffer(fake_request_transfer_));
task_environment_.RunUntilIdle();
fake_pipe_->Receive(MessageToBuffer(fake_success_));
task_environment_.RunUntilIdle();
fake_pipe_->ClosePipe();
const base::queue<std::string>& actual_sent_messages =
fake_pipe_->sent_messages();
base::queue<std::string> expected_sent_messages;
expected_sent_messages.push(fake_metadata_.SerializeAsString());
protocol::FileTransfer data;
data.mutable_data()->set_data(std::string(test_io.input_file->data.begin(),
test_io.input_file->data.end()));
expected_sent_messages.push(data.SerializeAsString());
expected_sent_messages.push(fake_end_.SerializeAsString());
ASSERT_TRUE(QueuesEqual(expected_sent_messages, actual_sent_messages));
}
// Verifies that the message handler forwards errors from opening a file with
// the reader.
TEST_F(FileTransferMessageHandlerTest, ForwardsReaderOpenError) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
test_io.input_file = protocol::MakeFileTransferError(
FROM_HERE, protocol::FileTransfer_Error_Type_CANCELED);
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
fake_pipe_->OpenPipe();
fake_pipe_->Receive(MessageToBuffer(fake_request_transfer_));
task_environment_.RunUntilIdle();
fake_pipe_->ClosePipe();
const base::queue<std::string>& actual_sent_messages =
fake_pipe_->sent_messages();
base::queue<std::string> expected_sent_messages;
protocol::FileTransfer error;
*error.mutable_error() = test_io.input_file.error();
expected_sent_messages.push(error.SerializeAsString());
ASSERT_TRUE(QueuesEqual(expected_sent_messages, actual_sent_messages));
}
// Verifies that the message handler forwards errors from reading a file.
TEST_F(FileTransferMessageHandlerTest, ForwardsReadError) {
FakeFileOperations::TestIo test_io;
auto file_operations = std::make_unique<FakeFileOperations>(&test_io);
test_io.input_file = FakeFileOperations::InputFile(
base::FilePath::FromASCII(kTestFilename),
ByteArrayFrom(kTestDataOne, kTestDataTwo, kTestDataThree),
protocol::MakeFileTransferError(
FROM_HERE, protocol::FileTransfer_Error_Type_IO_ERROR));
// This will delete itself when fake_pipe_->ClosePipe() is called.
new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(),
std::move(file_operations));
fake_pipe_->OpenPipe();
fake_pipe_->Receive(MessageToBuffer(fake_request_transfer_));
task_environment_.RunUntilIdle();
fake_pipe_->ClosePipe();
const base::queue<std::string>& actual_sent_messages =
fake_pipe_->sent_messages();
base::queue<std::string> expected_sent_messages;
expected_sent_messages.push(fake_metadata_.SerializeAsString());
protocol::FileTransfer data;
data.mutable_data()->set_data(std::string(test_io.input_file->data.begin(),
test_io.input_file->data.end()));
expected_sent_messages.push(data.SerializeAsString());
protocol::FileTransfer error;
*error.mutable_error() = *test_io.input_file->io_error;
expected_sent_messages.push(error.SerializeAsString());
ASSERT_TRUE(QueuesEqual(expected_sent_messages, actual_sent_messages));
}
} // namespace remoting
|