| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/security_key/remote_security_key_message_reader.h" | 5 #include "remoting/host/security_key/remote_security_key_message_reader.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 class RemoteSecurityKeyMessageReaderTest : public testing::Test { | 27 class RemoteSecurityKeyMessageReaderTest : public testing::Test { |
| 28 public: | 28 public: |
| 29 RemoteSecurityKeyMessageReaderTest(); | 29 RemoteSecurityKeyMessageReaderTest(); |
| 30 ~RemoteSecurityKeyMessageReaderTest() override; | 30 ~RemoteSecurityKeyMessageReaderTest() override; |
| 31 | 31 |
| 32 // SecurityKeyMessageCallback passed to the Reader. Stores |message| so it can | 32 // SecurityKeyMessageCallback passed to the Reader. Stores |message| so it can |
| 33 // be verified by tests. | 33 // be verified by tests. |
| 34 void OnMessage(scoped_ptr<SecurityKeyMessage> message); | 34 void OnMessage(scoped_ptr<SecurityKeyMessage> message); |
| 35 | 35 |
| 36 // Used as a callback to signal completion. | |
| 37 void OperationComplete(); | |
| 38 | |
| 39 protected: | 36 protected: |
| 40 // testing::Test interface. | 37 // testing::Test interface. |
| 41 void SetUp() override; | 38 void SetUp() override; |
| 42 | 39 |
| 43 // Runs the MessageLoop until the reader has completed and called back. | 40 // Runs the MessageLoop until the reader has completed and called back. |
| 44 void Run(); | 41 void Run(); |
| 45 | 42 |
| 46 // Writes a message (header+code+body) to the write-end of the pipe. | 43 // Writes a message (header+code+body) to the write-end of the pipe. |
| 47 void WriteMessage(RemoteSecurityKeyMessageType message_type, | 44 void WriteMessage(RemoteSecurityKeyMessageType message_type, |
| 48 const std::string& message_payload); | 45 const std::string& message_payload); |
| 49 | 46 |
| 50 // Writes some data to the write-end of the pipe. | 47 // Writes some data to the write-end of the pipe. |
| 51 void WriteData(const char* data, int length); | 48 void WriteData(const char* data, int length); |
| 52 | 49 |
| 53 scoped_ptr<RemoteSecurityKeyMessageReader> reader_; | 50 scoped_ptr<RemoteSecurityKeyMessageReader> reader_; |
| 54 base::File read_file_; | 51 base::File read_file_; |
| 55 base::File write_file_; | 52 base::File write_file_; |
| 56 | 53 |
| 57 std::vector<scoped_ptr<SecurityKeyMessage>> messages_received_; | 54 std::vector<scoped_ptr<SecurityKeyMessage>> messages_received_; |
| 58 | 55 |
| 59 private: | 56 private: |
| 60 base::MessageLoopForIO message_loop_; | 57 base::MessageLoopForIO message_loop_; |
| 61 scoped_ptr<base::RunLoop> run_loop_; | 58 base::RunLoop run_loop_; |
| 62 | 59 |
| 63 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageReaderTest); | 60 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageReaderTest); |
| 64 }; | 61 }; |
| 65 | 62 |
| 66 RemoteSecurityKeyMessageReaderTest::RemoteSecurityKeyMessageReaderTest() | 63 RemoteSecurityKeyMessageReaderTest::RemoteSecurityKeyMessageReaderTest() {} |
| 67 : run_loop_(new base::RunLoop()) {} | |
| 68 | 64 |
| 69 RemoteSecurityKeyMessageReaderTest::~RemoteSecurityKeyMessageReaderTest() {} | 65 RemoteSecurityKeyMessageReaderTest::~RemoteSecurityKeyMessageReaderTest() {} |
| 70 | 66 |
| 71 void RemoteSecurityKeyMessageReaderTest::SetUp() { | 67 void RemoteSecurityKeyMessageReaderTest::SetUp() { |
| 72 ASSERT_TRUE(MakePipe(&read_file_, &write_file_)); | 68 ASSERT_TRUE(MakePipe(&read_file_, &write_file_)); |
| 73 reader_.reset(new RemoteSecurityKeyMessageReader(std::move(read_file_))); | 69 reader_.reset(new RemoteSecurityKeyMessageReader(std::move(read_file_))); |
| 74 | 70 |
| 75 // base::Unretained is safe since no further tasks can run after | 71 // base::Unretained is safe since no further tasks can run after |
| 76 // RunLoop::Run() returns. | 72 // RunLoop::Run() returns. |
| 77 reader_->Start( | 73 reader_->Start(base::Bind(&RemoteSecurityKeyMessageReaderTest::OnMessage, |
| 78 base::Bind(&RemoteSecurityKeyMessageReaderTest::OnMessage, | 74 base::Unretained(this)), |
| 79 base::Unretained(this)), | 75 run_loop_.QuitClosure()); |
| 80 base::Bind(&RemoteSecurityKeyMessageReaderTest::OperationComplete, | |
| 81 base::Unretained(this))); | |
| 82 } | 76 } |
| 83 | 77 |
| 84 void RemoteSecurityKeyMessageReaderTest::Run() { | 78 void RemoteSecurityKeyMessageReaderTest::Run() { |
| 85 run_loop_->Run(); | 79 // Close the write-end, so the reader doesn't block waiting for more data. |
| 86 run_loop_.reset(new base::RunLoop()); | 80 write_file_.Close(); |
| 81 run_loop_.Run(); |
| 87 } | 82 } |
| 88 | 83 |
| 89 void RemoteSecurityKeyMessageReaderTest::OnMessage( | 84 void RemoteSecurityKeyMessageReaderTest::OnMessage( |
| 90 scoped_ptr<SecurityKeyMessage> message) { | 85 scoped_ptr<SecurityKeyMessage> message) { |
| 91 messages_received_.push_back(std::move(message)); | 86 messages_received_.push_back(std::move(message)); |
| 92 OperationComplete(); | |
| 93 } | |
| 94 | |
| 95 void RemoteSecurityKeyMessageReaderTest::OperationComplete() { | |
| 96 run_loop_->Quit(); | |
| 97 } | 87 } |
| 98 | 88 |
| 99 void RemoteSecurityKeyMessageReaderTest::WriteMessage( | 89 void RemoteSecurityKeyMessageReaderTest::WriteMessage( |
| 100 RemoteSecurityKeyMessageType message_type, | 90 RemoteSecurityKeyMessageType message_type, |
| 101 const std::string& message_payload) { | 91 const std::string& message_payload) { |
| 102 uint32_t length = | 92 uint32_t length = |
| 103 SecurityKeyMessage::kMessageTypeSizeBytes + message_payload.size(); | 93 SecurityKeyMessage::kMessageTypeSizeBytes + message_payload.size(); |
| 104 WriteData(reinterpret_cast<char*>(&length), | 94 WriteData(reinterpret_cast<char*>(&length), |
| 105 SecurityKeyMessage::kHeaderSizeBytes); | 95 SecurityKeyMessage::kHeaderSizeBytes); |
| 106 WriteData(reinterpret_cast<char*>(&message_type), | 96 WriteData(reinterpret_cast<char*>(&message_type), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 SecurityKeyMessage::kMessageTypeSizeBytes, | 136 SecurityKeyMessage::kMessageTypeSizeBytes, |
| 147 'Y'); | 137 'Y'); |
| 148 WriteMessage(kTestMessageType, payload); | 138 WriteMessage(kTestMessageType, payload); |
| 149 Run(); | 139 Run(); |
| 150 ASSERT_EQ(1u, messages_received_.size()); | 140 ASSERT_EQ(1u, messages_received_.size()); |
| 151 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); | 141 ASSERT_EQ(kTestMessageType, messages_received_[0]->type()); |
| 152 ASSERT_EQ(payload, messages_received_[0]->payload()); | 142 ASSERT_EQ(payload, messages_received_[0]->payload()); |
| 153 } | 143 } |
| 154 | 144 |
| 155 TEST_F(RemoteSecurityKeyMessageReaderTest, EmptyFile) { | 145 TEST_F(RemoteSecurityKeyMessageReaderTest, EmptyFile) { |
| 156 write_file_.Close(); | |
| 157 Run(); | 146 Run(); |
| 158 ASSERT_EQ(0u, messages_received_.size()); | 147 ASSERT_EQ(0u, messages_received_.size()); |
| 159 } | 148 } |
| 160 | 149 |
| 161 TEST_F(RemoteSecurityKeyMessageReaderTest, InvalidMessageLength) { | 150 TEST_F(RemoteSecurityKeyMessageReaderTest, InvalidMessageLength) { |
| 162 uint32_t length = kMaxSecurityKeyMessageByteCount + 1; | 151 uint32_t length = kMaxSecurityKeyMessageByteCount + 1; |
| 163 ASSERT_FALSE(SecurityKeyMessage::IsValidMessageSize(length)); | 152 ASSERT_FALSE(SecurityKeyMessage::IsValidMessageSize(length)); |
| 164 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); | 153 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 165 Run(); | 154 Run(); |
| 166 ASSERT_EQ(0u, messages_received_.size()); | 155 ASSERT_EQ(0u, messages_received_.size()); |
| 167 } | 156 } |
| 168 | 157 |
| 169 TEST_F(RemoteSecurityKeyMessageReaderTest, ShortHeader) { | 158 TEST_F(RemoteSecurityKeyMessageReaderTest, ShortHeader) { |
| 170 // Write only 3 bytes - the message length header is supposed to be 4 bytes. | 159 // Write only 3 bytes - the message length header is supposed to be 4 bytes. |
| 171 WriteData("xxx", SecurityKeyMessage::kHeaderSizeBytes - 1); | 160 WriteData("xxx", SecurityKeyMessage::kHeaderSizeBytes - 1); |
| 172 write_file_.Close(); | |
| 173 Run(); | 161 Run(); |
| 174 ASSERT_EQ(0u, messages_received_.size()); | 162 ASSERT_EQ(0u, messages_received_.size()); |
| 175 } | 163 } |
| 176 | 164 |
| 177 TEST_F(RemoteSecurityKeyMessageReaderTest, ZeroLengthMessage) { | 165 TEST_F(RemoteSecurityKeyMessageReaderTest, ZeroLengthMessage) { |
| 178 uint32_t length = 0; | 166 uint32_t length = 0; |
| 179 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); | 167 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 180 Run(); | 168 Run(); |
| 181 ASSERT_EQ(0u, messages_received_.size()); | 169 ASSERT_EQ(0u, messages_received_.size()); |
| 182 } | 170 } |
| 183 | 171 |
| 184 TEST_F(RemoteSecurityKeyMessageReaderTest, MissingControlCode) { | 172 TEST_F(RemoteSecurityKeyMessageReaderTest, MissingControlCode) { |
| 185 uint32_t length = 1; | 173 uint32_t length = 1; |
| 186 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); | 174 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 187 write_file_.Close(); | |
| 188 Run(); | 175 Run(); |
| 189 ASSERT_EQ(0u, messages_received_.size()); | 176 ASSERT_EQ(0u, messages_received_.size()); |
| 190 } | 177 } |
| 191 | 178 |
| 192 TEST_F(RemoteSecurityKeyMessageReaderTest, MissingPayload) { | 179 TEST_F(RemoteSecurityKeyMessageReaderTest, MissingPayload) { |
| 193 uint32_t length = 2; | 180 uint32_t length = 2; |
| 194 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); | 181 WriteData(reinterpret_cast<char*>(&length), sizeof(length)); |
| 195 | 182 |
| 196 char test_control_code = static_cast<char>(kTestMessageType); | 183 char test_control_code = static_cast<char>(kTestMessageType); |
| 197 WriteData(&test_control_code, sizeof(test_control_code)); | 184 WriteData(&test_control_code, sizeof(test_control_code)); |
| 198 write_file_.Close(); | |
| 199 Run(); | 185 Run(); |
| 200 ASSERT_EQ(0u, messages_received_.size()); | 186 ASSERT_EQ(0u, messages_received_.size()); |
| 201 } | 187 } |
| 202 | 188 |
| 203 TEST_F(RemoteSecurityKeyMessageReaderTest, MultipleMessages) { | 189 TEST_F(RemoteSecurityKeyMessageReaderTest, MultipleMessages) { |
| 204 std::vector<std::string> payloads({"", "S", // Really short | 190 std::vector<std::string> payloads({"", "S", // Really short |
| 205 "", "Short", "", "Medium Length", "", | 191 "", "Short", "", "Medium Length", "", |
| 206 "Longer than medium, but not super long", | 192 "Longer than medium, but not super long", |
| 207 "", std::string(2048, 'Y'), ""}); | 193 "", std::string(2048, 'Y'), ""}); |
| 208 | 194 |
| 209 for (auto& payload : payloads) { | 195 for (auto& payload : payloads) { |
| 210 WriteMessage(kTestMessageType, payload); | 196 WriteMessage(kTestMessageType, payload); |
| 211 Run(); | |
| 212 } | 197 } |
| 213 | 198 |
| 199 Run(); |
| 214 ASSERT_EQ(payloads.size(), messages_received_.size()); | 200 ASSERT_EQ(payloads.size(), messages_received_.size()); |
| 215 | 201 |
| 216 for (size_t i = 0; i < payloads.size(); i++) { | 202 for (size_t i = 0; i < payloads.size(); i++) { |
| 217 ASSERT_EQ(kTestMessageType, messages_received_[i]->type()); | 203 ASSERT_EQ(kTestMessageType, messages_received_[i]->type()); |
| 218 ASSERT_EQ(payloads[i], messages_received_[i]->payload()); | 204 ASSERT_EQ(payloads[i], messages_received_[i]->payload()); |
| 219 } | 205 } |
| 220 } | 206 } |
| 221 | 207 |
| 222 } // namespace remoting | 208 } // namespace remoting |
| OLD | NEW |