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

Side by Side Diff: chrome/browser/policy/cloud/component_cloud_policy_service_unittest.cc

Issue 70863002: Refactored the ComponentCloudPolicyService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_service.h" 5 #include "chrome/browser/policy/cloud/component_cloud_policy_service.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/pickle.h"
11 #include "base/run_loop.h" 10 #include "base/run_loop.h"
12 #include "base/sha1.h" 11 #include "base/sha1.h"
13 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
14 #include "base/stl_util.h" 13 #include "base/stl_util.h"
15 #include "base/values.h" 14 #include "base/values.h"
16 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" 15 #include "chrome/browser/policy/cloud/cloud_policy_constants.h"
17 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" 16 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h"
18 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" 17 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h"
19 #include "chrome/browser/policy/cloud/policy_builder.h" 18 #include "chrome/browser/policy/cloud/policy_builder.h"
20 #include "chrome/browser/policy/cloud/resource_cache.h" 19 #include "chrome/browser/policy/cloud/resource_cache.h"
21 #include "chrome/browser/policy/external_data_fetcher.h" 20 #include "chrome/browser/policy/external_data_fetcher.h"
22 #include "chrome/browser/policy/policy_map.h" 21 #include "chrome/browser/policy/policy_map.h"
23 #include "chrome/browser/policy/policy_types.h" 22 #include "chrome/browser/policy/policy_types.h"
24 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" 23 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h"
25 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" 24 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
26 #include "chrome/browser/policy/schema_registry.h" 25 #include "chrome/browser/policy/schema_map.h"
27 #include "components/policy/core/common/schema.h" 26 #include "components/policy/core/common/schema.h"
28 #include "net/url_request/test_url_fetcher_factory.h" 27 #include "net/url_request/test_url_fetcher_factory.h"
29 #include "net/url_request/url_fetcher_delegate.h" 28 #include "net/url_request/url_fetcher_delegate.h"
30 #include "net/url_request/url_request_context.h" 29 #include "net/url_request/url_request_context.h"
31 #include "net/url_request/url_request_context_getter.h" 30 #include "net/url_request/url_request_context_getter.h"
32 #include "testing/gmock/include/gmock/gmock.h" 31 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
34 33
35 namespace em = enterprise_management; 34 namespace em = enterprise_management;
36 35
37 using testing::Mock; 36 using testing::Mock;
38 37
39 namespace policy { 38 namespace policy {
40 39
41 namespace { 40 namespace {
42 41
43 const char kTestExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 42 const char kTestExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
44 const char kTestExtension2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; 43 const char kTestExtension2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
45 const char kTestDownload[] = "http://example.com/getpolicy?id=123"; 44 const char kTestDownload[] = "http://example.com/getpolicy?id=123";
46 const char kTestDownload2[] = "http://example.com/getpolicy?id=456";
47 45
48 const char kTestPolicy[] = 46 const char kTestPolicy[] =
49 "{" 47 "{"
50 " \"Name\": {" 48 " \"Name\": {"
51 " \"Value\": \"disabled\"" 49 " \"Value\": \"disabled\""
52 " }," 50 " },"
53 " \"Second\": {" 51 " \"Second\": {"
54 " \"Value\": \"maybe\"," 52 " \"Value\": \"maybe\","
55 " \"Level\": \"Recommended\"" 53 " \"Level\": \"Recommended\""
56 " }" 54 " }"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 }; 92 };
95 93
96 } // namespace 94 } // namespace
97 95
98 class ComponentCloudPolicyServiceTest : public testing::Test { 96 class ComponentCloudPolicyServiceTest : public testing::Test {
99 protected: 97 protected:
100 ComponentCloudPolicyServiceTest() {} 98 ComponentCloudPolicyServiceTest() {}
101 99
102 virtual void SetUp() OVERRIDE { 100 virtual void SetUp() OVERRIDE {
103 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
102
104 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy()); 103 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy());
104 request_context_ =
105 new TestURLRequestContextGetter(loop_.message_loop_proxy());
105 service_.reset(new ComponentCloudPolicyService( 106 service_.reset(new ComponentCloudPolicyService(
106 &delegate_, 107 &delegate_,
108 &registry_,
107 &store_, 109 &store_,
108 make_scoped_ptr(cache_), 110 make_scoped_ptr(cache_),
111 &client_,
112 request_context_,
109 loop_.message_loop_proxy(), 113 loop_.message_loop_proxy(),
110 loop_.message_loop_proxy())); 114 loop_.message_loop_proxy()));
111 115
112 builder_.policy_data().set_policy_type( 116 builder_.policy_data().set_policy_type(
113 dm_protocol::kChromeExtensionPolicyType); 117 dm_protocol::kChromeExtensionPolicyType);
114 builder_.policy_data().set_settings_entity_id(kTestExtension); 118 builder_.policy_data().set_settings_entity_id(kTestExtension);
115 builder_.payload().set_download_url(kTestDownload); 119 builder_.payload().set_download_url(kTestDownload);
116 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy)); 120 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy));
117 121
118 expected_policy_.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 122 expected_policy_.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
119 base::Value::CreateStringValue("disabled"), NULL); 123 base::Value::CreateStringValue("disabled"), NULL);
120 expected_policy_.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 124 expected_policy_.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
121 base::Value::CreateStringValue("maybe"), NULL); 125 base::Value::CreateStringValue("maybe"), NULL);
122 126
123 // A NULL |request_context_| is enough to construct the updater, but 127 EXPECT_EQ(0u, client_.namespaces_to_fetch_.size());
124 // ComponentCloudPolicyService::Backend::LoadStore() tests the pointer when
125 // Connect() is called before the store was loaded.
126 request_context_ =
127 new TestURLRequestContextGetter(loop_.message_loop_proxy());
128 } 128 }
129 129
130 virtual void TearDown() OVERRIDE { 130 virtual void TearDown() OVERRIDE {
131 // The service cleans up its backend on the FILE thread. 131 // The service cleans up its backend on the background thread.
132 service_.reset(); 132 service_.reset();
133 RunUntilIdle(); 133 RunUntilIdle();
134 } 134 }
135 135
136 void RunUntilIdle() { 136 void RunUntilIdle() {
137 base::RunLoop().RunUntilIdle(); 137 base::RunLoop().RunUntilIdle();
138 } 138 }
139 139
140 void LoadStore() { 140 void LoadStore() {
141 EXPECT_FALSE(store_.is_initialized()); 141 EXPECT_FALSE(store_.is_initialized());
142 EXPECT_FALSE(service_->is_initialized());
143 142
144 em::PolicyData* data = new em::PolicyData(); 143 em::PolicyData* data = new em::PolicyData();
145 data->set_username(ComponentPolicyBuilder::kFakeUsername); 144 data->set_username(ComponentPolicyBuilder::kFakeUsername);
146 data->set_request_token(ComponentPolicyBuilder::kFakeToken); 145 data->set_request_token(ComponentPolicyBuilder::kFakeToken);
147 store_.policy_.reset(data); 146 store_.policy_.reset(data);
148 147
149 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
150 store_.NotifyStoreLoaded(); 148 store_.NotifyStoreLoaded();
151 RunUntilIdle(); 149 RunUntilIdle();
152 Mock::VerifyAndClearExpectations(&delegate_); 150 EXPECT_TRUE(store_.is_initialized());
153 EXPECT_TRUE(service_->is_initialized()); 151 }
152
153 void InitializeRegistry() {
154 registry_.RegisterComponent(
155 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
156 CreateTestSchema());
157 registry_.SetReady(POLICY_DOMAIN_CHROME);
158 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS);
154 } 159 }
155 160
156 void PopulateCache() { 161 void PopulateCache() {
157 Pickle pickle;
158 pickle.WriteString(kTestExtension);
159 pickle.WriteString(kTestExtension2);
160 std::string data(reinterpret_cast<const char*>(pickle.data()),
161 pickle.size());
162 EXPECT_TRUE(
163 cache_->Store(ComponentCloudPolicyService::kComponentNamespaceCache,
164 dm_protocol::kChromeExtensionPolicyType,
165 data));
166
167 EXPECT_TRUE(cache_->Store( 162 EXPECT_TRUE(cache_->Store(
168 "extension-policy", kTestExtension, CreateSerializedResponse())); 163 "extension-policy", kTestExtension, CreateSerializedResponse()));
169 EXPECT_TRUE( 164 EXPECT_TRUE(
170 cache_->Store("extension-policy-data", kTestExtension, kTestPolicy)); 165 cache_->Store("extension-policy-data", kTestExtension, kTestPolicy));
171 166
172 builder_.policy_data().set_settings_entity_id(kTestExtension2); 167 builder_.policy_data().set_settings_entity_id(kTestExtension2);
173 EXPECT_TRUE(cache_->Store( 168 EXPECT_TRUE(cache_->Store(
174 "extension-policy", kTestExtension2, CreateSerializedResponse())); 169 "extension-policy", kTestExtension2, CreateSerializedResponse()));
175 EXPECT_TRUE( 170 EXPECT_TRUE(
176 cache_->Store("extension-policy-data", kTestExtension2, kTestPolicy)); 171 cache_->Store("extension-policy-data", kTestExtension2, kTestPolicy));
177
178 EXPECT_TRUE(cache_->Store("unrelated", "stuff", "here"));
179 } 172 }
180 173
181 scoped_ptr<em::PolicyFetchResponse> CreateResponse() { 174 scoped_ptr<em::PolicyFetchResponse> CreateResponse() {
182 builder_.Build(); 175 builder_.Build();
183 return make_scoped_ptr(new em::PolicyFetchResponse(builder_.policy())); 176 return make_scoped_ptr(new em::PolicyFetchResponse(builder_.policy()));
184 } 177 }
185 178
186 std::string CreateSerializedResponse() { 179 std::string CreateSerializedResponse() {
187 builder_.Build(); 180 builder_.Build();
188 return builder_.GetBlob(); 181 return builder_.GetBlob();
(...skipping 15 matching lines...) Expand all
204 // is destroyed. 197 // is destroyed.
205 ResourceCache* cache_; 198 ResourceCache* cache_;
206 MockCloudPolicyClient client_; 199 MockCloudPolicyClient client_;
207 MockCloudPolicyStore store_; 200 MockCloudPolicyStore store_;
208 SchemaRegistry registry_; 201 SchemaRegistry registry_;
209 scoped_ptr<ComponentCloudPolicyService> service_; 202 scoped_ptr<ComponentCloudPolicyService> service_;
210 ComponentPolicyBuilder builder_; 203 ComponentPolicyBuilder builder_;
211 PolicyMap expected_policy_; 204 PolicyMap expected_policy_;
212 }; 205 };
213 206
214 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithoutCredentials) { 207 TEST_F(ComponentCloudPolicyServiceTest, InitializedAtConstructionTime) {
215 EXPECT_FALSE(service_->is_initialized()); 208 service_.reset();
216 // Run the background task to initialize the backend. 209 LoadStore();
217 RunUntilIdle(); 210 InitializeRegistry();
218 // Still waiting for the |store_|. 211
212 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy());
213 service_.reset(new ComponentCloudPolicyService(&delegate_,
214 &registry_,
215 &store_,
216 make_scoped_ptr(cache_),
217 &client_,
218 request_context_,
219 loop_.message_loop_proxy(),
220 loop_.message_loop_proxy()));
219 EXPECT_FALSE(service_->is_initialized()); 221 EXPECT_FALSE(service_->is_initialized());
220 222
221 // Initialize with a store without credentials. 223 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
222 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 224 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
223 store_.NotifyStoreLoaded();
224 RunUntilIdle(); 225 RunUntilIdle();
226 Mock::VerifyAndClearExpectations(&delegate_);
227
225 EXPECT_TRUE(service_->is_initialized()); 228 EXPECT_TRUE(service_->is_initialized());
226 Mock::VerifyAndClearExpectations(&delegate_); 229 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size());
227 const PolicyBundle empty_bundle; 230 const PolicyBundle empty_bundle;
228 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); 231 EXPECT_TRUE(service_->policy().Equals(empty_bundle));
229 } 232 }
230 233
231 TEST_F(ComponentCloudPolicyServiceTest, InitializationWithCachedComponents) { 234 TEST_F(ComponentCloudPolicyServiceTest, InitializeStoreThenRegistry) {
232 // Store a previous list of components. 235 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0);
233 Pickle pickle; 236 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0);
234 pickle.WriteString("aa"); 237 LoadStore();
235 pickle.WriteString("bb"); 238 Mock::VerifyAndClearExpectations(&delegate_);
236 pickle.WriteString("cc"); 239 EXPECT_FALSE(service_->is_initialized());
237 std::string data(reinterpret_cast<const char*>(pickle.data()), pickle.size());
238 EXPECT_TRUE(
239 cache_->Store(ComponentCloudPolicyService::kComponentNamespaceCache,
240 dm_protocol::kChromeExtensionPolicyType,
241 data));
242 // Store some garbage in another domain; it won't be fetched.
243 EXPECT_TRUE(cache_->Store(
244 ComponentCloudPolicyService::kComponentNamespaceCache, "garbage", data));
245 240
246 // Connect a client before initialization is complete. 241 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
247 service_->Connect(&client_, request_context_); 242 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
248 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); 243 InitializeRegistry();
249 244 RunUntilIdle();
250 // Now load the backend. 245 Mock::VerifyAndClearExpectations(&delegate_);
251 LoadStore(); 246 EXPECT_TRUE(service_->is_initialized());
252 247 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size());
253 // The cached namespaces were added to the client. 248 const PolicyBundle empty_bundle;
254 std::set<PolicyNamespaceKey> set; 249 EXPECT_TRUE(service_->policy().Equals(empty_bundle));
255 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, "aa"));
256 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, "bb"));
257 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, "cc"));
258 EXPECT_EQ(set, client_.namespaces_to_fetch_);
259
260 // And the bad components were wiped from the cache.
261 std::map<std::string, std::string> contents;
262 cache_->LoadAllSubkeys(ComponentCloudPolicyService::kComponentNamespaceCache,
263 &contents);
264 ASSERT_EQ(1u, contents.size());
265 EXPECT_EQ(std::string(dm_protocol::kChromeExtensionPolicyType),
266 contents.begin()->first);
267 } 250 }
268 251
269 TEST_F(ComponentCloudPolicyServiceTest, ConnectAfterRegister) { 252 TEST_F(ComponentCloudPolicyServiceTest, InitializeRegistryThenStore) {
270 // Add some components. 253 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0);
254 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0);
255 InitializeRegistry();
256 RunUntilIdle();
257 Mock::VerifyAndClearExpectations(&delegate_);
258 EXPECT_FALSE(service_->is_initialized());
259
260 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
261 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
262 LoadStore();
263 Mock::VerifyAndClearExpectations(&delegate_);
264 EXPECT_TRUE(service_->is_initialized());
265 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size());
266 const PolicyBundle empty_bundle;
267 EXPECT_TRUE(service_->policy().Equals(empty_bundle));
268 }
269
270 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithCachedPolicy) {
271 PopulateCache();
272
273 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
274 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
275 InitializeRegistry();
276 LoadStore();
277 Mock::VerifyAndClearExpectations(&delegate_);
278
279 EXPECT_TRUE(service_->is_initialized());
280 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size());
281
282 // kTestExtension2 is not in the registry so it was dropped.
283 std::map<std::string, std::string> contents;
284 cache_->LoadAllSubkeys("extension-policy", &contents);
285 ASSERT_EQ(1u, contents.size());
286 EXPECT_EQ(kTestExtension, contents.begin()->first);
287
288 PolicyBundle expected_bundle;
289 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
290 expected_bundle.Get(ns).CopyFrom(expected_policy_);
291 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
292 }
293
294 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) {
295 // Initialize the store and create the backend.
296 // A refresh is not needed, because no components are registered yet.
297 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0);
298 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
299 registry_.SetReady(POLICY_DOMAIN_CHROME);
300 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS);
301 LoadStore();
302 Mock::VerifyAndClearExpectations(&delegate_);
303 EXPECT_TRUE(service_->is_initialized());
304
305 // Register the components to fetch.
306 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
271 registry_.RegisterComponent( 307 registry_.RegisterComponent(
272 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), 308 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
273 CreateTestSchema()); 309 CreateTestSchema());
274 registry_.RegisterComponent( 310 RunUntilIdle();
275 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2),
276 CreateTestSchema());
277 service_->OnSchemasUpdated(registry_.schema_map());
278
279 // Now connect the client.
280 EXPECT_TRUE(client_.namespaces_to_fetch_.empty());
281 service_->Connect(&client_, request_context_);
282 EXPECT_TRUE(client_.namespaces_to_fetch_.empty());
283
284 // It receives the namespaces once the backend is initialized.
285 LoadStore();
286 std::set<PolicyNamespaceKey> set;
287 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
288 kTestExtension));
289 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
290 kTestExtension2));
291 EXPECT_EQ(set, client_.namespaces_to_fetch_);
292
293 // The current components were also persisted to the cache.
294 std::map<std::string, std::string> contents;
295 cache_->LoadAllSubkeys(ComponentCloudPolicyService::kComponentNamespaceCache,
296 &contents);
297 ASSERT_EQ(1u, contents.size());
298 EXPECT_EQ(std::string(dm_protocol::kChromeExtensionPolicyType),
299 contents.begin()->first);
300 const std::string data(contents.begin()->second);
301 Pickle pickle(data.data(), data.size());
302 PickleIterator pickit(pickle);
303 std::string value0;
304 std::string value1;
305 std::string tmp;
306 ASSERT_TRUE(pickit.ReadString(&value0));
307 ASSERT_TRUE(pickit.ReadString(&value1));
308 EXPECT_FALSE(pickit.ReadString(&tmp));
309 std::set<std::string> unpickled;
310 unpickled.insert(value0);
311 unpickled.insert(value1);
312
313 std::set<std::string> expected_components;
314 expected_components.insert(kTestExtension);
315 expected_components.insert(kTestExtension2);
316
317 EXPECT_EQ(expected_components, unpickled);
318 }
319
320 TEST_F(ComponentCloudPolicyServiceTest, StoreReadyAfterConnectAndRegister) {
321 // Add some previous data to the cache.
322 PopulateCache();
323
324 // Add some components.
325 registry_.RegisterComponent(
326 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
327 CreateTestSchema());
328 service_->OnSchemasUpdated(registry_.schema_map());
329
330 // And connect the client. Make the client have some policies, with a new
331 // download_url.
332 builder_.payload().set_download_url(kTestDownload2);
333 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
334 kTestExtension),
335 *CreateResponse());
336 service_->Connect(&client_, request_context_);
337
338 // Now make the store ready.
339 LoadStore();
340
341 // The components that were registed before have their caches purged.
342 std::map<std::string, std::string> contents;
343 cache_->LoadAllSubkeys("extension-policy", &contents);
344 EXPECT_EQ(1u, contents.size()); // kTestExtension2 was purged.
345 EXPECT_TRUE(ContainsKey(contents, kTestExtension));
346 cache_->LoadAllSubkeys("unrelated", &contents);
347 EXPECT_EQ(1u, contents.size()); // unrelated keys are not purged.
348 EXPECT_TRUE(ContainsKey(contents, "stuff"));
349
350 // The policy responses that the client already had start updating.
351 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
352 ASSERT_TRUE(fetcher);
353 // Expect the URL of the client's PolicyFetchResponse, not the cached URL.
354 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
355 }
356
357 TEST_F(ComponentCloudPolicyServiceTest, ConnectThenRegisterThenStoreReady) {
358 // Connect right after creating the service.
359 service_->Connect(&client_, request_context_);
360
361 // Now register the current components, before the backend has been
362 // initialized.
363 EXPECT_TRUE(client_.namespaces_to_fetch_.empty());
364 registry_.RegisterComponent(
365 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
366 CreateTestSchema());
367 service_->OnSchemasUpdated(registry_.schema_map());
368 EXPECT_TRUE(client_.namespaces_to_fetch_.empty());
369
370 // Now load the store. The client gets the namespaces.
371 LoadStore();
372 std::set<PolicyNamespaceKey> set;
373 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
374 kTestExtension));
375 EXPECT_EQ(set, client_.namespaces_to_fetch_);
376 }
377
378 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) {
379 // Initialize the store and create the backend, and connect the client.
380 LoadStore();
381 // A refresh is not needed, because no components were found.
382 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0);
383 service_->Connect(&client_, request_context_);
384 Mock::VerifyAndClearExpectations(&delegate_);
385
386 // Register the components to fetch.
387 registry_.RegisterComponent(
388 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
389 CreateTestSchema());
390 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
391 service_->OnSchemasUpdated(registry_.schema_map());
392 Mock::VerifyAndClearExpectations(&delegate_); 311 Mock::VerifyAndClearExpectations(&delegate_);
393 312
394 // Send back a fake policy fetch response. 313 // Send back a fake policy fetch response.
395 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, 314 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
396 kTestExtension), 315 kTestExtension),
397 *CreateResponse()); 316 *CreateResponse());
398 service_->OnPolicyFetched(&client_); 317 service_->OnPolicyFetched(&client_);
399 RunUntilIdle(); 318 RunUntilIdle();
400 319
401 // That should have triggered the download fetch. 320 // That should have triggered the download fetch.
402 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 321 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
403 ASSERT_TRUE(fetcher); 322 ASSERT_TRUE(fetcher);
404 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); 323 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
405 fetcher->set_response_code(200); 324 fetcher->set_response_code(200);
406 fetcher->SetResponseString(kTestPolicy); 325 fetcher->SetResponseString(kTestPolicy);
407 fetcher->delegate()->OnURLFetchComplete(fetcher); 326 fetcher->delegate()->OnURLFetchComplete(fetcher);
408 327
409 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 328 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
410 RunUntilIdle(); 329 RunUntilIdle();
411 Mock::VerifyAndClearExpectations(&delegate_); 330 Mock::VerifyAndClearExpectations(&delegate_);
412 331
413 // The policy is now being served. 332 // The policy is now being served.
414 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 333 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
415 PolicyBundle expected_bundle; 334 PolicyBundle expected_bundle;
416 expected_bundle.Get(ns).CopyFrom(expected_policy_); 335 expected_bundle.Get(ns).CopyFrom(expected_policy_);
417 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 336 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
418 } 337 }
419 338
420 TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) { 339 TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) {
421 // Insert data in the cache. 340 // Insert data in the cache.
422 PopulateCache(); 341 PopulateCache();
342 registry_.RegisterComponent(
343 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2),
344 CreateTestSchema());
345 InitializeRegistry();
423 346
424 // Load the initial cache. 347 // Load the initial cache.
348 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
349 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
425 LoadStore(); 350 LoadStore();
426
427 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
428 service_->Connect(&client_, request_context_);
429 Mock::VerifyAndClearExpectations(&delegate_); 351 Mock::VerifyAndClearExpectations(&delegate_);
430 352
431 PolicyBundle expected_bundle; 353 PolicyBundle expected_bundle;
432 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 354 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
433 expected_bundle.Get(ns).CopyFrom(expected_policy_); 355 expected_bundle.Get(ns).CopyFrom(expected_policy_);
434 ns.component_id = kTestExtension2; 356 ns.component_id = kTestExtension2;
435 expected_bundle.Get(ns).CopyFrom(expected_policy_); 357 expected_bundle.Get(ns).CopyFrom(expected_policy_);
436 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 358 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
437 359
438 // Now purge one of the extensions. 360 // Now purge one of the extensions.
439 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 361 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
440 // The service will start updating the components that are registered, which 362 registry_.UnregisterComponent(
441 // starts by fetching policy for them. 363 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension));
442 registry_.RegisterComponent(
443 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2),
444 CreateTestSchema());
445 service_->OnSchemasUpdated(registry_.schema_map());
446 RunUntilIdle(); 364 RunUntilIdle();
447 Mock::VerifyAndClearExpectations(&delegate_); 365 Mock::VerifyAndClearExpectations(&delegate_);
448 366
449 ns.component_id = kTestExtension; 367 ns.component_id = kTestExtension;
450 expected_bundle.Get(ns).Clear(); 368 expected_bundle.Get(ns).Clear();
451 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 369 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
452 370
453 std::map<std::string, std::string> contents; 371 std::map<std::string, std::string> contents;
454 cache_->LoadAllSubkeys("extension-policy", &contents); 372 cache_->LoadAllSubkeys("extension-policy", &contents);
455 EXPECT_EQ(1u, contents.size()); 373 EXPECT_EQ(1u, contents.size());
456 EXPECT_TRUE(ContainsKey(contents, kTestExtension2)); 374 EXPECT_TRUE(ContainsKey(contents, kTestExtension2));
457 cache_->LoadAllSubkeys("unrelated", &contents);
458 EXPECT_EQ(1u, contents.size());
459 EXPECT_TRUE(ContainsKey(contents, "stuff"));
460 } 375 }
461 376
462 TEST_F(ComponentCloudPolicyServiceTest, UpdateCredentials) { 377 TEST_F(ComponentCloudPolicyServiceTest, SignInAfterStartup) {
378 registry_.SetReady(POLICY_DOMAIN_CHROME);
379 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS);
380
463 // Do the same as LoadStore() but without the initial credentials. 381 // Do the same as LoadStore() but without the initial credentials.
464 EXPECT_FALSE(store_.is_initialized()); 382 EXPECT_FALSE(store_.is_initialized());
465 EXPECT_FALSE(service_->is_initialized()); 383 EXPECT_FALSE(service_->is_initialized());
466 store_.policy_.reset(new em::PolicyData()); // No credentials. 384 store_.policy_.reset(new em::PolicyData()); // No credentials.
467 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 385 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
468 store_.NotifyStoreLoaded(); 386 store_.NotifyStoreLoaded();
469 RunUntilIdle(); 387 RunUntilIdle();
470 Mock::VerifyAndClearExpectations(&delegate_); 388 Mock::VerifyAndClearExpectations(&delegate_);
471 EXPECT_TRUE(service_->is_initialized()); 389 EXPECT_TRUE(service_->is_initialized());
472 390
473 // Connect the client and register an extension. 391 // Register an extension.
474 service_->Connect(&client_, request_context_);
475 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); 392 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
476 registry_.RegisterComponent( 393 registry_.RegisterComponent(
477 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), 394 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
478 CreateTestSchema()); 395 CreateTestSchema());
479 service_->OnSchemasUpdated(registry_.schema_map()); 396 RunUntilIdle();
480 Mock::VerifyAndClearExpectations(&delegate_); 397 Mock::VerifyAndClearExpectations(&delegate_);
481 398
482 // Send the response to the service. The response data will be rejected, 399 // Send the response to the service. The response data will be ignored,
483 // because the store doesn't have the updated credentials yet. 400 // because the store doesn't have the updated credentials yet.
484 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, 401 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
485 kTestExtension), 402 kTestExtension),
486 *CreateResponse()); 403 *CreateResponse());
487 service_->OnPolicyFetched(&client_); 404 service_->OnPolicyFetched(&client_);
488 RunUntilIdle(); 405 RunUntilIdle();
489 406
490 // The policy was not verified, and no download is started. 407 // The policy was ignored, and no download is started.
491 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 408 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
492 EXPECT_FALSE(fetcher); 409 EXPECT_FALSE(fetcher);
493 410
494 // Now update the |store_| with the updated policy, which includes 411 // Now update the |store_| with the updated policy, which includes
495 // credentials. The responses in the |client_| will be reloaded. 412 // credentials. The responses in the |client_| will be reloaded.
496 em::PolicyData* data = new em::PolicyData(); 413 em::PolicyData* data = new em::PolicyData();
497 data->set_username(ComponentPolicyBuilder::kFakeUsername); 414 data->set_username(ComponentPolicyBuilder::kFakeUsername);
498 data->set_request_token(ComponentPolicyBuilder::kFakeToken); 415 data->set_request_token(ComponentPolicyBuilder::kFakeToken);
499 store_.policy_.reset(data); 416 store_.policy_.reset(data);
500 store_.NotifyStoreLoaded(); 417 store_.NotifyStoreLoaded();
(...skipping 12 matching lines...) Expand all
513 Mock::VerifyAndClearExpectations(&delegate_); 430 Mock::VerifyAndClearExpectations(&delegate_);
514 431
515 // The policy is now being served. 432 // The policy is now being served.
516 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 433 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
517 PolicyBundle expected_bundle; 434 PolicyBundle expected_bundle;
518 expected_bundle.Get(ns).CopyFrom(expected_policy_); 435 expected_bundle.Get(ns).CopyFrom(expected_policy_);
519 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 436 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
520 } 437 }
521 438
522 } // namespace policy 439 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698