OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/security_key/remote_security_key_message_reader_impl.h" |
| 6 |
| 7 #include <cstdint> |
| 8 #include <memory> |
| 9 #include <string> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "remoting/host/security_key/security_key_message.h" |
| 16 #include "remoting/host/setup/test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace { |
| 20 const remoting::RemoteSecurityKeyMessageType kTestMessageType = |
| 21 remoting::RemoteSecurityKeyMessageType::CONNECT; |
| 22 const uint32_t kMaxSecurityKeyMessageByteCount = 256 * 1024; |
| 23 } // namespace |
| 24 |
| 25 namespace remoting { |
| 26 |
| 27 class RemoteSecurityKeyMessageReaderImplTest : public testing::Test { |
| 28 public: |
| 29 RemoteSecurityKeyMessageReaderImplTest(); |
| 30 ~RemoteSecurityKeyMessageReaderImplTest() override; |
| 31 |
| 32 // SecurityKeyMessageCallback passed to the Reader. Stores |message| so it can |
| 33 // be verified by tests. |
| 34 void OnMessage(std::unique_ptr<SecurityKeyMessage> message); |
| 35 |
| 36 // Used as a callback to signal completion. |
| 37 void OperationComplete(); |
| 38 |
| 39 protected: |
| 40 // testing::Test interface. |
| 41 void SetUp() override; |
| 42 |
| 43 // Runs the MessageLoop until the reader has completed and called back. |
| 44 void RunLoop(); |
| 45 |
| 46 // Closes |write_file_| and runs the MessageLoop until the reader has |
| 47 // completed and called back. |
| 48 void CloseWriteFileAndRunLoop(); |
| 49 |
| 50 // Writes a message (header+code+body) to the write-end of the pipe. |
| 51 void WriteMessage(RemoteSecurityKeyMessageType message_type, |
| 52 const std::string& message_payload); |
| 53 |
| 54 // Writes some data to the write-end of the pipe. |
| 55 void WriteData(const char* data, int length); |
| 56 |
| 57 std::unique_ptr<RemoteSecurityKeyMessageReader> reader_; |
| 58 base::File read_file_; |
| 59 base::File write_file_; |
| 60 |
| 61 std::vector<std::unique_ptr<SecurityKeyMessage>> messages_received_; |
| 62 |
| 63 private: |
| 64 base::MessageLoopForIO message_loop_; |
| 65 std::unique_ptr<base::RunLoop> run_loop_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageReaderImplTest); |
| 68 }; |
| 69 |
| 70 RemoteSecurityKeyMessageReaderImplTest::RemoteSecurityKeyMessageReaderImplTest() |
| 71 : run_loop_(new base::RunLoop()) {} |
| 72 |
| 73 RemoteSecurityKeyMessageReaderImplTest:: |
| 74 ~RemoteSecurityKeyMessageReaderImplTest() {} |
| 75 |
| 76 void RemoteSecurityKeyMessageReaderImplTest::SetUp() { |
| 77 ASSERT_TRUE(MakePipe(&read_file_, &write_file_)); |
| 78 reader_.reset(new RemoteSecurityKeyMessageReaderImpl(std::move(read_file_))); |
| 79 |
| 80 // base::Unretained is safe since no further tasks can run after |
| 81 // RunLoop::Run() returns. |
| 82 reader_->Start( |
| 83 base::Bind(&RemoteSecurityKeyMessageReaderImplTest::OnMessage, |
| 84 base::Unretained(this)), |
| 85 base::Bind(&RemoteSecurityKeyMessageReaderImplTest::OperationComplete, |
| 86 base::Unretained(this))); |
| 87 } |
| 88 |
| 89 void RemoteSecurityKeyMessageReaderImplTest::RunLoop() { |
| 90 run_loop_->Run(); |
| 91 run_loop_.reset(new base::RunLoop()); |
| 92 } |
| 93 |
| 94 void RemoteSecurityKeyMessageReaderImplTest::CloseWriteFileAndRunLoop() { |
| 95 write_file_.Close(); |
| 96 run_loop_->Run(); |
| 97 run_loop_.reset(new base::RunLoop()); |
| 98 } |
| 99 |
| 100 void RemoteSecurityKeyMessageReaderImplTest::OnMessage( |
| 101 std::unique_ptr<SecurityKeyMessage> message) { |
| 102 messages_received_.push_back(std::move(message)); |
| 103 OperationComplete(); |
| 104 } |
| 105 |
| 106 void RemoteSecurityKeyMessageReaderImplTest::OperationComplete() { |
| 107 run_loop_->Quit(); |
| 108 } |
| 109 |
| 110 void RemoteSecurityKeyMessageReaderImplTest::WriteMessage( |
| 111 RemoteSecurityKeyMessageType message_type, |
| 112 const std::string& message_payload) { |
| 113 uint32_t length = |
| 114 SecurityKeyMessage::kMessageTypeSizeBytes + message_payload.size(); |
| 115 WriteData(reinterpret_cast<char*>(&length), |
| 116 SecurityKeyMessage::kHeaderSizeBytes); |
| 117 WriteData(reinterpret_cast<char*>(&message_type), |
| 118 SecurityKeyMessage::kMessageTypeSizeBytes); |
| 119 if (!message_payload.empty()) { |
| 120 WriteData(message_payload.data(), message_payload.size()); |
| 121 } |
| 122 } |
| 123 |
| 124 void RemoteSecurityKeyMessageReaderImplTest::WriteData(const char* data, |
| 125 int length) { |
| 126 int written = write_file_.WriteAtCurrentPos(data, length); |
| 127 ASSERT_EQ(length, written); |
| 128 } |
| 129 |
| 130 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageWithNoPayload) { |
| 131 WriteMessage(kTestMessageType, std::string()); |
| 132 RunLoop(); |
| 133 ASSERT_EQ(1u, messages_received_.size()); |
| 134 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); |
| 135 ASSERT_EQ("", messages_received_[0]->payload()); |
| 136 |
| 137 CloseWriteFileAndRunLoop(); |
| 138 } |
| 139 |
| 140 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageWithPayload) { |
| 141 std::string payload("I AM A VALID MESSAGE PAYLOAD!!!!!!!!!!!!!!!!!!!!!!"); |
| 142 WriteMessage(kTestMessageType, payload); |
| 143 RunLoop(); |
| 144 ASSERT_EQ(1u, messages_received_.size()); |
| 145 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); |
| 146 ASSERT_EQ(payload, messages_received_[0]->payload()); |
| 147 |
| 148 CloseWriteFileAndRunLoop(); |
| 149 } |
| 150 |
| 151 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageViaSingleWrite) { |
| 152 // All other tests write in 2-3 chunks, this writes the message in one shot. |
| 153 std::string payload("LLLLTI am the best payload in the history of testing."); |
| 154 // Overwite the 'L' values with the actual length. |
| 155 uint8_t length = payload.size() - SecurityKeyMessage::kHeaderSizeBytes; |
| 156 payload[0] = static_cast<char>(length); |
| 157 payload[1] = 0; |
| 158 payload[2] = 0; |
| 159 payload[3] = 0; |
| 160 // Overwite the 'T' value with the actual type. |
| 161 payload[4] = static_cast<char>(kTestMessageType); |
| 162 WriteData(payload.data(), payload.size()); |
| 163 RunLoop(); |
| 164 ASSERT_EQ(1u, messages_received_.size()); |
| 165 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); |
| 166 ASSERT_EQ(payload.substr(5), messages_received_[0]->payload()); |
| 167 |
| 168 CloseWriteFileAndRunLoop(); |
| 169 } |
| 170 |
| 171 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageViaMultipleWrites) { |
| 172 // All other tests write in 2-3 chunks, this writes the message byte by byte. |
| 173 std::string payload("LLLLTI am the worst payload in the history of testing."); |
| 174 // Overwite the 'L' values with the actual length. |
| 175 uint8_t length = payload.size() - SecurityKeyMessage::kHeaderSizeBytes; |
| 176 payload[0] = static_cast<char>(length); |
| 177 payload[1] = 0; |
| 178 payload[2] = 0; |
| 179 payload[3] = 0; |
| 180 // Overwite the 'T' value with the actual type. |
| 181 payload[4] = static_cast<char>(kTestMessageType); |
| 182 |
| 183 for (uint32_t i = 0; i < payload.size(); i++) { |
| 184 WriteData(&payload[i], 1); |
| 185 } |
| 186 RunLoop(); |
| 187 ASSERT_EQ(1u, messages_received_.size()); |
| 188 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); |
| 189 ASSERT_EQ(payload.substr(5), messages_received_[0]->payload()); |
| 190 |
| 191 CloseWriteFileAndRunLoop(); |
| 192 } |
| 193 |
| 194 TEST_F(RemoteSecurityKeyMessageReaderImplTest, SingleMessageWithLargePayload) { |
| 195 std::string payload(kMaxSecurityKeyMessageByteCount - |
| 196 SecurityKeyMessage::kMessageTypeSizeBytes, |
| 197 'Y'); |
| 198 WriteMessage(kTestMessageType, payload); |
| 199 RunLoop(); |
| 200 ASSERT_EQ(1u, messages_received_.size()); |
| 201 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); |
| 202 ASSERT_EQ(payload, messages_received_[0]->payload()); |
| 203 |
| 204 CloseWriteFileAndRunLoop(); |
| 205 } |
| 206 |
| 207 TEST_F(RemoteSecurityKeyMessageReaderImplTest, EmptyFile) { |
| 208 CloseWriteFileAndRunLoop(); |
| 209 ASSERT_EQ(0u, messages_received_.size()); |
| 210 } |
| 211 |
| 212 TEST_F(RemoteSecurityKeyMessageReaderImplTest, InvalidMessageLength) { |
| 213 uint32_t length = kMaxSecurityKeyMessageByteCount + 1; |
| 214 ASSERT_FALSE(SecurityKeyMessage::IsValidMessageSize(length)); |
| 215 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 216 CloseWriteFileAndRunLoop(); |
| 217 ASSERT_EQ(0u, messages_received_.size()); |
| 218 } |
| 219 |
| 220 TEST_F(RemoteSecurityKeyMessageReaderImplTest, ShortHeader) { |
| 221 // Write only 3 bytes - the message length header is supposed to be 4 bytes. |
| 222 WriteData("xxx", SecurityKeyMessage::kHeaderSizeBytes - 1); |
| 223 CloseWriteFileAndRunLoop(); |
| 224 ASSERT_EQ(0u, messages_received_.size()); |
| 225 } |
| 226 |
| 227 TEST_F(RemoteSecurityKeyMessageReaderImplTest, ZeroLengthMessage) { |
| 228 uint32_t length = 0; |
| 229 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 230 CloseWriteFileAndRunLoop(); |
| 231 ASSERT_EQ(0u, messages_received_.size()); |
| 232 } |
| 233 |
| 234 TEST_F(RemoteSecurityKeyMessageReaderImplTest, MissingControlCode) { |
| 235 uint32_t length = 1; |
| 236 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 237 CloseWriteFileAndRunLoop(); |
| 238 ASSERT_EQ(0u, messages_received_.size()); |
| 239 } |
| 240 |
| 241 TEST_F(RemoteSecurityKeyMessageReaderImplTest, MissingPayload) { |
| 242 uint32_t length = 2; |
| 243 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 244 |
| 245 char test_control_code = static_cast<char>(kTestMessageType); |
| 246 WriteData(&test_control_code, sizeof(test_control_code)); |
| 247 CloseWriteFileAndRunLoop(); |
| 248 ASSERT_EQ(0u, messages_received_.size()); |
| 249 } |
| 250 |
| 251 TEST_F(RemoteSecurityKeyMessageReaderImplTest, MultipleMessages) { |
| 252 std::vector<std::string> payloads({"", "S", // Really short |
| 253 "", "Short", "", "Medium Length", "", |
| 254 "Longer than medium, but not super long", |
| 255 "", std::string(2048, 'Y'), ""}); |
| 256 for (size_t i = 0; i < payloads.size(); i++) { |
| 257 WriteMessage(kTestMessageType, payloads[i]); |
| 258 RunLoop(); |
| 259 ASSERT_EQ(i + 1, messages_received_.size()); |
| 260 } |
| 261 CloseWriteFileAndRunLoop(); |
| 262 |
| 263 for (size_t i = 0; i < payloads.size(); i++) { |
| 264 ASSERT_EQ(kTestMessageType, messages_received_[i]->type()); |
| 265 ASSERT_EQ(payloads[i], messages_received_[i]->payload()); |
| 266 } |
| 267 } |
| 268 |
| 269 } // namespace remoting |
OLD | NEW |