1 // Copyright 2013 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.
5 #include "remoting/host/setup/me2me_native_messaging_host.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/stl_util.h"
14 #include "base/strings/stringize_macros.h"
15 #include "base/values.h"
16 #include "google_apis/gaia/gaia_oauth_client.h"
17 #include "net/base/file_stream.h"
18 #include "net/base/net_util.h"
19 #include "remoting/base/auto_thread_task_runner.h"
20 #include "remoting/host/native_messaging/pipe_messaging_channel.h"
21 #include "remoting/host/pin_hash.h"
22 #include "remoting/host/setup/test_util.h"
23 #include "remoting/protocol/pairing_registry.h"
24 #include "remoting/protocol/protocol_mock_objects.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using remoting::protocol::MockPairingRegistryDelegate
;
28 using remoting::protocol::PairingRegistry
;
29 using remoting::protocol::SynchronousPairingRegistry
;
33 void VerifyHelloResponse(scoped_ptr
<base::DictionaryValue
> response
) {
34 ASSERT_TRUE(response
);
36 EXPECT_TRUE(response
->GetString("type", &value
));
37 EXPECT_EQ("helloResponse", value
);
38 EXPECT_TRUE(response
->GetString("version", &value
));
39 EXPECT_EQ(STRINGIZE(VERSION
), value
);
42 void VerifyGetHostNameResponse(scoped_ptr
<base::DictionaryValue
> response
) {
43 ASSERT_TRUE(response
);
45 EXPECT_TRUE(response
->GetString("type", &value
));
46 EXPECT_EQ("getHostNameResponse", value
);
47 EXPECT_TRUE(response
->GetString("hostname", &value
));
48 EXPECT_EQ(net::GetHostName(), value
);
51 void VerifyGetPinHashResponse(scoped_ptr
<base::DictionaryValue
> response
) {
52 ASSERT_TRUE(response
);
54 EXPECT_TRUE(response
->GetString("type", &value
));
55 EXPECT_EQ("getPinHashResponse", value
);
56 EXPECT_TRUE(response
->GetString("hash", &value
));
57 EXPECT_EQ(remoting::MakeHostPinHash("my_host", "1234"), value
);
60 void VerifyGenerateKeyPairResponse(scoped_ptr
<base::DictionaryValue
> response
) {
61 ASSERT_TRUE(response
);
63 EXPECT_TRUE(response
->GetString("type", &value
));
64 EXPECT_EQ("generateKeyPairResponse", value
);
65 EXPECT_TRUE(response
->GetString("privateKey", &value
));
66 EXPECT_TRUE(response
->GetString("publicKey", &value
));
69 void VerifyGetDaemonConfigResponse(scoped_ptr
<base::DictionaryValue
> response
) {
70 ASSERT_TRUE(response
);
72 EXPECT_TRUE(response
->GetString("type", &value
));
73 EXPECT_EQ("getDaemonConfigResponse", value
);
74 const base::DictionaryValue
* config
= NULL
;
75 EXPECT_TRUE(response
->GetDictionary("config", &config
));
76 EXPECT_TRUE(base::DictionaryValue().Equals(config
));
79 void VerifyGetUsageStatsConsentResponse(
80 scoped_ptr
<base::DictionaryValue
> response
) {
81 ASSERT_TRUE(response
);
83 EXPECT_TRUE(response
->GetString("type", &value
));
84 EXPECT_EQ("getUsageStatsConsentResponse", value
);
85 bool supported
, allowed
, set_by_policy
;
86 EXPECT_TRUE(response
->GetBoolean("supported", &supported
));
87 EXPECT_TRUE(response
->GetBoolean("allowed", &allowed
));
88 EXPECT_TRUE(response
->GetBoolean("setByPolicy", &set_by_policy
));
89 EXPECT_TRUE(supported
);
91 EXPECT_TRUE(set_by_policy
);
94 void VerifyStopDaemonResponse(scoped_ptr
<base::DictionaryValue
> response
) {
95 ASSERT_TRUE(response
);
97 EXPECT_TRUE(response
->GetString("type", &value
));
98 EXPECT_EQ("stopDaemonResponse", value
);
99 EXPECT_TRUE(response
->GetString("result", &value
));
100 EXPECT_EQ("OK", value
);
103 void VerifyGetDaemonStateResponse(scoped_ptr
<base::DictionaryValue
> response
) {
104 ASSERT_TRUE(response
);
106 EXPECT_TRUE(response
->GetString("type", &value
));
107 EXPECT_EQ("getDaemonStateResponse", value
);
108 EXPECT_TRUE(response
->GetString("state", &value
));
109 EXPECT_EQ("STARTED", value
);
112 void VerifyUpdateDaemonConfigResponse(
113 scoped_ptr
<base::DictionaryValue
> response
) {
114 ASSERT_TRUE(response
);
116 EXPECT_TRUE(response
->GetString("type", &value
));
117 EXPECT_EQ("updateDaemonConfigResponse", value
);
118 EXPECT_TRUE(response
->GetString("result", &value
));
119 EXPECT_EQ("OK", value
);
122 void VerifyStartDaemonResponse(scoped_ptr
<base::DictionaryValue
> response
) {
123 ASSERT_TRUE(response
);
125 EXPECT_TRUE(response
->GetString("type", &value
));
126 EXPECT_EQ("startDaemonResponse", value
);
127 EXPECT_TRUE(response
->GetString("result", &value
));
128 EXPECT_EQ("OK", value
);
135 class MockDaemonControllerDelegate
: public DaemonController::Delegate
{
137 MockDaemonControllerDelegate();
138 virtual ~MockDaemonControllerDelegate();
140 // DaemonController::Delegate interface.
141 virtual DaemonController::State
GetState() override
;
142 virtual scoped_ptr
<base::DictionaryValue
> GetConfig() override
;
143 virtual void InstallHost(
144 const DaemonController::CompletionCallback
& done
) override
;
145 virtual void SetConfigAndStart(
146 scoped_ptr
<base::DictionaryValue
> config
,
148 const DaemonController::CompletionCallback
& done
) override
;
149 virtual void UpdateConfig(
150 scoped_ptr
<base::DictionaryValue
> config
,
151 const DaemonController::CompletionCallback
& done
) override
;
152 virtual void Stop(const DaemonController::CompletionCallback
& done
) override
;
153 virtual void SetWindow(void* window_handle
) override
;
154 virtual std::string
GetVersion() override
;
155 virtual DaemonController::UsageStatsConsent
GetUsageStatsConsent() override
;
158 DISALLOW_COPY_AND_ASSIGN(MockDaemonControllerDelegate
);
161 MockDaemonControllerDelegate::MockDaemonControllerDelegate() {}
163 MockDaemonControllerDelegate::~MockDaemonControllerDelegate() {}
165 DaemonController::State
MockDaemonControllerDelegate::GetState() {
166 return DaemonController::STATE_STARTED
;
169 scoped_ptr
<base::DictionaryValue
> MockDaemonControllerDelegate::GetConfig() {
170 return make_scoped_ptr(new base::DictionaryValue());
173 void MockDaemonControllerDelegate::InstallHost(
174 const DaemonController::CompletionCallback
& done
) {
175 done
.Run(DaemonController::RESULT_OK
);
178 void MockDaemonControllerDelegate::SetConfigAndStart(
179 scoped_ptr
<base::DictionaryValue
> config
,
181 const DaemonController::CompletionCallback
& done
) {
183 // Verify parameters passed in.
184 if (consent
&& config
&& config
->HasKey("start")) {
185 done
.Run(DaemonController::RESULT_OK
);
187 done
.Run(DaemonController::RESULT_FAILED
);
191 void MockDaemonControllerDelegate::UpdateConfig(
192 scoped_ptr
<base::DictionaryValue
> config
,
193 const DaemonController::CompletionCallback
& done
) {
194 if (config
&& config
->HasKey("update")) {
195 done
.Run(DaemonController::RESULT_OK
);
197 done
.Run(DaemonController::RESULT_FAILED
);
201 void MockDaemonControllerDelegate::Stop(
202 const DaemonController::CompletionCallback
& done
) {
203 done
.Run(DaemonController::RESULT_OK
);
206 void MockDaemonControllerDelegate::SetWindow(void* window_handle
) {}
208 std::string
MockDaemonControllerDelegate::GetVersion() {
209 // Unused - Me2MeNativeMessagingHost returns the compiled-in version string
210 // instead of calling this method.
212 return std::string();
215 DaemonController::UsageStatsConsent
216 MockDaemonControllerDelegate::GetUsageStatsConsent() {
217 DaemonController::UsageStatsConsent consent
;
218 consent
.supported
= true;
219 consent
.allowed
= true;
220 consent
.set_by_policy
= true;
224 class Me2MeNativeMessagingHostTest
: public testing::Test
{
226 Me2MeNativeMessagingHostTest();
227 virtual ~Me2MeNativeMessagingHostTest();
229 virtual void SetUp() override
;
230 virtual void TearDown() override
;
232 scoped_ptr
<base::DictionaryValue
> ReadMessageFromOutputPipe();
234 void WriteMessageToInputPipe(const base::Value
& message
);
236 // The Host process should shut down when it receives a malformed request.
237 // This is tested by sending a known-good request, followed by |message|,
238 // followed by the known-good request again. The response file should only
239 // contain a single response from the first good request.
240 void TestBadRequest(const base::Value
& message
);
243 // Reference to the MockDaemonControllerDelegate, which is owned by
245 MockDaemonControllerDelegate
* daemon_controller_delegate_
;
252 // Each test creates two unidirectional pipes: "input" and "output".
253 // Me2MeNativeMessagingHost reads from input_read_handle and writes to
254 // output_write_file. The unittest supplies data to input_write_handle, and
255 // verifies output from output_read_handle.
257 // unittest -> [input] -> Me2MeNativeMessagingHost -> [output] -> unittest
258 base::File input_write_file_
;
259 base::File output_read_file_
;
261 // Message loop of the test thread.
262 scoped_ptr
<base::MessageLoop
> test_message_loop_
;
263 scoped_ptr
<base::RunLoop
> test_run_loop_
;
265 scoped_ptr
<base::Thread
> host_thread_
;
266 scoped_ptr
<base::RunLoop
> host_run_loop_
;
268 // Task runner of the host thread.
269 scoped_refptr
<AutoThreadTaskRunner
> host_task_runner_
;
270 scoped_ptr
<remoting::Me2MeNativeMessagingHost
> host_
;
272 DISALLOW_COPY_AND_ASSIGN(Me2MeNativeMessagingHostTest
);
275 Me2MeNativeMessagingHostTest::Me2MeNativeMessagingHostTest() {}
277 Me2MeNativeMessagingHostTest::~Me2MeNativeMessagingHostTest() {}
279 void Me2MeNativeMessagingHostTest::SetUp() {
280 base::File input_read_file
;
281 base::File output_write_file
;
283 ASSERT_TRUE(MakePipe(&input_read_file
, &input_write_file_
));
284 ASSERT_TRUE(MakePipe(&output_read_file_
, &output_write_file
));
286 test_message_loop_
.reset(new base::MessageLoop());
287 test_run_loop_
.reset(new base::RunLoop());
289 // Run the host on a dedicated thread.
290 host_thread_
.reset(new base::Thread("host_thread"));
291 host_thread_
->Start();
293 // Arrange to run |test_message_loop_| until no components depend on it.
294 host_task_runner_
= new AutoThreadTaskRunner(
295 host_thread_
->message_loop_proxy(),
296 base::Bind(&Me2MeNativeMessagingHostTest::ExitTest
,
297 base::Unretained(this)));
299 host_task_runner_
->PostTask(
301 base::Bind(&Me2MeNativeMessagingHostTest::StartHost
,
302 base::Unretained(this)));
304 // Wait until the host finishes starting.
305 test_run_loop_
->Run();
308 void Me2MeNativeMessagingHostTest::StartHost() {
309 DCHECK(host_task_runner_
->RunsTasksOnCurrentThread());
311 base::File input_read_file
;
312 base::File output_write_file
;
314 ASSERT_TRUE(MakePipe(&input_read_file
, &input_write_file_
));
315 ASSERT_TRUE(MakePipe(&output_read_file_
, &output_write_file
));
317 daemon_controller_delegate_
= new MockDaemonControllerDelegate();
318 scoped_refptr
<DaemonController
> daemon_controller(
319 new DaemonController(make_scoped_ptr(daemon_controller_delegate_
)));
321 scoped_refptr
<PairingRegistry
> pairing_registry
=
322 new SynchronousPairingRegistry(
323 make_scoped_ptr(new MockPairingRegistryDelegate()));
325 scoped_ptr
<extensions::NativeMessagingChannel
> channel(
326 new PipeMessagingChannel(input_read_file
.Pass(),
327 output_write_file
.Pass()));
329 host_
.reset(new Me2MeNativeMessagingHost(
330 false, 0, channel
.Pass(), daemon_controller
, pairing_registry
, nullptr));
331 host_
->Start(base::Bind(&Me2MeNativeMessagingHostTest::StopHost
,
332 base::Unretained(this)));
334 // Notify the test that the host has finished starting up.
335 test_message_loop_
->message_loop_proxy()->PostTask(
336 FROM_HERE
, test_run_loop_
->QuitClosure());
339 void Me2MeNativeMessagingHostTest::StopHost() {
340 DCHECK(host_task_runner_
->RunsTasksOnCurrentThread());
344 // Wait till all shutdown tasks have completed.
345 base::RunLoop().RunUntilIdle();
347 // Trigger a test shutdown via ExitTest().
348 host_task_runner_
= NULL
;
351 void Me2MeNativeMessagingHostTest::ExitTest() {
352 if (!test_message_loop_
->message_loop_proxy()->RunsTasksOnCurrentThread()) {
353 test_message_loop_
->message_loop_proxy()->PostTask(
355 base::Bind(&Me2MeNativeMessagingHostTest::ExitTest
,
356 base::Unretained(this)));
359 test_run_loop_
->Quit();
362 void Me2MeNativeMessagingHostTest::TearDown() {
363 // Closing the write-end of the input will send an EOF to the native
364 // messaging reader. This will trigger a host shutdown.
365 input_write_file_
.Close();
367 // Start a new RunLoop and Wait until the host finishes shutting down.
368 test_run_loop_
.reset(new base::RunLoop());
369 test_run_loop_
->Run();
371 // Verify there are no more message in the output pipe.
372 scoped_ptr
<base::DictionaryValue
> response
= ReadMessageFromOutputPipe();
373 EXPECT_FALSE(response
);
375 // The It2MeMe2MeNativeMessagingHost dtor closes the handles that are passed
376 // to it. So the only handle left to close is |output_read_file_|.
377 output_read_file_
.Close();
380 scoped_ptr
<base::DictionaryValue
>
381 Me2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() {
383 int read_result
= output_read_file_
.ReadAtCurrentPos(
384 reinterpret_cast<char*>(&length
), sizeof(length
));
385 if (read_result
!= sizeof(length
)) {
389 std::string
message_json(length
, '\0');
390 read_result
= output_read_file_
.ReadAtCurrentPos(
391 string_as_array(&message_json
), length
);
392 if (read_result
!= static_cast<int>(length
)) {
396 scoped_ptr
<base::Value
> message(base::JSONReader::Read(message_json
));
397 if (!message
|| !message
->IsType(base::Value::TYPE_DICTIONARY
)) {
401 return make_scoped_ptr(
402 static_cast<base::DictionaryValue
*>(message
.release()));
405 void Me2MeNativeMessagingHostTest::WriteMessageToInputPipe(
406 const base::Value
& message
) {
407 std::string message_json
;
408 base::JSONWriter::Write(&message
, &message_json
);
410 uint32 length
= message_json
.length();
411 input_write_file_
.WriteAtCurrentPos(reinterpret_cast<char*>(&length
),
413 input_write_file_
.WriteAtCurrentPos(message_json
.data(), length
);
416 void Me2MeNativeMessagingHostTest::TestBadRequest(const base::Value
& message
) {
417 base::DictionaryValue good_message
;
418 good_message
.SetString("type", "hello");
420 // This test currently relies on synchronous processing of hello messages and
421 // message parameters verification.
422 WriteMessageToInputPipe(good_message
);
423 WriteMessageToInputPipe(message
);
424 WriteMessageToInputPipe(good_message
);
426 // Read from output pipe, and verify responses.
427 scoped_ptr
<base::DictionaryValue
> response
= ReadMessageFromOutputPipe();
428 VerifyHelloResponse(response
.Pass());
430 response
= ReadMessageFromOutputPipe();
431 EXPECT_FALSE(response
);
434 // TODO (weitaosu): crbug.com/323306. Re-enable these tests.
435 // Test all valid request-types.
436 TEST_F(Me2MeNativeMessagingHostTest
, All
) {
438 base::DictionaryValue message
;
439 message
.SetInteger("id", next_id
++);
440 message
.SetString("type", "hello");
441 WriteMessageToInputPipe(message
);
443 message
.SetInteger("id", next_id
++);
444 message
.SetString("type", "getHostName");
445 WriteMessageToInputPipe(message
);
447 message
.SetInteger("id", next_id
++);
448 message
.SetString("type", "getPinHash");
449 message
.SetString("hostId", "my_host");
450 message
.SetString("pin", "1234");
451 WriteMessageToInputPipe(message
);
454 message
.SetInteger("id", next_id
++);
455 message
.SetString("type", "generateKeyPair");
456 WriteMessageToInputPipe(message
);
458 message
.SetInteger("id", next_id
++);
459 message
.SetString("type", "getDaemonConfig");
460 WriteMessageToInputPipe(message
);
462 message
.SetInteger("id", next_id
++);
463 message
.SetString("type", "getUsageStatsConsent");
464 WriteMessageToInputPipe(message
);
466 message
.SetInteger("id", next_id
++);
467 message
.SetString("type", "stopDaemon");
468 WriteMessageToInputPipe(message
);
470 message
.SetInteger("id", next_id
++);
471 message
.SetString("type", "getDaemonState");
472 WriteMessageToInputPipe(message
);
474 // Following messages require a "config" dictionary.
475 base::DictionaryValue config
;
476 config
.SetBoolean("update", true);
477 message
.Set("config", config
.DeepCopy());
478 message
.SetInteger("id", next_id
++);
479 message
.SetString("type", "updateDaemonConfig");
480 WriteMessageToInputPipe(message
);
483 config
.SetBoolean("start", true);
484 message
.Set("config", config
.DeepCopy());
485 message
.SetBoolean("consent", true);
486 message
.SetInteger("id", next_id
++);
487 message
.SetString("type", "startDaemon");
488 WriteMessageToInputPipe(message
);
490 void (*verify_routines
[])(scoped_ptr
<base::DictionaryValue
>) = {
491 &VerifyHelloResponse
,
492 &VerifyGetHostNameResponse
,
493 &VerifyGetPinHashResponse
,
494 &VerifyGenerateKeyPairResponse
,
495 &VerifyGetDaemonConfigResponse
,
496 &VerifyGetUsageStatsConsentResponse
,
497 &VerifyStopDaemonResponse
,
498 &VerifyGetDaemonStateResponse
,
499 &VerifyUpdateDaemonConfigResponse
,
500 &VerifyStartDaemonResponse
,
502 ASSERT_EQ(arraysize(verify_routines
), static_cast<size_t>(next_id
));
504 // Read all responses from output pipe, and verify them.
505 for (int i
= 0; i
< next_id
; ++i
) {
506 scoped_ptr
<base::DictionaryValue
> response
= ReadMessageFromOutputPipe();
508 // Make sure that id is available and is in the range.
510 ASSERT_TRUE(response
->GetInteger("id", &id
));
511 ASSERT_TRUE(0 <= id
&& id
< next_id
);
513 // Call the verification routine corresponding to the message id.
514 ASSERT_TRUE(verify_routines
[id
]);
515 verify_routines
[id
](response
.Pass());
517 // Clear the pointer so that the routine cannot be called the second time.
518 verify_routines
[id
] = NULL
;
522 // Verify that response ID matches request ID.
523 TEST_F(Me2MeNativeMessagingHostTest
, Id
) {
524 base::DictionaryValue message
;
525 message
.SetString("type", "hello");
526 WriteMessageToInputPipe(message
);
527 message
.SetString("id", "42");
528 WriteMessageToInputPipe(message
);
530 scoped_ptr
<base::DictionaryValue
> response
= ReadMessageFromOutputPipe();
531 EXPECT_TRUE(response
);
533 EXPECT_FALSE(response
->GetString("id", &value
));
535 response
= ReadMessageFromOutputPipe();
536 EXPECT_TRUE(response
);
537 EXPECT_TRUE(response
->GetString("id", &value
));
538 EXPECT_EQ("42", value
);
541 // Verify non-Dictionary requests are rejected.
542 TEST_F(Me2MeNativeMessagingHostTest
, WrongFormat
) {
543 base::ListValue message
;
544 TestBadRequest(message
);
547 // Verify requests with no type are rejected.
548 TEST_F(Me2MeNativeMessagingHostTest
, MissingType
) {
549 base::DictionaryValue message
;
550 TestBadRequest(message
);
553 // Verify rejection if type is unrecognized.
554 TEST_F(Me2MeNativeMessagingHostTest
, InvalidType
) {
555 base::DictionaryValue message
;
556 message
.SetString("type", "xxx");
557 TestBadRequest(message
);
560 // Verify rejection if getPinHash request has no hostId.
561 TEST_F(Me2MeNativeMessagingHostTest
, GetPinHashNoHostId
) {
562 base::DictionaryValue message
;
563 message
.SetString("type", "getPinHash");
564 message
.SetString("pin", "1234");
565 TestBadRequest(message
);
568 // Verify rejection if getPinHash request has no pin.
569 TEST_F(Me2MeNativeMessagingHostTest
, GetPinHashNoPin
) {
570 base::DictionaryValue message
;
571 message
.SetString("type", "getPinHash");
572 message
.SetString("hostId", "my_host");
573 TestBadRequest(message
);
576 // Verify rejection if updateDaemonConfig request has invalid config.
577 TEST_F(Me2MeNativeMessagingHostTest
, UpdateDaemonConfigInvalidConfig
) {
578 base::DictionaryValue message
;
579 message
.SetString("type", "updateDaemonConfig");
580 message
.SetString("config", "xxx");
581 TestBadRequest(message
);
584 // Verify rejection if startDaemon request has invalid config.
585 TEST_F(Me2MeNativeMessagingHostTest
, StartDaemonInvalidConfig
) {
586 base::DictionaryValue message
;
587 message
.SetString("type", "startDaemon");
588 message
.SetString("config", "xxx");
589 message
.SetBoolean("consent", true);
590 TestBadRequest(message
);
593 // Verify rejection if startDaemon request has no "consent" parameter.
594 TEST_F(Me2MeNativeMessagingHostTest
, StartDaemonNoConsent
) {
595 base::DictionaryValue message
;
596 message
.SetString("type", "startDaemon");
597 message
.Set("config", base::DictionaryValue().DeepCopy());
598 TestBadRequest(message
);
601 } // namespace remoting