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_auth_handler.h" | 5 #include "remoting/host/security_key/security_key_auth_handler.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.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 "ipc/ipc_listener.h" | 17 #include "ipc/ipc_listener.h" |
18 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
19 #include "ipc/ipc_message_macros.h" | 19 #include "ipc/ipc_message_macros.h" |
20 #include "mojo/edk/test/scoped_ipc_support.h" | |
20 #include "remoting/host/host_mock_objects.h" | 21 #include "remoting/host/host_mock_objects.h" |
21 #include "remoting/host/security_key/fake_security_key_ipc_client.h" | 22 #include "remoting/host/security_key/fake_security_key_ipc_client.h" |
22 #include "remoting/host/security_key/fake_security_key_ipc_server.h" | 23 #include "remoting/host/security_key/fake_security_key_ipc_server.h" |
23 #include "remoting/host/security_key/security_key_ipc_constants.h" | 24 #include "remoting/host/security_key/security_key_ipc_constants.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 namespace { | 28 namespace { |
28 const int kConnectionId1 = 1; | 29 const int kConnectionId1 = 1; |
29 const int kConnectionId2 = 2; | 30 const int kConnectionId2 = 2; |
(...skipping 12 matching lines...) Expand all Loading... | |
42 | 43 |
43 protected: | 44 protected: |
44 // Waits until the current |run_loop_| instance is signaled, then resets it. | 45 // Waits until the current |run_loop_| instance is signaled, then resets it. |
45 void WaitForOperationComplete(); | 46 void WaitForOperationComplete(); |
46 | 47 |
47 // Used as a callback given to the object under test, expected to be called | 48 // 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 // back when a security key request is received by it. |
49 void SendMessageToClient(int connection_id, const std::string& data); | 50 void SendMessageToClient(int connection_id, const std::string& data); |
50 | 51 |
51 // Creates a new security key connection on the object under test. | 52 // Creates a new security key connection on the object under test. |
52 void CreateSecurityKeyConnection(const std::string& channel_name); | 53 void CreateSecurityKeyConnection( |
54 const mojo::edk::NamedPlatformHandle& channel_handle); | |
53 | 55 |
54 // Sets |desktop_session_id_| to the id for the current Windows session. | 56 // Sets |desktop_session_id_| to the id for the current Windows session. |
55 void InitializeDesktopSessionId(); | 57 void InitializeDesktopSessionId(); |
56 | 58 |
57 // Uses |fake_ipc_client| to connect to the initial IPC server channel, it | 59 // Uses |fake_ipc_client| to connect to the IPC server channel, it then |
58 // then validates internal state of the object under test and closes the | 60 // validates internal state of the object under test and closes the connection |
59 // connection based on |close_connection|. | 61 // based on |close_connection|. |
60 void EstablishInitialIpcConnection(FakeSecurityKeyIpcClient* fake_ipc_client, | 62 void EstablishIpcConnection( |
61 int expected_connection_id, | 63 FakeSecurityKeyIpcClient* fake_ipc_client, |
62 const std::string& channel_name, | 64 int expected_connection_id, |
63 bool close_connection); | 65 const mojo::edk::NamedPlatformHandle& channel_handle, |
66 bool close_connection); | |
64 | 67 |
65 // Sends a security key response message using |fake_ipc_server| and | 68 // Sends a security key response message using |fake_ipc_server| and |
66 // validates the state of the object under test. | 69 // validates the state of the object under test. |
67 void SendRequestToSecurityKeyAuthHandler( | 70 void SendRequestToSecurityKeyAuthHandler( |
68 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, | 71 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, |
69 int connection_id, | 72 int connection_id, |
70 const std::string& request_payload); | 73 const std::string& request_payload); |
71 | 74 |
72 // Sends a security key response message to |fake_ipc_server| and validates | 75 // Sends a security key response message to |fake_ipc_server| and validates |
73 // the state of the object under test. | 76 // the state of the object under test. |
74 void SendResponseViaSecurityKeyAuthHandler( | 77 void SendResponseViaSecurityKeyAuthHandler( |
75 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, | 78 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, |
76 int connection_id, | 79 int connection_id, |
77 const std::string& response_payload); | 80 const std::string& response_payload); |
78 | 81 |
79 // Closes a security key session IPC channel and validates state. | 82 // Closes a security key session IPC channel and validates state. |
80 void CloseSecurityKeySessionIpcChannel( | 83 void CloseSecurityKeySessionIpcChannel( |
81 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, | 84 const base::WeakPtr<FakeSecurityKeyIpcServer>& fake_ipc_server, |
82 int connection_id); | 85 int connection_id); |
83 | 86 |
84 // Returns a unique IPC channel name which prevents conflicts when running | 87 // Returns a unique IPC channel handle which prevents conflicts when running |
85 // tests concurrently. | 88 // tests concurrently. |
86 std::string GetUniqueTestChannelName(); | 89 std::string GetUniqueTestChannelHandle(); |
87 | 90 |
88 // IPC tests require a valid MessageLoop to run. | 91 // IPC tests require a valid MessageLoop to run. |
89 base::MessageLoopForIO message_loop_; | 92 base::MessageLoopForIO message_loop_; |
90 | 93 |
94 mojo::edk::test::ScopedIPCSupport ipc_support_; | |
95 | |
91 // Used to allow |message_loop_| to run during tests. The instance is reset | 96 // Used to allow |message_loop_| to run during tests. The instance is reset |
92 // after each stage of the tests has been completed. | 97 // after each stage of the tests has been completed. |
93 std::unique_ptr<base::RunLoop> run_loop_; | 98 std::unique_ptr<base::RunLoop> run_loop_; |
94 | 99 |
95 // The object under test. | 100 // The object under test. |
96 std::unique_ptr<SecurityKeyAuthHandler> auth_handler_; | 101 std::unique_ptr<SecurityKeyAuthHandler> auth_handler_; |
97 | 102 |
98 // Set as the default factory to create SecurityKeyIpcServerFactory | 103 // Set as the default factory to create SecurityKeyIpcServerFactory |
99 // instances, this class will track each objects creation and allow the tests | 104 // instances, this class will track each objects creation and allow the tests |
100 // to access it and use it for driving tests and validating state. | 105 // to access it and use it for driving tests and validating state. |
(...skipping 10 matching lines...) Expand all Loading... | |
111 // Stores the contents of the last IPC message received for validation. | 116 // Stores the contents of the last IPC message received for validation. |
112 std::string last_message_received_; | 117 std::string last_message_received_; |
113 | 118 |
114 private: | 119 private: |
115 testing::NiceMock<MockClientSessionDetails> mock_client_session_details_; | 120 testing::NiceMock<MockClientSessionDetails> mock_client_session_details_; |
116 | 121 |
117 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerWinTest); | 122 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerWinTest); |
118 }; | 123 }; |
119 | 124 |
120 SecurityKeyAuthHandlerWinTest::SecurityKeyAuthHandlerWinTest() | 125 SecurityKeyAuthHandlerWinTest::SecurityKeyAuthHandlerWinTest() |
121 : run_loop_(new base::RunLoop()) { | 126 : ipc_support_(message_loop_.task_runner()), |
127 run_loop_(new base::RunLoop()) { | |
122 auth_handler_ = remoting::SecurityKeyAuthHandler::Create( | 128 auth_handler_ = remoting::SecurityKeyAuthHandler::Create( |
123 &mock_client_session_details_, | 129 &mock_client_session_details_, |
124 base::Bind(&SecurityKeyAuthHandlerWinTest::SendMessageToClient, | 130 base::Bind(&SecurityKeyAuthHandlerWinTest::SendMessageToClient, |
125 base::Unretained(this)), | 131 base::Unretained(this)), |
126 /*file_task_runner=*/nullptr); | 132 /*file_task_runner=*/nullptr); |
127 } | 133 } |
128 | 134 |
129 SecurityKeyAuthHandlerWinTest::~SecurityKeyAuthHandlerWinTest() {} | 135 SecurityKeyAuthHandlerWinTest::~SecurityKeyAuthHandlerWinTest() {} |
130 | 136 |
131 void SecurityKeyAuthHandlerWinTest::OperationComplete() { | 137 void SecurityKeyAuthHandlerWinTest::OperationComplete() { |
132 run_loop_->Quit(); | 138 run_loop_->Quit(); |
133 } | 139 } |
134 | 140 |
135 void SecurityKeyAuthHandlerWinTest::WaitForOperationComplete() { | 141 void SecurityKeyAuthHandlerWinTest::WaitForOperationComplete() { |
136 run_loop_->Run(); | 142 run_loop_->Run(); |
137 run_loop_.reset(new base::RunLoop()); | 143 run_loop_.reset(new base::RunLoop()); |
138 } | 144 } |
139 | 145 |
140 void SecurityKeyAuthHandlerWinTest::SendMessageToClient( | 146 void SecurityKeyAuthHandlerWinTest::SendMessageToClient( |
141 int connection_id, | 147 int connection_id, |
142 const std::string& data) { | 148 const std::string& data) { |
143 last_connection_id_received_ = connection_id; | 149 last_connection_id_received_ = connection_id; |
144 last_message_received_ = data; | 150 last_message_received_ = data; |
145 OperationComplete(); | 151 OperationComplete(); |
146 } | 152 } |
147 | 153 |
148 void SecurityKeyAuthHandlerWinTest::CreateSecurityKeyConnection( | 154 void SecurityKeyAuthHandlerWinTest::CreateSecurityKeyConnection( |
149 const std::string& channel_name) { | 155 const mojo::edk::NamedPlatformHandle& channel_handle) { |
150 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 156 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
151 | 157 |
152 remoting::SetSecurityKeyIpcChannelNameForTest(channel_name); | 158 remoting::SetSecurityKeyIpcChannelForTest(channel_handle); |
153 | 159 |
154 // Create a new SecurityKey IPC Server connection. | 160 // Create a new SecurityKey IPC Server connection. |
155 auth_handler_->CreateSecurityKeyConnection(); | 161 auth_handler_->CreateSecurityKeyConnection(); |
156 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(channel_name)); | |
157 | 162 |
158 InitializeDesktopSessionId(); | 163 InitializeDesktopSessionId(); |
159 } | 164 } |
160 | 165 |
161 void SecurityKeyAuthHandlerWinTest::InitializeDesktopSessionId() { | 166 void SecurityKeyAuthHandlerWinTest::InitializeDesktopSessionId() { |
162 ASSERT_TRUE( | 167 ASSERT_TRUE( |
163 ProcessIdToSessionId(GetCurrentProcessId(), &desktop_session_id_)); | 168 ProcessIdToSessionId(GetCurrentProcessId(), &desktop_session_id_)); |
164 | 169 |
165 ON_CALL(mock_client_session_details_, desktop_session_id()) | 170 ON_CALL(mock_client_session_details_, desktop_session_id()) |
166 .WillByDefault(testing::Return(desktop_session_id_)); | 171 .WillByDefault(testing::Return(desktop_session_id_)); |
167 } | 172 } |
168 | 173 |
169 void SecurityKeyAuthHandlerWinTest::EstablishInitialIpcConnection( | 174 void SecurityKeyAuthHandlerWinTest::EstablishIpcConnection( |
170 FakeSecurityKeyIpcClient* fake_ipc_client, | 175 FakeSecurityKeyIpcClient* fake_ipc_client, |
171 int expected_connection_id, | 176 int expected_connection_id, |
172 const std::string& channel_name, | 177 const mojo::edk::NamedPlatformHandle& channel_handle, |
173 bool close_connection) { | 178 bool close_connection) { |
174 size_t expected_connection_count = | 179 size_t expected_connection_count = |
175 auth_handler_->GetActiveConnectionCountForTest() + 1; | 180 auth_handler_->GetActiveConnectionCountForTest() + 1; |
176 | 181 |
177 ASSERT_TRUE(fake_ipc_client->ConnectViaIpc(channel_name)); | 182 ASSERT_FALSE(auth_handler_->IsValidConnectionId(expected_connection_id)); |
178 // Client and Server will each signal us once when OnChannelConenect() is | 183 ASSERT_TRUE(fake_ipc_client->ConnectViaIpc(channel_handle)); |
179 // called so we wait on complete twice. The order in which each is signaled | |
180 // is not important. | |
181 WaitForOperationComplete(); | 184 WaitForOperationComplete(); |
182 WaitForOperationComplete(); | |
183 | |
184 // Verify the connection details have been passed to the client. | |
185 std::string new_channel_name = fake_ipc_client->last_message_received(); | |
186 ASSERT_FALSE(new_channel_name.empty()); | |
187 | 185 |
188 // Verify the internal state of the SecurityKeyAuthHandler is correct. | 186 // Verify the internal state of the SecurityKeyAuthHandler is correct. |
189 ASSERT_TRUE(auth_handler_->IsValidConnectionId(expected_connection_id)); | 187 ASSERT_TRUE(auth_handler_->IsValidConnectionId(expected_connection_id)); |
190 ASSERT_EQ(expected_connection_count, | 188 ASSERT_EQ(expected_connection_count, |
191 auth_handler_->GetActiveConnectionCountForTest()); | 189 auth_handler_->GetActiveConnectionCountForTest()); |
192 | 190 |
193 if (close_connection) { | 191 if (close_connection) { |
194 fake_ipc_client->CloseIpcConnection(); | 192 fake_ipc_client->CloseIpcConnection(); |
195 WaitForOperationComplete(); | 193 WaitForOperationComplete(); |
196 } | 194 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 | 247 |
250 // Verify the internal state has been updated. | 248 // Verify the internal state has been updated. |
251 ASSERT_FALSE(auth_handler_->IsValidConnectionId(connection_id)); | 249 ASSERT_FALSE(auth_handler_->IsValidConnectionId(connection_id)); |
252 ASSERT_EQ(expected_connection_count, | 250 ASSERT_EQ(expected_connection_count, |
253 auth_handler_->GetActiveConnectionCountForTest()); | 251 auth_handler_->GetActiveConnectionCountForTest()); |
254 | 252 |
255 // Verify the FakeSecurityKeyIpcServer instance was destroyed. | 253 // Verify the FakeSecurityKeyIpcServer instance was destroyed. |
256 ASSERT_FALSE(fake_ipc_server.get()); | 254 ASSERT_FALSE(fake_ipc_server.get()); |
257 } | 255 } |
258 | 256 |
259 std::string SecurityKeyAuthHandlerWinTest::GetUniqueTestChannelName() { | 257 std::string SecurityKeyAuthHandlerWinTest::GetUniqueTestChannelHandle() { |
260 std::string channel_name("Uber_Awesome_Super_Mega_Test_Channel."); | 258 std::string channel_name("Uber_Awesome_Super_Mega_Test_Channel."); |
261 channel_name.append(IPC::Channel::GenerateUniqueRandomChannelID()); | 259 channel_name.append(IPC::Channel::GenerateUniqueRandomChannelID()); |
262 | 260 |
263 return channel_name; | 261 return channel_name; |
264 } | 262 } |
265 | 263 |
266 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSingleSecurityKeyRequest) { | 264 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSingleSecurityKeyRequest) { |
267 std::string channel_name(GetUniqueTestChannelName()); | 265 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelHandle()); |
268 CreateSecurityKeyConnection(channel_name); | 266 CreateSecurityKeyConnection(channel_handle); |
267 | |
268 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); | |
269 | 269 |
270 // Create a fake client and connect to the IPC server channel. | 270 // Create a fake client and connect to the IPC server channel. |
271 FakeSecurityKeyIpcClient fake_ipc_client( | 271 FakeSecurityKeyIpcClient fake_ipc_client( |
272 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 272 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
273 base::Unretained(this))); | 273 base::Unretained(this))); |
274 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | 274 EstablishIpcConnection(&fake_ipc_client, kConnectionId1, channel_handle, |
275 /*close_connection=*/true); | 275 /*close_connection=*/true); |
276 | |
277 // Connect to the private IPC server channel created for this client. | |
278 std::string new_channel_name = fake_ipc_client.last_message_received(); | |
279 | 276 |
280 // Retrieve the IPC server instance created when the client connected. | 277 // Retrieve the IPC server instance created when the client connected. |
281 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server = | 278 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server = |
282 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | 279 ipc_server_factory_.GetIpcServerObject(kConnectionId1); |
283 ASSERT_TRUE(fake_ipc_server.get()); | 280 ASSERT_TRUE(fake_ipc_server.get()); |
284 ASSERT_EQ(new_channel_name, fake_ipc_server->channel_name()); | |
285 | 281 |
286 fake_ipc_server->set_send_response_callback( | 282 fake_ipc_server->set_send_response_callback( |
287 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 283 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
288 base::Unretained(this))); | 284 base::Unretained(this))); |
289 | 285 |
290 // Send a security key request using the fake IPC server. | 286 // Send a security key request using the fake IPC server. |
291 SendRequestToSecurityKeyAuthHandler(fake_ipc_server, kConnectionId1, | 287 SendRequestToSecurityKeyAuthHandler(fake_ipc_server, kConnectionId1, |
292 "0123456789"); | 288 "0123456789"); |
293 | 289 |
294 // Send a security key response using the new IPC channel. | 290 // Send a security key response using the new IPC channel. |
295 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server, kConnectionId1, | 291 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server, kConnectionId1, |
296 "9876543210"); | 292 "9876543210"); |
297 | 293 |
298 CloseSecurityKeySessionIpcChannel(fake_ipc_server, kConnectionId1); | 294 CloseSecurityKeySessionIpcChannel(fake_ipc_server, kConnectionId1); |
299 } | 295 } |
300 | 296 |
301 TEST_F(SecurityKeyAuthHandlerWinTest, HandleConcurrentSecurityKeyRequests) { | 297 TEST_F(SecurityKeyAuthHandlerWinTest, HandleConcurrentSecurityKeyRequests) { |
302 std::string channel_name(GetUniqueTestChannelName()); | 298 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelHandle()); |
303 CreateSecurityKeyConnection(channel_name); | 299 CreateSecurityKeyConnection(channel_handle); |
304 | 300 |
305 // Create fake clients and connect each to the IPC server channel. | 301 // Create fake clients and connect each to the IPC server channel. |
306 FakeSecurityKeyIpcClient fake_ipc_client_1( | 302 FakeSecurityKeyIpcClient fake_ipc_client_1( |
307 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 303 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
308 base::Unretained(this))); | 304 base::Unretained(this))); |
309 FakeSecurityKeyIpcClient fake_ipc_client_2( | 305 FakeSecurityKeyIpcClient fake_ipc_client_2( |
310 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 306 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
311 base::Unretained(this))); | 307 base::Unretained(this))); |
312 | 308 |
313 EstablishInitialIpcConnection(&fake_ipc_client_1, kConnectionId1, | 309 EstablishIpcConnection(&fake_ipc_client_1, kConnectionId1, channel_handle, |
314 channel_name, | 310 /*close_connection=*/true); |
315 /*close_connection=*/true); | 311 EstablishIpcConnection(&fake_ipc_client_2, kConnectionId2, channel_handle, |
316 EstablishInitialIpcConnection(&fake_ipc_client_2, kConnectionId2, | 312 /*close_connection=*/true); |
317 channel_name, | |
318 /*close_connection=*/true); | |
319 | |
320 // Verify the connection details have been passed to the client. | |
321 std::string channel_name_1 = fake_ipc_client_1.last_message_received(); | |
322 std::string channel_name_2 = fake_ipc_client_2.last_message_received(); | |
323 ASSERT_NE(channel_name_1, channel_name_2); | |
324 | 313 |
325 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_1 = | 314 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_1 = |
326 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | 315 ipc_server_factory_.GetIpcServerObject(kConnectionId1); |
327 ASSERT_TRUE(fake_ipc_server_1.get()); | 316 ASSERT_TRUE(fake_ipc_server_1.get()); |
328 ASSERT_EQ(channel_name_1, fake_ipc_server_1->channel_name()); | |
329 | 317 |
330 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_2 = | 318 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_2 = |
331 ipc_server_factory_.GetIpcServerObject(kConnectionId2); | 319 ipc_server_factory_.GetIpcServerObject(kConnectionId2); |
332 ASSERT_TRUE(fake_ipc_server_2.get()); | 320 ASSERT_TRUE(fake_ipc_server_2.get()); |
333 ASSERT_EQ(channel_name_2, fake_ipc_server_2->channel_name()); | |
334 | 321 |
335 fake_ipc_server_1->set_send_response_callback( | 322 fake_ipc_server_1->set_send_response_callback( |
336 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 323 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
337 base::Unretained(this))); | 324 base::Unretained(this))); |
338 fake_ipc_server_2->set_send_response_callback( | 325 fake_ipc_server_2->set_send_response_callback( |
339 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 326 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
340 base::Unretained(this))); | 327 base::Unretained(this))); |
341 | 328 |
342 // Connect and send a security key request using the first IPC channel. | 329 // Connect and send a security key request using the first IPC channel. |
343 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, | 330 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, |
(...skipping 10 matching lines...) Expand all Loading... | |
354 // Send a security key response using the second IPC channel. | 341 // Send a security key response using the second IPC channel. |
355 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, | 342 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, |
356 "dddddddddd"); | 343 "dddddddddd"); |
357 | 344 |
358 // Close the IPC channels. | 345 // Close the IPC channels. |
359 CloseSecurityKeySessionIpcChannel(fake_ipc_server_1, kConnectionId1); | 346 CloseSecurityKeySessionIpcChannel(fake_ipc_server_1, kConnectionId1); |
360 CloseSecurityKeySessionIpcChannel(fake_ipc_server_2, kConnectionId2); | 347 CloseSecurityKeySessionIpcChannel(fake_ipc_server_2, kConnectionId2); |
361 } | 348 } |
362 | 349 |
363 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSequentialSecurityKeyRequests) { | 350 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSequentialSecurityKeyRequests) { |
364 std::string channel_name(GetUniqueTestChannelName()); | 351 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelHandle()); |
365 CreateSecurityKeyConnection(channel_name); | 352 CreateSecurityKeyConnection(channel_handle); |
366 | 353 |
367 // Create fake clients to connect to the IPC server channel. | 354 // Create fake clients to connect to the IPC server channel. |
368 FakeSecurityKeyIpcClient fake_ipc_client_1( | 355 FakeSecurityKeyIpcClient fake_ipc_client_1( |
369 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 356 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
370 base::Unretained(this))); | 357 base::Unretained(this))); |
371 | 358 |
372 EstablishInitialIpcConnection(&fake_ipc_client_1, kConnectionId1, | 359 EstablishIpcConnection(&fake_ipc_client_1, kConnectionId1, channel_handle, |
373 channel_name, | 360 /*close_connection=*/true); |
374 /*close_connection=*/true); | |
375 | |
376 // Verify the connection details have been passed to the client. | |
377 std::string channel_name_1 = fake_ipc_client_1.last_message_received(); | |
378 | 361 |
379 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_1 = | 362 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_1 = |
380 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | 363 ipc_server_factory_.GetIpcServerObject(kConnectionId1); |
381 ASSERT_TRUE(fake_ipc_server_1.get()); | 364 ASSERT_TRUE(fake_ipc_server_1.get()); |
382 ASSERT_EQ(channel_name_1, fake_ipc_server_1->channel_name()); | |
383 | 365 |
384 fake_ipc_server_1->set_send_response_callback( | 366 fake_ipc_server_1->set_send_response_callback( |
385 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 367 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
386 base::Unretained(this))); | 368 base::Unretained(this))); |
387 | 369 |
388 // Send a security key request using the first IPC channel. | 370 // Send a security key request using the first IPC channel. |
389 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, | 371 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, |
390 "aaaaaaaaaa"); | 372 "aaaaaaaaaa"); |
391 | 373 |
392 // Send a security key response using the first IPC channel. | 374 // Send a security key response using the first IPC channel. |
393 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, | 375 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_1, kConnectionId1, |
394 "cccccccccc"); | 376 "cccccccccc"); |
395 | 377 |
396 // Close the IPC channel. | 378 // Close the IPC channel. |
397 CloseSecurityKeySessionIpcChannel(fake_ipc_server_1, kConnectionId1); | 379 CloseSecurityKeySessionIpcChannel(fake_ipc_server_1, kConnectionId1); |
398 | 380 |
399 // Now connect with a second client. | 381 // Now connect with a second client. |
400 FakeSecurityKeyIpcClient fake_ipc_client_2( | 382 FakeSecurityKeyIpcClient fake_ipc_client_2( |
401 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 383 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
402 base::Unretained(this))); | 384 base::Unretained(this))); |
403 EstablishInitialIpcConnection(&fake_ipc_client_2, kConnectionId2, | 385 EstablishIpcConnection(&fake_ipc_client_2, kConnectionId2, channel_handle, |
404 channel_name, | 386 /*close_connection=*/true); |
405 /*close_connection=*/true); | |
406 | |
407 std::string channel_name_2 = fake_ipc_client_2.last_message_received(); | |
408 ASSERT_NE(channel_name_1, channel_name_2); | |
409 | 387 |
410 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_2 = | 388 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server_2 = |
411 ipc_server_factory_.GetIpcServerObject(kConnectionId2); | 389 ipc_server_factory_.GetIpcServerObject(kConnectionId2); |
412 ASSERT_TRUE(fake_ipc_server_2.get()); | 390 ASSERT_TRUE(fake_ipc_server_2.get()); |
413 ASSERT_EQ(channel_name_2, fake_ipc_server_2->channel_name()); | |
414 | 391 |
415 fake_ipc_server_2->set_send_response_callback( | 392 fake_ipc_server_2->set_send_response_callback( |
416 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 393 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
417 base::Unretained(this))); | 394 base::Unretained(this))); |
418 | 395 |
419 // Send a security key request using the second IPC channel. | 396 // Send a security key request using the second IPC channel. |
420 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, | 397 SendRequestToSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, |
421 "bbbbbbbbbb"); | 398 "bbbbbbbbbb"); |
422 | 399 |
423 // Send a security key response using the second IPC channel. | 400 // Send a security key response using the second IPC channel. |
424 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, | 401 SendResponseViaSecurityKeyAuthHandler(fake_ipc_server_2, kConnectionId2, |
425 "dddddddddd"); | 402 "dddddddddd"); |
426 | 403 |
427 // Close the IPC channel. | 404 // Close the IPC channel. |
428 CloseSecurityKeySessionIpcChannel(fake_ipc_server_2, kConnectionId2); | 405 CloseSecurityKeySessionIpcChannel(fake_ipc_server_2, kConnectionId2); |
429 } | 406 } |
430 | 407 |
431 TEST_F(SecurityKeyAuthHandlerWinTest, | 408 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSecurityKeyErrorResponse) { |
432 ClientNeverDisconnectsFromInitialIpcChannel) { | 409 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelHandle()); |
433 const int kLowConnectionTimeoutInMs = 25; | 410 CreateSecurityKeyConnection(channel_handle); |
434 auth_handler_->SetRequestTimeoutForTest( | 411 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
435 base::TimeDelta::FromMilliseconds(kLowConnectionTimeoutInMs)); | |
436 | |
437 std::string channel_name(GetUniqueTestChannelName()); | |
438 CreateSecurityKeyConnection(channel_name); | |
439 | 412 |
440 // Create a fake client and connect to the IPC server channel. | 413 // Create a fake client and connect to the IPC server channel. |
441 FakeSecurityKeyIpcClient fake_ipc_client( | 414 FakeSecurityKeyIpcClient fake_ipc_client( |
442 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 415 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
443 base::Unretained(this))); | 416 base::Unretained(this))); |
444 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | 417 EstablishIpcConnection(&fake_ipc_client, kConnectionId1, channel_handle, |
445 /*close_connection=*/false); | 418 /*close_connection=*/true); |
446 | |
447 // Don't close the channel here, instead wait for the SecurityKeyAuthHandler | |
448 // to close the connection due to the timeout. | |
449 WaitForOperationComplete(); | |
450 | |
451 // Verify the connection that was set up still exists. | |
452 ASSERT_TRUE(auth_handler_->IsValidConnectionId(kConnectionId1)); | |
453 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); | |
454 | |
455 // Attempt to connect again after the error. | |
456 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | |
457 /*close_connection=*/true); | |
458 } | |
459 | |
460 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSecurityKeyRequestTimeout) { | |
461 std::string channel_name(GetUniqueTestChannelName()); | |
462 CreateSecurityKeyConnection(channel_name); | |
463 | |
464 // Create a fake client and connect to the IPC server channel. | |
465 FakeSecurityKeyIpcClient fake_ipc_client( | |
466 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
467 base::Unretained(this))); | |
468 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
469 /*close_connection=*/true); | |
470 | |
471 // Connect to the private IPC server channel created for this client. | |
472 std::string new_channel_name = fake_ipc_client.last_message_received(); | |
473 | 419 |
474 // Retrieve the IPC server instance created when the client connected. | 420 // Retrieve the IPC server instance created when the client connected. |
475 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server = | 421 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server = |
476 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | 422 ipc_server_factory_.GetIpcServerObject(kConnectionId1); |
477 ASSERT_TRUE(fake_ipc_server.get()); | 423 ASSERT_TRUE(fake_ipc_server.get()); |
478 ASSERT_EQ(new_channel_name, fake_ipc_server->channel_name()); | |
479 | 424 |
480 fake_ipc_server->set_send_response_callback( | 425 fake_ipc_server->set_send_response_callback( |
481 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | 426 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, |
482 base::Unretained(this))); | |
483 | |
484 // Simulate a timeout and verify the IPC server is cleaned up. | |
485 CloseSecurityKeySessionIpcChannel(fake_ipc_server, kConnectionId1); | |
486 | |
487 // Attempt to connect again after the error. | |
488 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | |
489 /*close_connection=*/true); | |
490 } | |
491 | |
492 TEST_F(SecurityKeyAuthHandlerWinTest, HandleSecurityKeyErrorResponse) { | |
493 std::string channel_name(GetUniqueTestChannelName()); | |
494 CreateSecurityKeyConnection(channel_name); | |
495 | |
496 // Create a fake client and connect to the IPC server channel. | |
497 FakeSecurityKeyIpcClient fake_ipc_client( | |
498 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
499 base::Unretained(this))); | |
500 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId1, channel_name, | |
501 /*close_connection=*/true); | |
502 | |
503 // Connect to the private IPC server channel created for this client. | |
504 std::string new_channel_name = fake_ipc_client.last_message_received(); | |
505 | |
506 // Retrieve the IPC server instance created when the client connected. | |
507 base::WeakPtr<FakeSecurityKeyIpcServer> fake_ipc_server = | |
508 ipc_server_factory_.GetIpcServerObject(kConnectionId1); | |
509 ASSERT_TRUE(fake_ipc_server.get()); | |
510 ASSERT_EQ(new_channel_name, fake_ipc_server->channel_name()); | |
511 | |
512 fake_ipc_server->set_send_response_callback( | |
513 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
514 base::Unretained(this))); | 427 base::Unretained(this))); |
515 | 428 |
516 // Send a security key request using the fake IPC server. | 429 // Send a security key request using the fake IPC server. |
517 SendRequestToSecurityKeyAuthHandler(fake_ipc_server, kConnectionId1, | 430 SendRequestToSecurityKeyAuthHandler(fake_ipc_server, kConnectionId1, |
518 "0123456789"); | 431 "0123456789"); |
519 | 432 |
520 // Simulate a security key error from the client. | 433 // Simulate a security key error from the client. |
521 auth_handler_->SendErrorAndCloseConnection(kConnectionId1); | 434 auth_handler_->SendErrorAndCloseConnection(kConnectionId1); |
522 // Wait for the ipc server channel to be torn down. | 435 // Wait for the ipc server channel to be torn down. |
523 WaitForOperationComplete(); | 436 WaitForOperationComplete(); |
524 | 437 |
525 // Verify the connection was cleaned up. | 438 // Verify the connection was cleaned up. |
526 ASSERT_FALSE(fake_ipc_server.get()); | 439 ASSERT_FALSE(fake_ipc_server.get()); |
527 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); | 440 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); |
528 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 441 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
529 | 442 |
530 // Attempt to connect again after the error. | 443 // Attempt to connect again after the error. |
531 EstablishInitialIpcConnection(&fake_ipc_client, kConnectionId2, channel_name, | 444 EstablishIpcConnection(&fake_ipc_client, kConnectionId2, channel_handle, |
532 /*close_connection=*/true); | 445 /*close_connection=*/true); |
533 } | |
534 | |
535 TEST_F(SecurityKeyAuthHandlerWinTest, IpcConnectionFailsFromInvalidSession) { | |
joedow
2016/11/03 22:25:18
We still want this to work (i.e. Ipc connections f
Sam McNally
2016/11/04 02:51:09
SecurityKeyAuthHandlerWin no longer receives initi
| |
536 std::string channel_name(GetUniqueTestChannelName()); | |
537 CreateSecurityKeyConnection(channel_name); | |
538 | |
539 // Set the current session id to a 'different' session. | |
540 desktop_session_id_ += 1; | |
541 | |
542 // Create a fake client and connect to the IPC server channel. | |
543 FakeSecurityKeyIpcClient fake_ipc_client( | |
544 base::Bind(&SecurityKeyAuthHandlerWinTest::OperationComplete, | |
545 base::Unretained(this))); | |
546 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | |
547 // Wait for the error callback to be signaled. | |
548 WaitForOperationComplete(); | |
549 | |
550 // Verify the connection was not set up. | |
551 ASSERT_FALSE(auth_handler_->IsValidConnectionId(kConnectionId1)); | |
552 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | |
553 } | 446 } |
554 | 447 |
555 } // namespace remoting | 448 } // namespace remoting |
OLD | NEW |