OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/security_key/remote_security_key_ipc_server.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" |
| 16 #include "ipc/ipc_channel.h" |
| 17 #include "remoting/host/security_key/fake_remote_security_key_ipc_client.h" |
| 18 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace { |
| 22 const int kTestConnectionId = 42; |
| 23 const int kInitialConnectTimeoutMs = 250; |
| 24 const int kConnectionTimeoutErrorDeltaMs = 100; |
| 25 const int kLargeMessageSizeBytes = 256 * 1024; |
| 26 } // namespace |
| 27 |
| 28 namespace remoting { |
| 29 |
| 30 class RemoteSecurityKeyIpcServerTest : public testing::Test { |
| 31 public: |
| 32 RemoteSecurityKeyIpcServerTest(); |
| 33 ~RemoteSecurityKeyIpcServerTest() override; |
| 34 |
| 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. |
| 37 void OperationComplete(); |
| 38 |
| 39 // Used as a callback to signal receipt of a security key request message. |
| 40 void SendRequestToClient(int connection_id, const std::string& data); |
| 41 |
| 42 protected: |
| 43 // Returns a unique IPC channel name which prevents conflicts when running |
| 44 // tests concurrently. |
| 45 std::string GetUniqueTestChannelName(); |
| 46 |
| 47 // Waits until the current |run_loop_| instance is signaled, then resets it. |
| 48 void WaitForOperationComplete(); |
| 49 |
| 50 // IPC tests require a valid MessageLoop to run. |
| 51 base::MessageLoopForIO message_loop_; |
| 52 |
| 53 // Used to allow |message_loop_| to run during tests. The instance is reset |
| 54 // after each stage of the tests has been completed. |
| 55 std::unique_ptr<base::RunLoop> run_loop_; |
| 56 |
| 57 // The object under test. |
| 58 std::unique_ptr<RemoteSecurityKeyIpcServer> remote_security_key_ipc_server_; |
| 59 |
| 60 // Used to validate the object under test uses the correct ID when |
| 61 // communicating over the IPC channel. |
| 62 int last_connection_id_received_ = -1; |
| 63 |
| 64 // Stores the contents of the last IPC message received for validation. |
| 65 std::string last_message_received_; |
| 66 |
| 67 private: |
| 68 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyIpcServerTest); |
| 69 }; |
| 70 |
| 71 RemoteSecurityKeyIpcServerTest::RemoteSecurityKeyIpcServerTest() |
| 72 : run_loop_(new base::RunLoop()) { |
| 73 uint32_t peer_session_id = UINT32_MAX; |
| 74 #if defined(OS_WIN) |
| 75 EXPECT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), |
| 76 reinterpret_cast<DWORD*>(&peer_session_id))); |
| 77 #endif // defined(OS_WIN) |
| 78 |
| 79 remote_security_key_ipc_server_ = |
| 80 remoting::RemoteSecurityKeyIpcServer::Create( |
| 81 kTestConnectionId, peer_session_id, |
| 82 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs), |
| 83 base::Bind(&RemoteSecurityKeyIpcServerTest::SendRequestToClient, |
| 84 base::Unretained(this)), |
| 85 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, |
| 86 base::Unretained(this))); |
| 87 } |
| 88 |
| 89 RemoteSecurityKeyIpcServerTest::~RemoteSecurityKeyIpcServerTest() {} |
| 90 |
| 91 void RemoteSecurityKeyIpcServerTest::OperationComplete() { |
| 92 run_loop_->Quit(); |
| 93 } |
| 94 |
| 95 void RemoteSecurityKeyIpcServerTest::WaitForOperationComplete() { |
| 96 run_loop_->Run(); |
| 97 run_loop_.reset(new base::RunLoop()); |
| 98 } |
| 99 |
| 100 void RemoteSecurityKeyIpcServerTest::SendRequestToClient( |
| 101 int connection_id, |
| 102 const std::string& data) { |
| 103 last_connection_id_received_ = connection_id; |
| 104 last_message_received_ = data; |
| 105 OperationComplete(); |
| 106 } |
| 107 |
| 108 std::string RemoteSecurityKeyIpcServerTest::GetUniqueTestChannelName() { |
| 109 return GetChannelNamePathPrefixForTest() + "Super_Awesome_Test_Channel." + |
| 110 IPC::Channel::GenerateUniqueRandomChannelID(); |
| 111 } |
| 112 |
| 113 TEST_F(RemoteSecurityKeyIpcServerTest, HandleSingleGnubbyRequest) { |
| 114 std::string channel_name(GetUniqueTestChannelName()); |
| 115 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( |
| 116 channel_name, |
| 117 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 118 |
| 119 // Create a fake client and connect to the IPC server channel. |
| 120 FakeRemoteSecurityKeyIpcClient fake_ipc_client( |
| 121 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, |
| 122 base::Unretained(this))); |
| 123 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 124 WaitForOperationComplete(); |
| 125 |
| 126 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 127 |
| 128 // Send a request from the IPC client to the IPC server. |
| 129 std::string request_data("Blergh!"); |
| 130 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 131 WaitForOperationComplete(); |
| 132 |
| 133 // Verify the request was received. |
| 134 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 135 ASSERT_EQ(request_data, last_message_received_); |
| 136 |
| 137 // Send a response from the IPC server to the IPC client. |
| 138 std::string response_data("Blargh!"); |
| 139 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data)); |
| 140 WaitForOperationComplete(); |
| 141 |
| 142 // Verify the request was received. |
| 143 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); |
| 144 |
| 145 // Typically the client will be the one to close the connection. |
| 146 fake_ipc_client.CloseIpcConnection(); |
| 147 } |
| 148 |
| 149 TEST_F(RemoteSecurityKeyIpcServerTest, HandleLargeGnubbyRequest) { |
| 150 std::string channel_name(GetUniqueTestChannelName()); |
| 151 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( |
| 152 channel_name, |
| 153 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 154 |
| 155 // Create a fake client and connect to the IPC server channel. |
| 156 FakeRemoteSecurityKeyIpcClient fake_ipc_client( |
| 157 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, |
| 158 base::Unretained(this))); |
| 159 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 160 WaitForOperationComplete(); |
| 161 |
| 162 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 163 |
| 164 // Send a request from the IPC client to the IPC server. |
| 165 std::string request_data(kLargeMessageSizeBytes, 'Y'); |
| 166 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 167 WaitForOperationComplete(); |
| 168 |
| 169 // Verify the request was received. |
| 170 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 171 ASSERT_EQ(request_data, last_message_received_); |
| 172 |
| 173 // Send a response from the IPC server to the IPC client. |
| 174 std::string response_data(kLargeMessageSizeBytes, 'Z'); |
| 175 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data)); |
| 176 WaitForOperationComplete(); |
| 177 |
| 178 // Verify the request was received. |
| 179 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); |
| 180 |
| 181 // Typically the client will be the one to close the connection. |
| 182 fake_ipc_client.CloseIpcConnection(); |
| 183 } |
| 184 |
| 185 TEST_F(RemoteSecurityKeyIpcServerTest, HandleReallyLargeGnubbyRequest) { |
| 186 std::string channel_name(GetUniqueTestChannelName()); |
| 187 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( |
| 188 channel_name, |
| 189 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 190 |
| 191 // Create a fake client and connect to the IPC server channel. |
| 192 FakeRemoteSecurityKeyIpcClient fake_ipc_client( |
| 193 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, |
| 194 base::Unretained(this))); |
| 195 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 196 WaitForOperationComplete(); |
| 197 |
| 198 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 199 |
| 200 // Send a request from the IPC client to the IPC server. |
| 201 std::string request_data(kLargeMessageSizeBytes * 2, 'Y'); |
| 202 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 203 WaitForOperationComplete(); |
| 204 |
| 205 // Verify the request was received. |
| 206 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 207 ASSERT_EQ(request_data, last_message_received_); |
| 208 |
| 209 // Send a response from the IPC server to the IPC client. |
| 210 std::string response_data(kLargeMessageSizeBytes * 2, 'Z'); |
| 211 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data)); |
| 212 WaitForOperationComplete(); |
| 213 |
| 214 // Verify the request was received. |
| 215 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); |
| 216 |
| 217 // Typically the client will be the one to close the connection. |
| 218 fake_ipc_client.CloseIpcConnection(); |
| 219 } |
| 220 |
| 221 TEST_F(RemoteSecurityKeyIpcServerTest, HandleMultipleGnubbyRequests) { |
| 222 std::string channel_name(GetUniqueTestChannelName()); |
| 223 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( |
| 224 channel_name, |
| 225 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 226 |
| 227 // Create a fake client and connect to the IPC server channel. |
| 228 FakeRemoteSecurityKeyIpcClient fake_ipc_client( |
| 229 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, |
| 230 base::Unretained(this))); |
| 231 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 232 WaitForOperationComplete(); |
| 233 |
| 234 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 235 |
| 236 // Send a request from the IPC client to the IPC server. |
| 237 std::string request_data_1("Blergh!"); |
| 238 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data_1); |
| 239 WaitForOperationComplete(); |
| 240 |
| 241 // Verify the request was received. |
| 242 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 243 ASSERT_EQ(request_data_1, last_message_received_); |
| 244 |
| 245 // Send a response from the IPC server to the IPC client. |
| 246 std::string response_data_1("Blargh!"); |
| 247 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data_1)); |
| 248 WaitForOperationComplete(); |
| 249 |
| 250 // Verify the response was received. |
| 251 ASSERT_EQ(response_data_1, fake_ipc_client.last_message_received()); |
| 252 |
| 253 // Send a request from the IPC client to the IPC server. |
| 254 std::string request_data_2("Bleh!"); |
| 255 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data_2); |
| 256 WaitForOperationComplete(); |
| 257 |
| 258 // Verify the request was received. |
| 259 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 260 ASSERT_EQ(request_data_2, last_message_received_); |
| 261 |
| 262 // Send a response from the IPC server to the IPC client. |
| 263 std::string response_data_2("Meh!"); |
| 264 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data_2)); |
| 265 WaitForOperationComplete(); |
| 266 |
| 267 // Verify the response was received. |
| 268 ASSERT_EQ(response_data_2, fake_ipc_client.last_message_received()); |
| 269 |
| 270 // Typically the client will be the one to close the connection. |
| 271 fake_ipc_client.CloseIpcConnection(); |
| 272 } |
| 273 |
| 274 TEST_F(RemoteSecurityKeyIpcServerTest, InitialIpcConnectionTimeout) { |
| 275 // Create a channel, then wait for the done callback to be called indicating |
| 276 // the connection was closed. This test simulates the IPC Server being |
| 277 // created but the client failing to connect to it. |
| 278 std::string channel_name(GetUniqueTestChannelName()); |
| 279 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( |
| 280 channel_name, |
| 281 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 282 base::Time start_time(base::Time::NowFromSystemTime()); |
| 283 WaitForOperationComplete(); |
| 284 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; |
| 285 |
| 286 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs, |
| 287 kConnectionTimeoutErrorDeltaMs); |
| 288 } |
| 289 |
| 290 TEST_F(RemoteSecurityKeyIpcServerTest, NoGnubbyRequestTimeout) { |
| 291 // Create a channel and connect to it via IPC but do not send a request. |
| 292 // The channel should be closed and cleaned up if the IPC client does not |
| 293 // issue a request within the specified timeout period. |
| 294 std::string channel_name(GetUniqueTestChannelName()); |
| 295 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( |
| 296 channel_name, |
| 297 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 298 |
| 299 // Create a fake client and connect to the IPC server channel. |
| 300 FakeRemoteSecurityKeyIpcClient fake_ipc_client( |
| 301 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, |
| 302 base::Unretained(this))); |
| 303 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 304 WaitForOperationComplete(); |
| 305 |
| 306 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 307 |
| 308 // Now that a connection has been established, we wait for the timeout. |
| 309 base::Time start_time(base::Time::NowFromSystemTime()); |
| 310 WaitForOperationComplete(); |
| 311 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; |
| 312 |
| 313 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs, |
| 314 kConnectionTimeoutErrorDeltaMs); |
| 315 } |
| 316 |
| 317 TEST_F(RemoteSecurityKeyIpcServerTest, GnubbyResponseTimeout) { |
| 318 // Create a channel, connect to it via IPC, and issue a request, but do |
| 319 // not send a response. This simulates a client-side timeout. |
| 320 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(50)); |
| 321 std::string channel_name(GetUniqueTestChannelName()); |
| 322 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel(channel_name, |
| 323 request_timeout)); |
| 324 |
| 325 // Create a fake client and connect to the IPC server channel. |
| 326 FakeRemoteSecurityKeyIpcClient fake_ipc_client( |
| 327 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, |
| 328 base::Unretained(this))); |
| 329 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 330 WaitForOperationComplete(); |
| 331 |
| 332 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 333 |
| 334 // Now that a connection has been established, we issue a request and |
| 335 // then wait for the timeout. |
| 336 std::string request_data("I can haz Auth?"); |
| 337 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 338 WaitForOperationComplete(); |
| 339 |
| 340 // Leave the request hanging until it times out... |
| 341 base::Time start_time(base::Time::NowFromSystemTime()); |
| 342 WaitForOperationComplete(); |
| 343 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; |
| 344 |
| 345 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(), |
| 346 kConnectionTimeoutErrorDeltaMs); |
| 347 } |
| 348 |
| 349 TEST_F(RemoteSecurityKeyIpcServerTest, SendResponseTimeout) { |
| 350 // Create a channel, connect to it via IPC, issue a request, and send |
| 351 // a response, but do not close the channel after that. The connection |
| 352 // should be terminated after the initial timeout period has elapsed. |
| 353 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); |
| 354 std::string channel_name(GetUniqueTestChannelName()); |
| 355 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( |
| 356 channel_name, request_timeout)); |
| 357 |
| 358 // Create a fake client and connect to the IPC server channel. |
| 359 FakeRemoteSecurityKeyIpcClient fake_ipc_client( |
| 360 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, |
| 361 base::Unretained(this))); |
| 362 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 363 WaitForOperationComplete(); |
| 364 |
| 365 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 366 |
| 367 // Issue a request. |
| 368 std::string request_data("Auth me yo!"); |
| 369 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 370 WaitForOperationComplete(); |
| 371 |
| 372 // Send a response from the IPC server to the IPC client. |
| 373 std::string response_data("OK, the secret code is 1-2-3-4-5"); |
| 374 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data)); |
| 375 WaitForOperationComplete(); |
| 376 |
| 377 // Now wait for the timeout period for the connection to be torn down. |
| 378 base::Time start_time(base::Time::NowFromSystemTime()); |
| 379 WaitForOperationComplete(); |
| 380 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; |
| 381 |
| 382 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(), |
| 383 kConnectionTimeoutErrorDeltaMs); |
| 384 } |
| 385 |
| 386 #if defined(OS_WIN) |
| 387 TEST_F(RemoteSecurityKeyIpcServerTest, IpcConnectionFailsFromInvalidSession) { |
| 388 uint32_t peer_session_id = UINT32_MAX; |
| 389 ASSERT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), |
| 390 reinterpret_cast<DWORD*>(&peer_session_id))); |
| 391 peer_session_id++; |
| 392 |
| 393 // Reinitialize the object under test. |
| 394 remote_security_key_ipc_server_ = |
| 395 remoting::RemoteSecurityKeyIpcServer::Create( |
| 396 kTestConnectionId, peer_session_id, |
| 397 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs), |
| 398 base::Bind(&RemoteSecurityKeyIpcServerTest::SendRequestToClient, |
| 399 base::Unretained(this)), |
| 400 base::Bind(&base::DoNothing)); |
| 401 |
| 402 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); |
| 403 std::string channel_name(GetUniqueTestChannelName()); |
| 404 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel(channel_name, |
| 405 request_timeout)); |
| 406 |
| 407 // Create a fake client and attempt to connect to the IPC server channel. |
| 408 FakeRemoteSecurityKeyIpcClient fake_ipc_client( |
| 409 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, |
| 410 base::Unretained(this))); |
| 411 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 412 WaitForOperationComplete(); |
| 413 WaitForOperationComplete(); |
| 414 |
| 415 // Verify the connection failed. |
| 416 ASSERT_FALSE(fake_ipc_client.ipc_channel_connected()); |
| 417 } |
| 418 #endif // defined(OS_WIN) |
| 419 |
| 420 } // namespace remoting |
OLD | NEW |