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 "chrome/browser/policy/user_policy_cache.h" | 5 #include "chrome/browser/policy/user_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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 UserPolicyCacheTest() | 50 UserPolicyCacheTest() |
51 : loop_(MessageLoop::TYPE_UI), | 51 : loop_(MessageLoop::TYPE_UI), |
52 ui_thread_(BrowserThread::UI, &loop_), | 52 ui_thread_(BrowserThread::UI, &loop_), |
53 file_thread_(BrowserThread::FILE, &loop_) {} | 53 file_thread_(BrowserThread::FILE, &loop_) {} |
54 | 54 |
55 void SetUp() { | 55 void SetUp() { |
56 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 56 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
57 } | 57 } |
58 | 58 |
59 void TearDown() { | 59 void TearDown() { |
60 loop_.RunAllPending(); | 60 loop_.RunUntilIdle(); |
61 } | 61 } |
62 | 62 |
63 // Creates a (signed) PolicyFetchResponse setting the given |homepage| and | 63 // Creates a (signed) PolicyFetchResponse setting the given |homepage| and |
64 // featuring the given |timestamp| (as issued by the server). | 64 // featuring the given |timestamp| (as issued by the server). |
65 // Mildly hacky special feature: pass an empty string as |homepage| to get | 65 // Mildly hacky special feature: pass an empty string as |homepage| to get |
66 // a completely empty policy. | 66 // a completely empty policy. |
67 em::PolicyFetchResponse* CreateHomepagePolicy( | 67 em::PolicyFetchResponse* CreateHomepagePolicy( |
68 const std::string& homepage, | 68 const std::string& homepage, |
69 const base::Time& timestamp, | 69 const base::Time& timestamp, |
70 const em::PolicyOptions::PolicyMode policy_mode) { | 70 const em::PolicyOptions::PolicyMode policy_mode) { |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 TEST_F(UserPolicyCacheTest, Empty) { | 207 TEST_F(UserPolicyCacheTest, Empty) { |
208 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 208 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
209 PolicyMap empty; | 209 PolicyMap empty; |
210 EXPECT_TRUE(empty.Equals(*cache.policy())); | 210 EXPECT_TRUE(empty.Equals(*cache.policy())); |
211 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); | 211 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); |
212 } | 212 } |
213 | 213 |
214 TEST_F(UserPolicyCacheTest, LoadNoFile) { | 214 TEST_F(UserPolicyCacheTest, LoadNoFile) { |
215 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 215 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
216 cache.Load(); | 216 cache.Load(); |
217 loop_.RunAllPending(); | 217 loop_.RunUntilIdle(); |
218 PolicyMap empty; | 218 PolicyMap empty; |
219 EXPECT_TRUE(empty.Equals(*cache.policy())); | 219 EXPECT_TRUE(empty.Equals(*cache.policy())); |
220 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); | 220 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); |
221 } | 221 } |
222 | 222 |
223 TEST_F(UserPolicyCacheTest, RejectFuture) { | 223 TEST_F(UserPolicyCacheTest, RejectFuture) { |
224 scoped_ptr<em::PolicyFetchResponse> policy_response( | 224 scoped_ptr<em::PolicyFetchResponse> policy_response( |
225 CreateHomepagePolicy("", base::Time::NowFromSystemTime() + | 225 CreateHomepagePolicy("", base::Time::NowFromSystemTime() + |
226 base::TimeDelta::FromMinutes(5), | 226 base::TimeDelta::FromMinutes(5), |
227 em::PolicyOptions::MANDATORY)); | 227 em::PolicyOptions::MANDATORY)); |
228 WritePolicy(*policy_response); | 228 WritePolicy(*policy_response); |
229 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 229 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
230 cache.Load(); | 230 cache.Load(); |
231 loop_.RunAllPending(); | 231 loop_.RunUntilIdle(); |
232 PolicyMap empty; | 232 PolicyMap empty; |
233 EXPECT_TRUE(empty.Equals(*cache.policy())); | 233 EXPECT_TRUE(empty.Equals(*cache.policy())); |
234 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); | 234 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); |
235 } | 235 } |
236 | 236 |
237 TEST_F(UserPolicyCacheTest, LoadWithFile) { | 237 TEST_F(UserPolicyCacheTest, LoadWithFile) { |
238 scoped_ptr<em::PolicyFetchResponse> policy_response( | 238 scoped_ptr<em::PolicyFetchResponse> policy_response( |
239 CreateHomepagePolicy("", base::Time::NowFromSystemTime(), | 239 CreateHomepagePolicy("", base::Time::NowFromSystemTime(), |
240 em::PolicyOptions::MANDATORY)); | 240 em::PolicyOptions::MANDATORY)); |
241 WritePolicy(*policy_response); | 241 WritePolicy(*policy_response); |
242 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 242 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
243 cache.Load(); | 243 cache.Load(); |
244 loop_.RunAllPending(); | 244 loop_.RunUntilIdle(); |
245 PolicyMap empty; | 245 PolicyMap empty; |
246 EXPECT_TRUE(empty.Equals(*cache.policy())); | 246 EXPECT_TRUE(empty.Equals(*cache.policy())); |
247 EXPECT_NE(base::Time(), cache.last_policy_refresh_time()); | 247 EXPECT_NE(base::Time(), cache.last_policy_refresh_time()); |
248 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time()); | 248 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time()); |
249 } | 249 } |
250 | 250 |
251 TEST_F(UserPolicyCacheTest, LoadWithData) { | 251 TEST_F(UserPolicyCacheTest, LoadWithData) { |
252 scoped_ptr<em::PolicyFetchResponse> policy( | 252 scoped_ptr<em::PolicyFetchResponse> policy( |
253 CreateHomepagePolicy("http://www.example.com", | 253 CreateHomepagePolicy("http://www.example.com", |
254 base::Time::NowFromSystemTime(), | 254 base::Time::NowFromSystemTime(), |
255 em::PolicyOptions::MANDATORY)); | 255 em::PolicyOptions::MANDATORY)); |
256 WritePolicy(*policy); | 256 WritePolicy(*policy); |
257 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 257 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
258 cache.Load(); | 258 cache.Load(); |
259 loop_.RunAllPending(); | 259 loop_.RunUntilIdle(); |
260 PolicyMap expected; | 260 PolicyMap expected; |
261 expected.Set(key::kHomepageLocation, | 261 expected.Set(key::kHomepageLocation, |
262 POLICY_LEVEL_MANDATORY, | 262 POLICY_LEVEL_MANDATORY, |
263 POLICY_SCOPE_USER, | 263 POLICY_SCOPE_USER, |
264 Value::CreateStringValue("http://www.example.com")); | 264 Value::CreateStringValue("http://www.example.com")); |
265 EXPECT_TRUE(expected.Equals(*cache.policy())); | 265 EXPECT_TRUE(expected.Equals(*cache.policy())); |
266 } | 266 } |
267 | 267 |
268 TEST_F(UserPolicyCacheTest, SetPolicy) { | 268 TEST_F(UserPolicyCacheTest, SetPolicy) { |
269 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 269 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 TEST_F(UserPolicyCacheTest, PersistPolicy) { | 320 TEST_F(UserPolicyCacheTest, PersistPolicy) { |
321 { | 321 { |
322 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 322 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
323 scoped_ptr<em::PolicyFetchResponse> policy( | 323 scoped_ptr<em::PolicyFetchResponse> policy( |
324 CreateHomepagePolicy("http://www.example.com", | 324 CreateHomepagePolicy("http://www.example.com", |
325 base::Time::NowFromSystemTime(), | 325 base::Time::NowFromSystemTime(), |
326 em::PolicyOptions::MANDATORY)); | 326 em::PolicyOptions::MANDATORY)); |
327 EXPECT_TRUE(cache.SetPolicy(*policy)); | 327 EXPECT_TRUE(cache.SetPolicy(*policy)); |
328 } | 328 } |
329 | 329 |
330 loop_.RunAllPending(); | 330 loop_.RunUntilIdle(); |
331 | 331 |
332 EXPECT_TRUE(file_util::PathExists(test_file())); | 332 EXPECT_TRUE(file_util::PathExists(test_file())); |
333 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 333 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
334 cache.Load(); | 334 cache.Load(); |
335 loop_.RunAllPending(); | 335 loop_.RunUntilIdle(); |
336 PolicyMap expected; | 336 PolicyMap expected; |
337 expected.Set(key::kHomepageLocation, | 337 expected.Set(key::kHomepageLocation, |
338 POLICY_LEVEL_MANDATORY, | 338 POLICY_LEVEL_MANDATORY, |
339 POLICY_SCOPE_USER, | 339 POLICY_SCOPE_USER, |
340 Value::CreateStringValue("http://www.example.com")); | 340 Value::CreateStringValue("http://www.example.com")); |
341 EXPECT_TRUE(expected.Equals(*cache.policy())); | 341 EXPECT_TRUE(expected.Equals(*cache.policy())); |
342 } | 342 } |
343 | 343 |
344 TEST_F(UserPolicyCacheTest, FreshPolicyOverride) { | 344 TEST_F(UserPolicyCacheTest, FreshPolicyOverride) { |
345 scoped_ptr<em::PolicyFetchResponse> policy( | 345 scoped_ptr<em::PolicyFetchResponse> policy( |
346 CreateHomepagePolicy("http://www.example.com", | 346 CreateHomepagePolicy("http://www.example.com", |
347 base::Time::NowFromSystemTime(), | 347 base::Time::NowFromSystemTime(), |
348 em::PolicyOptions::MANDATORY)); | 348 em::PolicyOptions::MANDATORY)); |
349 WritePolicy(*policy); | 349 WritePolicy(*policy); |
350 | 350 |
351 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 351 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
352 em::PolicyFetchResponse* updated_policy = | 352 em::PolicyFetchResponse* updated_policy = |
353 CreateHomepagePolicy("http://www.chromium.org", | 353 CreateHomepagePolicy("http://www.chromium.org", |
354 base::Time::NowFromSystemTime(), | 354 base::Time::NowFromSystemTime(), |
355 em::PolicyOptions::MANDATORY); | 355 em::PolicyOptions::MANDATORY); |
356 EXPECT_TRUE(SetPolicy(&cache, updated_policy)); | 356 EXPECT_TRUE(SetPolicy(&cache, updated_policy)); |
357 | 357 |
358 cache.Load(); | 358 cache.Load(); |
359 loop_.RunAllPending(); | 359 loop_.RunUntilIdle(); |
360 PolicyMap expected; | 360 PolicyMap expected; |
361 expected.Set(key::kHomepageLocation, | 361 expected.Set(key::kHomepageLocation, |
362 POLICY_LEVEL_MANDATORY, | 362 POLICY_LEVEL_MANDATORY, |
363 POLICY_SCOPE_USER, | 363 POLICY_SCOPE_USER, |
364 Value::CreateStringValue("http://www.chromium.org")); | 364 Value::CreateStringValue("http://www.chromium.org")); |
365 EXPECT_TRUE(expected.Equals(*cache.policy())); | 365 EXPECT_TRUE(expected.Equals(*cache.policy())); |
366 } | 366 } |
367 | 367 |
368 TEST_F(UserPolicyCacheTest, SetReady) { | 368 TEST_F(UserPolicyCacheTest, SetReady) { |
369 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 369 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 POLICY_SCOPE_USER, | 419 POLICY_SCOPE_USER, |
420 Value::CreateStringValue("http://www.example.com")); | 420 Value::CreateStringValue("http://www.example.com")); |
421 EXPECT_TRUE(SetPolicy(&cache, policy)); | 421 EXPECT_TRUE(SetPolicy(&cache, policy)); |
422 EXPECT_TRUE(expected.Equals(*cache.policy())); | 422 EXPECT_TRUE(expected.Equals(*cache.policy())); |
423 } | 423 } |
424 | 424 |
425 TEST_F(UserPolicyCacheTest, CheckReadyNoWaiting) { | 425 TEST_F(UserPolicyCacheTest, CheckReadyNoWaiting) { |
426 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); | 426 UserPolicyCache cache(test_file(), false /* wait_for_policy_fetch */); |
427 EXPECT_FALSE(cache.IsReady()); | 427 EXPECT_FALSE(cache.IsReady()); |
428 cache.Load(); | 428 cache.Load(); |
429 loop_.RunAllPending(); | 429 loop_.RunUntilIdle(); |
430 EXPECT_TRUE(cache.IsReady()); | 430 EXPECT_TRUE(cache.IsReady()); |
431 } | 431 } |
432 | 432 |
433 TEST_F(UserPolicyCacheTest, CheckReadyWaitForFetch) { | 433 TEST_F(UserPolicyCacheTest, CheckReadyWaitForFetch) { |
434 UserPolicyCache cache(test_file(), true /* wait_for_policy_fetch */); | 434 UserPolicyCache cache(test_file(), true /* wait_for_policy_fetch */); |
435 EXPECT_FALSE(cache.IsReady()); | 435 EXPECT_FALSE(cache.IsReady()); |
436 cache.Load(); | 436 cache.Load(); |
437 loop_.RunAllPending(); | 437 loop_.RunUntilIdle(); |
438 EXPECT_FALSE(cache.IsReady()); | 438 EXPECT_FALSE(cache.IsReady()); |
439 cache.SetFetchingDone(); | 439 cache.SetFetchingDone(); |
440 EXPECT_TRUE(cache.IsReady()); | 440 EXPECT_TRUE(cache.IsReady()); |
441 } | 441 } |
442 | 442 |
443 TEST_F(UserPolicyCacheTest, CheckReadyWaitForDisk) { | 443 TEST_F(UserPolicyCacheTest, CheckReadyWaitForDisk) { |
444 UserPolicyCache cache(test_file(), true /* wait_for_policy_fetch */); | 444 UserPolicyCache cache(test_file(), true /* wait_for_policy_fetch */); |
445 EXPECT_FALSE(cache.IsReady()); | 445 EXPECT_FALSE(cache.IsReady()); |
446 cache.SetFetchingDone(); | 446 cache.SetFetchingDone(); |
447 EXPECT_FALSE(cache.IsReady()); | 447 EXPECT_FALSE(cache.IsReady()); |
448 cache.Load(); | 448 cache.Load(); |
449 loop_.RunAllPending(); | 449 loop_.RunUntilIdle(); |
450 EXPECT_TRUE(cache.IsReady()); | 450 EXPECT_TRUE(cache.IsReady()); |
451 } | 451 } |
452 | 452 |
453 } // namespace policy | 453 } // namespace policy |
OLD | NEW |