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