| 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/security_key_message_writer_impl.h" | |
| 6 | |
| 7 #include <cstdint> | |
| 8 #include <memory> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/stl_util.h" | |
| 15 #include "base/task_runner_util.h" | |
| 16 #include "base/threading/thread.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "remoting/host/security_key/security_key_message.h" | |
| 19 #include "remoting/host/setup/test_util.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 namespace { | |
| 23 const remoting::SecurityKeyMessageType kTestMessageType = | |
| 24 remoting::SecurityKeyMessageType::CONNECT; | |
| 25 const unsigned int kLargeMessageSizeBytes = 200000; | |
| 26 } // namespace | |
| 27 | |
| 28 namespace remoting { | |
| 29 | |
| 30 class SecurityKeyMessageWriterImplTest : public testing::Test { | |
| 31 public: | |
| 32 SecurityKeyMessageWriterImplTest(); | |
| 33 ~SecurityKeyMessageWriterImplTest() override; | |
| 34 | |
| 35 // Run on a separate thread, this method reads the message written to the | |
| 36 // output stream and returns the result. | |
| 37 std::string ReadMessage(int payload_length_bytes); | |
| 38 | |
| 39 // Called back once the read operation has completed. | |
| 40 void OnReadComplete(const base::Closure& done_callback, | |
| 41 const std::string& result); | |
| 42 | |
| 43 protected: | |
| 44 // testing::Test interface. | |
| 45 void SetUp() override; | |
| 46 | |
| 47 // Writes |kTestMessageType| and |payload| to the output stream and verifies | |
| 48 // they were written correctly. | |
| 49 void WriteMessageToOutput(const std::string& payload); | |
| 50 | |
| 51 std::unique_ptr<SecurityKeyMessageWriter> writer_; | |
| 52 base::File read_file_; | |
| 53 base::File write_file_; | |
| 54 | |
| 55 // Stores the result of the last read operation. | |
| 56 std::string message_result_; | |
| 57 | |
| 58 private: | |
| 59 DISALLOW_COPY_AND_ASSIGN(SecurityKeyMessageWriterImplTest); | |
| 60 }; | |
| 61 | |
| 62 SecurityKeyMessageWriterImplTest::SecurityKeyMessageWriterImplTest() {} | |
| 63 | |
| 64 SecurityKeyMessageWriterImplTest::~SecurityKeyMessageWriterImplTest() {} | |
| 65 | |
| 66 std::string SecurityKeyMessageWriterImplTest::ReadMessage( | |
| 67 int payload_length_bytes) { | |
| 68 std::string message_header(SecurityKeyMessage::kHeaderSizeBytes, '\0'); | |
| 69 read_file_.ReadAtCurrentPos(string_as_array(&message_header), | |
| 70 SecurityKeyMessage::kHeaderSizeBytes); | |
| 71 | |
| 72 std::string message_type(SecurityKeyMessage::kMessageTypeSizeBytes, '\0'); | |
| 73 read_file_.ReadAtCurrentPos(string_as_array(&message_type), | |
| 74 SecurityKeyMessage::kMessageTypeSizeBytes); | |
| 75 | |
| 76 std::string message_data(payload_length_bytes, '\0'); | |
| 77 if (payload_length_bytes) { | |
| 78 read_file_.ReadAtCurrentPos(string_as_array(&message_data), | |
| 79 payload_length_bytes); | |
| 80 } | |
| 81 | |
| 82 return message_header + message_type + message_data; | |
| 83 } | |
| 84 | |
| 85 void SecurityKeyMessageWriterImplTest::OnReadComplete( | |
| 86 const base::Closure& done_callback, | |
| 87 const std::string& result) { | |
| 88 message_result_ = result; | |
| 89 done_callback.Run(); | |
| 90 } | |
| 91 | |
| 92 void SecurityKeyMessageWriterImplTest::SetUp() { | |
| 93 ASSERT_TRUE(MakePipe(&read_file_, &write_file_)); | |
| 94 writer_.reset(new SecurityKeyMessageWriterImpl(std::move(write_file_))); | |
| 95 } | |
| 96 | |
| 97 void SecurityKeyMessageWriterImplTest::WriteMessageToOutput( | |
| 98 const std::string& payload) { | |
| 99 // Thread used for blocking IO operations. | |
| 100 base::Thread reader_thread("ReaderThread"); | |
| 101 | |
| 102 base::Thread::Options options; | |
| 103 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
| 104 reader_thread.StartWithOptions(options); | |
| 105 | |
| 106 // Used to block until the read complete callback is triggered. | |
| 107 base::MessageLoopForIO message_loop; | |
| 108 base::RunLoop run_loop; | |
| 109 | |
| 110 ASSERT_TRUE(base::PostTaskAndReplyWithResult( | |
| 111 reader_thread.task_runner().get(), FROM_HERE, | |
| 112 base::Bind(&SecurityKeyMessageWriterImplTest::ReadMessage, | |
| 113 base::Unretained(this), payload.size()), | |
| 114 base::Bind(&SecurityKeyMessageWriterImplTest::OnReadComplete, | |
| 115 base::Unretained(this), run_loop.QuitClosure()))); | |
| 116 | |
| 117 if (payload.size()) { | |
| 118 ASSERT_TRUE(writer_->WriteMessageWithPayload(kTestMessageType, payload)); | |
| 119 } else { | |
| 120 ASSERT_TRUE(writer_->WriteMessage(kTestMessageType)); | |
| 121 } | |
| 122 | |
| 123 run_loop.Run(); | |
| 124 | |
| 125 size_t total_size = SecurityKeyMessage::kHeaderSizeBytes + | |
| 126 SecurityKeyMessage::kMessageTypeSizeBytes + | |
| 127 payload.size(); | |
| 128 ASSERT_EQ(message_result_.size(), total_size); | |
| 129 | |
| 130 SecurityKeyMessageType type = | |
| 131 SecurityKeyMessage::MessageTypeFromValue(message_result_[4]); | |
| 132 ASSERT_EQ(kTestMessageType, type); | |
| 133 | |
| 134 if (payload.size()) { | |
| 135 ASSERT_EQ(message_result_.substr(5), payload); | |
| 136 } | |
| 137 | |
| 138 // Destroy the writer and verify the other end of the pipe is clean. | |
| 139 writer_.reset(); | |
| 140 char unused; | |
| 141 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0); | |
| 142 } | |
| 143 | |
| 144 TEST_F(SecurityKeyMessageWriterImplTest, WriteMessageWithoutPayload) { | |
| 145 std::string empty_payload; | |
| 146 WriteMessageToOutput(empty_payload); | |
| 147 } | |
| 148 | |
| 149 TEST_F(SecurityKeyMessageWriterImplTest, WriteMessageWithPayload) { | |
| 150 WriteMessageToOutput("Super-test-payload!"); | |
| 151 } | |
| 152 | |
| 153 TEST_F(SecurityKeyMessageWriterImplTest, WriteMessageWithLargePayload) { | |
| 154 WriteMessageToOutput(std::string(kLargeMessageSizeBytes, 'Y')); | |
| 155 } | |
| 156 | |
| 157 TEST_F(SecurityKeyMessageWriterImplTest, WriteMultipleMessages) { | |
| 158 int total_messages_to_write = 10; | |
| 159 for (int i = 0; i < total_messages_to_write; i++) { | |
| 160 if (i % 2 == 0) { | |
| 161 ASSERT_TRUE(writer_->WriteMessage(SecurityKeyMessageType::CONNECT)); | |
| 162 } else { | |
| 163 ASSERT_TRUE(writer_->WriteMessage(SecurityKeyMessageType::REQUEST)); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 for (int i = 0; i < total_messages_to_write; i++) { | |
| 168 // Retrieve and verify the message header. | |
| 169 int length; | |
| 170 ASSERT_EQ(SecurityKeyMessage::kHeaderSizeBytes, | |
| 171 read_file_.ReadAtCurrentPos(reinterpret_cast<char*>(&length), 4)); | |
| 172 ASSERT_EQ(SecurityKeyMessage::kMessageTypeSizeBytes, length); | |
| 173 | |
| 174 // Retrieve and verify the message type. | |
| 175 std::string message_type(length, '\0'); | |
| 176 int bytes_read = | |
| 177 read_file_.ReadAtCurrentPos(string_as_array(&message_type), length); | |
| 178 ASSERT_EQ(length, bytes_read); | |
| 179 | |
| 180 SecurityKeyMessageType type = | |
| 181 SecurityKeyMessage::MessageTypeFromValue(message_type[0]); | |
| 182 if (i % 2 == 0) { | |
| 183 ASSERT_EQ(SecurityKeyMessageType::CONNECT, type); | |
| 184 } else { | |
| 185 ASSERT_EQ(SecurityKeyMessageType::REQUEST, type); | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 // Destroy the writer and verify the other end of the pipe is clean. | |
| 190 writer_.reset(); | |
| 191 char unused; | |
| 192 ASSERT_LE(read_file_.ReadAtCurrentPos(&unused, 1), 0); | |
| 193 } | |
| 194 | |
| 195 TEST_F(SecurityKeyMessageWriterImplTest, EnsureWriteFailsWhenPipeClosed) { | |
| 196 // Close the read end so that writing fails immediately. | |
| 197 read_file_.Close(); | |
| 198 | |
| 199 EXPECT_FALSE(writer_->WriteMessage(kTestMessageType)); | |
| 200 } | |
| 201 | |
| 202 } // namespace remoting | |
| OLD | NEW |