summaryrefslogtreecommitdiffstats
path: root/remoting/host/policy_hack/policy_watcher_unittest.cc
blob: d32371853b2dd23d649319c63bf078bbf484ca61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "remoting/host/dns_blackhole_checker.h"
#include "remoting/host/policy_hack/fake_policy_watcher.h"
#include "remoting/host/policy_hack/mock_policy_callback.h"
#include "remoting/host/policy_hack/policy_watcher.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {
namespace policy_hack {

class PolicyWatcherTest : public testing::Test {
 public:
  PolicyWatcherTest() {
  }

  virtual void SetUp() OVERRIDE {
    message_loop_proxy_ = base::MessageLoopProxy::current();
    policy_callback_ = base::Bind(&MockPolicyCallback::OnPolicyUpdate,
                                  base::Unretained(&mock_policy_callback_));
    policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_));
    nat_true_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
    nat_false_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
    nat_one_.SetInteger(PolicyWatcher::kNatPolicyName, 1);
    domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, "");
    domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, kHostDomain);
    SetDefaults(nat_true_others_default_);
    nat_true_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
    SetDefaults(nat_false_others_default_);
    nat_false_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
    SetDefaults(domain_empty_others_default_);
    domain_empty_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
                                           "");
    SetDefaults(domain_full_others_default_);
    domain_full_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
                                          kHostDomain);
    nat_true_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
    nat_true_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, "");
    nat_true_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
    nat_true_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
                                   kHostDomain);
    nat_false_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
    nat_false_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, "");
    nat_false_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
    nat_false_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
                                    kHostDomain);
    SetDefaults(nat_true_domain_empty_others_default_);
    nat_true_domain_empty_others_default_.SetBoolean(
        PolicyWatcher::kNatPolicyName, true);
    nat_true_domain_empty_others_default_.SetString(
        PolicyWatcher::kHostDomainPolicyName, "");
    unknown_policies_.SetString("UnknownPolicyOne", "");
    unknown_policies_.SetString("UnknownPolicyTwo", "");

    const char kOverrideNatTraversalToFalse[] =
      "{ \"RemoteAccessHostFirewallTraversal\": false }";
    nat_true_and_overridden_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
    nat_true_and_overridden_.SetString(
        PolicyWatcher::kHostDebugOverridePoliciesName,
        kOverrideNatTraversalToFalse);
#if !defined(NDEBUG)
    SetDefaults(nat_false_overridden_others_default_);
    nat_false_overridden_others_default_.SetBoolean(
        PolicyWatcher::kNatPolicyName, false);
    nat_false_overridden_others_default_.SetString(
        PolicyWatcher::kHostDebugOverridePoliciesName,
        kOverrideNatTraversalToFalse);
#endif
  }

 protected:
  void StartWatching() {
    policy_watcher_->StartWatching(policy_callback_);
    message_loop_.RunUntilIdle();
  }

  void StopWatching() {
    base::WaitableEvent stop_event(false, false);
    policy_watcher_->StopWatching(&stop_event);
    message_loop_.RunUntilIdle();
    EXPECT_EQ(true, stop_event.IsSignaled());
  }

  static const char* kHostDomain;
  MessageLoop message_loop_;
  scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
  MockPolicyCallback mock_policy_callback_;
  PolicyWatcher::PolicyCallback policy_callback_;
  scoped_ptr<FakePolicyWatcher> policy_watcher_;
  base::DictionaryValue empty_;
  base::DictionaryValue nat_true_;
  base::DictionaryValue nat_false_;
  base::DictionaryValue nat_one_;
  base::DictionaryValue domain_empty_;
  base::DictionaryValue domain_full_;
  base::DictionaryValue nat_true_others_default_;
  base::DictionaryValue nat_false_others_default_;
  base::DictionaryValue domain_empty_others_default_;
  base::DictionaryValue domain_full_others_default_;
  base::DictionaryValue nat_true_domain_empty_;
  base::DictionaryValue nat_true_domain_full_;
  base::DictionaryValue nat_false_domain_empty_;
  base::DictionaryValue nat_false_domain_full_;
  base::DictionaryValue nat_true_domain_empty_others_default_;
  base::DictionaryValue unknown_policies_;
  base::DictionaryValue nat_true_and_overridden_;
  base::DictionaryValue nat_false_overridden_others_default_;

 private:
  void SetDefaults(base::DictionaryValue& dict) {
    dict.SetBoolean(PolicyWatcher::kNatPolicyName, true);
    dict.SetBoolean(PolicyWatcher::kHostRequireTwoFactorPolicyName, false);
    dict.SetString(PolicyWatcher::kHostDomainPolicyName, "");
    dict.SetBoolean(PolicyWatcher::kHostMatchUsernamePolicyName, false);
    dict.SetString(PolicyWatcher::kHostTalkGadgetPrefixPolicyName,
                   kDefaultHostTalkGadgetPrefix);
    dict.SetBoolean(PolicyWatcher::kHostRequireCurtainPolicyName, false);
    dict.SetString(PolicyWatcher::kHostTokenUrlPolicyName, "");
    dict.SetString(PolicyWatcher::kHostTokenValidationUrlPolicyName, "");
#if !defined(NDEBUG)
    dict.SetString(PolicyWatcher::kHostDebugOverridePoliciesName, "");
#endif
  }
};

