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/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
20 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
22 #include "components/policy/core/common/fake_async_policy_loader.h" | |
23 #include "components/policy/policy_constants.h" | 22 #include "components/policy/policy_constants.h" |
24 #include "remoting/base/auto_thread_task_runner.h" | 23 #include "remoting/base/auto_thread_task_runner.h" |
25 #include "remoting/host/chromoting_host_context.h" | 24 #include "remoting/host/chromoting_host_context.h" |
26 #include "remoting/host/it2me/it2me_confirmation_dialog.h" | 25 #include "remoting/host/it2me/it2me_confirmation_dialog.h" |
27 #include "remoting/host/policy_watcher.h" | 26 #include "remoting/host/policy_watcher.h" |
28 #include "remoting/signaling/fake_signal_strategy.h" | 27 #include "remoting/signaling/fake_signal_strategy.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
30 | 29 |
31 namespace remoting { | 30 namespace remoting { |
32 | 31 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 private: | 164 private: |
166 void StartupHostStateHelper(const base::Closure& quit_closure); | 165 void StartupHostStateHelper(const base::Closure& quit_closure); |
167 | 166 |
168 std::unique_ptr<base::MessageLoop> message_loop_; | 167 std::unique_ptr<base::MessageLoop> message_loop_; |
169 std::unique_ptr<base::RunLoop> run_loop_; | 168 std::unique_ptr<base::RunLoop> run_loop_; |
170 | 169 |
171 scoped_refptr<AutoThreadTaskRunner> network_task_runner_; | 170 scoped_refptr<AutoThreadTaskRunner> network_task_runner_; |
172 scoped_refptr<AutoThreadTaskRunner> ui_task_runner_; | 171 scoped_refptr<AutoThreadTaskRunner> ui_task_runner_; |
173 | 172 |
174 scoped_refptr<It2MeHost> it2me_host_; | 173 scoped_refptr<It2MeHost> it2me_host_; |
175 // The FakeAsyncPolicyLoader is owned by it2me_host_, but we retain a raw | |
176 // pointer so we can control the policy contents. | |
177 policy::FakeAsyncPolicyLoader* policy_loader_ = nullptr; | |
178 | 174 |
179 base::WeakPtrFactory<It2MeHostTest> weak_factory_; | 175 base::WeakPtrFactory<It2MeHostTest> weak_factory_; |
180 | 176 |
181 DISALLOW_COPY_AND_ASSIGN(It2MeHostTest); | 177 DISALLOW_COPY_AND_ASSIGN(It2MeHostTest); |
182 }; | 178 }; |
183 | 179 |
184 It2MeHostTest::It2MeHostTest() : weak_factory_(this) {} | 180 It2MeHostTest::It2MeHostTest() : weak_factory_(this) {} |
185 | 181 |
186 It2MeHostTest::~It2MeHostTest() {} | 182 It2MeHostTest::~It2MeHostTest() {} |
187 | 183 |
188 void It2MeHostTest::SetUp() { | 184 void It2MeHostTest::SetUp() { |
189 message_loop_.reset(new base::MessageLoop()); | 185 message_loop_.reset(new base::MessageLoop()); |
190 run_loop_.reset(new base::RunLoop()); | 186 run_loop_.reset(new base::RunLoop()); |
191 | 187 |
192 std::unique_ptr<ChromotingHostContext> host_context( | 188 std::unique_ptr<ChromotingHostContext> host_context( |
193 ChromotingHostContext::Create(new AutoThreadTaskRunner( | 189 ChromotingHostContext::Create(new AutoThreadTaskRunner( |
194 base::ThreadTaskRunnerHandle::Get(), run_loop_->QuitClosure()))); | 190 base::ThreadTaskRunnerHandle::Get(), run_loop_->QuitClosure()))); |
195 network_task_runner_ = host_context->network_task_runner(); | 191 network_task_runner_ = host_context->network_task_runner(); |
196 ui_task_runner_ = host_context->ui_task_runner(); | 192 ui_task_runner_ = host_context->ui_task_runner(); |
197 | 193 |
198 std::unique_ptr<FakeIt2MeDialogFactory> dialog_factory( | 194 std::unique_ptr<FakeIt2MeDialogFactory> dialog_factory( |
199 new FakeIt2MeDialogFactory()); | 195 new FakeIt2MeDialogFactory()); |
200 dialog_factory_ = dialog_factory.get(); | 196 dialog_factory_ = dialog_factory.get(); |
201 policy_loader_ = | |
202 new policy::FakeAsyncPolicyLoader(base::ThreadTaskRunnerHandle::Get()); | |
203 it2me_host_ = new It2MeHost( | 197 it2me_host_ = new It2MeHost( |
204 std::move(host_context), | 198 std::move(host_context), |
205 PolicyWatcher::CreateFromPolicyLoaderForTesting( | |
206 base::WrapUnique(policy_loader_)), | |
207 std::move(dialog_factory), weak_factory_.GetWeakPtr(), | 199 std::move(dialog_factory), weak_factory_.GetWeakPtr(), |
208 base::WrapUnique( | 200 base::WrapUnique( |
209 new FakeSignalStrategy(SignalingAddress("fake_local_jid"))), | 201 new FakeSignalStrategy(SignalingAddress("fake_local_jid"))), |
210 kTestUserName, "fake_bot_jid"); | 202 kTestUserName, "fake_bot_jid"); |
| 203 |
| 204 it2me_host_->OnPolicyUpdate(PolicyWatcher::GetDefaultPolicies()); |
211 } | 205 } |
212 | 206 |
213 void It2MeHostTest::TearDown() { | 207 void It2MeHostTest::TearDown() { |
214 // Shutdown the host if it hasn't been already. Without this, the call to | 208 // Shutdown the host if it hasn't been already. Without this, the call to |
215 // run_loop_->Run() may never return. | 209 // run_loop_->Run() may never return. |
216 it2me_host_->Disconnect(); | 210 it2me_host_->Disconnect(); |
217 network_task_runner_ = nullptr; | 211 network_task_runner_ = nullptr; |
218 ui_task_runner_ = nullptr; | 212 ui_task_runner_ = nullptr; |
219 it2me_host_ = nullptr; | 213 it2me_host_ = nullptr; |
220 run_loop_->Run(); | 214 run_loop_->Run(); |
221 } | 215 } |
222 | 216 |
223 void It2MeHostTest::OnValidationComplete(const base::Closure& resume_callback, | 217 void It2MeHostTest::OnValidationComplete(const base::Closure& resume_callback, |
224 ValidationResult validation_result) { | 218 ValidationResult validation_result) { |
225 validation_result_ = validation_result; | 219 validation_result_ = validation_result; |
226 | 220 |
227 ui_task_runner_->PostTask(FROM_HERE, resume_callback); | 221 ui_task_runner_->PostTask(FROM_HERE, resume_callback); |
228 } | 222 } |
229 | 223 |
230 void It2MeHostTest::SetPolicies( | 224 void It2MeHostTest::SetPolicies( |
231 std::initializer_list<std::pair<base::StringPiece, const base::Value&>> | 225 std::initializer_list<std::pair<base::StringPiece, const base::Value&>> |
232 policies) { | 226 policies) { |
233 policy::PolicyBundle bundle; | 227 auto dictionary = base::MakeUnique<base::DictionaryValue>(); |
234 policy::PolicyMap& map = bundle.Get( | |
235 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string())); | |
236 for (const auto& policy : policies) { | 228 for (const auto& policy : policies) { |
237 map.Set(policy.first.as_string(), policy::POLICY_LEVEL_MANDATORY, | 229 dictionary->Set(policy.first, policy.second.CreateDeepCopy()); |
238 policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_PLATFORM, | |
239 policy.second.CreateDeepCopy(), nullptr); | |
240 } | 230 } |
241 policy_loader_->SetPolicies(bundle); | 231 it2me_host_->OnPolicyUpdate(std::move(dictionary)); |
242 policy_loader_->PostReloadOnBackgroundThread(true); | |
243 base::RunLoop().RunUntilIdle(); | |
244 } | 232 } |
245 | 233 |
246 void It2MeHostTest::StartupHostStateHelper(const base::Closure& quit_closure) { | 234 void It2MeHostTest::StartupHostStateHelper(const base::Closure& quit_closure) { |
247 if (last_host_state_ == It2MeHostState::kRequestedAccessCode) { | 235 if (last_host_state_ == It2MeHostState::kRequestedAccessCode) { |
248 network_task_runner_->PostTask( | 236 network_task_runner_->PostTask( |
249 FROM_HERE, | 237 FROM_HERE, |
250 base::Bind(&It2MeHost::SetStateForTesting, it2me_host_.get(), | 238 base::Bind(&It2MeHost::SetStateForTesting, it2me_host_.get(), |
251 It2MeHostState::kReceivedAccessCode, std::string())); | 239 It2MeHostState::kReceivedAccessCode, std::string())); |
252 } else if (last_host_state_ != It2MeHostState::kStarting) { | 240 } else if (last_host_state_ != It2MeHostState::kStarting) { |
253 quit_closure.Run(); | 241 quit_closure.Run(); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); | 521 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); |
534 ASSERT_EQ(It2MeHostState::kConnecting, last_host_state_); | 522 ASSERT_EQ(It2MeHostState::kConnecting, last_host_state_); |
535 | 523 |
536 RunValidationCallback(kTestClientJid2); | 524 RunValidationCallback(kTestClientJid2); |
537 ASSERT_EQ(ValidationResult::ERROR_TOO_MANY_CONNECTIONS, validation_result_); | 525 ASSERT_EQ(ValidationResult::ERROR_TOO_MANY_CONNECTIONS, validation_result_); |
538 RunUntilStateChanged(It2MeHostState::kDisconnected); | 526 RunUntilStateChanged(It2MeHostState::kDisconnected); |
539 ASSERT_EQ(It2MeHostState::kDisconnected, last_host_state_); | 527 ASSERT_EQ(It2MeHostState::kDisconnected, last_host_state_); |
540 } | 528 } |
541 | 529 |
542 } // namespace remoting | 530 } // namespace remoting |
OLD | NEW |