Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(614)

Side by Side Diff: remoting/host/it2me/it2me_host_unittest.cc

Issue 2847853003: Remove policy watching from It2MeHost. (Closed)
Patch Set: Remove pending policies from It2MeNativeMessagingHost and add an accessor to PolicyWatcher. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/host/it2me/it2me_host.cc ('k') | remoting/host/it2me/it2me_native_messaging_host.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 private: 161 private:
163 void StartupHostStateHelper(const base::Closure& quit_closure); 162 void StartupHostStateHelper(const base::Closure& quit_closure);
164 163
165 std::unique_ptr<base::MessageLoop> message_loop_; 164 std::unique_ptr<base::MessageLoop> message_loop_;
166 std::unique_ptr<base::RunLoop> run_loop_; 165 std::unique_ptr<base::RunLoop> run_loop_;
167 166
168 scoped_refptr<AutoThreadTaskRunner> network_task_runner_; 167 scoped_refptr<AutoThreadTaskRunner> network_task_runner_;
169 scoped_refptr<AutoThreadTaskRunner> ui_task_runner_; 168 scoped_refptr<AutoThreadTaskRunner> ui_task_runner_;
170 169
171 scoped_refptr<It2MeHost> it2me_host_; 170 scoped_refptr<It2MeHost> it2me_host_;
172 // The FakeAsyncPolicyLoader is owned by it2me_host_, but we retain a raw
173 // pointer so we can control the policy contents.
174 policy::FakeAsyncPolicyLoader* policy_loader_ = nullptr;
175 171
176 base::WeakPtrFactory<It2MeHostTest> weak_factory_; 172 base::WeakPtrFactory<It2MeHostTest> weak_factory_;
177 173
178 DISALLOW_COPY_AND_ASSIGN(It2MeHostTest); 174 DISALLOW_COPY_AND_ASSIGN(It2MeHostTest);
179 }; 175 };
180 176
181 It2MeHostTest::It2MeHostTest() : weak_factory_(this) {} 177 It2MeHostTest::It2MeHostTest() : weak_factory_(this) {}
182 178
183 It2MeHostTest::~It2MeHostTest() {} 179 It2MeHostTest::~It2MeHostTest() {}
184 180
185 void It2MeHostTest::SetUp() { 181 void It2MeHostTest::SetUp() {
186 message_loop_.reset(new base::MessageLoop()); 182 message_loop_.reset(new base::MessageLoop());
187 run_loop_.reset(new base::RunLoop()); 183 run_loop_.reset(new base::RunLoop());
188 184
189 std::unique_ptr<ChromotingHostContext> host_context( 185 std::unique_ptr<ChromotingHostContext> host_context(
190 ChromotingHostContext::Create(new AutoThreadTaskRunner( 186 ChromotingHostContext::Create(new AutoThreadTaskRunner(
191 base::ThreadTaskRunnerHandle::Get(), run_loop_->QuitClosure()))); 187 base::ThreadTaskRunnerHandle::Get(), run_loop_->QuitClosure())));
192 network_task_runner_ = host_context->network_task_runner(); 188 network_task_runner_ = host_context->network_task_runner();
193 ui_task_runner_ = host_context->ui_task_runner(); 189 ui_task_runner_ = host_context->ui_task_runner();
194 190
195 std::unique_ptr<FakeIt2MeDialogFactory> dialog_factory( 191 std::unique_ptr<FakeIt2MeDialogFactory> dialog_factory(
196 new FakeIt2MeDialogFactory()); 192 new FakeIt2MeDialogFactory());
197 dialog_factory_ = dialog_factory.get(); 193 dialog_factory_ = dialog_factory.get();
198 policy_loader_ =
199 new policy::FakeAsyncPolicyLoader(base::ThreadTaskRunnerHandle::Get());
200 it2me_host_ = new It2MeHost( 194 it2me_host_ = new It2MeHost(
201 std::move(host_context), 195 std::move(host_context),
202 PolicyWatcher::CreateFromPolicyLoaderForTesting(
203 base::WrapUnique(policy_loader_)),
204 std::move(dialog_factory), weak_factory_.GetWeakPtr(), 196 std::move(dialog_factory), weak_factory_.GetWeakPtr(),
205 base::WrapUnique( 197 base::WrapUnique(
206 new FakeSignalStrategy(SignalingAddress("fake_local_jid"))), 198 new FakeSignalStrategy(SignalingAddress("fake_local_jid"))),
207 kTestUserName, "fake_bot_jid"); 199 kTestUserName, "fake_bot_jid");
208 } 200 }
209 201
210 void It2MeHostTest::TearDown() { 202 void It2MeHostTest::TearDown() {
211 // Shutdown the host if it hasn't been already. Without this, the call to 203 // Shutdown the host if it hasn't been already. Without this, the call to
212 // run_loop_->Run() may never return. 204 // run_loop_->Run() may never return.
213 it2me_host_->Disconnect(); 205 it2me_host_->Disconnect();
214 network_task_runner_ = nullptr; 206 network_task_runner_ = nullptr;
215 ui_task_runner_ = nullptr; 207 ui_task_runner_ = nullptr;
216 it2me_host_ = nullptr; 208 it2me_host_ = nullptr;
217 run_loop_->Run(); 209 run_loop_->Run();
218 } 210 }
219 211
220 void It2MeHostTest::OnValidationComplete(const base::Closure& resume_callback, 212 void It2MeHostTest::OnValidationComplete(const base::Closure& resume_callback,
221 ValidationResult validation_result) { 213 ValidationResult validation_result) {
222 validation_result_ = validation_result; 214 validation_result_ = validation_result;
223 215
224 ui_task_runner_->PostTask(FROM_HERE, resume_callback); 216 ui_task_runner_->PostTask(FROM_HERE, resume_callback);
225 } 217 }
226 218
227 void It2MeHostTest::SetPolicies( 219 void It2MeHostTest::SetPolicies(
228 std::initializer_list<std::pair<base::StringPiece, const base::Value&>> 220 std::initializer_list<std::pair<base::StringPiece, const base::Value&>>
229 policies) { 221 policies) {
230 policy::PolicyBundle bundle; 222 std::unique_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue);
rkjnsn 2017/04/28 21:15:36 nit: I think MakeUnique is more readable: auto dic
Jamie 2017/04/28 22:41:09 Done.
231 policy::PolicyMap& map = bundle.Get(
232 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()));
233 for (const auto& policy : policies) { 223 for (const auto& policy : policies) {
234 map.Set(policy.first.as_string(), policy::POLICY_LEVEL_MANDATORY, 224 dictionary->Set(policy.first, policy.second.CreateDeepCopy());
235 policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_PLATFORM,
236 policy.second.CreateDeepCopy(), nullptr);
237 } 225 }
238 policy_loader_->SetPolicies(bundle); 226 it2me_host_->OnPolicyUpdate(std::move(dictionary));
rkjnsn 2017/04/28 21:15:36 Note that this will bypass normalization and depre
joedow 2017/04/28 21:46:32 Using the old system (PolicyWatcher + FakeAsyncPol
Jamie 2017/04/28 22:41:09 The tests hang without a call to SetPolicies (I th
Jamie 2017/04/28 22:41:09 I'll hold off landing until that's in then. I thin
rkjnsn 2017/05/02 19:19:12 It2MeHost will indeed wait for OnPolicyUpdate to b
Jamie 2017/05/02 22:07:56 I think that's mixing up responsibilities too much
rkjnsn 2017/05/02 23:20:19 I mostly agree. My main concern is here was the di
rkjnsn 2017/05/02 23:27:19 their*
Jamie 2017/05/02 23:49:03 That's a good point. I was going to argue that It2
Jamie 2017/05/03 20:41:04 I've taken this approach in the most recent iterat
239 policy_loader_->PostReloadOnBackgroundThread(true);
240 base::RunLoop().RunUntilIdle();
241 } 227 }
242 228
243 void It2MeHostTest::StartupHostStateHelper(const base::Closure& quit_closure) { 229 void It2MeHostTest::StartupHostStateHelper(const base::Closure& quit_closure) {
244 if (last_host_state_ == It2MeHostState::kRequestedAccessCode) { 230 if (last_host_state_ == It2MeHostState::kRequestedAccessCode) {
245 network_task_runner_->PostTask( 231 network_task_runner_->PostTask(
246 FROM_HERE, 232 FROM_HERE,
247 base::Bind(&It2MeHost::SetStateForTesting, it2me_host_.get(), 233 base::Bind(&It2MeHost::SetStateForTesting, it2me_host_.get(),
248 It2MeHostState::kReceivedAccessCode, std::string())); 234 It2MeHostState::kReceivedAccessCode, std::string()));
249 } else if (last_host_state_ != It2MeHostState::kStarting) { 235 } else if (last_host_state_ != It2MeHostState::kStarting) {
250 quit_closure.Run(); 236 quit_closure.Run();
251 return; 237 return;
252 } 238 }
253 state_change_callback_ = base::Bind(&It2MeHostTest::StartupHostStateHelper, 239 state_change_callback_ = base::Bind(&It2MeHostTest::StartupHostStateHelper,
254 base::Unretained(this), quit_closure); 240 base::Unretained(this), quit_closure);
255 } 241 }
256 242
257 void It2MeHostTest::StartHost() { 243 void It2MeHostTest::StartHost() {
244 SetPolicies({}); // For tests that don't set any policies
258 it2me_host_->Connect(); 245 it2me_host_->Connect();
259 246
260 base::RunLoop run_loop; 247 base::RunLoop run_loop;
261 state_change_callback_ = 248 state_change_callback_ =
262 base::Bind(&It2MeHostTest::StartupHostStateHelper, base::Unretained(this), 249 base::Bind(&It2MeHostTest::StartupHostStateHelper, base::Unretained(this),
263 run_loop.QuitClosure()); 250 run_loop.QuitClosure());
264 run_loop.Run(); 251 run_loop.Run();
265 } 252 }
266 253
267 void It2MeHostTest::RunUntilStateChanged(It2MeHostState expected_state) { 254 void It2MeHostTest::RunUntilStateChanged(It2MeHostState expected_state) {
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_); 462 ASSERT_EQ(ValidationResult::SUCCESS, validation_result_);
476 ASSERT_EQ(It2MeHostState::kConnecting, last_host_state_); 463 ASSERT_EQ(It2MeHostState::kConnecting, last_host_state_);
477 464
478 RunValidationCallback(kTestClientJid2); 465 RunValidationCallback(kTestClientJid2);
479 ASSERT_EQ(ValidationResult::ERROR_TOO_MANY_CONNECTIONS, validation_result_); 466 ASSERT_EQ(ValidationResult::ERROR_TOO_MANY_CONNECTIONS, validation_result_);
480 RunUntilStateChanged(It2MeHostState::kDisconnected); 467 RunUntilStateChanged(It2MeHostState::kDisconnected);
481 ASSERT_EQ(It2MeHostState::kDisconnected, last_host_state_); 468 ASSERT_EQ(It2MeHostState::kDisconnected, last_host_state_);
482 } 469 }
483 470
484 } // namespace remoting 471 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/it2me/it2me_host.cc ('k') | remoting/host/it2me/it2me_native_messaging_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698