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"
7 #include "base/message_loop/message_loop.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "remoting/host/dns_blackhole_checker.h"
10 #include "remoting/host/policy_hack/fake_policy_watcher.h"
11 #include "remoting/host/policy_hack/mock_policy_callback.h"
12 #include "remoting/host/policy_hack/policy_watcher.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 namespace policy_hack {
19 class PolicyWatcherTest : public testing::Test {
24 virtual void SetUp() OVERRIDE {
25 message_loop_proxy_ = base::MessageLoopProxy::current();
26 policy_callback_ = base::Bind(&MockPolicyCallback::OnPolicyUpdate,
27 base::Unretained(&mock_policy_callback_));
28 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_));
29 nat_true_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
30 nat_false_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
31 nat_one_.SetInteger(PolicyWatcher::kNatPolicyName, 1);
32 domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName,
34 domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, kHostDomain);
35 SetDefaults(nat_true_others_default_);
36 nat_true_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
37 SetDefaults(nat_false_others_default_);
38 nat_false_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
39 SetDefaults(domain_empty_others_default_);
40 domain_empty_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
42 SetDefaults(domain_full_others_default_);
43 domain_full_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
45 nat_true_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
46 nat_true_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName,
48 nat_true_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
49 nat_true_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
51 nat_false_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
52 nat_false_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName,
54 nat_false_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
55 nat_false_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
57 SetDefaults(nat_true_domain_empty_others_default_);
58 nat_true_domain_empty_others_default_.SetBoolean(
59 PolicyWatcher::kNatPolicyName, true);
60 nat_true_domain_empty_others_default_.SetString(
61 PolicyWatcher::kHostDomainPolicyName, std::string());
62 unknown_policies_.SetString("UnknownPolicyOne", std::string());
63 unknown_policies_.SetString("UnknownPolicyTwo", std::string());
65 const char kOverrideNatTraversalToFalse[] =
66 "{ \"RemoteAccessHostFirewallTraversal\": false }";
67 nat_true_and_overridden_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
68 nat_true_and_overridden_.SetString(
69 PolicyWatcher::kHostDebugOverridePoliciesName,
70 kOverrideNatTraversalToFalse);
71 pairing_true_.SetBoolean(PolicyWatcher::kHostAllowClientPairing, true);
72 pairing_false_.SetBoolean(PolicyWatcher::kHostAllowClientPairing, false);
74 SetDefaults(nat_false_overridden_others_default_);
75 nat_false_overridden_others_default_.SetBoolean(
76 PolicyWatcher::kNatPolicyName, false);
77 nat_false_overridden_others_default_.SetString(
78 PolicyWatcher::kHostDebugOverridePoliciesName,
79 kOverrideNatTraversalToFalse);
84 void StartWatching() {
85 policy_watcher_->StartWatching(policy_callback_);
86 message_loop_.RunUntilIdle();
90 base::WaitableEvent stop_event(false, false);
91 policy_watcher_->StopWatching(&stop_event);
92 message_loop_.RunUntilIdle();
93 EXPECT_EQ(true, stop_event.IsSignaled());
96 static const char* kHostDomain;
97 base::MessageLoop message_loop_;
98 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
99 MockPolicyCallback mock_policy_callback_;
100 PolicyWatcher::PolicyCallback policy_callback_;
101 scoped_ptr<FakePolicyWatcher> policy_watcher_;
102 base::DictionaryValue empty_;
103 base::DictionaryValue nat_true_;
104 base::DictionaryValue nat_false_;
105 base::DictionaryValue nat_one_;
106 base::DictionaryValue domain_empty_;
107 base::DictionaryValue domain_full_;
108 base::DictionaryValue nat_true_others_default_;
109 base::DictionaryValue nat_false_others_default_;
110 base::DictionaryValue domain_empty_others_default_;
111 base::DictionaryValue domain_full_others_default_;
112 base::DictionaryValue nat_true_domain_empty_;
113 base::DictionaryValue nat_true_domain_full_;
114 base::DictionaryValue nat_false_domain_empty_;
115 base::DictionaryValue nat_false_domain_full_;
116 base::DictionaryValue nat_true_domain_empty_others_default_;
117 base::DictionaryValue unknown_policies_;
118 base::DictionaryValue nat_true_and_overridden_;
119 base::DictionaryValue nat_false_overridden_others_default_;
120 base::DictionaryValue pairing_true_;
121 base::DictionaryValue pairing_false_;
124 void SetDefaults(base::DictionaryValue& dict) {
125 dict.SetBoolean(PolicyWatcher::kNatPolicyName, true);
126 dict.SetBoolean(PolicyWatcher::kHostRequireTwoFactorPolicyName, false);
127 dict.SetString(PolicyWatcher::kHostDomainPolicyName, std::string());
128 dict.SetBoolean(PolicyWatcher::kHostMatchUsernamePolicyName, false);
129 dict.SetString(PolicyWatcher::kHostTalkGadgetPrefixPolicyName,
130 kDefaultHostTalkGadgetPrefix);
131 dict.SetBoolean(PolicyWatcher::kHostRequireCurtainPolicyName, false);
132 dict.SetString(PolicyWatcher::kHostTokenUrlPolicyName, std::string());
133 dict.SetString(PolicyWatcher::kHostTokenValidationUrlPolicyName,
135 dict.SetBoolean(PolicyWatcher::kHostAllowClientPairing, true);
137 dict.SetString(PolicyWatcher::kHostDebugOverridePoliciesName, "");
142 const char* PolicyWatcherTest::kHostDomain = "google.com";
144 MATCHER_P(IsPolicies, dict, "") {
145 return arg->Equals(dict);
148 TEST_F(PolicyWatcherTest, None) {
149 EXPECT_CALL(mock_policy_callback_,
150 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
153 policy_watcher_->SetPolicies(&empty_);
157 TEST_F(PolicyWatcherTest, NatTrue) {
158 EXPECT_CALL(mock_policy_callback_,
159 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
162 policy_watcher_->SetPolicies(&nat_true_);
166 TEST_F(PolicyWatcherTest, NatFalse) {
167 EXPECT_CALL(mock_policy_callback_,
168 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
171 policy_watcher_->SetPolicies(&nat_false_);
175 TEST_F(PolicyWatcherTest, NatOne) {
176 EXPECT_CALL(mock_policy_callback_,
177 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
180 policy_watcher_->SetPolicies(&nat_one_);
184 TEST_F(PolicyWatcherTest, DomainEmpty) {
185 EXPECT_CALL(mock_policy_callback_,
186 OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_)));
189 policy_watcher_->SetPolicies(&domain_empty_);
193 TEST_F(PolicyWatcherTest, DomainFull) {
194 EXPECT_CALL(mock_policy_callback_,
195 OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_)));
198 policy_watcher_->SetPolicies(&domain_full_);
202 TEST_F(PolicyWatcherTest, NatNoneThenTrue) {
203 EXPECT_CALL(mock_policy_callback_,
204 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
207 policy_watcher_->SetPolicies(&empty_);
208 policy_watcher_->SetPolicies(&nat_true_);
212 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) {
213 EXPECT_CALL(mock_policy_callback_,
214 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
217 policy_watcher_->SetPolicies(&empty_);
218 policy_watcher_->SetPolicies(&nat_true_);
219 policy_watcher_->SetPolicies(&nat_true_);
223 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) {
224 testing::InSequence sequence;
225 EXPECT_CALL(mock_policy_callback_,
226 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
227 EXPECT_CALL(mock_policy_callback_,
228 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
231 policy_watcher_->SetPolicies(&empty_);
232 policy_watcher_->SetPolicies(&nat_true_);
233 policy_watcher_->SetPolicies(&nat_true_);
234 policy_watcher_->SetPolicies(&nat_false_);
238 TEST_F(PolicyWatcherTest, NatNoneThenFalse) {
239 testing::InSequence sequence;
240 EXPECT_CALL(mock_policy_callback_,
241 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
242 EXPECT_CALL(mock_policy_callback_,
243 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
246 policy_watcher_->SetPolicies(&empty_);
247 policy_watcher_->SetPolicies(&nat_false_);
251 TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) {
252 testing::InSequence sequence;
253 EXPECT_CALL(mock_policy_callback_,
254 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
255 EXPECT_CALL(mock_policy_callback_,
256 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
257 EXPECT_CALL(mock_policy_callback_,
258 OnPolicyUpdatePtr(IsPolicies(&nat_true_)));
261 policy_watcher_->SetPolicies(&empty_);
262 policy_watcher_->SetPolicies(&nat_false_);
263 policy_watcher_->SetPolicies(&nat_true_);
267 TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) {
268 testing::InSequence sequence;
269 EXPECT_CALL(mock_policy_callback_,
270 OnPolicyUpdatePtr(IsPolicies(
271 &nat_true_domain_empty_others_default_)));
272 EXPECT_CALL(mock_policy_callback_,
273 OnPolicyUpdatePtr(IsPolicies(&domain_full_)));
274 EXPECT_CALL(mock_policy_callback_,
275 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
276 EXPECT_CALL(mock_policy_callback_,
277 OnPolicyUpdatePtr(IsPolicies(&domain_empty_)));
278 EXPECT_CALL(mock_policy_callback_,
279 OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_)));
282 policy_watcher_->SetPolicies(&nat_true_domain_empty_);
283 policy_watcher_->SetPolicies(&nat_true_domain_full_);
284 policy_watcher_->SetPolicies(&nat_false_domain_full_);
285 policy_watcher_->SetPolicies(&nat_false_domain_empty_);
286 policy_watcher_->SetPolicies(&nat_true_domain_full_);
290 TEST_F(PolicyWatcherTest, FilterUnknownPolicies) {
291 testing::InSequence sequence;
292 EXPECT_CALL(mock_policy_callback_,
293 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
296 policy_watcher_->SetPolicies(&empty_);
297 policy_watcher_->SetPolicies(&unknown_policies_);
298 policy_watcher_->SetPolicies(&empty_);
302 TEST_F(PolicyWatcherTest, DebugOverrideNatPolicy) {
304 EXPECT_CALL(mock_policy_callback_,
305 OnPolicyUpdatePtr(IsPolicies(&nat_false_overridden_others_default_)));
307 EXPECT_CALL(mock_policy_callback_,
308 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
312 policy_watcher_->SetPolicies(&nat_true_and_overridden_);
316 TEST_F(PolicyWatcherTest, PairingFalseThenTrue) {
317 testing::InSequence sequence;
318 EXPECT_CALL(mock_policy_callback_,
319 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
320 EXPECT_CALL(mock_policy_callback_,
321 OnPolicyUpdatePtr(IsPolicies(&pairing_false_)));
322 EXPECT_CALL(mock_policy_callback_,
323 OnPolicyUpdatePtr(IsPolicies(&pairing_true_)));
326 policy_watcher_->SetPolicies(&empty_);
327 policy_watcher_->SetPolicies(&pairing_false_);
328 policy_watcher_->SetPolicies(&pairing_true_);
332 } // namespace policy_hack
333 } // namespace remoting