| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/cloud/component_cloud_policy_service.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/sha1.h" | |
| 15 #include "base/single_thread_task_runner.h" | |
| 16 #include "base/stl_util.h" | |
| 17 #include "base/values.h" | |
| 18 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | |
| 19 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | |
| 20 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | |
| 21 #include "chrome/browser/policy/cloud/policy_builder.h" | |
| 22 #include "chrome/browser/policy/cloud/resource_cache.h" | |
| 23 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" | |
| 24 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | |
| 25 #include "components/policy/core/common/external_data_fetcher.h" | |
| 26 #include "components/policy/core/common/policy_map.h" | |
| 27 #include "components/policy/core/common/policy_types.h" | |
| 28 #include "components/policy/core/common/schema.h" | |
| 29 #include "components/policy/core/common/schema_map.h" | |
| 30 #include "net/url_request/test_url_fetcher_factory.h" | |
| 31 #include "net/url_request/url_fetcher_delegate.h" | |
| 32 #include "net/url_request/url_request_context.h" | |
| 33 #include "net/url_request/url_request_context_getter.h" | |
| 34 #include "testing/gmock/include/gmock/gmock.h" | |
| 35 #include "testing/gtest/include/gtest/gtest.h" | |
| 36 | |
| 37 namespace em = enterprise_management; | |
| 38 | |
| 39 using testing::Mock; | |
| 40 | |
| 41 namespace policy { | |
| 42 | |
| 43 namespace { | |
| 44 | |
| 45 const char kTestExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | |
| 46 const char kTestExtension2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; | |
| 47 const char kTestDownload[] = "http://example.com/getpolicy?id=123"; | |
| 48 | |
| 49 const char kTestPolicy[] = | |
| 50 "{" | |
| 51 " \"Name\": {" | |
| 52 " \"Value\": \"disabled\"" | |
| 53 " }," | |
| 54 " \"Second\": {" | |
| 55 " \"Value\": \"maybe\"," | |
| 56 " \"Level\": \"Recommended\"" | |
| 57 " }" | |
| 58 "}"; | |
| 59 | |
| 60 const char kInvalidTestPolicy[] = | |
| 61 "{" | |
| 62 " \"Name\": {" | |
| 63 " \"Value\": \"published\"" | |
| 64 " }," | |
| 65 " \"Undeclared Name\": {" | |
| 66 " \"Value\": \"not published\"" | |
| 67 " }" | |
| 68 "}"; | |
| 69 | |
| 70 const char kTestSchema[] = | |
| 71 "{" | |
| 72 " \"type\": \"object\"," | |
| 73 " \"properties\": {" | |
| 74 " \"Name\": { \"type\": \"string\" }," | |
| 75 " \"Second\": { \"type\": \"string\" }" | |
| 76 " }" | |
| 77 "}"; | |
| 78 | |
| 79 class MockComponentCloudPolicyDelegate | |
| 80 : public ComponentCloudPolicyService::Delegate { | |
| 81 public: | |
| 82 virtual ~MockComponentCloudPolicyDelegate() {} | |
| 83 | |
| 84 MOCK_METHOD0(OnComponentCloudPolicyUpdated, void()); | |
| 85 }; | |
| 86 | |
| 87 class TestURLRequestContextGetter : public net::URLRequestContextGetter { | |
| 88 public: | |
| 89 explicit TestURLRequestContextGetter( | |
| 90 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
| 91 : task_runner_(task_runner) {} | |
| 92 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE { | |
| 93 return NULL; | |
| 94 } | |
| 95 virtual scoped_refptr<base::SingleThreadTaskRunner> | |
| 96 GetNetworkTaskRunner() const OVERRIDE { | |
| 97 return task_runner_; | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 virtual ~TestURLRequestContextGetter() {} | |
| 102 | |
| 103 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 104 }; | |
| 105 | |
| 106 } // namespace | |
| 107 | |
| 108 class ComponentCloudPolicyServiceTest : public testing::Test { | |
| 109 protected: | |
| 110 ComponentCloudPolicyServiceTest() | |
| 111 : client_(NULL), | |
| 112 core_(PolicyNamespaceKey(GetChromeUserPolicyType(), ""), | |
| 113 &store_, | |
| 114 loop_.message_loop_proxy()) {} | |
| 115 | |
| 116 virtual void SetUp() OVERRIDE { | |
| 117 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 118 | |
| 119 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy()); | |
| 120 request_context_ = | |
| 121 new TestURLRequestContextGetter(loop_.message_loop_proxy()); | |
| 122 service_.reset(new ComponentCloudPolicyService( | |
| 123 &delegate_, | |
| 124 ®istry_, | |
| 125 &core_, | |
| 126 make_scoped_ptr(cache_), | |
| 127 request_context_, | |
| 128 loop_.message_loop_proxy(), | |
| 129 loop_.message_loop_proxy())); | |
| 130 | |
| 131 builder_.policy_data().set_policy_type( | |
| 132 dm_protocol::kChromeExtensionPolicyType); | |
| 133 builder_.policy_data().set_settings_entity_id(kTestExtension); | |
| 134 builder_.payload().set_download_url(kTestDownload); | |
| 135 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy)); | |
| 136 | |
| 137 expected_policy_.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
| 138 base::Value::CreateStringValue("disabled"), NULL); | |
| 139 expected_policy_.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | |
| 140 base::Value::CreateStringValue("maybe"), NULL); | |
| 141 } | |
| 142 | |
| 143 virtual void TearDown() OVERRIDE { | |
| 144 // The service cleans up its backend on the background thread. | |
| 145 service_.reset(); | |
| 146 RunUntilIdle(); | |
| 147 } | |
| 148 | |
| 149 void RunUntilIdle() { | |
| 150 base::RunLoop().RunUntilIdle(); | |
| 151 } | |
| 152 | |
| 153 void Connect(size_t expected_namespaces_in_client) { | |
| 154 client_ = new MockCloudPolicyClient(); | |
| 155 client_->SetDMToken(ComponentPolicyBuilder::kFakeToken); | |
| 156 EXPECT_EQ(0u, client_->namespaces_to_fetch_.size()); | |
| 157 | |
| 158 core_.Connect(scoped_ptr<CloudPolicyClient>(client_)); | |
| 159 | |
| 160 // |expected_namespaces_in_client| is the expected number of components | |
| 161 // that the ComponentCloudPolicyService will set at the |client_| at | |
| 162 // OnCoreConnected. | |
| 163 EXPECT_EQ(expected_namespaces_in_client, | |
| 164 client_->namespaces_to_fetch_.size()); | |
| 165 | |
| 166 // Also initialize the refresh scheduler, so that calls to | |
| 167 // core()->RefreshSoon() trigger a FetchPolicy() call on the mock |client_|. | |
| 168 // Expect the initial refresh now, if the store doesn't have policy (if it | |
| 169 // does then the CloudPolicyRefreshScheduler won't start refreshing until | |
| 170 // invalidations are available, or a timeout elapses). | |
| 171 if (!store_.has_policy()) | |
| 172 EXPECT_CALL(*client_, FetchPolicy()); | |
| 173 core_.StartRefreshScheduler(); | |
| 174 RunUntilIdle(); | |
| 175 Mock::VerifyAndClearExpectations(client_); | |
| 176 } | |
| 177 | |
| 178 void LoadStore() { | |
| 179 EXPECT_FALSE(store_.is_initialized()); | |
| 180 | |
| 181 em::PolicyData* data = new em::PolicyData(); | |
| 182 data->set_username(ComponentPolicyBuilder::kFakeUsername); | |
| 183 data->set_request_token(ComponentPolicyBuilder::kFakeToken); | |
| 184 store_.policy_.reset(data); | |
| 185 | |
| 186 store_.NotifyStoreLoaded(); | |
| 187 RunUntilIdle(); | |
| 188 EXPECT_TRUE(store_.is_initialized()); | |
| 189 } | |
| 190 | |
| 191 void InitializeRegistry() { | |
| 192 registry_.RegisterComponent( | |
| 193 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), | |
| 194 CreateTestSchema()); | |
| 195 registry_.SetReady(POLICY_DOMAIN_CHROME); | |
| 196 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); | |
| 197 } | |
| 198 | |
| 199 void PopulateCache() { | |
| 200 EXPECT_TRUE(cache_->Store( | |
| 201 "extension-policy", kTestExtension, CreateSerializedResponse())); | |
| 202 EXPECT_TRUE( | |
| 203 cache_->Store("extension-policy-data", kTestExtension, kTestPolicy)); | |
| 204 | |
| 205 builder_.policy_data().set_settings_entity_id(kTestExtension2); | |
| 206 EXPECT_TRUE(cache_->Store( | |
| 207 "extension-policy", kTestExtension2, CreateSerializedResponse())); | |
| 208 EXPECT_TRUE( | |
| 209 cache_->Store("extension-policy-data", kTestExtension2, kTestPolicy)); | |
| 210 } | |
| 211 | |
| 212 scoped_ptr<em::PolicyFetchResponse> CreateResponse() { | |
| 213 builder_.Build(); | |
| 214 return make_scoped_ptr(new em::PolicyFetchResponse(builder_.policy())); | |
| 215 } | |
| 216 | |
| 217 std::string CreateSerializedResponse() { | |
| 218 builder_.Build(); | |
| 219 return builder_.GetBlob(); | |
| 220 } | |
| 221 | |
| 222 Schema CreateTestSchema() { | |
| 223 std::string error; | |
| 224 Schema schema = Schema::Parse(kTestSchema, &error); | |
| 225 EXPECT_TRUE(schema.valid()) << error; | |
| 226 return schema; | |
| 227 } | |
| 228 | |
| 229 base::MessageLoop loop_; | |
| 230 base::ScopedTempDir temp_dir_; | |
| 231 scoped_refptr<TestURLRequestContextGetter> request_context_; | |
| 232 net::TestURLFetcherFactory fetcher_factory_; | |
| 233 MockComponentCloudPolicyDelegate delegate_; | |
| 234 // |cache_| is owned by the |service_| and is invalid once the |service_| | |
| 235 // is destroyed. | |
| 236 ResourceCache* cache_; | |
| 237 MockCloudPolicyClient* client_; | |
| 238 MockCloudPolicyStore store_; | |
| 239 CloudPolicyCore core_; | |
| 240 SchemaRegistry registry_; | |
| 241 scoped_ptr<ComponentCloudPolicyService> service_; | |
| 242 ComponentPolicyBuilder builder_; | |
| 243 PolicyMap expected_policy_; | |
| 244 }; | |
| 245 | |
| 246 TEST_F(ComponentCloudPolicyServiceTest, InitializedAtConstructionTime) { | |
| 247 service_.reset(); | |
| 248 Connect(1u); | |
| 249 LoadStore(); | |
| 250 InitializeRegistry(); | |
| 251 | |
| 252 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy()); | |
| 253 service_.reset(new ComponentCloudPolicyService(&delegate_, | |
| 254 ®istry_, | |
| 255 &core_, | |
| 256 make_scoped_ptr(cache_), | |
| 257 request_context_, | |
| 258 loop_.message_loop_proxy(), | |
| 259 loop_.message_loop_proxy())); | |
| 260 EXPECT_FALSE(service_->is_initialized()); | |
| 261 | |
| 262 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 263 EXPECT_CALL(*client_, FetchPolicy()); | |
| 264 RunUntilIdle(); | |
| 265 Mock::VerifyAndClearExpectations(&client_); | |
| 266 Mock::VerifyAndClearExpectations(&delegate_); | |
| 267 | |
| 268 EXPECT_TRUE(service_->is_initialized()); | |
| 269 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size()); | |
| 270 const PolicyBundle empty_bundle; | |
| 271 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); | |
| 272 } | |
| 273 | |
| 274 TEST_F(ComponentCloudPolicyServiceTest, InitializeStoreThenRegistry) { | |
| 275 Connect(1u); | |
| 276 | |
| 277 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0); | |
| 278 EXPECT_CALL(*client_, FetchPolicy()).Times(0); | |
| 279 LoadStore(); | |
| 280 Mock::VerifyAndClearExpectations(client_); | |
| 281 Mock::VerifyAndClearExpectations(&delegate_); | |
| 282 EXPECT_FALSE(service_->is_initialized()); | |
| 283 | |
| 284 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 285 EXPECT_CALL(*client_, FetchPolicy()); | |
| 286 InitializeRegistry(); | |
| 287 RunUntilIdle(); | |
| 288 Mock::VerifyAndClearExpectations(client_); | |
| 289 Mock::VerifyAndClearExpectations(&delegate_); | |
| 290 EXPECT_TRUE(service_->is_initialized()); | |
| 291 | |
| 292 const PolicyBundle empty_bundle; | |
| 293 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); | |
| 294 } | |
| 295 | |
| 296 TEST_F(ComponentCloudPolicyServiceTest, InitializeRegistryThenStore) { | |
| 297 Connect(1u); | |
| 298 | |
| 299 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0); | |
| 300 EXPECT_CALL(*client_, FetchPolicy()).Times(0); | |
| 301 InitializeRegistry(); | |
| 302 RunUntilIdle(); | |
| 303 Mock::VerifyAndClearExpectations(client_); | |
| 304 Mock::VerifyAndClearExpectations(&delegate_); | |
| 305 EXPECT_FALSE(service_->is_initialized()); | |
| 306 | |
| 307 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 308 EXPECT_CALL(*client_, FetchPolicy()); | |
| 309 LoadStore(); | |
| 310 Mock::VerifyAndClearExpectations(client_); | |
| 311 Mock::VerifyAndClearExpectations(&delegate_); | |
| 312 EXPECT_TRUE(service_->is_initialized()); | |
| 313 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size()); | |
| 314 const PolicyBundle empty_bundle; | |
| 315 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); | |
| 316 } | |
| 317 | |
| 318 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithCachedPolicy) { | |
| 319 PopulateCache(); | |
| 320 Connect(1u); | |
| 321 | |
| 322 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 323 EXPECT_CALL(*client_, FetchPolicy()); | |
| 324 InitializeRegistry(); | |
| 325 LoadStore(); | |
| 326 Mock::VerifyAndClearExpectations(client_); | |
| 327 Mock::VerifyAndClearExpectations(&delegate_); | |
| 328 | |
| 329 EXPECT_TRUE(service_->is_initialized()); | |
| 330 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size()); | |
| 331 | |
| 332 // kTestExtension2 is not in the registry so it was dropped. | |
| 333 std::map<std::string, std::string> contents; | |
| 334 cache_->LoadAllSubkeys("extension-policy", &contents); | |
| 335 ASSERT_EQ(1u, contents.size()); | |
| 336 EXPECT_EQ(kTestExtension, contents.begin()->first); | |
| 337 | |
| 338 PolicyBundle expected_bundle; | |
| 339 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | |
| 340 expected_bundle.Get(ns).CopyFrom(expected_policy_); | |
| 341 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | |
| 342 } | |
| 343 | |
| 344 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) { | |
| 345 Connect(1u); | |
| 346 // Initialize the store and create the backend. | |
| 347 // A refresh is not needed, because no components are registered yet. | |
| 348 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 349 EXPECT_CALL(*client_, FetchPolicy()).Times(0); | |
| 350 registry_.SetReady(POLICY_DOMAIN_CHROME); | |
| 351 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); | |
| 352 LoadStore(); | |
| 353 Mock::VerifyAndClearExpectations(client_); | |
| 354 Mock::VerifyAndClearExpectations(&delegate_); | |
| 355 EXPECT_TRUE(service_->is_initialized()); | |
| 356 | |
| 357 // Register the components to fetch. | |
| 358 EXPECT_CALL(*client_, FetchPolicy()); | |
| 359 registry_.RegisterComponent( | |
| 360 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), | |
| 361 CreateTestSchema()); | |
| 362 RunUntilIdle(); | |
| 363 Mock::VerifyAndClearExpectations(client_); | |
| 364 | |
| 365 // Send back a fake policy fetch response. | |
| 366 client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, | |
| 367 kTestExtension), | |
| 368 *CreateResponse()); | |
| 369 service_->OnPolicyFetched(client_); | |
| 370 RunUntilIdle(); | |
| 371 | |
| 372 // That should have triggered the download fetch. | |
| 373 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 374 ASSERT_TRUE(fetcher); | |
| 375 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
| 376 fetcher->set_response_code(200); | |
| 377 fetcher->SetResponseString(kTestPolicy); | |
| 378 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 379 | |
| 380 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 381 RunUntilIdle(); | |
| 382 Mock::VerifyAndClearExpectations(&delegate_); | |
| 383 | |
| 384 // The policy is now being served. | |
| 385 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | |
| 386 PolicyBundle expected_bundle; | |
| 387 expected_bundle.Get(ns).CopyFrom(expected_policy_); | |
| 388 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | |
| 389 } | |
| 390 | |
| 391 TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) { | |
| 392 Connect(1u); | |
| 393 // Insert data in the cache. | |
| 394 PopulateCache(); | |
| 395 registry_.RegisterComponent( | |
| 396 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2), | |
| 397 CreateTestSchema()); | |
| 398 InitializeRegistry(); | |
| 399 | |
| 400 // Load the initial cache. | |
| 401 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 402 EXPECT_CALL(*client_, FetchPolicy()); | |
| 403 LoadStore(); | |
| 404 Mock::VerifyAndClearExpectations(client_); | |
| 405 Mock::VerifyAndClearExpectations(&delegate_); | |
| 406 | |
| 407 PolicyBundle expected_bundle; | |
| 408 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | |
| 409 expected_bundle.Get(ns).CopyFrom(expected_policy_); | |
| 410 ns.component_id = kTestExtension2; | |
| 411 expected_bundle.Get(ns).CopyFrom(expected_policy_); | |
| 412 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | |
| 413 | |
| 414 // Now purge one of the extensions. | |
| 415 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 416 registry_.UnregisterComponent( | |
| 417 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension)); | |
| 418 RunUntilIdle(); | |
| 419 Mock::VerifyAndClearExpectations(&delegate_); | |
| 420 | |
| 421 ns.component_id = kTestExtension; | |
| 422 expected_bundle.Get(ns).Clear(); | |
| 423 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | |
| 424 | |
| 425 std::map<std::string, std::string> contents; | |
| 426 cache_->LoadAllSubkeys("extension-policy", &contents); | |
| 427 EXPECT_EQ(1u, contents.size()); | |
| 428 EXPECT_TRUE(ContainsKey(contents, kTestExtension2)); | |
| 429 } | |
| 430 | |
| 431 TEST_F(ComponentCloudPolicyServiceTest, SignInAfterStartup) { | |
| 432 registry_.SetReady(POLICY_DOMAIN_CHROME); | |
| 433 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); | |
| 434 | |
| 435 // Initialize the store without credentials. | |
| 436 EXPECT_FALSE(store_.is_initialized()); | |
| 437 EXPECT_FALSE(service_->is_initialized()); | |
| 438 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 439 store_.NotifyStoreLoaded(); | |
| 440 RunUntilIdle(); | |
| 441 Mock::VerifyAndClearExpectations(&delegate_); | |
| 442 EXPECT_TRUE(service_->is_initialized()); | |
| 443 | |
| 444 // Register an extension. | |
| 445 registry_.RegisterComponent( | |
| 446 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), | |
| 447 CreateTestSchema()); | |
| 448 RunUntilIdle(); | |
| 449 | |
| 450 // Now signin. A fetch will be requested for the new extension. | |
| 451 Connect(2u); | |
| 452 | |
| 453 // Send the response to the service. The response data will be ignored, | |
| 454 // because the store doesn't have the updated credentials yet. | |
| 455 client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, | |
| 456 kTestExtension), | |
| 457 *CreateResponse()); | |
| 458 service_->OnPolicyFetched(client_); | |
| 459 RunUntilIdle(); | |
| 460 | |
| 461 // The policy was ignored and no download is started because the store | |
| 462 // doesn't have credentials. | |
| 463 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 464 EXPECT_FALSE(fetcher); | |
| 465 | |
| 466 // Now update the |store_| with the updated policy, which includes | |
| 467 // credentials. The responses in the |client_| will be reloaded. | |
| 468 em::PolicyData* data = new em::PolicyData(); | |
| 469 data->set_username(ComponentPolicyBuilder::kFakeUsername); | |
| 470 data->set_request_token(ComponentPolicyBuilder::kFakeToken); | |
| 471 store_.policy_.reset(data); | |
| 472 store_.NotifyStoreLoaded(); | |
| 473 RunUntilIdle(); | |
| 474 | |
| 475 // The extension policy was validated this time, and the download is started. | |
| 476 fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 477 ASSERT_TRUE(fetcher); | |
| 478 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
| 479 fetcher->set_response_code(200); | |
| 480 fetcher->SetResponseString(kTestPolicy); | |
| 481 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 482 | |
| 483 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 484 RunUntilIdle(); | |
| 485 Mock::VerifyAndClearExpectations(&delegate_); | |
| 486 | |
| 487 // The policy is now being served. | |
| 488 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | |
| 489 PolicyBundle expected_bundle; | |
| 490 expected_bundle.Get(ns).CopyFrom(expected_policy_); | |
| 491 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | |
| 492 } | |
| 493 | |
| 494 TEST_F(ComponentCloudPolicyServiceTest, SignOut) { | |
| 495 // Initialize everthing and serve policy for a component. | |
| 496 PopulateCache(); | |
| 497 LoadStore(); | |
| 498 InitializeRegistry(); | |
| 499 | |
| 500 // The initial, cached policy will be served once the backend is initialized. | |
| 501 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 502 RunUntilIdle(); | |
| 503 Mock::VerifyAndClearExpectations(&delegate_); | |
| 504 PolicyBundle expected_bundle; | |
| 505 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | |
| 506 expected_bundle.Get(ns).CopyFrom(expected_policy_); | |
| 507 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | |
| 508 std::map<std::string, std::string> contents; | |
| 509 cache_->LoadAllSubkeys("extension-policy", &contents); | |
| 510 ASSERT_EQ(1u, contents.size()); | |
| 511 | |
| 512 // Now sign in. | |
| 513 Connect(2u); | |
| 514 | |
| 515 // Signing out removes all of the component policies from the service and | |
| 516 // from the cache. It does not trigger a refresh. | |
| 517 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 518 core_.Disconnect(); | |
| 519 store_.policy_.reset(); | |
| 520 store_.NotifyStoreLoaded(); | |
| 521 RunUntilIdle(); | |
| 522 Mock::VerifyAndClearExpectations(&delegate_); | |
| 523 const PolicyBundle empty_bundle; | |
| 524 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); | |
| 525 cache_->LoadAllSubkeys("extension-policy", &contents); | |
| 526 ASSERT_EQ(0u, contents.size()); | |
| 527 } | |
| 528 | |
| 529 TEST_F(ComponentCloudPolicyServiceTest, LoadInvalidPolicyFromCache) { | |
| 530 // Put the invalid test policy in the cache. One of its policies will be | |
| 531 // loaded, the other should be filtered out by the schema. | |
| 532 builder_.payload().set_secure_hash(base::SHA1HashString(kInvalidTestPolicy)); | |
| 533 EXPECT_TRUE(cache_->Store( | |
| 534 "extension-policy", kTestExtension, CreateSerializedResponse())); | |
| 535 EXPECT_TRUE(cache_->Store( | |
| 536 "extension-policy-data", kTestExtension, kInvalidTestPolicy)); | |
| 537 | |
| 538 LoadStore(); | |
| 539 InitializeRegistry(); | |
| 540 | |
| 541 // The initial, cached policy will be served once the backend is initialized. | |
| 542 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 543 RunUntilIdle(); | |
| 544 Mock::VerifyAndClearExpectations(&delegate_); | |
| 545 | |
| 546 PolicyBundle expected_bundle; | |
| 547 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | |
| 548 expected_bundle.Get(ns).Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
| 549 base::Value::CreateStringValue("published"), | |
| 550 NULL); | |
| 551 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | |
| 552 } | |
| 553 | |
| 554 } // namespace policy | |
| OLD | NEW |