Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: chrome/browser/policy/cloud_policy_cache_unittest.cc

Issue 6409040: New policy protobuf protocol. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address feedback; fix gyp files Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "chrome/browser/policy/cloud_policy_cache.h"
6
7 #include <limits>
8 #include <string>
9
10 #include "base/file_util.h"
11 #include "base/message_loop.h"
12 #include "base/scoped_temp_dir.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_thread.h"
15 #include "chrome/browser/policy/proto/cloud_policy.pb.h"
16 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
17 #include "chrome/browser/policy/proto/device_management_local.pb.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace policy {
21
22 Value* DecodeIntegerValue(google::protobuf::int64 value);
23 ListValue* DecodeStringList(const em::StringList& string_list);
24
25 // Tests the device management policy cache.
26 class CloudPolicyCacheTest : public testing::Test {
27 protected:
28 typedef ConfigurationPolicyProvider::PolicyMapType PolicyMapType;
29
30 CloudPolicyCacheTest()
31 : loop_(MessageLoop::TYPE_UI),
32 ui_thread_(BrowserThread::UI, &loop_),
33 file_thread_(BrowserThread::FILE, &loop_) {}
34
35 void SetUp() {
36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
37 }
38
39 void TearDown() {
40 loop_.RunAllPending();
41 }
42
43 // Creates a (signed) CloudPolicyResponse setting the given |homepage| and
44 // featuring the given |timestamp| (as issued by the server).
45 // Mildly hacky special feature: pass an empty string as |homepage| to get
46 // a completely empty policy.
47 em::CloudPolicyResponse* CreateHomepagePolicy(
48 const std::string& homepage,
49 const base::Time& timestamp,
50 const em::PolicyOptions::PolicyMode policy_mode) {
51 em::SignedCloudPolicyResponse signed_response;
52 if (homepage != "") {
53 em::CloudPolicySettings* settings = signed_response.mutable_settings();
54 em::HomepageProto* homepage_proto = settings->mutable_homepage();
55 homepage_proto->set_homepagelocation(homepage);
56 homepage_proto->mutable_policy_options()->set_mode(policy_mode);
57 }
58 signed_response.set_timestamp(timestamp.ToTimeT());
59 std::string serialized_signed_response;
60 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response));
61
62 em::CloudPolicyResponse* response = new em::CloudPolicyResponse;
63 response->set_signed_response(serialized_signed_response);
64 // TODO(jkummerow): Set proper certificate_chain and signature (when
65 // implementing support for signature verification).
66 response->set_signature("TODO");
67 response->add_certificate_chain("TODO");
68 return response;
69 }
70
71 void WritePolicy(const em::CloudPolicyResponse& policy) {
72 std::string data;
73 em::CachedCloudPolicyResponse cached_policy;
74 cached_policy.mutable_cloud_policy()->CopyFrom(policy);
75 EXPECT_TRUE(cached_policy.SerializeToString(&data));
76 int size = static_cast<int>(data.size());
77 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size));
78 }
79
80 FilePath test_file() {
81 return temp_dir_.path().AppendASCII("CloudPolicyCacheTest");
82 }
83
84 bool Equals(const PolicyMapType* a, const PolicyMapType* b) const {
85 return CloudPolicyCache::Equals(a, b);
86 }
87
88 MessageLoop loop_;
89
90 private:
91 ScopedTempDir temp_dir_;
92 BrowserThread ui_thread_;
93 BrowserThread file_thread_;
94 };
95
96 TEST_F(CloudPolicyCacheTest, Equals) {
97 PolicyMapType a;
98 a.insert(std::make_pair(kPolicyHomepageLocation,
99 Value::CreateStringValue("aaa")));
100 PolicyMapType a2;
101 a2.insert(std::make_pair(kPolicyHomepageLocation,
102 Value::CreateStringValue("aaa")));
103 PolicyMapType b;
104 b.insert(std::make_pair(kPolicyHomepageLocation,
105 Value::CreateStringValue("bbb")));
106 PolicyMapType c;
107 c.insert(std::make_pair(kPolicyHomepageLocation,
108 Value::CreateStringValue("aaa")));
109 c.insert(std::make_pair(kPolicyHomepageIsNewTabPage,
110 Value::CreateBooleanValue(true)));
111 EXPECT_FALSE(Equals(&a, &b));
112 EXPECT_FALSE(Equals(&b, &a));
113 EXPECT_FALSE(Equals(&a, &c));
114 EXPECT_FALSE(Equals(&c, &a));
115 EXPECT_TRUE(Equals(&a, &a2));
116 EXPECT_TRUE(Equals(&a2, &a));
117 PolicyMapType empty1;
118 PolicyMapType empty2;
119 EXPECT_TRUE(Equals(&empty1, &empty2));
120 EXPECT_TRUE(Equals(&empty2, &empty1));
121 EXPECT_FALSE(Equals(&empty1, &a));
122 EXPECT_FALSE(Equals(&a, &empty1));
123 }
124
125 TEST_F(CloudPolicyCacheTest, DecodePolicy) {
126 em::CloudPolicySettings settings;
127 settings.mutable_homepage()->set_homepagelocation("chromium.org");
128 settings.mutable_javascriptenabled()->set_javascriptenabled(true);
129 settings.mutable_javascriptenabled()->mutable_policy_options()->set_mode(
130 em::PolicyOptions::MANDATORY);
131 settings.mutable_policyrefreshrate()->set_policyrefreshrate(5);
132 settings.mutable_policyrefreshrate()->mutable_policy_options()->set_mode(
133 em::PolicyOptions::RECOMMENDED);
134 scoped_ptr<PolicyMapType> mandatory_policy(new PolicyMapType);
135 scoped_ptr<PolicyMapType> recommended_policy(new PolicyMapType);
136 DecodePolicy(settings, mandatory_policy.get(), recommended_policy.get());
137 PolicyMapType mandatory;
138 mandatory.insert(std::make_pair(kPolicyHomepageLocation,
139 Value::CreateStringValue("chromium.org")));
140 mandatory.insert(std::make_pair(kPolicyJavascriptEnabled,
141 Value::CreateBooleanValue(true)));
142 PolicyMapType recommended;
143 recommended.insert(std::make_pair(kPolicyPolicyRefreshRate,
144 Value::CreateIntegerValue(5)));
145 EXPECT_TRUE(Equals(&mandatory, mandatory_policy.get()));
146 EXPECT_TRUE(Equals(&recommended, recommended_policy.get()));
147 }
148
149 TEST_F(CloudPolicyCacheTest, DecodeIntegerValue) {
150 const int min = std::numeric_limits<int>::min();
151 const int max = std::numeric_limits<int>::max();
152 scoped_ptr<Value> value(
153 DecodeIntegerValue(static_cast<google::protobuf::int64>(42)));
154 ASSERT_TRUE(value.get());
155 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(42)));
156 value.reset(
157 DecodeIntegerValue(static_cast<google::protobuf::int64>(min - 1LL)));
158 EXPECT_EQ(NULL, value.get());
159 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(min)));
160 ASSERT_TRUE(value.get());
161 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(min)));
162 value.reset(
163 DecodeIntegerValue(static_cast<google::protobuf::int64>(max + 1LL)));
164 EXPECT_EQ(NULL, value.get());
165 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(max)));
166 ASSERT_TRUE(value.get());
167 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(max)));
168 }
169
170 TEST_F(CloudPolicyCacheTest, DecodeStringList) {
171 em::StringList string_list;
172 string_list.add_entries("ponies");
173 string_list.add_entries("more ponies");
174 scoped_ptr<ListValue> decoded(DecodeStringList(string_list));
175 ListValue expected;
176 expected.Append(Value::CreateStringValue("ponies"));
177 expected.Append(Value::CreateStringValue("more ponies"));
178 EXPECT_TRUE(decoded->Equals(&expected));
179 }
180
181 TEST_F(CloudPolicyCacheTest, Empty) {
182 CloudPolicyCache cache(test_file());
183 PolicyMapType empty;
184 scoped_ptr<PolicyMapType> policy(cache.GetMandatoryPolicy());
185 EXPECT_TRUE(Equals(&empty, policy.get()));
186 policy.reset(cache.GetRecommendedPolicy());
187 EXPECT_TRUE(Equals(&empty, policy.get()));
188 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
189 }
190
191 TEST_F(CloudPolicyCacheTest, LoadNoFile) {
192 CloudPolicyCache cache(test_file());
193 cache.LoadPolicyFromFile();
194 PolicyMapType empty;
195 scoped_ptr<PolicyMapType> policy(cache.GetMandatoryPolicy());
196 EXPECT_TRUE(Equals(&empty, policy.get()));
197 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
198 }
199
200 TEST_F(CloudPolicyCacheTest, RejectFuture) {
201 scoped_ptr<em::CloudPolicyResponse> policy_response(
202 CreateHomepagePolicy("", base::Time::NowFromSystemTime() +
203 base::TimeDelta::FromMinutes(5),
204 em::PolicyOptions::MANDATORY));
205 WritePolicy(*policy_response);
206 CloudPolicyCache cache(test_file());
207 cache.LoadPolicyFromFile();
208 PolicyMapType empty;
209 scoped_ptr<PolicyMapType> policy(cache.GetMandatoryPolicy());
210 EXPECT_TRUE(Equals(&empty, policy.get()));
211 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
212 }
213
214 TEST_F(CloudPolicyCacheTest, LoadWithFile) {
215 scoped_ptr<em::CloudPolicyResponse> policy_response(
216 CreateHomepagePolicy("", base::Time::NowFromSystemTime(),
217 em::PolicyOptions::MANDATORY));
218 WritePolicy(*policy_response);
219 CloudPolicyCache cache(test_file());
220 cache.LoadPolicyFromFile();
221 PolicyMapType empty;
222 scoped_ptr<PolicyMapType> policy(cache.GetMandatoryPolicy());
223 EXPECT_TRUE(Equals(&empty, policy.get()));
224 EXPECT_NE(base::Time(), cache.last_policy_refresh_time());
225 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time());
226 }
227
228 TEST_F(CloudPolicyCacheTest, LoadWithData) {
229 scoped_ptr<em::CloudPolicyResponse> policy(
230 CreateHomepagePolicy("http://www.example.com",
231 base::Time::NowFromSystemTime(),
232 em::PolicyOptions::MANDATORY));
233 WritePolicy(*policy);
234 CloudPolicyCache cache(test_file());
235 cache.LoadPolicyFromFile();
236 PolicyMapType expected;
237 expected.insert(
238 std::make_pair(kPolicyHomepageLocation,
239 Value::CreateStringValue("http://www.example.com")));
240 scoped_ptr<PolicyMapType> policy_value(cache.GetMandatoryPolicy());
241 EXPECT_TRUE(Equals(&expected, policy_value.get()));
242 }
243
244 TEST_F(CloudPolicyCacheTest, SetPolicy) {
245 CloudPolicyCache cache(test_file());
246 scoped_ptr<em::CloudPolicyResponse> policy(
247 CreateHomepagePolicy("http://www.example.com",
248 base::Time::NowFromSystemTime(),
249 em::PolicyOptions::MANDATORY));
250 EXPECT_TRUE(cache.SetPolicy(*policy));
251 scoped_ptr<em::CloudPolicyResponse> policy2(
252 CreateHomepagePolicy("http://www.example.com",
253 base::Time::NowFromSystemTime(),
254 em::PolicyOptions::MANDATORY));
255 EXPECT_FALSE(cache.SetPolicy(*policy2));
256 PolicyMapType expected;
257 expected.insert(
258 std::make_pair(kPolicyHomepageLocation,
259 Value::CreateStringValue("http://www.example.com")));
260 PolicyMapType empty;
261 scoped_ptr<PolicyMapType> policy_value(cache.GetMandatoryPolicy());
262 EXPECT_TRUE(Equals(&expected, policy_value.get()));
263 policy_value.reset(cache.GetRecommendedPolicy());
264 EXPECT_TRUE(Equals(&empty, policy_value.get()));
265 policy.reset(CreateHomepagePolicy("http://www.example.com",
266 base::Time::NowFromSystemTime(),
267 em::PolicyOptions::RECOMMENDED));
268 EXPECT_TRUE(cache.SetPolicy(*policy));
269 policy_value.reset(cache.GetRecommendedPolicy());
270 EXPECT_TRUE(Equals(&expected, policy_value.get()));
271 policy_value.reset(cache.GetMandatoryPolicy());
272 EXPECT_TRUE(Equals(&empty, policy_value.get()));
273 }
274
275 TEST_F(CloudPolicyCacheTest, ResetPolicy) {
276 CloudPolicyCache cache(test_file());
277
278 scoped_ptr<em::CloudPolicyResponse> policy(
279 CreateHomepagePolicy("http://www.example.com",
280 base::Time::NowFromSystemTime(),
281 em::PolicyOptions::MANDATORY));
282 EXPECT_TRUE(cache.SetPolicy(*policy));
283 PolicyMapType expected;
284 expected.insert(
285 std::make_pair(kPolicyHomepageLocation,
286 Value::CreateStringValue("http://www.example.com")));
287 scoped_ptr<PolicyMapType> policy_value(cache.GetMandatoryPolicy());
288 EXPECT_TRUE(Equals(&expected, policy_value.get()));
289
290 scoped_ptr<em::CloudPolicyResponse> empty_policy(
291 CreateHomepagePolicy("", base::Time::NowFromSystemTime(),
292 em::PolicyOptions::MANDATORY));
293 EXPECT_TRUE(cache.SetPolicy(*empty_policy));
294 policy_value.reset(cache.GetMandatoryPolicy());
295 PolicyMapType empty;
296 EXPECT_TRUE(Equals(&empty, policy_value.get()));
297 }
298
299 TEST_F(CloudPolicyCacheTest, PersistPolicy) {
300 {
301 CloudPolicyCache cache(test_file());
302 scoped_ptr<em::CloudPolicyResponse> policy(
303 CreateHomepagePolicy("http://www.example.com",
304 base::Time::NowFromSystemTime(),
305 em::PolicyOptions::MANDATORY));
306 cache.SetPolicy(*policy);
307 }
308
309 loop_.RunAllPending();
310
311 EXPECT_TRUE(file_util::PathExists(test_file()));
312 CloudPolicyCache cache(test_file());
313 cache.LoadPolicyFromFile();
314 PolicyMapType expected;
315 expected.insert(
316 std::make_pair(kPolicyHomepageLocation,
317 Value::CreateStringValue("http://www.example.com")));
318 scoped_ptr<PolicyMapType> policy_value(cache.GetMandatoryPolicy());
319 EXPECT_TRUE(Equals(&expected, policy_value.get()));
320 }
321
322 TEST_F(CloudPolicyCacheTest, FreshPolicyOverride) {
323 scoped_ptr<em::CloudPolicyResponse> policy(
324 CreateHomepagePolicy("http://www.example.com",
325 base::Time::NowFromSystemTime(),
326 em::PolicyOptions::MANDATORY));
327 WritePolicy(*policy);
328
329 CloudPolicyCache cache(test_file());
330 scoped_ptr<em::CloudPolicyResponse> updated_policy(
331 CreateHomepagePolicy("http://www.chromium.org",
332 base::Time::NowFromSystemTime(),
333 em::PolicyOptions::MANDATORY));
334 EXPECT_TRUE(cache.SetPolicy(*updated_policy));
335
336 cache.LoadPolicyFromFile();
337 PolicyMapType expected;
338 expected.insert(
339 std::make_pair(kPolicyHomepageLocation,
340 Value::CreateStringValue("http://www.chromium.org")));
341 scoped_ptr<PolicyMapType> policy_value(cache.GetMandatoryPolicy());
342 EXPECT_TRUE(Equals(&expected, policy_value.get()));
343 }
344
345 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698