| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/component_cloud_policy_store.h" | 5 #include "chrome/browser/policy/cloud/component_cloud_policy_store.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | |
| 9 #include <string> | 8 #include <string> |
| 10 | 9 |
| 11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/sha1.h" | 15 #include "base/sha1.h" |
| 16 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 17 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 17 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
| 18 #include "chrome/browser/policy/cloud/policy_builder.h" | 18 #include "chrome/browser/policy/cloud/policy_builder.h" |
| 19 #include "chrome/browser/policy/cloud/resource_cache.h" | 19 #include "chrome/browser/policy/cloud/resource_cache.h" |
| 20 #include "chrome/browser/policy/external_data_fetcher.h" | 20 #include "chrome/browser/policy/external_data_fetcher.h" |
| 21 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" | 21 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 41 " \"Second\": {" | 41 " \"Second\": {" |
| 42 " \"Value\": \"maybe\"," | 42 " \"Value\": \"maybe\"," |
| 43 " \"Level\": \"Recommended\"" | 43 " \"Level\": \"Recommended\"" |
| 44 " }" | 44 " }" |
| 45 "}"; | 45 "}"; |
| 46 | 46 |
| 47 std::string TestPolicyHash() { | 47 std::string TestPolicyHash() { |
| 48 return base::SHA1HashString(kTestPolicy); | 48 return base::SHA1HashString(kTestPolicy); |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool NotEqual(const std::string& expected, const std::string& key) { |
| 52 return key != expected; |
| 53 } |
| 54 |
| 55 bool True(const std::string& ignored) { |
| 56 return true; |
| 57 } |
| 58 |
| 51 class MockComponentCloudPolicyStoreDelegate | 59 class MockComponentCloudPolicyStoreDelegate |
| 52 : public ComponentCloudPolicyStore::Delegate { | 60 : public ComponentCloudPolicyStore::Delegate { |
| 53 public: | 61 public: |
| 54 virtual ~MockComponentCloudPolicyStoreDelegate() {} | 62 virtual ~MockComponentCloudPolicyStoreDelegate() {} |
| 55 | 63 |
| 56 MOCK_METHOD0(OnComponentCloudPolicyStoreUpdated, void()); | 64 MOCK_METHOD0(OnComponentCloudPolicyStoreUpdated, void()); |
| 57 }; | 65 }; |
| 58 | 66 |
| 59 } // namespace | 67 } // namespace |
| 60 | 68 |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 TEST_F(ComponentCloudPolicyStoreTest, Purge) { | 283 TEST_F(ComponentCloudPolicyStoreTest, Purge) { |
| 276 // Store a valid policy. | 284 // Store a valid policy. |
| 277 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); | 285 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); |
| 278 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | 286 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); |
| 279 EXPECT_TRUE(store_->Store( | 287 EXPECT_TRUE(store_->Store( |
| 280 ns, CreateSerializedResponse(), TestPolicyHash(), kTestPolicy)); | 288 ns, CreateSerializedResponse(), TestPolicyHash(), kTestPolicy)); |
| 281 Mock::VerifyAndClearExpectations(&store_delegate_); | 289 Mock::VerifyAndClearExpectations(&store_delegate_); |
| 282 EXPECT_FALSE(IsEmpty()); | 290 EXPECT_FALSE(IsEmpty()); |
| 283 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); | 291 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); |
| 284 | 292 |
| 285 // Purge other namespaces. | 293 // Purge other components. |
| 286 std::set<std::string> keep; | 294 store_->Purge(POLICY_DOMAIN_EXTENSIONS, |
| 287 keep.insert(kTestExtension); | 295 base::Bind(&NotEqual, kTestExtension)); |
| 288 store_->Purge(POLICY_DOMAIN_EXTENSIONS, keep); | |
| 289 | 296 |
| 290 // The policy for |ns| is still served. | 297 // The policy for |ns| is still served. |
| 291 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); | 298 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); |
| 292 | 299 |
| 293 // Loading the store again will still see |ns|. | 300 // Loading the store again will still see |ns|. |
| 294 ComponentCloudPolicyStore another_store(&store_delegate_, cache_.get()); | 301 ComponentCloudPolicyStore another_store(&store_delegate_, cache_.get()); |
| 295 const PolicyBundle empty_bundle; | 302 const PolicyBundle empty_bundle; |
| 296 EXPECT_TRUE(another_store.policy().Equals(empty_bundle)); | 303 EXPECT_TRUE(another_store.policy().Equals(empty_bundle)); |
| 297 another_store.SetCredentials(ComponentPolicyBuilder::kFakeUsername, | 304 another_store.SetCredentials(ComponentPolicyBuilder::kFakeUsername, |
| 298 ComponentPolicyBuilder::kFakeToken); | 305 ComponentPolicyBuilder::kFakeToken); |
| 299 another_store.Load(); | 306 another_store.Load(); |
| 300 EXPECT_TRUE(another_store.policy().Equals(expected_bundle_)); | 307 EXPECT_TRUE(another_store.policy().Equals(expected_bundle_)); |
| 301 | 308 |
| 302 // Now purge everything. | 309 // Now purge everything. |
| 303 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); | 310 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); |
| 304 keep.clear(); | 311 store_->Purge(POLICY_DOMAIN_EXTENSIONS, base::Bind(&True)); |
| 305 store_->Purge(POLICY_DOMAIN_EXTENSIONS, keep); | |
| 306 Mock::VerifyAndClearExpectations(&store_delegate_); | 312 Mock::VerifyAndClearExpectations(&store_delegate_); |
| 307 | 313 |
| 308 // No policies are served anymore. | 314 // No policies are served anymore. |
| 309 EXPECT_TRUE(store_->policy().Equals(empty_bundle)); | 315 EXPECT_TRUE(store_->policy().Equals(empty_bundle)); |
| 310 | 316 |
| 311 // And they aren't loaded anymore either. | 317 // And they aren't loaded anymore either. |
| 312 ComponentCloudPolicyStore yet_another_store(&store_delegate_, cache_.get()); | 318 ComponentCloudPolicyStore yet_another_store(&store_delegate_, cache_.get()); |
| 313 yet_another_store.SetCredentials(ComponentPolicyBuilder::kFakeUsername, | 319 yet_another_store.SetCredentials(ComponentPolicyBuilder::kFakeUsername, |
| 314 ComponentPolicyBuilder::kFakeToken); | 320 ComponentPolicyBuilder::kFakeToken); |
| 315 yet_another_store.Load(); | 321 yet_another_store.Load(); |
| 316 EXPECT_TRUE(yet_another_store.policy().Equals(empty_bundle)); | 322 EXPECT_TRUE(yet_another_store.policy().Equals(empty_bundle)); |
| 317 } | 323 } |
| 318 | 324 |
| 319 } // namespace policy | 325 } // namespace policy |
| OLD | NEW |