Chromium Code Reviews| 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/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 " \"Name\": { \"type\": \"string\" }," | 61 " \"Name\": { \"type\": \"string\" }," |
| 62 " \"Second\": { \"type\": \"string\" }" | 62 " \"Second\": { \"type\": \"string\" }" |
| 63 " }" | 63 " }" |
| 64 "}"; | 64 "}"; |
| 65 | 65 |
| 66 class MockComponentCloudPolicyDelegate | 66 class MockComponentCloudPolicyDelegate |
| 67 : public ComponentCloudPolicyService::Delegate { | 67 : public ComponentCloudPolicyService::Delegate { |
| 68 public: | 68 public: |
| 69 virtual ~MockComponentCloudPolicyDelegate() {} | 69 virtual ~MockComponentCloudPolicyDelegate() {} |
| 70 | 70 |
| 71 MOCK_METHOD0(OnComponentCloudPolicyRefreshNeeded, void()); | |
| 72 MOCK_METHOD0(OnComponentCloudPolicyUpdated, void()); | 71 MOCK_METHOD0(OnComponentCloudPolicyUpdated, void()); |
| 73 }; | 72 }; |
| 74 | 73 |
| 75 class TestURLRequestContextGetter : public net::URLRequestContextGetter { | 74 class TestURLRequestContextGetter : public net::URLRequestContextGetter { |
| 76 public: | 75 public: |
| 77 explicit TestURLRequestContextGetter( | 76 explicit TestURLRequestContextGetter( |
| 78 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 79 : task_runner_(task_runner) {} | 78 : task_runner_(task_runner) {} |
| 80 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE { | 79 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE { |
| 81 return NULL; | 80 return NULL; |
| 82 } | 81 } |
| 83 virtual scoped_refptr<base::SingleThreadTaskRunner> | 82 virtual scoped_refptr<base::SingleThreadTaskRunner> |
| 84 GetNetworkTaskRunner() const OVERRIDE { | 83 GetNetworkTaskRunner() const OVERRIDE { |
| 85 return task_runner_; | 84 return task_runner_; |
| 86 } | 85 } |
| 87 | 86 |
| 88 private: | 87 private: |
| 89 virtual ~TestURLRequestContextGetter() {} | 88 virtual ~TestURLRequestContextGetter() {} |
| 90 | 89 |
| 91 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 90 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 92 }; | 91 }; |
| 93 | 92 |
| 94 } // namespace | 93 } // namespace |
| 95 | 94 |
| 96 class ComponentCloudPolicyServiceTest : public testing::Test { | 95 class ComponentCloudPolicyServiceTest : public testing::Test { |
| 97 protected: | 96 protected: |
| 98 ComponentCloudPolicyServiceTest() {} | 97 ComponentCloudPolicyServiceTest() |
| 98 : client_(NULL), | |
| 99 core_(PolicyNamespaceKey(GetChromeUserPolicyType(), ""), | |
| 100 &store_, | |
| 101 loop_.message_loop_proxy()) {} | |
| 99 | 102 |
| 100 virtual void SetUp() OVERRIDE { | 103 virtual void SetUp() OVERRIDE { |
| 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 102 | 105 |
| 103 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy()); | 106 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy()); |
| 104 request_context_ = | 107 request_context_ = |
| 105 new TestURLRequestContextGetter(loop_.message_loop_proxy()); | 108 new TestURLRequestContextGetter(loop_.message_loop_proxy()); |
| 106 service_.reset(new ComponentCloudPolicyService( | 109 service_.reset(new ComponentCloudPolicyService( |
| 107 &delegate_, | 110 &delegate_, |
| 108 ®istry_, | 111 ®istry_, |
| 109 &store_, | 112 &core_, |
| 110 make_scoped_ptr(cache_), | 113 make_scoped_ptr(cache_), |
| 111 &client_, | |
| 112 request_context_, | 114 request_context_, |
| 113 loop_.message_loop_proxy(), | 115 loop_.message_loop_proxy(), |
| 114 loop_.message_loop_proxy())); | 116 loop_.message_loop_proxy())); |
| 115 | 117 |
| 116 builder_.policy_data().set_policy_type( | 118 builder_.policy_data().set_policy_type( |
| 117 dm_protocol::kChromeExtensionPolicyType); | 119 dm_protocol::kChromeExtensionPolicyType); |
| 118 builder_.policy_data().set_settings_entity_id(kTestExtension); | 120 builder_.policy_data().set_settings_entity_id(kTestExtension); |
| 119 builder_.payload().set_download_url(kTestDownload); | 121 builder_.payload().set_download_url(kTestDownload); |
| 120 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy)); | 122 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy)); |
| 121 | 123 |
| 122 expected_policy_.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 124 expected_policy_.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 123 base::Value::CreateStringValue("disabled"), NULL); | 125 base::Value::CreateStringValue("disabled"), NULL); |
| 124 expected_policy_.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | 126 expected_policy_.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, |
| 125 base::Value::CreateStringValue("maybe"), NULL); | 127 base::Value::CreateStringValue("maybe"), NULL); |
| 126 | |
| 127 EXPECT_EQ(0u, client_.namespaces_to_fetch_.size()); | |
| 128 } | 128 } |
| 129 | 129 |
| 130 virtual void TearDown() OVERRIDE { | 130 virtual void TearDown() OVERRIDE { |
| 131 // The service cleans up its backend on the background 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 Connect(size_t expected_namespaces_in_client) { | |
| 141 client_ = new MockCloudPolicyClient(); | |
| 142 client_->SetDMToken(ComponentPolicyBuilder::kFakeToken); | |
| 143 EXPECT_EQ(0u, client_->namespaces_to_fetch_.size()); | |
| 144 | |
| 145 core_.Connect(scoped_ptr<CloudPolicyClient>(client_)); | |
| 146 | |
| 147 // |expected_namespaces_in_client| is the expected number of components | |
| 148 // that the ComponentCloudPolicyService will set at the |client_| at | |
| 149 // OnCoreConnected. | |
| 150 EXPECT_EQ(expected_namespaces_in_client, | |
| 151 client_->namespaces_to_fetch_.size()); | |
| 152 | |
| 153 // Also initialize the refresh scheduler, so that calls to | |
| 154 // core()->RefreshSoon() trigger a FetchPolicy() call on the mock |client_|. | |
| 155 // Expect the initial refresh now, if the store doesn't have policy (if it | |
| 156 // does then the CloudPolicyRefreshScheduler won't start refreshing until | |
| 157 // invalidations are available, or a timeout elapses). | |
| 158 if (!store_.has_policy()) | |
| 159 EXPECT_CALL(*client_, FetchPolicy()); | |
| 160 core_.StartRefreshScheduler(); | |
| 161 RunUntilIdle(); | |
| 162 Mock::VerifyAndClearExpectations(client_); | |
| 163 } | |
| 164 | |
| 140 void LoadStore() { | 165 void LoadStore() { |
| 141 EXPECT_FALSE(store_.is_initialized()); | 166 EXPECT_FALSE(store_.is_initialized()); |
| 142 | 167 |
| 143 em::PolicyData* data = new em::PolicyData(); | 168 em::PolicyData* data = new em::PolicyData(); |
| 144 data->set_username(ComponentPolicyBuilder::kFakeUsername); | 169 data->set_username(ComponentPolicyBuilder::kFakeUsername); |
| 145 data->set_request_token(ComponentPolicyBuilder::kFakeToken); | 170 data->set_request_token(ComponentPolicyBuilder::kFakeToken); |
| 146 store_.policy_.reset(data); | 171 store_.policy_.reset(data); |
| 147 | 172 |
| 148 store_.NotifyStoreLoaded(); | 173 store_.NotifyStoreLoaded(); |
| 149 RunUntilIdle(); | 174 RunUntilIdle(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 } | 214 } |
| 190 | 215 |
| 191 base::MessageLoop loop_; | 216 base::MessageLoop loop_; |
| 192 base::ScopedTempDir temp_dir_; | 217 base::ScopedTempDir temp_dir_; |
| 193 scoped_refptr<TestURLRequestContextGetter> request_context_; | 218 scoped_refptr<TestURLRequestContextGetter> request_context_; |
| 194 net::TestURLFetcherFactory fetcher_factory_; | 219 net::TestURLFetcherFactory fetcher_factory_; |
| 195 MockComponentCloudPolicyDelegate delegate_; | 220 MockComponentCloudPolicyDelegate delegate_; |
| 196 // |cache_| is owned by the |service_| and is invalid once the |service_| | 221 // |cache_| is owned by the |service_| and is invalid once the |service_| |
| 197 // is destroyed. | 222 // is destroyed. |
| 198 ResourceCache* cache_; | 223 ResourceCache* cache_; |
| 199 MockCloudPolicyClient client_; | 224 MockCloudPolicyClient* client_; |
| 200 MockCloudPolicyStore store_; | 225 MockCloudPolicyStore store_; |
| 226 CloudPolicyCore core_; | |
| 201 SchemaRegistry registry_; | 227 SchemaRegistry registry_; |
| 202 scoped_ptr<ComponentCloudPolicyService> service_; | 228 scoped_ptr<ComponentCloudPolicyService> service_; |
| 203 ComponentPolicyBuilder builder_; | 229 ComponentPolicyBuilder builder_; |
| 204 PolicyMap expected_policy_; | 230 PolicyMap expected_policy_; |
| 205 }; | 231 }; |
| 206 | 232 |
| 207 TEST_F(ComponentCloudPolicyServiceTest, InitializedAtConstructionTime) { | 233 TEST_F(ComponentCloudPolicyServiceTest, InitializedAtConstructionTime) { |
| 208 service_.reset(); | 234 service_.reset(); |
| 235 Connect(1u); | |
| 209 LoadStore(); | 236 LoadStore(); |
| 210 InitializeRegistry(); | 237 InitializeRegistry(); |
| 211 | 238 |
| 212 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy()); | 239 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy()); |
| 213 service_.reset(new ComponentCloudPolicyService(&delegate_, | 240 service_.reset(new ComponentCloudPolicyService(&delegate_, |
| 214 ®istry_, | 241 ®istry_, |
| 215 &store_, | 242 &core_, |
| 216 make_scoped_ptr(cache_), | 243 make_scoped_ptr(cache_), |
| 217 &client_, | |
| 218 request_context_, | 244 request_context_, |
| 219 loop_.message_loop_proxy(), | 245 loop_.message_loop_proxy(), |
| 220 loop_.message_loop_proxy())); | 246 loop_.message_loop_proxy())); |
| 221 EXPECT_FALSE(service_->is_initialized()); | 247 EXPECT_FALSE(service_->is_initialized()); |
| 222 | 248 |
| 223 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); | |
| 224 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 249 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 250 EXPECT_CALL(*client_, FetchPolicy()); | |
| 225 RunUntilIdle(); | 251 RunUntilIdle(); |
| 252 Mock::VerifyAndClearExpectations(&client_); | |
| 226 Mock::VerifyAndClearExpectations(&delegate_); | 253 Mock::VerifyAndClearExpectations(&delegate_); |
| 227 | 254 |
| 228 EXPECT_TRUE(service_->is_initialized()); | 255 EXPECT_TRUE(service_->is_initialized()); |
| 229 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size()); | 256 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size()); |
| 230 const PolicyBundle empty_bundle; | 257 const PolicyBundle empty_bundle; |
| 231 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); | 258 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); |
| 232 } | 259 } |
| 233 | 260 |
| 234 TEST_F(ComponentCloudPolicyServiceTest, InitializeStoreThenRegistry) { | 261 TEST_F(ComponentCloudPolicyServiceTest, InitializeStoreThenRegistry) { |
| 235 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0); | 262 Connect(1u); |
| 263 | |
| 236 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0); | 264 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0); |
| 265 EXPECT_CALL(*client_, FetchPolicy()).Times(0); | |
| 237 LoadStore(); | 266 LoadStore(); |
| 267 Mock::VerifyAndClearExpectations(client_); | |
| 238 Mock::VerifyAndClearExpectations(&delegate_); | 268 Mock::VerifyAndClearExpectations(&delegate_); |
| 239 EXPECT_FALSE(service_->is_initialized()); | 269 EXPECT_FALSE(service_->is_initialized()); |
| 240 | 270 |
| 241 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); | |
| 242 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 271 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 272 EXPECT_CALL(*client_, FetchPolicy()); | |
| 243 InitializeRegistry(); | 273 InitializeRegistry(); |
| 244 RunUntilIdle(); | 274 RunUntilIdle(); |
| 275 Mock::VerifyAndClearExpectations(client_); | |
| 245 Mock::VerifyAndClearExpectations(&delegate_); | 276 Mock::VerifyAndClearExpectations(&delegate_); |
| 246 EXPECT_TRUE(service_->is_initialized()); | 277 EXPECT_TRUE(service_->is_initialized()); |
| 247 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size()); | 278 |
| 248 const PolicyBundle empty_bundle; | 279 const PolicyBundle empty_bundle; |
| 249 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); | 280 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); |
| 250 } | 281 } |
| 251 | 282 |
| 252 TEST_F(ComponentCloudPolicyServiceTest, InitializeRegistryThenStore) { | 283 TEST_F(ComponentCloudPolicyServiceTest, InitializeRegistryThenStore) { |
| 253 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0); | 284 Connect(1u); |
| 285 | |
| 254 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0); | 286 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0); |
| 287 EXPECT_CALL(*client_, FetchPolicy()).Times(0); | |
| 255 InitializeRegistry(); | 288 InitializeRegistry(); |
| 256 RunUntilIdle(); | 289 RunUntilIdle(); |
| 290 Mock::VerifyAndClearExpectations(client_); | |
| 257 Mock::VerifyAndClearExpectations(&delegate_); | 291 Mock::VerifyAndClearExpectations(&delegate_); |
| 258 EXPECT_FALSE(service_->is_initialized()); | 292 EXPECT_FALSE(service_->is_initialized()); |
| 259 | 293 |
| 260 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); | |
| 261 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 294 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 295 EXPECT_CALL(*client_, FetchPolicy()); | |
| 262 LoadStore(); | 296 LoadStore(); |
| 297 Mock::VerifyAndClearExpectations(client_); | |
| 263 Mock::VerifyAndClearExpectations(&delegate_); | 298 Mock::VerifyAndClearExpectations(&delegate_); |
| 264 EXPECT_TRUE(service_->is_initialized()); | 299 EXPECT_TRUE(service_->is_initialized()); |
| 265 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size()); | 300 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size()); |
| 266 const PolicyBundle empty_bundle; | 301 const PolicyBundle empty_bundle; |
| 267 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); | 302 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); |
| 268 } | 303 } |
| 269 | 304 |
| 270 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithCachedPolicy) { | 305 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithCachedPolicy) { |
| 271 PopulateCache(); | 306 PopulateCache(); |
| 307 Connect(1u); | |
| 272 | 308 |
| 273 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); | |
| 274 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 309 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 310 EXPECT_CALL(*client_, FetchPolicy()); | |
| 275 InitializeRegistry(); | 311 InitializeRegistry(); |
| 276 LoadStore(); | 312 LoadStore(); |
| 313 Mock::VerifyAndClearExpectations(client_); | |
| 277 Mock::VerifyAndClearExpectations(&delegate_); | 314 Mock::VerifyAndClearExpectations(&delegate_); |
| 278 | 315 |
| 279 EXPECT_TRUE(service_->is_initialized()); | 316 EXPECT_TRUE(service_->is_initialized()); |
| 280 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size()); | 317 EXPECT_EQ(2u, client_->namespaces_to_fetch_.size()); |
| 281 | 318 |
| 282 // kTestExtension2 is not in the registry so it was dropped. | 319 // kTestExtension2 is not in the registry so it was dropped. |
| 283 std::map<std::string, std::string> contents; | 320 std::map<std::string, std::string> contents; |
| 284 cache_->LoadAllSubkeys("extension-policy", &contents); | 321 cache_->LoadAllSubkeys("extension-policy", &contents); |
| 285 ASSERT_EQ(1u, contents.size()); | 322 ASSERT_EQ(1u, contents.size()); |
| 286 EXPECT_EQ(kTestExtension, contents.begin()->first); | 323 EXPECT_EQ(kTestExtension, contents.begin()->first); |
| 287 | 324 |
| 288 PolicyBundle expected_bundle; | 325 PolicyBundle expected_bundle; |
| 289 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | 326 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); |
| 290 expected_bundle.Get(ns).CopyFrom(expected_policy_); | 327 expected_bundle.Get(ns).CopyFrom(expected_policy_); |
| 291 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | 328 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); |
| 292 } | 329 } |
| 293 | 330 |
| 294 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) { | 331 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) { |
| 332 Connect(1u); | |
| 295 // Initialize the store and create the backend. | 333 // Initialize the store and create the backend. |
| 296 // A refresh is not needed, because no components are registered yet. | 334 // A refresh is not needed, because no components are registered yet. |
| 297 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0); | |
| 298 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 335 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 336 EXPECT_CALL(*client_, FetchPolicy()).Times(0); | |
| 299 registry_.SetReady(POLICY_DOMAIN_CHROME); | 337 registry_.SetReady(POLICY_DOMAIN_CHROME); |
| 300 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); | 338 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); |
| 301 LoadStore(); | 339 LoadStore(); |
| 340 Mock::VerifyAndClearExpectations(client_); | |
| 302 Mock::VerifyAndClearExpectations(&delegate_); | 341 Mock::VerifyAndClearExpectations(&delegate_); |
| 303 EXPECT_TRUE(service_->is_initialized()); | 342 EXPECT_TRUE(service_->is_initialized()); |
| 304 | 343 |
| 305 // Register the components to fetch. | 344 // Register the components to fetch. |
| 306 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); | 345 EXPECT_CALL(*client_, FetchPolicy()); |
| 307 registry_.RegisterComponent( | 346 registry_.RegisterComponent( |
| 308 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), | 347 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), |
| 309 CreateTestSchema()); | 348 CreateTestSchema()); |
| 310 RunUntilIdle(); | 349 RunUntilIdle(); |
| 311 Mock::VerifyAndClearExpectations(&delegate_); | 350 Mock::VerifyAndClearExpectations(client_); |
| 312 | 351 |
| 313 // Send back a fake policy fetch response. | 352 // Send back a fake policy fetch response. |
| 314 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, | 353 client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, |
| 315 kTestExtension), | 354 kTestExtension), |
| 316 *CreateResponse()); | 355 *CreateResponse()); |
| 317 service_->OnPolicyFetched(&client_); | 356 service_->OnPolicyFetched(client_); |
| 318 RunUntilIdle(); | 357 RunUntilIdle(); |
| 319 | 358 |
| 320 // That should have triggered the download fetch. | 359 // That should have triggered the download fetch. |
| 321 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 360 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 322 ASSERT_TRUE(fetcher); | 361 ASSERT_TRUE(fetcher); |
| 323 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | 362 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); |
| 324 fetcher->set_response_code(200); | 363 fetcher->set_response_code(200); |
| 325 fetcher->SetResponseString(kTestPolicy); | 364 fetcher->SetResponseString(kTestPolicy); |
| 326 fetcher->delegate()->OnURLFetchComplete(fetcher); | 365 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 327 | 366 |
| 328 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 367 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 329 RunUntilIdle(); | 368 RunUntilIdle(); |
| 330 Mock::VerifyAndClearExpectations(&delegate_); | 369 Mock::VerifyAndClearExpectations(&delegate_); |
| 331 | 370 |
| 332 // The policy is now being served. | 371 // The policy is now being served. |
| 333 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | 372 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); |
| 334 PolicyBundle expected_bundle; | 373 PolicyBundle expected_bundle; |
| 335 expected_bundle.Get(ns).CopyFrom(expected_policy_); | 374 expected_bundle.Get(ns).CopyFrom(expected_policy_); |
| 336 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | 375 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); |
| 337 } | 376 } |
| 338 | 377 |
| 339 TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) { | 378 TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) { |
| 379 Connect(1u); | |
| 340 // Insert data in the cache. | 380 // Insert data in the cache. |
| 341 PopulateCache(); | 381 PopulateCache(); |
| 342 registry_.RegisterComponent( | 382 registry_.RegisterComponent( |
| 343 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2), | 383 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2), |
| 344 CreateTestSchema()); | 384 CreateTestSchema()); |
| 345 InitializeRegistry(); | 385 InitializeRegistry(); |
| 346 | 386 |
| 347 // Load the initial cache. | 387 // Load the initial cache. |
| 348 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 388 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 349 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); | 389 EXPECT_CALL(*client_, FetchPolicy()); |
| 350 LoadStore(); | 390 LoadStore(); |
| 391 Mock::VerifyAndClearExpectations(client_); | |
| 351 Mock::VerifyAndClearExpectations(&delegate_); | 392 Mock::VerifyAndClearExpectations(&delegate_); |
| 352 | 393 |
| 353 PolicyBundle expected_bundle; | 394 PolicyBundle expected_bundle; |
| 354 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | 395 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); |
| 355 expected_bundle.Get(ns).CopyFrom(expected_policy_); | 396 expected_bundle.Get(ns).CopyFrom(expected_policy_); |
| 356 ns.component_id = kTestExtension2; | 397 ns.component_id = kTestExtension2; |
| 357 expected_bundle.Get(ns).CopyFrom(expected_policy_); | 398 expected_bundle.Get(ns).CopyFrom(expected_policy_); |
| 358 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | 399 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); |
| 359 | 400 |
| 360 // Now purge one of the extensions. | 401 // Now purge one of the extensions. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 371 std::map<std::string, std::string> contents; | 412 std::map<std::string, std::string> contents; |
| 372 cache_->LoadAllSubkeys("extension-policy", &contents); | 413 cache_->LoadAllSubkeys("extension-policy", &contents); |
| 373 EXPECT_EQ(1u, contents.size()); | 414 EXPECT_EQ(1u, contents.size()); |
| 374 EXPECT_TRUE(ContainsKey(contents, kTestExtension2)); | 415 EXPECT_TRUE(ContainsKey(contents, kTestExtension2)); |
| 375 } | 416 } |
| 376 | 417 |
| 377 TEST_F(ComponentCloudPolicyServiceTest, SignInAfterStartup) { | 418 TEST_F(ComponentCloudPolicyServiceTest, SignInAfterStartup) { |
| 378 registry_.SetReady(POLICY_DOMAIN_CHROME); | 419 registry_.SetReady(POLICY_DOMAIN_CHROME); |
| 379 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); | 420 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); |
| 380 | 421 |
| 381 // Do the same as LoadStore() but without the initial credentials. | 422 // Initialize the store without credentials. |
| 382 EXPECT_FALSE(store_.is_initialized()); | 423 EXPECT_FALSE(store_.is_initialized()); |
| 383 EXPECT_FALSE(service_->is_initialized()); | 424 EXPECT_FALSE(service_->is_initialized()); |
| 384 store_.policy_.reset(new em::PolicyData()); // No credentials. | |
| 385 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | 425 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 386 store_.NotifyStoreLoaded(); | 426 store_.NotifyStoreLoaded(); |
| 387 RunUntilIdle(); | 427 RunUntilIdle(); |
| 388 Mock::VerifyAndClearExpectations(&delegate_); | 428 Mock::VerifyAndClearExpectations(&delegate_); |
| 389 EXPECT_TRUE(service_->is_initialized()); | 429 EXPECT_TRUE(service_->is_initialized()); |
| 390 | 430 |
| 391 // Register an extension. | 431 // Register an extension. |
| 392 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); | |
| 393 registry_.RegisterComponent( | 432 registry_.RegisterComponent( |
| 394 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), | 433 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), |
| 395 CreateTestSchema()); | 434 CreateTestSchema()); |
| 396 RunUntilIdle(); | 435 RunUntilIdle(); |
| 397 Mock::VerifyAndClearExpectations(&delegate_); | 436 |
| 437 // Now signin. A fetch will be requested for the new extension. | |
| 438 Connect(2u); | |
| 398 | 439 |
| 399 // Send the response to the service. The response data will be ignored, | 440 // Send the response to the service. The response data will be ignored, |
| 400 // because the store doesn't have the updated credentials yet. | 441 // because the store doesn't have the updated credentials yet. |
| 401 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, | 442 client_->SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, |
| 402 kTestExtension), | 443 kTestExtension), |
| 403 *CreateResponse()); | 444 *CreateResponse()); |
| 404 service_->OnPolicyFetched(&client_); | 445 service_->OnPolicyFetched(client_); |
| 405 RunUntilIdle(); | 446 RunUntilIdle(); |
| 406 | 447 |
| 407 // The policy was ignored, and no download is started. | 448 // The policy was ignored and no download is started because the store |
| 449 // doesn't have credentials. | |
| 408 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 450 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 409 EXPECT_FALSE(fetcher); | 451 EXPECT_FALSE(fetcher); |
| 410 | 452 |
| 411 // Now update the |store_| with the updated policy, which includes | 453 // Now update the |store_| with the updated policy, which includes |
| 412 // credentials. The responses in the |client_| will be reloaded. | 454 // credentials. The responses in the |client_| will be reloaded. |
| 413 em::PolicyData* data = new em::PolicyData(); | 455 em::PolicyData* data = new em::PolicyData(); |
| 414 data->set_username(ComponentPolicyBuilder::kFakeUsername); | 456 data->set_username(ComponentPolicyBuilder::kFakeUsername); |
| 415 data->set_request_token(ComponentPolicyBuilder::kFakeToken); | 457 data->set_request_token(ComponentPolicyBuilder::kFakeToken); |
| 416 store_.policy_.reset(data); | 458 store_.policy_.reset(data); |
| 417 store_.NotifyStoreLoaded(); | 459 store_.NotifyStoreLoaded(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 429 RunUntilIdle(); | 471 RunUntilIdle(); |
| 430 Mock::VerifyAndClearExpectations(&delegate_); | 472 Mock::VerifyAndClearExpectations(&delegate_); |
| 431 | 473 |
| 432 // The policy is now being served. | 474 // The policy is now being served. |
| 433 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | 475 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); |
| 434 PolicyBundle expected_bundle; | 476 PolicyBundle expected_bundle; |
| 435 expected_bundle.Get(ns).CopyFrom(expected_policy_); | 477 expected_bundle.Get(ns).CopyFrom(expected_policy_); |
| 436 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | 478 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); |
| 437 } | 479 } |
| 438 | 480 |
| 481 TEST_F(ComponentCloudPolicyServiceTest, SignOut) { | |
| 482 // Initialize everthing and serve policy for a component. | |
| 483 PopulateCache(); | |
| 484 LoadStore(); | |
| 485 InitializeRegistry(); | |
| 486 | |
| 487 // The initial, cached policy will be served once the backend is initialized. | |
| 488 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 489 RunUntilIdle(); | |
| 490 Mock::VerifyAndClearExpectations(&delegate_); | |
| 491 PolicyBundle expected_bundle; | |
| 492 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | |
| 493 expected_bundle.Get(ns).CopyFrom(expected_policy_); | |
| 494 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); | |
| 495 std::map<std::string, std::string> contents; | |
|
bartfab (slow)
2013/11/18 13:15:39
Nit 1: #include <map>
Nit 2: #include <string>
Joao da Silva
2013/11/18 14:54:18
Done.
| |
| 496 cache_->LoadAllSubkeys("extension-policy", &contents); | |
| 497 ASSERT_EQ(1u, contents.size()); | |
| 498 | |
| 499 // Now signin. | |
|
bartfab (slow)
2013/11/18 13:15:39
Nit: s/signin/sign in/.
Joao da Silva
2013/11/18 14:54:18
Done.
| |
| 500 Connect(2u); | |
| 501 | |
| 502 // Signing out removes all of the component policies from the service and | |
| 503 // from the cache. It does not trigger a refresh. | |
| 504 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); | |
| 505 core_.Disconnect(); | |
| 506 store_.policy_.reset(); | |
| 507 store_.NotifyStoreLoaded(); | |
| 508 RunUntilIdle(); | |
| 509 Mock::VerifyAndClearExpectations(&delegate_); | |
| 510 const PolicyBundle empty_bundle; | |
| 511 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); | |
| 512 cache_->LoadAllSubkeys("extension-policy", &contents); | |
| 513 ASSERT_EQ(0u, contents.size()); | |
| 514 } | |
| 515 | |
| 439 } // namespace policy | 516 } // namespace policy |
| OLD | NEW |