OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_host.h" | 5 #include "remoting/host/it2me/it2me_host.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" |
15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
16 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
19 #include "components/policy/policy_constants.h" | 20 #include "components/policy/policy_constants.h" |
20 #include "remoting/base/auto_thread_task_runner.h" | 21 #include "remoting/base/auto_thread_task_runner.h" |
21 #include "remoting/host/chromoting_host_context.h" | 22 #include "remoting/host/chromoting_host_context.h" |
| 23 #include "remoting/host/it2me/it2me_confirmation_dialog.h" |
22 #include "remoting/host/policy_watcher.h" | 24 #include "remoting/host/policy_watcher.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
24 | 26 |
25 namespace remoting { | 27 namespace remoting { |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
| 31 // Shortening some type names for readability. |
29 typedef protocol::ValidatingAuthenticator::Result ValidationResult; | 32 typedef protocol::ValidatingAuthenticator::Result ValidationResult; |
| 33 typedef It2MeConfirmationDialog::Result DialogResult; |
30 | 34 |
| 35 const char kTestClientUserName[] = "ficticious_user@gmail.com"; |
31 const char kTestClientJid[] = "ficticious_user@gmail.com/jid_resource"; | 36 const char kTestClientJid[] = "ficticious_user@gmail.com/jid_resource"; |
32 const char kTestClientUsernameNoJid[] = "completely_ficticious_user@gmail.com"; | 37 const char kTestClientUsernameNoJid[] = "completely_ficticious_user@gmail.com"; |
33 const char kTestClientJidWithSlash[] = "fake/user@gmail.com/jid_resource"; | 38 const char kTestClientJidWithSlash[] = "fake/user@gmail.com/jid_resource"; |
| 39 const char kResourceOnly[] = "/jid_resource"; |
34 const char kMatchingDomain[] = "gmail.com"; | 40 const char kMatchingDomain[] = "gmail.com"; |
35 const char kMismatchedDomain1[] = "similar_to_gmail.com"; | 41 const char kMismatchedDomain1[] = "similar_to_gmail.com"; |
36 const char kMismatchedDomain2[] = "gmail_at_the_beginning.com"; | 42 const char kMismatchedDomain2[] = "gmail_at_the_beginning.com"; |
37 const char kMismatchedDomain3[] = "not_even_close.com"; | 43 const char kMismatchedDomain3[] = "not_even_close.com"; |
38 | 44 |
39 } // namespace | 45 } // namespace |
40 | 46 |
| 47 class FakeIt2MeConfirmationDialog : public It2MeConfirmationDialog { |
| 48 public: |
| 49 FakeIt2MeConfirmationDialog(); |
| 50 ~FakeIt2MeConfirmationDialog() override; |
| 51 |
| 52 // It2MeConfirmationDialog implementation. |
| 53 void Show(const std::string& remote_user_email, |
| 54 const ResultCallback& callback) override; |
| 55 |
| 56 void set_dialog_result(DialogResult dialog_result) { |
| 57 dialog_result_ = dialog_result; |
| 58 } |
| 59 |
| 60 const std::string& get_remote_user_email() { return remote_user_email_; } |
| 61 |
| 62 private: |
| 63 std::string remote_user_email_; |
| 64 DialogResult dialog_result_ = DialogResult::OK; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(FakeIt2MeConfirmationDialog); |
| 67 }; |
| 68 |
| 69 FakeIt2MeConfirmationDialog::FakeIt2MeConfirmationDialog() {} |
| 70 |
| 71 FakeIt2MeConfirmationDialog::~FakeIt2MeConfirmationDialog() {} |
| 72 |
| 73 void FakeIt2MeConfirmationDialog::Show(const std::string& remote_user_email, |
| 74 const ResultCallback& callback) { |
| 75 remote_user_email_ = remote_user_email; |
| 76 |
| 77 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 78 FROM_HERE, base::Bind(callback, dialog_result_)); |
| 79 } |
| 80 |
| 81 class FakeIt2MeConfirmationDialogFactory |
| 82 : public It2MeConfirmationDialogFactory { |
| 83 public: |
| 84 FakeIt2MeConfirmationDialogFactory(); |
| 85 ~FakeIt2MeConfirmationDialogFactory() override; |
| 86 |
| 87 // It2MeConfirmationDialogFactory override. |
| 88 std::unique_ptr<It2MeConfirmationDialog> Create() override; |
| 89 |
| 90 void set_confirmation_dialog( |
| 91 std::unique_ptr<It2MeConfirmationDialog> confirmation_dialog) { |
| 92 confirmation_dialog_ = std::move(confirmation_dialog); |
| 93 } |
| 94 |
| 95 private: |
| 96 std::unique_ptr<It2MeConfirmationDialog> confirmation_dialog_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(FakeIt2MeConfirmationDialogFactory); |
| 99 }; |
| 100 |
| 101 FakeIt2MeConfirmationDialogFactory::FakeIt2MeConfirmationDialogFactory() {} |
| 102 |
| 103 FakeIt2MeConfirmationDialogFactory::~FakeIt2MeConfirmationDialogFactory() {} |
| 104 |
| 105 std::unique_ptr<It2MeConfirmationDialog> |
| 106 FakeIt2MeConfirmationDialogFactory::Create() { |
| 107 return std::move(confirmation_dialog_); |
| 108 } |
| 109 |
41 class It2MeHostTest : public testing::Test { | 110 class It2MeHostTest : public testing::Test { |
42 public: | 111 public: |
43 It2MeHostTest() {} | 112 It2MeHostTest() {} |
44 ~It2MeHostTest() override {} | 113 ~It2MeHostTest() override {} |
45 | 114 |
46 // testing::Test interface. | 115 // testing::Test interface. |
47 void SetUp() override; | 116 void SetUp() override; |
48 void TearDown() override; | 117 void TearDown() override; |
49 | 118 |
50 void OnValidationComplete(const base::Closure& resume_callback, | 119 void OnValidationComplete(const base::Closure& resume_callback, |
51 ValidationResult validation_result); | 120 ValidationResult validation_result); |
52 | 121 |
53 protected: | 122 protected: |
54 void SetClientDomainPolicy(const std::string& policy_value); | 123 void SetClientDomainPolicy(const std::string& policy_value); |
55 | 124 |
56 void RunValidationCallback(const std::string& remote_jid); | 125 void RunValidationCallback(const std::string& remote_jid); |
57 | 126 |
58 ValidationResult validation_result_ = ValidationResult::SUCCESS; | 127 ValidationResult validation_result_ = ValidationResult::SUCCESS; |
59 | 128 |
| 129 // Used to set ConfirmationDialog behavior. |
| 130 FakeIt2MeConfirmationDialogFactory* fake_dialog_factory_ = nullptr; |
| 131 |
60 private: | 132 private: |
61 std::unique_ptr<base::MessageLoop> message_loop_; | 133 std::unique_ptr<base::MessageLoop> message_loop_; |
62 std::unique_ptr<base::RunLoop> run_loop_; | 134 std::unique_ptr<base::RunLoop> run_loop_; |
63 | 135 |
64 scoped_refptr<It2MeHost> it2me_host_; | 136 scoped_refptr<It2MeHost> it2me_host_; |
65 | 137 |
66 std::string directory_bot_jid_; | 138 std::string directory_bot_jid_; |
67 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; | 139 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; |
68 | 140 |
69 DISALLOW_COPY_AND_ASSIGN(It2MeHostTest); | 141 DISALLOW_COPY_AND_ASSIGN(It2MeHostTest); |
70 }; | 142 }; |
71 | 143 |
72 void It2MeHostTest::SetUp() { | 144 void It2MeHostTest::SetUp() { |
73 message_loop_.reset(new base::MessageLoop()); | 145 message_loop_.reset(new base::MessageLoop()); |
74 run_loop_.reset(new base::RunLoop()); | 146 run_loop_.reset(new base::RunLoop()); |
75 | 147 |
| 148 fake_dialog_factory_ = new FakeIt2MeConfirmationDialogFactory(); |
76 scoped_refptr<AutoThreadTaskRunner> auto_thread_task_runner = | 149 scoped_refptr<AutoThreadTaskRunner> auto_thread_task_runner = |
77 new AutoThreadTaskRunner(base::ThreadTaskRunnerHandle::Get(), | 150 new AutoThreadTaskRunner(base::ThreadTaskRunnerHandle::Get(), |
78 run_loop_->QuitClosure()); | 151 run_loop_->QuitClosure()); |
79 it2me_host_ = new It2MeHost( | 152 it2me_host_ = new It2MeHost( |
80 ChromotingHostContext::Create(auto_thread_task_runner), | 153 ChromotingHostContext::Create(auto_thread_task_runner), |
81 /*policy_watcher=*/nullptr, | 154 /*policy_watcher=*/nullptr, base::WrapUnique(fake_dialog_factory_), |
82 /*confirmation_dialog_factory=*/nullptr, | |
83 /*observer=*/nullptr, xmpp_server_config_, directory_bot_jid_); | 155 /*observer=*/nullptr, xmpp_server_config_, directory_bot_jid_); |
84 } | 156 } |
85 | 157 |
86 void It2MeHostTest::TearDown() { | 158 void It2MeHostTest::TearDown() { |
87 it2me_host_ = nullptr; | 159 it2me_host_ = nullptr; |
88 run_loop_->Run(); | 160 run_loop_->Run(); |
89 } | 161 } |
90 | 162 |
91 void It2MeHostTest::OnValidationComplete(const base::Closure& resume_callback, | 163 void It2MeHostTest::OnValidationComplete(const base::Closure& resume_callback, |
92 ValidationResult validation_result) { | 164 ValidationResult validation_result) { |
(...skipping 20 matching lines...) Expand all Loading... |
113 run_loop.Run(); | 185 run_loop.Run(); |
114 } | 186 } |
115 | 187 |
116 TEST_F(It2MeHostTest, ConnectionValidation_NoClientDomainPolicy_ValidJid) { | 188 TEST_F(It2MeHostTest, ConnectionValidation_NoClientDomainPolicy_ValidJid) { |
117 RunValidationCallback(kTestClientJid); | 189 RunValidationCallback(kTestClientJid); |
118 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); | 190 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
119 } | 191 } |
120 | 192 |
121 TEST_F(It2MeHostTest, ConnectionValidation_NoClientDomainPolicy_InvalidJid) { | 193 TEST_F(It2MeHostTest, ConnectionValidation_NoClientDomainPolicy_InvalidJid) { |
122 RunValidationCallback(kTestClientUsernameNoJid); | 194 RunValidationCallback(kTestClientUsernameNoJid); |
123 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); | 195 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
124 } | 196 } |
125 | 197 |
126 TEST_F(It2MeHostTest, | 198 TEST_F(It2MeHostTest, |
127 ConnectionValidation_NoClientDomainPolicy_InvalidUsername) { | 199 ConnectionValidation_NoClientDomainPolicy_InvalidUsername) { |
128 RunValidationCallback(kTestClientJidWithSlash); | 200 RunValidationCallback(kTestClientJidWithSlash); |
129 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); | 201 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
130 } | 202 } |
131 | 203 |
| 204 TEST_F(It2MeHostTest, ConnectionValidation_NoClientDomainPolicy_ResourceOnly) { |
| 205 RunValidationCallback(kResourceOnly); |
| 206 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
| 207 } |
| 208 |
132 TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_MatchingDomain) { | 209 TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_MatchingDomain) { |
133 SetClientDomainPolicy(kMatchingDomain); | 210 SetClientDomainPolicy(kMatchingDomain); |
134 RunValidationCallback(kTestClientJid); | 211 RunValidationCallback(kTestClientJid); |
135 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); | 212 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
136 } | 213 } |
137 | 214 |
138 TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_InvalidUserName) { | 215 TEST_F(It2MeHostTest, ConnectionValidation_ClientDomainPolicy_InvalidUserName) { |
139 SetClientDomainPolicy(kMatchingDomain); | 216 SetClientDomainPolicy(kMatchingDomain); |
140 RunValidationCallback(kTestClientJidWithSlash); | 217 RunValidationCallback(kTestClientJidWithSlash); |
141 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); | 218 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
(...skipping 16 matching lines...) Expand all Loading... |
158 RunValidationCallback(kTestClientJid); | 235 RunValidationCallback(kTestClientJid); |
159 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); | 236 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
160 } | 237 } |
161 | 238 |
162 TEST_F(It2MeHostTest, ConnectionValidation_WrongClientDomain_MatchEnd) { | 239 TEST_F(It2MeHostTest, ConnectionValidation_WrongClientDomain_MatchEnd) { |
163 SetClientDomainPolicy(kMismatchedDomain1); | 240 SetClientDomainPolicy(kMismatchedDomain1); |
164 RunValidationCallback(kTestClientJid); | 241 RunValidationCallback(kTestClientJid); |
165 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); | 242 ASSERT_EQ(ValidationResult::ERROR_INVALID_ACCOUNT, validation_result_); |
166 } | 243 } |
167 | 244 |
| 245 TEST_F(It2MeHostTest, ConnectionValidation_ConfirmationDialog_NoDialog) { |
| 246 RunValidationCallback(kTestClientJid); |
| 247 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
| 248 } |
| 249 |
| 250 TEST_F(It2MeHostTest, ConnectionValidation_ConfirmationDialog_Accept) { |
| 251 FakeIt2MeConfirmationDialog* dialog = new FakeIt2MeConfirmationDialog(); |
| 252 fake_dialog_factory_->set_confirmation_dialog(base::WrapUnique(dialog)); |
| 253 |
| 254 RunValidationCallback(kTestClientJid); |
| 255 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
| 256 ASSERT_STREQ(kTestClientUserName, dialog->get_remote_user_email().c_str()); |
| 257 } |
| 258 |
| 259 TEST_F(It2MeHostTest, ConnectionValidation_ConfirmationDialog_Reject) { |
| 260 FakeIt2MeConfirmationDialog* dialog = new FakeIt2MeConfirmationDialog(); |
| 261 dialog->set_dialog_result(DialogResult::CANCEL); |
| 262 fake_dialog_factory_->set_confirmation_dialog(base::WrapUnique(dialog)); |
| 263 |
| 264 RunValidationCallback(kTestClientJid); |
| 265 ASSERT_EQ(ValidationResult::ERROR_REJECTED_BY_USER, validation_result_); |
| 266 ASSERT_STREQ(kTestClientUserName, dialog->get_remote_user_email().c_str()); |
| 267 } |
| 268 |
168 } // namespace remoting | 269 } // namespace remoting |
OLD | NEW |