| 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" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "ipc/ipc_channel.h" | 14 #include "ipc/ipc_channel.h" |
| 15 #include "remoting/host/security_key/fake_ipc_security_key_auth_handler.h" | 15 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 16 #include "mojo/edk/test/scoped_ipc_support.h" |
| 16 #include "remoting/host/security_key/fake_security_key_ipc_server.h" | 17 #include "remoting/host/security_key/fake_security_key_ipc_server.h" |
| 17 #include "remoting/host/security_key/security_key_ipc_constants.h" | 18 #include "remoting/host/security_key/security_key_ipc_constants.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 const int kTestConnectionId = 1; | 22 const int kTestConnectionId = 1; |
| 22 const char kNonexistentIpcChannelName[] = "Nonexistent_IPC_Channel"; | 23 const char kNonexistentIpcChannelName[] = "Nonexistent_IPC_Channel"; |
| 23 const char kValidIpcChannelName[] = "SecurityKeyIpcClientTest"; | 24 const char kValidIpcChannelName[] = "SecurityKeyIpcClientTest"; |
| 24 const int kLargeMessageSizeBytes = 256 * 1024; | 25 const int kLargeMessageSizeBytes = 256 * 1024; |
| 25 } // namespace | 26 } // namespace |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 // a response from |fake_ipc_server_| and verifies the payloads for both. | 60 // a response from |fake_ipc_server_| and verifies the payloads for both. |
| 60 void SendRequestAndResponse(const std::string& request_data, | 61 void SendRequestAndResponse(const std::string& request_data, |
| 61 const std::string& response_data); | 62 const std::string& response_data); |
| 62 | 63 |
| 63 // Creates a unique IPC channel name to use for testing. | 64 // Creates a unique IPC channel name to use for testing. |
| 64 std::string GenerateUniqueTestChannelName(); | 65 std::string GenerateUniqueTestChannelName(); |
| 65 | 66 |
| 66 // IPC tests require a valid MessageLoop to run. | 67 // IPC tests require a valid MessageLoop to run. |
| 67 base::MessageLoopForIO message_loop_; | 68 base::MessageLoopForIO message_loop_; |
| 68 | 69 |
| 70 mojo::edk::test::ScopedIPCSupport ipc_support_; |
| 71 |
| 69 // Used to allow |message_loop_| to run during tests. The instance is reset | 72 // Used to allow |message_loop_| to run during tests. The instance is reset |
| 70 // after each stage of the tests has been completed. | 73 // after each stage of the tests has been completed. |
| 71 std::unique_ptr<base::RunLoop> run_loop_; | 74 std::unique_ptr<base::RunLoop> run_loop_; |
| 72 | 75 |
| 73 // The object under test. | 76 // The object under test. |
| 74 SecurityKeyIpcClient security_key_ipc_client_; | 77 SecurityKeyIpcClient security_key_ipc_client_; |
| 75 | 78 |
| 76 // Provides a connection details message to |security_key_ipc_client_| | |
| 77 // for testing. | |
| 78 FakeIpcSecurityKeyAuthHandler fake_security_key_auth_handler_; | |
| 79 | |
| 80 // Used to send/receive security key IPC messages for testing. | 79 // Used to send/receive security key IPC messages for testing. |
| 81 FakeSecurityKeyIpcServer fake_ipc_server_; | 80 FakeSecurityKeyIpcServer fake_ipc_server_; |
| 82 | 81 |
| 83 // Stores the current session ID on supported platforms. | 82 // Stores the current session ID on supported platforms. |
| 84 uint32_t session_id_ = 0; | 83 uint32_t session_id_ = 0; |
| 85 | 84 |
| 86 // Tracks the success/failure of the last async operation. | 85 // Tracks the success/failure of the last async operation. |
| 87 bool operation_failed_ = false; | 86 bool operation_failed_ = false; |
| 88 | 87 |
| 89 // Used to validate the object under test uses the correct ID when | 88 // Used to validate the object under test uses the correct ID when |
| 90 // communicating over the IPC channel. | 89 // communicating over the IPC channel. |
| 91 int last_connection_id_received_ = -1; | 90 int last_connection_id_received_ = -1; |
| 92 | 91 |
| 93 // Stores the contents of the last IPC message received for validation. | 92 // Stores the contents of the last IPC message received for validation. |
| 94 std::string last_message_received_; | 93 std::string last_message_received_; |
| 95 | 94 |
| 96 private: | 95 private: |
| 97 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcClientTest); | 96 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcClientTest); |
| 98 }; | 97 }; |
| 99 | 98 |
| 100 SecurityKeyIpcClientTest::SecurityKeyIpcClientTest() | 99 SecurityKeyIpcClientTest::SecurityKeyIpcClientTest() |
| 101 : run_loop_(new base::RunLoop()), | 100 : ipc_support_(message_loop_.task_runner()), |
| 101 run_loop_(new base::RunLoop()), |
| 102 fake_ipc_server_( | 102 fake_ipc_server_( |
| 103 kTestConnectionId, | 103 kTestConnectionId, |
| 104 /*client_session_details=*/nullptr, | 104 /*client_session_details=*/nullptr, |
| 105 /*initial_connect_timeout=*/base::TimeDelta::FromMilliseconds(500), | 105 /*initial_connect_timeout=*/base::TimeDelta::FromMilliseconds(500), |
| 106 base::Bind(&SecurityKeyIpcClientTest::SendMessageToClient, | 106 base::Bind(&SecurityKeyIpcClientTest::SendMessageToClient, |
| 107 base::Unretained(this)), | 107 base::Unretained(this)), |
| 108 base::Bind(&base::DoNothing), |
| 108 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 109 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 109 base::Unretained(this), | 110 base::Unretained(this), |
| 110 /*failed=*/false)) {} | 111 /*failed=*/false)) {} |
| 111 | 112 |
| 112 SecurityKeyIpcClientTest::~SecurityKeyIpcClientTest() {} | 113 SecurityKeyIpcClientTest::~SecurityKeyIpcClientTest() {} |
| 113 | 114 |
| 114 void SecurityKeyIpcClientTest::SetUp() { | 115 void SecurityKeyIpcClientTest::SetUp() { |
| 115 #if defined(OS_WIN) | 116 #if defined(OS_WIN) |
| 116 DWORD session_id = 0; | 117 DWORD session_id = 0; |
| 117 // If we are on Windows, then we need to set the correct session ID or the | 118 // If we are on Windows, then we need to set the correct session ID or the |
| (...skipping 28 matching lines...) Expand all Loading... |
| 146 } | 147 } |
| 147 | 148 |
| 148 std::string SecurityKeyIpcClientTest::GenerateUniqueTestChannelName() { | 149 std::string SecurityKeyIpcClientTest::GenerateUniqueTestChannelName() { |
| 149 return GetChannelNamePathPrefixForTest() + kValidIpcChannelName + | 150 return GetChannelNamePathPrefixForTest() + kValidIpcChannelName + |
| 150 IPC::Channel::GenerateUniqueRandomChannelID(); | 151 IPC::Channel::GenerateUniqueRandomChannelID(); |
| 151 } | 152 } |
| 152 | 153 |
| 153 void SecurityKeyIpcClientTest::EstablishConnection(bool expect_success) { | 154 void SecurityKeyIpcClientTest::EstablishConnection(bool expect_success) { |
| 154 // Start up the security key forwarding session IPC channel first, that way | 155 // Start up the security key forwarding session IPC channel first, that way |
| 155 // we can provide the channel using the fake SecurityKeyAuthHandler later on. | 156 // we can provide the channel using the fake SecurityKeyAuthHandler later on. |
| 156 std::string ipc_session_channel_name = GenerateUniqueTestChannelName(); | 157 mojo::edk::NamedPlatformHandle channel_handle( |
| 158 GenerateUniqueTestChannelName()); |
| 159 security_key_ipc_client_.SetIpcChannelHandleForTest(channel_handle); |
| 157 ASSERT_TRUE(fake_ipc_server_.CreateChannel( | 160 ASSERT_TRUE(fake_ipc_server_.CreateChannel( |
| 158 ipc_session_channel_name, | 161 channel_handle, |
| 159 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 162 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 160 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(ipc_session_channel_name)); | |
| 161 fake_security_key_auth_handler_.set_ipc_security_key_channel_name( | |
| 162 ipc_session_channel_name); | |
| 163 | 163 |
| 164 // Set up the channel name for the initial IPC channel. | 164 ASSERT_TRUE(security_key_ipc_client_.CheckForSecurityKeyIpcServerChannel()); |
| 165 std::string ipc_server_channel_name = GenerateUniqueTestChannelName(); | |
| 166 fake_security_key_auth_handler_.set_ipc_server_channel_name( | |
| 167 ipc_server_channel_name); | |
| 168 security_key_ipc_client_.SetInitialIpcChannelNameForTest( | |
| 169 ipc_server_channel_name); | |
| 170 | |
| 171 // Create the initial IPC channel and verify it was set up correctly. | |
| 172 ASSERT_FALSE(security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
| 173 fake_security_key_auth_handler_.CreateSecurityKeyConnection(); | |
| 174 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(ipc_server_channel_name)); | |
| 175 ASSERT_TRUE(security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
| 176 | 165 |
| 177 // Establish the IPC channel so we can begin sending and receiving security | 166 // Establish the IPC channel so we can begin sending and receiving security |
| 178 // key messages. | 167 // key messages. |
| 179 security_key_ipc_client_.EstablishIpcConnection( | 168 security_key_ipc_client_.EstablishIpcConnection( |
| 180 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 169 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 181 base::Unretained(this), /*failed=*/false), | 170 base::Unretained(this), /*failed=*/false), |
| 182 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 171 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 183 base::Unretained(this), /*failed=*/true)); | 172 base::Unretained(this), /*failed=*/true)); |
| 184 WaitForOperationComplete(); | 173 WaitForOperationComplete(); |
| 185 ASSERT_NE(operation_failed_, expect_success); | 174 ASSERT_NE(operation_failed_, expect_success); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 } | 267 } |
| 279 | 268 |
| 280 TEST_F(SecurityKeyIpcClientTest, SendRequestBeforeEstablishingConnection) { | 269 TEST_F(SecurityKeyIpcClientTest, SendRequestBeforeEstablishingConnection) { |
| 281 // Sending a request will fail since the IPC connection has not been | 270 // Sending a request will fail since the IPC connection has not been |
| 282 // established. | 271 // established. |
| 283 ASSERT_FALSE(security_key_ipc_client_.SendSecurityKeyRequest( | 272 ASSERT_FALSE(security_key_ipc_client_.SendSecurityKeyRequest( |
| 284 "Too soon!!", base::Bind(&SecurityKeyIpcClientTest::ClientMessageReceived, | 273 "Too soon!!", base::Bind(&SecurityKeyIpcClientTest::ClientMessageReceived, |
| 285 base::Unretained(this)))); | 274 base::Unretained(this)))); |
| 286 } | 275 } |
| 287 | 276 |
| 288 TEST_F(SecurityKeyIpcClientTest, NonExistentMainIpcServerChannel) { | 277 TEST_F(SecurityKeyIpcClientTest, NonExistentIpcServerChannel) { |
| 289 std::string ipc_server_channel_name(kNonexistentIpcChannelName); | 278 security_key_ipc_client_.SetIpcChannelHandleForTest( |
| 290 security_key_ipc_client_.SetInitialIpcChannelNameForTest( | 279 mojo::edk::NamedPlatformHandle(kNonexistentIpcChannelName)); |
| 291 ipc_server_channel_name); | |
| 292 | 280 |
| 293 // Attempt to establish the conection (should fail since the IPC channel does | 281 // Attempt to establish the conection (should fail since the IPC channel does |
| 294 // not exist). | 282 // not exist). |
| 295 security_key_ipc_client_.EstablishIpcConnection( | |
| 296 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | |
| 297 base::Unretained(this), /*failed=*/false), | |
| 298 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | |
| 299 base::Unretained(this), /*failed=*/true)); | |
| 300 WaitForOperationComplete(); | |
| 301 ASSERT_TRUE(operation_failed_); | |
| 302 } | |
| 303 | |
| 304 TEST_F(SecurityKeyIpcClientTest, NonExistentIpcSessionChannel) { | |
| 305 fake_security_key_auth_handler_.set_ipc_security_key_channel_name( | |
| 306 kNonexistentIpcChannelName); | |
| 307 | |
| 308 // Set up the channel name for the initial IPC channel. | |
| 309 std::string ipc_server_channel_name = GenerateUniqueTestChannelName(); | |
| 310 fake_security_key_auth_handler_.set_ipc_server_channel_name( | |
| 311 ipc_server_channel_name); | |
| 312 security_key_ipc_client_.SetInitialIpcChannelNameForTest( | |
| 313 ipc_server_channel_name); | |
| 314 | |
| 315 // Create the initial IPC channel and verify it was set up correctly. | |
| 316 ASSERT_FALSE(security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
| 317 fake_security_key_auth_handler_.CreateSecurityKeyConnection(); | |
| 318 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(ipc_server_channel_name)); | |
| 319 ASSERT_TRUE(security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
| 320 | |
| 321 // Attempt to establish the conection (should fail since the IPC channel does | |
| 322 // not exist). | |
| 323 security_key_ipc_client_.EstablishIpcConnection( | 283 security_key_ipc_client_.EstablishIpcConnection( |
| 324 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 284 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 325 base::Unretained(this), /*failed=*/false), | 285 base::Unretained(this), /*failed=*/false), |
| 326 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 286 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 327 base::Unretained(this), /*failed=*/true)); | 287 base::Unretained(this), /*failed=*/true)); |
| 328 WaitForOperationComplete(); | 288 WaitForOperationComplete(); |
| 329 ASSERT_TRUE(operation_failed_); | 289 ASSERT_TRUE(operation_failed_); |
| 330 } | 290 } |
| 331 | 291 |
| 332 #if defined(OS_WIN) | 292 #if defined(OS_WIN) |
| 333 TEST_F(SecurityKeyIpcClientTest, SecurityKeyIpcServerRunningInWrongSession) { | 293 TEST_F(SecurityKeyIpcClientTest, SecurityKeyIpcServerRunningInWrongSession) { |
| 334 // Set the expected session Id to a different session than we are running in. | 294 // Set the expected session Id to a different session than we are running in. |
| 335 security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest(session_id_ + | 295 security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest(session_id_ + |
| 336 1); | 296 1); |
| 337 | 297 |
| 338 // Attempting to establish a connection should fail here since the IPC Server | 298 // Attempting to establish a connection should fail here since the IPC Server |
| 339 // is 'running' in a different session than expected. | 299 // is 'running' in a different session than expected. |
| 340 EstablishConnection(/*expect_success=*/false); | 300 EstablishConnection(/*expect_success=*/false); |
| 341 } | 301 } |
| 342 #endif // defined(OS_WIN) | 302 #endif // defined(OS_WIN) |
| 343 | 303 |
| 344 } // namespace remoting | 304 } // namespace remoting |
| OLD | NEW |