policy_watcher_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
4
5#include "base/basictypes.h"
6#include "base/bind.h"
7#include "base/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"
15
16namespace remoting {
17namespace policy_hack {
18
19class PolicyWatcherTest : public testing::Test {
20 public:
21  PolicyWatcherTest() {
22  }
23
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, "");
33    domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, kHostDomain);
34    SetDefaults(nat_true_others_default_);
35    nat_true_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
36    SetDefaults(nat_false_others_default_);
37    nat_false_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
38    SetDefaults(domain_empty_others_default_);
39    domain_empty_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
40                                           "");
41    SetDefaults(domain_full_others_default_);
42    domain_full_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
43                                          kHostDomain);
44    nat_true_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
45    nat_true_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, "");
46    nat_true_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
47    nat_true_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
48                                   kHostDomain);
49    nat_false_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
50    nat_false_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, "");
51    nat_false_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
52    nat_false_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
53                                    kHostDomain);
54    SetDefaults(nat_true_domain_empty_others_default_);
55    nat_true_domain_empty_others_default_.SetBoolean(
56        PolicyWatcher::kNatPolicyName, true);
57    nat_true_domain_empty_others_default_.SetString(
58        PolicyWatcher::kHostDomainPolicyName, "");
59  }
60
61 protected:
62  void StartWatching() {
63    policy_watcher_->StartWatching(policy_callback_);
64    message_loop_.RunUntilIdle();
65  }
66
67  void StopWatching() {
68    base::WaitableEvent stop_event(false, false);
69    policy_watcher_->StopWatching(&stop_event);
70    message_loop_.RunUntilIdle();
71    EXPECT_EQ(true, stop_event.IsSignaled());
72  }
73
74  static const char* kHostDomain;
75  MessageLoop message_loop_;
76  scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
77  MockPolicyCallback mock_policy_callback_;
78  PolicyWatcher::PolicyCallback policy_callback_;
79  scoped_ptr<FakePolicyWatcher> policy_watcher_;
80  base::DictionaryValue empty_;
81  base::DictionaryValue nat_true_;
82  base::DictionaryValue nat_false_;
83  base::DictionaryValue nat_one_;
84  base::DictionaryValue domain_empty_;
85  base::DictionaryValue domain_full_;
86  base::DictionaryValue nat_true_others_default_;
87  base::DictionaryValue nat_false_others_default_;
88  base::DictionaryValue domain_empty_others_default_;
89  base::DictionaryValue domain_full_others_default_;
90  base::DictionaryValue nat_true_domain_empty_;
91  base::DictionaryValue nat_true_domain_full_;
92  base::DictionaryValue nat_false_domain_empty_;
93  base::DictionaryValue nat_false_domain_full_;
94  base::DictionaryValue nat_true_domain_empty_others_default_;
95
96 private:
97  void SetDefaults(base::DictionaryValue& dict) {
98    dict.SetBoolean(PolicyWatcher::kNatPolicyName, true);
99    dict.SetBoolean(PolicyWatcher::kHostRequireTwoFactorPolicyName, false);
100    dict.SetString(PolicyWatcher::kHostDomainPolicyName, "");
101    dict.SetBoolean(PolicyWatcher::kHostMatchUsernamePolicyName, false);
102    dict.SetString(PolicyWatcher::kHostTalkGadgetPrefixPolicyName,
103                   kDefaultHostTalkGadgetPrefix);
104    dict.SetBoolean(PolicyWatcher::kHostRequireCurtainPolicyName, false);
105  }
106};
107
108const char* PolicyWatcherTest::kHostDomain = "google.com";
109
110MATCHER_P(IsPolicies, dict, "") {
111  return arg->Equals(dict);
112}
113
114TEST_F(PolicyWatcherTest, None) {
115  EXPECT_CALL(mock_policy_callback_,
116              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
117
118  StartWatching();
119  policy_watcher_->SetPolicies(&empty_);
120  StopWatching();
121}
122
123TEST_F(PolicyWatcherTest, NatTrue) {
124  EXPECT_CALL(mock_policy_callback_,
125              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
126
127  StartWatching();
128  policy_watcher_->SetPolicies(&nat_true_);
129  StopWatching();
130}
131
132TEST_F(PolicyWatcherTest, NatFalse) {
133  EXPECT_CALL(mock_policy_callback_,
134              OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
135
136  StartWatching();
137  policy_watcher_->SetPolicies(&nat_false_);
138  StopWatching();
139}
140
141TEST_F(PolicyWatcherTest, NatOne) {
142  EXPECT_CALL(mock_policy_callback_,
143              OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
144
145  StartWatching();
146  policy_watcher_->SetPolicies(&nat_one_);
147  StopWatching();
148}
149
150TEST_F(PolicyWatcherTest, DomainEmpty) {
151  EXPECT_CALL(mock_policy_callback_,
152              OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_)));
153
154  StartWatching();
155  policy_watcher_->SetPolicies(&domain_empty_);
156  StopWatching();
157}
158
159TEST_F(PolicyWatcherTest, DomainFull) {
160  EXPECT_CALL(mock_policy_callback_,
161              OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_)));
162
163  StartWatching();
164  policy_watcher_->SetPolicies(&domain_full_);
165  StopWatching();
166}
167
168TEST_F(PolicyWatcherTest, NatNoneThenTrue) {
169  EXPECT_CALL(mock_policy_callback_,
170              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
171
172  StartWatching();
173  policy_watcher_->SetPolicies(&empty_);
174  policy_watcher_->SetPolicies(&nat_true_);
175  StopWatching();
176}
177
178TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) {
179  EXPECT_CALL(mock_policy_callback_,
180              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
181
182  StartWatching();
183  policy_watcher_->SetPolicies(&empty_);
184  policy_watcher_->SetPolicies(&nat_true_);
185  policy_watcher_->SetPolicies(&nat_true_);
186  StopWatching();
187}
188
189TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) {
190  testing::InSequence sequence;
191  EXPECT_CALL(mock_policy_callback_,
192              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
193  EXPECT_CALL(mock_policy_callback_,
194              OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
195
196  StartWatching();
197  policy_watcher_->SetPolicies(&empty_);
198  policy_watcher_->SetPolicies(&nat_true_);
199  policy_watcher_->SetPolicies(&nat_true_);
200  policy_watcher_->SetPolicies(&nat_false_);
201  StopWatching();
202}
203
204TEST_F(PolicyWatcherTest, NatNoneThenFalse) {
205  testing::InSequence sequence;
206  EXPECT_CALL(mock_policy_callback_,
207              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
208  EXPECT_CALL(mock_policy_callback_,
209              OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
210
211  StartWatching();
212  policy_watcher_->SetPolicies(&empty_);
213  policy_watcher_->SetPolicies(&nat_false_);
214  StopWatching();
215}
216
217TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) {
218  testing::InSequence sequence;
219  EXPECT_CALL(mock_policy_callback_,
220              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
221  EXPECT_CALL(mock_policy_callback_,
222              OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
223  EXPECT_CALL(mock_policy_callback_,
224              OnPolicyUpdatePtr(IsPolicies(&nat_true_)));
225
226  StartWatching();
227  policy_watcher_->SetPolicies(&empty_);
228  policy_watcher_->SetPolicies(&nat_false_);
229  policy_watcher_->SetPolicies(&nat_true_);
230  StopWatching();
231}
232
233TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) {
234  testing::InSequence sequence;
235  EXPECT_CALL(mock_policy_callback_,
236              OnPolicyUpdatePtr(IsPolicies(
237                  &nat_true_domain_empty_others_default_)));
238  EXPECT_CALL(mock_policy_callback_,
239              OnPolicyUpdatePtr(IsPolicies(&domain_full_)));
240  EXPECT_CALL(mock_policy_callback_,
241              OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
242  EXPECT_CALL(mock_policy_callback_,
243              OnPolicyUpdatePtr(IsPolicies(&domain_empty_)));
244  EXPECT_CALL(mock_policy_callback_,
245              OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_)));
246
247  StartWatching();
248  policy_watcher_->SetPolicies(&nat_true_domain_empty_);
249  policy_watcher_->SetPolicies(&nat_true_domain_full_);
250  policy_watcher_->SetPolicies(&nat_false_domain_full_);
251  policy_watcher_->SetPolicies(&nat_false_domain_empty_);
252  policy_watcher_->SetPolicies(&nat_true_domain_full_);
253  StopWatching();
254}
255
256}  // namespace policy_hack
257}  // namespace remoting
258