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_client.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/macros.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/run_loop.h" | |
14 #include "ipc/ipc_channel.h" | |
15 #include "remoting/host/security_key/fake_ipc_gnubby_auth_handler.h" | |
16 #include "remoting/host/security_key/fake_remote_security_key_ipc_server.h" | |
17 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace { | |
21 const int kTestConnectionId = 1; | |
22 const char kNonexistentIpcChannelName[] = "Nonexistent_IPC_Channel"; | |
23 const char kValidIpcChannelName[] = | |
24 "Remote_Security_Key_Ipc_Client_Test_Channel."; | |
25 const int kLargeMessageSizeBytes = 256 * 1024; | |
26 } // namespace | |
27 | |
28 namespace remoting { | |
29 | |
30 class RemoteSecurityKeyIpcClientTest : public testing::Test { | |
31 public: | |
32 RemoteSecurityKeyIpcClientTest(); | |
33 ~RemoteSecurityKeyIpcClientTest() 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(bool failed); | |
38 | |
39 // Used as a callback given to the object under test, expected to be called | |
40 // back when a security key request is received by it. | |
41 void SendMessageToClient(int connection_id, const std::string& data); | |
42 | |
43 // Used as a callback given to the object under test, expected to be called | |
44 // back when a security key response is sent. | |
45 void ClientMessageReceived(const std::string& response_payload); | |
46 | |
47 protected: | |
48 // testing::Test interface. | |
49 void SetUp() override; | |
50 | |
51 // Waits until the current |run_loop_| instance is signaled, then resets it. | |
52 void WaitForOperationComplete(); | |
53 | |
54 // Sets up an active IPC connection between |remote_security_key_ipc_client_| | |
55 // and |fake_ipc_server_|. |expect_success| defines whether the operation | |
56 // is expected to succeed or fail. | |
57 void EstablishConnection(bool expect_success); | |
58 | |
59 // Sends a security key request from |remote_security_key_ipc_client_| and | |
60 // a response from |fake_ipc_server_| and verifies the payloads for both. | |
61 void SendRequestAndResponse(const std::string& request_data, | |
62 const std::string& response_data); | |
63 | |
64 // Creates a unique IPC channel name to use for testing. | |
65 std::string GenerateUniqueTestChannelName(); | |
66 | |
67 // IPC tests require a valid MessageLoop to run. | |
68 base::MessageLoopForIO message_loop_; | |
69 | |
70 // Used to allow |message_loop_| to run during tests. The instance is reset | |
71 // after each stage of the tests has been completed. | |
72 std::unique_ptr<base::RunLoop> run_loop_; | |
73 | |
74 // The object under test. | |
75 RemoteSecurityKeyIpcClient remote_security_key_ipc_client_; | |
76 | |
77 // Provides a connection details message to |remote_security_key_ipc_client_| | |
78 // for testing. | |
79 FakeIpcGnubbyAuthHandler fake_gnubby_auth_handler_; | |
80 | |
81 // Used to send/receive security key IPC messages for testing. | |
82 FakeRemoteSecurityKeyIpcServer fake_ipc_server_; | |
83 | |
84 // Stores the current session ID on supported platforms. | |
85 uint32_t session_id_ = 0; | |
86 | |
87 // Tracks the success/failure of the last async operation. | |
88 bool operation_failed_ = false; | |
89 | |
90 // Used to validate the object under test uses the correct ID when | |
91 // communicating over the IPC channel. | |
92 int last_connection_id_received_ = -1; | |
93 | |
94 // Stores the contents of the last IPC message received for validation. | |
95 std::string last_message_received_; | |
96 | |
97 private: | |
98 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyIpcClientTest); | |
99 }; | |
100 | |
101 RemoteSecurityKeyIpcClientTest::RemoteSecurityKeyIpcClientTest() | |
102 : run_loop_(new base::RunLoop()), | |
103 fake_ipc_server_( | |
104 kTestConnectionId, | |
105 /*peer_session_id=*/UINT32_MAX, | |
106 /*initial_connect_timeout=*/base::TimeDelta::FromMilliseconds(500), | |
107 base::Bind(&RemoteSecurityKeyIpcClientTest::SendMessageToClient, | |
108 base::Unretained(this)), | |
109 base::Bind(&RemoteSecurityKeyIpcClientTest::OperationComplete, | |
110 base::Unretained(this), | |
111 /*failed=*/false)) {} | |
112 | |
113 RemoteSecurityKeyIpcClientTest::~RemoteSecurityKeyIpcClientTest() {} | |
114 | |
115 void RemoteSecurityKeyIpcClientTest::SetUp() { | |
116 #if defined(OS_WIN) | |
117 DWORD session_id = 0; | |
118 // If we are on Windows, then we need to set the correct session ID or the | |
119 // IPC connection will not be created successfully. | |
120 ASSERT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), &session_id)); | |
121 session_id_ = session_id; | |
122 remote_security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest( | |
123 session_id_); | |
124 #endif // defined(OS_WIN) | |
125 } | |
126 | |
127 void RemoteSecurityKeyIpcClientTest::OperationComplete(bool failed) { | |
128 operation_failed_ |= failed; | |
129 run_loop_->Quit(); | |
130 } | |
131 | |
132 void RemoteSecurityKeyIpcClientTest::WaitForOperationComplete() { | |
133 run_loop_->Run(); | |
134 run_loop_.reset(new base::RunLoop()); | |
135 } | |
136 | |
137 void RemoteSecurityKeyIpcClientTest::SendMessageToClient( | |
138 int connection_id, | |
139 const std::string& data) { | |
140 last_connection_id_received_ = connection_id; | |
141 last_message_received_ = data; | |
142 OperationComplete(/*failed=*/false); | |
143 } | |
144 | |
145 void RemoteSecurityKeyIpcClientTest::ClientMessageReceived( | |
146 const std::string& response_payload) { | |
147 last_message_received_ = response_payload; | |
148 OperationComplete(/*failed=*/false); | |
149 } | |
150 | |
151 std::string RemoteSecurityKeyIpcClientTest::GenerateUniqueTestChannelName() { | |
152 return GetChannelNamePathPrefixForTest() + kValidIpcChannelName + | |
153 IPC::Channel::GenerateUniqueRandomChannelID(); | |
154 } | |
155 | |
156 void RemoteSecurityKeyIpcClientTest::EstablishConnection(bool expect_success) { | |
157 // Start up the security key forwarding session IPC channel first, that way | |
158 // we can provide the channel using the fake GnubbyAuthHandler later on. | |
159 std::string ipc_session_channel_name = GenerateUniqueTestChannelName(); | |
160 ASSERT_TRUE(fake_ipc_server_.CreateChannel( | |
161 ipc_session_channel_name, | |
162 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | |
163 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(ipc_session_channel_name)); | |
164 fake_gnubby_auth_handler_.set_ipc_security_key_channel_name( | |
165 ipc_session_channel_name); | |
166 | |
167 // Set up the channel name for the initial IPC channel. | |
168 std::string ipc_server_channel_name = GenerateUniqueTestChannelName(); | |
169 fake_gnubby_auth_handler_.set_ipc_server_channel_name( | |
170 ipc_server_channel_name); | |
171 remote_security_key_ipc_client_.SetInitialIpcChannelNameForTest( | |
172 ipc_server_channel_name); | |
173 | |
174 // Create the initial IPC channel and verify it was set up correctly. | |
175 ASSERT_FALSE( | |
176 remote_security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
177 fake_gnubby_auth_handler_.CreateGnubbyConnection(); | |
178 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(ipc_server_channel_name)); | |
179 ASSERT_TRUE( | |
180 remote_security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
181 | |
182 // Establish the IPC channel so we can begin sending and receiving security | |
183 // key messages. | |
184 remote_security_key_ipc_client_.EstablishIpcConnection( | |
185 base::Bind(&RemoteSecurityKeyIpcClientTest::OperationComplete, | |
186 base::Unretained(this), /*failed=*/false), | |
187 base::Bind(&RemoteSecurityKeyIpcClientTest::OperationComplete, | |
188 base::Unretained(this), /*failed=*/true)); | |
189 WaitForOperationComplete(); | |
190 ASSERT_NE(operation_failed_, expect_success); | |
191 } | |
192 | |
193 void RemoteSecurityKeyIpcClientTest::SendRequestAndResponse( | |
194 const std::string& request_data, | |
195 const std::string& response_data) { | |
196 ASSERT_TRUE(remote_security_key_ipc_client_.SendSecurityKeyRequest( | |
197 request_data, | |
198 base::Bind(&RemoteSecurityKeyIpcClientTest::ClientMessageReceived, | |
199 base::Unretained(this)))); | |
200 WaitForOperationComplete(); | |
201 ASSERT_FALSE(operation_failed_); | |
202 ASSERT_EQ(last_connection_id_received_, kTestConnectionId); | |
203 ASSERT_EQ(last_message_received_, request_data); | |
204 | |
205 ASSERT_TRUE(fake_ipc_server_.SendResponse(response_data)); | |
206 WaitForOperationComplete(); | |
207 ASSERT_FALSE(operation_failed_); | |
208 ASSERT_EQ(last_message_received_, response_data); | |
209 } | |
210 | |
211 TEST_F(RemoteSecurityKeyIpcClientTest, GenerateSingleGnubbyRequest) { | |
212 EstablishConnection(/*expect_success=*/true); | |
213 | |
214 SendRequestAndResponse("Auth me!", "You've been authed!"); | |
215 | |
216 remote_security_key_ipc_client_.CloseIpcConnection(); | |
217 } | |
218 | |
219 TEST_F(RemoteSecurityKeyIpcClientTest, GenerateLargeGnubbyRequest) { | |
220 EstablishConnection(/*expect_success=*/true); | |
221 | |
222 SendRequestAndResponse(std::string(kLargeMessageSizeBytes, 'Y'), | |
223 std::string(kLargeMessageSizeBytes, 'Z')); | |
224 | |
225 remote_security_key_ipc_client_.CloseIpcConnection(); | |
226 } | |
227 | |
228 TEST_F(RemoteSecurityKeyIpcClientTest, GenerateReallyLargeGnubbyRequest) { | |
229 EstablishConnection(/*expect_success=*/true); | |
230 | |
231 SendRequestAndResponse(std::string(kLargeMessageSizeBytes * 2, 'Y'), | |
232 std::string(kLargeMessageSizeBytes * 2, 'Z')); | |
233 | |
234 remote_security_key_ipc_client_.CloseIpcConnection(); | |
235 } | |
236 | |
237 TEST_F(RemoteSecurityKeyIpcClientTest, GenerateMultipleGnubbyRequest) { | |
238 EstablishConnection(/*expect_success=*/true); | |
239 | |
240 SendRequestAndResponse("Auth me 1!", "You've been authed once!"); | |
241 SendRequestAndResponse("Auth me 2!", "You've been authed twice!"); | |
242 SendRequestAndResponse("Auth me 3!", "You've been authed thrice!"); | |
243 | |
244 remote_security_key_ipc_client_.CloseIpcConnection(); | |
245 } | |
246 | |
247 TEST_F(RemoteSecurityKeyIpcClientTest, | |
248 ServerClosesConnectionAfterRequestTimeout) { | |
249 EstablishConnection(/*expect_success=*/true); | |
250 fake_ipc_server_.CloseChannel(); | |
251 WaitForOperationComplete(); | |
252 ASSERT_FALSE(operation_failed_); | |
253 } | |
254 | |
255 TEST_F(RemoteSecurityKeyIpcClientTest, | |
256 SecondGnubbyRequestBeforeFirstResponseReceived) { | |
257 EstablishConnection(/*expect_success=*/true); | |
258 | |
259 ASSERT_TRUE(remote_security_key_ipc_client_.SendSecurityKeyRequest( | |
260 "First Request", | |
261 base::Bind(&RemoteSecurityKeyIpcClientTest::ClientMessageReceived, | |
262 base::Unretained(this)))); | |
263 WaitForOperationComplete(); | |
264 ASSERT_FALSE(operation_failed_); | |
265 | |
266 ASSERT_FALSE(remote_security_key_ipc_client_.SendSecurityKeyRequest( | |
267 "Second Request", | |
268 base::Bind(&RemoteSecurityKeyIpcClientTest::ClientMessageReceived, | |
269 base::Unretained(this)))); | |
270 } | |
271 | |
272 TEST_F(RemoteSecurityKeyIpcClientTest, ReceiveGnubbyResponseWithEmptyPayload) { | |
273 EstablishConnection(/*expect_success=*/true); | |
274 | |
275 ASSERT_TRUE(remote_security_key_ipc_client_.SendSecurityKeyRequest( | |
276 "Valid request", | |
277 base::Bind(&RemoteSecurityKeyIpcClientTest::ClientMessageReceived, | |
278 base::Unretained(this)))); | |
279 WaitForOperationComplete(); | |
280 ASSERT_FALSE(operation_failed_); | |
281 | |
282 ASSERT_TRUE(fake_ipc_server_.SendResponse("")); | |
283 WaitForOperationComplete(); | |
284 ASSERT_TRUE(operation_failed_); | |
285 } | |
286 | |
287 TEST_F(RemoteSecurityKeyIpcClientTest, | |
288 SendRequestBeforeEstablishingConnection) { | |
289 // Sending a request will fail since the IPC connection has not been | |
290 // established. | |
291 ASSERT_FALSE(remote_security_key_ipc_client_.SendSecurityKeyRequest( | |
292 "Too soon!!", | |
293 base::Bind(&RemoteSecurityKeyIpcClientTest::ClientMessageReceived, | |
294 base::Unretained(this)))); | |
295 } | |
296 | |
297 TEST_F(RemoteSecurityKeyIpcClientTest, NonExistentMainIpcServerChannel) { | |
298 std::string ipc_server_channel_name(kNonexistentIpcChannelName); | |
299 remote_security_key_ipc_client_.SetInitialIpcChannelNameForTest( | |
300 ipc_server_channel_name); | |
301 | |
302 // Attempt to establish the conection (should fail since the IPC channel does | |
303 // not exist). | |
304 remote_security_key_ipc_client_.EstablishIpcConnection( | |
305 base::Bind(&RemoteSecurityKeyIpcClientTest::OperationComplete, | |
306 base::Unretained(this), /*failed=*/false), | |
307 base::Bind(&RemoteSecurityKeyIpcClientTest::OperationComplete, | |
308 base::Unretained(this), /*failed=*/true)); | |
309 WaitForOperationComplete(); | |
310 ASSERT_TRUE(operation_failed_); | |
311 } | |
312 | |
313 TEST_F(RemoteSecurityKeyIpcClientTest, NonExistentIpcSessionChannel) { | |
314 fake_gnubby_auth_handler_.set_ipc_security_key_channel_name( | |
315 kNonexistentIpcChannelName); | |
316 | |
317 // Set up the channel name for the initial IPC channel. | |
318 std::string ipc_server_channel_name = GenerateUniqueTestChannelName(); | |
319 fake_gnubby_auth_handler_.set_ipc_server_channel_name( | |
320 ipc_server_channel_name); | |
321 remote_security_key_ipc_client_.SetInitialIpcChannelNameForTest( | |
322 ipc_server_channel_name); | |
323 | |
324 // Create the initial IPC channel and verify it was set up correctly. | |
325 ASSERT_FALSE( | |
326 remote_security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
327 fake_gnubby_auth_handler_.CreateGnubbyConnection(); | |
328 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(ipc_server_channel_name)); | |
329 ASSERT_TRUE( | |
330 remote_security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
331 | |
332 // Attempt to establish the conection (should fail since the IPC channel does | |
333 // not exist). | |
334 remote_security_key_ipc_client_.EstablishIpcConnection( | |
335 base::Bind(&RemoteSecurityKeyIpcClientTest::OperationComplete, | |
336 base::Unretained(this), /*failed=*/false), | |
337 base::Bind(&RemoteSecurityKeyIpcClientTest::OperationComplete, | |
338 base::Unretained(this), /*failed=*/true)); | |
339 WaitForOperationComplete(); | |
340 ASSERT_TRUE(operation_failed_); | |
341 } | |
342 | |
343 #if defined(OS_WIN) | |
344 TEST_F(RemoteSecurityKeyIpcClientTest, GnubbyIpcServerRunningInWrongSession) { | |
345 // Set the expected session Id to a different session than we are running in. | |
346 remote_security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest( | |
347 session_id_ + 1); | |
348 | |
349 // Attempting to establish a connection should fail here since the IPC Server | |
350 // is 'running' in a different session than expected. | |
351 EstablishConnection(/*expect_success=*/false); | |
352 } | |
353 #endif // defined(OS_WIN) | |
354 | |
355 } // namespace remoting | |
OLD | NEW |