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/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"
18 namespace policy_hack
{
20 class PolicyWatcherTest
: public testing::Test
{
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
,
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
,
43 SetDefaults(domain_full_others_default_
);
44 domain_full_others_default_
.SetString(PolicyWatcher::kHostDomainPolicyName
,
46 nat_true_domain_empty_
.SetBoolean(PolicyWatcher::kNatPolicyName
, true);
47 nat_true_domain_empty_
.SetString(PolicyWatcher::kHostDomainPolicyName
,
49 nat_true_domain_full_
.SetBoolean(PolicyWatcher::kNatPolicyName
, true);
50 nat_true_domain_full_
.SetString(PolicyWatcher::kHostDomainPolicyName
,
52 nat_false_domain_empty_
.SetBoolean(PolicyWatcher::kNatPolicyName
, false);
53 nat_false_domain_empty_
.SetString(PolicyWatcher::kHostDomainPolicyName
,
55 nat_false_domain_full_
.SetBoolean(PolicyWatcher::kNatPolicyName
, false);
56 nat_false_domain_full_
.SetString(PolicyWatcher::kHostDomainPolicyName
,
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
,
76 gnubby_auth_false_
.SetBoolean(PolicyWatcher::kHostAllowGnubbyAuthPolicyName
,
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
);
89 void StartWatching() {
90 policy_watcher_
->StartWatching(policy_callback_
);
91 base::RunLoop().RunUntilIdle();
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_
;
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
,
142 dict
.SetString(PolicyWatcher::kHostTokenValidationCertIssuerPolicyName
,
144 dict
.SetBoolean(PolicyWatcher::kHostAllowClientPairing
, true);
145 dict
.SetBoolean(PolicyWatcher::kHostAllowGnubbyAuthPolicyName
, true);
147 dict
.SetString(PolicyWatcher::kHostDebugOverridePoliciesName
, "");
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_
)));
163 policy_watcher_
->SetPolicies(&empty_
);
167 TEST_F(PolicyWatcherTest
, NatTrue
) {
168 EXPECT_CALL(mock_policy_callback_
,
169 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
172 policy_watcher_
->SetPolicies(&nat_true_
);
176 TEST_F(PolicyWatcherTest
, NatFalse
) {
177 EXPECT_CALL(mock_policy_callback_
,
178 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_
)));
181 policy_watcher_
->SetPolicies(&nat_false_
);
185 TEST_F(PolicyWatcherTest
, NatOne
) {
186 EXPECT_CALL(mock_policy_callback_
,
187 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_
)));
190 policy_watcher_
->SetPolicies(&nat_one_
);
194 TEST_F(PolicyWatcherTest
, DomainEmpty
) {
195 EXPECT_CALL(mock_policy_callback_
,
196 OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_
)));
199 policy_watcher_
->SetPolicies(&domain_empty_
);
203 TEST_F(PolicyWatcherTest
, DomainFull
) {
204 EXPECT_CALL(mock_policy_callback_
,
205 OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_
)));
208 policy_watcher_
->SetPolicies(&domain_full_
);
212 TEST_F(PolicyWatcherTest
, NatNoneThenTrue
) {
213 EXPECT_CALL(mock_policy_callback_
,
214 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
217 policy_watcher_
->SetPolicies(&empty_
);
218 policy_watcher_
->SetPolicies(&nat_true_
);
222 TEST_F(PolicyWatcherTest
, NatNoneThenTrueThenTrue
) {
223 EXPECT_CALL(mock_policy_callback_
,
224 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
227 policy_watcher_
->SetPolicies(&empty_
);
228 policy_watcher_
->SetPolicies(&nat_true_
);
229 policy_watcher_
->SetPolicies(&nat_true_
);
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_
)));
241 policy_watcher_
->SetPolicies(&empty_
);
242 policy_watcher_
->SetPolicies(&nat_true_
);
243 policy_watcher_
->SetPolicies(&nat_true_
);
244 policy_watcher_
->SetPolicies(&nat_false_
);
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_
)));
256 policy_watcher_
->SetPolicies(&empty_
);
257 policy_watcher_
->SetPolicies(&nat_false_
);
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_
)));
271 policy_watcher_
->SetPolicies(&empty_
);
272 policy_watcher_
->SetPolicies(&nat_false_
);
273 policy_watcher_
->SetPolicies(&nat_true_
);
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_
)));
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_
);
300 TEST_F(PolicyWatcherTest
, FilterUnknownPolicies
) {
301 testing::InSequence sequence
;
302 EXPECT_CALL(mock_policy_callback_
,
303 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
306 policy_watcher_
->SetPolicies(&empty_
);
307 policy_watcher_
->SetPolicies(&unknown_policies_
);
308 policy_watcher_
->SetPolicies(&empty_
);
312 TEST_F(PolicyWatcherTest
, DebugOverrideNatPolicy
) {
314 EXPECT_CALL(mock_policy_callback_
,
315 OnPolicyUpdatePtr(IsPolicies(&nat_false_overridden_others_default_
)));
317 EXPECT_CALL(mock_policy_callback_
,
318 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
322 policy_watcher_
->SetPolicies(&nat_true_and_overridden_
);
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_
)));
336 policy_watcher_
->SetPolicies(&empty_
);
337 policy_watcher_
->SetPolicies(&pairing_false_
);
338 policy_watcher_
->SetPolicies(&pairing_true_
);
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_
)));
352 policy_watcher_
->SetPolicies(&empty_
);
353 policy_watcher_
->SetPolicies(&gnubby_auth_false_
);
354 policy_watcher_
->SetPolicies(&gnubby_auth_true_
);
358 } // namespace policy_hack
359 } // namespace remoting