Chromium Code Reviews| 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> |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 return true; | 118 return true; |
| 119 } | 119 } |
| 120 | 120 |
| 121 void FakePolicyService::RefreshPolicies(const base::Closure& callback) { | 121 void FakePolicyService::RefreshPolicies(const base::Closure& callback) { |
| 122 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 122 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 123 } | 123 } |
| 124 | 124 |
| 125 class MockIt2MeHost : public It2MeHost { | 125 class MockIt2MeHost : public It2MeHost { |
| 126 public: | 126 public: |
| 127 MockIt2MeHost(std::unique_ptr<ChromotingHostContext> context, | 127 MockIt2MeHost(std::unique_ptr<ChromotingHostContext> context, |
| 128 std::unique_ptr<PolicyWatcher> policy_watcher, | |
| 129 base::WeakPtr<It2MeHost::Observer> observer, | 128 base::WeakPtr<It2MeHost::Observer> observer, |
| 130 std::unique_ptr<SignalStrategy> signal_strategy, | 129 std::unique_ptr<SignalStrategy> signal_strategy, |
| 131 const std::string& username, | 130 const std::string& username, |
| 132 const std::string& directory_bot_jid) | 131 const std::string& directory_bot_jid) |
| 133 : It2MeHost(std::move(context), | 132 : It2MeHost(std::move(context), |
| 134 std::move(policy_watcher), | |
| 135 /*confirmation_dialog_factory=*/nullptr, | 133 /*confirmation_dialog_factory=*/nullptr, |
| 136 observer, | 134 observer, |
| 137 std::move(signal_strategy), | 135 std::move(signal_strategy), |
| 138 username, | 136 username, |
| 139 directory_bot_jid) {} | 137 directory_bot_jid) {} |
| 140 | 138 |
| 141 // It2MeHost overrides | 139 // It2MeHost overrides |
| 142 void Connect() override; | 140 void Connect() override; |
| 143 void Disconnect() override; | 141 void Disconnect() override; |
| 144 void RequestNatPolicy() override; | 142 void RequestNatPolicy() override; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 } | 199 } |
| 202 } | 200 } |
| 203 | 201 |
| 204 class MockIt2MeHostFactory : public It2MeHostFactory { | 202 class MockIt2MeHostFactory : public It2MeHostFactory { |
| 205 public: | 203 public: |
| 206 MockIt2MeHostFactory(); | 204 MockIt2MeHostFactory(); |
| 207 ~MockIt2MeHostFactory() override; | 205 ~MockIt2MeHostFactory() override; |
| 208 | 206 |
| 209 scoped_refptr<It2MeHost> CreateIt2MeHost( | 207 scoped_refptr<It2MeHost> CreateIt2MeHost( |
| 210 std::unique_ptr<ChromotingHostContext> context, | 208 std::unique_ptr<ChromotingHostContext> context, |
| 211 policy::PolicyService* policy_service, | |
| 212 base::WeakPtr<It2MeHost::Observer> observer, | 209 base::WeakPtr<It2MeHost::Observer> observer, |
| 213 std::unique_ptr<SignalStrategy> signal_strategy, | 210 std::unique_ptr<SignalStrategy> signal_strategy, |
| 214 const std::string& username, | 211 const std::string& username, |
| 215 const std::string& directory_bot_jid) override; | 212 const std::string& directory_bot_jid) override; |
| 216 | 213 |
| 217 private: | 214 private: |
| 218 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHostFactory); | 215 DISALLOW_COPY_AND_ASSIGN(MockIt2MeHostFactory); |
| 219 }; | 216 }; |
| 220 | 217 |
| 221 MockIt2MeHostFactory::MockIt2MeHostFactory() : It2MeHostFactory() {} | 218 MockIt2MeHostFactory::MockIt2MeHostFactory() : It2MeHostFactory() {} |
| 222 | 219 |
| 223 MockIt2MeHostFactory::~MockIt2MeHostFactory() {} | 220 MockIt2MeHostFactory::~MockIt2MeHostFactory() {} |
| 224 | 221 |
| 225 scoped_refptr<It2MeHost> MockIt2MeHostFactory::CreateIt2MeHost( | 222 scoped_refptr<It2MeHost> MockIt2MeHostFactory::CreateIt2MeHost( |
| 226 std::unique_ptr<ChromotingHostContext> context, | 223 std::unique_ptr<ChromotingHostContext> context, |
| 227 policy::PolicyService* policy_service, | |
| 228 base::WeakPtr<It2MeHost::Observer> observer, | 224 base::WeakPtr<It2MeHost::Observer> observer, |
| 229 std::unique_ptr<SignalStrategy> signal_strategy, | 225 std::unique_ptr<SignalStrategy> signal_strategy, |
| 230 const std::string& username, | 226 const std::string& username, |
| 231 const std::string& directory_bot_jid) { | 227 const std::string& directory_bot_jid) { |
| 232 return new MockIt2MeHost(std::move(context), | 228 return new MockIt2MeHost(std::move(context), observer, |
| 233 /*policy_watcher=*/nullptr, observer, | |
| 234 std::move(signal_strategy), username, | 229 std::move(signal_strategy), username, |
| 235 directory_bot_jid); | 230 directory_bot_jid); |
| 236 } | 231 } |
| 237 | 232 |
| 238 } // namespace | 233 } // namespace |
| 239 | 234 |
| 240 class It2MeNativeMessagingHostTest : public testing::Test { | 235 class It2MeNativeMessagingHostTest : public testing::Test { |
| 241 public: | 236 public: |
| 242 It2MeNativeMessagingHostTest() {} | 237 It2MeNativeMessagingHostTest() {} |
| 243 ~It2MeNativeMessagingHostTest() override {} | 238 ~It2MeNativeMessagingHostTest() override {} |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 VerifyCommonProperties(std::move(response), "helloResponse", request_id); | 387 VerifyCommonProperties(std::move(response), "helloResponse", request_id); |
| 393 } | 388 } |
| 394 | 389 |
| 395 void It2MeNativeMessagingHostTest::VerifyErrorResponse() { | 390 void It2MeNativeMessagingHostTest::VerifyErrorResponse() { |
| 396 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); | 391 std::unique_ptr<base::DictionaryValue> response = ReadMessageFromOutputPipe(); |
| 397 VerifyStringProperty(std::move(response), "type", "error"); | 392 VerifyStringProperty(std::move(response), "type", "error"); |
| 398 } | 393 } |
| 399 | 394 |
| 400 void It2MeNativeMessagingHostTest::VerifyConnectResponses(int request_id) { | 395 void It2MeNativeMessagingHostTest::VerifyConnectResponses(int request_id) { |
| 401 bool connect_response_received = false; | 396 bool connect_response_received = false; |
| 397 bool nat_policy_received = false; | |
| 402 bool starting_received = false; | 398 bool starting_received = false; |
| 403 bool requestedAccessCode_received = false; | 399 bool requestedAccessCode_received = false; |
| 404 bool receivedAccessCode_received = false; | 400 bool receivedAccessCode_received = false; |
| 405 bool connecting_received = false; | 401 bool connecting_received = false; |
| 406 bool connected_received = false; | 402 bool connected_received = false; |
| 407 | 403 |
| 408 // We expect a total of 6 messages: 1 connectResponse and 5 hostStateChanged. | 404 // We expect a total of 7 messages: 1 connectResponse, 1 natPolicyChanged, |
| 409 for (int i = 0; i < 6; ++i) { | 405 // and 5 hostStateChanged. |
| 406 for (int i = 0; i < 7; ++i) { | |
| 410 std::unique_ptr<base::DictionaryValue> response = | 407 std::unique_ptr<base::DictionaryValue> response = |
| 411 ReadMessageFromOutputPipe(); | 408 ReadMessageFromOutputPipe(); |
| 412 ASSERT_TRUE(response); | 409 ASSERT_TRUE(response); |
| 413 | 410 |
| 414 std::string type; | 411 std::string type; |
| 415 ASSERT_TRUE(response->GetString("type", &type)); | 412 ASSERT_TRUE(response->GetString("type", &type)); |
| 416 | 413 |
| 417 if (type == "connectResponse") { | 414 if (type == "connectResponse") { |
| 418 EXPECT_FALSE(connect_response_received); | 415 EXPECT_FALSE(connect_response_received); |
| 419 connect_response_received = true; | 416 connect_response_received = true; |
| 420 VerifyId(std::move(response), request_id); | 417 VerifyId(std::move(response), request_id); |
| 418 } else if (type == "natPolicyChanged") { | |
| 419 EXPECT_FALSE(nat_policy_received); | |
| 420 nat_policy_received = true; | |
| 421 } else if (type == "hostStateChanged") { | 421 } else if (type == "hostStateChanged") { |
| 422 std::string state; | 422 std::string state; |
| 423 ASSERT_TRUE(response->GetString("state", &state)); | 423 ASSERT_TRUE(response->GetString("state", &state)); |
| 424 | 424 |
| 425 std::string value; | 425 std::string value; |
| 426 if (state == It2MeNativeMessagingHost::HostStateToString(kStarting)) { | 426 if (state == It2MeNativeMessagingHost::HostStateToString(kStarting)) { |
| 427 EXPECT_FALSE(starting_received); | 427 EXPECT_FALSE(starting_received); |
| 428 starting_received = true; | 428 starting_received = true; |
| 429 } else if (state == It2MeNativeMessagingHost::HostStateToString( | 429 } else if (state == It2MeNativeMessagingHost::HostStateToString( |
| 430 kRequestedAccessCode)) { | 430 kRequestedAccessCode)) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); | 523 ASSERT_TRUE(MakePipe(&input_read_file, &input_write_file_)); |
| 524 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); | 524 ASSERT_TRUE(MakePipe(&output_read_file_, &output_write_file)); |
| 525 | 525 |
| 526 pipe_.reset(new NativeMessagingPipe()); | 526 pipe_.reset(new NativeMessagingPipe()); |
| 527 | 527 |
| 528 std::unique_ptr<extensions::NativeMessagingChannel> channel( | 528 std::unique_ptr<extensions::NativeMessagingChannel> channel( |
| 529 new PipeMessagingChannel(std::move(input_read_file), | 529 new PipeMessagingChannel(std::move(input_read_file), |
| 530 std::move(output_write_file))); | 530 std::move(output_write_file))); |
| 531 | 531 |
| 532 // Creating a native messaging host with a mock It2MeHostFactory. | 532 // Creating a native messaging host with a mock It2MeHostFactory. |
| 533 std::unique_ptr<ChromotingHostContext> context = | |
| 534 ChromotingHostContext::Create(host_task_runner_); | |
| 535 std::unique_ptr<PolicyWatcher> policy_watcher = PolicyWatcher::Create( | |
| 536 fake_policy_service_.get(), context->file_task_runner()); | |
|
rkjnsn
2017/05/04 22:52:40
This will still create a PolicyWatcher that reads
Jamie
2017/05/04 23:08:40
Done (it let me delete FakePolicyService entirely,
| |
| 533 std::unique_ptr<extensions::NativeMessageHost> it2me_host( | 537 std::unique_ptr<extensions::NativeMessageHost> it2me_host( |
| 534 new It2MeNativeMessagingHost( | 538 new It2MeNativeMessagingHost( |
| 535 /*needs_elevation=*/false, fake_policy_service_.get(), | 539 /*needs_elevation=*/false, std::move(policy_watcher), |
| 536 ChromotingHostContext::Create(host_task_runner_), | 540 std::move(context), base::WrapUnique(new MockIt2MeHostFactory()))); |
| 537 base::WrapUnique(new MockIt2MeHostFactory()))); | |
| 538 it2me_host->Start(pipe_.get()); | 541 it2me_host->Start(pipe_.get()); |
| 539 | 542 |
| 540 pipe_->Start(std::move(it2me_host), std::move(channel)); | 543 pipe_->Start(std::move(it2me_host), std::move(channel)); |
| 541 | 544 |
| 542 // Notify the test that the host has finished starting up. | 545 // Notify the test that the host has finished starting up. |
| 543 test_message_loop_->task_runner()->PostTask( | 546 test_message_loop_->task_runner()->PostTask( |
| 544 FROM_HERE, test_run_loop_->QuitClosure()); | 547 FROM_HERE, test_run_loop_->QuitClosure()); |
| 545 } | 548 } |
| 546 | 549 |
| 547 void It2MeNativeMessagingHostTest::ExitTest() { | 550 void It2MeNativeMessagingHostTest::ExitTest() { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 602 EXPECT_TRUE(response); | 605 EXPECT_TRUE(response); |
| 603 std::string value; | 606 std::string value; |
| 604 EXPECT_FALSE(response->GetString("id", &value)); | 607 EXPECT_FALSE(response->GetString("id", &value)); |
| 605 | 608 |
| 606 response = ReadMessageFromOutputPipe(); | 609 response = ReadMessageFromOutputPipe(); |
| 607 EXPECT_TRUE(response); | 610 EXPECT_TRUE(response); |
| 608 EXPECT_TRUE(response->GetString("id", &value)); | 611 EXPECT_TRUE(response->GetString("id", &value)); |
| 609 EXPECT_EQ("42", value); | 612 EXPECT_EQ("42", value); |
| 610 } | 613 } |
| 611 | 614 |
| 612 TEST_F(It2MeNativeMessagingHostTest, Connect) { | 615 TEST_F(It2MeNativeMessagingHostTest, ConnectMultiple) { |
| 613 // A new It2MeHost instance is created for every it2me session. The native | 616 // 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 | 617 // messaging host, on the other hand, is long lived. This test verifies |
| 615 // multiple It2Me host startup and shutdowns. | 618 // multiple It2Me host startup and shutdowns. |
| 616 for (int i = 0; i < 3; ++i) { | 619 for (int i = 0; i < 3; ++i) { |
| 617 TestConnect(); | 620 TestConnect(); |
| 618 } | 621 } |
| 619 } | 622 } |
| 620 | 623 |
| 621 // Verify non-Dictionary requests are rejected. | 624 // Verify non-Dictionary requests are rejected. |
| 622 TEST_F(It2MeNativeMessagingHostTest, WrongFormat) { | 625 TEST_F(It2MeNativeMessagingHostTest, WrongFormat) { |
| 623 base::ListValue message; | 626 base::ListValue message; |
| 624 // No "error" response will be sent for non-Dictionary messages. | 627 // No "error" response will be sent for non-Dictionary messages. |
| 625 TestBadRequest(message, false); | 628 TestBadRequest(message, false); |
| 626 } | 629 } |
| 627 | 630 |
| 628 // Verify requests with no type are rejected. | 631 // Verify requests with no type are rejected. |
| 629 TEST_F(It2MeNativeMessagingHostTest, MissingType) { | 632 TEST_F(It2MeNativeMessagingHostTest, MissingType) { |
| 630 base::DictionaryValue message; | 633 base::DictionaryValue message; |
| 631 TestBadRequest(message, true); | 634 TestBadRequest(message, true); |
| 632 } | 635 } |
| 633 | 636 |
| 634 // Verify rejection if type is unrecognized. | 637 // Verify rejection if type is unrecognized. |
| 635 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { | 638 TEST_F(It2MeNativeMessagingHostTest, InvalidType) { |
| 636 base::DictionaryValue message; | 639 base::DictionaryValue message; |
| 637 message.SetString("type", "xxx"); | 640 message.SetString("type", "xxx"); |
| 638 TestBadRequest(message, true); | 641 TestBadRequest(message, true); |
| 639 } | 642 } |
| 640 | 643 |
| 641 } // namespace remoting | 644 } // namespace remoting |
| OLD | NEW |