| 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_server.h" | 5 #include "remoting/host/security_key/security_key_ipc_server.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/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.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 "mojo/edk/embedder/embedder.h" | 17 #include "mojo/edk/embedder/embedder.h" |
| 18 #include "mojo/edk/embedder/named_platform_handle_utils.h" | 18 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 19 #include "mojo/edk/embedder/scoped_ipc_support.h" | |
| 20 #include "remoting/host/client_session_details.h" | 19 #include "remoting/host/client_session_details.h" |
| 21 #include "remoting/host/security_key/fake_security_key_ipc_client.h" | 20 #include "remoting/host/security_key/fake_security_key_ipc_client.h" |
| 22 #include "remoting/host/security_key/security_key_ipc_constants.h" | 21 #include "remoting/host/security_key/security_key_ipc_constants.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 const int kTestConnectionId = 42; | 25 const int kTestConnectionId = 42; |
| 27 const int kInitialConnectTimeoutMs = 250; | 26 const int kInitialConnectTimeoutMs = 250; |
| 28 const int kConnectionTimeoutErrorDeltaMs = 100; | 27 const int kConnectionTimeoutErrorDeltaMs = 100; |
| 29 const int kLargeMessageSizeBytes = 256 * 1024; | 28 const int kLargeMessageSizeBytes = 256 * 1024; |
| 30 } // namespace | 29 } // namespace |
| 31 | 30 |
| 32 namespace remoting { | 31 namespace remoting { |
| 33 | 32 |
| 34 class SecurityKeyIpcServerTest : public testing::Test, | 33 class SecurityKeyIpcServerTest : public testing::Test, |
| 35 public ClientSessionDetails { | 34 public ClientSessionDetails { |
| 36 public: | 35 public: |
| 37 SecurityKeyIpcServerTest(); | 36 SecurityKeyIpcServerTest(); |
| 38 ~SecurityKeyIpcServerTest() override; | 37 ~SecurityKeyIpcServerTest() override; |
| 39 | 38 |
| 40 // Passed to the object used for testing to be called back to signal | 39 // Passed to the object used for testing to be called back to signal |
| 41 // completion of an IPC channel state change or reception of an IPC message. | 40 // completion of an IPC channel state change or reception of an IPC message. |
| 42 void OperationComplete(); | 41 void OperationComplete(); |
| 43 | 42 |
| 44 // Used as a callback to signal receipt of a security key request message. | 43 // Used as a callback to signal receipt of a security key request message. |
| 45 void SendRequestToClient(int connection_id, const std::string& data); | 44 void SendRequestToClient(int connection_id, const std::string& data); |
| 46 | 45 |
| 47 protected: | 46 protected: |
| 47 // testing::Test interface. |
| 48 void TearDown() override; |
| 49 |
| 48 // Returns a unique IPC channel name which prevents conflicts when running | 50 // Returns a unique IPC channel name which prevents conflicts when running |
| 49 // tests concurrently. | 51 // tests concurrently. |
| 50 std::string GetUniqueTestChannelName(); | 52 std::string GetUniqueTestChannelName(); |
| 51 | 53 |
| 52 // Waits until the current |run_loop_| instance is signaled, then resets it. | 54 // Waits until the current |run_loop_| instance is signaled, then resets it. |
| 53 void WaitForOperationComplete(); | 55 void WaitForOperationComplete(); |
| 54 | 56 |
| 57 // Waits until all tasks have been run on the current message loop. |
| 58 void RunPendingTasks(); |
| 59 |
| 55 // ClientSessionControl overrides: | 60 // ClientSessionControl overrides: |
| 56 ClientSessionControl* session_control() override { return nullptr; } | 61 ClientSessionControl* session_control() override { return nullptr; } |
| 57 uint32_t desktop_session_id() const override { return peer_session_id_; } | 62 uint32_t desktop_session_id() const override { return peer_session_id_; } |
| 58 | 63 |
| 59 // IPC tests require a valid MessageLoop to run. | 64 // IPC tests require a valid MessageLoop to run. |
| 60 base::MessageLoopForIO message_loop_; | 65 base::MessageLoopForIO message_loop_; |
| 61 | 66 |
| 62 mojo::edk::ScopedIPCSupport ipc_support_; | |
| 63 | |
| 64 // Used to allow |message_loop_| to run during tests. The instance is reset | 67 // Used to allow |message_loop_| to run during tests. The instance is reset |
| 65 // after each stage of the tests has been completed. | 68 // after each stage of the tests has been completed. |
| 66 std::unique_ptr<base::RunLoop> run_loop_; | 69 std::unique_ptr<base::RunLoop> run_loop_; |
| 67 | 70 |
| 68 // The object under test. | 71 // The object under test. |
| 69 std::unique_ptr<SecurityKeyIpcServer> security_key_ipc_server_; | 72 std::unique_ptr<SecurityKeyIpcServer> security_key_ipc_server_; |
| 70 | 73 |
| 71 // Used to validate the object under test uses the correct ID when | 74 // Used to validate the object under test uses the correct ID when |
| 72 // communicating over the IPC channel. | 75 // communicating over the IPC channel. |
| 73 int last_connection_id_received_ = -1; | 76 int last_connection_id_received_ = -1; |
| 74 | 77 |
| 75 // Stores the contents of the last IPC message received for validation. | 78 // Stores the contents of the last IPC message received for validation. |
| 76 std::string last_message_received_; | 79 std::string last_message_received_; |
| 77 | 80 |
| 78 uint32_t peer_session_id_ = UINT32_MAX; | 81 uint32_t peer_session_id_ = UINT32_MAX; |
| 79 | 82 |
| 80 private: | 83 private: |
| 81 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcServerTest); | 84 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcServerTest); |
| 82 }; | 85 }; |
| 83 | 86 |
| 84 SecurityKeyIpcServerTest::SecurityKeyIpcServerTest() | 87 SecurityKeyIpcServerTest::SecurityKeyIpcServerTest() |
| 85 : ipc_support_(message_loop_.task_runner(), | 88 : run_loop_(new base::RunLoop()) { |
| 86 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST), | |
| 87 run_loop_(new base::RunLoop()) { | |
| 88 #if defined(OS_WIN) | 89 #if defined(OS_WIN) |
| 89 EXPECT_TRUE(ProcessIdToSessionId( | 90 EXPECT_TRUE(ProcessIdToSessionId( |
| 90 GetCurrentProcessId(), reinterpret_cast<DWORD*>(&peer_session_id_))); | 91 GetCurrentProcessId(), reinterpret_cast<DWORD*>(&peer_session_id_))); |
| 91 #endif // defined(OS_WIN) | 92 #endif // defined(OS_WIN) |
| 92 | 93 |
| 93 security_key_ipc_server_ = remoting::SecurityKeyIpcServer::Create( | 94 security_key_ipc_server_ = remoting::SecurityKeyIpcServer::Create( |
| 94 kTestConnectionId, this, | 95 kTestConnectionId, this, |
| 95 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs), | 96 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs), |
| 96 base::Bind(&SecurityKeyIpcServerTest::SendRequestToClient, | 97 base::Bind(&SecurityKeyIpcServerTest::SendRequestToClient, |
| 97 base::Unretained(this)), | 98 base::Unretained(this)), |
| 98 base::Bind(&base::DoNothing), | 99 base::Bind(&base::DoNothing), |
| 99 base::Bind(&SecurityKeyIpcServerTest::OperationComplete, | 100 base::Bind(&SecurityKeyIpcServerTest::OperationComplete, |
| 100 base::Unretained(this))); | 101 base::Unretained(this))); |
| 101 } | 102 } |
| 102 | 103 |
| 103 SecurityKeyIpcServerTest::~SecurityKeyIpcServerTest() {} | 104 SecurityKeyIpcServerTest::~SecurityKeyIpcServerTest() {} |
| 104 | 105 |
| 105 void SecurityKeyIpcServerTest::OperationComplete() { | 106 void SecurityKeyIpcServerTest::OperationComplete() { |
| 106 run_loop_->Quit(); | 107 run_loop_->Quit(); |
| 107 } | 108 } |
| 108 | 109 |
| 109 void SecurityKeyIpcServerTest::WaitForOperationComplete() { | 110 void SecurityKeyIpcServerTest::WaitForOperationComplete() { |
| 110 run_loop_->Run(); | 111 run_loop_->Run(); |
| 111 run_loop_.reset(new base::RunLoop()); | 112 run_loop_.reset(new base::RunLoop()); |
| 112 } | 113 } |
| 113 | 114 |
| 115 void SecurityKeyIpcServerTest::RunPendingTasks() { |
| 116 // Run until there are no pending work items in the queue. |
| 117 base::RunLoop().RunUntilIdle(); |
| 118 } |
| 119 |
| 120 void SecurityKeyIpcServerTest::TearDown() { |
| 121 RunPendingTasks(); |
| 122 } |
| 123 |
| 114 void SecurityKeyIpcServerTest::SendRequestToClient(int connection_id, | 124 void SecurityKeyIpcServerTest::SendRequestToClient(int connection_id, |
| 115 const std::string& data) { | 125 const std::string& data) { |
| 116 last_connection_id_received_ = connection_id; | 126 last_connection_id_received_ = connection_id; |
| 117 last_message_received_ = data; | 127 last_message_received_ = data; |
| 118 OperationComplete(); | 128 OperationComplete(); |
| 119 } | 129 } |
| 120 | 130 |
| 121 std::string SecurityKeyIpcServerTest::GetUniqueTestChannelName() { | 131 std::string SecurityKeyIpcServerTest::GetUniqueTestChannelName() { |
| 122 return GetChannelNamePathPrefixForTest() + "Super_Awesome_Test_Channel." + | 132 return GetChannelNamePathPrefixForTest() + "Super_Awesome_Test_Channel." + |
| 123 IPC::Channel::GenerateUniqueRandomChannelID(); | 133 IPC::Channel::GenerateUniqueRandomChannelID(); |
| 124 } | 134 } |
| 125 | 135 |
| 126 TEST_F(SecurityKeyIpcServerTest, HandleSingleSecurityKeyRequest) { | 136 TEST_F(SecurityKeyIpcServerTest, HandleSingleSecurityKeyRequest) { |
| 127 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName()); | 137 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName()); |
| 128 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( | 138 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( |
| 129 channel_handle, | 139 channel_handle, |
| 130 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 140 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 131 | 141 |
| 132 // Create a fake client and connect to the IPC server channel. | 142 // Create a fake client and connect to the IPC server channel. |
| 133 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( | 143 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 134 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); | 144 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); |
| 135 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle)); | 145 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle)); |
| 136 WaitForOperationComplete(); | 146 WaitForOperationComplete(); |
| 137 | 147 |
| 148 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 138 ASSERT_FALSE(fake_ipc_client.invalid_session_error()); | 149 ASSERT_FALSE(fake_ipc_client.invalid_session_error()); |
| 139 ASSERT_TRUE(fake_ipc_client.connection_ready()); | 150 ASSERT_TRUE(fake_ipc_client.connection_ready()); |
| 140 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); | |
| 141 | 151 |
| 142 // Send a request from the IPC client to the IPC server. | 152 // Send a request from the IPC client to the IPC server. |
| 143 std::string request_data("Blergh!"); | 153 std::string request_data("Blergh!"); |
| 144 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); | 154 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 145 WaitForOperationComplete(); | 155 WaitForOperationComplete(); |
| 146 | 156 |
| 147 // Verify the request was received. | 157 // Verify the request was received. |
| 148 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); | 158 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 149 ASSERT_EQ(request_data, last_message_received_); | 159 ASSERT_EQ(request_data, last_message_received_); |
| 150 | 160 |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 TEST_F(SecurityKeyIpcServerTest, IpcConnectionFailsFromInvalidSession) { | 492 TEST_F(SecurityKeyIpcServerTest, IpcConnectionFailsFromInvalidSession) { |
| 483 // Change the expected session ID to not match the current session. | 493 // Change the expected session ID to not match the current session. |
| 484 peer_session_id_++; | 494 peer_session_id_++; |
| 485 | 495 |
| 486 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); | 496 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); |
| 487 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName()); | 497 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName()); |
| 488 ASSERT_TRUE( | 498 ASSERT_TRUE( |
| 489 security_key_ipc_server_->CreateChannel(channel_handle, request_timeout)); | 499 security_key_ipc_server_->CreateChannel(channel_handle, request_timeout)); |
| 490 | 500 |
| 491 // Create a fake client and attempt to connect to the IPC server channel. | 501 // Create a fake client and attempt to connect to the IPC server channel. |
| 492 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( | 502 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(&base::DoNothing)); |
| 493 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); | |
| 494 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle)); | 503 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle)); |
| 495 WaitForOperationComplete(); | 504 WaitForOperationComplete(); |
| 496 WaitForOperationComplete(); | |
| 497 WaitForOperationComplete(); | |
| 498 | 505 |
| 499 // Verify the connection failed. | 506 // Verify the connection failed. |
| 500 ASSERT_TRUE(fake_ipc_client.invalid_session_error()); | 507 ASSERT_TRUE(fake_ipc_client.invalid_session_error()); |
| 501 ASSERT_FALSE(fake_ipc_client.connection_ready()); | 508 ASSERT_FALSE(fake_ipc_client.connection_ready()); |
| 509 |
| 510 RunPendingTasks(); |
| 502 ASSERT_FALSE(fake_ipc_client.ipc_channel_connected()); | 511 ASSERT_FALSE(fake_ipc_client.ipc_channel_connected()); |
| 503 } | 512 } |
| 504 #endif // defined(OS_WIN) | 513 #endif // defined(OS_WIN) |
| 505 | 514 |
| 506 } // namespace remoting | 515 } // namespace remoting |
| OLD | NEW |