| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/user_cloud_policy_store.h" | 5 #include "chrome/browser/policy/cloud/user_cloud_policy_store.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.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/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "chrome/browser/policy/cloud/mock_cloud_external_data_manager.h" | 13 #include "chrome/browser/policy/cloud/mock_cloud_external_data_manager.h" |
| 14 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | 14 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" |
| 15 #include "chrome/browser/policy/cloud/policy_builder.h" | 15 #include "chrome/browser/policy/cloud/policy_builder.h" |
| 16 #include "chrome/browser/signin/fake_signin_manager.h" | |
| 17 #include "chrome/browser/signin/signin_manager.h" | |
| 18 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 19 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 21 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
| 22 #include "policy/policy_constants.h" | 19 #include "policy/policy_constants.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 22 |
| 26 using testing::AllOf; | 23 using testing::AllOf; |
| 27 using testing::Eq; | 24 using testing::Eq; |
| 28 using testing::Mock; | 25 using testing::Mock; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 39 } | 36 } |
| 40 | 37 |
| 41 class UserCloudPolicyStoreTest : public testing::Test { | 38 class UserCloudPolicyStoreTest : public testing::Test { |
| 42 public: | 39 public: |
| 43 UserCloudPolicyStoreTest() | 40 UserCloudPolicyStoreTest() |
| 44 : loop_(base::MessageLoop::TYPE_UI), | 41 : loop_(base::MessageLoop::TYPE_UI), |
| 45 profile_(new TestingProfile()) {} | 42 profile_(new TestingProfile()) {} |
| 46 | 43 |
| 47 virtual void SetUp() OVERRIDE { | 44 virtual void SetUp() OVERRIDE { |
| 48 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); | 45 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); |
| 49 SigninManager* signin = static_cast<SigninManager*>( | |
| 50 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 51 profile_.get(), FakeSigninManager::Build)); | |
| 52 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | 46 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, |
| 53 PolicyBuilder::kFakeUsername); | 47 PolicyBuilder::kFakeUsername); |
| 54 signin->Initialize(profile_.get(), NULL); | |
| 55 store_.reset(new UserCloudPolicyStore( | 48 store_.reset(new UserCloudPolicyStore( |
| 56 profile_.get(), policy_file(), loop_.message_loop_proxy())); | 49 profile_.get(), policy_file(), loop_.message_loop_proxy())); |
| 57 external_data_manager_.reset(new MockCloudExternalDataManager); | 50 external_data_manager_.reset(new MockCloudExternalDataManager); |
| 58 external_data_manager_->SetPolicyStore(store_.get()); | 51 external_data_manager_->SetPolicyStore(store_.get()); |
| 52 store_->SetSigninUsername(PolicyBuilder::kFakeUsername); |
| 59 store_->AddObserver(&observer_); | 53 store_->AddObserver(&observer_); |
| 60 | 54 |
| 61 policy_.payload().mutable_passwordmanagerenabled()->set_value(true); | 55 policy_.payload().mutable_passwordmanagerenabled()->set_value(true); |
| 62 policy_.payload().mutable_urlblacklist()->mutable_value()->add_entries( | 56 policy_.payload().mutable_urlblacklist()->mutable_value()->add_entries( |
| 63 "chromium.org"); | 57 "chromium.org"); |
| 64 | 58 |
| 65 policy_.Build(); | 59 policy_.Build(); |
| 66 } | 60 } |
| 67 | 61 |
| 68 virtual void TearDown() OVERRIDE { | 62 virtual void TearDown() OVERRIDE { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 // policy. | 266 // policy. |
| 273 Sequence s; | 267 Sequence s; |
| 274 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); | 268 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); |
| 275 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); | 269 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); |
| 276 store_->Store(policy_.policy()); | 270 store_->Store(policy_.policy()); |
| 277 RunUntilIdle(); | 271 RunUntilIdle(); |
| 278 | 272 |
| 279 // Now, make sure the policy can be read back in from a second store. | 273 // Now, make sure the policy can be read back in from a second store. |
| 280 scoped_ptr<UserCloudPolicyStore> store2(new UserCloudPolicyStore( | 274 scoped_ptr<UserCloudPolicyStore> store2(new UserCloudPolicyStore( |
| 281 profile_.get(), policy_file(), loop_.message_loop_proxy())); | 275 profile_.get(), policy_file(), loop_.message_loop_proxy())); |
| 276 store2->SetSigninUsername(PolicyBuilder::kFakeUsername); |
| 282 store2->AddObserver(&observer_); | 277 store2->AddObserver(&observer_); |
| 283 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); | 278 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); |
| 284 store2->Load(); | 279 store2->Load(); |
| 285 RunUntilIdle(); | 280 RunUntilIdle(); |
| 286 | 281 |
| 287 ASSERT_TRUE(store2->policy()); | 282 ASSERT_TRUE(store2->policy()); |
| 288 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | 283 EXPECT_EQ(policy_.policy_data().SerializeAsString(), |
| 289 store2->policy()->SerializeAsString()); | 284 store2->policy()->SerializeAsString()); |
| 290 VerifyPolicyMap(store2.get()); | 285 VerifyPolicyMap(store2.get()); |
| 291 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); | 286 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); |
| 292 store2->RemoveObserver(&observer_); | 287 store2->RemoveObserver(&observer_); |
| 293 } | 288 } |
| 294 | 289 |
| 295 TEST_F(UserCloudPolicyStoreTest, StoreThenLoadImmediately) { | 290 TEST_F(UserCloudPolicyStoreTest, StoreThenLoadImmediately) { |
| 296 // Store a simple policy and make sure it can be read back in. | 291 // Store a simple policy and make sure it can be read back in. |
| 297 // policy. | 292 // policy. |
| 298 Sequence s; | 293 Sequence s; |
| 299 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); | 294 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); |
| 300 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); | 295 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); |
| 301 store_->Store(policy_.policy()); | 296 store_->Store(policy_.policy()); |
| 302 RunUntilIdle(); | 297 RunUntilIdle(); |
| 303 | 298 |
| 304 // Now, make sure the policy can be read back in from a second store. | 299 // Now, make sure the policy can be read back in from a second store. |
| 305 scoped_ptr<UserCloudPolicyStore> store2(new UserCloudPolicyStore( | 300 scoped_ptr<UserCloudPolicyStore> store2(new UserCloudPolicyStore( |
| 306 profile_.get(), policy_file(), loop_.message_loop_proxy())); | 301 profile_.get(), policy_file(), loop_.message_loop_proxy())); |
| 302 store2->SetSigninUsername(PolicyBuilder::kFakeUsername); |
| 307 store2->AddObserver(&observer_); | 303 store2->AddObserver(&observer_); |
| 308 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); | 304 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); |
| 309 store2->LoadImmediately(); // Should load without running the message loop. | 305 store2->LoadImmediately(); // Should load without running the message loop. |
| 310 | 306 |
| 311 ASSERT_TRUE(store2->policy()); | 307 ASSERT_TRUE(store2->policy()); |
| 312 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | 308 EXPECT_EQ(policy_.policy_data().SerializeAsString(), |
| 313 store2->policy()->SerializeAsString()); | 309 store2->policy()->SerializeAsString()); |
| 314 VerifyPolicyMap(store2.get()); | 310 VerifyPolicyMap(store2.get()); |
| 315 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); | 311 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); |
| 316 store2->RemoveObserver(&observer_); | 312 store2->RemoveObserver(&observer_); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 331 TEST_F(UserCloudPolicyStoreTest, LoadValidationError) { | 327 TEST_F(UserCloudPolicyStoreTest, LoadValidationError) { |
| 332 // Force a validation error by changing the username after policy is stored. | 328 // Force a validation error by changing the username after policy is stored. |
| 333 Sequence s; | 329 Sequence s; |
| 334 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); | 330 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); |
| 335 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); | 331 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); |
| 336 store_->Store(policy_.policy()); | 332 store_->Store(policy_.policy()); |
| 337 RunUntilIdle(); | 333 RunUntilIdle(); |
| 338 | 334 |
| 339 // Sign out, and sign back in as a different user, and try to load the profile | 335 // Sign out, and sign back in as a different user, and try to load the profile |
| 340 // data (should fail due to mismatched username). | 336 // data (should fail due to mismatched username). |
| 341 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); | |
| 342 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | |
| 343 "foobar@foobar.com"); | |
| 344 | |
| 345 scoped_ptr<UserCloudPolicyStore> store2(new UserCloudPolicyStore( | 337 scoped_ptr<UserCloudPolicyStore> store2(new UserCloudPolicyStore( |
| 346 profile_.get(), policy_file(), loop_.message_loop_proxy())); | 338 profile_.get(), policy_file(), loop_.message_loop_proxy())); |
| 339 store2->SetSigninUsername("foobar@foobar.com"); |
| 347 store2->AddObserver(&observer_); | 340 store2->AddObserver(&observer_); |
| 348 ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | 341 ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); |
| 349 store2->Load(); | 342 store2->Load(); |
| 350 RunUntilIdle(); | 343 RunUntilIdle(); |
| 351 | 344 |
| 352 ASSERT_FALSE(store2->policy()); | 345 ASSERT_FALSE(store2->policy()); |
| 353 store2->RemoveObserver(&observer_); | 346 store2->RemoveObserver(&observer_); |
| 354 | 347 |
| 355 // Sign out - we should be able to load the policy (don't check usernames | 348 // Sign out - we should be able to load the policy (don't check usernames |
| 356 // when signed out). | 349 // when signed out). |
| 357 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); | |
| 358 scoped_ptr<UserCloudPolicyStore> store3(new UserCloudPolicyStore( | 350 scoped_ptr<UserCloudPolicyStore> store3(new UserCloudPolicyStore( |
| 359 profile_.get(), policy_file(), loop_.message_loop_proxy())); | 351 profile_.get(), policy_file(), loop_.message_loop_proxy())); |
| 360 store3->AddObserver(&observer_); | 352 store3->AddObserver(&observer_); |
| 361 EXPECT_CALL(observer_, OnStoreLoaded(store3.get())); | 353 EXPECT_CALL(observer_, OnStoreLoaded(store3.get())); |
| 362 store3->Load(); | 354 store3->Load(); |
| 363 RunUntilIdle(); | 355 RunUntilIdle(); |
| 364 | 356 |
| 365 ASSERT_TRUE(store3->policy()); | 357 ASSERT_TRUE(store3->policy()); |
| 366 store3->RemoveObserver(&observer_); | 358 store3->RemoveObserver(&observer_); |
| 367 | 359 |
| 368 // Now start a signin as a different user - this should fail validation. | 360 // Now start a signin as a different user - this should fail validation. |
| 369 FakeSigninManager* signin = static_cast<FakeSigninManager*>( | |
| 370 SigninManagerFactory::GetForProfile(profile_.get())); | |
| 371 signin->set_auth_in_progress("foobar@foobar.com"); | |
| 372 | |
| 373 scoped_ptr<UserCloudPolicyStore> store4(new UserCloudPolicyStore( | 361 scoped_ptr<UserCloudPolicyStore> store4(new UserCloudPolicyStore( |
| 374 profile_.get(), policy_file(), loop_.message_loop_proxy())); | 362 profile_.get(), policy_file(), loop_.message_loop_proxy())); |
| 363 store4->SetSigninUsername("foobar@foobar.com"); |
| 375 store4->AddObserver(&observer_); | 364 store4->AddObserver(&observer_); |
| 376 ExpectError(store4.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | 365 ExpectError(store4.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); |
| 377 store4->Load(); | 366 store4->Load(); |
| 378 RunUntilIdle(); | 367 RunUntilIdle(); |
| 379 | 368 |
| 380 ASSERT_FALSE(store4->policy()); | 369 ASSERT_FALSE(store4->policy()); |
| 381 store4->RemoveObserver(&observer_); | 370 store4->RemoveObserver(&observer_); |
| 382 } | 371 } |
| 383 | 372 |
| 384 } // namespace | 373 } // namespace |
| 385 | 374 |
| 386 } // namespace policy | 375 } // namespace policy |
| OLD | NEW |