OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/it2me/it2me_host.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/location.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "components/policy/policy_constants.h" |
| 20 #include "remoting/base/auto_thread_task_runner.h" |
| 21 #include "remoting/host/chromoting_host_context.h" |
| 22 #include "remoting/host/policy_watcher.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 namespace remoting { |
| 26 |
| 27 namespace { |
| 28 |
| 29 typedef protocol::ValidatingAuthenticator::Result ValidationResult; |
| 30 |
| 31 const char kTestClientJid[] = "ficticious_user@gmail.com/jid_resource"; |
| 32 const char kTestClientUsernameNoJid[] = "completely_ficticious_user@gmail.com"; |
| 33 const char kTestClientJidWithSlash[] = "fake/user@gmail.com/jid_resource"; |
| 34 const char kMatchingDomain[] = "gmail.com"; |
| 35 const char kMismatchedDomain1[] = "similar_to_gmail.com"; |
| 36 const char kMismatchedDomain2[] = "gmail_at_the_beginning.com"; |
| 37 const char kMismatchedDomain3[] = "not_even_close.com"; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 class It2MeHostTest : public testing::Test { |
| 42 public: |
| 43 It2MeHostTest() {} |
| 44 ~It2MeHostTest() override {} |
| 45 |
| 46 // testing::Test interface. |
| 47 void SetUp() override; |
| 48 void TearDown() override; |
| 49 |
| 50 void OnValidationComplete(const base::Closure& resume_callback, |
| 51 ValidationResult validation_result); |
| 52 |
| 53 protected: |
| 54 void SetClientDomainPolicy(const std::string& policy_value); |
| 55 |
| 56 void RunValidationCallback(const std::string& remote_jid); |
| 57 |
| 58 ValidationResult validation_result_ = ValidationResult::SUCCESS; |
| 59 |
| 60 private: |
| 61 std::unique_ptr<base::MessageLoop> message_loop_; |
| 62 std::unique_ptr<base::RunLoop> run_loop_; |
| 63 |
| 64 scoped_refptr<It2MeHost> it2me_host_; |
| 65 |
| 66 std::string directory_bot_jid_; |
| 67 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(It2MeHostTest); |
| 70 }; |
| 71 |
| 72 void It2MeHostTest::SetUp() { |
| 73 message_loop_.reset(new base::MessageLoop()); |
| 74 run_loop_.reset(new base::RunLoop()); |
| 75 |
| 76 scoped_refptr<AutoThreadTaskRunner> auto_thread_task_runner = |
| 77 new AutoThreadTaskRunner(base::ThreadTaskRunnerHandle::Get(), |
| 78 run_loop_->QuitClosure()); |
| 79 it2me_host_ = new It2MeHost( |
| 80 ChromotingHostContext::Create(auto_thread_task_runner), |
| 81 /*policy_watcher=*/nullptr, |
| 82 /*confirmation_dialog_factory=*/nullptr, |
| 83 /*observer=*/nullptr, xmpp_server_config_, directory_bot_jid_); |
| 84 } |
| 85 |
| 86 void It2MeHostTest::TearDown() { |
| 87 it2me_host_ = nullptr; |
| 88 run_loop_->Run(); |
| 89 } |
| 90 |
| 91 void It2MeHostTest::OnValidationComplete(const base::Closure& resume_callback, |
| 92 ValidationResult validation_result) { |
| 93 validation_result_ = validation_result; |
| 94 resume_callback.Run(); |
| 95 } |
| 96 |
| 97 void It2MeHostTest::SetClientDomainPolicy(const std::string& policy_value) { |
| 98 std::unique_ptr<base::DictionaryValue> policies(new base::DictionaryValue()); |
| 99 policies->SetString(policy::key::kRemoteAccessHostClientDomain, policy_value); |
| 100 |
| 101 base::RunLoop run_loop; |
| 102 it2me_host_->SetPolicyForTesting(std::move(policies), run_loop.QuitClosure()); |
| 103 run_loop.Run(); |
| 104 } |
| 105 |
| 106 void It2MeHostTest::RunValidationCallback(const std::string& remote_jid) { |
| 107 base::RunLoop run_loop; |
| 108 |
| 109 it2me_host_->GetValidationCallbackForTesting().Run( |
| 110 remote_jid, base::Bind(&It2MeHostTest::OnValidationComplete, |
| 111 base::Unretained(this), run_loop.QuitClosure())); |
| 112 |
| 113 run_loop.Run(); |
| 114 } |
| 115 |
| 116 TEST_F(It2MeHostTest, ConnectionValidation_NoClientDomainPolicy_ValidJid) { |
| 117 RunValidationCallback(kTestClientJid); |
| 118 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
| 119 } |
| 120 |
| 121 TEST_F(It2MeHostTest, ConnectionValidation_NoClientDomainPolicy_InvalidJid) { |
| 122 RunValidationCallback(kTestClientUsernameNoJid); |
| 123 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
| 124 } |
| 125 |
| 126 TEST_F(It2MeHostTest, |
| 127 ConnectionValidation_NoClientDomainPolicy_InvalidUsername) { |
| 128 RunValidationCallback(kTestClientJidWithSlash); |
| 129 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
| 130 } |
| 131 |
| 132 TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_MatchingDomain) { |
| 133 SetClientDomainPolicy(kMatchingDomain); |
| 134 RunValidationCallback(kTestClientJid); |
| 135 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
| 136 } |
| 137 |
| 138 TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_InvalidUserName) { |
| 139 SetClientDomainPolicy(kMatchingDomain); |
| 140 RunValidationCallback(kTestClientJidWithSlash); |
| 141 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
| 142 } |
| 143 |
| 144 TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_NoJid) { |
| 145 SetClientDomainPolicy(kMatchingDomain); |
| 146 RunValidationCallback(kTestClientUsernameNoJid); |
| 147 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
| 148 } |
| 149 |
| 150 TEST_F(It2MeHostTest, ConnectionValidation_WrongClientDomain_NoMatch) { |
| 151 SetClientDomainPolicy(kMismatchedDomain3); |
| 152 RunValidationCallback(kTestClientJid); |
| 153 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
| 154 } |
| 155 |
| 156 TEST_F(It2MeHostTest, ConnectionValidation_WrongClientDomain_MatchStart) { |
| 157 SetClientDomainPolicy(kMismatchedDomain2); |
| 158 RunValidationCallback(kTestClientJid); |
| 159 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
| 160 } |
| 161 |
| 162 TEST_F(It2MeHostTest, ConnectionValidation_WrongClientDomain_MatchEnd) { |
| 163 SetClientDomainPolicy(kMismatchedDomain1); |
| 164 RunValidationCallback(kTestClientJid); |
| 165 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
| 166 } |
| 167 |
| 168 } // namespace remoting |
OLD | NEW |