| 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" | |
| 12 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 13 #include "chrome/browser/policy/cloud/mock_cloud_external_data_manager.h" | 12 #include "chrome/browser/policy/cloud/mock_cloud_external_data_manager.h" |
| 14 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | 13 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" |
| 15 #include "chrome/browser/policy/cloud/policy_builder.h" | 14 #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" | |
| 20 #include "chrome/test/base/testing_profile.h" | |
| 21 #include "net/url_request/url_request_context_getter.h" | 15 #include "net/url_request/url_request_context_getter.h" |
| 22 #include "policy/policy_constants.h" | 16 #include "policy/policy_constants.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 19 |
| 26 using testing::AllOf; | 20 using testing::AllOf; |
| 27 using testing::Eq; | 21 using testing::Eq; |
| 28 using testing::Mock; | 22 using testing::Mock; |
| 29 using testing::Property; | 23 using testing::Property; |
| 30 using testing::Sequence; | 24 using testing::Sequence; |
| 31 | 25 |
| 32 namespace policy { | 26 namespace policy { |
| 33 | 27 |
| 34 namespace { | 28 namespace { |
| 35 | 29 |
| 36 void RunUntilIdle() { | 30 void RunUntilIdle() { |
| 37 base::RunLoop run_loop; | 31 base::RunLoop run_loop; |
| 38 run_loop.RunUntilIdle(); | 32 run_loop.RunUntilIdle(); |
| 39 } | 33 } |
| 40 | 34 |
| 41 class UserCloudPolicyStoreTest : public testing::Test { | 35 class UserCloudPolicyStoreTest : public testing::Test { |
| 42 public: | 36 public: |
| 43 UserCloudPolicyStoreTest() | 37 UserCloudPolicyStoreTest() : loop_(base::MessageLoop::TYPE_UI) {} |
| 44 : loop_(base::MessageLoop::TYPE_UI), | |
| 45 profile_(new TestingProfile()) {} | |
| 46 | 38 |
| 47 virtual void SetUp() OVERRIDE { | 39 virtual void SetUp() OVERRIDE { |
| 48 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); | 40 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); |
| 49 SigninManager* signin = static_cast<SigninManager*>( | 41 store_.reset( |
| 50 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 42 new UserCloudPolicyStore(policy_file(), loop_.message_loop_proxy())); |
| 51 profile_.get(), FakeSigninManager::Build)); | |
| 52 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | |
| 53 PolicyBuilder::kFakeUsername); | |
| 54 signin->Initialize(profile_.get(), NULL); | |
| 55 store_.reset(new UserCloudPolicyStore( | |
| 56 profile_.get(), policy_file(), loop_.message_loop_proxy())); | |
| 57 external_data_manager_.reset(new MockCloudExternalDataManager); | 43 external_data_manager_.reset(new MockCloudExternalDataManager); |
| 58 external_data_manager_->SetPolicyStore(store_.get()); | 44 external_data_manager_->SetPolicyStore(store_.get()); |
| 45 store_->SetSigninUsername(PolicyBuilder::kFakeUsername); |
| 59 store_->AddObserver(&observer_); | 46 store_->AddObserver(&observer_); |
| 60 | 47 |
| 61 policy_.payload().mutable_passwordmanagerenabled()->set_value(true); | 48 policy_.payload().mutable_passwordmanagerenabled()->set_value(true); |
| 62 policy_.payload().mutable_urlblacklist()->mutable_value()->add_entries( | 49 policy_.payload().mutable_urlblacklist()->mutable_value()->add_entries( |
| 63 "chromium.org"); | 50 "chromium.org"); |
| 64 | 51 |
| 65 policy_.Build(); | 52 policy_.Build(); |
| 66 } | 53 } |
| 67 | 54 |
| 68 virtual void TearDown() OVERRIDE { | 55 virtual void TearDown() OVERRIDE { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 97 UserPolicyBuilder policy_; | 84 UserPolicyBuilder policy_; |
| 98 MockCloudPolicyStoreObserver observer_; | 85 MockCloudPolicyStoreObserver observer_; |
| 99 scoped_ptr<UserCloudPolicyStore> store_; | 86 scoped_ptr<UserCloudPolicyStore> store_; |
| 100 scoped_ptr<MockCloudExternalDataManager> external_data_manager_; | 87 scoped_ptr<MockCloudExternalDataManager> external_data_manager_; |
| 101 | 88 |
| 102 // CloudPolicyValidator() requires a FILE thread so declare one here. Both | 89 // CloudPolicyValidator() requires a FILE thread so declare one here. Both |
| 103 // |ui_thread_| and |file_thread_| share the same MessageLoop |loop_| so | 90 // |ui_thread_| and |file_thread_| share the same MessageLoop |loop_| so |
| 104 // callers can use RunLoop to manage both virtual threads. | 91 // callers can use RunLoop to manage both virtual threads. |
| 105 base::MessageLoop loop_; | 92 base::MessageLoop loop_; |
| 106 | 93 |
| 107 scoped_ptr<TestingProfile> profile_; | |
| 108 base::ScopedTempDir tmp_dir_; | 94 base::ScopedTempDir tmp_dir_; |
| 109 | 95 |
| 110 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreTest); | 96 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreTest); |
| 111 }; | 97 }; |
| 112 | 98 |
| 113 TEST_F(UserCloudPolicyStoreTest, LoadWithNoFile) { | 99 TEST_F(UserCloudPolicyStoreTest, LoadWithNoFile) { |
| 114 EXPECT_FALSE(store_->policy()); | 100 EXPECT_FALSE(store_->policy()); |
| 115 EXPECT_TRUE(store_->policy_map().empty()); | 101 EXPECT_TRUE(store_->policy_map().empty()); |
| 116 | 102 |
| 117 Sequence s; | 103 Sequence s; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 TEST_F(UserCloudPolicyStoreTest, StoreThenLoad) { | 256 TEST_F(UserCloudPolicyStoreTest, StoreThenLoad) { |
| 271 // Store a simple policy and make sure it can be read back in. | 257 // Store a simple policy and make sure it can be read back in. |
| 272 // policy. | 258 // policy. |
| 273 Sequence s; | 259 Sequence s; |
| 274 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); | 260 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); |
| 275 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); | 261 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); |
| 276 store_->Store(policy_.policy()); | 262 store_->Store(policy_.policy()); |
| 277 RunUntilIdle(); | 263 RunUntilIdle(); |
| 278 | 264 |
| 279 // Now, make sure the policy can be read back in from a second store. | 265 // Now, make sure the policy can be read back in from a second store. |
| 280 scoped_ptr<UserCloudPolicyStore> store2(new UserCloudPolicyStore( | 266 scoped_ptr<UserCloudPolicyStore> store2( |
| 281 profile_.get(), policy_file(), loop_.message_loop_proxy())); | 267 new UserCloudPolicyStore(policy_file(), loop_.message_loop_proxy())); |
| 268 store2->SetSigninUsername(PolicyBuilder::kFakeUsername); |
| 282 store2->AddObserver(&observer_); | 269 store2->AddObserver(&observer_); |
| 283 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); | 270 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); |
| 284 store2->Load(); | 271 store2->Load(); |
| 285 RunUntilIdle(); | 272 RunUntilIdle(); |
| 286 | 273 |
| 287 ASSERT_TRUE(store2->policy()); | 274 ASSERT_TRUE(store2->policy()); |
| 288 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | 275 EXPECT_EQ(policy_.policy_data().SerializeAsString(), |
| 289 store2->policy()->SerializeAsString()); | 276 store2->policy()->SerializeAsString()); |
| 290 VerifyPolicyMap(store2.get()); | 277 VerifyPolicyMap(store2.get()); |
| 291 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); | 278 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); |
| 292 store2->RemoveObserver(&observer_); | 279 store2->RemoveObserver(&observer_); |
| 293 } | 280 } |
| 294 | 281 |
| 295 TEST_F(UserCloudPolicyStoreTest, StoreThenLoadImmediately) { | 282 TEST_F(UserCloudPolicyStoreTest, StoreThenLoadImmediately) { |
| 296 // Store a simple policy and make sure it can be read back in. | 283 // Store a simple policy and make sure it can be read back in. |
| 297 // policy. | 284 // policy. |
| 298 Sequence s; | 285 Sequence s; |
| 299 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); | 286 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); |
| 300 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); | 287 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); |
| 301 store_->Store(policy_.policy()); | 288 store_->Store(policy_.policy()); |
| 302 RunUntilIdle(); | 289 RunUntilIdle(); |
| 303 | 290 |
| 304 // Now, make sure the policy can be read back in from a second store. | 291 // Now, make sure the policy can be read back in from a second store. |
| 305 scoped_ptr<UserCloudPolicyStore> store2(new UserCloudPolicyStore( | 292 scoped_ptr<UserCloudPolicyStore> store2( |
| 306 profile_.get(), policy_file(), loop_.message_loop_proxy())); | 293 new UserCloudPolicyStore(policy_file(), loop_.message_loop_proxy())); |
| 294 store2->SetSigninUsername(PolicyBuilder::kFakeUsername); |
| 307 store2->AddObserver(&observer_); | 295 store2->AddObserver(&observer_); |
| 308 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); | 296 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); |
| 309 store2->LoadImmediately(); // Should load without running the message loop. | 297 store2->LoadImmediately(); // Should load without running the message loop. |
| 310 | 298 |
| 311 ASSERT_TRUE(store2->policy()); | 299 ASSERT_TRUE(store2->policy()); |
| 312 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | 300 EXPECT_EQ(policy_.policy_data().SerializeAsString(), |
| 313 store2->policy()->SerializeAsString()); | 301 store2->policy()->SerializeAsString()); |
| 314 VerifyPolicyMap(store2.get()); | 302 VerifyPolicyMap(store2.get()); |
| 315 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); | 303 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); |
| 316 store2->RemoveObserver(&observer_); | 304 store2->RemoveObserver(&observer_); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 331 TEST_F(UserCloudPolicyStoreTest, LoadValidationError) { | 319 TEST_F(UserCloudPolicyStoreTest, LoadValidationError) { |
| 332 // Force a validation error by changing the username after policy is stored. | 320 // Force a validation error by changing the username after policy is stored. |
| 333 Sequence s; | 321 Sequence s; |
| 334 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); | 322 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); |
| 335 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); | 323 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); |
| 336 store_->Store(policy_.policy()); | 324 store_->Store(policy_.policy()); |
| 337 RunUntilIdle(); | 325 RunUntilIdle(); |
| 338 | 326 |
| 339 // Sign out, and sign back in as a different user, and try to load the profile | 327 // Sign out, and sign back in as a different user, and try to load the profile |
| 340 // data (should fail due to mismatched username). | 328 // data (should fail due to mismatched username). |
| 341 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); | 329 scoped_ptr<UserCloudPolicyStore> store2( |
| 342 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | 330 new UserCloudPolicyStore(policy_file(), loop_.message_loop_proxy())); |
| 343 "foobar@foobar.com"); | 331 store2->SetSigninUsername("foobar@foobar.com"); |
| 344 | |
| 345 scoped_ptr<UserCloudPolicyStore> store2(new UserCloudPolicyStore( | |
| 346 profile_.get(), policy_file(), loop_.message_loop_proxy())); | |
| 347 store2->AddObserver(&observer_); | 332 store2->AddObserver(&observer_); |
| 348 ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | 333 ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); |
| 349 store2->Load(); | 334 store2->Load(); |
| 350 RunUntilIdle(); | 335 RunUntilIdle(); |
| 351 | 336 |
| 352 ASSERT_FALSE(store2->policy()); | 337 ASSERT_FALSE(store2->policy()); |
| 353 store2->RemoveObserver(&observer_); | 338 store2->RemoveObserver(&observer_); |
| 354 | 339 |
| 355 // Sign out - we should be able to load the policy (don't check usernames | 340 // Sign out - we should be able to load the policy (don't check usernames |
| 356 // when signed out). | 341 // when signed out). |
| 357 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); | 342 scoped_ptr<UserCloudPolicyStore> store3( |
| 358 scoped_ptr<UserCloudPolicyStore> store3(new UserCloudPolicyStore( | 343 new UserCloudPolicyStore(policy_file(), loop_.message_loop_proxy())); |
| 359 profile_.get(), policy_file(), loop_.message_loop_proxy())); | |
| 360 store3->AddObserver(&observer_); | 344 store3->AddObserver(&observer_); |
| 361 EXPECT_CALL(observer_, OnStoreLoaded(store3.get())); | 345 EXPECT_CALL(observer_, OnStoreLoaded(store3.get())); |
| 362 store3->Load(); | 346 store3->Load(); |
| 363 RunUntilIdle(); | 347 RunUntilIdle(); |
| 364 | 348 |
| 365 ASSERT_TRUE(store3->policy()); | 349 ASSERT_TRUE(store3->policy()); |
| 366 store3->RemoveObserver(&observer_); | 350 store3->RemoveObserver(&observer_); |
| 367 | 351 |
| 368 // Now start a signin as a different user - this should fail validation. | 352 // Now start a signin as a different user - this should fail validation. |
| 369 FakeSigninManager* signin = static_cast<FakeSigninManager*>( | 353 scoped_ptr<UserCloudPolicyStore> store4( |
| 370 SigninManagerFactory::GetForProfile(profile_.get())); | 354 new UserCloudPolicyStore(policy_file(), loop_.message_loop_proxy())); |
| 371 signin->set_auth_in_progress("foobar@foobar.com"); | 355 store4->SetSigninUsername("foobar@foobar.com"); |
| 372 | |
| 373 scoped_ptr<UserCloudPolicyStore> store4(new UserCloudPolicyStore( | |
| 374 profile_.get(), policy_file(), loop_.message_loop_proxy())); | |
| 375 store4->AddObserver(&observer_); | 356 store4->AddObserver(&observer_); |
| 376 ExpectError(store4.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | 357 ExpectError(store4.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); |
| 377 store4->Load(); | 358 store4->Load(); |
| 378 RunUntilIdle(); | 359 RunUntilIdle(); |
| 379 | 360 |
| 380 ASSERT_FALSE(store4->policy()); | 361 ASSERT_FALSE(store4->policy()); |
| 381 store4->RemoveObserver(&observer_); | 362 store4->RemoveObserver(&observer_); |
| 382 } | 363 } |
| 383 | 364 |
| 384 } // namespace | 365 } // namespace |
| 385 | 366 |
| 386 } // namespace policy | 367 } // namespace policy |
| OLD | NEW |