Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / remoting / host / policy_hack / policy_watcher_unittest.cc
blob729a7bbad06620a00147a7070a9cf56b0a320c2d
1 // Copyright (c) 2012 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.
5 #include "base/basictypes.h"
6 #include "base/bind.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "remoting/host/dns_blackhole_checker.h"
11 #include "remoting/host/policy_hack/fake_policy_watcher.h"
12 #include "remoting/host/policy_hack/mock_policy_callback.h"
13 #include "remoting/host/policy_hack/policy_watcher.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace remoting {
18 namespace policy_hack {
20 class PolicyWatcherTest : public testing::Test {
21 public:
22 PolicyWatcherTest() {
25 virtual void SetUp() OVERRIDE {
26 message_loop_proxy_ = base::MessageLoopProxy::current();
27 policy_callback_ = base::Bind(&MockPolicyCallback::OnPolicyUpdate,
28 base::Unretained(&mock_policy_callback_));
29 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_));
30 nat_true_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
31 nat_false_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
32 nat_one_.SetInteger(PolicyWatcher::kNatPolicyName, 1);
33 domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName,
34 std::string());
35 domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, kHostDomain);
36 SetDefaults(nat_true_others_default_);
37 nat_true_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
38 SetDefaults(nat_false_others_default_);
39 nat_false_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
40 SetDefaults(domain_empty_others_default_);
41 domain_empty_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
42 std::string());
43 SetDefaults(domain_full_others_default_);
44 domain_full_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
45 kHostDomain);
46 nat_true_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
47 nat_true_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName,
48 std::string());
49 nat_true_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
50 nat_true_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
51 kHostDomain);
52 nat_false_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
53 nat_false_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName,
54 std::string());
55 nat_false_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
56 nat_false_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
57 kHostDomain);
58 SetDefaults(nat_true_domain_empty_others_default_);
59 nat_true_domain_empty_others_default_.SetBoolean(
60 PolicyWatcher::kNatPolicyName, true);
61 nat_true_domain_empty_others_default_.SetString(
62 PolicyWatcher::kHostDomainPolicyName, std::string());
63 unknown_policies_.SetString("UnknownPolicyOne", std::string());
64 unknown_policies_.SetString("UnknownPolicyTwo", std::string());
66 const char kOverrideNatTraversalToFalse[] =
67 "{ \"RemoteAccessHostFirewallTraversal\": false }";
68 nat_true_and_overridden_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
69 nat_true_and_overridden_.SetString(
70 PolicyWatcher::kHostDebugOverridePoliciesName,
71 kOverrideNatTraversalToFalse);
72 pairing_true_.SetBoolean(PolicyWatcher::kHostAllowClientPairing, true);
73 pairing_false_.SetBoolean(PolicyWatcher::kHostAllowClientPairing, false);
74 gnubby_auth_true_.SetBoolean(PolicyWatcher::kHostAllowGnubbyAuthPolicyName,
75 true);
76 gnubby_auth_false_.SetBoolean(PolicyWatcher::kHostAllowGnubbyAuthPolicyName,
77 false);
78 #if !defined(NDEBUG)
79 SetDefaults(nat_false_overridden_others_default_);
80 nat_false_overridden_others_default_.SetBoolean(
81 PolicyWatcher::kNatPolicyName, false);
82 nat_false_overridden_others_default_.SetString(
83 PolicyWatcher::kHostDebugOverridePoliciesName,
84 kOverrideNatTraversalToFalse);
85 #endif
88 protected:
89 void StartWatching() {
90 policy_watcher_->StartWatching(policy_callback_);
91 base::RunLoop().RunUntilIdle();
94 void StopWatching() {
95 base::WaitableEvent stop_event(false, false);
96 policy_watcher_->StopWatching(&stop_event);
97 base::RunLoop().RunUntilIdle();
98 EXPECT_EQ(true, stop_event.IsSignaled());
101 static const char* kHostDomain;
102 base::MessageLoop message_loop_;
103 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
104 MockPolicyCallback mock_policy_callback_;
105 PolicyWatcher::PolicyCallback policy_callback_;
106 scoped_ptr<FakePolicyWatcher> policy_watcher_;
107 base::DictionaryValue empty_;
108 base::DictionaryValue nat_true_;
109 base::DictionaryValue nat_false_;
110 base::DictionaryValue nat_one_;
111 base::DictionaryValue domain_empty_;
112 base::DictionaryValue domain_full_;
113 base::DictionaryValue nat_true_others_default_;
114 base::DictionaryValue nat_false_others_default_;
115 base::DictionaryValue domain_empty_others_default_;
116 base::DictionaryValue domain_full_others_default_;
117 base::DictionaryValue nat_true_domain_empty_;
118 base::DictionaryValue nat_true_domain_full_;
119 base::DictionaryValue nat_false_domain_empty_;
120 base::DictionaryValue nat_false_domain_full_;
121 base::DictionaryValue nat_true_domain_empty_others_default_;
122 base::DictionaryValue unknown_policies_;
123 base::DictionaryValue nat_true_and_overridden_;
124 base::DictionaryValue nat_false_overridden_others_default_;
125 base::DictionaryValue pairing_true_;
126 base::DictionaryValue pairing_false_;
127 base::DictionaryValue gnubby_auth_true_;
128 base::DictionaryValue gnubby_auth_false_;
130 private:
131 void SetDefaults(base::DictionaryValue& dict) {
132 dict.SetBoolean(PolicyWatcher::kNatPolicyName, true);
133 dict.SetBoolean(PolicyWatcher::kHostRequireTwoFactorPolicyName, false);
134 dict.SetString(PolicyWatcher::kHostDomainPolicyName, std::string());
135 dict.SetBoolean(PolicyWatcher::kHostMatchUsernamePolicyName, false);
136 dict.SetString(PolicyWatcher::kHostTalkGadgetPrefixPolicyName,
137 kDefaultHostTalkGadgetPrefix);
138 dict.SetBoolean(PolicyWatcher::kHostRequireCurtainPolicyName, false);
139 dict.SetString(PolicyWatcher::kHostTokenUrlPolicyName, std::string());
140 dict.SetString(PolicyWatcher::kHostTokenValidationUrlPolicyName,
141 std::string());
142 dict.SetString(PolicyWatcher::kHostTokenValidationCertIssuerPolicyName,
143 std::string());
144 dict.SetBoolean(PolicyWatcher::kHostAllowClientPairing, true);
145 dict.SetBoolean(PolicyWatcher::kHostAllowGnubbyAuthPolicyName, true);
146 #if !defined(NDEBUG)
147 dict.SetString(PolicyWatcher::kHostDebugOverridePoliciesName, "");
148 #endif
152 const char* PolicyWatcherTest::kHostDomain = "google.com";
154 MATCHER_P(IsPolicies, dict, "") {
155 return arg->Equals(dict);
158 TEST_F(PolicyWatcherTest, None) {
159 EXPECT_CALL(mock_policy_callback_,
160 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
162 StartWatching();
163 policy_watcher_->SetPolicies(&empty_);
164 StopWatching();
167 TEST_F(PolicyWatcherTest, NatTrue) {
168 EXPECT_CALL(mock_policy_callback_,
169 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
171 StartWatching();
172 policy_watcher_->SetPolicies(&nat_true_);
173 StopWatching();
176 TEST_F(PolicyWatcherTest, NatFalse) {
177 EXPECT_CALL(mock_policy_callback_,
178 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
180 StartWatching();
181 policy_watcher_->SetPolicies(&nat_false_);
182 StopWatching();
185 TEST_F(PolicyWatcherTest, NatOne) {
186 EXPECT_CALL(mock_policy_callback_,
187 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
189 StartWatching();
190 policy_watcher_->SetPolicies(&nat_one_);
191 StopWatching();
194 TEST_F(PolicyWatcherTest, DomainEmpty) {
195 EXPECT_CALL(mock_policy_callback_,
196 OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_)));
198 StartWatching();
199 policy_watcher_->SetPolicies(&domain_empty_);
200 StopWatching();
203 TEST_F(PolicyWatcherTest, DomainFull) {
204 EXPECT_CALL(mock_policy_callback_,
205 OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_)));
207 StartWatching();
208 policy_watcher_->SetPolicies(&domain_full_);
209 StopWatching();
212 TEST_F(PolicyWatcherTest, NatNoneThenTrue) {
213 EXPECT_CALL(mock_policy_callback_,
214 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
216 StartWatching();
217 policy_watcher_->SetPolicies(&empty_);
218 policy_watcher_->SetPolicies(&nat_true_);
219 StopWatching();
222 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) {
223 EXPECT_CALL(mock_policy_callback_,
224 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
226 StartWatching();
227 policy_watcher_->SetPolicies(&empty_);
228 policy_watcher_->SetPolicies(&nat_true_);
229 policy_watcher_->SetPolicies(&nat_true_);
230 StopWatching();
233 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) {
234 testing::InSequence sequence;
235 EXPECT_CALL(mock_policy_callback_,
236 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
237 EXPECT_CALL(mock_policy_callback_,
238 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
240 StartWatching();
241 policy_watcher_->SetPolicies(&empty_);
242 policy_watcher_->SetPolicies(&nat_true_);
243 policy_watcher_->SetPolicies(&nat_true_);
244 policy_watcher_->SetPolicies(&nat_false_);
245 StopWatching();
248 TEST_F(PolicyWatcherTest, NatNoneThenFalse) {
249 testing::InSequence sequence;
250 EXPECT_CALL(mock_policy_callback_,
251 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
252 EXPECT_CALL(mock_policy_callback_,
253 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
255 StartWatching();
256 policy_watcher_->SetPolicies(&empty_);
257 policy_watcher_->SetPolicies(&nat_false_);
258 StopWatching();
261 TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) {
262 testing::InSequence sequence;
263 EXPECT_CALL(mock_policy_callback_,
264 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
265 EXPECT_CALL(mock_policy_callback_,
266 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
267 EXPECT_CALL(mock_policy_callback_,
268 OnPolicyUpdatePtr(IsPolicies(&nat_true_)));
270 StartWatching();
271 policy_watcher_->SetPolicies(&empty_);
272 policy_watcher_->SetPolicies(&nat_false_);
273 policy_watcher_->SetPolicies(&nat_true_);
274 StopWatching();
277 TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) {
278 testing::InSequence sequence;
279 EXPECT_CALL(mock_policy_callback_,
280 OnPolicyUpdatePtr(IsPolicies(
281 &nat_true_domain_empty_others_default_)));
282 EXPECT_CALL(mock_policy_callback_,
283 OnPolicyUpdatePtr(IsPolicies(&domain_full_)));
284 EXPECT_CALL(mock_policy_callback_,
285 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
286 EXPECT_CALL(mock_policy_callback_,
287 OnPolicyUpdatePtr(IsPolicies(&domain_empty_)));
288 EXPECT_CALL(mock_policy_callback_,
289 OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_)));
291 StartWatching();
292 policy_watcher_->SetPolicies(&nat_true_domain_empty_);
293 policy_watcher_->SetPolicies(&nat_true_domain_full_);
294 policy_watcher_->SetPolicies(&nat_false_domain_full_);
295 policy_watcher_->SetPolicies(&nat_false_domain_empty_);
296 policy_watcher_->SetPolicies(&nat_true_domain_full_);
297 StopWatching();
300 TEST_F(PolicyWatcherTest, FilterUnknownPolicies) {
301 testing::InSequence sequence;
302 EXPECT_CALL(mock_policy_callback_,
303 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
305 StartWatching();
306 policy_watcher_->SetPolicies(&empty_);
307 policy_watcher_->SetPolicies(&unknown_policies_);
308 policy_watcher_->SetPolicies(&empty_);
309 StopWatching();
312 TEST_F(PolicyWatcherTest, DebugOverrideNatPolicy) {
313 #if !defined(NDEBUG)
314 EXPECT_CALL(mock_policy_callback_,
315 OnPolicyUpdatePtr(IsPolicies(&nat_false_overridden_others_default_)));
316 #else
317 EXPECT_CALL(mock_policy_callback_,
318 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
319 #endif
321 StartWatching();
322 policy_watcher_->SetPolicies(&nat_true_and_overridden_);
323 StopWatching();
326 TEST_F(PolicyWatcherTest, PairingFalseThenTrue) {
327 testing::InSequence sequence;
328 EXPECT_CALL(mock_policy_callback_,
329 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
330 EXPECT_CALL(mock_policy_callback_,
331 OnPolicyUpdatePtr(IsPolicies(&pairing_false_)));
332 EXPECT_CALL(mock_policy_callback_,
333 OnPolicyUpdatePtr(IsPolicies(&pairing_true_)));
335 StartWatching();
336 policy_watcher_->SetPolicies(&empty_);
337 policy_watcher_->SetPolicies(&pairing_false_);
338 policy_watcher_->SetPolicies(&pairing_true_);
339 StopWatching();
342 TEST_F(PolicyWatcherTest, GnubbyAuth) {
343 testing::InSequence sequence;
344 EXPECT_CALL(mock_policy_callback_,
345 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
346 EXPECT_CALL(mock_policy_callback_,
347 OnPolicyUpdatePtr(IsPolicies(&gnubby_auth_false_)));
348 EXPECT_CALL(mock_policy_callback_,
349 OnPolicyUpdatePtr(IsPolicies(&gnubby_auth_true_)));
351 StartWatching();
352 policy_watcher_->SetPolicies(&empty_);
353 policy_watcher_->SetPolicies(&gnubby_auth_false_);
354 policy_watcher_->SetPolicies(&gnubby_auth_true_);
355 StopWatching();
358 } // namespace policy_hack
359 } // namespace remoting