Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / remoting / host / policy_hack / policy_watcher_unittest.cc
blobbef254c8cfcc973f6cd20111c3f394d694642e3d
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 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 relay_true_.SetBoolean(PolicyWatcher::kRelayPolicyName, true);
79 relay_false_.SetBoolean(PolicyWatcher::kRelayPolicyName, false);
80 port_range_full_.SetString(PolicyWatcher::kUdpPortRangePolicyName,
81 kPortRange);
82 port_range_empty_.SetString(PolicyWatcher::kUdpPortRangePolicyName,
83 std::string());
85 #if !defined(NDEBUG)
86 SetDefaults(nat_false_overridden_others_default_);
87 nat_false_overridden_others_default_.SetBoolean(
88 PolicyWatcher::kNatPolicyName, false);
89 nat_false_overridden_others_default_.SetString(
90 PolicyWatcher::kHostDebugOverridePoliciesName,
91 kOverrideNatTraversalToFalse);
92 #endif
95 protected:
96 void StartWatching() {
97 policy_watcher_->StartWatching(policy_callback_);
98 base::RunLoop().RunUntilIdle();
101 void StopWatching() {
102 EXPECT_CALL(*this, PostPolicyWatcherShutdown()).Times(1);
103 policy_watcher_->StopWatching(base::Bind(
104 &PolicyWatcherTest::PostPolicyWatcherShutdown, base::Unretained(this)));
105 base::RunLoop().RunUntilIdle();
108 MOCK_METHOD0(PostPolicyWatcherShutdown, void());
110 static const char* kHostDomain;
111 static const char* kPortRange;
112 base::MessageLoop message_loop_;
113 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
114 MockPolicyCallback mock_policy_callback_;
115 PolicyWatcher::PolicyCallback policy_callback_;
116 scoped_ptr<FakePolicyWatcher> policy_watcher_;
117 base::DictionaryValue empty_;
118 base::DictionaryValue nat_true_;
119 base::DictionaryValue nat_false_;
120 base::DictionaryValue nat_one_;
121 base::DictionaryValue domain_empty_;
122 base::DictionaryValue domain_full_;
123 base::DictionaryValue nat_true_others_default_;
124 base::DictionaryValue nat_false_others_default_;
125 base::DictionaryValue domain_empty_others_default_;
126 base::DictionaryValue domain_full_others_default_;
127 base::DictionaryValue nat_true_domain_empty_;
128 base::DictionaryValue nat_true_domain_full_;
129 base::DictionaryValue nat_false_domain_empty_;
130 base::DictionaryValue nat_false_domain_full_;
131 base::DictionaryValue nat_true_domain_empty_others_default_;
132 base::DictionaryValue unknown_policies_;
133 base::DictionaryValue nat_true_and_overridden_;
134 base::DictionaryValue nat_false_overridden_others_default_;
135 base::DictionaryValue pairing_true_;
136 base::DictionaryValue pairing_false_;
137 base::DictionaryValue gnubby_auth_true_;
138 base::DictionaryValue gnubby_auth_false_;
139 base::DictionaryValue relay_true_;
140 base::DictionaryValue relay_false_;
141 base::DictionaryValue port_range_full_;
142 base::DictionaryValue port_range_empty_;
144 private:
145 void SetDefaults(base::DictionaryValue& dict) {
146 dict.SetBoolean(PolicyWatcher::kNatPolicyName, true);
147 dict.SetBoolean(PolicyWatcher::kRelayPolicyName, true);
148 dict.SetString(PolicyWatcher::kUdpPortRangePolicyName, "");
149 dict.SetBoolean(PolicyWatcher::kHostRequireTwoFactorPolicyName, false);
150 dict.SetString(PolicyWatcher::kHostDomainPolicyName, std::string());
151 dict.SetBoolean(PolicyWatcher::kHostMatchUsernamePolicyName, false);
152 dict.SetString(PolicyWatcher::kHostTalkGadgetPrefixPolicyName,
153 kDefaultHostTalkGadgetPrefix);
154 dict.SetBoolean(PolicyWatcher::kHostRequireCurtainPolicyName, false);
155 dict.SetString(PolicyWatcher::kHostTokenUrlPolicyName, std::string());
156 dict.SetString(PolicyWatcher::kHostTokenValidationUrlPolicyName,
157 std::string());
158 dict.SetString(PolicyWatcher::kHostTokenValidationCertIssuerPolicyName,
159 std::string());
160 dict.SetBoolean(PolicyWatcher::kHostAllowClientPairing, true);
161 dict.SetBoolean(PolicyWatcher::kHostAllowGnubbyAuthPolicyName, true);
162 #if !defined(NDEBUG)
163 dict.SetString(PolicyWatcher::kHostDebugOverridePoliciesName, "");
164 #endif
168 const char* PolicyWatcherTest::kHostDomain = "google.com";
169 const char* PolicyWatcherTest::kPortRange = "12400-12409";
171 MATCHER_P(IsPolicies, dict, "") {
172 return arg->Equals(dict);
175 TEST_F(PolicyWatcherTest, None) {
176 EXPECT_CALL(mock_policy_callback_,
177 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
179 StartWatching();
180 policy_watcher_->SetPolicies(&empty_);
181 StopWatching();
184 TEST_F(PolicyWatcherTest, NatTrue) {
185 EXPECT_CALL(mock_policy_callback_,
186 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
188 StartWatching();
189 policy_watcher_->SetPolicies(&nat_true_);
190 StopWatching();
193 TEST_F(PolicyWatcherTest, NatFalse) {
194 EXPECT_CALL(mock_policy_callback_,
195 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
197 StartWatching();
198 policy_watcher_->SetPolicies(&nat_false_);
199 StopWatching();
202 TEST_F(PolicyWatcherTest, NatOne) {
203 EXPECT_CALL(mock_policy_callback_,
204 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
206 StartWatching();
207 policy_watcher_->SetPolicies(&nat_one_);
208 StopWatching();
211 TEST_F(PolicyWatcherTest, DomainEmpty) {
212 EXPECT_CALL(mock_policy_callback_,
213 OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_)));
215 StartWatching();
216 policy_watcher_->SetPolicies(&domain_empty_);
217 StopWatching();
220 TEST_F(PolicyWatcherTest, DomainFull) {
221 EXPECT_CALL(mock_policy_callback_,
222 OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_)));
224 StartWatching();
225 policy_watcher_->SetPolicies(&domain_full_);
226 StopWatching();
229 TEST_F(PolicyWatcherTest, NatNoneThenTrue) {
230 EXPECT_CALL(mock_policy_callback_,
231 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
233 StartWatching();
234 policy_watcher_->SetPolicies(&empty_);
235 policy_watcher_->SetPolicies(&nat_true_);
236 StopWatching();
239 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) {
240 EXPECT_CALL(mock_policy_callback_,
241 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
243 StartWatching();
244 policy_watcher_->SetPolicies(&empty_);
245 policy_watcher_->SetPolicies(&nat_true_);
246 policy_watcher_->SetPolicies(&nat_true_);
247 StopWatching();
250 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) {
251 testing::InSequence sequence;
252 EXPECT_CALL(mock_policy_callback_,
253 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
254 EXPECT_CALL(mock_policy_callback_,
255 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
257 StartWatching();
258 policy_watcher_->SetPolicies(&empty_);
259 policy_watcher_->SetPolicies(&nat_true_);
260 policy_watcher_->SetPolicies(&nat_true_);
261 policy_watcher_->SetPolicies(&nat_false_);
262 StopWatching();
265 TEST_F(PolicyWatcherTest, NatNoneThenFalse) {
266 testing::InSequence sequence;
267 EXPECT_CALL(mock_policy_callback_,
268 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
269 EXPECT_CALL(mock_policy_callback_,
270 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
272 StartWatching();
273 policy_watcher_->SetPolicies(&empty_);
274 policy_watcher_->SetPolicies(&nat_false_);
275 StopWatching();
278 TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) {
279 testing::InSequence sequence;
280 EXPECT_CALL(mock_policy_callback_,
281 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
282 EXPECT_CALL(mock_policy_callback_,
283 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
284 EXPECT_CALL(mock_policy_callback_,
285 OnPolicyUpdatePtr(IsPolicies(&nat_true_)));
287 StartWatching();
288 policy_watcher_->SetPolicies(&empty_);
289 policy_watcher_->SetPolicies(&nat_false_);
290 policy_watcher_->SetPolicies(&nat_true_);
291 StopWatching();
294 TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) {
295 testing::InSequence sequence;
296 EXPECT_CALL(mock_policy_callback_,
297 OnPolicyUpdatePtr(IsPolicies(
298 &nat_true_domain_empty_others_default_)));
299 EXPECT_CALL(mock_policy_callback_,
300 OnPolicyUpdatePtr(IsPolicies(&domain_full_)));
301 EXPECT_CALL(mock_policy_callback_,
302 OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
303 EXPECT_CALL(mock_policy_callback_,
304 OnPolicyUpdatePtr(IsPolicies(&domain_empty_)));
305 EXPECT_CALL(mock_policy_callback_,
306 OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_)));
308 StartWatching();
309 policy_watcher_->SetPolicies(&nat_true_domain_empty_);
310 policy_watcher_->SetPolicies(&nat_true_domain_full_);
311 policy_watcher_->SetPolicies(&nat_false_domain_full_);
312 policy_watcher_->SetPolicies(&nat_false_domain_empty_);
313 policy_watcher_->SetPolicies(&nat_true_domain_full_);
314 StopWatching();
317 TEST_F(PolicyWatcherTest, FilterUnknownPolicies) {
318 testing::InSequence sequence;
319 EXPECT_CALL(mock_policy_callback_,
320 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
322 StartWatching();
323 policy_watcher_->SetPolicies(&empty_);
324 policy_watcher_->SetPolicies(&unknown_policies_);
325 policy_watcher_->SetPolicies(&empty_);
326 StopWatching();
329 TEST_F(PolicyWatcherTest, DebugOverrideNatPolicy) {
330 #if !defined(NDEBUG)
331 EXPECT_CALL(mock_policy_callback_,
332 OnPolicyUpdatePtr(IsPolicies(&nat_false_overridden_others_default_)));
333 #else
334 EXPECT_CALL(mock_policy_callback_,
335 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
336 #endif
338 StartWatching();
339 policy_watcher_->SetPolicies(&nat_true_and_overridden_);
340 StopWatching();
343 TEST_F(PolicyWatcherTest, PairingFalseThenTrue) {
344 testing::InSequence sequence;
345 EXPECT_CALL(mock_policy_callback_,
346 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
347 EXPECT_CALL(mock_policy_callback_,
348 OnPolicyUpdatePtr(IsPolicies(&pairing_false_)));
349 EXPECT_CALL(mock_policy_callback_,
350 OnPolicyUpdatePtr(IsPolicies(&pairing_true_)));
352 StartWatching();
353 policy_watcher_->SetPolicies(&empty_);
354 policy_watcher_->SetPolicies(&pairing_false_);
355 policy_watcher_->SetPolicies(&pairing_true_);
356 StopWatching();
359 TEST_F(PolicyWatcherTest, GnubbyAuth) {
360 testing::InSequence sequence;
361 EXPECT_CALL(mock_policy_callback_,
362 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
363 EXPECT_CALL(mock_policy_callback_,
364 OnPolicyUpdatePtr(IsPolicies(&gnubby_auth_false_)));
365 EXPECT_CALL(mock_policy_callback_,
366 OnPolicyUpdatePtr(IsPolicies(&gnubby_auth_true_)));
368 StartWatching();
369 policy_watcher_->SetPolicies(&empty_);
370 policy_watcher_->SetPolicies(&gnubby_auth_false_);
371 policy_watcher_->SetPolicies(&gnubby_auth_true_);
372 StopWatching();
375 TEST_F(PolicyWatcherTest, Relay) {
376 testing::InSequence sequence;
377 EXPECT_CALL(mock_policy_callback_,
378 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
379 EXPECT_CALL(mock_policy_callback_,
380 OnPolicyUpdatePtr(IsPolicies(&relay_false_)));
381 EXPECT_CALL(mock_policy_callback_,
382 OnPolicyUpdatePtr(IsPolicies(&relay_true_)));
384 StartWatching();
385 policy_watcher_->SetPolicies(&empty_);
386 policy_watcher_->SetPolicies(&relay_false_);
387 policy_watcher_->SetPolicies(&relay_true_);
388 StopWatching();
391 TEST_F(PolicyWatcherTest, UdpPortRange) {
392 testing::InSequence sequence;
393 EXPECT_CALL(mock_policy_callback_,
394 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
395 EXPECT_CALL(mock_policy_callback_,
396 OnPolicyUpdatePtr(IsPolicies(&port_range_full_)));
397 EXPECT_CALL(mock_policy_callback_,
398 OnPolicyUpdatePtr(IsPolicies(&port_range_empty_)));
400 StartWatching();
401 policy_watcher_->SetPolicies(&empty_);
402 policy_watcher_->SetPolicies(&port_range_full_);
403 policy_watcher_->SetPolicies(&port_range_empty_);
404 StopWatching();
407 } // namespace policy_hack
408 } // namespace remoting