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