| 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.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <string> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" |
| 16 #include "remoting/host/security_key/security_key_message.h" |
| 17 #include "remoting/host/setup/test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace { |
| 21 const uint8_t kTestControlCode = 42; |
| 22 const unsigned int kBufferSizeBytes = 40000; |
| 23 const uint32_t kMaxSecurityKeyMessageByteCount = 256 * 1024; |
| 24 } // namespace |
| 25 |
| 26 namespace remoting { |
| 27 |
| 28 class RemoteSecurityKeyMessageReaderTest : public testing::Test { |
| 29 public: |
| 30 RemoteSecurityKeyMessageReaderTest(); |
| 31 ~RemoteSecurityKeyMessageReaderTest() override; |
| 32 |
| 33 // Starts the reader and runs the MessageLoop to completion. |
| 34 void Run(); |
| 35 |
| 36 // SecurityKeyMessage::Callback passed to the Reader. Stores |message| so it |
| 37 // can be verified by tests. |
| 38 void OnMessage(scoped_ptr<SecurityKeyMessage> message); |
| 39 |
| 40 // Writes a message (header+code+body) to the write-end of the pipe. |
| 41 void WriteMessage(uint8_t message_type, const std::string& message_payload); |
| 42 |
| 43 // Writes some data to the write-end of the pipe. |
| 44 void WriteData(const char* data, int length); |
| 45 |
| 46 protected: |
| 47 // testing::Test interface. |
| 48 void SetUp() override; |
| 49 |
| 50 scoped_ptr<RemoteSecurityKeyMessageReader> reader_; |
| 51 base::File read_file_; |
| 52 base::File write_file_; |
| 53 uint8_t last_message_type_ = 0; |
| 54 std::string last_message_payload_; |
| 55 |
| 56 std::vector<uint8_t> message_types_; |
| 57 std::vector<std::string> message_payloads_; |
| 58 |
| 59 private: |
| 60 base::MessageLoopForIO message_loop_; |
| 61 base::RunLoop run_loop_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageReaderTest); |
| 64 }; |
| 65 |
| 66 RemoteSecurityKeyMessageReaderTest::RemoteSecurityKeyMessageReaderTest() {} |
| 67 RemoteSecurityKeyMessageReaderTest::~RemoteSecurityKeyMessageReaderTest() {} |
| 68 |
| 69 void RemoteSecurityKeyMessageReaderTest::SetUp() { |
| 70 ASSERT_TRUE(MakePipe(&read_file_, &write_file_, kBufferSizeBytes)); |
| 71 reader_.reset(new RemoteSecurityKeyMessageReader(std::move(read_file_))); |
| 72 } |
| 73 |
| 74 void RemoteSecurityKeyMessageReaderTest::Run() { |
| 75 // Close the write-end, so the reader doesn't block waiting for more data. |
| 76 write_file_.Close(); |
| 77 |
| 78 // base::Unretained is safe since no further tasks can run after |
| 79 // RunLoop::Run() returns. |
| 80 reader_->Start(base::Bind(&RemoteSecurityKeyMessageReaderTest::OnMessage, |
| 81 base::Unretained(this)), |
| 82 run_loop_.QuitClosure()); |
| 83 run_loop_.Run(); |
| 84 } |
| 85 |
| 86 void RemoteSecurityKeyMessageReaderTest::OnMessage( |
| 87 scoped_ptr<SecurityKeyMessage> message) { |
| 88 last_message_type_ = static_cast<uint8_t>(message->type()); |
| 89 last_message_payload_ = message->payload(); |
| 90 |
| 91 message_types_.push_back(last_message_type_); |
| 92 message_payloads_.push_back(last_message_payload_); |
| 93 } |
| 94 |
| 95 void RemoteSecurityKeyMessageReaderTest::WriteMessage( |
| 96 uint8_t message_type, |
| 97 const std::string& message_payload) { |
| 98 uint32_t length = sizeof(message_type) + message_payload.size(); |
| 99 WriteData(reinterpret_cast<char*>(&length), |
| 100 SecurityKeyMessage::kHeaderSizeBytes); |
| 101 WriteData(reinterpret_cast<char*>(&message_type), sizeof(message_type)); |
| 102 if (!message_payload.empty()) { |
| 103 WriteData(message_payload.data(), message_payload.size()); |
| 104 } |
| 105 } |
| 106 |
| 107 void RemoteSecurityKeyMessageReaderTest::WriteData(const char* data, |
| 108 int length) { |
| 109 int written = write_file_.WriteAtCurrentPos(data, length); |
| 110 ASSERT_EQ(length, written); |
| 111 } |
| 112 |
| 113 TEST_F(RemoteSecurityKeyMessageReaderTest, SingleMessageWithNoPayload) { |
| 114 WriteMessage(kTestControlCode, std::string()); |
| 115 Run(); |
| 116 EXPECT_EQ(last_message_type_, kTestControlCode); |
| 117 EXPECT_EQ(last_message_payload_, ""); |
| 118 } |
| 119 |
| 120 TEST_F(RemoteSecurityKeyMessageReaderTest, SingleMessageWithPayload) { |
| 121 std::string payload("I AM A VALID MESSAGE PAYLOAD!!!!!!!!!!!!!!!!!!!!!!"); |
| 122 WriteMessage(kTestControlCode, payload); |
| 123 Run(); |
| 124 EXPECT_EQ(last_message_type_, kTestControlCode); |
| 125 EXPECT_EQ(last_message_payload_, payload); |
| 126 } |
| 127 |
| 128 TEST_F(RemoteSecurityKeyMessageReaderTest, SingleMessageWithLargePayload) { |
| 129 std::string payload(kBufferSizeBytes - sizeof(RemoteSecurityKeyMessageType) - |
| 130 SecurityKeyMessage::kHeaderSizeBytes, |
| 131 'Y'); |
| 132 WriteMessage(kTestControlCode, payload); |
| 133 Run(); |
| 134 EXPECT_EQ(last_message_type_, kTestControlCode); |
| 135 EXPECT_EQ(last_message_payload_, payload); |
| 136 } |
| 137 |
| 138 TEST_F(RemoteSecurityKeyMessageReaderTest, EmptyFile) { |
| 139 Run(); |
| 140 EXPECT_EQ(last_message_type_, 0); |
| 141 EXPECT_EQ(last_message_payload_, ""); |
| 142 } |
| 143 |
| 144 TEST_F(RemoteSecurityKeyMessageReaderTest, InvalidMessageLength) { |
| 145 uint32_t length = kMaxSecurityKeyMessageByteCount + 1; |
| 146 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 147 Run(); |
| 148 EXPECT_EQ(last_message_type_, 0); |
| 149 EXPECT_EQ(last_message_payload_, ""); |
| 150 } |
| 151 |
| 152 TEST_F(RemoteSecurityKeyMessageReaderTest, ShortHeader) { |
| 153 // Write only 3 bytes - the message length header is supposed to be 4 bytes. |
| 154 WriteData("xxx", SecurityKeyMessage::kHeaderSizeBytes - 1); |
| 155 Run(); |
| 156 EXPECT_EQ(last_message_type_, 0); |
| 157 EXPECT_EQ(last_message_payload_, ""); |
| 158 } |
| 159 |
| 160 TEST_F(RemoteSecurityKeyMessageReaderTest, MissingControlCode) { |
| 161 uint32_t length = 1; |
| 162 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 163 Run(); |
| 164 EXPECT_EQ(last_message_type_, 0); |
| 165 EXPECT_EQ(last_message_payload_, ""); |
| 166 } |
| 167 |
| 168 TEST_F(RemoteSecurityKeyMessageReaderTest, MissingPayload) { |
| 169 uint32_t length = 2; |
| 170 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 171 |
| 172 char test_control_code = static_cast<char>(kTestControlCode); |
| 173 WriteData(&test_control_code, sizeof(test_control_code)); |
| 174 Run(); |
| 175 EXPECT_EQ(last_message_type_, 0); |
| 176 EXPECT_EQ(last_message_payload_, ""); |
| 177 } |
| 178 |
| 179 TEST_F(RemoteSecurityKeyMessageReaderTest, MultipleMessages) { |
| 180 const size_t kMessageCount = 11; |
| 181 std::vector<std::string> payloads({"", "S", // Really short |
| 182 "", "Short", "", "Medium Length", "", |
| 183 "Longer than medium, but not super long", |
| 184 "", std::string(2048, 'Y'), ""}); |
| 185 ASSERT_EQ(kMessageCount, payloads.size()); |
| 186 |
| 187 for (size_t i = 0; i < kMessageCount; i++) { |
| 188 WriteMessage(kTestControlCode + i, payloads[i]); |
| 189 } |
| 190 |
| 191 Run(); |
| 192 |
| 193 for (size_t i = 0; i < kMessageCount; i++) { |
| 194 ASSERT_EQ(message_types_[i], kTestControlCode + i); |
| 195 ASSERT_EQ(message_payloads_[i], payloads[i]); |
| 196 } |
| 197 } |
| 198 |
| 199 } // namespace remoting |
| OLD | NEW |