| Index: remoting/host/it2me/it2me_host_unittest.cc
|
| diff --git a/remoting/host/it2me/it2me_host_unittest.cc b/remoting/host/it2me/it2me_host_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..48763927a330ef1dd008e3a1427abbdc87fd09e7
|
| --- /dev/null
|
| +++ b/remoting/host/it2me/it2me_host_unittest.cc
|
| @@ -0,0 +1,176 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "remoting/host/it2me/it2me_host.h"
|
| +
|
| +#include <memory>
|
| +#include <string>
|
| +#include <utility>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| +#include "base/callback.h"
|
| +#include "base/location.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/run_loop.h"
|
| +#include "base/threading/thread_task_runner_handle.h"
|
| +#include "components/policy/policy_constants.h"
|
| +#include "remoting/base/auto_thread_task_runner.h"
|
| +#include "remoting/host/chromoting_host_context.h"
|
| +#include "remoting/host/policy_watcher.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace remoting {
|
| +
|
| +namespace {
|
| +
|
| +typedef protocol::ValidatingAuthenticator::Result ValidationResult;
|
| +
|
| +const char kTestClientJid[] = "ficticious_user@gmail.com/jid_resource";
|
| +const char kTestClientUsernameNoJid[] = "completely_ficticious_user@gmail.com";
|
| +const char kTestClientJidWithSlash[] = "fake/user@gmail.com/jid_resource";
|
| +const char kMatchingDomain[] = "gmail.com";
|
| +const char kMismatchedDomain1[] = "similar_to_gmail.com";
|
| +const char kMismatchedDomain2[] = "gmail_at_the_beginning.com";
|
| +const char kMismatchedDomain3[] = "not_even_close.com";
|
| +
|
| +} // namespace
|
| +
|
| +class FakeIt2MeHostFactory : public It2MeHostFactory {
|
| + public:
|
| + FakeIt2MeHostFactory() : It2MeHostFactory() {}
|
| + scoped_refptr<It2MeHost> CreateIt2MeHost(
|
| + std::unique_ptr<ChromotingHostContext> context,
|
| + base::WeakPtr<It2MeHost::Observer> observer,
|
| + const XmppSignalStrategy::XmppServerConfig& xmpp_server_config,
|
| + const std::string& directory_bot_jid) override {
|
| + return new It2MeHost(std::move(context),
|
| + /*policy_watcher=*/nullptr,
|
| + /*confirmation_dialog_factory=*/nullptr, observer,
|
| + xmpp_server_config, directory_bot_jid);
|
| + }
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(FakeIt2MeHostFactory);
|
| +};
|
| +
|
| +class It2MeHostTest : public testing::Test {
|
| + public:
|
| + It2MeHostTest() {}
|
| + ~It2MeHostTest() override {}
|
| +
|
| + // testing::Test interface.
|
| + void SetUp() override;
|
| +
|
| + void OnValidationComplete(const base::Closure& resume_callback,
|
| + ValidationResult validation_result);
|
| +
|
| + protected:
|
| + void SetClientDomainPolicy(const std::string& policy_value);
|
| +
|
| + void RunValidationCallback(const std::string& remote_jid);
|
| +
|
| + ValidationResult validation_result_ = ValidationResult::SUCCESS;
|
| +
|
| + private:
|
| + std::unique_ptr<base::MessageLoop> message_loop_;
|
| +
|
| + scoped_refptr<It2MeHost> it2me_host_;
|
| +
|
| + std::string directory_bot_jid_;
|
| + XmppSignalStrategy::XmppServerConfig xmpp_server_config_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(It2MeHostTest);
|
| +};
|
| +
|
| +void It2MeHostTest::SetUp() {
|
| + message_loop_.reset(new base::MessageLoop());
|
| +
|
| + FakeIt2MeHostFactory factory;
|
| + it2me_host_ = factory.CreateIt2MeHost(
|
| + ChromotingHostContext::Create(new AutoThreadTaskRunner(
|
| + base::ThreadTaskRunnerHandle::Get(), base::Bind(&base::DoNothing))),
|
| + /*observer=*/nullptr, xmpp_server_config_, directory_bot_jid_);
|
| +}
|
| +
|
| +void It2MeHostTest::OnValidationComplete(const base::Closure& resume_callback,
|
| + ValidationResult validation_result) {
|
| + validation_result_ = validation_result;
|
| + resume_callback.Run();
|
| +}
|
| +
|
| +void It2MeHostTest::SetClientDomainPolicy(const std::string& policy_value) {
|
| + std::unique_ptr<base::DictionaryValue> policies(new base::DictionaryValue());
|
| + policies->SetString(policy::key::kRemoteAccessHostClientDomain, policy_value);
|
| +
|
| + base::RunLoop run_loop;
|
| + it2me_host_->SetPolicyForTesting(std::move(policies), run_loop.QuitClosure());
|
| + run_loop.Run();
|
| +}
|
| +
|
| +void It2MeHostTest::RunValidationCallback(const std::string& remote_jid) {
|
| + base::RunLoop run_loop;
|
| +
|
| + it2me_host_->GetValidationCallbackForTesting().Run(
|
| + remote_jid, base::Bind(&It2MeHostTest::OnValidationComplete,
|
| + base::Unretained(this), run_loop.QuitClosure()));
|
| +
|
| + run_loop.Run();
|
| +}
|
| +
|
| +TEST_F(It2MeHostTest, ConnectionValidation_NoClientDomainPolicy_ValidJid) {
|
| + RunValidationCallback(kTestClientJid);
|
| + ASSERT_EQ(ValidationResult::SUCCESS, validation_result_);
|
| +}
|
| +
|
| +TEST_F(It2MeHostTest, ConnectionValidation_NoClientDomainPolicy_InvalidJid) {
|
| + RunValidationCallback(kTestClientUsernameNoJid);
|
| + ASSERT_EQ(ValidationResult::SUCCESS, validation_result_);
|
| +}
|
| +
|
| +TEST_F(It2MeHostTest,
|
| + ConnectionValidation_NoClientDomainPolicy_InvalidUsername) {
|
| + RunValidationCallback(kTestClientJidWithSlash);
|
| + ASSERT_EQ(ValidationResult::SUCCESS, validation_result_);
|
| +}
|
| +
|
| +TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_MatchingDomain) {
|
| + SetClientDomainPolicy(kMatchingDomain);
|
| + RunValidationCallback(kTestClientJid);
|
| + ASSERT_EQ(ValidationResult::SUCCESS, validation_result_);
|
| +}
|
| +
|
| +TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_InvalidUserName) {
|
| + SetClientDomainPolicy(kMatchingDomain);
|
| + RunValidationCallback(kTestClientJidWithSlash);
|
| + ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_);
|
| +}
|
| +
|
| +TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_NoJid) {
|
| + SetClientDomainPolicy(kMatchingDomain);
|
| + RunValidationCallback(kTestClientUsernameNoJid);
|
| + ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_);
|
| +}
|
| +
|
| +TEST_F(It2MeHostTest, ConnectionValidation_WrongClientDomain_NoMatch) {
|
| + SetClientDomainPolicy(kMismatchedDomain3);
|
| + RunValidationCallback(kTestClientJid);
|
| + ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_);
|
| +}
|
| +
|
| +TEST_F(It2MeHostTest, ConnectionValidation_WrongClientDomain_MatchStart) {
|
| + SetClientDomainPolicy(kMismatchedDomain2);
|
| + RunValidationCallback(kTestClientJid);
|
| + ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_);
|
| +}
|
| +
|
| +TEST_F(It2MeHostTest, ConnectionValidation_WrongClientDomain_MatchEnd) {
|
| + SetClientDomainPolicy(kMismatchedDomain1);
|
| + RunValidationCallback(kTestClientJid);
|
| + ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_);
|
| +}
|
| +
|
| +} // namespace remoting
|
|
|