| 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/security_key_auth_handler.h" | |
| 6 | |
| 7 #include <cstdint> | |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "base/run_loop.h" | |
| 16 #include "ipc/ipc_channel.h" | |
| 17 #include "ipc/ipc_listener.h" | |
| 18 #include "ipc/ipc_message.h" | |
| 19 #include "ipc/ipc_message_macros.h" | |
| 20 #include "remoting/host/host_mock_objects.h" | |
| 21 #include "remoting/host/security_key/fake_security_key_ipc_client.h" | |
| 22 #include "remoting/host/security_key/fake_security_key_ipc_server.h" | |
| 23 #include "remoting/host/security_key/security_key_ipc_constants.h" | |
| 24 #include "testing/gmock/include/gmock/gmock.h" | |
| 25 #include "testing/gtest/include/gtest/gtest.h" | |
| 26 | |
| 27 namespace { | |
| 28 const int kConnectionId1 = 1; | |
| 29 const int kConnectionId2 = 2; | |
| 30 } // namespace | |
| 31 | |
| 32 namespace remoting { | |
| 33 | |
| 34 class SecurityKeyAuthHandlerWinTest : public testing::Test { | |
| 35 public: | |
| 36 SecurityKeyAuthHandlerWinTest(); | |
| 37 ~SecurityKeyAuthHandlerWinTest() override; | |
| 38 | |
| 39 // Passed to the object used for testing to be called back to signal | |
| 40 // completion of an IPC channel state change or reception of an IPC message. | |
| 41 void OperationComplete(); | |
| 42 | |
| 43 protected: | |
| 44 // Waits until the current |run_loop_| instance is signaled, then resets it. | |
| 45 void WaitForOperationComplete(); | |
| 46 | |
| 47 // Used as a callback given to the object under test, expected to be called | |
| 48 // back when a security key request is received by it. | |
| 49 void SendMessageToClient(int connection_id, const std::string& data); | |
| 50 | |
| 51 // Creates a new security key connection on the object under test. | |
| 52 void CreateSecurityKeyConnection(const std::string& channel_name); | |
| 53 | |
| 54 // Sets |desktop_session_id_| to the id for the current Windows session. | |
| 55 void InitializeDesktopSessionId(); | |
| 56 | |
| 57 // Uses |fake_ipc_client| to connect to the initial IPC server channel, it | |
| 58 // then validates internal state of the object under test and closes the | |
| 59 // connection based on |close_connection|. | |
| 60 void EstablishInitialIpcConnection(FakeSecurityKeyIpcClient* fake_ipc_client, | |
| 61 int expected_connection_id, | |
| 62 const std::string& channel_name, | |
| 63 bool close_connection); | |
| 64 | |
| 65 // Sends a security key response message using |fake_ipc_server| and | |
| 66 // validates the state of the object under test. | |
| 67 void SendRequestToSecurityKeyAuthHandler( | |
| 68 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, | |
| 69 int connection_id, | |
| 70 const std::string& request_payload); | |
| 71 | |
| 72 // Sends a security key response message to |fake_ipc_server| and validates | |
| 73 // the state of the object under test. | |
| 74 void SendResponseViaSecurityKeyAuthHandler( | |
| 75 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, | |
| 76 int connection_id, | |
| 77 const std::string& response_payload); | |
| 78 | |
| 79 // Closes a security key session IPC channel and validates state. | |
| 80 void CloseSecurityKeySessionIpcChannel( | |
| 81 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, | |
| 82 int connection_id); | |
| 83 | |
| 84 // Returns a unique IPC channel name which prevents conflicts when running | |
| 85 // tests concurrently. | |
| 86 std::string GetUniqueTestChannelName(); | |
| 87 | |
| 88 // IPC tests require a valid MessageLoop to run. | |
| 89 base::MessageLoopForIO message_loop_; | |
| 90 | |
| 91 // Used to allow |message_loop_| to run during tests. The instance is reset | |
| 92 // after each stage of the tests has been completed. | |
| 93 std::unique_ptr<base::RunLoop> run_loop_; | |
| 94 | |
| 95 // The object under test. | |
| 96 std::unique_ptr<SecurityKeyAuthHandler> auth_handler_; | |
| 97 | |
| 98 // Set as the default factory to create SecurityKeyIpcServerFactory | |
| 99 // instances, this class will track each objects creation and allow the tests | |
| 100 // to access it and use it for driving tests and validating state. | |
| 101 FakeSecurityKeyIpcServerFactory ipc_server_factory_; | |
| 102 | |
| 103 // Used to validate the object under test uses the correct ID when | |
| 104 // communicating over the IPC channel. | |
| 105 int last_connection_id_received_ = -1; | |
| 106 | |
| 107 // Used to validate that IPC connections are only allowed from a specific | |
| 108 // Windows session. | |
| 109 DWORD desktop_session_id_ = UINT32_MAX; | |
| 110 | |
| 111 // Stores the contents of the last IPC message received for validation. | |
| 112 std::string last_message_received_; | |
| 113 | |
| 114 private: | |
| 115 testing::NiceMock<MockClientSessionDetails> mock_client_session_details_; | |
| 116 | |
| 117 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerWinTest); | |
| 118 }; | |
| 119 | |
| 120 SecurityKeyAuthHandlerWinTest::SecurityKeyAuthHandlerWinTest() | |
| 121 : run_loop_(new base::RunLoop()) { | |
| 122 auth_handler_ = remoting::SecurityKeyAuthHandler::Create( | |
| 123 &mock_client_session_details_, | |
| 124 base::Bind(&SecurityKeyAuthHandlerWinTest::SendMessageToClient, | |
| 125 base::Unretained(this))); | |
| 126 } | |
| 127 | |
| 128 SecurityKeyAuthHandlerWinTest::~SecurityKeyAuthHandlerWinTest() {} | |
| 129 | |
| 130 void SecurityKeyAuthHandlerWinTest::OperationComplete() { | |
| 131 run_loop_->Quit(); | |
| 132 } | |
| 133 | |
| 134 void SecurityKeyAuthHandlerWinTest::WaitForOperationComplete() { | |
| 135 run_loop_->Run(); | |
| 136 run_loop_.reset(new base::RunLoop()); | |
| 137 } | |
| 138 | |
| 139 void SecurityKeyAuthHandlerWinTest::SendMessageToClient( | |
| 140 int connection_id, | |
| 141 const std::string& data) { | |
| 142 last_connection_id_received_ = connection_id; | |
| 143 last_message_received_ = data; | |
| 144 OperationComplete(); | |
| 145 } | |
| 146 | |
| 147 void SecurityKeyAuthHandlerWinTest::CreateSecurityKeyConnection( | |
| 148 const std::string& channel_name) { | |
| 149 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | |
| 150 | |
| 151 remoting::SetSecurityKeyIpcChannelNameForTest(channel_name); | |
| 152 | |
| 153 // Create a new SecurityKey IPC Server connection. | |
| 154 auth_handler_->CreateSecurityKeyConnection(); | |
| 155 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(channel_name)); | |
| 156 | |
| 157 InitializeDesktopSessionId(); | |
| 158 } | |
| 159 | |
| 160 void SecurityKeyAuthHandlerWinTest::InitializeDesktopSessionId() { | |
| 161 ASSERT_TRUE( | |
| 162 ProcessIdToSessionId(GetCurrentProcessId(), &desktop_session_id_)); | |
| 163 | |
| 164 ON_CALL(mock_client_session_details_, desktop_session_id()) | |
| 165 .WillByDefault(testing::Return(desktop_session_id_)); | |
| 166 } | |
| 167 | |
| 168 void SecurityKeyAuthHandlerWinTest::EstablishInitialIpcConnection( | |
| 169 FakeSecurityKeyIpcClient* fake_ipc_client, | |
| 170 int expected_connection_id, | |
| 171 const std::string& channel_name, | |
| 172 bool close_connection) { | |
| 173 size_t expected_connection_count = | |
| 174 auth_handler_->GetActiveConnectionCountForTest() + 1; | |
| 175 | |
| 176 ASSERT_TRUE(fake_ipc_client->ConnectViaIpc(channel_name)); | |
| 177 // Client and Server will each signal us once when OnChannelConenect() is | |
| 178 // called so we wait on complete twice. The order in which each is signaled | |
| 179 // is not important. | |
| 180 WaitForOperationComplete(); | |
| 181 WaitForOperationComplete(); | |
| 182 | |
| 183 // Verify the connection details have been passed to the client. | |
| 184 std::string new_channel_name = fake_ipc_client->last_message_received(); | |
| 185 ASSERT_FALSE(new_channel_name.empty()); | |
| 186 | |
| 187 // Verify the internal state of the SecurityKeyAuthHandler is correct. | |
| 188 ASSERT_TRUE(auth_handler_->IsValidConnectionId(expected_connection_id)); | |
| 189 ASSERT_EQ(expected_connection_count, | |
| 190 auth_handler_->GetActiveConnectionCountForTest()); | |
| 191 | |
| 192 if (close_connection) { | |
| 193 fake_ipc_client->CloseIpcConnection(); | |
| 194 WaitForOperationComplete(); | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 void SecurityKeyAuthHandlerWinTest::SendRequestToSecurityKeyAuthHandler( | |
| 199 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, | |
| 200 int connection_id, | |
| 201 const std::string& request_payload) { | |
| 202 size_t expected_connection_count = | |
| 203 auth_handler_->GetActiveConnectionCountForTest(); | |
| 204 // Send a security key request using the fake IPC server. | |
| 205 fake_ipc_server->SendRequest(request_payload); | |
| 206 WaitForOperationComplete(); | |
| 207 | |
| 208 // Verify the FakeSecurityKeyIpcServer instance was not destroyed. | |
| 209 ASSERT_TRUE(fake_ipc_server.get()); | |
| 210 | |
| 211 // Verify the request was received. | |
| 212 ASSERT_EQ(connection_id, last_connection_id_received_); | |
| 213 ASSERT_EQ(request_payload, last_message_received_); | |
| 214 | |
| 215 // Verify the internal state of the SecurityKeyAuthHandler is still correct. | |
| 216 ASSERT_TRUE(auth_handler_->IsValidConnectionId(connection_id)); | |
| 217 ASSERT_EQ(expected_connection_count, | |
| 218 auth_handler_->GetActiveConnectionCountForTest()); | |
| 219 } | |
| 220 | |
| 221 void SecurityKeyAuthHandlerWinTest::SendResponseViaSecurityKeyAuthHandler( | |
| 222 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, | |
| 223 int connection_id, | |
| 224 const std::string& response_payload) { | |
| 225 size_t expected_connection_count = | |
| 226 auth_handler_->GetActiveConnectionCountForTest(); | |
| 227 | |
| 228 // Send a security key response using the new IPC channel. | |
| 229 auth_handler_->SendClientResponse(connection_id, response_payload); | |
| 230 WaitForOperationComplete(); | |
| 231 | |
| 232 // Verify the security key response was received. | |
| 233 ASSERT_EQ(response_payload, fake_ipc_server->last_message_received()); | |
| 234 | |
| 235 // Verify the internal state of the SecurityKeyAuthHandler is still correct. | |
| 236 ASSERT_TRUE(auth_handler_->IsValidConnectionId(connection_id)); | |
| 237 ASSERT_EQ(expected_connection_count, | |
| 238 auth_handler_->GetActiveConnectionCountForTest()); | |
| 239 } | |
| 240 | |
| 241 void SecurityKeyAuthHandlerWinTest::CloseSecurityKeySessionIpcChannel( | |
| 242 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, | |
| 243 int connection_id) { | |
| 244 size_t expected_connection_count = | |
| 245 auth_handler_->GetActiveConnectionCountForTest() - 1; | |
| 246 | |
| 247 fake_ipc_server->CloseChannel(); | |
| 248 | |
| 249 // Verify the internal state has been updated. | |
| 250 ASSERT_FALSE(auth_handler_->IsValidConnectionId(connection_id)); | |
| 251 ASSERT_EQ(expected_connection_count, | |
| 252 auth_handler_->GetActiveConnectionCountForTest()); | |
| 253 | |
| 254 // Verify the FakeSecurityKeyIpcServer instance was destroyed. | |
| 255 ASSERT_FALSE(fake_ipc_server.get()); | |
| 256 } | |
| 257 | |
| 258 std::string SecurityKeyAuthHandlerWinTest::GetUniqueTestChannelName() { | |
| 259 std::string channel_name("Uber_Awesome_Super_Mega_Test_Channel."); | |
| 260 channel_name.append(IPC::Channel::GenerateUniqueRandomChannelID()); | |
| 261 | |
| 262 return channel_name; | |
| 263 } | |
| 264 | |
| 265 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSingleSecurityKeyRequest) { | |
| 266 std::string channel_name(GetUniqueTestChannelName()); | |
| 267 CreateSecurityKeyConnection(channel_name); | |
| 268 | |
| 269 // Create a fake client and connect to the IPC server channel. | |
| 270 FakeSecurityKeyIpcClient fake_ipc_client( | |
| 271 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 272 base::Unretained(this))); | |
| 273 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
| 274 /*close_connection=*/true); | |
| 275 | |
| 276 // Connect to the private IPC server channel created for this client. | |
| 277 std::string new_channel_name = fake_ipc_client.last_message_received(); | |
| 278 | |
| 279 // Retrieve the IPC server instance created when the client connected. | |
| 280 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server = | |
| 281 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
| 282 ASSERT_TRUE(fake_ipc_server.get()); | |
| 283 ASSERT_EQ(new_channel_name, fake_ipc_server->channel_name()); | |
| 284 | |
| 285 fake_ipc_server->set_send_response_callback( | |
| 286 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 287 base::Unretained(this))); | |
| 288 | |
| 289 // Send a security key request using the fake IPC server. | |
| 290 SendRequestToSecurityKeyAuthHandler(fake_ipc_server, kConnectionId1, | |
| 291 "0123456789"); | |
| 292 | |
| 293 // Send a security key response using the new IPC channel. | |
| 294 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server, kConnectionId1, | |
| 295 "9876543210"); | |
| 296 | |
| 297 CloseSecurityKeySessionIpcChannel(fake_ipc_server, kConnectionId1); | |
| 298 } | |
| 299 | |
| 300 TEST_F(SecurityKeyAuthHandlerWinTest, HandleConcurrentSecurityKeyRequests) { | |
| 301 std::string channel_name(GetUniqueTestChannelName()); | |
| 302 CreateSecurityKeyConnection(channel_name); | |
| 303 | |
| 304 // Create fake clients and connect each to the IPC server channel. | |
| 305 FakeSecurityKeyIpcClient fake_ipc_client_1( | |
| 306 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 307 base::Unretained(this))); | |
| 308 FakeSecurityKeyIpcClient fake_ipc_client_2( | |
| 309 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 310 base::Unretained(this))); | |
| 311 | |
| 312 EstablishInitialIpcConnection(&fake_ipc_client_1, kConnectionId1, | |
| 313 channel_name, | |
| 314 /*close_connection=*/true); | |
| 315 EstablishInitialIpcConnection(&fake_ipc_client_2, kConnectionId2, | |
| 316 channel_name, | |
| 317 /*close_connection=*/true); | |
| 318 | |
| 319 // Verify the connection details have been passed to the client. | |
| 320 std::string channel_name_1 = fake_ipc_client_1.last_message_received(); | |
| 321 std::string channel_name_2 = fake_ipc_client_2.last_message_received(); | |
| 322 ASSERT_NE(channel_name_1, channel_name_2); | |
| 323 | |
| 324 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_1 = | |
| 325 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
| 326 ASSERT_TRUE(fake_ipc_server_1.get()); | |
| 327 ASSERT_EQ(channel_name_1, fake_ipc_server_1->channel_name()); | |
| 328 | |
| 329 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_2 = | |
| 330 ipc_server_factory_.GetIpcServerObject(kConnectionId2); | |
| 331 ASSERT_TRUE(fake_ipc_server_2.get()); | |
| 332 ASSERT_EQ(channel_name_2, fake_ipc_server_2->channel_name()); | |
| 333 | |
| 334 fake_ipc_server_1->set_send_response_callback( | |
| 335 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 336 base::Unretained(this))); | |
| 337 fake_ipc_server_2->set_send_response_callback( | |
| 338 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 339 base::Unretained(this))); | |
| 340 | |
| 341 // Connect and send a security key request using the first IPC channel. | |
| 342 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, | |
| 343 "aaaaaaaaaa"); | |
| 344 | |
| 345 // Send a security key request using the second IPC channel. | |
| 346 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, | |
| 347 "bbbbbbbbbb"); | |
| 348 | |
| 349 // Send a security key response using the first IPC channel. | |
| 350 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, | |
| 351 "cccccccccc"); | |
| 352 | |
| 353 // Send a security key response using the second IPC channel. | |
| 354 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, | |
| 355 "dddddddddd"); | |
| 356 | |
| 357 // Close the IPC channels. | |
| 358 CloseSecurityKeySessionIpcChannel(fake_ipc_server_1, kConnectionId1); | |
| 359 CloseSecurityKeySessionIpcChannel(fake_ipc_server_2, kConnectionId2); | |
| 360 } | |
| 361 | |
| 362 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSequentialSecurityKeyRequests) { | |
| 363 std::string channel_name(GetUniqueTestChannelName()); | |
| 364 CreateSecurityKeyConnection(channel_name); | |
| 365 | |
| 366 // Create fake clients to connect to the IPC server channel. | |
| 367 FakeSecurityKeyIpcClient fake_ipc_client_1( | |
| 368 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 369 base::Unretained(this))); | |
| 370 | |
| 371 EstablishInitialIpcConnection(&fake_ipc_client_1, kConnectionId1, | |
| 372 channel_name, | |
| 373 /*close_connection=*/true); | |
| 374 | |
| 375 // Verify the connection details have been passed to the client. | |
| 376 std::string channel_name_1 = fake_ipc_client_1.last_message_received(); | |
| 377 | |
| 378 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_1 = | |
| 379 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
| 380 ASSERT_TRUE(fake_ipc_server_1.get()); | |
| 381 ASSERT_EQ(channel_name_1, fake_ipc_server_1->channel_name()); | |
| 382 | |
| 383 fake_ipc_server_1->set_send_response_callback( | |
| 384 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 385 base::Unretained(this))); | |
| 386 | |
| 387 // Send a security key request using the first IPC channel. | |
| 388 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, | |
| 389 "aaaaaaaaaa"); | |
| 390 | |
| 391 // Send a security key response using the first IPC channel. | |
| 392 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, | |
| 393 "cccccccccc"); | |
| 394 | |
| 395 // Close the IPC channel. | |
| 396 CloseSecurityKeySessionIpcChannel(fake_ipc_server_1, kConnectionId1); | |
| 397 | |
| 398 // Now connect with a second client. | |
| 399 FakeSecurityKeyIpcClient fake_ipc_client_2( | |
| 400 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 401 base::Unretained(this))); | |
| 402 EstablishInitialIpcConnection(&fake_ipc_client_2, kConnectionId2, | |
| 403 channel_name, | |
| 404 /*close_connection=*/true); | |
| 405 | |
| 406 std::string channel_name_2 = fake_ipc_client_2.last_message_received(); | |
| 407 ASSERT_NE(channel_name_1, channel_name_2); | |
| 408 | |
| 409 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_2 = | |
| 410 ipc_server_factory_.GetIpcServerObject(kConnectionId2); | |
| 411 ASSERT_TRUE(fake_ipc_server_2.get()); | |
| 412 ASSERT_EQ(channel_name_2, fake_ipc_server_2->channel_name()); | |
| 413 | |
| 414 fake_ipc_server_2->set_send_response_callback( | |
| 415 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 416 base::Unretained(this))); | |
| 417 | |
| 418 // Send a security key request using the second IPC channel. | |
| 419 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, | |
| 420 "bbbbbbbbbb"); | |
| 421 | |
| 422 // Send a security key response using the second IPC channel. | |
| 423 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, | |
| 424 "dddddddddd"); | |
| 425 | |
| 426 // Close the IPC channel. | |
| 427 CloseSecurityKeySessionIpcChannel(fake_ipc_server_2, kConnectionId2); | |
| 428 } | |
| 429 | |
| 430 TEST_F(SecurityKeyAuthHandlerWinTest, | |
| 431 ClientNeverDisconnectsFromInitialIpcChannel) { | |
| 432 const int kLowConnectionTimeoutInMs = 25; | |
| 433 auth_handler_->SetRequestTimeoutForTest( | |
| 434 base::TimeDelta::FromMilliseconds(kLowConnectionTimeoutInMs)); | |
| 435 | |
| 436 std::string channel_name(GetUniqueTestChannelName()); | |
| 437 CreateSecurityKeyConnection(channel_name); | |
| 438 | |
| 439 // Create a fake client and connect to the IPC server channel. | |
| 440 FakeSecurityKeyIpcClient fake_ipc_client( | |
| 441 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 442 base::Unretained(this))); | |
| 443 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
| 444 /*close_connection=*/false); | |
| 445 | |
| 446 // Don't close the channel here, instead wait for the SecurityKeyAuthHandler | |
| 447 // to close the connection due to the timeout. | |
| 448 WaitForOperationComplete(); | |
| 449 | |
| 450 // Verify the connection that was set up still exists. | |
| 451 ASSERT_TRUE(auth_handler_->IsValidConnectionId(kConnectionId1)); | |
| 452 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); | |
| 453 | |
| 454 // Attempt to connect again after the error. | |
| 455 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | |
| 456 /*close_connection=*/true); | |
| 457 } | |
| 458 | |
| 459 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSecurityKeyRequestTimeout) { | |
| 460 std::string channel_name(GetUniqueTestChannelName()); | |
| 461 CreateSecurityKeyConnection(channel_name); | |
| 462 | |
| 463 // Create a fake client and connect to the IPC server channel. | |
| 464 FakeSecurityKeyIpcClient fake_ipc_client( | |
| 465 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 466 base::Unretained(this))); | |
| 467 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
| 468 /*close_connection=*/true); | |
| 469 | |
| 470 // Connect to the private IPC server channel created for this client. | |
| 471 std::string new_channel_name = fake_ipc_client.last_message_received(); | |
| 472 | |
| 473 // Retrieve the IPC server instance created when the client connected. | |
| 474 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server = | |
| 475 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
| 476 ASSERT_TRUE(fake_ipc_server.get()); | |
| 477 ASSERT_EQ(new_channel_name, fake_ipc_server->channel_name()); | |
| 478 | |
| 479 fake_ipc_server->set_send_response_callback( | |
| 480 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 481 base::Unretained(this))); | |
| 482 | |
| 483 // Simulate a timeout and verify the IPC server is cleaned up. | |
| 484 CloseSecurityKeySessionIpcChannel(fake_ipc_server, kConnectionId1); | |
| 485 | |
| 486 // Attempt to connect again after the error. | |
| 487 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | |
| 488 /*close_connection=*/true); | |
| 489 } | |
| 490 | |
| 491 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSecurityKeyErrorResponse) { | |
| 492 std::string channel_name(GetUniqueTestChannelName()); | |
| 493 CreateSecurityKeyConnection(channel_name); | |
| 494 | |
| 495 // Create a fake client and connect to the IPC server channel. | |
| 496 FakeSecurityKeyIpcClient fake_ipc_client( | |
| 497 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 498 base::Unretained(this))); | |
| 499 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
| 500 /*close_connection=*/true); | |
| 501 | |
| 502 // Connect to the private IPC server channel created for this client. | |
| 503 std::string new_channel_name = fake_ipc_client.last_message_received(); | |
| 504 | |
| 505 // Retrieve the IPC server instance created when the client connected. | |
| 506 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server = | |
| 507 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
| 508 ASSERT_TRUE(fake_ipc_server.get()); | |
| 509 ASSERT_EQ(new_channel_name, fake_ipc_server->channel_name()); | |
| 510 | |
| 511 fake_ipc_server->set_send_response_callback( | |
| 512 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 513 base::Unretained(this))); | |
| 514 | |
| 515 // Send a security key request using the fake IPC server. | |
| 516 SendRequestToSecurityKeyAuthHandler(fake_ipc_server, kConnectionId1, | |
| 517 "0123456789"); | |
| 518 | |
| 519 // Simulate a security key error from the client. | |
| 520 auth_handler_->SendErrorAndCloseConnection(kConnectionId1); | |
| 521 // Wait for the ipc server channel to be torn down. | |
| 522 WaitForOperationComplete(); | |
| 523 | |
| 524 // Verify the connection was cleaned up. | |
| 525 ASSERT_FALSE(fake_ipc_server.get()); | |
| 526 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); | |
| 527 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | |
| 528 | |
| 529 // Attempt to connect again after the error. | |
| 530 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | |
| 531 /*close_connection=*/true); | |
| 532 } | |
| 533 | |
| 534 TEST_F(SecurityKeyAuthHandlerWinTest, IpcConnectionFailsFromInvalidSession) { | |
| 535 std::string channel_name(GetUniqueTestChannelName()); | |
| 536 CreateSecurityKeyConnection(channel_name); | |
| 537 | |
| 538 // Set the current session id to a 'different' session. | |
| 539 desktop_session_id_ += 1; | |
| 540 | |
| 541 // Create a fake client and connect to the IPC server channel. | |
| 542 FakeSecurityKeyIpcClient fake_ipc_client( | |
| 543 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
| 544 base::Unretained(this))); | |
| 545 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | |
| 546 // Wait for the error callback to be signaled. | |
| 547 WaitForOperationComplete(); | |
| 548 | |
| 549 // Verify the connection was not set up. | |
| 550 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); | |
| 551 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | |
| 552 } | |
| 553 | |
| 554 } // namespace remoting | |
| OLD | NEW |