Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(345)

Side by Side Diff: remoting/host/it2me/it2me_native_messaging_host_unittest.cc

Issue 2867223003: Send a message to the client if bad It2Me policies are read. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 SendDisonnectMessage(int id);
joedow 2017/05/09 18:09:47 s/SendDisonnectMessage/SendDisconnectMessage
Jamie 2017/05/09 18:19:33 Done.
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
233 // Task runner of the host thread. 243 // Task runner of the host thread.
234 scoped_refptr<AutoThreadTaskRunner> host_task_runner_; 244 scoped_refptr<AutoThreadTaskRunner> host_task_runner_;
235 std::unique_ptr<remoting::NativeMessagingPipe> pipe_; 245 std::unique_ptr<remoting::NativeMessagingPipe> pipe_;
236 246
237 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest); 247 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest);
238 }; 248 };
239 249
240 void It2MeNativeMessagingHostTest::SetUp() { 250 void It2MeNativeMessagingHostTest::SetUp() {
241 test_message_loop_.reset(new base::MessageLoop()); 251 test_message_loop_.reset(new base::MessageLoop());
242 test_run_loop_.reset(new base::RunLoop()); 252 test_run_loop_.reset(new base::RunLoop());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 284
275 // Verify there are no more message in the output pipe. 285 // Verify there are no more message in the output pipe.
276 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); 286 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
277 EXPECT_FALSE(response); 287 EXPECT_FALSE(response);
278 288
279 // The It2MeNativeMessagingHost dtor closes the handles that are passed to it. 289 // The It2MeNativeMessagingHost dtor closes the handles that are passed to it.
280 // So the only handle left to close is |output_read_file_|. 290 // So the only handle left to close is |output_read_file_|.
281 output_read_file_.Close(); 291 output_read_file_.Close();
282 } 292 }
283 293
294 void It2MeNativeMessagingHostTest::SetPolicies(
295 const base::DictionaryValue& dict) {
296 // Copy |dict| into |policy_bundle|.
297 policy::PolicyNamespace policy_namespace =
298 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string());
299 policy::PolicyBundle policy_bundle;
300 policy::PolicyMap& policy_map = policy_bundle.Get(policy_namespace);
301 policy_map.LoadFrom(&dict, policy::POLICY_LEVEL_MANDATORY,
302 policy::POLICY_SCOPE_MACHINE,
303 policy::POLICY_SOURCE_CLOUD);
304
305 // Simulate a policy file/registry/preference update.
306 policy_loader_->SetPolicies(policy_bundle);
307 policy_loader_->PostReloadOnBackgroundThread(true /* force reload asap */);
308 base::RunLoop().RunUntilIdle();
309 }
310
284 std::unique_ptr<base::DictionaryValue> 311 std::unique_ptr<base::DictionaryValue>
285 It2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() { 312 It2MeNativeMessagingHostTest::ReadMessageFromOutputPipe() {
286 while (true) { 313 while (true) {
287 uint32_t length; 314 uint32_t length;
288 int read_result = output_read_file_.ReadAtCurrentPos( 315 int read_result = output_read_file_.ReadAtCurrentPos(
289 reinterpret_cast<char*>(&length), sizeof(length)); 316 reinterpret_cast<char*>(&length), sizeof(length));
290 if (read_result != sizeof(length)) { 317 if (read_result != sizeof(length)) {
291 // The output pipe has been closed, return an empty message. 318 // The output pipe has been closed, return an empty message.
292 return nullptr; 319 return nullptr;
293 } 320 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 disconnected_received = true; 461 disconnected_received = true;
435 } else { 462 } else {
436 ADD_FAILURE() << "Unexpected host state: " << state; 463 ADD_FAILURE() << "Unexpected host state: " << state;
437 } 464 }
438 } else { 465 } else {
439 ADD_FAILURE() << "Unexpected message type: " << type; 466 ADD_FAILURE() << "Unexpected message type: " << type;
440 } 467 }
441 } 468 }
442 } 469 }
443 470
471 void It2MeNativeMessagingHostTest::VerifyPolicyErrorResponse() {
472 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe();
473 ASSERT_TRUE(response);
474 std::string type;
475 ASSERT_TRUE(response->GetString("type", &type));
476 ASSERT_EQ("policyError", type);
477 }
478
444 void It2MeNativeMessagingHostTest::TestBadRequest(const base::Value& message, 479 void It2MeNativeMessagingHostTest::TestBadRequest(const base::Value& message,
445 bool expect_error_response) { 480 bool expect_error_response) {
446 base::DictionaryValue good_message; 481 base::DictionaryValue good_message;
447 good_message.SetString("type", "hello"); 482 good_message.SetString("type", "hello");
448 good_message.SetInteger("id", 1); 483 good_message.SetInteger("id", 1);
449 484
450 WriteMessageToInputPipe(good_message); 485 WriteMessageToInputPipe(good_message);
451 WriteMessageToInputPipe(message); 486 WriteMessageToInputPipe(message);
452 WriteMessageToInputPipe(good_message); 487 WriteMessageToInputPipe(good_message);
453 488
(...skipping 15 matching lines...) Expand all
469 504
470 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); 505 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_));
471 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); 506 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file));
472 507
473 pipe_.reset(new NativeMessagingPipe()); 508 pipe_.reset(new NativeMessagingPipe());
474 509
475 std::unique_ptr<extensions::NativeMessagingChannel> channel( 510 std::unique_ptr<extensions::NativeMessagingChannel> channel(
476 new PipeMessagingChannel(std::move(input_read_file), 511 new PipeMessagingChannel(std::move(input_read_file),
477 std::move(output_write_file))); 512 std::move(output_write_file)));
478 513
479 // Creating a native messaging host with a mock It2MeHostFactory. 514 // Creating a native messaging host with a mock It2MeHostFactory and policy
515 // loader.
480 std::unique_ptr<ChromotingHostContext> context = 516 std::unique_ptr<ChromotingHostContext> context =
481 ChromotingHostContext::Create(host_task_runner_); 517 ChromotingHostContext::Create(host_task_runner_);
518 auto policy_loader = base::MakeUnique<policy::FakeAsyncPolicyLoader>(
519 base::ThreadTaskRunnerHandle::Get());
520 policy_loader_ = policy_loader.get();
482 std::unique_ptr<PolicyWatcher> policy_watcher = 521 std::unique_ptr<PolicyWatcher> policy_watcher =
483 PolicyWatcher::CreateFromPolicyLoaderForTesting( 522 PolicyWatcher::CreateFromPolicyLoaderForTesting(std::move(policy_loader));
484 base::MakeUnique<policy::FakeAsyncPolicyLoader>(
485 base::ThreadTaskRunnerHandle::Get()));
486 std::unique_ptr<extensions::NativeMessageHost> it2me_host( 523 std::unique_ptr<extensions::NativeMessageHost> it2me_host(
487 new It2MeNativeMessagingHost( 524 new It2MeNativeMessagingHost(
488 /*needs_elevation=*/false, std::move(policy_watcher), 525 /*needs_elevation=*/false, std::move(policy_watcher),
489 std::move(context), base::WrapUnique(new MockIt2MeHostFactory()))); 526 std::move(context), base::WrapUnique(new MockIt2MeHostFactory())));
490 it2me_host->Start(pipe_.get()); 527 it2me_host->Start(pipe_.get());
491 528
492 pipe_->Start(std::move(it2me_host), std::move(channel)); 529 pipe_->Start(std::move(it2me_host), std::move(channel));
493 530
494 // Notify the test that the host has finished starting up. 531 // Notify the test that the host has finished starting up.
495 test_message_loop_->task_runner()->PostTask( 532 test_message_loop_->task_runner()->PostTask(
496 FROM_HERE, test_run_loop_->QuitClosure()); 533 FROM_HERE, test_run_loop_->QuitClosure());
497 } 534 }
498 535
499 void It2MeNativeMessagingHostTest::ExitTest() { 536 void It2MeNativeMessagingHostTest::ExitTest() {
500 if (!test_message_loop_->task_runner()->RunsTasksOnCurrentThread()) { 537 if (!test_message_loop_->task_runner()->RunsTasksOnCurrentThread()) {
501 test_message_loop_->task_runner()->PostTask( 538 test_message_loop_->task_runner()->PostTask(
502 FROM_HERE, 539 FROM_HERE,
503 base::Bind(&It2MeNativeMessagingHostTest::ExitTest, 540 base::Bind(&It2MeNativeMessagingHostTest::ExitTest,
504 base::Unretained(this))); 541 base::Unretained(this)));
505 return; 542 return;
506 } 543 }
507 test_run_loop_->Quit(); 544 test_run_loop_->Quit();
508 } 545 }
509 546
510 void It2MeNativeMessagingHostTest::TestConnect() { 547 void It2MeNativeMessagingHostTest::SendConnectMessage(int id) {
511 base::DictionaryValue connect_message; 548 base::DictionaryValue connect_message;
512 int next_id = 0; 549 connect_message.SetInteger("id", id);
513
514 // Send the "connect" request.
515 connect_message.SetInteger("id", ++next_id);
516 connect_message.SetString("type", "connect"); 550 connect_message.SetString("type", "connect");
517 connect_message.SetString("xmppServerAddress", "talk.google.com:5222"); 551 connect_message.SetString("xmppServerAddress", "talk.google.com:5222");
518 connect_message.SetBoolean("xmppServerUseTls", true); 552 connect_message.SetBoolean("xmppServerUseTls", true);
519 connect_message.SetString("directoryBotJid", "remoting@bot.talk.google.com"); 553 connect_message.SetString("directoryBotJid", "remoting@bot.talk.google.com");
520 connect_message.SetString("userName", "chromo.pyauto@gmail.com"); 554 connect_message.SetString("userName", "chromo.pyauto@gmail.com");
521 connect_message.SetString("authServiceWithToken", "oauth2:sometoken"); 555 connect_message.SetString("authServiceWithToken", "oauth2:sometoken");
522 WriteMessageToInputPipe(connect_message); 556 WriteMessageToInputPipe(connect_message);
557 }
523 558
524 VerifyConnectResponses(next_id); 559 void It2MeNativeMessagingHostTest::SendDisonnectMessage(int id) {
525
526 base::DictionaryValue disconnect_message; 560 base::DictionaryValue disconnect_message;
527 disconnect_message.SetInteger("id", ++next_id); 561 disconnect_message.SetInteger("id", id);
528 disconnect_message.SetString("type", "disconnect"); 562 disconnect_message.SetString("type", "disconnect");
529 WriteMessageToInputPipe(disconnect_message); 563 WriteMessageToInputPipe(disconnect_message);
564 }
530 565
566 void It2MeNativeMessagingHostTest::TestConnect() {
567 int next_id = 1;
568 SendConnectMessage(next_id);
569 VerifyConnectResponses(next_id);
570 ++next_id;
571 SendDisonnectMessage(next_id);
531 VerifyDisconnectResponses(next_id); 572 VerifyDisconnectResponses(next_id);
532 } 573 }
533 574
534 // Test hello request. 575 // Test hello request.
535 TEST_F(It2MeNativeMessagingHostTest, Hello) { 576 TEST_F(It2MeNativeMessagingHostTest, Hello) {
536 int next_id = 0; 577 int next_id = 0;
537 base::DictionaryValue message; 578 base::DictionaryValue message;
538 message.SetInteger("id", ++next_id); 579 message.SetInteger("id", ++next_id);
539 message.SetString("type", "hello"); 580 message.SetString("type", "hello");
540 WriteMessageToInputPipe(message); 581 WriteMessageToInputPipe(message);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 TestBadRequest(message, true); 624 TestBadRequest(message, true);
584 } 625 }
585 626
586 // Verify rejection if type is unrecognized. 627 // Verify rejection if type is unrecognized.
587 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { 628 TEST_F(It2MeNativeMessagingHostTest, InvalidType) {
588 base::DictionaryValue message; 629 base::DictionaryValue message;
589 message.SetString("type", "xxx"); 630 message.SetString("type", "xxx");
590 TestBadRequest(message, true); 631 TestBadRequest(message, true);
591 } 632 }
592 633
634 // Verify rejection if type is unrecognized.
635 TEST_F(It2MeNativeMessagingHostTest, BadPoliciesBeforeConnect) {
636 base::DictionaryValue bad_policy;
637 bad_policy.SetInteger(policy::key::kRemoteAccessHostFirewallTraversal, 1);
638 SetPolicies(bad_policy);
639 SendConnectMessage(1);
640 VerifyPolicyErrorResponse();
641 }
642
643 // Verify rejection if type is unrecognized.
644 TEST_F(It2MeNativeMessagingHostTest, BadPoliciesAfterConnect) {
645 base::DictionaryValue bad_policy;
646 bad_policy.SetInteger(policy::key::kRemoteAccessHostFirewallTraversal, 1);
647 SendConnectMessage(1);
648 VerifyConnectResponses(1);
649 SetPolicies(bad_policy);
650 VerifyPolicyErrorResponse();
651 }
652
593 } // namespace remoting 653 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698