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/gnubby_auth_handler.h" | |
6 | |
7 #include <cstdint> | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/run_loop.h" | |
16 #include "ipc/ipc_channel.h" | |
17 #include "ipc/ipc_listener.h" | |
18 #include "ipc/ipc_message.h" | |
19 #include "ipc/ipc_message_macros.h" | |
20 #include "remoting/host/host_mock_objects.h" | |
21 #include "remoting/host/security_key/fake_remote_security_key_ipc_client.h" | |
22 #include "remoting/host/security_key/fake_remote_security_key_ipc_server.h" | |
23 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | |
24 #include "testing/gmock/include/gmock/gmock.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | |
26 | |
27 namespace { | |
28 const int kConnectionId1 = 1; | |
29 const int kConnectionId2 = 2; | |
30 } // namespace | |
31 | |
32 namespace remoting { | |
33 | |
34 class GnubbyAuthHandlerWinTest : public testing::Test { | |
35 public: | |
36 GnubbyAuthHandlerWinTest(); | |
37 ~GnubbyAuthHandlerWinTest() override; | |
38 | |
39 // Passed to the object used for testing to be called back to signal | |
40 // completion of an IPC channel state change or reception of an IPC message. | |
41 void OperationComplete(); | |
42 | |
43 protected: | |
44 // Waits until the current |run_loop_| instance is signaled, then resets it. | |
45 void WaitForOperationComplete(); | |
46 | |
47 // Used as a callback given to the object under test, expected to be called | |
48 // back when a security key request is received by it. | |
49 void SendMessageToClient(int connection_id, const std::string& data); | |
50 | |
51 // Creates a new gnubby connection on the object under test. | |
52 void CreateGnubbyConnection(const std::string& channel_name); | |
53 | |
54 // Sets |desktop_session_id_| to the id for the current Windows session. | |
55 void InitializeDesktopSessionId(); | |
56 | |
57 // Uses |fake_ipc_client| to connect to the initial IPC server channel, it | |
58 // then validates internal state of the object under test and closes the | |
59 // connection based on |close_connection|. | |
60 void EstablishInitialIpcConnection( | |
61 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, | |
62 int expected_connection_id, | |
63 const std::string& channel_name, | |
64 bool close_connection); | |
65 | |
66 // Sends a security key response message using |fake_ipc_server| and | |
67 // validates the state of the object under test. | |
68 void SendRequestToGnubbyAuthHandler( | |
69 const base::WeakPtr<FakeRemoteSecurityKeyIpcServer>& fake_ipc_server, | |
70 int connection_id, | |
71 const std::string& request_payload); | |
72 | |
73 // Sends a security key response message to |fake_ipc_server| and validates | |
74 // the state of the object under test. | |
75 void SendResponseViaGnubbyAuthHandler( | |
76 const base::WeakPtr<FakeRemoteSecurityKeyIpcServer>& fake_ipc_server, | |
77 int connection_id, | |
78 const std::string& response_payload); | |
79 | |
80 // Closes a security key session IPC channel and validates state. | |
81 void CloseSecurityKeySessionIpcChannel( | |
82 const base::WeakPtr<FakeRemoteSecurityKeyIpcServer>& fake_ipc_server, | |
83 int connection_id); | |
84 | |
85 // Returns a unique IPC channel name which prevents conflicts when running | |
86 // tests concurrently. | |
87 std::string GetUniqueTestChannelName(); | |
88 | |
89 // IPC tests require a valid MessageLoop to run. | |
90 base::MessageLoopForIO message_loop_; | |
91 | |
92 // Used to allow |message_loop_| to run during tests. The instance is reset | |
93 // after each stage of the tests has been completed. | |
94 std::unique_ptr<base::RunLoop> run_loop_; | |
95 | |
96 // The object under test. | |
97 std::unique_ptr<GnubbyAuthHandler> auth_handler_; | |
98 | |
99 // Set as the default factory to create RemoteSecurityKeyIpcServerFactory | |
100 // instances, this class will track each objects creation and allow the tests | |
101 // to access it and use it for driving tests and validating state. | |
102 FakeRemoteSecurityKeyIpcServerFactory ipc_server_factory_; | |
103 | |
104 // Used to validate the object under test uses the correct ID when | |
105 // communicating over the IPC channel. | |
106 int last_connection_id_received_ = -1; | |
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 | |
112 // Stores the contents of the last IPC message received for validation. | |
113 std::string last_message_received_; | |
114 | |
115 private: | |
116 testing::NiceMock<MockClientSessionDetails> mock_client_session_details_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerWinTest); | |
119 }; | |
120 | |
121 GnubbyAuthHandlerWinTest::GnubbyAuthHandlerWinTest() | |
122 : run_loop_(new base::RunLoop()) { | |
123 auth_handler_ = remoting::GnubbyAuthHandler::Create( | |
124 &mock_client_session_details_, | |
125 base::Bind(&GnubbyAuthHandlerWinTest::SendMessageToClient, | |
126 base::Unretained(this))); | |
127 } | |
128 | |
129 GnubbyAuthHandlerWinTest::~GnubbyAuthHandlerWinTest() {} | |
130 | |
131 void GnubbyAuthHandlerWinTest::OperationComplete() { | |
132 run_loop_->Quit(); | |
133 } | |
134 | |
135 void GnubbyAuthHandlerWinTest::WaitForOperationComplete() { | |
136 run_loop_->Run(); | |
137 run_loop_.reset(new base::RunLoop()); | |
138 } | |
139 | |
140 void GnubbyAuthHandlerWinTest::SendMessageToClient(int connection_id, | |
141 const std::string& data) { | |
142 last_connection_id_received_ = connection_id; | |
143 last_message_received_ = data; | |
144 OperationComplete(); | |
145 } | |
146 | |
147 void GnubbyAuthHandlerWinTest::CreateGnubbyConnection( | |
148 const std::string& channel_name) { | |
149 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | |
150 | |
151 remoting::SetRemoteSecurityKeyIpcChannelNameForTest(channel_name); | |
152 | |
153 // Create a new Gnubby IPC Server connection. | |
154 auth_handler_->CreateGnubbyConnection(); | |
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_)); | |
166 } | |
167 | |
168 void GnubbyAuthHandlerWinTest::EstablishInitialIpcConnection( | |
169 FakeRemoteSecurityKeyIpcClient* fake_ipc_client, | |
170 int expected_connection_id, | |
171 const std::string& channel_name, | |
172 bool close_connection) { | |
173 size_t expected_connection_count = | |
174 auth_handler_->GetActiveConnectionCountForTest() + 1; | |
175 | |
176 ASSERT_TRUE(fake_ipc_client->ConnectViaIpc(channel_name)); | |
177 // Client and Server will each signal us once when OnChannelConenect() is | |
178 // called so we wait on complete twice. The order in which each is signaled | |
179 // is not important. | |
180 WaitForOperationComplete(); | |
181 WaitForOperationComplete(); | |
182 | |
183 // Verify the connection details have been passed to the client. | |
184 std::string new_channel_name = fake_ipc_client->last_message_received(); | |
185 ASSERT_FALSE(new_channel_name.empty()); | |
186 | |
187 // Verify the internal state of the GnubbyAuthHandler is correct. | |
188 ASSERT_TRUE(auth_handler_->IsValidConnectionId(expected_connection_id)); | |
189 ASSERT_EQ(expected_connection_count, | |
190 auth_handler_->GetActiveConnectionCountForTest()); | |
191 | |
192 if (close_connection) { | |
193 fake_ipc_client->CloseIpcConnection(); | |
194 WaitForOperationComplete(); | |
195 } | |
196 } | |
197 | |
198 void GnubbyAuthHandlerWinTest::SendRequestToGnubbyAuthHandler( | |
199 const base::WeakPtr<FakeRemoteSecurityKeyIpcServer>& fake_ipc_server, | |
200 int connection_id, | |
201 const std::string& request_payload) { | |
202 size_t expected_connection_count = | |
203 auth_handler_->GetActiveConnectionCountForTest(); | |
204 // Send a gnubby request using the fake IPC server. | |
205 fake_ipc_server->SendRequest(request_payload); | |
206 WaitForOperationComplete(); | |
207 | |
208 // Verify the FakeRemoteSecurityKeyIpcServer instance was not destroyed. | |
209 ASSERT_TRUE(fake_ipc_server.get()); | |
210 | |
211 // Verify the request was received. | |
212 ASSERT_EQ(connection_id, last_connection_id_received_); | |
213 ASSERT_EQ(request_payload, last_message_received_); | |
214 | |
215 // Verify the internal state of the GnubbyAuthHandler is still correct. | |
216 ASSERT_TRUE(auth_handler_->IsValidConnectionId(connection_id)); | |
217 ASSERT_EQ(expected_connection_count, | |
218 auth_handler_->GetActiveConnectionCountForTest()); | |
219 } | |
220 | |
221 void GnubbyAuthHandlerWinTest::SendResponseViaGnubbyAuthHandler( | |
222 const base::WeakPtr<FakeRemoteSecurityKeyIpcServer>& fake_ipc_server, | |
223 int connection_id, | |
224 const std::string& response_payload) { | |
225 size_t expected_connection_count = | |
226 auth_handler_->GetActiveConnectionCountForTest(); | |
227 | |
228 // Send a gnubby response using the new IPC channel. | |
229 auth_handler_->SendClientResponse(connection_id, response_payload); | |
230 WaitForOperationComplete(); | |
231 | |
232 // Verify the gnubby response was received. | |
233 ASSERT_EQ(response_payload, fake_ipc_server->last_message_received()); | |
234 | |
235 // Verify the internal state of the GnubbyAuthHandler is still correct. | |
236 ASSERT_TRUE(auth_handler_->IsValidConnectionId(connection_id)); | |
237 ASSERT_EQ(expected_connection_count, | |
238 auth_handler_->GetActiveConnectionCountForTest()); | |
239 } | |
240 | |
241 void GnubbyAuthHandlerWinTest::CloseSecurityKeySessionIpcChannel( | |
242 const base::WeakPtr<FakeRemoteSecurityKeyIpcServer>& fake_ipc_server, | |
243 int connection_id) { | |
244 size_t expected_connection_count = | |
245 auth_handler_->GetActiveConnectionCountForTest() - 1; | |
246 | |
247 fake_ipc_server->CloseChannel(); | |
248 | |
249 // Verify the internal state has been updated. | |
250 ASSERT_FALSE(auth_handler_->IsValidConnectionId(connection_id)); | |
251 ASSERT_EQ(expected_connection_count, | |
252 auth_handler_->GetActiveConnectionCountForTest()); | |
253 | |
254 // Verify the FakeRemoteSecurityKeyIpcServer instance was destroyed. | |
255 ASSERT_FALSE(fake_ipc_server.get()); | |
256 } | |
257 | |
258 std::string GnubbyAuthHandlerWinTest::GetUniqueTestChannelName() { | |
259 std::string channel_name("Uber_Awesome_Super_Mega_Test_Channel."); | |
260 channel_name.append(IPC::Channel::GenerateUniqueRandomChannelID()); | |
261 | |
262 return channel_name; | |
263 } | |
264 | |
265 TEST_F(GnubbyAuthHandlerWinTest, HandleSingleGnubbyRequest) { | |
266 std::string channel_name(GetUniqueTestChannelName()); | |
267 CreateGnubbyConnection(channel_name); | |
268 | |
269 // Create a fake client and connect to the IPC server channel. | |
270 FakeRemoteSecurityKeyIpcClient fake_ipc_client(base::Bind( | |
271 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
272 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
273 /*close_connection=*/true); | |
274 | |
275 // Connect to the private IPC server channel created for this client. | |
276 std::string new_channel_name = fake_ipc_client.last_message_received(); | |
277 | |
278 // Retrieve the IPC server instance created when the client connected. | |
279 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> fake_ipc_server = | |
280 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
281 ASSERT_TRUE(fake_ipc_server.get()); | |
282 ASSERT_EQ(new_channel_name, fake_ipc_server->channel_name()); | |
283 | |
284 fake_ipc_server->set_send_response_callback(base::Bind( | |
285 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
286 | |
287 // Send a gnubby request using the fake IPC server. | |
288 SendRequestToGnubbyAuthHandler(fake_ipc_server, kConnectionId1, "0123456789"); | |
289 | |
290 // Send a gnubby response using the new IPC channel. | |
291 SendResponseViaGnubbyAuthHandler(fake_ipc_server, kConnectionId1, | |
292 "9876543210"); | |
293 | |
294 CloseSecurityKeySessionIpcChannel(fake_ipc_server, kConnectionId1); | |
295 } | |
296 | |
297 TEST_F(GnubbyAuthHandlerWinTest, HandleConcurrentGnubbyRequests) { | |
298 std::string channel_name(GetUniqueTestChannelName()); | |
299 CreateGnubbyConnection(channel_name); | |
300 | |
301 // Create fake clients and connect each to the IPC server channel. | |
302 FakeRemoteSecurityKeyIpcClient fake_ipc_client_1(base::Bind( | |
303 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
304 FakeRemoteSecurityKeyIpcClient fake_ipc_client_2(base::Bind( | |
305 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
306 | |
307 EstablishInitialIpcConnection(&fake_ipc_client_1, kConnectionId1, | |
308 channel_name, | |
309 /*close_connection=*/true); | |
310 EstablishInitialIpcConnection(&fake_ipc_client_2, kConnectionId2, | |
311 channel_name, | |
312 /*close_connection=*/true); | |
313 | |
314 // Verify the connection details have been passed to the client. | |
315 std::string channel_name_1 = fake_ipc_client_1.last_message_received(); | |
316 std::string channel_name_2 = fake_ipc_client_2.last_message_received(); | |
317 ASSERT_NE(channel_name_1, channel_name_2); | |
318 | |
319 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> fake_ipc_server_1 = | |
320 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
321 ASSERT_TRUE(fake_ipc_server_1.get()); | |
322 ASSERT_EQ(channel_name_1, fake_ipc_server_1->channel_name()); | |
323 | |
324 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> fake_ipc_server_2 = | |
325 ipc_server_factory_.GetIpcServerObject(kConnectionId2); | |
326 ASSERT_TRUE(fake_ipc_server_2.get()); | |
327 ASSERT_EQ(channel_name_2, fake_ipc_server_2->channel_name()); | |
328 | |
329 fake_ipc_server_1->set_send_response_callback(base::Bind( | |
330 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
331 fake_ipc_server_2->set_send_response_callback(base::Bind( | |
332 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
333 | |
334 // Connect and send a gnubby request using the first IPC channel. | |
335 SendRequestToGnubbyAuthHandler(fake_ipc_server_1, kConnectionId1, | |
336 "aaaaaaaaaa"); | |
337 | |
338 // Send a gnubby request using the second IPC channel. | |
339 SendRequestToGnubbyAuthHandler(fake_ipc_server_1, kConnectionId1, | |
340 "bbbbbbbbbb"); | |
341 | |
342 // Send a gnubby response using the first IPC channel. | |
343 SendResponseViaGnubbyAuthHandler(fake_ipc_server_2, kConnectionId2, | |
344 "cccccccccc"); | |
345 | |
346 // Send a gnubby response using the second IPC channel. | |
347 SendResponseViaGnubbyAuthHandler(fake_ipc_server_2, kConnectionId2, | |
348 "dddddddddd"); | |
349 | |
350 // Close the IPC channels. | |
351 CloseSecurityKeySessionIpcChannel(fake_ipc_server_1, kConnectionId1); | |
352 CloseSecurityKeySessionIpcChannel(fake_ipc_server_2, kConnectionId2); | |
353 } | |
354 | |
355 TEST_F(GnubbyAuthHandlerWinTest, HandleSequentialGnubbyRequests) { | |
356 std::string channel_name(GetUniqueTestChannelName()); | |
357 CreateGnubbyConnection(channel_name); | |
358 | |
359 // Create fake clients to connect to the IPC server channel. | |
360 FakeRemoteSecurityKeyIpcClient fake_ipc_client_1(base::Bind( | |
361 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
362 | |
363 EstablishInitialIpcConnection(&fake_ipc_client_1, kConnectionId1, | |
364 channel_name, | |
365 /*close_connection=*/true); | |
366 | |
367 // Verify the connection details have been passed to the client. | |
368 std::string channel_name_1 = fake_ipc_client_1.last_message_received(); | |
369 | |
370 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> fake_ipc_server_1 = | |
371 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
372 ASSERT_TRUE(fake_ipc_server_1.get()); | |
373 ASSERT_EQ(channel_name_1, fake_ipc_server_1->channel_name()); | |
374 | |
375 fake_ipc_server_1->set_send_response_callback(base::Bind( | |
376 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
377 | |
378 // Send a gnubby request using the first IPC channel. | |
379 SendRequestToGnubbyAuthHandler(fake_ipc_server_1, kConnectionId1, | |
380 "aaaaaaaaaa"); | |
381 | |
382 // Send a gnubby response using the first IPC channel. | |
383 SendResponseViaGnubbyAuthHandler(fake_ipc_server_1, kConnectionId1, | |
384 "cccccccccc"); | |
385 | |
386 // Close the IPC channel. | |
387 CloseSecurityKeySessionIpcChannel(fake_ipc_server_1, kConnectionId1); | |
388 | |
389 // Now connect with a second client. | |
390 FakeRemoteSecurityKeyIpcClient fake_ipc_client_2(base::Bind( | |
391 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
392 EstablishInitialIpcConnection(&fake_ipc_client_2, kConnectionId2, | |
393 channel_name, | |
394 /*close_connection=*/true); | |
395 | |
396 std::string channel_name_2 = fake_ipc_client_2.last_message_received(); | |
397 ASSERT_NE(channel_name_1, channel_name_2); | |
398 | |
399 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> fake_ipc_server_2 = | |
400 ipc_server_factory_.GetIpcServerObject(kConnectionId2); | |
401 ASSERT_TRUE(fake_ipc_server_2.get()); | |
402 ASSERT_EQ(channel_name_2, fake_ipc_server_2->channel_name()); | |
403 | |
404 fake_ipc_server_2->set_send_response_callback(base::Bind( | |
405 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
406 | |
407 // Send a gnubby request using the second IPC channel. | |
408 SendRequestToGnubbyAuthHandler(fake_ipc_server_2, kConnectionId2, | |
409 "bbbbbbbbbb"); | |
410 | |
411 // Send a gnubby response using the second IPC channel. | |
412 SendResponseViaGnubbyAuthHandler(fake_ipc_server_2, kConnectionId2, | |
413 "dddddddddd"); | |
414 | |
415 // Close the IPC channel. | |
416 CloseSecurityKeySessionIpcChannel(fake_ipc_server_2, kConnectionId2); | |
417 } | |
418 | |
419 TEST_F(GnubbyAuthHandlerWinTest, ClientNeverDisconnectsFromInitialIpcChannel) { | |
420 const int kLowConnectionTimeoutInMs = 25; | |
421 auth_handler_->SetRequestTimeoutForTest( | |
422 base::TimeDelta::FromMilliseconds(kLowConnectionTimeoutInMs)); | |
423 | |
424 std::string channel_name(GetUniqueTestChannelName()); | |
425 CreateGnubbyConnection(channel_name); | |
426 | |
427 // Create a fake client and connect to the IPC server channel. | |
428 FakeRemoteSecurityKeyIpcClient fake_ipc_client(base::Bind( | |
429 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
430 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
431 /*close_connection=*/false); | |
432 | |
433 // Don't close the channel here, instead wait for the GnubbyAuthHandler to | |
434 // close the connection due to the timeout. | |
435 WaitForOperationComplete(); | |
436 | |
437 // Verify the connection that was set up still exists. | |
438 ASSERT_TRUE(auth_handler_->IsValidConnectionId(kConnectionId1)); | |
439 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); | |
440 | |
441 // Attempt to connect again after the error. | |
442 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | |
443 /*close_connection=*/true); | |
444 } | |
445 | |
446 TEST_F(GnubbyAuthHandlerWinTest, HandleGnubbyRequestTimeout) { | |
447 std::string channel_name(GetUniqueTestChannelName()); | |
448 CreateGnubbyConnection(channel_name); | |
449 | |
450 // Create a fake client and connect to the IPC server channel. | |
451 FakeRemoteSecurityKeyIpcClient fake_ipc_client(base::Bind( | |
452 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
453 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
454 /*close_connection=*/true); | |
455 | |
456 // Connect to the private IPC server channel created for this client. | |
457 std::string new_channel_name = fake_ipc_client.last_message_received(); | |
458 | |
459 // Retrieve the IPC server instance created when the client connected. | |
460 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> fake_ipc_server = | |
461 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
462 ASSERT_TRUE(fake_ipc_server.get()); | |
463 ASSERT_EQ(new_channel_name, fake_ipc_server->channel_name()); | |
464 | |
465 fake_ipc_server->set_send_response_callback(base::Bind( | |
466 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
467 | |
468 // Simulate a timeout and verify the IPC server is cleaned up. | |
469 CloseSecurityKeySessionIpcChannel(fake_ipc_server, kConnectionId1); | |
470 | |
471 // Attempt to connect again after the error. | |
472 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | |
473 /*close_connection=*/true); | |
474 } | |
475 | |
476 TEST_F(GnubbyAuthHandlerWinTest, HandleGnubbyErrorResponse) { | |
477 std::string channel_name(GetUniqueTestChannelName()); | |
478 CreateGnubbyConnection(channel_name); | |
479 | |
480 // Create a fake client and connect to the IPC server channel. | |
481 FakeRemoteSecurityKeyIpcClient fake_ipc_client(base::Bind( | |
482 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
483 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
484 /*close_connection=*/true); | |
485 | |
486 // Connect to the private IPC server channel created for this client. | |
487 std::string new_channel_name = fake_ipc_client.last_message_received(); | |
488 | |
489 // Retrieve the IPC server instance created when the client connected. | |
490 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> fake_ipc_server = | |
491 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
492 ASSERT_TRUE(fake_ipc_server.get()); | |
493 ASSERT_EQ(new_channel_name, fake_ipc_server->channel_name()); | |
494 | |
495 fake_ipc_server->set_send_response_callback(base::Bind( | |
496 &GnubbyAuthHandlerWinTest::OperationComplete, base::Unretained(this))); | |
497 | |
498 // Send a gnubby request using the fake IPC server. | |
499 SendRequestToGnubbyAuthHandler(fake_ipc_server, kConnectionId1, "0123456789"); | |
500 | |
501 // Simulate a gnubby error from the client. | |
502 auth_handler_->SendErrorAndCloseConnection(kConnectionId1); | |
503 // Wait for the ipc server channel to be torn down. | |
504 WaitForOperationComplete(); | |
505 | |
506 // Verify the connection was cleaned up. | |
507 ASSERT_FALSE(fake_ipc_server.get()); | |
508 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); | |
509 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | |
510 | |
511 // Attempt to connect again after the error. | |
512 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | |
513 /*close_connection=*/true); | |
514 } | |
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 | |
535 } // namespace remoting | |
OLD | NEW |