| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/user_cloud_policy_store.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/files/scoped_temp_dir.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "chrome/browser/policy/mock_cloud_policy_store.h" | |
| 12 #include "chrome/browser/policy/policy_builder.h" | |
| 13 #include "chrome/browser/signin/signin_manager.h" | |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 15 #include "chrome/browser/signin/signin_manager_fake.h" | |
| 16 #include "chrome/test/base/testing_profile.h" | |
| 17 #include "content/public/test/test_browser_thread.h" | |
| 18 #include "policy/policy_constants.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 using testing::AllOf; | |
| 23 using testing::Eq; | |
| 24 using testing::Property; | |
| 25 | |
| 26 namespace policy { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 void RunUntilIdle() { | |
| 31 base::RunLoop run_loop; | |
| 32 run_loop.RunUntilIdle(); | |
| 33 } | |
| 34 | |
| 35 class UserCloudPolicyStoreTest : public testing::Test { | |
| 36 public: | |
| 37 UserCloudPolicyStoreTest() | |
| 38 : loop_(MessageLoop::TYPE_UI), | |
| 39 ui_thread_(content::BrowserThread::UI, &loop_), | |
| 40 file_thread_(content::BrowserThread::FILE, &loop_), | |
| 41 profile_(new TestingProfile()) {} | |
| 42 | |
| 43 virtual void SetUp() OVERRIDE { | |
| 44 ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); | |
| 45 SigninManager* signin = static_cast<SigninManager*>( | |
| 46 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 47 profile_.get(), FakeSigninManager::Build)); | |
| 48 signin->SetAuthenticatedUsername(PolicyBuilder::kFakeUsername); | |
| 49 store_.reset(new UserCloudPolicyStore(profile_.get(), policy_file())); | |
| 50 store_->AddObserver(&observer_); | |
| 51 | |
| 52 policy_.payload().mutable_showhomebutton()->set_value(true); | |
| 53 policy_.payload().mutable_syncdisabled()->set_value(true); | |
| 54 policy_.Build(); | |
| 55 } | |
| 56 | |
| 57 virtual void TearDown() OVERRIDE { | |
| 58 store_->RemoveObserver(&observer_); | |
| 59 store_.reset(); | |
| 60 RunUntilIdle(); | |
| 61 } | |
| 62 | |
| 63 FilePath policy_file() { | |
| 64 return tmp_dir_.path().AppendASCII("policy"); | |
| 65 } | |
| 66 | |
| 67 // Verifies that store_->policy_map() has the appropriate entries. | |
| 68 void VerifyPolicyMap(CloudPolicyStore* store) { | |
| 69 EXPECT_EQ(1U, store->policy_map().size()); | |
| 70 const PolicyMap::Entry* entry = | |
| 71 store->policy_map().Get(key::kShowHomeButton); | |
| 72 ASSERT_TRUE(entry); | |
| 73 EXPECT_TRUE(base::FundamentalValue(true).Equals(entry->value)); | |
| 74 | |
| 75 // SyncDisabled policy should be filtered out. | |
| 76 ASSERT_FALSE(store->policy_map().Get(key::kSyncDisabled)); | |
| 77 } | |
| 78 | |
| 79 // Install an expectation on |observer_| for an error code. | |
| 80 void ExpectError(CloudPolicyStore* store, CloudPolicyStore::Status error) { | |
| 81 EXPECT_CALL(observer_, | |
| 82 OnStoreError(AllOf(Eq(store), | |
| 83 Property(&CloudPolicyStore::status, | |
| 84 Eq(error))))); | |
| 85 } | |
| 86 | |
| 87 UserPolicyBuilder policy_; | |
| 88 MockCloudPolicyStoreObserver observer_; | |
| 89 scoped_ptr<UserCloudPolicyStore> store_; | |
| 90 | |
| 91 // CloudPolicyValidator() requires a FILE thread so declare one here. Both | |
| 92 // |ui_thread_| and |file_thread_| share the same MessageLoop |loop_| so | |
| 93 // callers can use RunLoop to manage both virtual threads. | |
| 94 MessageLoop loop_; | |
| 95 content::TestBrowserThread ui_thread_; | |
| 96 content::TestBrowserThread file_thread_; | |
| 97 | |
| 98 scoped_ptr<TestingProfile> profile_; | |
| 99 base::ScopedTempDir tmp_dir_; | |
| 100 | |
| 101 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreTest); | |
| 102 }; | |
| 103 | |
| 104 TEST_F(UserCloudPolicyStoreTest, LoadWithNoFile) { | |
| 105 EXPECT_FALSE(store_->policy()); | |
| 106 EXPECT_TRUE(store_->policy_map().empty()); | |
| 107 | |
| 108 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 109 store_->Load(); | |
| 110 RunUntilIdle(); | |
| 111 | |
| 112 EXPECT_FALSE(store_->policy()); | |
| 113 EXPECT_TRUE(store_->policy_map().empty()); | |
| 114 } | |
| 115 | |
| 116 TEST_F(UserCloudPolicyStoreTest, LoadWithInvalidFile) { | |
| 117 EXPECT_FALSE(store_->policy()); | |
| 118 EXPECT_TRUE(store_->policy_map().empty()); | |
| 119 | |
| 120 // Create a bogus file. | |
| 121 ASSERT_TRUE(file_util::CreateDirectory(policy_file().DirName())); | |
| 122 std::string bogus_data = "bogus_data"; | |
| 123 int size = bogus_data.size(); | |
| 124 ASSERT_EQ(size, file_util::WriteFile(policy_file(), | |
| 125 bogus_data.c_str(), | |
| 126 bogus_data.size())); | |
| 127 | |
| 128 ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR); | |
| 129 store_->Load(); | |
| 130 RunUntilIdle(); | |
| 131 | |
| 132 EXPECT_FALSE(store_->policy()); | |
| 133 EXPECT_TRUE(store_->policy_map().empty()); | |
| 134 } | |
| 135 | |
| 136 TEST_F(UserCloudPolicyStoreTest, LoadImmediatelyWithNoFile) { | |
| 137 EXPECT_FALSE(store_->policy()); | |
| 138 EXPECT_TRUE(store_->policy_map().empty()); | |
| 139 | |
| 140 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 141 store_->LoadImmediately(); // Should load without running the message loop. | |
| 142 | |
| 143 EXPECT_FALSE(store_->policy()); | |
| 144 EXPECT_TRUE(store_->policy_map().empty()); | |
| 145 } | |
| 146 | |
| 147 TEST_F(UserCloudPolicyStoreTest, LoadImmediatelyWithInvalidFile) { | |
| 148 EXPECT_FALSE(store_->policy()); | |
| 149 EXPECT_TRUE(store_->policy_map().empty()); | |
| 150 | |
| 151 // Create a bogus file. | |
| 152 ASSERT_TRUE(file_util::CreateDirectory(policy_file().DirName())); | |
| 153 std::string bogus_data = "bogus_data"; | |
| 154 int size = bogus_data.size(); | |
| 155 ASSERT_EQ(size, file_util::WriteFile(policy_file(), | |
| 156 bogus_data.c_str(), | |
| 157 bogus_data.size())); | |
| 158 | |
| 159 ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR); | |
| 160 store_->LoadImmediately(); // Should load without running the message loop. | |
| 161 | |
| 162 EXPECT_FALSE(store_->policy()); | |
| 163 EXPECT_TRUE(store_->policy_map().empty()); | |
| 164 } | |
| 165 | |
| 166 TEST_F(UserCloudPolicyStoreTest, Store) { | |
| 167 EXPECT_FALSE(store_->policy()); | |
| 168 EXPECT_TRUE(store_->policy_map().empty()); | |
| 169 | |
| 170 // Store a simple policy and make sure it ends up as the currently active | |
| 171 // policy. | |
| 172 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 173 store_->Store(policy_.policy()); | |
| 174 RunUntilIdle(); | |
| 175 | |
| 176 // Policy should be decoded and stored. | |
| 177 ASSERT_TRUE(store_->policy()); | |
| 178 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
| 179 store_->policy()->SerializeAsString()); | |
| 180 VerifyPolicyMap(store_.get()); | |
| 181 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | |
| 182 } | |
| 183 | |
| 184 TEST_F(UserCloudPolicyStoreTest, StoreThenClear) { | |
| 185 EXPECT_FALSE(store_->policy()); | |
| 186 EXPECT_TRUE(store_->policy_map().empty()); | |
| 187 | |
| 188 // Store a simple policy and make sure the file exists. | |
| 189 // policy. | |
| 190 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 191 store_->Store(policy_.policy()); | |
| 192 RunUntilIdle(); | |
| 193 | |
| 194 EXPECT_TRUE(store_->policy()); | |
| 195 EXPECT_FALSE(store_->policy_map().empty()); | |
| 196 | |
| 197 // Policy file should exist. | |
| 198 ASSERT_TRUE(file_util::PathExists(policy_file())); | |
| 199 | |
| 200 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 201 store_->Clear(); | |
| 202 RunUntilIdle(); | |
| 203 | |
| 204 // Policy file should not exist. | |
| 205 ASSERT_TRUE(!file_util::PathExists(policy_file())); | |
| 206 | |
| 207 // Policy should be gone. | |
| 208 EXPECT_FALSE(store_->policy()); | |
| 209 EXPECT_TRUE(store_->policy_map().empty()); | |
| 210 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | |
| 211 } | |
| 212 | |
| 213 TEST_F(UserCloudPolicyStoreTest, StoreTwoTimes) { | |
| 214 EXPECT_FALSE(store_->policy()); | |
| 215 EXPECT_TRUE(store_->policy_map().empty()); | |
| 216 | |
| 217 // Store a simple policy then store a second policy before the first one | |
| 218 // finishes validating, and make sure the second policy ends up as the active | |
| 219 // policy. | |
| 220 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).Times(2); | |
| 221 | |
| 222 UserPolicyBuilder first_policy; | |
| 223 first_policy.payload().mutable_showhomebutton()->set_value(false); | |
| 224 first_policy.Build(); | |
| 225 store_->Store(first_policy.policy()); | |
| 226 RunUntilIdle(); | |
| 227 | |
| 228 store_->Store(policy_.policy()); | |
| 229 RunUntilIdle(); | |
| 230 | |
| 231 // Policy should be decoded and stored. | |
| 232 ASSERT_TRUE(store_->policy()); | |
| 233 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
| 234 store_->policy()->SerializeAsString()); | |
| 235 VerifyPolicyMap(store_.get()); | |
| 236 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); | |
| 237 } | |
| 238 | |
| 239 TEST_F(UserCloudPolicyStoreTest, StoreThenLoad) { | |
| 240 // Store a simple policy and make sure it can be read back in. | |
| 241 // policy. | |
| 242 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 243 store_->Store(policy_.policy()); | |
| 244 RunUntilIdle(); | |
| 245 | |
| 246 // Now, make sure the policy can be read back in from a second store. | |
| 247 scoped_ptr<UserCloudPolicyStore> store2( | |
| 248 new UserCloudPolicyStore(profile_.get(), policy_file())); | |
| 249 store2->AddObserver(&observer_); | |
| 250 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); | |
| 251 store2->Load(); | |
| 252 RunUntilIdle(); | |
| 253 | |
| 254 ASSERT_TRUE(store2->policy()); | |
| 255 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
| 256 store2->policy()->SerializeAsString()); | |
| 257 VerifyPolicyMap(store2.get()); | |
| 258 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); | |
| 259 store2->RemoveObserver(&observer_); | |
| 260 } | |
| 261 | |
| 262 TEST_F(UserCloudPolicyStoreTest, StoreThenLoadImmediately) { | |
| 263 // Store a simple policy and make sure it can be read back in. | |
| 264 // policy. | |
| 265 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 266 store_->Store(policy_.policy()); | |
| 267 RunUntilIdle(); | |
| 268 | |
| 269 // Now, make sure the policy can be read back in from a second store. | |
| 270 scoped_ptr<UserCloudPolicyStore> store2( | |
| 271 new UserCloudPolicyStore(profile_.get(), policy_file())); | |
| 272 store2->AddObserver(&observer_); | |
| 273 EXPECT_CALL(observer_, OnStoreLoaded(store2.get())); | |
| 274 store2->LoadImmediately(); // Should load without running the message loop. | |
| 275 | |
| 276 ASSERT_TRUE(store2->policy()); | |
| 277 EXPECT_EQ(policy_.policy_data().SerializeAsString(), | |
| 278 store2->policy()->SerializeAsString()); | |
| 279 VerifyPolicyMap(store2.get()); | |
| 280 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status()); | |
| 281 store2->RemoveObserver(&observer_); | |
| 282 } | |
| 283 | |
| 284 TEST_F(UserCloudPolicyStoreTest, StoreValidationError) { | |
| 285 // Create an invalid policy (no policy type). | |
| 286 policy_.policy_data().clear_policy_type(); | |
| 287 policy_.Build(); | |
| 288 | |
| 289 // Store policy. | |
| 290 ExpectError(store_.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | |
| 291 store_->Store(policy_.policy()); | |
| 292 RunUntilIdle(); | |
| 293 ASSERT_FALSE(store_->policy()); | |
| 294 } | |
| 295 | |
| 296 TEST_F(UserCloudPolicyStoreTest, LoadValidationError) { | |
| 297 // Force a validation error by changing the username after policy is stored. | |
| 298 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())); | |
| 299 store_->Store(policy_.policy()); | |
| 300 RunUntilIdle(); | |
| 301 | |
| 302 // Sign out, and sign back in as a different user, and try to load the profile | |
| 303 // data (should fail due to mismatched username). | |
| 304 SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); | |
| 305 SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( | |
| 306 "foobar@foobar.com"); | |
| 307 | |
| 308 scoped_ptr<UserCloudPolicyStore> store2( | |
| 309 new UserCloudPolicyStore(profile_.get(), policy_file())); | |
| 310 store2->AddObserver(&observer_); | |
| 311 ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | |
| 312 store2->Load(); | |
| 313 RunUntilIdle(); | |
| 314 | |
| 315 ASSERT_FALSE(store2->policy()); | |
| 316 store2->RemoveObserver(&observer_); | |
| 317 } | |
| 318 | |
| 319 } // namespace | |
| 320 | |
| 321 } // namespace policy | |
| OLD | NEW |