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

Unified Diff: components/policy/core/common/cloud/component_cloud_policy_service_unittest.cc

Issue 756133002: Purge cache when the server removes the policy for an extension. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: components/policy/core/common/cloud/component_cloud_policy_service_unittest.cc
diff --git a/components/policy/core/common/cloud/component_cloud_policy_service_unittest.cc b/components/policy/core/common/cloud/component_cloud_policy_service_unittest.cc
index 605be1aca75e11fea1f3d3acf2bdda988165b701..77fa309f5feddbbb1ecffa352292f9d04380667c 100644
--- a/components/policy/core/common/cloud/component_cloud_policy_service_unittest.cc
+++ b/components/policy/core/common/cloud/component_cloud_policy_service_unittest.cc
@@ -154,7 +154,7 @@ class ComponentCloudPolicyServiceTest : public testing::Test {
base::RunLoop().RunUntilIdle();
}
- void Connect(size_t expected_namespaces_in_client) {
+ void Connect(bool expect_extensions_namespace) {
client_ = new MockCloudPolicyClient();
client_->SetDMToken(ComponentPolicyBuilder::kFakeToken);
EXPECT_EQ(0u, client_->namespaces_to_fetch_.size());
@@ -164,6 +164,7 @@ class ComponentCloudPolicyServiceTest : public testing::Test {
// |expected_namespaces_in_client| is the expected number of components
// that the ComponentCloudPolicyService will set at the |client_| at
// OnCoreConnected.
+ size_t expected_namespaces_in_client = expect_extensions_namespace ? 2 : 1;
EXPECT_EQ(expected_namespaces_in_client,
client_->namespaces_to_fetch_.size());
@@ -246,7 +247,7 @@ class ComponentCloudPolicyServiceTest : public testing::Test {
TEST_F(ComponentCloudPolicyServiceTest, InitializedAtConstructionTime) {
service_.reset();
- Connect(1u);
+ Connect(false);
LoadStore();
InitializeRegistry();
@@ -273,7 +274,7 @@ TEST_F(ComponentCloudPolicyServiceTest, InitializedAtConstructionTime) {
}
TEST_F(ComponentCloudPolicyServiceTest, InitializeStoreThenRegistry) {
- Connect(2u);
+ Connect(true);
EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0);
EXPECT_CALL(*client_, FetchPolicy()).Times(0);
@@ -295,7 +296,7 @@ TEST_F(ComponentCloudPolicyServiceTest, InitializeStoreThenRegistry) {
}
TEST_F(ComponentCloudPolicyServiceTest, InitializeRegistryThenStore) {
- Connect(2u);
+ Connect(true);
EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0);
EXPECT_CALL(*client_, FetchPolicy()).Times(0);
@@ -318,7 +319,7 @@ TEST_F(ComponentCloudPolicyServiceTest, InitializeRegistryThenStore) {
TEST_F(ComponentCloudPolicyServiceTest, InitializeWithCachedPolicy) {
PopulateCache();
- Connect(2u);
+ Connect(true);
EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
EXPECT_CALL(*client_, FetchPolicy()).Times(0);
@@ -343,7 +344,7 @@ TEST_F(ComponentCloudPolicyServiceTest, InitializeWithCachedPolicy) {
}
TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) {
- Connect(2u);
+ Connect(true);
// Initialize the store and create the backend.
// A refresh is not needed, because no components are registered yet.
EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
@@ -370,6 +371,8 @@ TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) {
client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
kTestExtension),
*CreateResponse());
+ client_->namespaces_requested_.insert(
+ PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, ""));
service_->OnPolicyFetched(client_);
RunUntilIdle();
@@ -393,7 +396,7 @@ TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) {
}
TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) {
- Connect(2u);
+ Connect(true);
// Insert data in the cache.
PopulateCache();
registry_.RegisterComponent(
@@ -456,13 +459,15 @@ TEST_F(ComponentCloudPolicyServiceTest, SignInAfterStartup) {
Mock::VerifyAndClearExpectations(&delegate_);
// Now signin. A fetch will be requested for the new extension.
- Connect(2u);
+ Connect(true);
// Send the response to the service. The response data will be ignored,
// because the store doesn't have the updated credentials yet.
client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
kTestExtension),
*CreateResponse());
+ client_->namespaces_requested_.insert(
+ PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, ""));
service_->OnPolicyFetched(client_);
RunUntilIdle();
@@ -518,7 +523,7 @@ TEST_F(ComponentCloudPolicyServiceTest, SignOut) {
ASSERT_EQ(1u, contents.size());
// Now sign in.
- Connect(2u);
+ Connect(true);
// Signing out removes all of the component policies from the service and
// from the cache. It does not trigger a refresh.
@@ -562,4 +567,66 @@ TEST_F(ComponentCloudPolicyServiceTest, LoadInvalidPolicyFromCache) {
EXPECT_TRUE(service_->policy().Equals(expected_bundle));
}
+TEST_F(ComponentCloudPolicyServiceTest, PurgeWhenServerRemovesPolicy) {
+ // Initialize with cached policy.
+ PopulateCache();
+ Connect(true);
+
+ EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
+ EXPECT_CALL(*client_, FetchPolicy()).Times(0);
+ registry_.RegisterComponent(
+ PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2),
+ CreateTestSchema());
+ InitializeRegistry();
+ LoadStore();
+ Mock::VerifyAndClearExpectations(client_);
+ Mock::VerifyAndClearExpectations(&delegate_);
+
+ EXPECT_TRUE(service_->is_initialized());
+ EXPECT_EQ(2u, client_->namespaces_to_fetch_.size());
+
+ // Verify that policy for 2 extensions has been loaded from the cache.
+ std::map<std::string, std::string> contents;
+ cache_->LoadAllSubkeys("extension-policy", &contents);
+ ASSERT_EQ(2u, contents.size());
+ EXPECT_TRUE(ContainsKey(contents, kTestExtension));
+ EXPECT_TRUE(ContainsKey(contents, kTestExtension2));
+
+ PolicyBundle expected_bundle;
+ const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
+ expected_bundle.Get(ns).CopyFrom(expected_policy_);
+ const PolicyNamespace ns2(POLICY_DOMAIN_EXTENSIONS, kTestExtension2);
+ expected_bundle.Get(ns2).CopyFrom(expected_policy_);
+ EXPECT_TRUE(service_->policy().Equals(expected_bundle));
+
+ // Receive an updated fetch response from the server. There is no response for
+ // extension 2, so it will be dropped from the cache. This triggers an
+ // immediate notification to the delegate.
+ EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
+ client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
+ kTestExtension),
+ *CreateResponse());
+ client_->namespaces_requested_.insert(
+ PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, ""));
+ service_->OnPolicyFetched(client_);
+ RunUntilIdle();
+ Mock::VerifyAndClearExpectations(&delegate_);
+
+ // That should have triggered the download fetch for the first extension.
+ net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
+ ASSERT_TRUE(fetcher);
+
+ // The cache should have dropped the entries for the second extension.
+ contents.clear();
+ cache_->LoadAllSubkeys("extension-policy", &contents);
+ ASSERT_EQ(1u, contents.size());
+ EXPECT_TRUE(ContainsKey(contents, kTestExtension));
+ EXPECT_FALSE(ContainsKey(contents, kTestExtension2));
+
+ // And the service isn't publishing policy for the second extension anymore.
+ expected_bundle.Clear();
+ expected_bundle.Get(ns).CopyFrom(expected_policy_);
+ EXPECT_TRUE(service_->policy().Equals(expected_bundle));
+}
+
} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698