| 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_handler.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "remoting/host/security_key/fake_remote_security_key_ipc_client.h" | |
| 16 #include "remoting/host/security_key/fake_remote_security_key_message_reader.h" | |
| 17 #include "remoting/host/security_key/fake_remote_security_key_message_writer.h" | |
| 18 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | |
| 19 #include "remoting/host/security_key/security_key_message.h" | |
| 20 #include "remoting/host/setup/test_util.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 namespace remoting { | |
| 24 | |
| 25 class RemoteSecurityKeyMessageHandlerTest : public testing::Test { | |
| 26 public: | |
| 27 RemoteSecurityKeyMessageHandlerTest(); | |
| 28 ~RemoteSecurityKeyMessageHandlerTest() override; | |
| 29 | |
| 30 // Passed to the object used for testing to be called back to signal | |
| 31 // completion of an action. | |
| 32 void OperationComplete(); | |
| 33 | |
| 34 protected: | |
| 35 // testing::Test interface. | |
| 36 void SetUp() override; | |
| 37 | |
| 38 // Waits until the current |run_loop_| instance is signaled, then resets it. | |
| 39 void WaitForOperationComplete(); | |
| 40 | |
| 41 // Passed to |message_channel_| and called back when a message is received. | |
| 42 void OnSecurityKeyMessage(RemoteSecurityKeyMessageType message_type, | |
| 43 const std::string& message_payload); | |
| 44 | |
| 45 bool last_operation_failed_ = false; | |
| 46 RemoteSecurityKeyMessageType last_message_type_received_ = | |
| 47 RemoteSecurityKeyMessageType::INVALID; | |
| 48 std::string last_message_payload_received_; | |
| 49 | |
| 50 base::WeakPtr<FakeRemoteSecurityKeyIpcClient> ipc_client_weak_ptr_; | |
| 51 base::WeakPtr<FakeRemoteSecurityKeyMessageReader> reader_weak_ptr_; | |
| 52 base::WeakPtr<FakeRemoteSecurityKeyMessageWriter> writer_weak_ptr_; | |
| 53 std::unique_ptr<RemoteSecurityKeyMessageHandler> message_handler_; | |
| 54 | |
| 55 private: | |
| 56 base::MessageLoopForIO message_loop_; | |
| 57 std::unique_ptr<base::RunLoop> run_loop_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyMessageHandlerTest); | |
| 60 }; | |
| 61 | |
| 62 RemoteSecurityKeyMessageHandlerTest::RemoteSecurityKeyMessageHandlerTest() {} | |
| 63 | |
| 64 RemoteSecurityKeyMessageHandlerTest::~RemoteSecurityKeyMessageHandlerTest() {} | |
| 65 | |
| 66 void RemoteSecurityKeyMessageHandlerTest::OperationComplete() { | |
| 67 run_loop_->Quit(); | |
| 68 } | |
| 69 | |
| 70 void RemoteSecurityKeyMessageHandlerTest::SetUp() { | |
| 71 run_loop_.reset(new base::RunLoop()); | |
| 72 message_handler_.reset(new RemoteSecurityKeyMessageHandler()); | |
| 73 | |
| 74 std::unique_ptr<FakeRemoteSecurityKeyIpcClient> ipc_client( | |
| 75 new FakeRemoteSecurityKeyIpcClient( | |
| 76 base::Bind(&RemoteSecurityKeyMessageHandlerTest::OperationComplete, | |
| 77 base::Unretained(this)))); | |
| 78 ipc_client_weak_ptr_ = ipc_client->AsWeakPtr(); | |
| 79 | |
| 80 std::unique_ptr<FakeRemoteSecurityKeyMessageReader> reader( | |
| 81 new FakeRemoteSecurityKeyMessageReader()); | |
| 82 reader_weak_ptr_ = reader->AsWeakPtr(); | |
| 83 | |
| 84 std::unique_ptr<FakeRemoteSecurityKeyMessageWriter> writer( | |
| 85 new FakeRemoteSecurityKeyMessageWriter( | |
| 86 base::Bind(&RemoteSecurityKeyMessageHandlerTest::OperationComplete, | |
| 87 base::Unretained(this)))); | |
| 88 writer_weak_ptr_ = writer->AsWeakPtr(); | |
| 89 | |
| 90 message_handler_->SetRemoteSecurityKeyMessageReaderForTest(std::move(reader)); | |
| 91 | |
| 92 message_handler_->SetRemoteSecurityKeyMessageWriterForTest(std::move(writer)); | |
| 93 | |
| 94 base::File read_file; | |
| 95 base::File write_file; | |
| 96 ASSERT_TRUE(MakePipe(&read_file, &write_file)); | |
| 97 message_handler_->Start( | |
| 98 std::move(read_file), std::move(write_file), std::move(ipc_client), | |
| 99 base::Bind(&RemoteSecurityKeyMessageHandlerTest::OperationComplete, | |
| 100 base::Unretained(this))); | |
| 101 } | |
| 102 | |
| 103 void RemoteSecurityKeyMessageHandlerTest::WaitForOperationComplete() { | |
| 104 run_loop_->Run(); | |
| 105 run_loop_.reset(new base::RunLoop()); | |
| 106 } | |
| 107 | |
| 108 void RemoteSecurityKeyMessageHandlerTest::OnSecurityKeyMessage( | |
| 109 RemoteSecurityKeyMessageType message_type, | |
| 110 const std::string& message_payload) { | |
| 111 last_message_type_received_ = message_type; | |
| 112 last_message_payload_received_ = message_payload; | |
| 113 | |
| 114 OperationComplete(); | |
| 115 } | |
| 116 | |
| 117 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 118 ProcessConnectMessage_SessionExists_ConnectionAttemptSuccess) { | |
| 119 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); | |
| 120 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(true); | |
| 121 | |
| 122 reader_weak_ptr_->message_callback().Run( | |
| 123 SecurityKeyMessage::CreateMessageForTest( | |
| 124 RemoteSecurityKeyMessageType::CONNECT, std::string())); | |
| 125 WaitForOperationComplete(); | |
| 126 | |
| 127 ASSERT_EQ(RemoteSecurityKeyMessageType::CONNECT_RESPONSE, | |
| 128 writer_weak_ptr_->last_message_type()); | |
| 129 ASSERT_EQ(std::string(1, kConnectResponseActiveSession), | |
| 130 writer_weak_ptr_->last_message_payload()); | |
| 131 } | |
| 132 | |
| 133 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 134 ProcessConnectMessage_SessionExists_WriteFails) { | |
| 135 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); | |
| 136 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(true); | |
| 137 writer_weak_ptr_->set_write_request_succeeded(/*should_succeed=*/false); | |
| 138 | |
| 139 reader_weak_ptr_->message_callback().Run( | |
| 140 SecurityKeyMessage::CreateMessageForTest( | |
| 141 RemoteSecurityKeyMessageType::CONNECT, std::string())); | |
| 142 WaitForOperationComplete(); | |
| 143 | |
| 144 ASSERT_FALSE(ipc_client_weak_ptr_.get()); | |
| 145 ASSERT_FALSE(reader_weak_ptr_.get()); | |
| 146 ASSERT_FALSE(writer_weak_ptr_.get()); | |
| 147 } | |
| 148 | |
| 149 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 150 ProcessConnectMessage_SessionExists_ConnectionAttemptFailure) { | |
| 151 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); | |
| 152 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(false); | |
| 153 | |
| 154 reader_weak_ptr_->message_callback().Run( | |
| 155 SecurityKeyMessage::CreateMessageForTest( | |
| 156 RemoteSecurityKeyMessageType::CONNECT, std::string())); | |
| 157 WaitForOperationComplete(); | |
| 158 | |
| 159 ASSERT_EQ(RemoteSecurityKeyMessageType::CONNECT_ERROR, | |
| 160 writer_weak_ptr_->last_message_type()); | |
| 161 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
| 162 } | |
| 163 | |
| 164 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 165 ProcessConnectMessage_NoSessionExists) { | |
| 166 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(false); | |
| 167 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(false); | |
| 168 | |
| 169 reader_weak_ptr_->message_callback().Run( | |
| 170 SecurityKeyMessage::CreateMessageForTest( | |
| 171 RemoteSecurityKeyMessageType::CONNECT, std::string())); | |
| 172 WaitForOperationComplete(); | |
| 173 | |
| 174 ASSERT_EQ(RemoteSecurityKeyMessageType::CONNECT_RESPONSE, | |
| 175 writer_weak_ptr_->last_message_type()); | |
| 176 ASSERT_EQ(std::string(1, kConnectResponseNoSession), | |
| 177 writer_weak_ptr_->last_message_payload()); | |
| 178 } | |
| 179 | |
| 180 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 181 ProcessConnectMessage_IncorrectPayload) { | |
| 182 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); | |
| 183 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(false); | |
| 184 | |
| 185 reader_weak_ptr_->message_callback().Run( | |
| 186 SecurityKeyMessage::CreateMessageForTest( | |
| 187 RemoteSecurityKeyMessageType::CONNECT, "Invalid request payload")); | |
| 188 WaitForOperationComplete(); | |
| 189 | |
| 190 ASSERT_EQ(RemoteSecurityKeyMessageType::CONNECT_ERROR, | |
| 191 writer_weak_ptr_->last_message_type()); | |
| 192 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
| 193 } | |
| 194 | |
| 195 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 196 ProcessRequestMessage_ValidPayload_IpcSendSuccess) { | |
| 197 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
| 198 std::string response_payload("I AM A VALID RESPONSE PAYLOAD!"); | |
| 199 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); | |
| 200 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); | |
| 201 | |
| 202 reader_weak_ptr_->message_callback().Run( | |
| 203 SecurityKeyMessage::CreateMessageForTest( | |
| 204 RemoteSecurityKeyMessageType::REQUEST, request_payload)); | |
| 205 WaitForOperationComplete(); | |
| 206 | |
| 207 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_RESPONSE, | |
| 208 writer_weak_ptr_->last_message_type()); | |
| 209 ASSERT_EQ(response_payload, writer_weak_ptr_->last_message_payload()); | |
| 210 } | |
| 211 | |
| 212 TEST_F(RemoteSecurityKeyMessageHandlerTest, ProcessRequestMessage_WriteFails) { | |
| 213 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
| 214 std::string response_payload("I AM A VALID RESPONSE PAYLOAD!"); | |
| 215 | |
| 216 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); | |
| 217 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); | |
| 218 writer_weak_ptr_->set_write_request_succeeded(/*should_succeed=*/false); | |
| 219 | |
| 220 reader_weak_ptr_->message_callback().Run( | |
| 221 SecurityKeyMessage::CreateMessageForTest( | |
| 222 RemoteSecurityKeyMessageType::REQUEST, request_payload)); | |
| 223 WaitForOperationComplete(); | |
| 224 | |
| 225 ASSERT_FALSE(ipc_client_weak_ptr_.get()); | |
| 226 ASSERT_FALSE(reader_weak_ptr_.get()); | |
| 227 ASSERT_FALSE(writer_weak_ptr_.get()); | |
| 228 } | |
| 229 | |
| 230 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 231 ProcessRequestMessage_ValidPayload_IpcSendFailure) { | |
| 232 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
| 233 ipc_client_weak_ptr_->set_send_security_request_should_succeed(false); | |
| 234 | |
| 235 reader_weak_ptr_->message_callback().Run( | |
| 236 SecurityKeyMessage::CreateMessageForTest( | |
| 237 RemoteSecurityKeyMessageType::REQUEST, request_payload)); | |
| 238 WaitForOperationComplete(); | |
| 239 | |
| 240 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_ERROR, | |
| 241 writer_weak_ptr_->last_message_type()); | |
| 242 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
| 243 } | |
| 244 | |
| 245 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 246 ProcessRequestMessage_ValidPayload_EmptyClientResponse) { | |
| 247 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
| 248 std::string response_payload(""); | |
| 249 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); | |
| 250 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); | |
| 251 | |
| 252 reader_weak_ptr_->message_callback().Run( | |
| 253 SecurityKeyMessage::CreateMessageForTest( | |
| 254 RemoteSecurityKeyMessageType::REQUEST, request_payload)); | |
| 255 WaitForOperationComplete(); | |
| 256 | |
| 257 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_ERROR, | |
| 258 writer_weak_ptr_->last_message_type()); | |
| 259 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
| 260 } | |
| 261 | |
| 262 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 263 ProcessRequestMessage_ValidPayload_ClientResponseError) { | |
| 264 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
| 265 std::string response_payload(kRemoteSecurityKeyConnectionError); | |
| 266 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); | |
| 267 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); | |
| 268 | |
| 269 reader_weak_ptr_->message_callback().Run( | |
| 270 SecurityKeyMessage::CreateMessageForTest( | |
| 271 RemoteSecurityKeyMessageType::REQUEST, request_payload)); | |
| 272 WaitForOperationComplete(); | |
| 273 | |
| 274 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_ERROR, | |
| 275 writer_weak_ptr_->last_message_type()); | |
| 276 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
| 277 } | |
| 278 | |
| 279 TEST_F(RemoteSecurityKeyMessageHandlerTest, | |
| 280 ProcessRequestMessage_InvalidPayload) { | |
| 281 std::string invalid_payload(""); | |
| 282 reader_weak_ptr_->message_callback().Run( | |
| 283 SecurityKeyMessage::CreateMessageForTest( | |
| 284 RemoteSecurityKeyMessageType::REQUEST, invalid_payload)); | |
| 285 WaitForOperationComplete(); | |
| 286 | |
| 287 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_ERROR, | |
| 288 writer_weak_ptr_->last_message_type()); | |
| 289 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
| 290 } | |
| 291 | |
| 292 TEST_F(RemoteSecurityKeyMessageHandlerTest, ProcessUnknownMessage) { | |
| 293 reader_weak_ptr_->message_callback().Run( | |
| 294 SecurityKeyMessage::CreateMessageForTest( | |
| 295 RemoteSecurityKeyMessageType::UNKNOWN_ERROR, std::string())); | |
| 296 WaitForOperationComplete(); | |
| 297 | |
| 298 ASSERT_EQ(RemoteSecurityKeyMessageType::UNKNOWN_COMMAND, | |
| 299 writer_weak_ptr_->last_message_type()); | |
| 300 } | |
| 301 | |
| 302 } // namespace remoting | |
| OLD | NEW |