Chromium Code Reviews| 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/user_cloud_policy_store.h" | 5 #include "chrome/browser/policy/user_cloud_policy_store.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/scoped_temp_dir.h" | |
| 9 #include "chrome/browser/policy/policy_builder.h" | 10 #include "chrome/browser/policy/policy_builder.h" |
| 10 #include "chrome/browser/signin/signin_manager.h" | 11 #include "chrome/browser/signin/signin_manager.h" |
| 11 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
| 12 #include "chrome/browser/signin/signin_manager_fake.h" | 13 #include "chrome/browser/signin/signin_manager_fake.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 15 #include "policy/policy_constants.h" | 16 #include "policy/policy_constants.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 38 | 39 |
| 39 class UserCloudPolicyStoreTest : public testing::Test { | 40 class UserCloudPolicyStoreTest : public testing::Test { |
| 40 public: | 41 public: |
| 41 UserCloudPolicyStoreTest() | 42 UserCloudPolicyStoreTest() |
| 42 : loop_(MessageLoop::TYPE_UI), | 43 : loop_(MessageLoop::TYPE_UI), |
| 43 ui_thread_(content::BrowserThread::UI, &loop_), | 44 ui_thread_(content::BrowserThread::UI, &loop_), |
| 44 file_thread_(content::BrowserThread::FILE, &loop_), | 45 file_thread_(content::BrowserThread::FILE, &loop_), |
| 45 profile_(new TestingProfile()) {} | 46 profile_(new TestingProfile()) {} |
| 46 | 47 |
| 47 virtual void SetUp() OVERRIDE { | 48 virtual void SetUp() OVERRIDE { |
| 49 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); | |
| 48 SigninManager* signin = static_cast<SigninManager*>( | 50 SigninManager* signin = static_cast<SigninManager*>( |
| 49 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 51 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 50 profile_.get(), FakeSigninManager::Build)); | 52 profile_.get(), FakeSigninManager::Build)); |
| 51 signin->SetAuthenticatedUsername(PolicyBuilder::kFakeUsername); | 53 signin->SetAuthenticatedUsername(PolicyBuilder::kFakeUsername); |
| 52 store_.reset(new UserCloudPolicyStore(profile_.get())); | 54 store_.reset(new UserCloudPolicyStore(profile_.get(), policy_file())); |
| 53 store_->AddObserver(&observer_); | 55 store_->AddObserver(&observer_); |
| 54 | 56 |
| 55 policy_.payload().mutable_showhomebutton()->set_showhomebutton(true); | 57 policy_.payload().mutable_showhomebutton()->set_showhomebutton(true); |
| 56 policy_.Build(); | 58 policy_.Build(); |
| 57 } | 59 } |
| 58 | 60 |
| 59 virtual void TearDown() OVERRIDE { | 61 virtual void TearDown() OVERRIDE { |
| 60 store_->RemoveObserver(&observer_); | 62 store_->RemoveObserver(&observer_); |
| 61 store_.reset(); | 63 store_.reset(); |
| 62 base::RunLoop run_loop; | 64 base::RunLoop run_loop; |
| 63 run_loop.RunUntilIdle(); | 65 run_loop.RunUntilIdle(); |
| 64 } | 66 } |
| 65 | 67 |
| 68 FilePath policy_file() { | |
| 69 return tmp_dir_.path().AppendASCII("policy"); | |
| 70 } | |
| 71 | |
| 66 // Verifies that store_->policy_map() has the ShowHomeButton entry. | 72 // Verifies that store_->policy_map() has the ShowHomeButton entry. |
| 67 void VerifyPolicyMap() { | 73 void VerifyPolicyMap(CloudPolicyStore* store) { |
| 68 EXPECT_EQ(1U, store_->policy_map().size()); | 74 EXPECT_EQ(1U, store->policy_map().size()); |
| 69 const PolicyMap::Entry* entry = | 75 const PolicyMap::Entry* entry = |
| 70 store_->policy_map().Get(key::kShowHomeButton); | 76 store->policy_map().Get(key::kShowHomeButton); |
| 71 ASSERT_TRUE(entry); | 77 ASSERT_TRUE(entry); |
| 72 EXPECT_TRUE(base::FundamentalValue(true).Equals(entry->value)); | 78 EXPECT_TRUE(base::FundamentalValue(true).Equals(entry->value)); |
| 73 } | 79 } |
| 74 | 80 |
| 75 // Install an expectation on |observer_| for an error code. | 81 // Install an expectation on |observer_| for an error code. |
| 76 void ExpectError(CloudPolicyStore::Status error) { | 82 void ExpectError(CloudPolicyStore* store, CloudPolicyStore::Status error) { |
| 77 EXPECT_CALL(observer_, | 83 EXPECT_CALL(observer_, |
| 78 OnStoreError(AllOf(Eq(store_.get()), | 84 OnStoreError(AllOf(Eq(store), |
| 79 Property(&CloudPolicyStore::status, | 85 Property(&CloudPolicyStore::status, |
| 80 Eq(error))))); | 86 Eq(error))))); |
| 81 } | 87 } |
| 82 | 88 |
| 83 UserPolicyBuilder policy_; | 89 UserPolicyBuilder policy_; |
| 84 MockCloudPolicyStoreObserver observer_; | 90 MockCloudPolicyStoreObserver observer_; |
| 85 scoped_ptr<UserCloudPolicyStore> store_; | 91 scoped_ptr<UserCloudPolicyStore> store_; |
| 86 | 92 |
| 87 // CloudPolicyValidator() requires a FILE thread so declare one here. Both | 93 // CloudPolicyValidator() requires a FILE thread so declare one here. Both |
| 88 // |ui_thread_| and |file_thread_| share the same MessageLoop |loop_| so | 94 // |ui_thread_| and |file_thread_| share the same MessageLoop |loop_| so |
| 89 // callers can use RunLoop to manage both virtual threads. | 95 // callers can use RunLoop to manage both virtual threads. |
| 90 MessageLoop loop_; | 96 MessageLoop loop_; |
| 91 content::TestBrowserThread ui_thread_; | 97 content::TestBrowserThread ui_thread_; |
| 92 content::TestBrowserThread file_thread_; | 98 content::TestBrowserThread file_thread_; |
| 93 | 99 |
| 94 scoped_ptr<TestingProfile> profile_; | 100 scoped_ptr<TestingProfile> profile_; |
| 101 ScopedTempDir tmp_dir_; | |
| 95 | 102 |
| 96 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreTest); | 103 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreTest); |
| 97 }; | 104 }; |
| 98 | 105 |
| 106 TEST_F(UserCloudPolicyStoreTest, LoadWithNoFile) { | |
| 107 EXPECT_FALSE(store_->policy()); | |
| 108 EXPECT_TRUE(store_->policy_map().empty()); | |
| 109 | |
| 110 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 111 store_->Load(); | |
| 112 base::RunLoop run_loop; | |
| 113 run_loop.RunUntilIdle(); | |
| 114 | |
| 115 EXPECT_FALSE(store_->policy()); | |
| 116 EXPECT_TRUE(store_->policy_map().empty()); | |
| 117 } | |
| 118 | |
| 99 TEST_F(UserCloudPolicyStoreTest, Store) { | 119 TEST_F(UserCloudPolicyStoreTest, Store) { |
| 100 EXPECT_FALSE(store_->policy()); | 120 EXPECT_FALSE(store_->policy()); |
| 101 EXPECT_TRUE(store_->policy_map().empty()); | 121 EXPECT_TRUE(store_->policy_map().empty()); |
| 102 | 122 |
| 103 // Store a simple policy and make sure it ends up as the currently active | 123 // Store a simple policy and make sure it ends up as the currently active |
| 104 // policy. | 124 // policy. |
| 105 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | 125 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); |
| 106 store_->Store(policy_.policy()); | 126 store_->Store(policy_.policy()); |
| 107 base::RunLoop run_loop; | 127 base::RunLoop run_loop; |
| 108 run_loop.RunUntilIdle(); | 128 run_loop.RunUntilIdle(); |
| 109 | 129 |
| 110 // Policy should be decoded and stored. | 130 // Policy should be decoded and stored. |
| 111 ASSERT_TRUE(store_->policy()); | 131 ASSERT_TRUE(store_->policy()); |
| 112 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | 132 EXPECT_EQ(policy_.policy_data().SerializeAsString(), |
| 113 store_->policy()->SerializeAsString()); | 133 store_->policy()->SerializeAsString()); |
| 114 VerifyPolicyMap(); | 134 VerifyPolicyMap(store_.get()); |
| 115 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | 135 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); |
| 116 } | 136 } |
| 117 | 137 |
| 138 TEST_F(UserCloudPolicyStoreTest, StoreTwoTimes) { | |
| 139 EXPECT_FALSE(store_->policy()); | |
| 140 EXPECT_TRUE(store_->policy_map().empty()); | |
| 141 | |
| 142 // Store a simple policy then store a second policy before the first one | |
| 143 // finishes validating, and make sure the second policy ends up as the active | |
| 144 // policy. | |
| 145 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).Times(2); | |
| 146 | |
| 147 UserPolicyBuilder first_policy; | |
| 148 first_policy.payload().mutable_showhomebutton()->set_showhomebutton(false); | |
| 149 first_policy.Build(); | |
| 150 store_->Store(first_policy.policy()); | |
| 151 store_->Store(policy_.policy()); | |
| 152 | |
| 153 base::RunLoop run_loop; | |
| 154 run_loop.RunUntilIdle(); | |
| 155 | |
| 156 // Policy should be decoded and stored. | |
| 157 ASSERT_TRUE(store_->policy()); | |
| 158 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
| 159 store_->policy()->SerializeAsString()); | |
| 160 VerifyPolicyMap(store_.get()); | |
|
Mattias Nissler (ping if slow)
2012/08/20 15:29:30
shouldn't this fail now that show home button is f
Andrew T Wilson (Slow)
2012/08/20 18:04:37
No, because the first policy I store is the one wi
Mattias Nissler (ping if slow)
2012/08/21 10:45:16
True, I had missed that. Makes sense then.
| |
| 161 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | |
| 162 } | |
| 163 | |
| 164 TEST_F(UserCloudPolicyStoreTest, StoreThenLoad) { | |
| 165 // Store a simple policy and make sure it can be read back in. | |
| 166 // policy. | |
| 167 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 168 store_->Store(policy_.policy()); | |
| 169 { | |
| 170 base::RunLoop run_loop; | |
| 171 run_loop.RunUntilIdle(); | |
| 172 } | |
| 173 | |
| 174 // Now, make sure the policy can be read back in from a second store. | |
| 175 scoped_ptr<UserCloudPolicyStore> store2( | |
| 176 new UserCloudPolicyStore(profile_.get(), policy_file())); | |
| 177 store2->AddObserver(&observer_); | |
| 178 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); | |
| 179 store2->Load(); | |
| 180 { | |
| 181 base::RunLoop run_loop; | |
| 182 run_loop.RunUntilIdle(); | |
| 183 } | |
| 184 ASSERT_TRUE(store2->policy()); | |
| 185 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
| 186 store2->policy()->SerializeAsString()); | |
| 187 VerifyPolicyMap(store2.get()); | |
| 188 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); | |
| 189 store2->RemoveObserver(&observer_); | |
| 190 } | |
| 191 | |
| 118 TEST_F(UserCloudPolicyStoreTest, StoreValidationError) { | 192 TEST_F(UserCloudPolicyStoreTest, StoreValidationError) { |
| 119 // Create an invalid policy (no policy type). | 193 // Create an invalid policy (no policy type). |
| 120 policy_.policy_data().clear_policy_type(); | 194 policy_.policy_data().clear_policy_type(); |
| 121 policy_.Build(); | 195 policy_.Build(); |
| 122 | 196 |
| 123 // Store policy. | 197 // Store policy. |
| 124 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR); | 198 ExpectError(store_.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); |
| 125 store_->Store(policy_.policy()); | 199 store_->Store(policy_.policy()); |
| 126 base::RunLoop run_loop; | 200 base::RunLoop run_loop; |
| 127 run_loop.RunUntilIdle(); | 201 run_loop.RunUntilIdle(); |
| 128 ASSERT_FALSE(store_->policy()); | 202 ASSERT_FALSE(store_->policy()); |
| 129 } | 203 } |
| 130 | 204 |
| 205 TEST_F(UserCloudPolicyStoreTest, LoadValidationError) { | |
| 206 // Force a validation error by changing the username after policy is stored. | |
| 207 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 208 store_->Store(policy_.policy()); | |
| 209 { | |
| 210 base::RunLoop run_loop; | |
| 211 run_loop.RunUntilIdle(); | |
| 212 } | |
| 213 | |
| 214 // Sign out, and sign back in as a different user, and try to load the profile | |
| 215 // data (should fail due to mismatched username). | |
| 216 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); | |
| 217 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | |
| 218 "foobar@foobar.com"); | |
| 219 | |
| 220 scoped_ptr<UserCloudPolicyStore> store2( | |
| 221 new UserCloudPolicyStore(profile_.get(), policy_file())); | |
| 222 store2->AddObserver(&observer_); | |
| 223 ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | |
| 224 store2->Load(); | |
| 225 { | |
| 226 base::RunLoop run_loop; | |
| 227 run_loop.RunUntilIdle(); | |
| 228 } | |
| 229 ASSERT_FALSE(store2->policy()); | |
| 230 store2->RemoveObserver(&observer_); | |
| 231 } | |
| 232 | |
| 131 } // namespace | 233 } // namespace |
| 132 | 234 |
| 133 } // namespace policy | 235 } // namespace policy |
| 134 | |
| OLD | NEW |