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