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

Side by Side Diff: components/policy/core/common/cloud/component_cloud_policy_service_unittest.cc

Issue 337053005: Precache policy-for-extensions for device-local accounts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed ios tests Created 6 years, 6 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) 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 "components/policy/core/common/cloud/component_cloud_policy_service.h" 5 #include "components/policy/core/common/cloud/component_cloud_policy_service.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 core_.Connect(scoped_ptr<CloudPolicyClient>(client_)); 158 core_.Connect(scoped_ptr<CloudPolicyClient>(client_));
159 159
160 // |expected_namespaces_in_client| is the expected number of components 160 // |expected_namespaces_in_client| is the expected number of components
161 // that the ComponentCloudPolicyService will set at the |client_| at 161 // that the ComponentCloudPolicyService will set at the |client_| at
162 // OnCoreConnected. 162 // OnCoreConnected.
163 EXPECT_EQ(expected_namespaces_in_client, 163 EXPECT_EQ(expected_namespaces_in_client,
164 client_->namespaces_to_fetch_.size()); 164 client_->namespaces_to_fetch_.size());
165 165
166 // Also initialize the refresh scheduler, so that calls to 166 // Also initialize the refresh scheduler, so that calls to
167 // core()->RefreshSoon() trigger a FetchPolicy() call on the mock |client_|. 167 // core()->RefreshSoon() trigger a FetchPolicy() call on the mock |client_|.
168 // The |service_| should never trigger new fetches.
168 EXPECT_CALL(*client_, FetchPolicy()); 169 EXPECT_CALL(*client_, FetchPolicy());
169 core_.StartRefreshScheduler(); 170 core_.StartRefreshScheduler();
170 RunUntilIdle(); 171 RunUntilIdle();
171 Mock::VerifyAndClearExpectations(client_); 172 Mock::VerifyAndClearExpectations(client_);
172 } 173 }
173 174
174 void LoadStore() { 175 void LoadStore() {
175 EXPECT_FALSE(store_.is_initialized()); 176 EXPECT_FALSE(store_.is_initialized());
176 177
177 em::PolicyData* data = new em::PolicyData(); 178 em::PolicyData* data = new em::PolicyData();
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 service_.reset(new ComponentCloudPolicyService(&delegate_, 250 service_.reset(new ComponentCloudPolicyService(&delegate_,
250 &registry_, 251 &registry_,
251 &core_, 252 &core_,
252 make_scoped_ptr(cache_), 253 make_scoped_ptr(cache_),
253 request_context_, 254 request_context_,
254 loop_.message_loop_proxy(), 255 loop_.message_loop_proxy(),
255 loop_.message_loop_proxy())); 256 loop_.message_loop_proxy()));
256 EXPECT_FALSE(service_->is_initialized()); 257 EXPECT_FALSE(service_->is_initialized());
257 258
258 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 259 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
259 EXPECT_CALL(*client_, FetchPolicy()); 260 EXPECT_CALL(*client_, FetchPolicy()).Times(0);
260 RunUntilIdle(); 261 RunUntilIdle();
261 Mock::VerifyAndClearExpectations(&client_); 262 Mock::VerifyAndClearExpectations(&client_);
262 Mock::VerifyAndClearExpectations(&delegate_); 263 Mock::VerifyAndClearExpectations(&delegate_);
263 264
264 EXPECT_TRUE(service_->is_initialized()); 265 EXPECT_TRUE(service_->is_initialized());
265 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size()); 266 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size());
266 const PolicyBundle empty_bundle; 267 const PolicyBundle empty_bundle;
267 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); 268 EXPECT_TRUE(service_->policy().Equals(empty_bundle));
268 } 269 }
269 270
270 TEST_F(ComponentCloudPolicyServiceTest, InitializeStoreThenRegistry) { 271 TEST_F(ComponentCloudPolicyServiceTest, InitializeStoreThenRegistry) {
271 Connect(1u); 272 Connect(2u);
272 273
273 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0); 274 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0);
274 EXPECT_CALL(*client_, FetchPolicy()).Times(0); 275 EXPECT_CALL(*client_, FetchPolicy()).Times(0);
275 LoadStore(); 276 LoadStore();
276 Mock::VerifyAndClearExpectations(client_); 277 Mock::VerifyAndClearExpectations(client_);
277 Mock::VerifyAndClearExpectations(&delegate_); 278 Mock::VerifyAndClearExpectations(&delegate_);
278 EXPECT_FALSE(service_->is_initialized()); 279 EXPECT_FALSE(service_->is_initialized());
279 280
280 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 281 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
281 EXPECT_CALL(*client_, FetchPolicy()); 282 EXPECT_CALL(*client_, FetchPolicy()).Times(0);
282 InitializeRegistry(); 283 InitializeRegistry();
283 RunUntilIdle(); 284 RunUntilIdle();
284 Mock::VerifyAndClearExpectations(client_); 285 Mock::VerifyAndClearExpectations(client_);
285 Mock::VerifyAndClearExpectations(&delegate_); 286 Mock::VerifyAndClearExpectations(&delegate_);
286 EXPECT_TRUE(service_->is_initialized()); 287 EXPECT_TRUE(service_->is_initialized());
287 288
288 const PolicyBundle empty_bundle; 289 const PolicyBundle empty_bundle;
289 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); 290 EXPECT_TRUE(service_->policy().Equals(empty_bundle));
290 } 291 }
291 292
292 TEST_F(ComponentCloudPolicyServiceTest, InitializeRegistryThenStore) { 293 TEST_F(ComponentCloudPolicyServiceTest, InitializeRegistryThenStore) {
293 Connect(1u); 294 Connect(2u);
294 295
295 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0); 296 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0);
296 EXPECT_CALL(*client_, FetchPolicy()).Times(0); 297 EXPECT_CALL(*client_, FetchPolicy()).Times(0);
297 InitializeRegistry(); 298 InitializeRegistry();
298 RunUntilIdle(); 299 RunUntilIdle();
299 Mock::VerifyAndClearExpectations(client_); 300 Mock::VerifyAndClearExpectations(client_);
300 Mock::VerifyAndClearExpectations(&delegate_); 301 Mock::VerifyAndClearExpectations(&delegate_);
301 EXPECT_FALSE(service_->is_initialized()); 302 EXPECT_FALSE(service_->is_initialized());
302 303
303 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 304 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
304 EXPECT_CALL(*client_, FetchPolicy()); 305 EXPECT_CALL(*client_, FetchPolicy()).Times(0);
305 LoadStore(); 306 LoadStore();
306 Mock::VerifyAndClearExpectations(client_); 307 Mock::VerifyAndClearExpectations(client_);
307 Mock::VerifyAndClearExpectations(&delegate_); 308 Mock::VerifyAndClearExpectations(&delegate_);
308 EXPECT_TRUE(service_->is_initialized()); 309 EXPECT_TRUE(service_->is_initialized());
309 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size()); 310 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size());
310 const PolicyBundle empty_bundle; 311 const PolicyBundle empty_bundle;
311 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); 312 EXPECT_TRUE(service_->policy().Equals(empty_bundle));
312 } 313 }
313 314
314 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithCachedPolicy) { 315 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithCachedPolicy) {
315 PopulateCache(); 316 PopulateCache();
316 Connect(1u); 317 Connect(2u);
317 318
318 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 319 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
319 EXPECT_CALL(*client_, FetchPolicy()); 320 EXPECT_CALL(*client_, FetchPolicy()).Times(0);
320 InitializeRegistry(); 321 InitializeRegistry();
321 LoadStore(); 322 LoadStore();
322 Mock::VerifyAndClearExpectations(client_); 323 Mock::VerifyAndClearExpectations(client_);
323 Mock::VerifyAndClearExpectations(&delegate_); 324 Mock::VerifyAndClearExpectations(&delegate_);
324 325
325 EXPECT_TRUE(service_->is_initialized()); 326 EXPECT_TRUE(service_->is_initialized());
326 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size()); 327 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size());
327 328
328 // kTestExtension2 is not in the registry so it was dropped. 329 // kTestExtension2 is not in the registry so it was dropped.
329 std::map<std::string, std::string> contents; 330 std::map<std::string, std::string> contents;
330 cache_->LoadAllSubkeys("extension-policy", &contents); 331 cache_->LoadAllSubkeys("extension-policy", &contents);
331 ASSERT_EQ(1u, contents.size()); 332 ASSERT_EQ(1u, contents.size());
332 EXPECT_EQ(kTestExtension, contents.begin()->first); 333 EXPECT_EQ(kTestExtension, contents.begin()->first);
333 334
334 PolicyBundle expected_bundle; 335 PolicyBundle expected_bundle;
335 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 336 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
336 expected_bundle.Get(ns).CopyFrom(expected_policy_); 337 expected_bundle.Get(ns).CopyFrom(expected_policy_);
337 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 338 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
338 } 339 }
339 340
340 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) { 341 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) {
341 Connect(1u); 342 Connect(2u);
342 // Initialize the store and create the backend. 343 // Initialize the store and create the backend.
343 // A refresh is not needed, because no components are registered yet. 344 // A refresh is not needed, because no components are registered yet.
344 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 345 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
345 EXPECT_CALL(*client_, FetchPolicy()).Times(0); 346 EXPECT_CALL(*client_, FetchPolicy()).Times(0);
346 registry_.SetReady(POLICY_DOMAIN_CHROME); 347 registry_.SetReady(POLICY_DOMAIN_CHROME);
347 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); 348 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS);
348 LoadStore(); 349 LoadStore();
349 Mock::VerifyAndClearExpectations(client_); 350 Mock::VerifyAndClearExpectations(client_);
350 Mock::VerifyAndClearExpectations(&delegate_); 351 Mock::VerifyAndClearExpectations(&delegate_);
351 EXPECT_TRUE(service_->is_initialized()); 352 EXPECT_TRUE(service_->is_initialized());
352 353
353 // Register the components to fetch. 354 // Register the components to fetch. The |service_| issues a new update
354 EXPECT_CALL(*client_, FetchPolicy()); 355 // because the new schema may filter different policies from the store.
356 EXPECT_CALL(*client_, FetchPolicy()).Times(0);
357 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
355 registry_.RegisterComponent( 358 registry_.RegisterComponent(
356 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), 359 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
357 CreateTestSchema()); 360 CreateTestSchema());
358 RunUntilIdle(); 361 RunUntilIdle();
359 Mock::VerifyAndClearExpectations(client_); 362 Mock::VerifyAndClearExpectations(client_);
363 Mock::VerifyAndClearExpectations(&delegate_);
360 364
361 // Send back a fake policy fetch response. 365 // Send back a fake policy fetch response.
362 client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, 366 client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
363 kTestExtension), 367 kTestExtension),
364 *CreateResponse()); 368 *CreateResponse());
365 service_->OnPolicyFetched(client_); 369 service_->OnPolicyFetched(client_);
366 RunUntilIdle(); 370 RunUntilIdle();
367 371
368 // That should have triggered the download fetch. 372 // That should have triggered the download fetch.
369 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 373 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
370 ASSERT_TRUE(fetcher); 374 ASSERT_TRUE(fetcher);
371 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); 375 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
372 fetcher->set_response_code(200); 376 fetcher->set_response_code(200);
373 fetcher->SetResponseString(kTestPolicy); 377 fetcher->SetResponseString(kTestPolicy);
374 fetcher->delegate()->OnURLFetchComplete(fetcher); 378 fetcher->delegate()->OnURLFetchComplete(fetcher);
375 379
376 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 380 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
377 RunUntilIdle(); 381 RunUntilIdle();
378 Mock::VerifyAndClearExpectations(&delegate_); 382 Mock::VerifyAndClearExpectations(&delegate_);
379 383
380 // The policy is now being served. 384 // The policy is now being served.
381 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 385 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
382 PolicyBundle expected_bundle; 386 PolicyBundle expected_bundle;
383 expected_bundle.Get(ns).CopyFrom(expected_policy_); 387 expected_bundle.Get(ns).CopyFrom(expected_policy_);
384 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 388 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
385 } 389 }
386 390
387 TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) { 391 TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) {
388 Connect(1u); 392 Connect(2u);
389 // Insert data in the cache. 393 // Insert data in the cache.
390 PopulateCache(); 394 PopulateCache();
391 registry_.RegisterComponent( 395 registry_.RegisterComponent(
392 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2), 396 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2),
393 CreateTestSchema()); 397 CreateTestSchema());
394 InitializeRegistry(); 398 InitializeRegistry();
395 399
396 // Load the initial cache. 400 // Load the initial cache.
397 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 401 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
398 EXPECT_CALL(*client_, FetchPolicy()); 402 EXPECT_CALL(*client_, FetchPolicy()).Times(0);
399 LoadStore(); 403 LoadStore();
400 Mock::VerifyAndClearExpectations(client_); 404 Mock::VerifyAndClearExpectations(client_);
401 Mock::VerifyAndClearExpectations(&delegate_); 405 Mock::VerifyAndClearExpectations(&delegate_);
402 406
403 PolicyBundle expected_bundle; 407 PolicyBundle expected_bundle;
404 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 408 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
405 expected_bundle.Get(ns).CopyFrom(expected_policy_); 409 expected_bundle.Get(ns).CopyFrom(expected_policy_);
406 ns.component_id = kTestExtension2; 410 ns.component_id = kTestExtension2;
407 expected_bundle.Get(ns).CopyFrom(expected_policy_); 411 expected_bundle.Get(ns).CopyFrom(expected_policy_);
408 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 412 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
409 413
410 // Now purge one of the extensions. 414 // Now purge one of the extensions. This generates 2 notifications: one for
411 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 415 // the new, immediate filtering, and another once the backend comes back
416 // after purging the cache.
417 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(2);
412 registry_.UnregisterComponent( 418 registry_.UnregisterComponent(
413 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension)); 419 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension));
414 RunUntilIdle(); 420 RunUntilIdle();
415 Mock::VerifyAndClearExpectations(&delegate_); 421 Mock::VerifyAndClearExpectations(&delegate_);
416 422
417 ns.component_id = kTestExtension; 423 ns.component_id = kTestExtension;
418 expected_bundle.Get(ns).Clear(); 424 expected_bundle.Get(ns).Clear();
419 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 425 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
420 426
421 std::map<std::string, std::string> contents; 427 std::map<std::string, std::string> contents;
422 cache_->LoadAllSubkeys("extension-policy", &contents); 428 cache_->LoadAllSubkeys("extension-policy", &contents);
423 EXPECT_EQ(1u, contents.size()); 429 EXPECT_EQ(1u, contents.size());
424 EXPECT_TRUE(ContainsKey(contents, kTestExtension2)); 430 EXPECT_TRUE(ContainsKey(contents, kTestExtension2));
425 } 431 }
426 432
427 TEST_F(ComponentCloudPolicyServiceTest, SignInAfterStartup) { 433 TEST_F(ComponentCloudPolicyServiceTest, SignInAfterStartup) {
428 registry_.SetReady(POLICY_DOMAIN_CHROME); 434 registry_.SetReady(POLICY_DOMAIN_CHROME);
429 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); 435 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS);
430 436
431 // Initialize the store without credentials. 437 // Initialize the store without credentials.
432 EXPECT_FALSE(store_.is_initialized()); 438 EXPECT_FALSE(store_.is_initialized());
433 EXPECT_FALSE(service_->is_initialized()); 439 EXPECT_FALSE(service_->is_initialized());
434 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 440 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
435 store_.NotifyStoreLoaded(); 441 store_.NotifyStoreLoaded();
436 RunUntilIdle(); 442 RunUntilIdle();
437 Mock::VerifyAndClearExpectations(&delegate_); 443 Mock::VerifyAndClearExpectations(&delegate_);
438 EXPECT_TRUE(service_->is_initialized()); 444 EXPECT_TRUE(service_->is_initialized());
439 445
440 // Register an extension. 446 // Register an extension.
447 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
441 registry_.RegisterComponent( 448 registry_.RegisterComponent(
442 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), 449 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
443 CreateTestSchema()); 450 CreateTestSchema());
444 RunUntilIdle(); 451 RunUntilIdle();
452 Mock::VerifyAndClearExpectations(&delegate_);
445 453
446 // Now signin. A fetch will be requested for the new extension. 454 // Now signin. A fetch will be requested for the new extension.
447 Connect(2u); 455 Connect(2u);
448 456
449 // Send the response to the service. The response data will be ignored, 457 // Send the response to the service. The response data will be ignored,
450 // because the store doesn't have the updated credentials yet. 458 // because the store doesn't have the updated credentials yet.
451 client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, 459 client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
452 kTestExtension), 460 kTestExtension),
453 *CreateResponse()); 461 *CreateResponse());
454 service_->OnPolicyFetched(client_); 462 service_->OnPolicyFetched(client_);
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 550
543 PolicyBundle expected_bundle; 551 PolicyBundle expected_bundle;
544 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 552 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
545 expected_bundle.Get(ns).Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 553 expected_bundle.Get(ns).Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
546 base::Value::CreateStringValue("published"), 554 base::Value::CreateStringValue("published"),
547 NULL); 555 NULL);
548 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 556 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
549 } 557 }
550 558
551 } // namespace policy 559 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698