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.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "remoting/host/constants.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
, "");
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
,
41 SetDefaults(domain_full_others_default_
);
42 domain_full_others_default_
.SetString(PolicyWatcher::kHostDomainPolicyName
,
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
,
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
,
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
, "");
62 void StartWatching() {
63 policy_watcher_
->StartWatching(policy_callback_
);
64 message_loop_
.RunUntilIdle();
68 base::WaitableEvent
stop_event(false, false);
69 policy_watcher_
->StopWatching(&stop_event
);
70 message_loop_
.RunUntilIdle();
71 EXPECT_EQ(true, stop_event
.IsSignaled());
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_
;
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
.SetString(PolicyWatcher::kHostTalkGadgetPrefixPolicyName
,
102 kDefaultTalkGadgetPrefix
);
103 dict
.SetBoolean(PolicyWatcher::kHostRequireCurtainPolicyName
, false);
107 const char* PolicyWatcherTest::kHostDomain
= "google.com";
109 MATCHER_P(IsPolicies
, dict
, "") {
110 return arg
->Equals(dict
);
113 TEST_F(PolicyWatcherTest
, None
) {
114 EXPECT_CALL(mock_policy_callback_
,
115 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
118 policy_watcher_
->SetPolicies(&empty_
);
122 TEST_F(PolicyWatcherTest
, NatTrue
) {
123 EXPECT_CALL(mock_policy_callback_
,
124 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
127 policy_watcher_
->SetPolicies(&nat_true_
);
131 TEST_F(PolicyWatcherTest
, NatFalse
) {
132 EXPECT_CALL(mock_policy_callback_
,
133 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_
)));
136 policy_watcher_
->SetPolicies(&nat_false_
);
140 TEST_F(PolicyWatcherTest
, NatOne
) {
141 EXPECT_CALL(mock_policy_callback_
,
142 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_
)));
145 policy_watcher_
->SetPolicies(&nat_one_
);
149 TEST_F(PolicyWatcherTest
, DomainEmpty
) {
150 EXPECT_CALL(mock_policy_callback_
,
151 OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_
)));
154 policy_watcher_
->SetPolicies(&domain_empty_
);
158 TEST_F(PolicyWatcherTest
, DomainFull
) {
159 EXPECT_CALL(mock_policy_callback_
,
160 OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_
)));
163 policy_watcher_
->SetPolicies(&domain_full_
);
167 TEST_F(PolicyWatcherTest
, NatNoneThenTrue
) {
168 EXPECT_CALL(mock_policy_callback_
,
169 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
172 policy_watcher_
->SetPolicies(&empty_
);
173 policy_watcher_
->SetPolicies(&nat_true_
);
177 TEST_F(PolicyWatcherTest
, NatNoneThenTrueThenTrue
) {
178 EXPECT_CALL(mock_policy_callback_
,
179 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
182 policy_watcher_
->SetPolicies(&empty_
);
183 policy_watcher_
->SetPolicies(&nat_true_
);
184 policy_watcher_
->SetPolicies(&nat_true_
);
188 TEST_F(PolicyWatcherTest
, NatNoneThenTrueThenTrueThenFalse
) {
189 testing::InSequence sequence
;
190 EXPECT_CALL(mock_policy_callback_
,
191 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
192 EXPECT_CALL(mock_policy_callback_
,
193 OnPolicyUpdatePtr(IsPolicies(&nat_false_
)));
196 policy_watcher_
->SetPolicies(&empty_
);
197 policy_watcher_
->SetPolicies(&nat_true_
);
198 policy_watcher_
->SetPolicies(&nat_true_
);
199 policy_watcher_
->SetPolicies(&nat_false_
);
203 TEST_F(PolicyWatcherTest
, NatNoneThenFalse
) {
204 testing::InSequence sequence
;
205 EXPECT_CALL(mock_policy_callback_
,
206 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
207 EXPECT_CALL(mock_policy_callback_
,
208 OnPolicyUpdatePtr(IsPolicies(&nat_false_
)));
211 policy_watcher_
->SetPolicies(&empty_
);
212 policy_watcher_
->SetPolicies(&nat_false_
);
216 TEST_F(PolicyWatcherTest
, NatNoneThenFalseThenTrue
) {
217 testing::InSequence sequence
;
218 EXPECT_CALL(mock_policy_callback_
,
219 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_
)));
220 EXPECT_CALL(mock_policy_callback_
,
221 OnPolicyUpdatePtr(IsPolicies(&nat_false_
)));
222 EXPECT_CALL(mock_policy_callback_
,
223 OnPolicyUpdatePtr(IsPolicies(&nat_true_
)));
226 policy_watcher_
->SetPolicies(&empty_
);
227 policy_watcher_
->SetPolicies(&nat_false_
);
228 policy_watcher_
->SetPolicies(&nat_true_
);
232 TEST_F(PolicyWatcherTest
, ChangeOneRepeatedlyThenTwo
) {
233 testing::InSequence sequence
;
234 EXPECT_CALL(mock_policy_callback_
,
235 OnPolicyUpdatePtr(IsPolicies(
236 &nat_true_domain_empty_others_default_
)));
237 EXPECT_CALL(mock_policy_callback_
,
238 OnPolicyUpdatePtr(IsPolicies(&domain_full_
)));
239 EXPECT_CALL(mock_policy_callback_
,
240 OnPolicyUpdatePtr(IsPolicies(&nat_false_
)));
241 EXPECT_CALL(mock_policy_callback_
,
242 OnPolicyUpdatePtr(IsPolicies(&domain_empty_
)));
243 EXPECT_CALL(mock_policy_callback_
,
244 OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_
)));
247 policy_watcher_
->SetPolicies(&nat_true_domain_empty_
);
248 policy_watcher_
->SetPolicies(&nat_true_domain_full_
);
249 policy_watcher_
->SetPolicies(&nat_false_domain_full_
);
250 policy_watcher_
->SetPolicies(&nat_false_domain_empty_
);
251 policy_watcher_
->SetPolicies(&nat_true_domain_full_
);
255 } // namespace policy_hack
256 } // namespace remoting