| 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/security_key_ipc_client.h" | 5 #include "remoting/host/security_key/security_key_ipc_client.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 class SecurityKeyIpcClientTest : public testing::Test { | 30 class SecurityKeyIpcClientTest : public testing::Test { |
| 31 public: | 31 public: |
| 32 SecurityKeyIpcClientTest(); | 32 SecurityKeyIpcClientTest(); |
| 33 ~SecurityKeyIpcClientTest() override; | 33 ~SecurityKeyIpcClientTest() override; |
| 34 | 34 |
| 35 // Passed to the object used for testing to be called back to signal | 35 // Passed to the object used for testing to be called back to signal |
| 36 // completion of an IPC channel state change or reception of an IPC message. | 36 // completion of an IPC channel state change or reception of an IPC message. |
| 37 void OperationComplete(bool failed); | 37 void OperationComplete(bool failed); |
| 38 | 38 |
| 39 // Callback used to signal when the IPC channel is ready for messages. |
| 40 void ConnectionStateHandler(bool established); |
| 41 |
| 42 // Callback used to drive the |fake_ipc_server_| connection behavior. |
| 43 void SendConnectionMessage(); |
| 44 |
| 39 // Used as a callback given to the object under test, expected to be called | 45 // Used as a callback given to the object under test, expected to be called |
| 40 // back when a security key request is received by it. | 46 // back when a security key request is received by it. |
| 41 void SendMessageToClient(int connection_id, const std::string& data); | 47 void SendMessageToClient(int connection_id, const std::string& data); |
| 42 | 48 |
| 43 // Used as a callback given to the object under test, expected to be called | 49 // Used as a callback given to the object under test, expected to be called |
| 44 // back when a security key response is sent. | 50 // back when a security key response is sent. |
| 45 void ClientMessageReceived(const std::string& response_payload); | 51 void ClientMessageReceived(const std::string& response_payload); |
| 46 | 52 |
| 47 protected: | 53 protected: |
| 48 // testing::Test interface. | 54 // testing::Test interface. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 78 | 84 |
| 79 // Used to send/receive security key IPC messages for testing. | 85 // Used to send/receive security key IPC messages for testing. |
| 80 FakeSecurityKeyIpcServer fake_ipc_server_; | 86 FakeSecurityKeyIpcServer fake_ipc_server_; |
| 81 | 87 |
| 82 // Stores the current session ID on supported platforms. | 88 // Stores the current session ID on supported platforms. |
| 83 uint32_t session_id_ = 0; | 89 uint32_t session_id_ = 0; |
| 84 | 90 |
| 85 // Tracks the success/failure of the last async operation. | 91 // Tracks the success/failure of the last async operation. |
| 86 bool operation_failed_ = false; | 92 bool operation_failed_ = false; |
| 87 | 93 |
| 94 // Tracks whether the IPC channel connection has been established. |
| 95 bool connection_established_ = false; |
| 96 |
| 97 // Used to drive invalid session behavior for testing the client. |
| 98 bool simulate_invalid_session_ = false; |
| 99 |
| 88 // Used to validate the object under test uses the correct ID when | 100 // Used to validate the object under test uses the correct ID when |
| 89 // communicating over the IPC channel. | 101 // communicating over the IPC channel. |
| 90 int last_connection_id_received_ = -1; | 102 int last_connection_id_received_ = -1; |
| 91 | 103 |
| 92 // Stores the contents of the last IPC message received for validation. | 104 // Stores the contents of the last IPC message received for validation. |
| 93 std::string last_message_received_; | 105 std::string last_message_received_; |
| 94 | 106 |
| 95 private: | 107 private: |
| 96 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcClientTest); | 108 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcClientTest); |
| 97 }; | 109 }; |
| 98 | 110 |
| 99 SecurityKeyIpcClientTest::SecurityKeyIpcClientTest() | 111 SecurityKeyIpcClientTest::SecurityKeyIpcClientTest() |
| 100 : ipc_support_(message_loop_.task_runner()), | 112 : ipc_support_(message_loop_.task_runner()), |
| 101 run_loop_(new base::RunLoop()), | 113 run_loop_(new base::RunLoop()), |
| 102 fake_ipc_server_( | 114 fake_ipc_server_( |
| 103 kTestConnectionId, | 115 kTestConnectionId, |
| 104 /*client_session_details=*/nullptr, | 116 /*client_session_details=*/nullptr, |
| 105 /*initial_connect_timeout=*/base::TimeDelta::FromMilliseconds(500), | 117 /*initial_connect_timeout=*/base::TimeDelta::FromMilliseconds(500), |
| 106 base::Bind(&SecurityKeyIpcClientTest::SendMessageToClient, | 118 base::Bind(&SecurityKeyIpcClientTest::SendMessageToClient, |
| 107 base::Unretained(this)), | 119 base::Unretained(this)), |
| 108 base::Bind(&base::DoNothing), | 120 base::Bind(&SecurityKeyIpcClientTest::SendConnectionMessage, |
| 121 base::Unretained(this)), |
| 109 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 122 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 110 base::Unretained(this), | 123 base::Unretained(this), |
| 111 /*failed=*/false)) {} | 124 /*failed=*/false)) {} |
| 112 | 125 |
| 113 SecurityKeyIpcClientTest::~SecurityKeyIpcClientTest() {} | 126 SecurityKeyIpcClientTest::~SecurityKeyIpcClientTest() {} |
| 114 | 127 |
| 115 void SecurityKeyIpcClientTest::SetUp() { | 128 void SecurityKeyIpcClientTest::SetUp() { |
| 116 #if defined(OS_WIN) | 129 #if defined(OS_WIN) |
| 117 DWORD session_id = 0; | 130 DWORD session_id = 0; |
| 118 // If we are on Windows, then we need to set the correct session ID or the | 131 // If we are on Windows, then we need to set the correct session ID or the |
| 119 // IPC connection will not be created successfully. | 132 // IPC connection will not be created successfully. |
| 120 ASSERT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), &session_id)); | 133 ASSERT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), &session_id)); |
| 121 session_id_ = session_id; | 134 session_id_ = session_id; |
| 122 security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest(session_id_); | 135 security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest(session_id_); |
| 123 #endif // defined(OS_WIN) | 136 #endif // defined(OS_WIN) |
| 124 } | 137 } |
| 125 | 138 |
| 126 void SecurityKeyIpcClientTest::OperationComplete(bool failed) { | 139 void SecurityKeyIpcClientTest::OperationComplete(bool failed) { |
| 127 operation_failed_ |= failed; | 140 operation_failed_ |= failed; |
| 128 run_loop_->Quit(); | 141 run_loop_->Quit(); |
| 129 } | 142 } |
| 130 | 143 |
| 144 void SecurityKeyIpcClientTest::ConnectionStateHandler(bool established) { |
| 145 connection_established_ = established; |
| 146 OperationComplete(/*failed=*/false); |
| 147 } |
| 148 |
| 149 void SecurityKeyIpcClientTest::SendConnectionMessage() { |
| 150 if (simulate_invalid_session_) { |
| 151 fake_ipc_server_.SendInvalidSessionMessage(); |
| 152 } else { |
| 153 fake_ipc_server_.SendConnectionReadyMessage(); |
| 154 } |
| 155 } |
| 156 |
| 131 void SecurityKeyIpcClientTest::WaitForOperationComplete() { | 157 void SecurityKeyIpcClientTest::WaitForOperationComplete() { |
| 132 run_loop_->Run(); | 158 run_loop_->Run(); |
| 133 run_loop_.reset(new base::RunLoop()); | 159 run_loop_.reset(new base::RunLoop()); |
| 134 } | 160 } |
| 135 | 161 |
| 136 void SecurityKeyIpcClientTest::SendMessageToClient(int connection_id, | 162 void SecurityKeyIpcClientTest::SendMessageToClient(int connection_id, |
| 137 const std::string& data) { | 163 const std::string& data) { |
| 138 last_connection_id_received_ = connection_id; | 164 last_connection_id_received_ = connection_id; |
| 139 last_message_received_ = data; | 165 last_message_received_ = data; |
| 140 OperationComplete(/*failed=*/false); | 166 OperationComplete(/*failed=*/false); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 159 security_key_ipc_client_.SetIpcChannelHandleForTest(channel_handle); | 185 security_key_ipc_client_.SetIpcChannelHandleForTest(channel_handle); |
| 160 ASSERT_TRUE(fake_ipc_server_.CreateChannel( | 186 ASSERT_TRUE(fake_ipc_server_.CreateChannel( |
| 161 channel_handle, | 187 channel_handle, |
| 162 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 188 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 163 | 189 |
| 164 ASSERT_TRUE(security_key_ipc_client_.CheckForSecurityKeyIpcServerChannel()); | 190 ASSERT_TRUE(security_key_ipc_client_.CheckForSecurityKeyIpcServerChannel()); |
| 165 | 191 |
| 166 // Establish the IPC channel so we can begin sending and receiving security | 192 // Establish the IPC channel so we can begin sending and receiving security |
| 167 // key messages. | 193 // key messages. |
| 168 security_key_ipc_client_.EstablishIpcConnection( | 194 security_key_ipc_client_.EstablishIpcConnection( |
| 169 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 195 base::Bind(&SecurityKeyIpcClientTest::ConnectionStateHandler, |
| 170 base::Unretained(this), /*failed=*/false), | 196 base::Unretained(this)), |
| 171 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 197 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 172 base::Unretained(this), /*failed=*/true)); | 198 base::Unretained(this), /*failed=*/true)); |
| 173 WaitForOperationComplete(); | 199 WaitForOperationComplete(); |
| 200 ASSERT_EQ(connection_established_, expect_success); |
| 174 ASSERT_NE(operation_failed_, expect_success); | 201 ASSERT_NE(operation_failed_, expect_success); |
| 175 } | 202 } |
| 176 | 203 |
| 177 void SecurityKeyIpcClientTest::SendRequestAndResponse( | 204 void SecurityKeyIpcClientTest::SendRequestAndResponse( |
| 178 const std::string& request_data, | 205 const std::string& request_data, |
| 179 const std::string& response_data) { | 206 const std::string& response_data) { |
| 180 ASSERT_TRUE(security_key_ipc_client_.SendSecurityKeyRequest( | 207 ASSERT_TRUE(security_key_ipc_client_.SendSecurityKeyRequest( |
| 181 request_data, base::Bind(&SecurityKeyIpcClientTest::ClientMessageReceived, | 208 request_data, base::Bind(&SecurityKeyIpcClientTest::ClientMessageReceived, |
| 182 base::Unretained(this)))); | 209 base::Unretained(this)))); |
| 183 WaitForOperationComplete(); | 210 WaitForOperationComplete(); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 base::Unretained(this)))); | 301 base::Unretained(this)))); |
| 275 } | 302 } |
| 276 | 303 |
| 277 TEST_F(SecurityKeyIpcClientTest, NonExistentIpcServerChannel) { | 304 TEST_F(SecurityKeyIpcClientTest, NonExistentIpcServerChannel) { |
| 278 security_key_ipc_client_.SetIpcChannelHandleForTest( | 305 security_key_ipc_client_.SetIpcChannelHandleForTest( |
| 279 mojo::edk::NamedPlatformHandle(kNonexistentIpcChannelName)); | 306 mojo::edk::NamedPlatformHandle(kNonexistentIpcChannelName)); |
| 280 | 307 |
| 281 // Attempt to establish the conection (should fail since the IPC channel does | 308 // Attempt to establish the conection (should fail since the IPC channel does |
| 282 // not exist). | 309 // not exist). |
| 283 security_key_ipc_client_.EstablishIpcConnection( | 310 security_key_ipc_client_.EstablishIpcConnection( |
| 284 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 311 base::Bind(&SecurityKeyIpcClientTest::ConnectionStateHandler, |
| 285 base::Unretained(this), /*failed=*/false), | 312 base::Unretained(this)), |
| 286 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 313 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 287 base::Unretained(this), /*failed=*/true)); | 314 base::Unretained(this), /*failed=*/true)); |
| 288 WaitForOperationComplete(); | 315 WaitForOperationComplete(); |
| 289 ASSERT_TRUE(operation_failed_); | 316 ASSERT_TRUE(operation_failed_); |
| 290 } | 317 } |
| 291 | 318 |
| 292 #if defined(OS_WIN) | 319 #if defined(OS_WIN) |
| 293 TEST_F(SecurityKeyIpcClientTest, SecurityKeyIpcServerRunningInWrongSession) { | 320 TEST_F(SecurityKeyIpcClientTest, SecurityKeyIpcServerRunningInWrongSession) { |
| 294 // Set the expected session Id to a different session than we are running in. | 321 // Set the expected session Id to a different session than we are running in. |
| 295 security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest(session_id_ + | 322 security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest(session_id_ + |
| 296 1); | 323 1); |
| 324 simulate_invalid_session_ = true; |
| 297 | 325 |
| 298 // Attempting to establish a connection should fail here since the IPC Server | 326 // Attempting to establish a connection should fail here since the IPC Server |
| 299 // is 'running' in a different session than expected. | 327 // is 'running' in a different session than expected. |
| 300 EstablishConnection(/*expect_success=*/false); | 328 EstablishConnection(/*expect_success=*/false); |
| 301 } | 329 } |
| 302 #endif // defined(OS_WIN) | 330 #endif // defined(OS_WIN) |
| 303 | 331 |
| 304 } // namespace remoting | 332 } // namespace remoting |
| OLD | NEW |