Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/host/file_transfer_message_handler.h" |
| 6 | |
| 7 | #include <memory> |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/bind.h" |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 13 | #include "base/containers/queue.h" |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 14 | #include "base/memory/ptr_util.h" |
| 15 | #include "net/base/io_buffer.h" |
| 16 | #include "remoting/base/compound_buffer.h" |
| 17 | #include "remoting/host/file_proxy_wrapper.h" |
| 18 | #include "remoting/protocol/fake_message_pipe.h" |
| 19 | #include "remoting/protocol/fake_message_pipe_wrapper.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | constexpr char kTestDatachannelName[] = "filetransfer-test"; |
| 25 | constexpr char kTestFilename[] = "test-file.txt"; |
| 26 | |
| 27 | std::unique_ptr<remoting::CompoundBuffer> ToBuffer(const std::string& data) { |
| 28 | std::unique_ptr<remoting::CompoundBuffer> buffer = |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame^] | 29 | std::make_unique<remoting::CompoundBuffer>(); |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 30 | buffer->Append(new net::WrappedIOBuffer(data.data()), data.size()); |
| 31 | return buffer; |
| 32 | } |
| 33 | |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 34 | // base::queue doesn't provide operator==. |
| 35 | template <typename T> |
| 36 | bool QueuesEqual(const base::queue<T>& a, const base::queue<T>& b) { |
| 37 | if (a.size() != b.size()) |
| 38 | return false; |
| 39 | |
| 40 | auto a_copy = a; |
| 41 | auto b_copy = b; |
| 42 | while (!a_copy.empty()) { |
| 43 | if (a_copy.front() != b_copy.front()) |
| 44 | return false; |
| 45 | a_copy.pop(); |
| 46 | b_copy.pop(); |
| 47 | } |
| 48 | return true; |
| 49 | } |
| 50 | |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 51 | } // namespace |
| 52 | |
| 53 | namespace remoting { |
| 54 | |
| 55 | class FakeFileProxyWrapper : public FileProxyWrapper { |
| 56 | public: |
| 57 | FakeFileProxyWrapper(); |
| 58 | ~FakeFileProxyWrapper() override; |
| 59 | |
| 60 | // FileProxyWrapper implementation. |
| 61 | void Init(StatusCallback status_callback) override; |
| 62 | void CreateFile(const base::FilePath& directory, |
| 63 | const std::string& filename) override; |
Joseph Arhar | 6f84f28 | 2017-09-08 19:19:34 | [diff] [blame] | 64 | void OpenFile(const base::FilePath& filepath, |
| 65 | OpenFileCallback open_callback) override; |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 66 | void WriteChunk(std::unique_ptr<CompoundBuffer> buffer) override; |
Joseph Arhar | 6f84f28 | 2017-09-08 19:19:34 | [diff] [blame] | 67 | void ReadChunk(uint64_t chunk_size, ReadCallback read_callback) override; |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 68 | void Close() override; |
| 69 | void Cancel() override; |
| 70 | State state() override; |
| 71 | |
| 72 | void RunStatusCallback( |
| 73 | base::Optional<protocol::FileTransferResponse_ErrorCode> error); |
| 74 | const std::string& filename(); |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 75 | base::queue<std::vector<char>> chunks(); |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | State state_ = kUninitialized; |
| 79 | StatusCallback status_callback_; |
| 80 | std::string filename_; |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 81 | base::queue<std::vector<char>> chunks_; |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | class FileTransferMessageHandlerTest : public testing::Test { |
| 85 | public: |
| 86 | FileTransferMessageHandlerTest(); |
| 87 | ~FileTransferMessageHandlerTest() override; |
| 88 | |
| 89 | // testing::Test implementation. |
| 90 | void SetUp() override; |
| 91 | void TearDown() override; |
| 92 | |
| 93 | protected: |
| 94 | const std::string kTestDataOne = "this is the first test string"; |
| 95 | const std::string kTestDataTwo = "this is the second test string"; |
| 96 | |
| 97 | std::unique_ptr<protocol::FakeMessagePipe> fake_pipe_; |
| 98 | protocol::FileTransferRequest fake_request_; |
| 99 | std::string fake_request_string_; |
| 100 | }; |
| 101 | |
| 102 | FakeFileProxyWrapper::FakeFileProxyWrapper() = default; |
| 103 | FakeFileProxyWrapper::~FakeFileProxyWrapper() = default; |
| 104 | |
| 105 | void FakeFileProxyWrapper::Init(StatusCallback status_callback) { |
| 106 | ASSERT_EQ(state_, kUninitialized); |
| 107 | state_ = kInitialized; |
| 108 | |
| 109 | status_callback_ = std::move(status_callback); |
| 110 | } |
| 111 | |
| 112 | void FakeFileProxyWrapper::CreateFile(const base::FilePath& directory, |
| 113 | const std::string& filename) { |
| 114 | ASSERT_EQ(state_, kInitialized); |
Joseph Arhar | 6f84f28 | 2017-09-08 19:19:34 | [diff] [blame] | 115 | state_ = kReady; |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 116 | |
| 117 | filename_ = filename; |
| 118 | } |
| 119 | |
Joseph Arhar | 6f84f28 | 2017-09-08 19:19:34 | [diff] [blame] | 120 | void FakeFileProxyWrapper::OpenFile(const base::FilePath& filepath, |
| 121 | OpenFileCallback open_callback) { |
| 122 | ASSERT_EQ(state_, kInitialized); |
| 123 | state_ = kReady; |
| 124 | |
| 125 | // TODO(jarhar): Implement fake file reading. |
| 126 | } |
| 127 | |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 128 | void FakeFileProxyWrapper::WriteChunk(std::unique_ptr<CompoundBuffer> buffer) { |
Joseph Arhar | 6f84f28 | 2017-09-08 19:19:34 | [diff] [blame] | 129 | ASSERT_EQ(state_, kReady); |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 130 | |
| 131 | std::vector<char> data; |
| 132 | data.resize(buffer->total_bytes()); |
| 133 | buffer->CopyTo(data.data(), data.size()); |
| 134 | chunks_.push(data); |
| 135 | } |
| 136 | |
Joseph Arhar | 6f84f28 | 2017-09-08 19:19:34 | [diff] [blame] | 137 | void FakeFileProxyWrapper::ReadChunk(uint64_t chunk_size, |
| 138 | ReadCallback read_callback) { |
| 139 | ASSERT_EQ(state_, kReady); |
| 140 | |
| 141 | // TODO(jarhar): Implement fake file reading. |
| 142 | } |
| 143 | |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 144 | void FakeFileProxyWrapper::Close() { |
Joseph Arhar | 6f84f28 | 2017-09-08 19:19:34 | [diff] [blame] | 145 | ASSERT_EQ(state_, kReady); |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 146 | state_ = kClosed; |
| 147 | } |
| 148 | |
| 149 | void FakeFileProxyWrapper::Cancel() { |
| 150 | state_ = kFailed; |
| 151 | } |
| 152 | |
| 153 | FileProxyWrapper::State FakeFileProxyWrapper::state() { |
| 154 | return state_; |
| 155 | } |
| 156 | |
| 157 | void FakeFileProxyWrapper::RunStatusCallback( |
| 158 | base::Optional<protocol::FileTransferResponse_ErrorCode> error) { |
| 159 | std::move(status_callback_).Run(state_, error); |
| 160 | } |
| 161 | |
| 162 | const std::string& FakeFileProxyWrapper::filename() { |
| 163 | return filename_; |
| 164 | } |
| 165 | |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 166 | base::queue<std::vector<char>> FakeFileProxyWrapper::chunks() { |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 167 | return chunks_; |
| 168 | } |
| 169 | |
| 170 | FileTransferMessageHandlerTest::FileTransferMessageHandlerTest() = default; |
| 171 | FileTransferMessageHandlerTest::~FileTransferMessageHandlerTest() = default; |
| 172 | |
| 173 | void FileTransferMessageHandlerTest::SetUp() { |
| 174 | fake_pipe_ = |
| 175 | base::WrapUnique(new protocol::FakeMessagePipe(false /* asynchronous */)); |
| 176 | |
| 177 | fake_request_ = protocol::FileTransferRequest(); |
| 178 | fake_request_.set_filename(kTestFilename); |
| 179 | fake_request_.set_filesize(kTestDataOne.size() + kTestDataTwo.size()); |
| 180 | fake_request_.SerializeToString(&fake_request_string_); |
| 181 | } |
| 182 | |
| 183 | void FileTransferMessageHandlerTest::TearDown() {} |
| 184 | |
| 185 | // Verify that the message handler creates, writes to, and closes a |
| 186 | // FileProxyWrapper without errors when given valid input. |
| 187 | TEST_F(FileTransferMessageHandlerTest, WriteTwoChunks) { |
| 188 | std::unique_ptr<FakeFileProxyWrapper> file_proxy_wrapper = |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame^] | 189 | std::make_unique<FakeFileProxyWrapper>(); |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 190 | // |file_proxy_wrapper_ptr| is valid until fake_pipe_->ClosePipe() is called. |
| 191 | FakeFileProxyWrapper* file_proxy_wrapper_ptr = file_proxy_wrapper.get(); |
| 192 | |
| 193 | // This will delete itself when fake_pipe_->ClosePipe() is called. |
| 194 | new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(), |
| 195 | std::move(file_proxy_wrapper)); |
| 196 | |
| 197 | fake_pipe_->OpenPipe(); |
| 198 | fake_pipe_->Receive(ToBuffer(fake_request_string_)); |
| 199 | fake_pipe_->Receive(ToBuffer(kTestDataOne)); |
| 200 | fake_pipe_->Receive(ToBuffer(kTestDataTwo)); |
| 201 | |
| 202 | file_proxy_wrapper_ptr->RunStatusCallback( |
| 203 | base::Optional<protocol::FileTransferResponse_ErrorCode>()); |
| 204 | |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 205 | base::queue<std::vector<char>> actual_chunks = |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 206 | file_proxy_wrapper_ptr->chunks(); |
| 207 | |
| 208 | fake_pipe_->ClosePipe(); |
| 209 | file_proxy_wrapper_ptr = nullptr; |
| 210 | |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 211 | base::queue<std::vector<char>> expected_chunks; |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 212 | expected_chunks.push( |
| 213 | std::vector<char>(kTestDataOne.begin(), kTestDataOne.end())); |
| 214 | expected_chunks.push( |
| 215 | std::vector<char>(kTestDataTwo.begin(), kTestDataTwo.end())); |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 216 | ASSERT_TRUE(QueuesEqual(expected_chunks, actual_chunks)); |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 217 | |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 218 | const base::queue<std::string>& actual_sent_messages = |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 219 | fake_pipe_->sent_messages(); |
| 220 | protocol::FileTransferResponse expected_response; |
| 221 | expected_response.set_state( |
| 222 | protocol::FileTransferResponse_TransferState_DONE); |
| 223 | expected_response.set_total_bytes_written(fake_request_.filesize()); |
| 224 | std::string expected_response_string; |
| 225 | expected_response.SerializeToString(&expected_response_string); |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 226 | base::queue<std::string> expected_sent_messages; |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 227 | expected_sent_messages.push(expected_response_string); |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 228 | ASSERT_TRUE(QueuesEqual(expected_sent_messages, actual_sent_messages)); |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Verifies that the message handler sends an error protobuf when |
| 232 | // FileProxyWrapper returns an error. |
| 233 | TEST_F(FileTransferMessageHandlerTest, FileProxyError) { |
| 234 | std::unique_ptr<FakeFileProxyWrapper> file_proxy_wrapper = |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame^] | 235 | std::make_unique<FakeFileProxyWrapper>(); |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 236 | // |file_proxy_wrapper_ptr| is valid until fake_pipe_->ClosePipe() is called. |
| 237 | FakeFileProxyWrapper* file_proxy_wrapper_ptr = file_proxy_wrapper.get(); |
| 238 | |
| 239 | protocol::FileTransferResponse_ErrorCode fake_error = |
| 240 | protocol::FileTransferResponse_ErrorCode_FILE_IO_ERROR; |
| 241 | |
| 242 | // This will delete itself when fake_pipe_->ClosePipe() is called. |
| 243 | new FileTransferMessageHandler(kTestDatachannelName, fake_pipe_->Wrap(), |
| 244 | std::move(file_proxy_wrapper)); |
| 245 | |
| 246 | fake_pipe_->OpenPipe(); |
| 247 | fake_pipe_->Receive(ToBuffer(fake_request_string_)); |
| 248 | fake_pipe_->Receive(ToBuffer(kTestDataOne)); |
| 249 | |
| 250 | file_proxy_wrapper_ptr->Cancel(); |
| 251 | file_proxy_wrapper_ptr->RunStatusCallback( |
| 252 | base::Optional<protocol::FileTransferResponse_ErrorCode>(fake_error)); |
| 253 | |
| 254 | fake_pipe_->ClosePipe(); |
| 255 | file_proxy_wrapper_ptr = nullptr; |
| 256 | |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 257 | const base::queue<std::string>& actual_sent_messages = |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 258 | fake_pipe_->sent_messages(); |
| 259 | protocol::FileTransferResponse expected_response; |
| 260 | expected_response.set_error(fake_error); |
| 261 | std::string expected_response_string; |
| 262 | expected_response.SerializeToString(&expected_response_string); |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 263 | base::queue<std::string> expected_sent_messages; |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 264 | expected_sent_messages.push(expected_response_string); |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 265 | ASSERT_TRUE(QueuesEqual(expected_sent_messages, actual_sent_messages)); |
Joseph Arhar | 10e5edd | 2017-08-28 22:12:50 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | } // namespace remoting |