const char* PolicyWatcherTest::kHostDomain = "google.com";

MATCHER_P(IsPolicies, dict, "") {
  return arg->Equals(dict);
}

TEST_F(PolicyWatcherTest, None) {
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));

  StartWatching();
  policy_watcher_->SetPolicies(&empty_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, NatTrue) {
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));

  StartWatching();
  policy_watcher_->SetPolicies(&nat_true_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, NatFalse) {
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));

  StartWatching();
  policy_watcher_->SetPolicies(&nat_false_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, NatOne) {
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));

  StartWatching();
  policy_watcher_->SetPolicies(&nat_one_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, DomainEmpty) {
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_)));

  StartWatching();
  policy_watcher_->SetPolicies(&domain_empty_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, DomainFull) {
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_)));

  StartWatching();
  policy_watcher_->SetPolicies(&domain_full_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, NatNoneThenTrue) {
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));

  StartWatching();
  policy_watcher_->SetPolicies(&empty_);
  policy_watcher_->SetPolicies(&nat_true_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) {
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));

  StartWatching();
  policy_watcher_->SetPolicies(&empty_);
  policy_watcher_->SetPolicies(&nat_true_);
  policy_watcher_->SetPolicies(&nat_true_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) {
  testing::InSequence sequence;
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_false_)));

  StartWatching();
  policy_watcher_->SetPolicies(&empty_);
  policy_watcher_->SetPolicies(&nat_true_);
  policy_watcher_->SetPolicies(&nat_true_);
  policy_watcher_->SetPolicies(&nat_false_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, NatNoneThenFalse) {
  testing::InSequence sequence;
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_false_)));

  StartWatching();
  policy_watcher_->SetPolicies(&empty_);
  policy_watcher_->SetPolicies(&nat_false_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) {
  testing::InSequence sequence;
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_)));

  StartWatching();
  policy_watcher_->SetPolicies(&empty_);
  policy_watcher_->SetPolicies(&nat_false_);
  policy_watcher_->SetPolicies(&nat_true_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) {
  testing::InSequence sequence;
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(
                  &nat_true_domain_empty_others_default_)));
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&domain_full_)));
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&domain_empty_)));
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_)));

  StartWatching();
  policy_watcher_->SetPolicies(&nat_true_domain_empty_);
  policy_watcher_->SetPolicies(&nat_true_domain_full_);
  policy_watcher_->SetPolicies(&nat_false_domain_full_);
  policy_watcher_->SetPolicies(&nat_false_domain_empty_);
  policy_watcher_->SetPolicies(&nat_true_domain_full_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, FilterUnknownPolicies) {
  testing::InSequence sequence;
  EXPECT_CALL(mock_policy_callback_,
              OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));

  StartWatching();
  policy_watcher_->SetPolicies(&empty_);
  policy_watcher_->SetPolicies(&unknown_policies_);
  policy_watcher_->SetPolicies(&empty_);
  StopWatching();
}

TEST_F(PolicyWatcherTest, DebugOverrideNatPolicy) {
#if !defined(NDEBUG)
  EXPECT_CALL(mock_policy_callback_,
      OnPolicyUpdatePtr(IsPolicies(&nat_false_overridden_others_default_)));
#else
  EXPECT_CALL(mock_policy_callback_,
      OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
#endif

  StartWatching();
  policy_watcher_->SetPolicies(&nat_true_and_overridden_);
  StopWatching();
}

}  // namespace policy_hack
}  // namespace remoting