| 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_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" | 10 #include "base/pickle.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 16 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
| 17 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | 17 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
| 18 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | 18 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" |
| 19 #include "chrome/browser/policy/cloud/policy_builder.h" | 19 #include "chrome/browser/policy/cloud/policy_builder.h" |
| 20 #include "chrome/browser/policy/cloud/resource_cache.h" | 20 #include "chrome/browser/policy/cloud/resource_cache.h" |
| 21 #include "chrome/browser/policy/external_data_fetcher.h" | 21 #include "chrome/browser/policy/external_data_fetcher.h" |
| 22 #include "chrome/browser/policy/policy_domain_descriptor.h" | |
| 23 #include "chrome/browser/policy/policy_map.h" | 22 #include "chrome/browser/policy/policy_map.h" |
| 24 #include "chrome/browser/policy/policy_types.h" | 23 #include "chrome/browser/policy/policy_types.h" |
| 25 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" | 24 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" |
| 26 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | 25 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" |
| 26 #include "chrome/browser/policy/schema_registry.h" |
| 27 #include "components/policy/core/common/schema.h" | 27 #include "components/policy/core/common/schema.h" |
| 28 #include "net/url_request/test_url_fetcher_factory.h" | 28 #include "net/url_request/test_url_fetcher_factory.h" |
| 29 #include "net/url_request/url_fetcher_delegate.h" | 29 #include "net/url_request/url_fetcher_delegate.h" |
| 30 #include "net/url_request/url_request_context.h" | 30 #include "net/url_request/url_request_context.h" |
| 31 #include "net/url_request/url_request_context_getter.h" | 31 #include "net/url_request/url_request_context_getter.h" |
| 32 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 34 |
| 35 namespace em = enterprise_management; | 35 namespace em = enterprise_management; |
| 36 | 36 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 base::MessageLoop loop_; | 198 base::MessageLoop loop_; |
| 199 base::ScopedTempDir temp_dir_; | 199 base::ScopedTempDir temp_dir_; |
| 200 scoped_refptr<TestURLRequestContextGetter> request_context_; | 200 scoped_refptr<TestURLRequestContextGetter> request_context_; |
| 201 net::TestURLFetcherFactory fetcher_factory_; | 201 net::TestURLFetcherFactory fetcher_factory_; |
| 202 MockComponentCloudPolicyDelegate delegate_; | 202 MockComponentCloudPolicyDelegate delegate_; |
| 203 // |cache_| is owned by the |service_| and is invalid once the |service_| | 203 // |cache_| is owned by the |service_| and is invalid once the |service_| |
| 204 // is destroyed. | 204 // is destroyed. |
| 205 ResourceCache* cache_; | 205 ResourceCache* cache_; |
| 206 MockCloudPolicyClient client_; | 206 MockCloudPolicyClient client_; |
| 207 MockCloudPolicyStore store_; | 207 MockCloudPolicyStore store_; |
| 208 SchemaRegistry registry_; |
| 208 scoped_ptr<ComponentCloudPolicyService> service_; | 209 scoped_ptr<ComponentCloudPolicyService> service_; |
| 209 ComponentPolicyBuilder builder_; | 210 ComponentPolicyBuilder builder_; |
| 210 PolicyMap expected_policy_; | 211 PolicyMap expected_policy_; |
| 211 }; | 212 }; |
| 212 | 213 |
| 213 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithoutCredentials) { | 214 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithoutCredentials) { |
| 214 EXPECT_FALSE(service_->is_initialized()); | 215 EXPECT_FALSE(service_->is_initialized()); |
| 215 // Run the background task to initialize the backend. | 216 // Run the background task to initialize the backend. |
| 216 RunUntilIdle(); | 217 RunUntilIdle(); |
| 217 // Still waiting for the |store_|. | 218 // Still waiting for the |store_|. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 std::map<std::string, std::string> contents; | 261 std::map<std::string, std::string> contents; |
| 261 cache_->LoadAllSubkeys(ComponentCloudPolicyService::kComponentNamespaceCache, | 262 cache_->LoadAllSubkeys(ComponentCloudPolicyService::kComponentNamespaceCache, |
| 262 &contents); | 263 &contents); |
| 263 ASSERT_EQ(1u, contents.size()); | 264 ASSERT_EQ(1u, contents.size()); |
| 264 EXPECT_EQ(std::string(dm_protocol::kChromeExtensionPolicyType), | 265 EXPECT_EQ(std::string(dm_protocol::kChromeExtensionPolicyType), |
| 265 contents.begin()->first); | 266 contents.begin()->first); |
| 266 } | 267 } |
| 267 | 268 |
| 268 TEST_F(ComponentCloudPolicyServiceTest, ConnectAfterRegister) { | 269 TEST_F(ComponentCloudPolicyServiceTest, ConnectAfterRegister) { |
| 269 // Add some components. | 270 // Add some components. |
| 270 scoped_refptr<PolicyDomainDescriptor> descriptor = new PolicyDomainDescriptor( | 271 registry_.RegisterComponent( |
| 271 POLICY_DOMAIN_EXTENSIONS); | 272 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), |
| 272 descriptor->RegisterComponent(kTestExtension, CreateTestSchema()); | 273 CreateTestSchema()); |
| 273 descriptor->RegisterComponent(kTestExtension2, CreateTestSchema()); | 274 registry_.RegisterComponent( |
| 274 service_->RegisterPolicyDomain(descriptor); | 275 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2), |
| 276 CreateTestSchema()); |
| 277 service_->OnSchemasUpdated(registry_.schema_map()); |
| 275 | 278 |
| 276 // Now connect the client. | 279 // Now connect the client. |
| 277 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); | 280 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); |
| 278 service_->Connect(&client_, request_context_); | 281 service_->Connect(&client_, request_context_); |
| 279 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); | 282 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); |
| 280 | 283 |
| 281 // It receives the namespaces once the backend is initialized. | 284 // It receives the namespaces once the backend is initialized. |
| 282 LoadStore(); | 285 LoadStore(); |
| 283 std::set<PolicyNamespaceKey> set; | 286 std::set<PolicyNamespaceKey> set; |
| 284 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, | 287 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 312 expected_components.insert(kTestExtension2); | 315 expected_components.insert(kTestExtension2); |
| 313 | 316 |
| 314 EXPECT_EQ(expected_components, unpickled); | 317 EXPECT_EQ(expected_components, unpickled); |
| 315 } | 318 } |
| 316 | 319 |
| 317 TEST_F(ComponentCloudPolicyServiceTest, StoreReadyAfterConnectAndRegister) { | 320 TEST_F(ComponentCloudPolicyServiceTest, StoreReadyAfterConnectAndRegister) { |
| 318 // Add some previous data to the cache. | 321 // Add some previous data to the cache. |
| 319 PopulateCache(); | 322 PopulateCache(); |
| 320 | 323 |
| 321 // Add some components. | 324 // Add some components. |
| 322 scoped_refptr<PolicyDomainDescriptor> descriptor = new PolicyDomainDescriptor( | 325 registry_.RegisterComponent( |
| 323 POLICY_DOMAIN_EXTENSIONS); | 326 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), |
| 324 descriptor->RegisterComponent(kTestExtension, CreateTestSchema()); | 327 CreateTestSchema()); |
| 325 service_->RegisterPolicyDomain(descriptor); | 328 service_->OnSchemasUpdated(registry_.schema_map()); |
| 326 | 329 |
| 327 // And connect the client. Make the client have some policies, with a new | 330 // And connect the client. Make the client have some policies, with a new |
| 328 // download_url. | 331 // download_url. |
| 329 builder_.payload().set_download_url(kTestDownload2); | 332 builder_.payload().set_download_url(kTestDownload2); |
| 330 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, | 333 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, |
| 331 kTestExtension), | 334 kTestExtension), |
| 332 *CreateResponse()); | 335 *CreateResponse()); |
| 333 service_->Connect(&client_, request_context_); | 336 service_->Connect(&client_, request_context_); |
| 334 | 337 |
| 335 // Now make the store ready. | 338 // Now make the store ready. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 351 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); | 354 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); |
| 352 } | 355 } |
| 353 | 356 |
| 354 TEST_F(ComponentCloudPolicyServiceTest, ConnectThenRegisterThenStoreReady) { | 357 TEST_F(ComponentCloudPolicyServiceTest, ConnectThenRegisterThenStoreReady) { |
| 355 // Connect right after creating the service. | 358 // Connect right after creating the service. |
| 356 service_->Connect(&client_, request_context_); | 359 service_->Connect(&client_, request_context_); |
| 357 | 360 |
| 358 // Now register the current components, before the backend has been | 361 // Now register the current components, before the backend has been |
| 359 // initialized. | 362 // initialized. |
| 360 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); | 363 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); |
| 361 scoped_refptr<PolicyDomainDescriptor> descriptor = new PolicyDomainDescriptor( | 364 registry_.RegisterComponent( |
| 362 POLICY_DOMAIN_EXTENSIONS); | 365 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), |
| 363 descriptor->RegisterComponent(kTestExtension, CreateTestSchema()); | 366 CreateTestSchema()); |
| 364 service_->RegisterPolicyDomain(descriptor); | 367 service_->OnSchemasUpdated(registry_.schema_map()); |
| 365 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); | 368 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); |
| 366 | 369 |
| 367 // Now load the store. The client gets the namespaces. | 370 // Now load the store. The client gets the namespaces. |
| 368 LoadStore(); | 371 LoadStore(); |
| 369 std::set<PolicyNamespaceKey> set; | 372 std::set<PolicyNamespaceKey> set; |
| 370 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, | 373 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, |
| 371 kTestExtension)); | 374 kTestExtension)); |
| 372 EXPECT_EQ(set, client_.namespaces_to_fetch_); | 375 EXPECT_EQ(set, client_.namespaces_to_fetch_); |
| 373 } | 376 } |
| 374 | 377 |
| 375 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) { | 378 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) { |
| 376 // Initialize the store and create the backend, and connect the client. | 379 // Initialize the store and create the backend, and connect the client. |
| 377 LoadStore(); | 380 LoadStore(); |
| 378 // A refresh is not needed, because no components were found. | 381 // A refresh is not needed, because no components were found. |
| 379 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0); | 382 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0); |
| 380 service_->Connect(&client_, request_context_); | 383 service_->Connect(&client_, request_context_); |
| 381 Mock::VerifyAndClearExpectations(&delegate_); | 384 Mock::VerifyAndClearExpectations(&delegate_); |
| 382 | 385 |
| 383 // Register the components to fetch. | 386 // Register the components to fetch. |
| 384 scoped_refptr<PolicyDomainDescriptor> descriptor = new PolicyDomainDescriptor( | 387 registry_.RegisterComponent( |
| 385 POLICY_DOMAIN_EXTENSIONS); | 388 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), |
| 386 descriptor->RegisterComponent(kTestExtension, CreateTestSchema()); | 389 CreateTestSchema()); |
| 387 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); | 390 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); |
| 388 service_->RegisterPolicyDomain(descriptor); | 391 service_->OnSchemasUpdated(registry_.schema_map()); |
| 389 Mock::VerifyAndClearExpectations(&delegate_); | 392 Mock::VerifyAndClearExpectations(&delegate_); |
| 390 | 393 |
| 391 // Send back a fake policy fetch response. | 394 // Send back a fake policy fetch response. |
| 392 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, | 395 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, |
| 393 kTestExtension), | 396 kTestExtension), |
| 394 *CreateResponse()); | 397 *CreateResponse()); |
| 395 service_->OnPolicyFetched(&client_); | 398 service_->OnPolicyFetched(&client_); |
| 396 RunUntilIdle(); | 399 RunUntilIdle(); |
| 397 | 400 |
| 398 // That should have triggered the download fetch. | 401 // That should have triggered the download fetch. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 429 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | 432 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); |
| 430 expected_bundle.Get(ns).CopyFrom(expected_policy_); | 433 expected_bundle.Get(ns).CopyFrom(expected_policy_); |
| 431 ns.component_id = kTestExtension2; | 434 ns.component_id = kTestExtension2; |
| 432 expected_bundle.Get(ns).CopyFrom(expected_policy_); | 435 expected_bundle.Get(ns).CopyFrom(expected_policy_); |
| 433 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | 436 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); |
| 434 | 437 |
| 435 // Now purge one of the extensions. | 438 // Now purge one of the extensions. |
| 436 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 439 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 437 // The service will start updating the components that are registered, which | 440 // The service will start updating the components that are registered, which |
| 438 // starts by fetching policy for them. | 441 // starts by fetching policy for them. |
| 439 scoped_refptr<PolicyDomainDescriptor> descriptor = new PolicyDomainDescriptor( | 442 registry_.RegisterComponent( |
| 440 POLICY_DOMAIN_EXTENSIONS); | 443 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2), |
| 441 descriptor->RegisterComponent(kTestExtension2, CreateTestSchema()); | 444 CreateTestSchema()); |
| 442 service_->RegisterPolicyDomain(descriptor); | 445 service_->OnSchemasUpdated(registry_.schema_map()); |
| 443 RunUntilIdle(); | 446 RunUntilIdle(); |
| 444 Mock::VerifyAndClearExpectations(&delegate_); | 447 Mock::VerifyAndClearExpectations(&delegate_); |
| 445 | 448 |
| 446 ns.component_id = kTestExtension; | 449 ns.component_id = kTestExtension; |
| 447 expected_bundle.Get(ns).Clear(); | 450 expected_bundle.Get(ns).Clear(); |
| 448 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | 451 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); |
| 449 | 452 |
| 450 std::map<std::string, std::string> contents; | 453 std::map<std::string, std::string> contents; |
| 451 cache_->LoadAllSubkeys("extension-policy", &contents); | 454 cache_->LoadAllSubkeys("extension-policy", &contents); |
| 452 EXPECT_EQ(1u, contents.size()); | 455 EXPECT_EQ(1u, contents.size()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 463 store_.policy_.reset(new em::PolicyData()); // No credentials. | 466 store_.policy_.reset(new em::PolicyData()); // No credentials. |
| 464 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 467 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 465 store_.NotifyStoreLoaded(); | 468 store_.NotifyStoreLoaded(); |
| 466 RunUntilIdle(); | 469 RunUntilIdle(); |
| 467 Mock::VerifyAndClearExpectations(&delegate_); | 470 Mock::VerifyAndClearExpectations(&delegate_); |
| 468 EXPECT_TRUE(service_->is_initialized()); | 471 EXPECT_TRUE(service_->is_initialized()); |
| 469 | 472 |
| 470 // Connect the client and register an extension. | 473 // Connect the client and register an extension. |
| 471 service_->Connect(&client_, request_context_); | 474 service_->Connect(&client_, request_context_); |
| 472 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); | 475 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); |
| 473 scoped_refptr<PolicyDomainDescriptor> descriptor = new PolicyDomainDescriptor( | 476 registry_.RegisterComponent( |
| 474 POLICY_DOMAIN_EXTENSIONS); | 477 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), |
| 475 descriptor->RegisterComponent(kTestExtension, CreateTestSchema()); | 478 CreateTestSchema()); |
| 476 service_->RegisterPolicyDomain(descriptor); | 479 service_->OnSchemasUpdated(registry_.schema_map()); |
| 477 Mock::VerifyAndClearExpectations(&delegate_); | 480 Mock::VerifyAndClearExpectations(&delegate_); |
| 478 | 481 |
| 479 // Send the response to the service. The response data will be rejected, | 482 // Send the response to the service. The response data will be rejected, |
| 480 // because the store doesn't have the updated credentials yet. | 483 // because the store doesn't have the updated credentials yet. |
| 481 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, | 484 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, |
| 482 kTestExtension), | 485 kTestExtension), |
| 483 *CreateResponse()); | 486 *CreateResponse()); |
| 484 service_->OnPolicyFetched(&client_); | 487 service_->OnPolicyFetched(&client_); |
| 485 RunUntilIdle(); | 488 RunUntilIdle(); |
| 486 | 489 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 510 Mock::VerifyAndClearExpectations(&delegate_); | 513 Mock::VerifyAndClearExpectations(&delegate_); |
| 511 | 514 |
| 512 // The policy is now being served. | 515 // The policy is now being served. |
| 513 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | 516 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); |
| 514 PolicyBundle expected_bundle; | 517 PolicyBundle expected_bundle; |
| 515 expected_bundle.Get(ns).CopyFrom(expected_policy_); | 518 expected_bundle.Get(ns).CopyFrom(expected_policy_); |
| 516 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | 519 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); |
| 517 } | 520 } |
| 518 | 521 |
| 519 } // namespace policy | 522 } // namespace policy |
| OLD | NEW |