| Index: chrome/browser/policy/cloud_policy_cache_unittest.cc
|
| diff --git a/chrome/browser/policy/cloud_policy_cache_unittest.cc b/chrome/browser/policy/cloud_policy_cache_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..38acb3429ae6b3d73f07134c239de1b483aada16
|
| --- /dev/null
|
| +++ b/chrome/browser/policy/cloud_policy_cache_unittest.cc
|
| @@ -0,0 +1,332 @@
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/policy/cloud_policy_cache.h"
|
| +
|
| +#include <limits>
|
| +#include <string>
|
| +
|
| +#include "base/file_util.h"
|
| +#include "base/message_loop.h"
|
| +#include "base/scoped_temp_dir.h"
|
| +#include "base/values.h"
|
| +#include "chrome/browser/browser_thread.h"
|
| +#include "chrome/browser/policy/proto/cloud_policy.pb.h"
|
| +#include "chrome/browser/policy/proto/device_management_backend.pb.h"
|
| +#include "chrome/browser/policy/proto/device_management_local.pb.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace policy {
|
| +
|
| +Value* DecodeIntegerValue(google::protobuf::int64 value);
|
| +ListValue* DecodeStringList(const em::StringList& string_list);
|
| +
|
| +// Tests the device management policy cache.
|
| +class CloudPolicyCacheTest : public testing::Test {
|
| + protected:
|
| + typedef ConfigurationPolicyProvider::PolicyMapType PolicyMapType;
|
| +
|
| + CloudPolicyCacheTest()
|
| + : loop_(MessageLoop::TYPE_UI),
|
| + ui_thread_(BrowserThread::UI, &loop_),
|
| + file_thread_(BrowserThread::FILE, &loop_) {}
|
| +
|
| + void SetUp() {
|
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
| + }
|
| +
|
| + void TearDown() {
|
| + loop_.RunAllPending();
|
| + }
|
| +
|
| + // Creates a (signed) CloudPolicyResponse setting the given |homepage| and
|
| + // featuring the given |timestamp| (as issued by the server).
|
| + // Mildly hacky special feature: pass an empty string as |homepage| to get
|
| + // a completely empty policy.
|
| + em::CloudPolicyResponse* CreateHomepagePolicy(
|
| + const std::string& homepage,
|
| + const base::Time& timestamp,
|
| + const em::PolicyOptions::PolicyMode policy_mode) {
|
| + em::SignedCloudPolicyResponse signed_response;
|
| + if (homepage != "") {
|
| + em::CloudPolicySettings* settings = signed_response.mutable_settings();
|
| + em::HomepageLocationProto* homepagelocation_proto =
|
| + settings->mutable_homepagelocation();
|
| + homepagelocation_proto->set_homepagelocation(homepage);
|
| + homepagelocation_proto->mutable_policy_options()->set_mode(policy_mode);
|
| + }
|
| + signed_response.set_timestamp(timestamp.ToTimeT());
|
| + std::string serialized_signed_response;
|
| + EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response));
|
| +
|
| + em::CloudPolicyResponse* response = new em::CloudPolicyResponse;
|
| + response->set_signed_response(serialized_signed_response);
|
| + // TODO(jkummerow): Set proper certificate_chain and signature (when
|
| + // implementing support for signature verification).
|
| + response->set_signature("TODO");
|
| + response->add_certificate_chain("TODO");
|
| + return response;
|
| + }
|
| +
|
| + void WritePolicy(const em::CloudPolicyResponse& policy) {
|
| + std::string data;
|
| + em::CachedCloudPolicyResponse cached_policy;
|
| + cached_policy.mutable_cloud_policy()->CopyFrom(policy);
|
| + EXPECT_TRUE(cached_policy.SerializeToString(&data));
|
| + int size = static_cast<int>(data.size());
|
| + EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size));
|
| + }
|
| +
|
| + FilePath test_file() {
|
| + return temp_dir_.path().AppendASCII("CloudPolicyCacheTest");
|
| + }
|
| +
|
| + bool Equals(const PolicyMapType& a, const PolicyMapType& b) const {
|
| + return CloudPolicyCache::Equals(a, b);
|
| + }
|
| +
|
| + MessageLoop loop_;
|
| +
|
| + private:
|
| + ScopedTempDir temp_dir_;
|
| + BrowserThread ui_thread_;
|
| + BrowserThread file_thread_;
|
| +};
|
| +
|
| +TEST_F(CloudPolicyCacheTest, Equals) {
|
| + PolicyMapType a;
|
| + a.insert(std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("aaa")));
|
| + PolicyMapType a2;
|
| + a2.insert(std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("aaa")));
|
| + PolicyMapType b;
|
| + b.insert(std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("bbb")));
|
| + PolicyMapType c;
|
| + c.insert(std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("aaa")));
|
| + c.insert(std::make_pair(kPolicyHomepageIsNewTabPage,
|
| + Value::CreateBooleanValue(true)));
|
| + EXPECT_FALSE(Equals(a, b));
|
| + EXPECT_FALSE(Equals(b, a));
|
| + EXPECT_FALSE(Equals(a, c));
|
| + EXPECT_FALSE(Equals(c, a));
|
| + EXPECT_TRUE(Equals(a, a2));
|
| + EXPECT_TRUE(Equals(a2, a));
|
| + PolicyMapType empty1;
|
| + PolicyMapType empty2;
|
| + EXPECT_TRUE(Equals(empty1, empty2));
|
| + EXPECT_TRUE(Equals(empty2, empty1));
|
| + EXPECT_FALSE(Equals(empty1, a));
|
| + EXPECT_FALSE(Equals(a, empty1));
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, DecodePolicy) {
|
| + em::CloudPolicySettings settings;
|
| + settings.mutable_homepagelocation()->set_homepagelocation("chromium.org");
|
| + settings.mutable_javascriptenabled()->set_javascriptenabled(true);
|
| + settings.mutable_javascriptenabled()->mutable_policy_options()->set_mode(
|
| + em::PolicyOptions::MANDATORY);
|
| + settings.mutable_policyrefreshrate()->set_policyrefreshrate(5);
|
| + settings.mutable_policyrefreshrate()->mutable_policy_options()->set_mode(
|
| + em::PolicyOptions::RECOMMENDED);
|
| + PolicyMapType mandatory_policy;
|
| + PolicyMapType recommended_policy;
|
| + DecodePolicy(settings, &mandatory_policy, &recommended_policy);
|
| + PolicyMapType mandatory;
|
| + mandatory.insert(std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("chromium.org")));
|
| + mandatory.insert(std::make_pair(kPolicyJavascriptEnabled,
|
| + Value::CreateBooleanValue(true)));
|
| + PolicyMapType recommended;
|
| + recommended.insert(std::make_pair(kPolicyPolicyRefreshRate,
|
| + Value::CreateIntegerValue(5)));
|
| + EXPECT_TRUE(Equals(mandatory, mandatory_policy));
|
| + EXPECT_TRUE(Equals(recommended, recommended_policy));
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, DecodeIntegerValue) {
|
| + const int min = std::numeric_limits<int>::min();
|
| + const int max = std::numeric_limits<int>::max();
|
| + scoped_ptr<Value> value(
|
| + DecodeIntegerValue(static_cast<google::protobuf::int64>(42)));
|
| + ASSERT_TRUE(value.get());
|
| + EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(42)));
|
| + value.reset(
|
| + DecodeIntegerValue(static_cast<google::protobuf::int64>(min - 1LL)));
|
| + EXPECT_EQ(NULL, value.get());
|
| + value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(min)));
|
| + ASSERT_TRUE(value.get());
|
| + EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(min)));
|
| + value.reset(
|
| + DecodeIntegerValue(static_cast<google::protobuf::int64>(max + 1LL)));
|
| + EXPECT_EQ(NULL, value.get());
|
| + value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(max)));
|
| + ASSERT_TRUE(value.get());
|
| + EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(max)));
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, DecodeStringList) {
|
| + em::StringList string_list;
|
| + string_list.add_entries("ponies");
|
| + string_list.add_entries("more ponies");
|
| + scoped_ptr<ListValue> decoded(DecodeStringList(string_list));
|
| + ListValue expected;
|
| + expected.Append(Value::CreateStringValue("ponies"));
|
| + expected.Append(Value::CreateStringValue("more ponies"));
|
| + EXPECT_TRUE(decoded->Equals(&expected));
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, Empty) {
|
| + CloudPolicyCache cache(test_file());
|
| + PolicyMapType empty;
|
| + EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy()));
|
| + EXPECT_TRUE(Equals(empty, *cache.GetRecommendedPolicy()));
|
| + EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, LoadNoFile) {
|
| + CloudPolicyCache cache(test_file());
|
| + cache.LoadPolicyFromFile();
|
| + PolicyMapType empty;
|
| + EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy()));
|
| + EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, RejectFuture) {
|
| + scoped_ptr<em::CloudPolicyResponse> policy_response(
|
| + CreateHomepagePolicy("", base::Time::NowFromSystemTime() +
|
| + base::TimeDelta::FromMinutes(5),
|
| + em::PolicyOptions::MANDATORY));
|
| + WritePolicy(*policy_response);
|
| + CloudPolicyCache cache(test_file());
|
| + cache.LoadPolicyFromFile();
|
| + PolicyMapType empty;
|
| + EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy()));
|
| + EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, LoadWithFile) {
|
| + scoped_ptr<em::CloudPolicyResponse> policy_response(
|
| + CreateHomepagePolicy("", base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::MANDATORY));
|
| + WritePolicy(*policy_response);
|
| + CloudPolicyCache cache(test_file());
|
| + cache.LoadPolicyFromFile();
|
| + PolicyMapType empty;
|
| + EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy()));
|
| + EXPECT_NE(base::Time(), cache.last_policy_refresh_time());
|
| + EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time());
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, LoadWithData) {
|
| + scoped_ptr<em::CloudPolicyResponse> policy(
|
| + CreateHomepagePolicy("http://www.example.com",
|
| + base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::MANDATORY));
|
| + WritePolicy(*policy);
|
| + CloudPolicyCache cache(test_file());
|
| + cache.LoadPolicyFromFile();
|
| + PolicyMapType expected;
|
| + expected.insert(
|
| + std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("http://www.example.com")));
|
| + EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy()));
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, SetPolicy) {
|
| + CloudPolicyCache cache(test_file());
|
| + scoped_ptr<em::CloudPolicyResponse> policy(
|
| + CreateHomepagePolicy("http://www.example.com",
|
| + base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::MANDATORY));
|
| + EXPECT_TRUE(cache.SetPolicy(*policy));
|
| + scoped_ptr<em::CloudPolicyResponse> policy2(
|
| + CreateHomepagePolicy("http://www.example.com",
|
| + base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::MANDATORY));
|
| + EXPECT_FALSE(cache.SetPolicy(*policy2));
|
| + PolicyMapType expected;
|
| + expected.insert(
|
| + std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("http://www.example.com")));
|
| + PolicyMapType empty;
|
| + EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy()));
|
| + EXPECT_TRUE(Equals(empty, *cache.GetRecommendedPolicy()));
|
| + policy.reset(CreateHomepagePolicy("http://www.example.com",
|
| + base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::RECOMMENDED));
|
| + EXPECT_TRUE(cache.SetPolicy(*policy));
|
| + EXPECT_TRUE(Equals(expected, *cache.GetRecommendedPolicy()));
|
| + EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy()));
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, ResetPolicy) {
|
| + CloudPolicyCache cache(test_file());
|
| +
|
| + scoped_ptr<em::CloudPolicyResponse> policy(
|
| + CreateHomepagePolicy("http://www.example.com",
|
| + base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::MANDATORY));
|
| + EXPECT_TRUE(cache.SetPolicy(*policy));
|
| + PolicyMapType expected;
|
| + expected.insert(
|
| + std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("http://www.example.com")));
|
| + EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy()));
|
| +
|
| + scoped_ptr<em::CloudPolicyResponse> empty_policy(
|
| + CreateHomepagePolicy("", base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::MANDATORY));
|
| + EXPECT_TRUE(cache.SetPolicy(*empty_policy));
|
| + PolicyMapType empty;
|
| + EXPECT_TRUE(Equals(empty, *cache.GetMandatoryPolicy()));
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, PersistPolicy) {
|
| + {
|
| + CloudPolicyCache cache(test_file());
|
| + scoped_ptr<em::CloudPolicyResponse> policy(
|
| + CreateHomepagePolicy("http://www.example.com",
|
| + base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::MANDATORY));
|
| + cache.SetPolicy(*policy);
|
| + }
|
| +
|
| + loop_.RunAllPending();
|
| +
|
| + EXPECT_TRUE(file_util::PathExists(test_file()));
|
| + CloudPolicyCache cache(test_file());
|
| + cache.LoadPolicyFromFile();
|
| + PolicyMapType expected;
|
| + expected.insert(
|
| + std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("http://www.example.com")));
|
| + EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy()));
|
| +}
|
| +
|
| +TEST_F(CloudPolicyCacheTest, FreshPolicyOverride) {
|
| + scoped_ptr<em::CloudPolicyResponse> policy(
|
| + CreateHomepagePolicy("http://www.example.com",
|
| + base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::MANDATORY));
|
| + WritePolicy(*policy);
|
| +
|
| + CloudPolicyCache cache(test_file());
|
| + scoped_ptr<em::CloudPolicyResponse> updated_policy(
|
| + CreateHomepagePolicy("http://www.chromium.org",
|
| + base::Time::NowFromSystemTime(),
|
| + em::PolicyOptions::MANDATORY));
|
| + EXPECT_TRUE(cache.SetPolicy(*updated_policy));
|
| +
|
| + cache.LoadPolicyFromFile();
|
| + PolicyMapType expected;
|
| + expected.insert(
|
| + std::make_pair(kPolicyHomepageLocation,
|
| + Value::CreateStringValue("http://www.chromium.org")));
|
| + EXPECT_TRUE(Equals(expected, *cache.GetMandatoryPolicy()));
|
| +}
|
| +
|
| +} // namespace policy
|
|
|