OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "remoting/host/policy_hack/fake_policy_watcher.h" | 9 #include "remoting/host/policy_hack/fake_policy_watcher.h" |
10 #include "remoting/host/policy_hack/mock_policy_callback.h" | 10 #include "remoting/host/policy_hack/mock_policy_callback.h" |
11 #include "remoting/host/policy_hack/policy_watcher.h" | 11 #include "remoting/host/policy_hack/policy_watcher.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace remoting { | 15 namespace remoting { |
16 namespace policy_hack { | 16 namespace policy_hack { |
17 | 17 |
18 class PolicyWatcherTest : public testing::Test { | 18 class PolicyWatcherTest : public testing::Test { |
19 public: | 19 public: |
20 PolicyWatcherTest() { | 20 PolicyWatcherTest() { |
21 } | 21 } |
22 | 22 |
23 virtual void SetUp() OVERRIDE { | 23 virtual void SetUp() OVERRIDE { |
24 message_loop_proxy_ = base::MessageLoopProxy::current(); | 24 message_loop_proxy_ = base::MessageLoopProxy::current(); |
25 policy_callback_ = base::Bind(&MockPolicyCallback::OnPolicyUpdate, | 25 policy_callback_ = base::Bind(&MockPolicyCallback::OnPolicyUpdate, |
26 base::Unretained(&mock_policy_callback_)); | 26 base::Unretained(&mock_policy_callback_)); |
27 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); | 27 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); |
28 nat_true.SetBoolean(PolicyWatcher::kNatPolicyName, true); | 28 nat_true_.SetBoolean(PolicyWatcher::kNatPolicyName, true); |
29 nat_false.SetBoolean(PolicyWatcher::kNatPolicyName, false); | 29 nat_false_.SetBoolean(PolicyWatcher::kNatPolicyName, false); |
30 nat_one.SetInteger(PolicyWatcher::kNatPolicyName, 1); | 30 nat_one_.SetInteger(PolicyWatcher::kNatPolicyName, 1); |
| 31 domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, ""); |
| 32 domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, kHostDomain); |
| 33 SetDefaults(nat_true_others_default_); |
| 34 nat_true_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, true); |
| 35 SetDefaults(nat_false_others_default_); |
| 36 nat_false_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, false); |
| 37 SetDefaults(domain_empty_others_default_); |
| 38 domain_empty_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName, |
| 39 ""); |
| 40 SetDefaults(domain_full_others_default_); |
| 41 domain_full_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName, |
| 42 kHostDomain); |
| 43 nat_true_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, true); |
| 44 nat_true_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, ""); |
| 45 nat_true_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, true); |
| 46 nat_true_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, |
| 47 kHostDomain); |
| 48 nat_false_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, false); |
| 49 nat_false_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, ""); |
| 50 nat_false_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, false); |
| 51 nat_false_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, |
| 52 kHostDomain); |
31 } | 53 } |
32 | 54 |
33 protected: | 55 protected: |
34 void StartWatching() { | 56 void StartWatching() { |
35 policy_watcher_->StartWatching(policy_callback_); | 57 policy_watcher_->StartWatching(policy_callback_); |
36 message_loop_.RunUntilIdle(); | 58 message_loop_.RunUntilIdle(); |
37 } | 59 } |
38 | 60 |
39 void StopWatching() { | 61 void StopWatching() { |
40 base::WaitableEvent stop_event(false, false); | 62 base::WaitableEvent stop_event(false, false); |
41 policy_watcher_->StopWatching(&stop_event); | 63 policy_watcher_->StopWatching(&stop_event); |
42 message_loop_.RunUntilIdle(); | 64 message_loop_.RunUntilIdle(); |
43 EXPECT_EQ(true, stop_event.IsSignaled()); | 65 EXPECT_EQ(true, stop_event.IsSignaled()); |
44 } | 66 } |
45 | 67 |
| 68 static const char* kHostDomain; |
46 MessageLoop message_loop_; | 69 MessageLoop message_loop_; |
47 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 70 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
48 MockPolicyCallback mock_policy_callback_; | 71 MockPolicyCallback mock_policy_callback_; |
49 PolicyWatcher::PolicyCallback policy_callback_; | 72 PolicyWatcher::PolicyCallback policy_callback_; |
50 scoped_ptr<FakePolicyWatcher> policy_watcher_; | 73 scoped_ptr<FakePolicyWatcher> policy_watcher_; |
51 base::DictionaryValue empty; | 74 base::DictionaryValue empty_; |
52 base::DictionaryValue nat_true; | 75 base::DictionaryValue nat_true_; |
53 base::DictionaryValue nat_false; | 76 base::DictionaryValue nat_false_; |
54 base::DictionaryValue nat_one; | 77 base::DictionaryValue nat_one_; |
| 78 base::DictionaryValue domain_empty_; |
| 79 base::DictionaryValue domain_full_; |
| 80 base::DictionaryValue nat_true_others_default_; |
| 81 base::DictionaryValue nat_false_others_default_; |
| 82 base::DictionaryValue domain_empty_others_default_; |
| 83 base::DictionaryValue domain_full_others_default_; |
| 84 base::DictionaryValue nat_true_domain_empty_; |
| 85 base::DictionaryValue nat_true_domain_full_; |
| 86 base::DictionaryValue nat_false_domain_empty_; |
| 87 base::DictionaryValue nat_false_domain_full_; |
| 88 |
| 89 private: |
| 90 void SetDefaults(base::DictionaryValue& dict) { |
| 91 dict.SetBoolean(PolicyWatcher::kNatPolicyName, true); |
| 92 dict.SetString(PolicyWatcher::kHostDomainPolicyName, ""); |
| 93 } |
55 }; | 94 }; |
56 | 95 |
| 96 const char* PolicyWatcherTest::kHostDomain = "google.com"; |
| 97 |
57 MATCHER_P(IsPolicies, dict, "") { | 98 MATCHER_P(IsPolicies, dict, "") { |
58 return arg->Equals(dict); | 99 return arg->Equals(dict); |
59 } | 100 } |
60 | 101 |
61 TEST_F(PolicyWatcherTest, None) { | 102 TEST_F(PolicyWatcherTest, None) { |
62 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); | 103 EXPECT_CALL(mock_policy_callback_, |
| 104 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
63 | 105 |
64 StartWatching(); | 106 StartWatching(); |
65 policy_watcher_->SetPolicies(&empty); | 107 policy_watcher_->SetPolicies(&empty_); |
66 StopWatching(); | 108 StopWatching(); |
67 } | 109 } |
68 | 110 |
69 TEST_F(PolicyWatcherTest, NatTrue) { | 111 TEST_F(PolicyWatcherTest, NatTrue) { |
70 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); | 112 EXPECT_CALL(mock_policy_callback_, |
| 113 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
71 | 114 |
72 StartWatching(); | 115 StartWatching(); |
73 policy_watcher_->SetPolicies(&nat_true); | 116 policy_watcher_->SetPolicies(&nat_true_); |
74 StopWatching(); | 117 StopWatching(); |
75 } | 118 } |
76 | 119 |
77 TEST_F(PolicyWatcherTest, NatFalse) { | 120 TEST_F(PolicyWatcherTest, NatFalse) { |
78 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); | 121 EXPECT_CALL(mock_policy_callback_, |
| 122 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_))); |
79 | 123 |
80 StartWatching(); | 124 StartWatching(); |
81 policy_watcher_->SetPolicies(&nat_false); | 125 policy_watcher_->SetPolicies(&nat_false_); |
82 StopWatching(); | 126 StopWatching(); |
83 } | 127 } |
84 | 128 |
85 TEST_F(PolicyWatcherTest, NatOne) { | 129 TEST_F(PolicyWatcherTest, NatOne) { |
86 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); | 130 EXPECT_CALL(mock_policy_callback_, |
| 131 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_))); |
87 | 132 |
88 StartWatching(); | 133 StartWatching(); |
89 policy_watcher_->SetPolicies(&nat_one); | 134 policy_watcher_->SetPolicies(&nat_one_); |
| 135 StopWatching(); |
| 136 } |
| 137 |
| 138 TEST_F(PolicyWatcherTest, DomainEmpty) { |
| 139 EXPECT_CALL(mock_policy_callback_, |
| 140 OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_))); |
| 141 |
| 142 StartWatching(); |
| 143 policy_watcher_->SetPolicies(&domain_empty_); |
| 144 StopWatching(); |
| 145 } |
| 146 |
| 147 TEST_F(PolicyWatcherTest, DomainFull) { |
| 148 EXPECT_CALL(mock_policy_callback_, |
| 149 OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_))); |
| 150 |
| 151 StartWatching(); |
| 152 policy_watcher_->SetPolicies(&domain_full_); |
90 StopWatching(); | 153 StopWatching(); |
91 } | 154 } |
92 | 155 |
93 TEST_F(PolicyWatcherTest, NatNoneThenTrue) { | 156 TEST_F(PolicyWatcherTest, NatNoneThenTrue) { |
94 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); | 157 EXPECT_CALL(mock_policy_callback_, |
| 158 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
95 | 159 |
96 StartWatching(); | 160 StartWatching(); |
97 policy_watcher_->SetPolicies(&empty); | 161 policy_watcher_->SetPolicies(&empty_); |
98 policy_watcher_->SetPolicies(&nat_true); | 162 policy_watcher_->SetPolicies(&nat_true_); |
99 StopWatching(); | 163 StopWatching(); |
100 } | 164 } |
101 | 165 |
102 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) { | 166 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) { |
103 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); | 167 EXPECT_CALL(mock_policy_callback_, |
| 168 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
104 | 169 |
105 StartWatching(); | 170 StartWatching(); |
106 policy_watcher_->SetPolicies(&empty); | 171 policy_watcher_->SetPolicies(&empty_); |
107 policy_watcher_->SetPolicies(&nat_true); | 172 policy_watcher_->SetPolicies(&nat_true_); |
108 policy_watcher_->SetPolicies(&nat_true); | 173 policy_watcher_->SetPolicies(&nat_true_); |
109 StopWatching(); | 174 StopWatching(); |
110 } | 175 } |
111 | 176 |
112 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) { | 177 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) { |
113 testing::InSequence sequence; | 178 testing::InSequence sequence; |
114 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); | 179 EXPECT_CALL(mock_policy_callback_, |
115 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); | 180 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 181 EXPECT_CALL(mock_policy_callback_, |
| 182 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); |
116 | 183 |
117 StartWatching(); | 184 StartWatching(); |
118 policy_watcher_->SetPolicies(&empty); | 185 policy_watcher_->SetPolicies(&empty_); |
119 policy_watcher_->SetPolicies(&nat_true); | 186 policy_watcher_->SetPolicies(&nat_true_); |
120 policy_watcher_->SetPolicies(&nat_true); | 187 policy_watcher_->SetPolicies(&nat_true_); |
121 policy_watcher_->SetPolicies(&nat_false); | 188 policy_watcher_->SetPolicies(&nat_false_); |
122 StopWatching(); | 189 StopWatching(); |
123 } | 190 } |
124 | 191 |
125 TEST_F(PolicyWatcherTest, NatNoneThenFalse) { | 192 TEST_F(PolicyWatcherTest, NatNoneThenFalse) { |
126 testing::InSequence sequence; | 193 testing::InSequence sequence; |
127 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); | 194 EXPECT_CALL(mock_policy_callback_, |
128 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); | 195 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 196 EXPECT_CALL(mock_policy_callback_, |
| 197 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); |
129 | 198 |
130 StartWatching(); | 199 StartWatching(); |
131 policy_watcher_->SetPolicies(&empty); | 200 policy_watcher_->SetPolicies(&empty_); |
132 policy_watcher_->SetPolicies(&nat_false); | 201 policy_watcher_->SetPolicies(&nat_false_); |
133 StopWatching(); | 202 StopWatching(); |
134 } | 203 } |
135 | 204 |
136 TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) { | 205 TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) { |
137 testing::InSequence sequence; | 206 testing::InSequence sequence; |
138 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); | 207 EXPECT_CALL(mock_policy_callback_, |
139 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_false))); | 208 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
140 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(IsPolicies(&nat_true))); | 209 EXPECT_CALL(mock_policy_callback_, |
| 210 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); |
| 211 EXPECT_CALL(mock_policy_callback_, |
| 212 OnPolicyUpdatePtr(IsPolicies(&nat_true_))); |
141 | 213 |
142 StartWatching(); | 214 StartWatching(); |
143 policy_watcher_->SetPolicies(&empty); | 215 policy_watcher_->SetPolicies(&empty_); |
144 policy_watcher_->SetPolicies(&nat_false); | 216 policy_watcher_->SetPolicies(&nat_false_); |
145 policy_watcher_->SetPolicies(&nat_true); | 217 policy_watcher_->SetPolicies(&nat_true_); |
146 StopWatching(); | 218 StopWatching(); |
147 } | 219 } |
148 | 220 |
| 221 TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) { |
| 222 testing::InSequence sequence; |
| 223 EXPECT_CALL(mock_policy_callback_, |
| 224 OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_empty_))); |
| 225 EXPECT_CALL(mock_policy_callback_, |
| 226 OnPolicyUpdatePtr(IsPolicies(&domain_full_))); |
| 227 EXPECT_CALL(mock_policy_callback_, |
| 228 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); |
| 229 EXPECT_CALL(mock_policy_callback_, |
| 230 OnPolicyUpdatePtr(IsPolicies(&domain_empty_))); |
| 231 EXPECT_CALL(mock_policy_callback_, |
| 232 OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_))); |
| 233 |
| 234 StartWatching(); |
| 235 policy_watcher_->SetPolicies(&nat_true_domain_empty_); |
| 236 policy_watcher_->SetPolicies(&nat_true_domain_full_); |
| 237 policy_watcher_->SetPolicies(&nat_false_domain_full_); |
| 238 policy_watcher_->SetPolicies(&nat_false_domain_empty_); |
| 239 policy_watcher_->SetPolicies(&nat_true_domain_full_); |
| 240 StopWatching(); |
| 241 } |
| 242 |
149 } // namespace policy_hack | 243 } // namespace policy_hack |
150 } // namespace remoting | 244 } // namespace remoting |
OLD | NEW |