Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Unified Diff: chrome/browser/policy/user_cloud_policy_store_unittest.cc

Issue 10825415: Added code to persist downloaded cloud policy to disk. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed debugging statements that are unneeded. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/user_cloud_policy_store_unittest.cc
diff --git a/chrome/browser/policy/user_cloud_policy_store_unittest.cc b/chrome/browser/policy/user_cloud_policy_store_unittest.cc
index aef93ed0c5ae5500d85c37f29e033b7634179992..87c8939d659d7fdb0e1b30a95e534f5cf855a724 100644
--- a/chrome/browser/policy/user_cloud_policy_store_unittest.cc
+++ b/chrome/browser/policy/user_cloud_policy_store_unittest.cc
@@ -4,8 +4,10 @@
#include "chrome/browser/policy/user_cloud_policy_store.h"
+#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/run_loop.h"
+#include "base/scoped_temp_dir.h"
#include "chrome/browser/policy/policy_builder.h"
#include "chrome/browser/signin/signin_manager.h"
#include "chrome/browser/signin/signin_manager_factory.h"
@@ -24,6 +26,11 @@ namespace policy {
namespace {
+void RunUntilIdle() {
+ base::RunLoop run_loop;
+ run_loop.RunUntilIdle();
+}
+
class MockCloudPolicyStoreObserver : public CloudPolicyStore::Observer {
public:
MockCloudPolicyStoreObserver() {}
@@ -45,11 +52,12 @@ class UserCloudPolicyStoreTest : public testing::Test {
profile_(new TestingProfile()) {}
virtual void SetUp() OVERRIDE {
+ ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
SigninManager* signin = static_cast<SigninManager*>(
SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
profile_.get(), FakeSigninManager::Build));
signin->SetAuthenticatedUsername(PolicyBuilder::kFakeUsername);
- store_.reset(new UserCloudPolicyStore(profile_.get()));
+ store_.reset(new UserCloudPolicyStore(profile_.get(), policy_file()));
store_->AddObserver(&observer_);
policy_.payload().mutable_showhomebutton()->set_showhomebutton(true);
@@ -59,23 +67,26 @@ class UserCloudPolicyStoreTest : public testing::Test {
virtual void TearDown() OVERRIDE {
store_->RemoveObserver(&observer_);
store_.reset();
- base::RunLoop run_loop;
- run_loop.RunUntilIdle();
+ RunUntilIdle();
+ }
+
+ FilePath policy_file() {
+ return tmp_dir_.path().AppendASCII("policy");
}
// Verifies that store_->policy_map() has the ShowHomeButton entry.
- void VerifyPolicyMap() {
- EXPECT_EQ(1U, store_->policy_map().size());
+ void VerifyPolicyMap(CloudPolicyStore* store) {
+ EXPECT_EQ(1U, store->policy_map().size());
const PolicyMap::Entry* entry =
- store_->policy_map().Get(key::kShowHomeButton);
+ store->policy_map().Get(key::kShowHomeButton);
ASSERT_TRUE(entry);
EXPECT_TRUE(base::FundamentalValue(true).Equals(entry->value));
}
// Install an expectation on |observer_| for an error code.
- void ExpectError(CloudPolicyStore::Status error) {
+ void ExpectError(CloudPolicyStore* store, CloudPolicyStore::Status error) {
EXPECT_CALL(observer_,
- OnStoreError(AllOf(Eq(store_.get()),
+ OnStoreError(AllOf(Eq(store),
Property(&CloudPolicyStore::status,
Eq(error)))));
}
@@ -92,10 +103,43 @@ class UserCloudPolicyStoreTest : public testing::Test {
content::TestBrowserThread file_thread_;
scoped_ptr<TestingProfile> profile_;
+ ScopedTempDir tmp_dir_;
DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreTest);
};
+TEST_F(UserCloudPolicyStoreTest, LoadWithNoFile) {
+ EXPECT_FALSE(store_->policy());
+ EXPECT_TRUE(store_->policy_map().empty());
+
+ EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
+ store_->Load();
+ RunUntilIdle();
+
+ EXPECT_FALSE(store_->policy());
+ EXPECT_TRUE(store_->policy_map().empty());
+}
+
+TEST_F(UserCloudPolicyStoreTest, LoadWithInvalidFile) {
+ EXPECT_FALSE(store_->policy());
+ EXPECT_TRUE(store_->policy_map().empty());
+
+ // Create a bogus file.
+ ASSERT_TRUE(file_util::CreateDirectory(policy_file().DirName()));
+ std::string bogus_data = "bogus_data";
+ int size = bogus_data.size();
+ ASSERT_EQ(size, file_util::WriteFile(policy_file(),
+ bogus_data.c_str(),
+ bogus_data.size()));
+
+ ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR);
+ store_->Load();
+ RunUntilIdle();
+
+ EXPECT_FALSE(store_->policy());
+ EXPECT_TRUE(store_->policy_map().empty());
+}
+
TEST_F(UserCloudPolicyStoreTest, Store) {
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
@@ -104,31 +148,129 @@ TEST_F(UserCloudPolicyStoreTest, Store) {
// policy.
EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
store_->Store(policy_.policy());
- base::RunLoop run_loop;
- run_loop.RunUntilIdle();
+ RunUntilIdle();
+
+ // Policy should be decoded and stored.
+ ASSERT_TRUE(store_->policy());
+ EXPECT_EQ(policy_.policy_data().SerializeAsString(),
+ store_->policy()->SerializeAsString());
+ VerifyPolicyMap(store_.get());
+ EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
+}
+
+TEST_F(UserCloudPolicyStoreTest, StoreThenClear) {
+ EXPECT_FALSE(store_->policy());
+ EXPECT_TRUE(store_->policy_map().empty());
+
+ // Store a simple policy and make sure the file exists.
+ // policy.
+ EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
+ store_->Store(policy_.policy());
+ RunUntilIdle();
+
+ EXPECT_TRUE(store_->policy());
+ EXPECT_FALSE(store_->policy_map().empty());
+
+ // Policy file should exist.
+ ASSERT_TRUE(file_util::PathExists(policy_file()));
+
+ EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
+ store_->Clear();
+ RunUntilIdle();
+
+ // Policy file should not exist.
+ ASSERT_TRUE(!file_util::PathExists(policy_file()));
+
+ // Policy should be gone.
+ EXPECT_FALSE(store_->policy());
+ EXPECT_TRUE(store_->policy_map().empty());
+ EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
+}
+
+TEST_F(UserCloudPolicyStoreTest, StoreTwoTimes) {
+ EXPECT_FALSE(store_->policy());
+ EXPECT_TRUE(store_->policy_map().empty());
+
+ // Store a simple policy then store a second policy before the first one
+ // finishes validating, and make sure the second policy ends up as the active
+ // policy.
+ EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).Times(2);
+
+ UserPolicyBuilder first_policy;
+ first_policy.payload().mutable_showhomebutton()->set_showhomebutton(false);
+ first_policy.Build();
+ store_->Store(first_policy.policy());
+ RunUntilIdle();
+
+ store_->Store(policy_.policy());
+ RunUntilIdle();
// Policy should be decoded and stored.
ASSERT_TRUE(store_->policy());
EXPECT_EQ(policy_.policy_data().SerializeAsString(),
store_->policy()->SerializeAsString());
- VerifyPolicyMap();
+ VerifyPolicyMap(store_.get());
EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
}
+TEST_F(UserCloudPolicyStoreTest, StoreThenLoad) {
+ // Store a simple policy and make sure it can be read back in.
+ // policy.
+ EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
+ store_->Store(policy_.policy());
+ RunUntilIdle();
+
+ // Now, make sure the policy can be read back in from a second store.
+ scoped_ptr<UserCloudPolicyStore> store2(
+ new UserCloudPolicyStore(profile_.get(), policy_file()));
+ store2->AddObserver(&observer_);
+ EXPECT_CALL(observer_, OnStoreLoaded(store2.get()));
+ store2->Load();
+ RunUntilIdle();
+
+ ASSERT_TRUE(store2->policy());
+ EXPECT_EQ(policy_.policy_data().SerializeAsString(),
+ store2->policy()->SerializeAsString());
+ VerifyPolicyMap(store2.get());
+ EXPECT_EQ(CloudPolicyStore::STATUS_OK, store2->status());
+ store2->RemoveObserver(&observer_);
+}
+
TEST_F(UserCloudPolicyStoreTest, StoreValidationError) {
// Create an invalid policy (no policy type).
policy_.policy_data().clear_policy_type();
policy_.Build();
// Store policy.
- ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR);
+ ExpectError(store_.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR);
store_->Store(policy_.policy());
- base::RunLoop run_loop;
- run_loop.RunUntilIdle();
+ RunUntilIdle();
ASSERT_FALSE(store_->policy());
}
+TEST_F(UserCloudPolicyStoreTest, LoadValidationError) {
+ // Force a validation error by changing the username after policy is stored.
+ EXPECT_CALL(observer_, OnStoreLoaded(store_.get()));
+ store_->Store(policy_.policy());
+ RunUntilIdle();
+
+ // Sign out, and sign back in as a different user, and try to load the profile
+ // data (should fail due to mismatched username).
+ SigninManagerFactory::GetForProfile(profile_.get())->SignOut();
+ SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername(
+ "foobar@foobar.com");
+
+ scoped_ptr<UserCloudPolicyStore> store2(
+ new UserCloudPolicyStore(profile_.get(), policy_file()));
+ store2->AddObserver(&observer_);
+ ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR);
+ store2->Load();
+ RunUntilIdle();
+
+ ASSERT_FALSE(store2->policy());
+ store2->RemoveObserver(&observer_);
+}
+
} // namespace
} // namespace policy
-

Powered by Google App Engine
This is Rietveld 408576698