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

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

Issue 6537020: Update policy backend and testserver for the newest policy protocol (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more last minute changes Created 9 years, 9 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/policy/cloud_policy_cache.h" 5 #include "chrome/browser/policy/cloud_policy_cache.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <string> 8 #include <string>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 file_thread_(BrowserThread::FILE, &loop_) {} 51 file_thread_(BrowserThread::FILE, &loop_) {}
52 52
53 void SetUp() { 53 void SetUp() {
54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
55 } 55 }
56 56
57 void TearDown() { 57 void TearDown() {
58 loop_.RunAllPending(); 58 loop_.RunAllPending();
59 } 59 }
60 60
61 // Creates a (signed) CloudPolicyResponse setting the given |homepage| and 61 // Creates a (signed) PolicyFetchResponse setting the given |homepage| and
62 // featuring the given |timestamp| (as issued by the server). 62 // featuring the given |timestamp| (as issued by the server).
63 // Mildly hacky special feature: pass an empty string as |homepage| to get 63 // Mildly hacky special feature: pass an empty string as |homepage| to get
64 // a completely empty policy. 64 // a completely empty policy.
65 em::CloudPolicyResponse* CreateHomepagePolicy( 65 em::PolicyFetchResponse* CreateHomepagePolicy(
66 const std::string& homepage, 66 const std::string& homepage,
67 const base::Time& timestamp, 67 const base::Time& timestamp,
68 const em::PolicyOptions::PolicyMode policy_mode) { 68 const em::PolicyOptions::PolicyMode policy_mode) {
69 em::SignedCloudPolicyResponse signed_response; 69 em::PolicyData signed_response;
70 if (homepage != "") { 70 if (homepage != "") {
71 em::CloudPolicySettings* settings = signed_response.mutable_settings(); 71 em::CloudPolicySettings settings;
72 em::HomepageLocationProto* homepagelocation_proto = 72 em::HomepageLocationProto* homepagelocation_proto =
73 settings->mutable_homepagelocation(); 73 settings.mutable_homepagelocation();
74 homepagelocation_proto->set_homepagelocation(homepage); 74 homepagelocation_proto->set_homepagelocation(homepage);
75 homepagelocation_proto->mutable_policy_options()->set_mode(policy_mode); 75 homepagelocation_proto->mutable_policy_options()->set_mode(policy_mode);
76 EXPECT_TRUE(
77 settings.SerializeToString(signed_response.mutable_policy_value()));
76 } 78 }
77 signed_response.set_timestamp(timestamp.ToTimeT()); 79 signed_response.set_timestamp(
80 (timestamp - base::Time::UnixEpoch()).InMilliseconds());
78 std::string serialized_signed_response; 81 std::string serialized_signed_response;
79 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response)); 82 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response));
80 83
81 em::CloudPolicyResponse* response = new em::CloudPolicyResponse; 84 em::PolicyFetchResponse* response = new em::PolicyFetchResponse;
82 response->set_signed_response(serialized_signed_response); 85 response->set_policy_data(serialized_signed_response);
83 // TODO(jkummerow): Set proper certificate_chain and signature (when 86 // TODO(jkummerow): Set proper certificate_chain and signature (when
84 // implementing support for signature verification). 87 // implementing support for signature verification).
85 response->set_signature("TODO"); 88 response->set_policy_data_signature("TODO");
86 response->add_certificate_chain("TODO"); 89 response->add_certificate_chain("TODO");
87 return response; 90 return response;
88 } 91 }
89 92
90 void WritePolicy(const em::CloudPolicyResponse& policy) { 93 void WritePolicy(const em::PolicyFetchResponse& policy) {
91 std::string data; 94 std::string data;
92 em::CachedCloudPolicyResponse cached_policy; 95 em::CachedCloudPolicyResponse cached_policy;
93 cached_policy.mutable_cloud_policy()->CopyFrom(policy); 96 cached_policy.mutable_cloud_policy()->CopyFrom(policy);
94 EXPECT_TRUE(cached_policy.SerializeToString(&data)); 97 EXPECT_TRUE(cached_policy.SerializeToString(&data));
95 int size = static_cast<int>(data.size()); 98 int size = static_cast<int>(data.size());
96 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size)); 99 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size));
97 } 100 }
98 101
99 // Takes ownership of |policy_response|. 102 // Takes ownership of |policy_response|.
100 void SetPolicy(CloudPolicyCache* cache, 103 void SetPolicy(CloudPolicyCache* cache,
101 em::CloudPolicyResponse* policy_response, 104 em::PolicyFetchResponse* policy_response,
102 bool expect_changed_policy) { 105 bool expect_changed_policy) {
103 scoped_ptr<em::CloudPolicyResponse> policy(policy_response); 106 scoped_ptr<em::PolicyFetchResponse> policy(policy_response);
104 ConfigurationPolicyObserverRegistrar registrar; 107 ConfigurationPolicyObserverRegistrar registrar;
105 registrar.Init(cache->GetManagedPolicyProvider(), &observer); 108 registrar.Init(cache->GetManagedPolicyProvider(), &observer);
106 if (expect_changed_policy) 109 if (expect_changed_policy)
107 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); 110 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1);
108 else 111 else
109 EXPECT_CALL(observer, OnUpdatePolicy()).Times(0); 112 EXPECT_CALL(observer, OnUpdatePolicy()).Times(0);
110 cache->SetPolicy(*policy); 113 cache->SetPolicy(*policy);
111 testing::Mock::VerifyAndClearExpectations(&observer); 114 testing::Mock::VerifyAndClearExpectations(&observer);
112 } 115 }
113 116
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 202
200 TEST_F(CloudPolicyCacheTest, LoadNoFile) { 203 TEST_F(CloudPolicyCacheTest, LoadNoFile) {
201 CloudPolicyCache cache(test_file()); 204 CloudPolicyCache cache(test_file());
202 cache.LoadFromFile(); 205 cache.LoadFromFile();
203 PolicyMap empty; 206 PolicyMap empty;
204 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); 207 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
205 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); 208 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
206 } 209 }
207 210
208 TEST_F(CloudPolicyCacheTest, RejectFuture) { 211 TEST_F(CloudPolicyCacheTest, RejectFuture) {
209 scoped_ptr<em::CloudPolicyResponse> policy_response( 212 scoped_ptr<em::PolicyFetchResponse> policy_response(
210 CreateHomepagePolicy("", base::Time::NowFromSystemTime() + 213 CreateHomepagePolicy("", base::Time::NowFromSystemTime() +
211 base::TimeDelta::FromMinutes(5), 214 base::TimeDelta::FromMinutes(5),
212 em::PolicyOptions::MANDATORY)); 215 em::PolicyOptions::MANDATORY));
213 WritePolicy(*policy_response); 216 WritePolicy(*policy_response);
214 CloudPolicyCache cache(test_file()); 217 CloudPolicyCache cache(test_file());
215 cache.LoadFromFile(); 218 cache.LoadFromFile();
216 PolicyMap empty; 219 PolicyMap empty;
217 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); 220 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
218 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); 221 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
219 } 222 }
220 223
221 TEST_F(CloudPolicyCacheTest, LoadWithFile) { 224 TEST_F(CloudPolicyCacheTest, LoadWithFile) {
222 scoped_ptr<em::CloudPolicyResponse> policy_response( 225 scoped_ptr<em::PolicyFetchResponse> policy_response(
223 CreateHomepagePolicy("", base::Time::NowFromSystemTime(), 226 CreateHomepagePolicy("", base::Time::NowFromSystemTime(),
224 em::PolicyOptions::MANDATORY)); 227 em::PolicyOptions::MANDATORY));
225 WritePolicy(*policy_response); 228 WritePolicy(*policy_response);
226 CloudPolicyCache cache(test_file()); 229 CloudPolicyCache cache(test_file());
227 cache.LoadFromFile(); 230 cache.LoadFromFile();
228 PolicyMap empty; 231 PolicyMap empty;
229 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); 232 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
230 EXPECT_NE(base::Time(), cache.last_policy_refresh_time()); 233 EXPECT_NE(base::Time(), cache.last_policy_refresh_time());
231 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time()); 234 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time());
232 } 235 }
233 236
234 TEST_F(CloudPolicyCacheTest, LoadWithData) { 237 TEST_F(CloudPolicyCacheTest, LoadWithData) {
235 scoped_ptr<em::CloudPolicyResponse> policy( 238 scoped_ptr<em::PolicyFetchResponse> policy(
236 CreateHomepagePolicy("http://www.example.com", 239 CreateHomepagePolicy("http://www.example.com",
237 base::Time::NowFromSystemTime(), 240 base::Time::NowFromSystemTime(),
238 em::PolicyOptions::MANDATORY)); 241 em::PolicyOptions::MANDATORY));
239 WritePolicy(*policy); 242 WritePolicy(*policy);
240 CloudPolicyCache cache(test_file()); 243 CloudPolicyCache cache(test_file());
241 cache.LoadFromFile(); 244 cache.LoadFromFile();
242 PolicyMap expected; 245 PolicyMap expected;
243 expected.Set(kPolicyHomepageLocation, 246 expected.Set(kPolicyHomepageLocation,
244 Value::CreateStringValue("http://www.example.com")); 247 Value::CreateStringValue("http://www.example.com"));
245 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); 248 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
246 } 249 }
247 250
248 TEST_F(CloudPolicyCacheTest, SetPolicy) { 251 TEST_F(CloudPolicyCacheTest, SetPolicy) {
249 CloudPolicyCache cache(test_file()); 252 CloudPolicyCache cache(test_file());
250 em::CloudPolicyResponse* policy = 253 em::PolicyFetchResponse* policy =
251 CreateHomepagePolicy("http://www.example.com", 254 CreateHomepagePolicy("http://www.example.com",
252 base::Time::NowFromSystemTime(), 255 base::Time::NowFromSystemTime(),
253 em::PolicyOptions::MANDATORY); 256 em::PolicyOptions::MANDATORY);
254 SetPolicy(&cache, policy, true); 257 SetPolicy(&cache, policy, true);
255 em::CloudPolicyResponse* policy2 = 258 em::PolicyFetchResponse* policy2 =
256 CreateHomepagePolicy("http://www.example.com", 259 CreateHomepagePolicy("http://www.example.com",
257 base::Time::NowFromSystemTime(), 260 base::Time::NowFromSystemTime(),
258 em::PolicyOptions::MANDATORY); 261 em::PolicyOptions::MANDATORY);
259 SetPolicy(&cache, policy2, false); 262 SetPolicy(&cache, policy2, false);
260 PolicyMap expected; 263 PolicyMap expected;
261 expected.Set(kPolicyHomepageLocation, 264 expected.Set(kPolicyHomepageLocation,
262 Value::CreateStringValue("http://www.example.com")); 265 Value::CreateStringValue("http://www.example.com"));
263 PolicyMap empty; 266 PolicyMap empty;
264 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); 267 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
265 EXPECT_TRUE(empty.Equals(recommended_policy(cache))); 268 EXPECT_TRUE(empty.Equals(recommended_policy(cache)));
266 policy = CreateHomepagePolicy("http://www.example.com", 269 policy = CreateHomepagePolicy("http://www.example.com",
267 base::Time::NowFromSystemTime(), 270 base::Time::NowFromSystemTime(),
268 em::PolicyOptions::RECOMMENDED); 271 em::PolicyOptions::RECOMMENDED);
269 SetPolicy(&cache, policy, true); 272 SetPolicy(&cache, policy, true);
270 EXPECT_TRUE(expected.Equals(recommended_policy(cache))); 273 EXPECT_TRUE(expected.Equals(recommended_policy(cache)));
271 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); 274 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
272 } 275 }
273 276
274 TEST_F(CloudPolicyCacheTest, ResetPolicy) { 277 TEST_F(CloudPolicyCacheTest, ResetPolicy) {
275 CloudPolicyCache cache(test_file()); 278 CloudPolicyCache cache(test_file());
276 279
277 em::CloudPolicyResponse* policy = 280 em::PolicyFetchResponse* policy =
278 CreateHomepagePolicy("http://www.example.com", 281 CreateHomepagePolicy("http://www.example.com",
279 base::Time::NowFromSystemTime(), 282 base::Time::NowFromSystemTime(),
280 em::PolicyOptions::MANDATORY); 283 em::PolicyOptions::MANDATORY);
281 SetPolicy(&cache, policy, true); 284 SetPolicy(&cache, policy, true);
282 PolicyMap expected; 285 PolicyMap expected;
283 expected.Set(kPolicyHomepageLocation, 286 expected.Set(kPolicyHomepageLocation,
284 Value::CreateStringValue("http://www.example.com")); 287 Value::CreateStringValue("http://www.example.com"));
285 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); 288 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
286 289
287 em::CloudPolicyResponse* empty_policy = 290 em::PolicyFetchResponse* empty_policy =
288 CreateHomepagePolicy("", base::Time::NowFromSystemTime(), 291 CreateHomepagePolicy("", base::Time::NowFromSystemTime(),
289 em::PolicyOptions::MANDATORY); 292 em::PolicyOptions::MANDATORY);
290 SetPolicy(&cache, empty_policy, true); 293 SetPolicy(&cache, empty_policy, true);
291 PolicyMap empty; 294 PolicyMap empty;
292 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); 295 EXPECT_TRUE(empty.Equals(mandatory_policy(cache)));
293 } 296 }
294 297
295 TEST_F(CloudPolicyCacheTest, PersistPolicy) { 298 TEST_F(CloudPolicyCacheTest, PersistPolicy) {
296 { 299 {
297 CloudPolicyCache cache(test_file()); 300 CloudPolicyCache cache(test_file());
298 scoped_ptr<em::CloudPolicyResponse> policy( 301 scoped_ptr<em::PolicyFetchResponse> policy(
299 CreateHomepagePolicy("http://www.example.com", 302 CreateHomepagePolicy("http://www.example.com",
300 base::Time::NowFromSystemTime(), 303 base::Time::NowFromSystemTime(),
301 em::PolicyOptions::MANDATORY)); 304 em::PolicyOptions::MANDATORY));
302 cache.SetPolicy(*policy); 305 cache.SetPolicy(*policy);
303 } 306 }
304 307
305 loop_.RunAllPending(); 308 loop_.RunAllPending();
306 309
307 EXPECT_TRUE(file_util::PathExists(test_file())); 310 EXPECT_TRUE(file_util::PathExists(test_file()));
308 CloudPolicyCache cache(test_file()); 311 CloudPolicyCache cache(test_file());
309 cache.LoadFromFile(); 312 cache.LoadFromFile();
310 PolicyMap expected; 313 PolicyMap expected;
311 expected.Set(kPolicyHomepageLocation, 314 expected.Set(kPolicyHomepageLocation,
312 Value::CreateStringValue("http://www.example.com")); 315 Value::CreateStringValue("http://www.example.com"));
313 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); 316 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
314 } 317 }
315 318
316 TEST_F(CloudPolicyCacheTest, FreshPolicyOverride) { 319 TEST_F(CloudPolicyCacheTest, FreshPolicyOverride) {
317 scoped_ptr<em::CloudPolicyResponse> policy( 320 scoped_ptr<em::PolicyFetchResponse> policy(
318 CreateHomepagePolicy("http://www.example.com", 321 CreateHomepagePolicy("http://www.example.com",
319 base::Time::NowFromSystemTime(), 322 base::Time::NowFromSystemTime(),
320 em::PolicyOptions::MANDATORY)); 323 em::PolicyOptions::MANDATORY));
321 WritePolicy(*policy); 324 WritePolicy(*policy);
322 325
323 CloudPolicyCache cache(test_file()); 326 CloudPolicyCache cache(test_file());
324 em::CloudPolicyResponse* updated_policy = 327 em::PolicyFetchResponse* updated_policy =
325 CreateHomepagePolicy("http://www.chromium.org", 328 CreateHomepagePolicy("http://www.chromium.org",
326 base::Time::NowFromSystemTime(), 329 base::Time::NowFromSystemTime(),
327 em::PolicyOptions::MANDATORY); 330 em::PolicyOptions::MANDATORY);
328 SetPolicy(&cache, updated_policy, true); 331 SetPolicy(&cache, updated_policy, true);
329 332
330 cache.LoadFromFile(); 333 cache.LoadFromFile();
331 PolicyMap expected; 334 PolicyMap expected;
332 expected.Set(kPolicyHomepageLocation, 335 expected.Set(kPolicyHomepageLocation,
333 Value::CreateStringValue("http://www.chromium.org")); 336 Value::CreateStringValue("http://www.chromium.org"));
334 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); 337 EXPECT_TRUE(expected.Equals(mandatory_policy(cache)));
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 em::DevicePolicyResponse policy; 642 em::DevicePolicyResponse policy;
640 AddStringPolicy(&policy, "HomepageLocation", "http://www.example.com"); 643 AddStringPolicy(&policy, "HomepageLocation", "http://www.example.com");
641 scoped_ptr<Value> decoded(DecodeDevicePolicy(policy)); 644 scoped_ptr<Value> decoded(DecodeDevicePolicy(policy));
642 DictionaryValue expected; 645 DictionaryValue expected;
643 expected.Set("HomepageLocation", 646 expected.Set("HomepageLocation",
644 Value::CreateStringValue("http://www.example.com")); 647 Value::CreateStringValue("http://www.example.com"));
645 EXPECT_TRUE(expected.Equals(decoded.get())); 648 EXPECT_TRUE(expected.Equals(decoded.get()));
646 } 649 }
647 650
648 } // namespace policy 651 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud_policy_cache.cc ('k') | chrome/browser/policy/cloud_policy_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698