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" |
(...skipping 21 matching lines...) Expand all Loading... |
38 void OperationComplete(); | 39 void OperationComplete(); |
39 | 40 |
40 protected: | 41 protected: |
41 // Waits until the current |run_loop_| instance is signaled, then resets it. | 42 // Waits until the current |run_loop_| instance is signaled, then resets it. |
42 void WaitForOperationComplete(); | 43 void WaitForOperationComplete(); |
43 | 44 |
44 // Used as a callback given to the object under test, expected to be called | 45 // 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. | 46 // back when a security key request is received by it. |
46 void SendMessageToClient(int connection_id, const std::string& data); | 47 void SendMessageToClient(int connection_id, const std::string& data); |
47 | 48 |
| 49 // Used as a callback given to the object under test, returns the id of the |
| 50 // Windows session which is being remoted. |
| 51 uint32_t GetDesktopSessionId() const; |
| 52 |
48 // Creates a new gnubby connection on the object under test. | 53 // Creates a new gnubby connection on the object under test. |
49 void CreateGnubbyConnection(const std::string& channel_name); | 54 void CreateGnubbyConnection(const std::string& channel_name); |
50 | 55 |
| 56 // Sets |desktop_session_id_| to the id for the current Windows session. |
| 57 bool SetDesktopSessionId(); |
| 58 |
51 // Uses |fake_ipc_client| to connect to the initial IPC server channel, it | 59 // 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 | 60 // then validates internal state of the object under test and closes the |
53 // connection based on |close_connection|. | 61 // connection based on |close_connection|. |
54 void EstablishInitialIpcConnection( | 62 void EstablishInitialIpcConnection( |
55 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, | 63 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, |
56 int expected_connection_id, | 64 int expected_connection_id, |
57 const std::string& channel_name, | 65 const std::string& channel_name, |
58 bool close_connection); | 66 bool close_connection); |
59 | 67 |
60 // Sends a security key response message using |fake_ipc_server| and | 68 // Sends a security key response message using |fake_ipc_server| and |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 100 |
93 // Set as the default factory to create RemoteSecurityKeyIpcServerFactory | 101 // Set as the default factory to create RemoteSecurityKeyIpcServerFactory |
94 // instances, this class will track each objects creation and allow the tests | 102 // 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. | 103 // to access it and use it for driving tests and validate state. |
96 FakeRemoteSecurityKeyIpcServerFactory ipc_server_factory_; | 104 FakeRemoteSecurityKeyIpcServerFactory ipc_server_factory_; |
97 | 105 |
98 // Used to validate the object under test uses the correct ID when | 106 // Used to validate the object under test uses the correct ID when |
99 // communicating over the IPC channel. | 107 // communicating over the IPC channel. |
100 int last_connection_id_received_ = -1; | 108 int last_connection_id_received_ = -1; |
101 | 109 |
| 110 // Used to validate that IPC connections are only allowed from a specific |
| 111 // Windows session. |
| 112 DWORD desktop_session_id_ = UINT32_MAX; |
| 113 |
102 // Stores the contents of the last IPC message received for validation. | 114 // Stores the contents of the last IPC message received for validation. |
103 std::string last_message_received_; | 115 std::string last_message_received_; |
104 | 116 |
105 private: | 117 private: |
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 base::Bind(&GnubbyAuthHandlerWinTest::SendMessageToClient, |
| 125 base::Unretained(this)), |
| 126 base::Bind(&GnubbyAuthHandlerWinTest::GetDesktopSessionId, |
| 127 base::Unretained(this))); |
113 } | 128 } |
114 | 129 |
115 GnubbyAuthHandlerWinTest::~GnubbyAuthHandlerWinTest() {} | 130 GnubbyAuthHandlerWinTest::~GnubbyAuthHandlerWinTest() {} |
116 | 131 |
117 void GnubbyAuthHandlerWinTest::OperationComplete() { | 132 void GnubbyAuthHandlerWinTest::OperationComplete() { |
118 run_loop_->Quit(); | 133 run_loop_->Quit(); |
119 } | 134 } |
120 | 135 |
121 void GnubbyAuthHandlerWinTest::WaitForOperationComplete() { | 136 void GnubbyAuthHandlerWinTest::WaitForOperationComplete() { |
122 run_loop_->Run(); | 137 run_loop_->Run(); |
123 run_loop_.reset(new base::RunLoop()); | 138 run_loop_.reset(new base::RunLoop()); |
124 } | 139 } |
125 | 140 |
126 void GnubbyAuthHandlerWinTest::SendMessageToClient(int connection_id, | 141 void GnubbyAuthHandlerWinTest::SendMessageToClient(int connection_id, |
127 const std::string& data) { | 142 const std::string& data) { |
128 last_connection_id_received_ = connection_id; | 143 last_connection_id_received_ = connection_id; |
129 last_message_received_ = data; | 144 last_message_received_ = data; |
130 OperationComplete(); | 145 OperationComplete(); |
131 } | 146 } |
132 | 147 |
| 148 uint32_t GnubbyAuthHandlerWinTest::GetDesktopSessionId() const { |
| 149 return desktop_session_id_; |
| 150 } |
| 151 |
133 void GnubbyAuthHandlerWinTest::CreateGnubbyConnection( | 152 void GnubbyAuthHandlerWinTest::CreateGnubbyConnection( |
134 const std::string& channel_name) { | 153 const std::string& channel_name) { |
135 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 154 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
136 | 155 |
137 remoting::SetRemoteSecurityKeyIpcChannelNameForTest(channel_name); | 156 remoting::SetRemoteSecurityKeyIpcChannelNameForTest(channel_name); |
138 | 157 |
139 // Create a new Gnubby IPC Server connection. | 158 // Create a new Gnubby IPC Server connection. |
140 auth_handler_->CreateGnubbyConnection(); | 159 auth_handler_->CreateGnubbyConnection(); |
141 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(channel_name)); | 160 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(channel_name)); |
| 161 |
| 162 ASSERT_TRUE(SetDesktopSessionId()); |
| 163 } |
| 164 |
| 165 bool GnubbyAuthHandlerWinTest::SetDesktopSessionId() { |
| 166 return ProcessIdToSessionId(GetCurrentProcessId(), &desktop_session_id_); |
142 } | 167 } |
143 | 168 |
144 void GnubbyAuthHandlerWinTest::EstablishInitialIpcConnection( | 169 void GnubbyAuthHandlerWinTest::EstablishInitialIpcConnection( |
145 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, | 170 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, |
146 int expected_connection_id, | 171 int expected_connection_id, |
147 const std::string& channel_name, | 172 const std::string& channel_name, |
148 bool close_connection) { | 173 bool close_connection) { |
149 size_t expected_connection_count = | 174 size_t expected_connection_count = |
150 auth_handler_->GetActiveConnectionCountForTest() + 1; | 175 auth_handler_->GetActiveConnectionCountForTest() + 1; |
151 | 176 |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 // Verify the connection was cleaned up. | 507 // Verify the connection was cleaned up. |
483 ASSERT_FALSE(fake_ipc_server.get()); | 508 ASSERT_FALSE(fake_ipc_server.get()); |
484 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); | 509 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); |
485 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 510 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
486 | 511 |
487 // Attempt to connect again after the error. | 512 // Attempt to connect again after the error. |
488 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | 513 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, |
489 /*close_connection=*/true); | 514 /*close_connection=*/true); |
490 } | 515 } |
491 | 516 |
| 517 TEST_F(GnubbyAuthHandlerWinTest, IpcConnectionFailsFromInvalidSession) { |
| 518 std::string channel_name(GetUniqueTestChannelName()); |
| 519 CreateGnubbyConnection(channel_name); |
| 520 |
| 521 // Set the current session id to a 'different' session. |
| 522 desktop_session_id_ = desktop_session_id_ + 1; |
| 523 |
| 524 // Create a fake client and connect to the IPC server channel. |
| 525 FakeRemoteSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 526 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); |
| 527 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 528 // Wait for the error callback to be signaled. |
| 529 WaitForOperationComplete(); |
| 530 |
| 531 // Verify the connection was not set up. |
| 532 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); |
| 533 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
| 534 } |
| 535 |
492 } // namespace remoting | 536 } // namespace remoting |
OLD | NEW |