| 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/gnubby_auth_handler.h" | 5 #include "remoting/host/security_key/gnubby_auth_handler.h" |
| 6 | 6 |
| 7 #include <cstdint> |
| 7 #include <memory> | 8 #include <memory> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 15 #include "ipc/ipc_channel.h" | 16 #include "ipc/ipc_channel.h" |
| 16 #include "ipc/ipc_listener.h" | 17 #include "ipc/ipc_listener.h" |
| 17 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
| 18 #include "ipc/ipc_message_macros.h" | 19 #include "ipc/ipc_message_macros.h" |
| 20 #include "remoting/host/host_mock_objects.h" |
| 19 #include "remoting/host/security_key/fake_remote_security_key_ipc_client.h" | 21 #include "remoting/host/security_key/fake_remote_security_key_ipc_client.h" |
| 20 #include "remoting/host/security_key/fake_remote_security_key_ipc_server.h" | 22 #include "remoting/host/security_key/fake_remote_security_key_ipc_server.h" |
| 21 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | 23 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 const int kConnectionId1 = 1; | 28 const int kConnectionId1 = 1; |
| 26 const int kConnectionId2 = 2; | 29 const int kConnectionId2 = 2; |
| 27 } // namespace | 30 } // namespace |
| 28 | 31 |
| 29 namespace remoting { | 32 namespace remoting { |
| 30 | 33 |
| 31 class GnubbyAuthHandlerWinTest : public testing::Test { | 34 class GnubbyAuthHandlerWinTest : public testing::Test { |
| 32 public: | 35 public: |
| 33 GnubbyAuthHandlerWinTest(); | 36 GnubbyAuthHandlerWinTest(); |
| 34 ~GnubbyAuthHandlerWinTest() override; | 37 ~GnubbyAuthHandlerWinTest() override; |
| 35 | 38 |
| 36 // 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 |
| 37 // 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. |
| 38 void OperationComplete(); | 41 void OperationComplete(); |
| 39 | 42 |
| 40 protected: | 43 protected: |
| 41 // Waits until the current |run_loop_| instance is signaled, then resets it. | 44 // Waits until the current |run_loop_| instance is signaled, then resets it. |
| 42 void WaitForOperationComplete(); | 45 void WaitForOperationComplete(); |
| 43 | 46 |
| 44 // Used as a callback given to the object under test, expected to be called | 47 // Used as a callback given to the object under test, expected to be called |
| 45 // back when a security key request is received by it. | 48 // back when a security key request is received by it. |
| 46 void SendMessageToClient(int connection_id, const std::string& data); | 49 void SendMessageToClient(int connection_id, const std::string& data); |
| 47 | 50 |
| 48 // Creates a new gnubby connection on the object under test. | 51 // Creates a new gnubby connection on the object under test. |
| 49 void CreateGnubbyConnection(const std::string& channel_name); | 52 void CreateGnubbyConnection(const std::string& channel_name); |
| 50 | 53 |
| 54 // Sets |desktop_session_id_| to the id for the current Windows session. |
| 55 void InitializeDesktopSessionId(); |
| 56 |
| 51 // Uses |fake_ipc_client| to connect to the initial IPC server channel, it | 57 // Uses |fake_ipc_client| to connect to the initial IPC server channel, it |
| 52 // then validates internal state of the object under test and closes the | 58 // then validates internal state of the object under test and closes the |
| 53 // connection based on |close_connection|. | 59 // connection based on |close_connection|. |
| 54 void EstablishInitialIpcConnection( | 60 void EstablishInitialIpcConnection( |
| 55 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, | 61 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, |
| 56 int expected_connection_id, | 62 int expected_connection_id, |
| 57 const std::string& channel_name, | 63 const std::string& channel_name, |
| 58 bool close_connection); | 64 bool close_connection); |
| 59 | 65 |
| 60 // Sends a security key response message using |fake_ipc_server| and | 66 // Sends a security key response message using |fake_ipc_server| and |
| (...skipping 24 matching lines...) Expand all Loading... |
| 85 | 91 |
| 86 // Used to allow |message_loop_| to run during tests. The instance is reset | 92 // Used to allow |message_loop_| to run during tests. The instance is reset |
| 87 // after each stage of the tests has been completed. | 93 // after each stage of the tests has been completed. |
| 88 std::unique_ptr<base::RunLoop> run_loop_; | 94 std::unique_ptr<base::RunLoop> run_loop_; |
| 89 | 95 |
| 90 // The object under test. | 96 // The object under test. |
| 91 std::unique_ptr<GnubbyAuthHandler> auth_handler_; | 97 std::unique_ptr<GnubbyAuthHandler> auth_handler_; |
| 92 | 98 |
| 93 // Set as the default factory to create RemoteSecurityKeyIpcServerFactory | 99 // Set as the default factory to create RemoteSecurityKeyIpcServerFactory |
| 94 // instances, this class will track each objects creation and allow the tests | 100 // instances, this class will track each objects creation and allow the tests |
| 95 // to access it and use it for driving tests and validate state. | 101 // to access it and use it for driving tests and validating state. |
| 96 FakeRemoteSecurityKeyIpcServerFactory ipc_server_factory_; | 102 FakeRemoteSecurityKeyIpcServerFactory ipc_server_factory_; |
| 97 | 103 |
| 98 // Used to validate the object under test uses the correct ID when | 104 // Used to validate the object under test uses the correct ID when |
| 99 // communicating over the IPC channel. | 105 // communicating over the IPC channel. |
| 100 int last_connection_id_received_ = -1; | 106 int last_connection_id_received_ = -1; |
| 101 | 107 |
| 108 // Used to validate that IPC connections are only allowed from a specific |
| 109 // Windows session. |
| 110 DWORD desktop_session_id_ = UINT32_MAX; |
| 111 |
| 102 // Stores the contents of the last IPC message received for validation. | 112 // Stores the contents of the last IPC message received for validation. |
| 103 std::string last_message_received_; | 113 std::string last_message_received_; |
| 104 | 114 |
| 105 private: | 115 private: |
| 116 testing::NiceMock<MockClientSessionDetails> mock_client_session_details_; |
| 117 |
| 106 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerWinTest); | 118 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerWinTest); |
| 107 }; | 119 }; |
| 108 | 120 |
| 109 GnubbyAuthHandlerWinTest::GnubbyAuthHandlerWinTest() | 121 GnubbyAuthHandlerWinTest::GnubbyAuthHandlerWinTest() |
| 110 : run_loop_(new base::RunLoop()) { | 122 : run_loop_(new base::RunLoop()) { |
| 111 auth_handler_ = remoting::GnubbyAuthHandler::Create(base::Bind( | 123 auth_handler_ = remoting::GnubbyAuthHandler::Create( |
| 112 &GnubbyAuthHandlerWinTest::SendMessageToClient, base::Unretained(this))); | 124 &mock_client_session_details_, |
| 125 base::Bind(&GnubbyAuthHandlerWinTest::SendMessageToClient, |
| 126 base::Unretained(this))); |
| 113 } | 127 } |
| 114 | 128 |
| 115 GnubbyAuthHandlerWinTest::~GnubbyAuthHandlerWinTest() {} | 129 GnubbyAuthHandlerWinTest::~GnubbyAuthHandlerWinTest() {} |
| 116 | 130 |
| 117 void GnubbyAuthHandlerWinTest::OperationComplete() { | 131 void GnubbyAuthHandlerWinTest::OperationComplete() { |
| 118 run_loop_->Quit(); | 132 run_loop_->Quit(); |
| 119 } | 133 } |
| 120 | 134 |
| 121 void GnubbyAuthHandlerWinTest::WaitForOperationComplete() { | 135 void GnubbyAuthHandlerWinTest::WaitForOperationComplete() { |
| 122 run_loop_->Run(); | 136 run_loop_->Run(); |
| 123 run_loop_.reset(new base::RunLoop()); | 137 run_loop_.reset(new base::RunLoop()); |
| 124 } | 138 } |
| 125 | 139 |
| 126 void GnubbyAuthHandlerWinTest::SendMessageToClient(int connection_id, | 140 void GnubbyAuthHandlerWinTest::SendMessageToClient(int connection_id, |
| 127 const std::string& data) { | 141 const std::string& data) { |
| 128 last_connection_id_received_ = connection_id; | 142 last_connection_id_received_ = connection_id; |
| 129 last_message_received_ = data; | 143 last_message_received_ = data; |
| 130 OperationComplete(); | 144 OperationComplete(); |
| 131 } | 145 } |
| 132 | 146 |
| 133 void GnubbyAuthHandlerWinTest::CreateGnubbyConnection( | 147 void GnubbyAuthHandlerWinTest::CreateGnubbyConnection( |
| 134 const std::string& channel_name) { | 148 const std::string& channel_name) { |
| 135 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 149 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
| 136 | 150 |
| 137 remoting::SetRemoteSecurityKeyIpcChannelNameForTest(channel_name); | 151 remoting::SetRemoteSecurityKeyIpcChannelNameForTest(channel_name); |
| 138 | 152 |
| 139 // Create a new Gnubby IPC Server connection. | 153 // Create a new Gnubby IPC Server connection. |
| 140 auth_handler_->CreateGnubbyConnection(); | 154 auth_handler_->CreateGnubbyConnection(); |
| 141 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(channel_name)); | 155 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(channel_name)); |
| 156 |
| 157 InitializeDesktopSessionId(); |
| 158 } |
| 159 |
| 160 void GnubbyAuthHandlerWinTest::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_)); |
| 142 } | 166 } |
| 143 | 167 |
| 144 void GnubbyAuthHandlerWinTest::EstablishInitialIpcConnection( | 168 void GnubbyAuthHandlerWinTest::EstablishInitialIpcConnection( |
| 145 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, | 169 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, |
| 146 int expected_connection_id, | 170 int expected_connection_id, |
| 147 const std::string& channel_name, | 171 const std::string& channel_name, |
| 148 bool close_connection) { | 172 bool close_connection) { |
| 149 size_t expected_connection_count = | 173 size_t expected_connection_count = |
| 150 auth_handler_->GetActiveConnectionCountForTest() + 1; | 174 auth_handler_->GetActiveConnectionCountForTest() + 1; |
| 151 | 175 |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 // Verify the connection was cleaned up. | 506 // Verify the connection was cleaned up. |
| 483 ASSERT_FALSE(fake_ipc_server.get()); | 507 ASSERT_FALSE(fake_ipc_server.get()); |
| 484 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); | 508 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); |
| 485 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 509 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
| 486 | 510 |
| 487 // Attempt to connect again after the error. | 511 // Attempt to connect again after the error. |
| 488 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | 512 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, |
| 489 /*close_connection=*/true); | 513 /*close_connection=*/true); |
| 490 } | 514 } |
| 491 | 515 |
| 516 TEST_F(GnubbyAuthHandlerWinTest, IpcConnectionFailsFromInvalidSession) { |
| 517 std::string channel_name(GetUniqueTestChannelName()); |
| 518 CreateGnubbyConnection(channel_name); |
| 519 |
| 520 // Set the current session id to a 'different' session. |
| 521 desktop_session_id_ += 1; |
| 522 |
| 523 // Create a fake client and connect to the IPC server channel. |
| 524 FakeRemoteSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 525 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); |
| 526 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 527 // Wait for the error callback to be signaled. |
| 528 WaitForOperationComplete(); |
| 529 |
| 530 // Verify the connection was not set up. |
| 531 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); |
| 532 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
| 533 } |
| 534 |
| 492 } // namespace remoting | 535 } // namespace remoting |
| OLD | NEW |