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(); |
216 | 222 |
217 // Each test creates two unidirectional pipes: "input" and "output". | 223 // Each test creates two unidirectional pipes: "input" and "output". |
218 // It2MeNativeMessagingHost reads from input_read_file and writes to | 224 // It2MeNativeMessagingHost reads from input_read_file and writes to |
219 // output_write_file. The unittest supplies data to input_write_handle, and | 225 // output_write_file. The unittest supplies data to input_write_handle, and |
220 // verifies output from output_read_handle. | 226 // verifies output from output_read_handle. |
221 // | 227 // |
222 // unittest -> [input] -> It2MeNativeMessagingHost -> [output] -> unittest | 228 // unittest -> [input] -> It2MeNativeMessagingHost -> [output] -> unittest |
223 base::File input_write_file_; | 229 base::File input_write_file_; |
224 base::File output_read_file_; | 230 base::File output_read_file_; |
225 | 231 |
226 // Message loop of the test thread. | 232 // Message loop of the test thread. |
227 std::unique_ptr<base::MessageLoop> test_message_loop_; | 233 std::unique_ptr<base::MessageLoop> test_message_loop_; |
228 std::unique_ptr<base::RunLoop> test_run_loop_; | 234 std::unique_ptr<base::RunLoop> test_run_loop_; |
229 | 235 |
230 std::unique_ptr<base::Thread> host_thread_; | 236 std::unique_ptr<base::Thread> host_thread_; |
231 std::unique_ptr<base::RunLoop> host_run_loop_; | 237 std::unique_ptr<base::RunLoop> host_run_loop_; |
232 | 238 |
239 // Retain a raw pointer to |policy_loader_| in order to control the policy | |
240 // contents. | |
241 policy::FakeAsyncPolicyLoader* policy_loader_ = nullptr; | |
242 | |
243 // Retain a raw pointer to the native messaging host to set the policy update | |
244 // closure. | |
245 It2MeNativeMessagingHost* it2me_host_ = nullptr; | |
246 | |
233 // Task runner of the host thread. | 247 // Task runner of the host thread. |
234 scoped_refptr<AutoThreadTaskRunner> host_task_runner_; | 248 scoped_refptr<AutoThreadTaskRunner> host_task_runner_; |
235 std::unique_ptr<remoting::NativeMessagingPipe> pipe_; | 249 std::unique_ptr<remoting::NativeMessagingPipe> pipe_; |
236 | 250 |
237 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest); | 251 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest); |
238 }; | 252 }; |
239 | 253 |
240 void It2MeNativeMessagingHostTest::SetUp() { | 254 void It2MeNativeMessagingHostTest::SetUp() { |
241 test_message_loop_.reset(new base::MessageLoop()); | 255 test_message_loop_.reset(new base::MessageLoop()); |
242 test_run_loop_.reset(new base::RunLoop()); | 256 test_run_loop_.reset(new base::RunLoop()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 | 288 |
275 // Verify there are no more message in the output pipe. | 289 // Verify there are no more message in the output pipe. |
276 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); | 290 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); |
277 EXPECT_FALSE(response); | 291 EXPECT_FALSE(response); |
278 | 292 |
279 // The It2MeNativeMessagingHost dtor closes the handles that are passed to it. | 293 // The It2MeNativeMessagingHost dtor closes the handles that are passed to it. |
280 // So the only handle left to close is |output_read_file_|. | 294 // So the only handle left to close is |output_read_file_|. |
281 output_read_file_.Close(); | 295 output_read_file_.Close(); |
282 } | 296 } |
283 | 297 |
298 void It2MeNativeMessagingHostTest::SetPolicies( | |
299 const base::DictionaryValue& dict) { | |
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 base::RunLoop run_loop; | |
311 it2me_host_->SetPolicyUpdateClosureForTesting( | |
312 base::Bind(base::IgnoreResult(&base::TaskRunner::PostTask), | |
313 base::MessageLoop::current()->task_runner(), FROM_HERE, | |
314 run_loop.QuitClosure())); | |
joedow
2017/05/10 00:55:04
I think this is fine, another option would be to p
| |
315 policy_loader_->SetPolicies(policy_bundle); | |
316 policy_loader_->PostReloadOnBackgroundThread(true /* force reload asap */); | |
317 run_loop.Run(); | |
318 } | |
319 | |
284 std::unique_ptr<base::DictionaryValue> | 320 std::unique_ptr<base::DictionaryValue> |
285 It2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() { | 321 It2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() { |
286 while (true) { | 322 while (true) { |
287 uint32_t length; | 323 uint32_t length; |
288 int read_result = output_read_file_.ReadAtCurrentPos( | 324 int read_result = output_read_file_.ReadAtCurrentPos( |
289 reinterpret_cast<char*>(&length), sizeof(length)); | 325 reinterpret_cast<char*>(&length), sizeof(length)); |
290 if (read_result != sizeof(length)) { | 326 if (read_result != sizeof(length)) { |
291 // The output pipe has been closed, return an empty message. | 327 // The output pipe has been closed, return an empty message. |
292 return nullptr; | 328 return nullptr; |
293 } | 329 } |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
434 disconnected_received = true; | 470 disconnected_received = true; |
435 } else { | 471 } else { |
436 ADD_FAILURE() << "Unexpected host state: " << state; | 472 ADD_FAILURE() << "Unexpected host state: " << state; |
437 } | 473 } |
438 } else { | 474 } else { |
439 ADD_FAILURE() << "Unexpected message type: " << type; | 475 ADD_FAILURE() << "Unexpected message type: " << type; |
440 } | 476 } |
441 } | 477 } |
442 } | 478 } |
443 | 479 |
480 void It2MeNativeMessagingHostTest::VerifyPolicyErrorResponse() { | |
481 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); | |
482 ASSERT_TRUE(response); | |
483 std::string type; | |
484 ASSERT_TRUE(response->GetString("type", &type)); | |
485 ASSERT_EQ("policyError", type); | |
486 } | |
487 | |
444 void It2MeNativeMessagingHostTest::TestBadRequest(const base::Value& message, | 488 void It2MeNativeMessagingHostTest::TestBadRequest(const base::Value& message, |
445 bool expect_error_response) { | 489 bool expect_error_response) { |
446 base::DictionaryValue good_message; | 490 base::DictionaryValue good_message; |
447 good_message.SetString("type", "hello"); | 491 good_message.SetString("type", "hello"); |
448 good_message.SetInteger("id", 1); | 492 good_message.SetInteger("id", 1); |
449 | 493 |
450 WriteMessageToInputPipe(good_message); | 494 WriteMessageToInputPipe(good_message); |
451 WriteMessageToInputPipe(message); | 495 WriteMessageToInputPipe(message); |
452 WriteMessageToInputPipe(good_message); | 496 WriteMessageToInputPipe(good_message); |
453 | 497 |
(...skipping 15 matching lines...) Expand all Loading... | |
469 | 513 |
470 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); | 514 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); |
471 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); | 515 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); |
472 | 516 |
473 pipe_.reset(new NativeMessagingPipe()); | 517 pipe_.reset(new NativeMessagingPipe()); |
474 | 518 |
475 std::unique_ptr<extensions::NativeMessagingChannel> channel( | 519 std::unique_ptr<extensions::NativeMessagingChannel> channel( |
476 new PipeMessagingChannel(std::move(input_read_file), | 520 new PipeMessagingChannel(std::move(input_read_file), |
477 std::move(output_write_file))); | 521 std::move(output_write_file))); |
478 | 522 |
479 // Creating a native messaging host with a mock It2MeHostFactory. | 523 // Creating a native messaging host with a mock It2MeHostFactory and policy |
524 // loader. | |
480 std::unique_ptr<ChromotingHostContext> context = | 525 std::unique_ptr<ChromotingHostContext> context = |
481 ChromotingHostContext::Create(host_task_runner_); | 526 ChromotingHostContext::Create(host_task_runner_); |
527 auto policy_loader = | |
528 base::MakeUnique<policy::FakeAsyncPolicyLoader>(host_task_runner_); | |
529 policy_loader_ = policy_loader.get(); | |
482 std::unique_ptr<PolicyWatcher> policy_watcher = | 530 std::unique_ptr<PolicyWatcher> policy_watcher = |
483 PolicyWatcher::CreateFromPolicyLoaderForTesting( | 531 PolicyWatcher::CreateFromPolicyLoaderForTesting(std::move(policy_loader)); |
484 base::MakeUnique<policy::FakeAsyncPolicyLoader>( | 532 std::unique_ptr<It2MeNativeMessagingHost> it2me_host( |
485 base::ThreadTaskRunnerHandle::Get())); | |
486 std::unique_ptr<extensions::NativeMessageHost> it2me_host( | |
487 new It2MeNativeMessagingHost( | 533 new It2MeNativeMessagingHost( |
488 /*needs_elevation=*/false, std::move(policy_watcher), | 534 /*needs_elevation=*/false, std::move(policy_watcher), |
489 std::move(context), base::WrapUnique(new MockIt2MeHostFactory()))); | 535 std::move(context), base::WrapUnique(new MockIt2MeHostFactory()))); |
536 it2me_host_ = it2me_host.get(); | |
490 it2me_host->Start(pipe_.get()); | 537 it2me_host->Start(pipe_.get()); |
491 | 538 |
492 pipe_->Start(std::move(it2me_host), std::move(channel)); | 539 pipe_->Start(std::move(it2me_host), std::move(channel)); |
493 | 540 |
494 // Notify the test that the host has finished starting up. | 541 // Notify the test that the host has finished starting up. |
495 test_message_loop_->task_runner()->PostTask( | 542 test_message_loop_->task_runner()->PostTask( |
496 FROM_HERE, test_run_loop_->QuitClosure()); | 543 FROM_HERE, test_run_loop_->QuitClosure()); |
497 } | 544 } |
498 | 545 |
499 void It2MeNativeMessagingHostTest::ExitTest() { | 546 void It2MeNativeMessagingHostTest::ExitTest() { |
500 if (!test_message_loop_->task_runner()->RunsTasksOnCurrentThread()) { | 547 if (!test_message_loop_->task_runner()->RunsTasksOnCurrentThread()) { |
501 test_message_loop_->task_runner()->PostTask( | 548 test_message_loop_->task_runner()->PostTask( |
502 FROM_HERE, | 549 FROM_HERE, |
503 base::Bind(&It2MeNativeMessagingHostTest::ExitTest, | 550 base::Bind(&It2MeNativeMessagingHostTest::ExitTest, |
504 base::Unretained(this))); | 551 base::Unretained(this))); |
505 return; | 552 return; |
506 } | 553 } |
507 test_run_loop_->Quit(); | 554 test_run_loop_->Quit(); |
508 } | 555 } |
509 | 556 |
510 void It2MeNativeMessagingHostTest::TestConnect() { | 557 void It2MeNativeMessagingHostTest::SendConnectMessage(int id) { |
511 base::DictionaryValue connect_message; | 558 base::DictionaryValue connect_message; |
512 int next_id = 0; | 559 connect_message.SetInteger("id", id); |
513 | |
514 // Send the "connect" request. | |
515 connect_message.SetInteger("id", ++next_id); | |
516 connect_message.SetString("type", "connect"); | 560 connect_message.SetString("type", "connect"); |
517 connect_message.SetString("xmppServerAddress", "talk.google.com:5222"); | 561 connect_message.SetString("xmppServerAddress", "talk.google.com:5222"); |
518 connect_message.SetBoolean("xmppServerUseTls", true); | 562 connect_message.SetBoolean("xmppServerUseTls", true); |
519 connect_message.SetString("directoryBotJid", "remoting@bot.talk.google.com"); | 563 connect_message.SetString("directoryBotJid", "remoting@bot.talk.google.com"); |
520 connect_message.SetString("userName", "chromo.pyauto@gmail.com"); | 564 connect_message.SetString("userName", "chromo.pyauto@gmail.com"); |
521 connect_message.SetString("authServiceWithToken", "oauth2:sometoken"); | 565 connect_message.SetString("authServiceWithToken", "oauth2:sometoken"); |
522 WriteMessageToInputPipe(connect_message); | 566 WriteMessageToInputPipe(connect_message); |
567 } | |
523 | 568 |
524 VerifyConnectResponses(next_id); | 569 void It2MeNativeMessagingHostTest::SendDisconnectMessage(int id) { |
525 | |
526 base::DictionaryValue disconnect_message; | 570 base::DictionaryValue disconnect_message; |
527 disconnect_message.SetInteger("id", ++next_id); | 571 disconnect_message.SetInteger("id", id); |
528 disconnect_message.SetString("type", "disconnect"); | 572 disconnect_message.SetString("type", "disconnect"); |
529 WriteMessageToInputPipe(disconnect_message); | 573 WriteMessageToInputPipe(disconnect_message); |
574 } | |
530 | 575 |
576 void It2MeNativeMessagingHostTest::TestConnect() { | |
577 int next_id = 1; | |
578 SendConnectMessage(next_id); | |
579 VerifyConnectResponses(next_id); | |
580 ++next_id; | |
581 SendDisconnectMessage(next_id); | |
531 VerifyDisconnectResponses(next_id); | 582 VerifyDisconnectResponses(next_id); |
532 } | 583 } |
533 | 584 |
534 // Test hello request. | 585 // Test hello request. |
535 TEST_F(It2MeNativeMessagingHostTest, Hello) { | 586 TEST_F(It2MeNativeMessagingHostTest, Hello) { |
536 int next_id = 0; | 587 int next_id = 0; |
537 base::DictionaryValue message; | 588 base::DictionaryValue message; |
538 message.SetInteger("id", ++next_id); | 589 message.SetInteger("id", ++next_id); |
539 message.SetString("type", "hello"); | 590 message.SetString("type", "hello"); |
540 WriteMessageToInputPipe(message); | 591 WriteMessageToInputPipe(message); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
583 TestBadRequest(message, true); | 634 TestBadRequest(message, true); |
584 } | 635 } |
585 | 636 |
586 // Verify rejection if type is unrecognized. | 637 // Verify rejection if type is unrecognized. |
587 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { | 638 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { |
588 base::DictionaryValue message; | 639 base::DictionaryValue message; |
589 message.SetString("type", "xxx"); | 640 message.SetString("type", "xxx"); |
590 TestBadRequest(message, true); | 641 TestBadRequest(message, true); |
591 } | 642 } |
592 | 643 |
644 // Verify rejection if type is unrecognized. | |
645 TEST_F(It2MeNativeMessagingHostTest, BadPoliciesBeforeConnect) { | |
646 base::DictionaryValue bad_policy; | |
647 bad_policy.SetInteger(policy::key::kRemoteAccessHostFirewallTraversal, 1); | |
648 SetPolicies(bad_policy); | |
649 SendConnectMessage(1); | |
650 VerifyPolicyErrorResponse(); | |
651 } | |
652 | |
653 // Verify rejection if type is unrecognized. | |
654 TEST_F(It2MeNativeMessagingHostTest, BadPoliciesAfterConnect) { | |
655 base::DictionaryValue bad_policy; | |
656 bad_policy.SetInteger(policy::key::kRemoteAccessHostFirewallTraversal, 1); | |
657 SendConnectMessage(1); | |
658 VerifyConnectResponses(1); | |
659 SetPolicies(bad_policy); | |
660 VerifyPolicyErrorResponse(); | |
661 } | |
662 | |
593 } // namespace remoting | 663 } // namespace remoting |
OLD | NEW |