OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/it2me/it2me_native_messaging_host.h" | 5 #include "remoting/host/it2me/it2me_native_messaging_host.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
15 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
19 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
20 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
21 #include "base/stl_util.h" | 21 #include "base/stl_util.h" |
22 #include "base/strings/stringize_macros.h" | 22 #include "base/strings/stringize_macros.h" |
23 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
24 #include "base/values.h" | 24 #include "base/values.h" |
25 #include "components/policy/core/common/fake_async_policy_loader.h" | 25 #include "components/policy/core/common/fake_async_policy_loader.h" |
26 #include "components/policy/core/common/mock_policy_service.h" | 26 #include "components/policy/core/common/mock_policy_service.h" |
| 27 #include "components/policy/policy_constants.h" |
27 #include "net/base/file_stream.h" | 28 #include "net/base/file_stream.h" |
28 #include "remoting/base/auto_thread_task_runner.h" | 29 #include "remoting/base/auto_thread_task_runner.h" |
29 #include "remoting/host/chromoting_host_context.h" | 30 #include "remoting/host/chromoting_host_context.h" |
30 #include "remoting/host/native_messaging/log_message_handler.h" | 31 #include "remoting/host/native_messaging/log_message_handler.h" |
31 #include "remoting/host/native_messaging/native_messaging_pipe.h" | 32 #include "remoting/host/native_messaging/native_messaging_pipe.h" |
32 #include "remoting/host/native_messaging/pipe_messaging_channel.h" | 33 #include "remoting/host/native_messaging/pipe_messaging_channel.h" |
33 #include "remoting/host/policy_watcher.h" | 34 #include "remoting/host/policy_watcher.h" |
34 #include "remoting/host/setup/test_util.h" | 35 #include "remoting/host/setup/test_util.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
36 | 37 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 | 189 |
189 class It2MeNativeMessagingHostTest : public testing::Test { | 190 class It2MeNativeMessagingHostTest : public testing::Test { |
190 public: | 191 public: |
191 It2MeNativeMessagingHostTest() {} | 192 It2MeNativeMessagingHostTest() {} |
192 ~It2MeNativeMessagingHostTest() override {} | 193 ~It2MeNativeMessagingHostTest() override {} |
193 | 194 |
194 void SetUp() override; | 195 void SetUp() override; |
195 void TearDown() override; | 196 void TearDown() override; |
196 | 197 |
197 protected: | 198 protected: |
| 199 void SetPolicies(const base::DictionaryValue& dict); |
198 std::unique_ptr<base::DictionaryValue> ReadMessageFromOutputPipe(); | 200 std::unique_ptr<base::DictionaryValue> ReadMessageFromOutputPipe(); |
199 void WriteMessageToInputPipe(const base::Value& message); | 201 void WriteMessageToInputPipe(const base::Value& message); |
200 | 202 |
201 void VerifyHelloResponse(int request_id); | 203 void VerifyHelloResponse(int request_id); |
202 void VerifyErrorResponse(); | 204 void VerifyErrorResponse(); |
203 void VerifyConnectResponses(int request_id); | 205 void VerifyConnectResponses(int request_id); |
204 void VerifyDisconnectResponses(int request_id); | 206 void VerifyDisconnectResponses(int request_id); |
| 207 void VerifyPolicyErrorResponse(); |
205 | 208 |
206 // The Host process should shut down when it receives a malformed request. | 209 // The Host process should shut down when it receives a malformed request. |
207 // This is tested by sending a known-good request, followed by |message|, | 210 // This is tested by sending a known-good request, followed by |message|, |
208 // followed by the known-good request again. The response file should only | 211 // followed by the known-good request again. The response file should only |
209 // contain a single response from the first good request. | 212 // contain a single response from the first good request. |
210 void TestBadRequest(const base::Value& message, bool expect_error_response); | 213 void TestBadRequest(const base::Value& message, bool expect_error_response); |
211 void TestConnect(); | 214 void TestConnect(); |
212 | 215 |
| 216 void SendConnectMessage(int id); |
| 217 void SendDisconnectMessage(int id); |
| 218 |
213 private: | 219 private: |
214 void StartHost(); | 220 void StartHost(); |
215 void ExitTest(); | 221 void ExitTest(); |
| 222 void ExitPolicyRunLoop(); |
216 | 223 |
217 // Each test creates two unidirectional pipes: "input" and "output". | 224 // Each test creates two unidirectional pipes: "input" and "output". |
218 // It2MeNativeMessagingHost reads from input_read_file and writes to | 225 // It2MeNativeMessagingHost reads from input_read_file and writes to |
219 // output_write_file. The unittest supplies data to input_write_handle, and | 226 // output_write_file. The unittest supplies data to input_write_handle, and |
220 // verifies output from output_read_handle. | 227 // verifies output from output_read_handle. |
221 // | 228 // |
222 // unittest -> [input] -> It2MeNativeMessagingHost -> [output] -> unittest | 229 // unittest -> [input] -> It2MeNativeMessagingHost -> [output] -> unittest |
223 base::File input_write_file_; | 230 base::File input_write_file_; |
224 base::File output_read_file_; | 231 base::File output_read_file_; |
225 | 232 |
226 // Message loop of the test thread. | 233 // Message loop of the test thread. |
227 std::unique_ptr<base::MessageLoop> test_message_loop_; | 234 std::unique_ptr<base::MessageLoop> test_message_loop_; |
228 std::unique_ptr<base::RunLoop> test_run_loop_; | 235 std::unique_ptr<base::RunLoop> test_run_loop_; |
229 | 236 |
230 std::unique_ptr<base::Thread> host_thread_; | 237 std::unique_ptr<base::Thread> host_thread_; |
231 std::unique_ptr<base::RunLoop> host_run_loop_; | 238 std::unique_ptr<base::RunLoop> host_run_loop_; |
232 | 239 |
| 240 std::unique_ptr<base::RunLoop> policy_run_loop_; |
| 241 |
| 242 // Retain a raw pointer to |policy_loader_| in order to control the policy |
| 243 // contents. |
| 244 policy::FakeAsyncPolicyLoader* policy_loader_ = nullptr; |
| 245 |
233 // Task runner of the host thread. | 246 // Task runner of the host thread. |
234 scoped_refptr<AutoThreadTaskRunner> host_task_runner_; | 247 scoped_refptr<AutoThreadTaskRunner> host_task_runner_; |
235 std::unique_ptr<remoting::NativeMessagingPipe> pipe_; | 248 std::unique_ptr<remoting::NativeMessagingPipe> pipe_; |
236 | 249 |
237 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest); | 250 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest); |
238 }; | 251 }; |
239 | 252 |
240 void It2MeNativeMessagingHostTest::SetUp() { | 253 void It2MeNativeMessagingHostTest::SetUp() { |
241 test_message_loop_.reset(new base::MessageLoop()); | 254 test_message_loop_.reset(new base::MessageLoop()); |
242 test_run_loop_.reset(new base::RunLoop()); | 255 test_run_loop_.reset(new base::RunLoop()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 | 287 |
275 // Verify there are no more message in the output pipe. | 288 // Verify there are no more message in the output pipe. |
276 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); | 289 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); |
277 EXPECT_FALSE(response); | 290 EXPECT_FALSE(response); |
278 | 291 |
279 // The It2MeNativeMessagingHost dtor closes the handles that are passed to it. | 292 // The It2MeNativeMessagingHost dtor closes the handles that are passed to it. |
280 // So the only handle left to close is |output_read_file_|. | 293 // So the only handle left to close is |output_read_file_|. |
281 output_read_file_.Close(); | 294 output_read_file_.Close(); |
282 } | 295 } |
283 | 296 |
| 297 void It2MeNativeMessagingHostTest::SetPolicies( |
| 298 const base::DictionaryValue& dict) { |
| 299 DCHECK(test_message_loop_->task_runner()->RunsTasksOnCurrentThread()); |
| 300 // Copy |dict| into |policy_bundle|. |
| 301 policy::PolicyNamespace policy_namespace = |
| 302 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()); |
| 303 policy::PolicyBundle policy_bundle; |
| 304 policy::PolicyMap& policy_map = policy_bundle.Get(policy_namespace); |
| 305 policy_map.LoadFrom(&dict, policy::POLICY_LEVEL_MANDATORY, |
| 306 policy::POLICY_SCOPE_MACHINE, |
| 307 policy::POLICY_SOURCE_CLOUD); |
| 308 |
| 309 // Simulate a policy update and wait for it to complete. |
| 310 policy_run_loop_.reset(new base::RunLoop); |
| 311 policy_loader_->SetPolicies(policy_bundle); |
| 312 policy_loader_->PostReloadOnBackgroundThread(true /* force reload asap */); |
| 313 policy_run_loop_->Run(); |
| 314 policy_run_loop_.reset(nullptr); |
| 315 } |
| 316 |
284 std::unique_ptr<base::DictionaryValue> | 317 std::unique_ptr<base::DictionaryValue> |
285 It2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() { | 318 It2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() { |
286 while (true) { | 319 while (true) { |
287 uint32_t length; | 320 uint32_t length; |
288 int read_result = output_read_file_.ReadAtCurrentPos( | 321 int read_result = output_read_file_.ReadAtCurrentPos( |
289 reinterpret_cast<char*>(&length), sizeof(length)); | 322 reinterpret_cast<char*>(&length), sizeof(length)); |
290 if (read_result != sizeof(length)) { | 323 if (read_result != sizeof(length)) { |
291 // The output pipe has been closed, return an empty message. | 324 // The output pipe has been closed, return an empty message. |
292 return nullptr; | 325 return nullptr; |
293 } | 326 } |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 disconnected_received = true; | 467 disconnected_received = true; |
435 } else { | 468 } else { |
436 ADD_FAILURE() << "Unexpected host state: " << state; | 469 ADD_FAILURE() << "Unexpected host state: " << state; |
437 } | 470 } |
438 } else { | 471 } else { |
439 ADD_FAILURE() << "Unexpected message type: " << type; | 472 ADD_FAILURE() << "Unexpected message type: " << type; |
440 } | 473 } |
441 } | 474 } |
442 } | 475 } |
443 | 476 |
| 477 void It2MeNativeMessagingHostTest::VerifyPolicyErrorResponse() { |
| 478 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); |
| 479 ASSERT_TRUE(response); |
| 480 std::string type; |
| 481 ASSERT_TRUE(response->GetString("type", &type)); |
| 482 ASSERT_EQ("policyError", type); |
| 483 } |
| 484 |
444 void It2MeNativeMessagingHostTest::TestBadRequest(const base::Value& message, | 485 void It2MeNativeMessagingHostTest::TestBadRequest(const base::Value& message, |
445 bool expect_error_response) { | 486 bool expect_error_response) { |
446 base::DictionaryValue good_message; | 487 base::DictionaryValue good_message; |
447 good_message.SetString("type", "hello"); | 488 good_message.SetString("type", "hello"); |
448 good_message.SetInteger("id", 1); | 489 good_message.SetInteger("id", 1); |
449 | 490 |
450 WriteMessageToInputPipe(good_message); | 491 WriteMessageToInputPipe(good_message); |
451 WriteMessageToInputPipe(message); | 492 WriteMessageToInputPipe(message); |
452 WriteMessageToInputPipe(good_message); | 493 WriteMessageToInputPipe(good_message); |
453 | 494 |
(...skipping 15 matching lines...) Expand all Loading... |
469 | 510 |
470 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); | 511 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); |
471 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); | 512 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); |
472 | 513 |
473 pipe_.reset(new NativeMessagingPipe()); | 514 pipe_.reset(new NativeMessagingPipe()); |
474 | 515 |
475 std::unique_ptr<extensions::NativeMessagingChannel> channel( | 516 std::unique_ptr<extensions::NativeMessagingChannel> channel( |
476 new PipeMessagingChannel(std::move(input_read_file), | 517 new PipeMessagingChannel(std::move(input_read_file), |
477 std::move(output_write_file))); | 518 std::move(output_write_file))); |
478 | 519 |
479 // Creating a native messaging host with a mock It2MeHostFactory. | 520 // Creating a native messaging host with a mock It2MeHostFactory and policy |
| 521 // loader. |
480 std::unique_ptr<ChromotingHostContext> context = | 522 std::unique_ptr<ChromotingHostContext> context = |
481 ChromotingHostContext::Create(host_task_runner_); | 523 ChromotingHostContext::Create(host_task_runner_); |
| 524 auto policy_loader = |
| 525 base::MakeUnique<policy::FakeAsyncPolicyLoader>(host_task_runner_); |
| 526 policy_loader_ = policy_loader.get(); |
482 std::unique_ptr<PolicyWatcher> policy_watcher = | 527 std::unique_ptr<PolicyWatcher> policy_watcher = |
483 PolicyWatcher::CreateFromPolicyLoaderForTesting( | 528 PolicyWatcher::CreateFromPolicyLoaderForTesting(std::move(policy_loader)); |
484 base::MakeUnique<policy::FakeAsyncPolicyLoader>( | 529 std::unique_ptr<It2MeNativeMessagingHost> it2me_host( |
485 base::ThreadTaskRunnerHandle::Get())); | |
486 std::unique_ptr<extensions::NativeMessageHost> it2me_host( | |
487 new It2MeNativeMessagingHost( | 530 new It2MeNativeMessagingHost( |
488 /*needs_elevation=*/false, std::move(policy_watcher), | 531 /*needs_elevation=*/false, std::move(policy_watcher), |
489 std::move(context), base::WrapUnique(new MockIt2MeHostFactory()))); | 532 std::move(context), base::WrapUnique(new MockIt2MeHostFactory()))); |
| 533 it2me_host->SetPolicyErrorClosureForTesting( |
| 534 base::Bind(base::IgnoreResult(&base::TaskRunner::PostTask), |
| 535 test_message_loop_->task_runner(), FROM_HERE, |
| 536 base::Bind(&It2MeNativeMessagingHostTest::ExitPolicyRunLoop, |
| 537 base::Unretained(this)))); |
490 it2me_host->Start(pipe_.get()); | 538 it2me_host->Start(pipe_.get()); |
491 | 539 |
492 pipe_->Start(std::move(it2me_host), std::move(channel)); | 540 pipe_->Start(std::move(it2me_host), std::move(channel)); |
493 | 541 |
494 // Notify the test that the host has finished starting up. | 542 // Notify the test that the host has finished starting up. |
495 test_message_loop_->task_runner()->PostTask( | 543 test_message_loop_->task_runner()->PostTask( |
496 FROM_HERE, test_run_loop_->QuitClosure()); | 544 FROM_HERE, test_run_loop_->QuitClosure()); |
497 } | 545 } |
498 | 546 |
499 void It2MeNativeMessagingHostTest::ExitTest() { | 547 void It2MeNativeMessagingHostTest::ExitTest() { |
500 if (!test_message_loop_->task_runner()->RunsTasksOnCurrentThread()) { | 548 if (!test_message_loop_->task_runner()->RunsTasksOnCurrentThread()) { |
501 test_message_loop_->task_runner()->PostTask( | 549 test_message_loop_->task_runner()->PostTask( |
502 FROM_HERE, | 550 FROM_HERE, |
503 base::Bind(&It2MeNativeMessagingHostTest::ExitTest, | 551 base::Bind(&It2MeNativeMessagingHostTest::ExitTest, |
504 base::Unretained(this))); | 552 base::Unretained(this))); |
505 return; | 553 return; |
506 } | 554 } |
507 test_run_loop_->Quit(); | 555 test_run_loop_->Quit(); |
508 } | 556 } |
509 | 557 |
510 void It2MeNativeMessagingHostTest::TestConnect() { | 558 void It2MeNativeMessagingHostTest::ExitPolicyRunLoop() { |
| 559 DCHECK(test_message_loop_->task_runner()->RunsTasksOnCurrentThread()); |
| 560 if (policy_run_loop_) { |
| 561 policy_run_loop_->Quit(); |
| 562 } |
| 563 } |
| 564 |
| 565 void It2MeNativeMessagingHostTest::SendConnectMessage(int id) { |
511 base::DictionaryValue connect_message; | 566 base::DictionaryValue connect_message; |
512 int next_id = 0; | 567 connect_message.SetInteger("id", id); |
513 | |
514 // Send the "connect" request. | |
515 connect_message.SetInteger("id", ++next_id); | |
516 connect_message.SetString("type", "connect"); | 568 connect_message.SetString("type", "connect"); |
517 connect_message.SetString("xmppServerAddress", "talk.google.com:5222"); | 569 connect_message.SetString("xmppServerAddress", "talk.google.com:5222"); |
518 connect_message.SetBoolean("xmppServerUseTls", true); | 570 connect_message.SetBoolean("xmppServerUseTls", true); |
519 connect_message.SetString("directoryBotJid", "remoting@bot.talk.google.com"); | 571 connect_message.SetString("directoryBotJid", "remoting@bot.talk.google.com"); |
520 connect_message.SetString("userName", "chromo.pyauto@gmail.com"); | 572 connect_message.SetString("userName", "chromo.pyauto@gmail.com"); |
521 connect_message.SetString("authServiceWithToken", "oauth2:sometoken"); | 573 connect_message.SetString("authServiceWithToken", "oauth2:sometoken"); |
522 WriteMessageToInputPipe(connect_message); | 574 WriteMessageToInputPipe(connect_message); |
| 575 } |
523 | 576 |
524 VerifyConnectResponses(next_id); | 577 void It2MeNativeMessagingHostTest::SendDisconnectMessage(int id) { |
525 | |
526 base::DictionaryValue disconnect_message; | 578 base::DictionaryValue disconnect_message; |
527 disconnect_message.SetInteger("id", ++next_id); | 579 disconnect_message.SetInteger("id", id); |
528 disconnect_message.SetString("type", "disconnect"); | 580 disconnect_message.SetString("type", "disconnect"); |
529 WriteMessageToInputPipe(disconnect_message); | 581 WriteMessageToInputPipe(disconnect_message); |
| 582 } |
530 | 583 |
| 584 void It2MeNativeMessagingHostTest::TestConnect() { |
| 585 int next_id = 1; |
| 586 SendConnectMessage(next_id); |
| 587 VerifyConnectResponses(next_id); |
| 588 ++next_id; |
| 589 SendDisconnectMessage(next_id); |
531 VerifyDisconnectResponses(next_id); | 590 VerifyDisconnectResponses(next_id); |
532 } | 591 } |
533 | 592 |
534 // Test hello request. | 593 // Test hello request. |
535 TEST_F(It2MeNativeMessagingHostTest, Hello) { | 594 TEST_F(It2MeNativeMessagingHostTest, Hello) { |
536 int next_id = 0; | 595 int next_id = 0; |
537 base::DictionaryValue message; | 596 base::DictionaryValue message; |
538 message.SetInteger("id", ++next_id); | 597 message.SetInteger("id", ++next_id); |
539 message.SetString("type", "hello"); | 598 message.SetString("type", "hello"); |
540 WriteMessageToInputPipe(message); | 599 WriteMessageToInputPipe(message); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 TestBadRequest(message, true); | 642 TestBadRequest(message, true); |
584 } | 643 } |
585 | 644 |
586 // Verify rejection if type is unrecognized. | 645 // Verify rejection if type is unrecognized. |
587 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { | 646 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { |
588 base::DictionaryValue message; | 647 base::DictionaryValue message; |
589 message.SetString("type", "xxx"); | 648 message.SetString("type", "xxx"); |
590 TestBadRequest(message, true); | 649 TestBadRequest(message, true); |
591 } | 650 } |
592 | 651 |
| 652 // Verify rejection if type is unrecognized. |
| 653 TEST_F(It2MeNativeMessagingHostTest, BadPoliciesBeforeConnect) { |
| 654 base::DictionaryValue bad_policy; |
| 655 bad_policy.SetInteger(policy::key::kRemoteAccessHostFirewallTraversal, 1); |
| 656 SetPolicies(bad_policy); |
| 657 SendConnectMessage(1); |
| 658 VerifyPolicyErrorResponse(); |
| 659 } |
| 660 |
| 661 // Verify rejection if type is unrecognized. |
| 662 TEST_F(It2MeNativeMessagingHostTest, BadPoliciesAfterConnect) { |
| 663 base::DictionaryValue bad_policy; |
| 664 bad_policy.SetInteger(policy::key::kRemoteAccessHostFirewallTraversal, 1); |
| 665 SendConnectMessage(1); |
| 666 VerifyConnectResponses(1); |
| 667 SetPolicies(bad_policy); |
| 668 VerifyPolicyErrorResponse(); |
| 669 } |
| 670 |
593 } // namespace remoting | 671 } // namespace remoting |
OLD | NEW |