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/mock_policy_service.h" | 26 #include "components/policy/core/common/mock_policy_service.h" |
26 #include "net/base/file_stream.h" | 27 #include "net/base/file_stream.h" |
27 #include "remoting/base/auto_thread_task_runner.h" | 28 #include "remoting/base/auto_thread_task_runner.h" |
28 #include "remoting/host/chromoting_host_context.h" | 29 #include "remoting/host/chromoting_host_context.h" |
29 #include "remoting/host/native_messaging/log_message_handler.h" | 30 #include "remoting/host/native_messaging/log_message_handler.h" |
30 #include "remoting/host/native_messaging/native_messaging_pipe.h" | 31 #include "remoting/host/native_messaging/native_messaging_pipe.h" |
31 #include "remoting/host/native_messaging/pipe_messaging_channel.h" | 32 #include "remoting/host/native_messaging/pipe_messaging_channel.h" |
32 #include "remoting/host/policy_watcher.h" | 33 #include "remoting/host/policy_watcher.h" |
33 #include "remoting/host/setup/test_util.h" | 34 #include "remoting/host/setup/test_util.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 69 |
69 std::string string_value; | 70 std::string string_value; |
70 EXPECT_TRUE(response->GetString("type", &string_value)); | 71 EXPECT_TRUE(response->GetString("type", &string_value)); |
71 EXPECT_EQ(type, string_value); | 72 EXPECT_EQ(type, string_value); |
72 | 73 |
73 int int_value; | 74 int int_value; |
74 EXPECT_TRUE(response->GetInteger("id", &int_value)); | 75 EXPECT_TRUE(response->GetInteger("id", &int_value)); |
75 EXPECT_EQ(id, int_value); | 76 EXPECT_EQ(id, int_value); |
76 } | 77 } |
77 | 78 |
78 class FakePolicyService : public policy::PolicyService { | |
79 public: | |
80 FakePolicyService(); | |
81 ~FakePolicyService() override; | |
82 | |
83 // policy::PolicyService overrides. | |
84 void AddObserver(policy::PolicyDomain domain, | |
85 policy::PolicyService::Observer* observer) override; | |
86 void RemoveObserver(policy::PolicyDomain domain, | |
87 policy::PolicyService::Observer* observer) override; | |
88 const policy::PolicyMap& GetPolicies( | |
89 const policy::PolicyNamespace& ns) const override; | |
90 bool IsInitializationComplete(policy::PolicyDomain domain) const override; | |
91 void RefreshPolicies(const base::Closure& callback) override; | |
92 | |
93 private: | |
94 policy::PolicyMap policy_map_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(FakePolicyService); | |
97 }; | |
98 | |
99 FakePolicyService::FakePolicyService() {} | |
100 | |
101 FakePolicyService::~FakePolicyService() {} | |
102 | |
103 void FakePolicyService::AddObserver(policy::PolicyDomain domain, | |
104 policy::PolicyService::Observer* observer) { | |
105 } | |
106 | |
107 void FakePolicyService::RemoveObserver( | |
108 policy::PolicyDomain domain, | |
109 policy::PolicyService::Observer* observer) {} | |
110 | |
111 const policy::PolicyMap& FakePolicyService::GetPolicies( | |
112 const policy::PolicyNamespace& ns) const { | |
113 return policy_map_; | |
114 } | |
115 | |
116 bool FakePolicyService::IsInitializationComplete( | |
117 policy::PolicyDomain domain) const { | |
118 return true; | |
119 } | |
120 | |
121 void FakePolicyService::RefreshPolicies(const base::Closure& callback) { | |
122 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | |
123 } | |
124 | |
125 class MockIt2MeHost : public It2MeHost { | 79 class MockIt2MeHost : public It2MeHost { |
126 public: | 80 public: |
127 MockIt2MeHost(std::unique_ptr<ChromotingHostContext> context, | 81 MockIt2MeHost(std::unique_ptr<ChromotingHostContext> context, |
128 std::unique_ptr<PolicyWatcher> policy_watcher, | |
129 base::WeakPtr<It2MeHost::Observer> observer, | 82 base::WeakPtr<It2MeHost::Observer> observer, |
130 std::unique_ptr<SignalStrategy> signal_strategy, | 83 std::unique_ptr<SignalStrategy> signal_strategy, |
131 const std::string& username, | 84 const std::string& username, |
132 const std::string& directory_bot_jid) | 85 const std::string& directory_bot_jid) |
133 : It2MeHost(std::move(context), | 86 : It2MeHost(std::move(context), |
134 std::move(policy_watcher), | |
135 /*confirmation_dialog_factory=*/nullptr, | 87 /*confirmation_dialog_factory=*/nullptr, |
136 observer, | 88 observer, |
137 std::move(signal_strategy), | 89 std::move(signal_strategy), |
138 username, | 90 username, |
139 directory_bot_jid) {} | 91 directory_bot_jid) {} |
140 | 92 |
141 // It2MeHost overrides | 93 // It2MeHost overrides |
142 void Connect() override; | 94 void Connect() override; |
143 void Disconnect() override; | 95 void Disconnect() override; |
144 void RequestNatPolicy() override; | 96 void RequestNatPolicy() override; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 } | 153 } |
202 } | 154 } |
203 | 155 |
204 class MockIt2MeHostFactory : public It2MeHostFactory { | 156 class MockIt2MeHostFactory : public It2MeHostFactory { |
205 public: | 157 public: |
206 MockIt2MeHostFactory(); | 158 MockIt2MeHostFactory(); |
207 ~MockIt2MeHostFactory() override; | 159 ~MockIt2MeHostFactory() override; |
208 | 160 |
209 scoped_refptr<It2MeHost> CreateIt2MeHost( | 161 scoped_refptr<It2MeHost> CreateIt2MeHost( |
210 std::unique_ptr<ChromotingHostContext> context, | 162 std::unique_ptr<ChromotingHostContext> context, |
211 policy::PolicyService* policy_service, | |
212 base::WeakPtr<It2MeHost::Observer> observer, | 163 base::WeakPtr<It2MeHost::Observer> observer, |
213 std::unique_ptr<SignalStrategy> signal_strategy, | 164 std::unique_ptr<SignalStrategy> signal_strategy, |
214 const std::string& username, | 165 const std::string& username, |
215 const std::string& directory_bot_jid) override; | 166 const std::string& directory_bot_jid) override; |
216 | 167 |
217 private: | 168 private: |
218 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHostFactory); | 169 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHostFactory); |
219 }; | 170 }; |
220 | 171 |
221 MockIt2MeHostFactory::MockIt2MeHostFactory() : It2MeHostFactory() {} | 172 MockIt2MeHostFactory::MockIt2MeHostFactory() : It2MeHostFactory() {} |
222 | 173 |
223 MockIt2MeHostFactory::~MockIt2MeHostFactory() {} | 174 MockIt2MeHostFactory::~MockIt2MeHostFactory() {} |
224 | 175 |
225 scoped_refptr<It2MeHost> MockIt2MeHostFactory::CreateIt2MeHost( | 176 scoped_refptr<It2MeHost> MockIt2MeHostFactory::CreateIt2MeHost( |
226 std::unique_ptr<ChromotingHostContext> context, | 177 std::unique_ptr<ChromotingHostContext> context, |
227 policy::PolicyService* policy_service, | |
228 base::WeakPtr<It2MeHost::Observer> observer, | 178 base::WeakPtr<It2MeHost::Observer> observer, |
229 std::unique_ptr<SignalStrategy> signal_strategy, | 179 std::unique_ptr<SignalStrategy> signal_strategy, |
230 const std::string& username, | 180 const std::string& username, |
231 const std::string& directory_bot_jid) { | 181 const std::string& directory_bot_jid) { |
232 return new MockIt2MeHost(std::move(context), | 182 return new MockIt2MeHost(std::move(context), observer, |
233 /*policy_watcher=*/nullptr, observer, | |
234 std::move(signal_strategy), username, | 183 std::move(signal_strategy), username, |
235 directory_bot_jid); | 184 directory_bot_jid); |
236 } | 185 } |
237 | 186 |
238 } // namespace | 187 } // namespace |
239 | 188 |
240 class It2MeNativeMessagingHostTest : public testing::Test { | 189 class It2MeNativeMessagingHostTest : public testing::Test { |
241 public: | 190 public: |
242 It2MeNativeMessagingHostTest() {} | 191 It2MeNativeMessagingHostTest() {} |
243 ~It2MeNativeMessagingHostTest() override {} | 192 ~It2MeNativeMessagingHostTest() override {} |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 std::unique_ptr<base::MessageLoop> test_message_loop_; | 227 std::unique_ptr<base::MessageLoop> test_message_loop_; |
279 std::unique_ptr<base::RunLoop> test_run_loop_; | 228 std::unique_ptr<base::RunLoop> test_run_loop_; |
280 | 229 |
281 std::unique_ptr<base::Thread> host_thread_; | 230 std::unique_ptr<base::Thread> host_thread_; |
282 std::unique_ptr<base::RunLoop> host_run_loop_; | 231 std::unique_ptr<base::RunLoop> host_run_loop_; |
283 | 232 |
284 // Task runner of the host thread. | 233 // Task runner of the host thread. |
285 scoped_refptr<AutoThreadTaskRunner> host_task_runner_; | 234 scoped_refptr<AutoThreadTaskRunner> host_task_runner_; |
286 std::unique_ptr<remoting::NativeMessagingPipe> pipe_; | 235 std::unique_ptr<remoting::NativeMessagingPipe> pipe_; |
287 | 236 |
288 std::unique_ptr<policy::PolicyService> fake_policy_service_; | |
289 | |
290 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest); | 237 DISALLOW_COPY_AND_ASSIGN(It2MeNativeMessagingHostTest); |
291 }; | 238 }; |
292 | 239 |
293 void It2MeNativeMessagingHostTest::SetUp() { | 240 void It2MeNativeMessagingHostTest::SetUp() { |
294 test_message_loop_.reset(new base::MessageLoop()); | 241 test_message_loop_.reset(new base::MessageLoop()); |
295 test_run_loop_.reset(new base::RunLoop()); | 242 test_run_loop_.reset(new base::RunLoop()); |
296 | 243 |
297 #if defined(OS_CHROMEOS) | |
298 // On Chrome OS, the browser owns the PolicyService so simulate that here. | |
299 fake_policy_service_.reset(new FakePolicyService()); | |
300 #endif // defined(OS_CHROMEOS) | |
301 | |
302 // Run the host on a dedicated thread. | 244 // Run the host on a dedicated thread. |
303 host_thread_.reset(new base::Thread("host_thread")); | 245 host_thread_.reset(new base::Thread("host_thread")); |
304 host_thread_->Start(); | 246 host_thread_->Start(); |
305 | 247 |
306 host_task_runner_ = new AutoThreadTaskRunner( | 248 host_task_runner_ = new AutoThreadTaskRunner( |
307 host_thread_->task_runner(), | 249 host_thread_->task_runner(), |
308 base::Bind(&It2MeNativeMessagingHostTest::ExitTest, | 250 base::Bind(&It2MeNativeMessagingHostTest::ExitTest, |
309 base::Unretained(this))); | 251 base::Unretained(this))); |
310 | 252 |
311 host_task_runner_->PostTask( | 253 host_task_runner_->PostTask( |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 VerifyCommonProperties(std::move(response), "helloResponse", request_id); | 334 VerifyCommonProperties(std::move(response), "helloResponse", request_id); |
393 } | 335 } |
394 | 336 |
395 void It2MeNativeMessagingHostTest::VerifyErrorResponse() { | 337 void It2MeNativeMessagingHostTest::VerifyErrorResponse() { |
396 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); | 338 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); |
397 VerifyStringProperty(std::move(response), "type", "error"); | 339 VerifyStringProperty(std::move(response), "type", "error"); |
398 } | 340 } |
399 | 341 |
400 void It2MeNativeMessagingHostTest::VerifyConnectResponses(int request_id) { | 342 void It2MeNativeMessagingHostTest::VerifyConnectResponses(int request_id) { |
401 bool connect_response_received = false; | 343 bool connect_response_received = false; |
| 344 bool nat_policy_received = false; |
402 bool starting_received = false; | 345 bool starting_received = false; |
403 bool requestedAccessCode_received = false; | 346 bool requestedAccessCode_received = false; |
404 bool receivedAccessCode_received = false; | 347 bool receivedAccessCode_received = false; |
405 bool connecting_received = false; | 348 bool connecting_received = false; |
406 bool connected_received = false; | 349 bool connected_received = false; |
407 | 350 |
408 // We expect a total of 6 messages: 1 connectResponse and 5 hostStateChanged. | 351 // We expect a total of 7 messages: 1 connectResponse, 1 natPolicyChanged, |
409 for (int i = 0; i < 6; ++i) { | 352 // and 5 hostStateChanged. |
| 353 for (int i = 0; i < 7; ++i) { |
410 std::unique_ptr<base::DictionaryValue> response = | 354 std::unique_ptr<base::DictionaryValue> response = |
411 ReadMessageFromOutputPipe(); | 355 ReadMessageFromOutputPipe(); |
412 ASSERT_TRUE(response); | 356 ASSERT_TRUE(response); |
413 | 357 |
414 std::string type; | 358 std::string type; |
415 ASSERT_TRUE(response->GetString("type", &type)); | 359 ASSERT_TRUE(response->GetString("type", &type)); |
416 | 360 |
417 if (type == "connectResponse") { | 361 if (type == "connectResponse") { |
418 EXPECT_FALSE(connect_response_received); | 362 EXPECT_FALSE(connect_response_received); |
419 connect_response_received = true; | 363 connect_response_received = true; |
420 VerifyId(std::move(response), request_id); | 364 VerifyId(std::move(response), request_id); |
| 365 } else if (type == "natPolicyChanged") { |
| 366 EXPECT_FALSE(nat_policy_received); |
| 367 nat_policy_received = true; |
421 } else if (type == "hostStateChanged") { | 368 } else if (type == "hostStateChanged") { |
422 std::string state; | 369 std::string state; |
423 ASSERT_TRUE(response->GetString("state", &state)); | 370 ASSERT_TRUE(response->GetString("state", &state)); |
424 | 371 |
425 std::string value; | 372 std::string value; |
426 if (state == It2MeNativeMessagingHost::HostStateToString(kStarting)) { | 373 if (state == It2MeNativeMessagingHost::HostStateToString(kStarting)) { |
427 EXPECT_FALSE(starting_received); | 374 EXPECT_FALSE(starting_received); |
428 starting_received = true; | 375 starting_received = true; |
429 } else if (state == It2MeNativeMessagingHost::HostStateToString( | 376 } else if (state == It2MeNativeMessagingHost::HostStateToString( |
430 kRequestedAccessCode)) { | 377 kRequestedAccessCode)) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); | 470 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); |
524 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); | 471 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); |
525 | 472 |
526 pipe_.reset(new NativeMessagingPipe()); | 473 pipe_.reset(new NativeMessagingPipe()); |
527 | 474 |
528 std::unique_ptr<extensions::NativeMessagingChannel> channel( | 475 std::unique_ptr<extensions::NativeMessagingChannel> channel( |
529 new PipeMessagingChannel(std::move(input_read_file), | 476 new PipeMessagingChannel(std::move(input_read_file), |
530 std::move(output_write_file))); | 477 std::move(output_write_file))); |
531 | 478 |
532 // Creating a native messaging host with a mock It2MeHostFactory. | 479 // Creating a native messaging host with a mock It2MeHostFactory. |
| 480 std::unique_ptr<ChromotingHostContext> context = |
| 481 ChromotingHostContext::Create(host_task_runner_); |
| 482 std::unique_ptr<PolicyWatcher> policy_watcher = |
| 483 PolicyWatcher::CreateFromPolicyLoaderForTesting( |
| 484 base::MakeUnique<policy::FakeAsyncPolicyLoader>( |
| 485 base::ThreadTaskRunnerHandle::Get())); |
533 std::unique_ptr<extensions::NativeMessageHost> it2me_host( | 486 std::unique_ptr<extensions::NativeMessageHost> it2me_host( |
534 new It2MeNativeMessagingHost( | 487 new It2MeNativeMessagingHost( |
535 /*needs_elevation=*/false, fake_policy_service_.get(), | 488 /*needs_elevation=*/false, std::move(policy_watcher), |
536 ChromotingHostContext::Create(host_task_runner_), | 489 std::move(context), base::WrapUnique(new MockIt2MeHostFactory()))); |
537 base::WrapUnique(new MockIt2MeHostFactory()))); | |
538 it2me_host->Start(pipe_.get()); | 490 it2me_host->Start(pipe_.get()); |
539 | 491 |
540 pipe_->Start(std::move(it2me_host), std::move(channel)); | 492 pipe_->Start(std::move(it2me_host), std::move(channel)); |
541 | 493 |
542 // Notify the test that the host has finished starting up. | 494 // Notify the test that the host has finished starting up. |
543 test_message_loop_->task_runner()->PostTask( | 495 test_message_loop_->task_runner()->PostTask( |
544 FROM_HERE, test_run_loop_->QuitClosure()); | 496 FROM_HERE, test_run_loop_->QuitClosure()); |
545 } | 497 } |
546 | 498 |
547 void It2MeNativeMessagingHostTest::ExitTest() { | 499 void It2MeNativeMessagingHostTest::ExitTest() { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 EXPECT_TRUE(response); | 554 EXPECT_TRUE(response); |
603 std::string value; | 555 std::string value; |
604 EXPECT_FALSE(response->GetString("id", &value)); | 556 EXPECT_FALSE(response->GetString("id", &value)); |
605 | 557 |
606 response = ReadMessageFromOutputPipe(); | 558 response = ReadMessageFromOutputPipe(); |
607 EXPECT_TRUE(response); | 559 EXPECT_TRUE(response); |
608 EXPECT_TRUE(response->GetString("id", &value)); | 560 EXPECT_TRUE(response->GetString("id", &value)); |
609 EXPECT_EQ("42", value); | 561 EXPECT_EQ("42", value); |
610 } | 562 } |
611 | 563 |
612 TEST_F(It2MeNativeMessagingHostTest, Connect) { | 564 TEST_F(It2MeNativeMessagingHostTest, ConnectMultiple) { |
613 // A new It2MeHost instance is created for every it2me session. The native | 565 // A new It2MeHost instance is created for every it2me session. The native |
614 // messaging host, on the other hand, is long lived. This test verifies | 566 // messaging host, on the other hand, is long lived. This test verifies |
615 // multiple It2Me host startup and shutdowns. | 567 // multiple It2Me host startup and shutdowns. |
616 for (int i = 0; i < 3; ++i) { | 568 for (int i = 0; i < 3; ++i) { |
617 TestConnect(); | 569 TestConnect(); |
618 } | 570 } |
619 } | 571 } |
620 | 572 |
621 // Verify non-Dictionary requests are rejected. | 573 // Verify non-Dictionary requests are rejected. |
622 TEST_F(It2MeNativeMessagingHostTest, WrongFormat) { | 574 TEST_F(It2MeNativeMessagingHostTest, WrongFormat) { |
623 base::ListValue message; | 575 base::ListValue message; |
624 // No "error" response will be sent for non-Dictionary messages. | 576 // No "error" response will be sent for non-Dictionary messages. |
625 TestBadRequest(message, false); | 577 TestBadRequest(message, false); |
626 } | 578 } |
627 | 579 |
628 // Verify requests with no type are rejected. | 580 // Verify requests with no type are rejected. |
629 TEST_F(It2MeNativeMessagingHostTest, MissingType) { | 581 TEST_F(It2MeNativeMessagingHostTest, MissingType) { |
630 base::DictionaryValue message; | 582 base::DictionaryValue message; |
631 TestBadRequest(message, true); | 583 TestBadRequest(message, true); |
632 } | 584 } |
633 | 585 |
634 // Verify rejection if type is unrecognized. | 586 // Verify rejection if type is unrecognized. |
635 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { | 587 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { |
636 base::DictionaryValue message; | 588 base::DictionaryValue message; |
637 message.SetString("type", "xxx"); | 589 message.SetString("type", "xxx"); |
638 TestBadRequest(message, true); | 590 TestBadRequest(message, true); |
639 } | 591 } |
640 | 592 |
641 } // namespace remoting | 593 } // namespace remoting |
OLD | NEW |