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_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/gnubby_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 gnubby 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 GnubbyExtensionSessionTest : public testing::Test { | |
147 public: | |
148 GnubbyExtensionSessionTest(); | |
149 ~GnubbyExtensionSessionTest() override; | |
150 | |
151 void WaitForAndVerifyHostMessage(); | |
152 | |
153 void CreateGnubbyConnection(); | |
154 | |
155 protected: | |
156 base::MessageLoopForIO message_loop_; | |
157 | |
158 // Object under test. | |
159 std::unique_ptr<GnubbyExtensionSession> gnubby_extension_session_; | |
160 | |
161 MockGnubbyAuthHandler* mock_gnubby_auth_handler_ = nullptr; | |
162 | |
163 TestClientStub client_stub_; | |
164 TestClientSessionDetails client_details_; | |
165 | |
166 private: | |
167 DISALLOW_COPY_AND_ASSIGN(GnubbyExtensionSessionTest); | |
168 }; | |
169 | |
170 GnubbyExtensionSessionTest::GnubbyExtensionSessionTest() | |
171 : gnubby_extension_session_( | |
172 new GnubbyExtensionSession(&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 |gnubby_extension_session_| is destroyed. | |
176 mock_gnubby_auth_handler_ = new MockGnubbyAuthHandler(); | |
177 gnubby_extension_session_->SetGnubbyAuthHandlerForTesting( | |
178 base::WrapUnique(mock_gnubby_auth_handler_)); | |
179 } | |
180 | |
181 GnubbyExtensionSessionTest::~GnubbyExtensionSessionTest() {} | |
182 | |
183 void GnubbyExtensionSessionTest::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 GnubbyExtensionSessionTest::CreateGnubbyConnection() { | |
199 EXPECT_CALL(*mock_gnubby_auth_handler_, CreateGnubbyConnection()).Times(1); | |
200 | |
201 protocol::ExtensionMessage message; | |
202 message.set_type("gnubby-auth"); | |
203 message.set_data("{\"type\":\"control\",\"option\":\"auth-v1\"}"); | |
204 | |
205 ASSERT_TRUE( | |
206 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
207 } | |
208 | |
209 TEST_F(GnubbyExtensionSessionTest, GnubbyConnectionCreated_ValidMessage) { | |
210 CreateGnubbyConnection(); | |
211 } | |
212 | |
213 TEST_F(GnubbyExtensionSessionTest, NoGnubbyConnectionCreated_WrongMessageType) { | |
214 EXPECT_CALL(*mock_gnubby_auth_handler_, CreateGnubbyConnection()).Times(0); | |
215 | |
216 protocol::ExtensionMessage message; | |
217 message.set_type("unsupported-gnubby-auth"); | |
218 message.set_data("{\"type\":\"control\",\"option\":\"auth-v1\"}"); | |
219 | |
220 ASSERT_FALSE( | |
221 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
222 } | |
223 | |
224 TEST_F(GnubbyExtensionSessionTest, | |
225 NoGnubbyConnectionCreated_InvalidMessageData) { | |
226 EXPECT_CALL(*mock_gnubby_auth_handler_, CreateGnubbyConnection()).Times(0); | |
227 | |
228 // First try invalid JSON. | |
229 protocol::ExtensionMessage message; | |
230 message.set_type("gnubby-auth"); | |
231 message.set_data("{\"type\":\"control\",\"option\":}"); | |
232 // handled should still be true, even if the message payload is invalid. | |
233 ASSERT_TRUE( | |
234 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
235 | |
236 // Now try an invalid message type. | |
237 message.set_type("gnubby-auth"); | |
238 message.set_data("{\"type\":\"control\",\"option\":\"auth-v0\"}"); | |
239 // handled should still be true, even if the message payload is invalid. | |
240 ASSERT_TRUE( | |
241 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
242 | |
243 // Now try a message that is missing the option and auth type. | |
244 message.set_type("gnubby-auth"); | |
245 message.set_data("{\"type\":\"control\"}"); | |
246 // handled should still be true, even if the message payload is invalid. | |
247 ASSERT_TRUE( | |
248 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
249 } | |
250 | |
251 TEST_F(GnubbyExtensionSessionTest, DataMessageProcessing_MissingConnectionId) { | |
252 CreateGnubbyConnection(); | |
253 | |
254 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
255 SendClientResponse(testing::_, testing::_)) | |
256 .Times(0); | |
257 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
258 SendErrorAndCloseConnection(testing::_)) | |
259 .Times(0); | |
260 EXPECT_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(testing::_)) | |
261 .Times(0); | |
262 | |
263 protocol::ExtensionMessage message; | |
264 message.set_type("gnubby-auth"); | |
265 message.set_data("{\"type\":\"data\"}"); | |
266 | |
267 ASSERT_TRUE( | |
268 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
269 } | |
270 | |
271 TEST_F(GnubbyExtensionSessionTest, DataMessageProcessing_InvalidConnectionId) { | |
272 CreateGnubbyConnection(); | |
273 | |
274 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
275 SendClientResponse(testing::_, testing::_)) | |
276 .Times(0); | |
277 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
278 SendErrorAndCloseConnection(testing::_)) | |
279 .Times(0); | |
280 EXPECT_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(1)).Times(1); | |
281 | |
282 ON_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(testing::_)) | |
283 .WillByDefault(testing::Return(false)); | |
284 | |
285 protocol::ExtensionMessage message; | |
286 message.set_type("gnubby-auth"); | |
287 message.set_data("{\"type\":\"data\",\"connectionId\":1}"); | |
288 | |
289 ASSERT_TRUE( | |
290 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
291 } | |
292 | |
293 TEST_F(GnubbyExtensionSessionTest, DataMessageProcessing_MissingPayload) { | |
294 CreateGnubbyConnection(); | |
295 | |
296 EXPECT_CALL(*mock_gnubby_auth_handler_, SendErrorAndCloseConnection(1)) | |
297 .Times(1); | |
298 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
299 SendClientResponse(testing::_, testing::_)) | |
300 .Times(0); | |
301 EXPECT_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(1)).Times(1); | |
302 | |
303 ON_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(testing::_)) | |
304 .WillByDefault(testing::Return(true)); | |
305 | |
306 protocol::ExtensionMessage message; | |
307 message.set_type("gnubby-auth"); | |
308 message.set_data("{\"type\":\"data\",\"connectionId\":1}"); | |
309 | |
310 ASSERT_TRUE( | |
311 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
312 } | |
313 | |
314 TEST_F(GnubbyExtensionSessionTest, DataMessageProcessing_InvalidPayload) { | |
315 CreateGnubbyConnection(); | |
316 | |
317 EXPECT_CALL(*mock_gnubby_auth_handler_, SendErrorAndCloseConnection(1)) | |
318 .Times(1); | |
319 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
320 SendClientResponse(testing::_, testing::_)) | |
321 .Times(0); | |
322 EXPECT_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(1)).Times(1); | |
323 | |
324 ON_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(testing::_)) | |
325 .WillByDefault(testing::Return(true)); | |
326 | |
327 protocol::ExtensionMessage message; | |
328 message.set_type("gnubby-auth"); | |
329 message.set_data( | |
330 "{\"type\":\"data\",\"connectionId\":1,\"data\":[\"a\",\"-\",\"z\"]}"); | |
331 | |
332 ASSERT_TRUE( | |
333 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
334 } | |
335 | |
336 TEST_F(GnubbyExtensionSessionTest, DataMessageProcessing_ValidData) { | |
337 CreateGnubbyConnection(); | |
338 | |
339 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
340 SendClientResponse(1, "\x1\x2\x3\x4\x5")) | |
341 .Times(1); | |
342 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
343 SendErrorAndCloseConnection(testing::_)) | |
344 .Times(0); | |
345 EXPECT_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(1)).Times(1); | |
346 | |
347 ON_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(testing::_)) | |
348 .WillByDefault(testing::Return(true)); | |
349 | |
350 protocol::ExtensionMessage message; | |
351 message.set_type("gnubby-auth"); | |
352 message.set_data( | |
353 "{\"type\":\"data\",\"connectionId\":1,\"data\":[1,2,3,4,5]}"); | |
354 | |
355 ASSERT_TRUE( | |
356 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
357 } | |
358 | |
359 TEST_F(GnubbyExtensionSessionTest, ErrorMessageProcessing_MissingConnectionId) { | |
360 CreateGnubbyConnection(); | |
361 | |
362 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
363 SendErrorAndCloseConnection(testing::_)) | |
364 .Times(0); | |
365 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
366 SendClientResponse(testing::_, testing::_)) | |
367 .Times(0); | |
368 EXPECT_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(testing::_)) | |
369 .Times(0); | |
370 | |
371 protocol::ExtensionMessage message; | |
372 message.set_type("gnubby-auth"); | |
373 message.set_data("{\"type\":\"error\"}"); | |
374 | |
375 ASSERT_TRUE( | |
376 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
377 } | |
378 | |
379 TEST_F(GnubbyExtensionSessionTest, ErrorMessageProcessing_InvalidConnectionId) { | |
380 CreateGnubbyConnection(); | |
381 | |
382 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
383 SendErrorAndCloseConnection(testing::_)) | |
384 .Times(0); | |
385 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
386 SendClientResponse(testing::_, testing::_)) | |
387 .Times(0); | |
388 EXPECT_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(1)).Times(1); | |
389 | |
390 ON_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(testing::_)) | |
391 .WillByDefault(testing::Return(false)); | |
392 | |
393 protocol::ExtensionMessage message; | |
394 message.set_type("gnubby-auth"); | |
395 message.set_data("{\"type\":\"error\",\"connectionId\":1}"); | |
396 | |
397 ASSERT_TRUE( | |
398 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
399 } | |
400 | |
401 TEST_F(GnubbyExtensionSessionTest, ErrorMessageProcessing_ValidData) { | |
402 CreateGnubbyConnection(); | |
403 | |
404 EXPECT_CALL(*mock_gnubby_auth_handler_, SendErrorAndCloseConnection(1)) | |
405 .Times(1); | |
406 EXPECT_CALL(*mock_gnubby_auth_handler_, | |
407 SendClientResponse(testing::_, testing::_)) | |
408 .Times(0); | |
409 EXPECT_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(1)).Times(1); | |
410 | |
411 ON_CALL(*mock_gnubby_auth_handler_, IsValidConnectionId(testing::_)) | |
412 .WillByDefault(testing::Return(true)); | |
413 | |
414 protocol::ExtensionMessage message; | |
415 message.set_type("gnubby-auth"); | |
416 message.set_data("{\"type\":\"error\",\"connectionId\":1}"); | |
417 | |
418 ASSERT_TRUE( | |
419 gnubby_extension_session_->OnExtensionMessage(nullptr, nullptr, message)); | |
420 } | |
421 | |
422 TEST_F(GnubbyExtensionSessionTest, SendMessageToClient_ValidData) { | |
423 CreateGnubbyConnection(); | |
424 | |
425 // Inject data into the SendMessageCallback to simulate a gnubby request. | |
426 mock_gnubby_auth_handler_->GetSendMessageCallback().Run(42, "test_msg"); | |
427 | |
428 client_stub_.WaitForDeliverHostMessage( | |
429 base::TimeDelta::FromMilliseconds(500)); | |
430 | |
431 // Expects a JSON array of the ASCII character codes for "test_msg". | |
432 client_stub_.CheckHostDataMessage(42, "[116,101,115,116,95,109,115,103]"); | |
433 } | |
434 | |
435 } // namespace remoting | |
OLD | NEW |