| 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 <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/memory/scoped_ptr.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 scoped_ptr<RemoteSecurityKeyMessageHandler> message_handler_; |
| 54 |
| 55 private: |
| 56 base::MessageLoopForIO message_loop_; |
| 57 scoped_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 scoped_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 scoped_ptr<FakeRemoteSecurityKeyMessageReader> reader( |
| 81 new FakeRemoteSecurityKeyMessageReader()); |
| 82 reader_weak_ptr_ = reader->AsWeakPtr(); |
| 83 |
| 84 scoped_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("1", writer_weak_ptr_->last_message_payload()); |
| 130 } |
| 131 |
| 132 TEST_F(RemoteSecurityKeyMessageHandlerTest, |
| 133 ProcessConnectMessage_SessionExists_WriteFails) { |
| 134 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); |
| 135 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(true); |
| 136 writer_weak_ptr_->set_write_request_succeeded(/*should_succeed=*/false); |
| 137 |
| 138 reader_weak_ptr_->message_callback().Run( |
| 139 SecurityKeyMessage::CreateMessageForTest( |
| 140 RemoteSecurityKeyMessageType::CONNECT, std::string())); |
| 141 WaitForOperationComplete(); |
| 142 |
| 143 ASSERT_FALSE(ipc_client_weak_ptr_.get()); |
| 144 ASSERT_FALSE(reader_weak_ptr_.get()); |
| 145 ASSERT_FALSE(writer_weak_ptr_.get()); |
| 146 } |
| 147 |
| 148 TEST_F(RemoteSecurityKeyMessageHandlerTest, |
| 149 ProcessConnectMessage_SessionExists_ConnectionAttemptFailure) { |
| 150 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); |
| 151 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(false); |
| 152 |
| 153 reader_weak_ptr_->message_callback().Run( |
| 154 SecurityKeyMessage::CreateMessageForTest( |
| 155 RemoteSecurityKeyMessageType::CONNECT, std::string())); |
| 156 WaitForOperationComplete(); |
| 157 |
| 158 ASSERT_EQ(RemoteSecurityKeyMessageType::CONNECT_ERROR, |
| 159 writer_weak_ptr_->last_message_type()); |
| 160 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); |
| 161 } |
| 162 |
| 163 TEST_F(RemoteSecurityKeyMessageHandlerTest, |
| 164 ProcessConnectMessage_NoSessionExists) { |
| 165 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(false); |
| 166 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(false); |
| 167 |
| 168 reader_weak_ptr_->message_callback().Run( |
| 169 SecurityKeyMessage::CreateMessageForTest( |
| 170 RemoteSecurityKeyMessageType::CONNECT, std::string())); |
| 171 WaitForOperationComplete(); |
| 172 |
| 173 ASSERT_EQ(RemoteSecurityKeyMessageType::CONNECT_RESPONSE, |
| 174 writer_weak_ptr_->last_message_type()); |
| 175 ASSERT_EQ("0", writer_weak_ptr_->last_message_payload()); |
| 176 } |
| 177 |
| 178 TEST_F(RemoteSecurityKeyMessageHandlerTest, |
| 179 ProcessConnectMessage_IncorrectPayload) { |
| 180 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); |
| 181 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(false); |
| 182 |
| 183 reader_weak_ptr_->message_callback().Run( |
| 184 SecurityKeyMessage::CreateMessageForTest( |
| 185 RemoteSecurityKeyMessageType::CONNECT, "Invalid request payload")); |
| 186 WaitForOperationComplete(); |
| 187 |
| 188 ASSERT_EQ(RemoteSecurityKeyMessageType::CONNECT_ERROR, |
| 189 writer_weak_ptr_->last_message_type()); |
| 190 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); |
| 191 } |
| 192 |
| 193 TEST_F(RemoteSecurityKeyMessageHandlerTest, |
| 194 ProcessRequestMessage_ValidPayload_IpcSendSuccess) { |
| 195 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); |
| 196 std::string response_payload("I AM A VALID RESPONSE PAYLOAD!"); |
| 197 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); |
| 198 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); |
| 199 |
| 200 reader_weak_ptr_->message_callback().Run( |
| 201 SecurityKeyMessage::CreateMessageForTest( |
| 202 RemoteSecurityKeyMessageType::REQUEST, request_payload)); |
| 203 WaitForOperationComplete(); |
| 204 |
| 205 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_RESPONSE, |
| 206 writer_weak_ptr_->last_message_type()); |
| 207 ASSERT_EQ(response_payload, writer_weak_ptr_->last_message_payload()); |
| 208 } |
| 209 |
| 210 TEST_F(RemoteSecurityKeyMessageHandlerTest, ProcessRequestMessage_WriteFails) { |
| 211 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); |
| 212 std::string response_payload("I AM A VALID RESPONSE PAYLOAD!"); |
| 213 |
| 214 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); |
| 215 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); |
| 216 writer_weak_ptr_->set_write_request_succeeded(/*should_succeed=*/false); |
| 217 |
| 218 reader_weak_ptr_->message_callback().Run( |
| 219 SecurityKeyMessage::CreateMessageForTest( |
| 220 RemoteSecurityKeyMessageType::REQUEST, request_payload)); |
| 221 WaitForOperationComplete(); |
| 222 |
| 223 ASSERT_FALSE(ipc_client_weak_ptr_.get()); |
| 224 ASSERT_FALSE(reader_weak_ptr_.get()); |
| 225 ASSERT_FALSE(writer_weak_ptr_.get()); |
| 226 } |
| 227 |
| 228 TEST_F(RemoteSecurityKeyMessageHandlerTest, |
| 229 ProcessRequestMessage_ValidPayload_IpcSendFailure) { |
| 230 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); |
| 231 ipc_client_weak_ptr_->set_send_security_request_should_succeed(false); |
| 232 |
| 233 reader_weak_ptr_->message_callback().Run( |
| 234 SecurityKeyMessage::CreateMessageForTest( |
| 235 RemoteSecurityKeyMessageType::REQUEST, request_payload)); |
| 236 WaitForOperationComplete(); |
| 237 |
| 238 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_ERROR, |
| 239 writer_weak_ptr_->last_message_type()); |
| 240 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); |
| 241 } |
| 242 |
| 243 TEST_F(RemoteSecurityKeyMessageHandlerTest, |
| 244 ProcessRequestMessage_ValidPayload_EmptyClientResponse) { |
| 245 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); |
| 246 std::string response_payload(""); |
| 247 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); |
| 248 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); |
| 249 |
| 250 reader_weak_ptr_->message_callback().Run( |
| 251 SecurityKeyMessage::CreateMessageForTest( |
| 252 RemoteSecurityKeyMessageType::REQUEST, request_payload)); |
| 253 WaitForOperationComplete(); |
| 254 |
| 255 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_ERROR, |
| 256 writer_weak_ptr_->last_message_type()); |
| 257 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); |
| 258 } |
| 259 |
| 260 TEST_F(RemoteSecurityKeyMessageHandlerTest, |
| 261 ProcessRequestMessage_ValidPayload_ClientResponseError) { |
| 262 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); |
| 263 std::string response_payload(kRemoteSecurityKeyConnectionError); |
| 264 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); |
| 265 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); |
| 266 |
| 267 reader_weak_ptr_->message_callback().Run( |
| 268 SecurityKeyMessage::CreateMessageForTest( |
| 269 RemoteSecurityKeyMessageType::REQUEST, request_payload)); |
| 270 WaitForOperationComplete(); |
| 271 |
| 272 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_ERROR, |
| 273 writer_weak_ptr_->last_message_type()); |
| 274 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); |
| 275 } |
| 276 |
| 277 TEST_F(RemoteSecurityKeyMessageHandlerTest, |
| 278 ProcessRequestMessage_InvalidPayload) { |
| 279 std::string invalid_payload(""); |
| 280 reader_weak_ptr_->message_callback().Run( |
| 281 SecurityKeyMessage::CreateMessageForTest( |
| 282 RemoteSecurityKeyMessageType::REQUEST, invalid_payload)); |
| 283 WaitForOperationComplete(); |
| 284 |
| 285 ASSERT_EQ(RemoteSecurityKeyMessageType::REQUEST_ERROR, |
| 286 writer_weak_ptr_->last_message_type()); |
| 287 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); |
| 288 } |
| 289 |
| 290 TEST_F(RemoteSecurityKeyMessageHandlerTest, ProcessUnknownMessage) { |
| 291 reader_weak_ptr_->message_callback().Run( |
| 292 SecurityKeyMessage::CreateMessageForTest( |
| 293 RemoteSecurityKeyMessageType::UNKNOWN_ERROR, std::string())); |
| 294 WaitForOperationComplete(); |
| 295 |
| 296 ASSERT_EQ(RemoteSecurityKeyMessageType::UNKNOWN_COMMAND, |
| 297 writer_weak_ptr_->last_message_type()); |
| 298 } |
| 299 |
| 300 } // namespace remoting |
| OLD | NEW |