This source file includes following definitions.
- RunUntilIdle
- WriteStringToFile
- SetUp
- TearDown
- InitPolicyPayload
- policy_file
- key_file
- VerifyPolicyMap
- ExpectError
- StorePolicyAndEnsureLoaded
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "components/policy/core/common/cloud/user_cloud_policy_store.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/run_loop.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/mock_cloud_external_data_manager.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "components/policy/core/common/cloud/policy_builder.h"
#include "components/policy/core/common/policy_switches.h"
#include "net/url_request/url_request_context_getter.h"
#include "policy/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::AllOf;
using testing::Eq;
using testing::Mock;
using testing::Property;
using testing::Sequence;
namespace policy {
namespace {
void RunUntilIdle() {
base::RunLoop run_loop;
run_loop.RunUntilIdle();
}
bool WriteStringToFile(const base::FilePath path, const std::string& data) {
if (!base::CreateDirectory(path.DirName())) {
DLOG(WARNING) << "Failed to create directory " << path.DirName().value();
return false;
}
int size = data.size();
if (base::WriteFile(path, data.c_str(), size) != size) {
DLOG(WARNING) << "Failed to write " << path.value();
return false;
}
return true;
}
}
class UserCloudPolicyStoreTest : public testing::Test {
public:
UserCloudPolicyStoreTest() {}
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
store_.reset(
new UserCloudPolicyStore(policy_file(),
key_file(),
GetPolicyVerificationKey(),
loop_.message_loop_proxy()));
external_data_manager_.reset(new MockCloudExternalDataManager);
external_data_manager_->SetPolicyStore(store_.get());
store_->SetSigninUsername(PolicyBuilder::kFakeUsername);
store_->AddObserver(&observer_);
policy_.SetDefaultInitialSigningKey();
InitPolicyPayload(&policy_.payload());
policy_.Build();
}
virtual void TearDown() OVERRIDE {
store_->RemoveObserver(&observer_);
external_data_manager_.reset();
store_.reset();
RunUntilIdle();
}
void InitPolicyPayload(enterprise_management::CloudPolicySettings* payload) {
payload->mutable_passwordmanagerenabled()->set_value(true);
payload->mutable_urlblacklist()->mutable_value()->add_entries(
"chromium.org");
}
base::FilePath policy_file() {
return tmp_dir_.path().AppendASCII("policy");
}
base::FilePath key_file() {
return tmp_dir_.path().AppendASCII("policy_key");
}
void VerifyPolicyMap(CloudPolicyStore* store) {
EXPECT_EQ(2U, store->policy_map().size());
const PolicyMap::Entry* entry =
store->policy_map().Get(key::kPasswordManagerEnabled);
ASSERT_TRUE(entry);
EXPECT_TRUE(base::FundamentalValue(true).Equals(entry->value));
ASSERT_TRUE(store->policy_map().Get(key::kURLBlacklist));
}
void ExpectError(CloudPolicyStore* store, CloudPolicyStore::Status error) {
EXPECT_CALL(observer_,
OnStoreError(AllOf(Eq(store),
Property(&CloudPolicyStore::status,
Eq(error)))));
}
void StorePolicyAndEnsureLoaded(
const enterprise_management::PolicyFetchResponse& policy) {
Sequence s;
EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s);
EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s);
store_->Store(policy);
RunUntilIdle();
Mock::VerifyAndClearExpectations(external_data_manager_.get());
Mock::VerifyAndClearExpectations(&observer_);
ASSERT_TRUE(store_->policy());
}
UserPolicyBuilder policy_;
MockCloudPolicyStoreObserver observer_;
scoped_ptr<UserCloudPolicyStore> store_;
scoped_ptr<MockCloudExternalDataManager> external_data_manager_;
base::MessageLoopForUI loop_;
base::ScopedTempDir tmp_dir_;
DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreTest);
};
TEST_F(UserCloudPolicyStoreTest, LoadWithNoFile) {
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
Sequence s;
EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s);
EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s);
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());
ASSERT_TRUE(base::CreateDirectory(policy_file().DirName()));
std::string bogus_data = "bogus_data";
int size = bogus_data.size();
ASSERT_EQ(size, base::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, LoadImmediatelyWithNoFile) {
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
Sequence s;
EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s);
EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s);
store_->LoadImmediately();
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
}
TEST_F(UserCloudPolicyStoreTest, LoadImmediatelyWithInvalidFile) {
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
ASSERT_TRUE(base::CreateDirectory(policy_file().DirName()));
std::string bogus_data = "bogus_data";
int size = bogus_data.size();
ASSERT_EQ(size, base::WriteFile(policy_file(),
bogus_data.c_str(), bogus_data.size()));
ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR);
store_->LoadImmediately();
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
}
TEST_F(UserCloudPolicyStoreTest, Migration) {
UserPolicyBuilder unsigned_builder;
unsigned_builder.UnsetSigningKey();
InitPolicyPayload(&unsigned_builder.payload());
unsigned_builder.Build();
EXPECT_FALSE(unsigned_builder.policy().has_policy_data_signature());
std::string data;
ASSERT_TRUE(unsigned_builder.policy().SerializeToString(&data));
ASSERT_TRUE(base::CreateDirectory(policy_file().DirName()));
int size = data.size();
ASSERT_EQ(size, base::WriteFile(policy_file(), data.c_str(), size));
Sequence s;
EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s);
EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s);
store_->LoadImmediately();
Mock::VerifyAndClearExpectations(external_data_manager_.get());
Mock::VerifyAndClearExpectations(&observer_);
ASSERT_TRUE(store_->policy());
EXPECT_EQ(unsigned_builder.policy_data().SerializeAsString(),
store_->policy()->SerializeAsString());
VerifyPolicyMap(store_.get());
EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
EXPECT_TRUE(store_->policy_key().empty());
EXPECT_FALSE(base::PathExists(key_file()));
StorePolicyAndEnsureLoaded(policy_.policy());
EXPECT_EQ(policy_.policy().new_public_key(), store_->policy_key());
EXPECT_TRUE(store_->policy()->has_public_key_version());
EXPECT_TRUE(base::PathExists(key_file()));
}
TEST_F(UserCloudPolicyStoreTest, Store) {
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
StorePolicyAndEnsureLoaded(policy_.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());
StorePolicyAndEnsureLoaded(policy_.policy());
EXPECT_FALSE(store_->policy_map().empty());
ASSERT_TRUE(base::PathExists(policy_file()));
Sequence s2;
EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s2);
EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s2);
store_->Clear();
RunUntilIdle();
ASSERT_TRUE(!base::PathExists(policy_file()));
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
}
TEST_F(UserCloudPolicyStoreTest, StoreRotatedKey) {
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
StorePolicyAndEnsureLoaded(policy_.policy());
EXPECT_FALSE(policy_.policy().has_new_public_key_signature());
std::string original_policy_key = policy_.policy().new_public_key();
EXPECT_EQ(original_policy_key, store_->policy_key());
policy_.SetDefaultSigningKey();
policy_.SetDefaultNewSigningKey();
policy_.Build();
EXPECT_TRUE(policy_.policy().has_new_public_key_signature());
EXPECT_NE(original_policy_key, policy_.policy().new_public_key());
StorePolicyAndEnsureLoaded(policy_.policy());
EXPECT_EQ(policy_.policy().new_public_key(), store_->policy_key());
}
TEST_F(UserCloudPolicyStoreTest, ProvisionKeyTwice) {
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
StorePolicyAndEnsureLoaded(policy_.policy());
policy_.UnsetSigningKey();
policy_.SetDefaultNewSigningKey();
policy_.Build();
EXPECT_FALSE(policy_.policy().has_new_public_key_signature());
ExpectError(store_.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR);
store_->Store(policy_.policy());
RunUntilIdle();
}
TEST_F(UserCloudPolicyStoreTest, StoreTwoTimes) {
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
UserPolicyBuilder first_policy;
first_policy.SetDefaultInitialSigningKey();
first_policy.payload().mutable_passwordmanagerenabled()->set_value(false);
first_policy.Build();
StorePolicyAndEnsureLoaded(first_policy.policy());
policy_.UnsetNewSigningKey();
policy_.SetDefaultSigningKey();
policy_.Build();
ASSERT_FALSE(policy_.policy().has_new_public_key());
StorePolicyAndEnsureLoaded(policy_.policy());
EXPECT_EQ(policy_.policy_data().SerializeAsString(),
store_->policy()->SerializeAsString());
VerifyPolicyMap(store_.get());
EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status());
}
TEST_F(UserCloudPolicyStoreTest, StoreThenLoad) {
StorePolicyAndEnsureLoaded(policy_.policy());
EXPECT_FALSE(store_->policy_key().empty());
scoped_ptr<UserCloudPolicyStore> store2(
new UserCloudPolicyStore(policy_file(),
key_file(),
GetPolicyVerificationKey(),
loop_.message_loop_proxy()));
store2->SetSigninUsername(PolicyBuilder::kFakeUsername);
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_);
EXPECT_EQ(store2->policy_key(), store_->policy_key());
}
TEST_F(UserCloudPolicyStoreTest, StoreThenLoadImmediately) {
StorePolicyAndEnsureLoaded(policy_.policy());
scoped_ptr<UserCloudPolicyStore> store2(
new UserCloudPolicyStore(policy_file(),
key_file(),
GetPolicyVerificationKey(),
loop_.message_loop_proxy()));
store2->SetSigninUsername(PolicyBuilder::kFakeUsername);
store2->AddObserver(&observer_);
EXPECT_CALL(observer_, OnStoreLoaded(store2.get()));
store2->LoadImmediately();
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) {
policy_.policy_data().clear_policy_type();
policy_.Build();
ExpectError(store_.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR);
store_->Store(policy_.policy());
RunUntilIdle();
ASSERT_FALSE(store_->policy());
}
TEST_F(UserCloudPolicyStoreTest, StoreUnsigned) {
policy_.policy().mutable_policy_data_signature()->clear();
ExpectError(store_.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR);
store_->Store(policy_.policy());
RunUntilIdle();
ASSERT_FALSE(store_->policy());
}
TEST_F(UserCloudPolicyStoreTest, LoadValidationError) {
StorePolicyAndEnsureLoaded(policy_.policy());
scoped_ptr<UserCloudPolicyStore> store2(
new UserCloudPolicyStore(policy_file(),
key_file(),
GetPolicyVerificationKey(),
loop_.message_loop_proxy()));
store2->SetSigninUsername("foobar@foobar.com");
store2->AddObserver(&observer_);
ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR);
store2->Load();
RunUntilIdle();
ASSERT_FALSE(store2->policy());
store2->RemoveObserver(&observer_);
scoped_ptr<UserCloudPolicyStore> store3(
new UserCloudPolicyStore(policy_file(),
key_file(),
GetPolicyVerificationKey(),
loop_.message_loop_proxy()));
store3->AddObserver(&observer_);
EXPECT_CALL(observer_, OnStoreLoaded(store3.get()));
store3->Load();
RunUntilIdle();
ASSERT_TRUE(store3->policy());
store3->RemoveObserver(&observer_);
scoped_ptr<UserCloudPolicyStore> store4(
new UserCloudPolicyStore(policy_file(),
key_file(),
GetPolicyVerificationKey(),
loop_.message_loop_proxy()));
store4->SetSigninUsername("foobar@foobar.com");
store4->AddObserver(&observer_);
ExpectError(store4.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR);
store4->Load();
RunUntilIdle();
ASSERT_FALSE(store4->policy());
store4->RemoveObserver(&observer_);
}
TEST_F(UserCloudPolicyStoreTest, KeyRotation) {
StorePolicyAndEnsureLoaded(policy_.policy());
ASSERT_TRUE(store_->policy()->has_public_key_version());
std::string key_data;
enterprise_management::PolicySigningKey key;
ASSERT_TRUE(base::ReadFileToString(key_file(), &key_data));
ASSERT_TRUE(key.ParseFromString(key_data));
key.set_verification_key("different_key");
key.SerializeToString(&key_data);
WriteStringToFile(key_file(), key_data);
scoped_ptr<UserCloudPolicyStore> store2(
new UserCloudPolicyStore(policy_file(),
key_file(),
GetPolicyVerificationKey(),
loop_.message_loop_proxy()));
store2->SetSigninUsername(PolicyBuilder::kFakeUsername);
store2->AddObserver(&observer_);
EXPECT_CALL(observer_, OnStoreLoaded(store2.get()));
store2->Load();
RunUntilIdle();
ASSERT_TRUE(store2->policy());
ASSERT_FALSE(store2->policy()->has_public_key_version());
store2->RemoveObserver(&observer_);
}
TEST_F(UserCloudPolicyStoreTest, InvalidCachedVerificationSignature) {
StorePolicyAndEnsureLoaded(policy_.policy());
std::string key_data;
enterprise_management::PolicySigningKey key;
ASSERT_TRUE(base::ReadFileToString(key_file(), &key_data));
ASSERT_TRUE(key.ParseFromString(key_data));
key.set_signing_key_signature("different_key");
key.SerializeToString(&key_data);
WriteStringToFile(key_file(), key_data);
scoped_ptr<UserCloudPolicyStore> store2(
new UserCloudPolicyStore(policy_file(),
key_file(),
GetPolicyVerificationKey(),
loop_.message_loop_proxy()));
store2->SetSigninUsername(PolicyBuilder::kFakeUsername);
store2->AddObserver(&observer_);
ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR);
store2->Load();
RunUntilIdle();
store2->RemoveObserver(&observer_);
}
}