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/security_key_extension_session.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/files/file_path.h" | |
10 #include "base/files/scoped_temp_dir.h" | |
11 #include "base/json/json_writer.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/run_loop.h" | |
16 #include "base/strings/stringprintf.h" | |
17 #include "base/threading/thread_task_runner_handle.h" | |
18 #include "base/timer/mock_timer.h" | |
19 #include "base/values.h" | |
20 #include "net/base/io_buffer.h" | |
21 #include "net/base/net_errors.h" | |
22 #include "net/base/test_completion_callback.h" | |
23 #include "net/socket/unix_domain_client_socket_posix.h" | |
24 #include "remoting/host/client_session_details.h" | |
25 #include "remoting/host/host_mock_objects.h" | |
26 #include "remoting/host/security_key/security_key_auth_handler.h" | |
27 #include "remoting/proto/internal.pb.h" | |
28 #include "remoting/protocol/client_stub.h" | |
29 #include "testing/gmock/include/gmock/gmock.h" | |
30 #include "testing/gtest/include/gtest/gtest.h" | |
31 | |
32 namespace remoting { | |
33 | |
34 namespace { | |
35 | |
36 // Test security key request data. | |
37 const unsigned char kRequestData[] = { | |
38 0x00, 0x00, 0x00, 0x9a, 0x65, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | |
39 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0x90, | |
40 0x24, 0x71, 0xf8, 0xf2, 0xe5, 0xdf, 0x7f, 0x81, 0xc7, 0x49, 0xc4, 0xa3, | |
41 0x58, 0x5c, 0xf6, 0xcc, 0x40, 0x14, 0x28, 0x0c, 0xa0, 0xfa, 0x03, 0x18, | |
42 0x38, 0xd8, 0x7d, 0x77, 0x2b, 0x3a, 0x00, 0x00, 0x00, 0x20, 0x64, 0x46, | |
43 0x47, 0x2f, 0xdf, 0x6e, 0xed, 0x7b, 0xf3, 0xc3, 0x37, 0x20, 0xf2, 0x36, | |
44 0x67, 0x6c, 0x36, 0xe1, 0xb4, 0x5e, 0xbe, 0x04, 0x85, 0xdb, 0x89, 0xa3, | |
45 0xcd, 0xfd, 0xd2, 0x4b, 0xd6, 0x9f, 0x00, 0x00, 0x00, 0x40, 0x38, 0x35, | |
46 0x05, 0x75, 0x1d, 0x13, 0x6e, 0xb3, 0x6b, 0x1d, 0x29, 0xae, 0xd3, 0x43, | |
47 0xe6, 0x84, 0x8f, 0xa3, 0x9d, 0x65, 0x4e, 0x2f, 0x57, 0xe3, 0xf6, 0xe6, | |
48 0x20, 0x3c, 0x00, 0xc6, 0xe1, 0x73, 0x34, 0xe2, 0x23, 0x99, 0xc4, 0xfa, | |
49 0x91, 0xc2, 0xd5, 0x97, 0xc1, 0x8b, 0xd0, 0x3c, 0x13, 0xba, 0xf0, 0xd7, | |
50 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd, | |
51 0xc8, 0xaa}; | |
52 | |
53 } // namespace | |
54 | |
55 class TestClientStub : public protocol::ClientStub { | |
56 public: | |
57 TestClientStub(); | |
58 ~TestClientStub() override; | |
59 | |
60 // protocol::ClientStub implementation. | |
61 void SetCapabilities(const protocol::Capabilities& capabilities) override; | |
62 void SetPairingResponse( | |
63 const protocol::PairingResponse& pairing_response) override; | |
64 void DeliverHostMessage(const protocol::ExtensionMessage& message) override; | |
65 void SetVideoLayout(const protocol::VideoLayout& layout) override; | |
66 | |
67 // protocol::ClipboardStub implementation. | |
68 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override; | |
69 | |
70 // protocol::CursorShapeStub implementation. | |
71 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override; | |
72 | |
73 void WaitForDeliverHostMessage(base::TimeDelta max_timeout); | |
74 | |
75 void CheckHostDataMessage(int id, const std::string& data); | |
76 | |
77 private: | |
78 protocol::ExtensionMessage message_; | |
79 std::unique_ptr<base::RunLoop> run_loop_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(TestClientStub); | |
82 }; | |
83 | |
84 TestClientStub::TestClientStub() : run_loop_(new base::RunLoop) {} | |
85 | |
86 TestClientStub::~TestClientStub() {} | |
87 | |
88 void TestClientStub::SetCapabilities( | |
89 const protocol::Capabilities& capabilities) {} | |
90 | |
91 void TestClientStub::SetPairingResponse( | |
92 const protocol::PairingResponse& pairing_response) {} | |
93 | |
94 void TestClientStub::DeliverHostMessage( | |
95 const protocol::ExtensionMessage& message) { | |
96 message_ = message; | |
97 run_loop_->Quit(); | |
98 } | |
99 | |
100 void TestClientStub::SetVideoLayout(const protocol::VideoLayout& layout) {} | |
101 | |
102 void TestClientStub::InjectClipboardEvent( | |
103 const protocol::ClipboardEvent& event) {} | |
104 | |
105 void TestClientStub::SetCursorShape( | |
106 const protocol::CursorShapeInfo& cursor_shape) {} | |
107 | |
108 void TestClientStub::WaitForDeliverHostMessage(base::TimeDelta max_timeout) { | |
109 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
110 FROM_HERE, run_loop_->QuitClosure(), max_timeout); | |
111 run_loop_->Run(); | |
112 run_loop_.reset(new base::RunLoop); | |
113 } | |
114 | |
115 void TestClientStub::CheckHostDataMessage(int id, const std::string& data) { | |
116 std::string connection_id = base::StringPrintf("\"connectionId\":%d", id); | |
117 std::string data_message = base::StringPrintf("\"data\":%s", data.c_str()); | |
118 | |
119 ASSERT_TRUE(message_.type() == "gnubby-auth" || message_.type() == "auth-v1"); | |
120 ASSERT_NE(message_.data().find("\"type\":\"data\""), std::string::npos); | |
121 ASSERT_NE(message_.data().find(connection_id), std::string::npos); | |
122 ASSERT_NE(message_.data().find(data_message), std::string::npos); | |
123 } | |
124 | |
125 class TestClientSessionDetails : public ClientSessionDetails { | |
126 public: | |
127 TestClientSessionDetails(); | |
128 ~TestClientSessionDetails() override; | |
129 | |
130 // ClientSessionDetails interface. | |
131 uint32_t desktop_session_id() const override { return desktop_session_id_; } | |
132 ClientSessionControl* session_control() override { return nullptr; } | |
133 | |
134 void set_desktop_session_id(uint32_t new_id) { desktop_session_id_ = new_id; } | |
135 | |
136 private: | |
137 uint32_t desktop_session_id_ = UINT32_MAX; | |
138 | |
139 DISALLOW_COPY_AND_ASSIGN(TestClientSessionDetails); | |
140 }; | |
141 | |
142 TestClientSessionDetails::TestClientSessionDetails() {} | |
143 | |
144 TestClientSessionDetails::~TestClientSessionDetails() {} | |
145 | |
146 class SecurityKeyExtensionSessionTest : public testing::Test { | |
147 public: | |
148 SecurityKeyExtensionSessionTest(); | |
149 ~SecurityKeyExtensionSessionTest() override; | |
150 | |
151 void WaitForAndVerifyHostMessage(); | |
152 | |
153 void CreateSecurityKeyConnection(); | |
154 | |
155 protected: | |
156 base::MessageLoopForIO message_loop_; | |
157 | |
158 // Object under test. | |
159 std::unique_ptr<SecurityKeyExtensionSession> security_key_extension_session_; | |
160 | |
161 MockSecurityKeyAuthHandler* mock_security_key_auth_handler_ = nullptr; | |
162 | |
163 TestClientStub client_stub_; | |
164 TestClientSessionDetails client_details_; | |
165 | |
166 private: | |
167 DISALLOW_COPY_AND_ASSIGN(SecurityKeyExtensionSessionTest); | |
168 }; | |
169 | |
170 SecurityKeyExtensionSessionTest::SecurityKeyExtensionSessionTest() | |
171 : security_key_extension_session_( | |
172 new SecurityKeyExtensionSession(&client_details_, &client_stub_)) { | |
173 // We want to retain ownership of mock object so we can use it to inject | |
174 // events into the extension session. The mock object should not be used | |
175 // once |security_key_extension_session_| is destroyed. | |
176 mock_security_key_auth_handler_ = new MockSecurityKeyAuthHandler(); | |
177 security_key_extension_session_->SetSecurityKeyAuthHandlerForTesting( | |
178 base::WrapUnique(mock_security_key_auth_handler_)); | |
179 } | |
180 | |
181 SecurityKeyExtensionSessionTest::~SecurityKeyExtensionSessionTest() {} | |
182 | |
183 void SecurityKeyExtensionSessionTest::WaitForAndVerifyHostMessage() { | |
184 client_stub_.WaitForDeliverHostMessage( | |
185 base::TimeDelta::FromMilliseconds(500)); | |
186 base::ListValue expected_data; | |
187 | |
188 // Skip first four bytes. | |
189 for (size_t i = 4; i < sizeof(kRequestData); ++i) { | |
190 expected_data.AppendInteger(kRequestData[i]); | |
191 } | |
192 | |
193 std::string expected_data_json; | |
194 base::JSONWriter::Write(expected_data, &expected_data_json); | |
195 client_stub_.CheckHostDataMessage(1, expected_data_json); | |
196 } | |
197 | |
198 void SecurityKeyExtensionSessionTest::CreateSecurityKeyConnection() { | |
199 EXPECT_CALL(*mock_security_key_auth_handler_, CreateSecurityKeyConnection()) | |
200 .Times(1); | |
201 | |
202 protocol::ExtensionMessage message; | |
203 message.set_type("gnubby-auth"); | |
204 message.set_data("{\"type\":\"control\",\"option\":\"auth-v1\"}"); | |
205 | |
206 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
207 nullptr, nullptr, message)); | |
208 } | |
209 | |
210 TEST_F(SecurityKeyExtensionSessionTest, | |
211 SecurityKeyConnectionCreated_ValidMessage) { | |
212 CreateSecurityKeyConnection(); | |
213 } | |
214 | |
215 TEST_F(SecurityKeyExtensionSessionTest, | |
216 NoSecurityKeyConnectionCreated_WrongMessageType) { | |
217 EXPECT_CALL(*mock_security_key_auth_handler_, CreateSecurityKeyConnection()) | |
218 .Times(0); | |
219 | |
220 protocol::ExtensionMessage message; | |
221 message.set_type("unsupported-gnubby-auth"); | |
222 message.set_data("{\"type\":\"control\",\"option\":\"auth-v1\"}"); | |
223 | |
224 ASSERT_FALSE(security_key_extension_session_->OnExtensionMessage( | |
225 nullptr, nullptr, message)); | |
226 } | |
227 | |
228 TEST_F(SecurityKeyExtensionSessionTest, | |
229 NoSecurityKeyConnectionCreated_InvalidMessageData) { | |
230 EXPECT_CALL(*mock_security_key_auth_handler_, CreateSecurityKeyConnection()) | |
231 .Times(0); | |
232 | |
233 // First try invalid JSON. | |
234 protocol::ExtensionMessage message; | |
235 message.set_type("gnubby-auth"); | |
236 message.set_data("{\"type\":\"control\",\"option\":}"); | |
237 // handled should still be true, even if the message payload is invalid. | |
238 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
239 nullptr, nullptr, message)); | |
240 | |
241 // Now try an invalid message type. | |
242 message.set_type("gnubby-auth"); | |
243 message.set_data("{\"type\":\"control\",\"option\":\"auth-v0\"}"); | |
244 // handled should still be true, even if the message payload is invalid. | |
245 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
246 nullptr, nullptr, message)); | |
247 | |
248 // Now try a message that is missing the option and auth type. | |
249 message.set_type("gnubby-auth"); | |
250 message.set_data("{\"type\":\"control\"}"); | |
251 // handled should still be true, even if the message payload is invalid. | |
252 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
253 nullptr, nullptr, message)); | |
254 } | |
255 | |
256 TEST_F(SecurityKeyExtensionSessionTest, | |
257 DataMessageProcessing_MissingConnectionId) { | |
258 CreateSecurityKeyConnection(); | |
259 | |
260 EXPECT_CALL(*mock_security_key_auth_handler_, | |
261 SendClientResponse(testing::_, testing::_)) | |
262 .Times(0); | |
263 EXPECT_CALL(*mock_security_key_auth_handler_, | |
264 SendErrorAndCloseConnection(testing::_)) | |
265 .Times(0); | |
266 EXPECT_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(testing::_)) | |
267 .Times(0); | |
268 | |
269 protocol::ExtensionMessage message; | |
270 message.set_type("gnubby-auth"); | |
271 message.set_data("{\"type\":\"data\"}"); | |
272 | |
273 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
274 nullptr, nullptr, message)); | |
275 } | |
276 | |
277 TEST_F(SecurityKeyExtensionSessionTest, | |
278 DataMessageProcessing_InvalidConnectionId) { | |
279 CreateSecurityKeyConnection(); | |
280 | |
281 EXPECT_CALL(*mock_security_key_auth_handler_, | |
282 SendClientResponse(testing::_, testing::_)) | |
283 .Times(0); | |
284 EXPECT_CALL(*mock_security_key_auth_handler_, | |
285 SendErrorAndCloseConnection(testing::_)) | |
286 .Times(0); | |
287 EXPECT_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(1)) | |
288 .Times(1); | |
289 | |
290 ON_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(testing::_)) | |
291 .WillByDefault(testing::Return(false)); | |
292 | |
293 protocol::ExtensionMessage message; | |
294 message.set_type("gnubby-auth"); | |
295 message.set_data("{\"type\":\"data\",\"connectionId\":1}"); | |
296 | |
297 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
298 nullptr, nullptr, message)); | |
299 } | |
300 | |
301 TEST_F(SecurityKeyExtensionSessionTest, DataMessageProcessing_MissingPayload) { | |
302 CreateSecurityKeyConnection(); | |
303 | |
304 EXPECT_CALL(*mock_security_key_auth_handler_, SendErrorAndCloseConnection(1)) | |
305 .Times(1); | |
306 EXPECT_CALL(*mock_security_key_auth_handler_, | |
307 SendClientResponse(testing::_, testing::_)) | |
308 .Times(0); | |
309 EXPECT_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(1)) | |
310 .Times(1); | |
311 | |
312 ON_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(testing::_)) | |
313 .WillByDefault(testing::Return(true)); | |
314 | |
315 protocol::ExtensionMessage message; | |
316 message.set_type("gnubby-auth"); | |
317 message.set_data("{\"type\":\"data\",\"connectionId\":1}"); | |
318 | |
319 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
320 nullptr, nullptr, message)); | |
321 } | |
322 | |
323 TEST_F(SecurityKeyExtensionSessionTest, DataMessageProcessing_InvalidPayload) { | |
324 CreateSecurityKeyConnection(); | |
325 | |
326 EXPECT_CALL(*mock_security_key_auth_handler_, SendErrorAndCloseConnection(1)) | |
327 .Times(1); | |
328 EXPECT_CALL(*mock_security_key_auth_handler_, | |
329 SendClientResponse(testing::_, testing::_)) | |
330 .Times(0); | |
331 EXPECT_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(1)) | |
332 .Times(1); | |
333 | |
334 ON_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(testing::_)) | |
335 .WillByDefault(testing::Return(true)); | |
336 | |
337 protocol::ExtensionMessage message; | |
338 message.set_type("gnubby-auth"); | |
339 message.set_data( | |
340 "{\"type\":\"data\",\"connectionId\":1,\"data\":[\"a\",\"-\",\"z\"]}"); | |
341 | |
342 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
343 nullptr, nullptr, message)); | |
344 } | |
345 | |
346 TEST_F(SecurityKeyExtensionSessionTest, DataMessageProcessing_ValidData) { | |
347 CreateSecurityKeyConnection(); | |
348 | |
349 EXPECT_CALL(*mock_security_key_auth_handler_, | |
350 SendClientResponse(1, "\x1\x2\x3\x4\x5")) | |
351 .Times(1); | |
352 EXPECT_CALL(*mock_security_key_auth_handler_, | |
353 SendErrorAndCloseConnection(testing::_)) | |
354 .Times(0); | |
355 EXPECT_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(1)) | |
356 .Times(1); | |
357 | |
358 ON_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(testing::_)) | |
359 .WillByDefault(testing::Return(true)); | |
360 | |
361 protocol::ExtensionMessage message; | |
362 message.set_type("gnubby-auth"); | |
363 message.set_data( | |
364 "{\"type\":\"data\",\"connectionId\":1,\"data\":[1,2,3,4,5]}"); | |
365 | |
366 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
367 nullptr, nullptr, message)); | |
368 } | |
369 | |
370 TEST_F(SecurityKeyExtensionSessionTest, | |
371 ErrorMessageProcessing_MissingConnectionId) { | |
372 CreateSecurityKeyConnection(); | |
373 | |
374 EXPECT_CALL(*mock_security_key_auth_handler_, | |
375 SendErrorAndCloseConnection(testing::_)) | |
376 .Times(0); | |
377 EXPECT_CALL(*mock_security_key_auth_handler_, | |
378 SendClientResponse(testing::_, testing::_)) | |
379 .Times(0); | |
380 EXPECT_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(testing::_)) | |
381 .Times(0); | |
382 | |
383 protocol::ExtensionMessage message; | |
384 message.set_type("gnubby-auth"); | |
385 message.set_data("{\"type\":\"error\"}"); | |
386 | |
387 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
388 nullptr, nullptr, message)); | |
389 } | |
390 | |
391 TEST_F(SecurityKeyExtensionSessionTest, | |
392 ErrorMessageProcessing_InvalidConnectionId) { | |
393 CreateSecurityKeyConnection(); | |
394 | |
395 EXPECT_CALL(*mock_security_key_auth_handler_, | |
396 SendErrorAndCloseConnection(testing::_)) | |
397 .Times(0); | |
398 EXPECT_CALL(*mock_security_key_auth_handler_, | |
399 SendClientResponse(testing::_, testing::_)) | |
400 .Times(0); | |
401 EXPECT_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(1)) | |
402 .Times(1); | |
403 | |
404 ON_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(testing::_)) | |
405 .WillByDefault(testing::Return(false)); | |
406 | |
407 protocol::ExtensionMessage message; | |
408 message.set_type("gnubby-auth"); | |
409 message.set_data("{\"type\":\"error\",\"connectionId\":1}"); | |
410 | |
411 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
412 nullptr, nullptr, message)); | |
413 } | |
414 | |
415 TEST_F(SecurityKeyExtensionSessionTest, ErrorMessageProcessing_ValidData) { | |
416 CreateSecurityKeyConnection(); | |
417 | |
418 EXPECT_CALL(*mock_security_key_auth_handler_, SendErrorAndCloseConnection(1)) | |
419 .Times(1); | |
420 EXPECT_CALL(*mock_security_key_auth_handler_, | |
421 SendClientResponse(testing::_, testing::_)) | |
422 .Times(0); | |
423 EXPECT_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(1)) | |
424 .Times(1); | |
425 | |
426 ON_CALL(*mock_security_key_auth_handler_, IsValidConnectionId(testing::_)) | |
427 .WillByDefault(testing::Return(true)); | |
428 | |
429 protocol::ExtensionMessage message; | |
430 message.set_type("gnubby-auth"); | |
431 message.set_data("{\"type\":\"error\",\"connectionId\":1}"); | |
432 | |
433 ASSERT_TRUE(security_key_extension_session_->OnExtensionMessage( | |
434 nullptr, nullptr, message)); | |
435 } | |
436 | |
437 TEST_F(SecurityKeyExtensionSessionTest, SendMessageToClient_ValidData) { | |
438 CreateSecurityKeyConnection(); | |
439 | |
440 // Inject data into SendMessageCallback to simulate a security key request. | |
441 mock_security_key_auth_handler_->GetSendMessageCallback().Run(42, "test_msg"); | |
442 | |
443 client_stub_.WaitForDeliverHostMessage( | |
444 base::TimeDelta::FromMilliseconds(500)); | |
445 | |
446 // Expects a JSON array of the ASCII character codes for "test_msg". | |
447 client_stub_.CheckHostDataMessage(42, "[116,101,115,116,95,109,115,103]"); | |
448 } | |
449 | |
450 } // namespace remoting | |
OLD | NEW |