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

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

Issue 6409040: New policy protobuf protocol. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Ready for review! Created 9 years, 11 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/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..954ed101ab84cf902f28ad36eb1ff358b04b23ee
--- /dev/null
+++ b/chrome/browser/policy/cloud_policy_cache_unittest.cc
@@ -0,0 +1,311 @@
+// 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:
+ 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) {
gfeher 2011/02/02 08:42:45 Nit: indent.
Jakob Kummerow 2011/02/03 14:36:52 Done.
+ em::SignedCloudPolicyResponse signed_response;
+ if (homepage != "") {
+ em::CloudPolicySettings* settings = signed_response.mutable_settings();
+ em::HomepageProto* homepage_proto = settings->mutable_homepage();
+ homepage_proto->set_homepagelocation(homepage);
+ }
+ 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_policy_response()->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));
+}
+
+TEST_F(CloudPolicyCacheTest, DecodePolicy) {
+ em::CloudPolicySettings settings;
+ settings.mutable_homepage()->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);
+ scoped_ptr<PolicyMapType> mandatory_policy(new PolicyMapType);
+ scoped_ptr<PolicyMapType> recommended_policy(new PolicyMapType);
+ DecodePolicy(settings, mandatory_policy.get(), recommended_policy.get());
+ 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.get()));
+ EXPECT_TRUE(Equals(&recommended, recommended_policy.get()));
+}
+
+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;
+ scoped_ptr<PolicyMapType> policy(cache.GetPolicy());
+ EXPECT_TRUE(Equals(&empty, policy.get()));
+ EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
+}
+
+TEST_F(CloudPolicyCacheTest, LoadNoFile) {
+ CloudPolicyCache cache(test_file());
+ cache.LoadPolicyFromFile();
+ PolicyMapType empty;
+ scoped_ptr<PolicyMapType> policy(cache.GetPolicy());
+ EXPECT_TRUE(Equals(&empty, policy.get()));
+ 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)));
+ WritePolicy(*policy_response);
+ CloudPolicyCache cache(test_file());
+ cache.LoadPolicyFromFile();
+ PolicyMapType empty;
+ scoped_ptr<PolicyMapType> policy(cache.GetPolicy());
+ EXPECT_TRUE(Equals(&empty, policy.get()));
+ EXPECT_EQ(base::Time(), cache.last_policy_refresh_time());
+}
+
+TEST_F(CloudPolicyCacheTest, LoadWithFile) {
+ scoped_ptr<em::CloudPolicyResponse> policy_response(
+ CreateHomepagePolicy("", base::Time::NowFromSystemTime()));
+ WritePolicy(*policy_response);
+ CloudPolicyCache cache(test_file());
+ cache.LoadPolicyFromFile();
+ PolicyMapType empty;
+ scoped_ptr<PolicyMapType> policy(cache.GetPolicy());
+ EXPECT_TRUE(Equals(&empty, policy.get()));
+ 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()));
+ WritePolicy(*policy);
+ CloudPolicyCache cache(test_file());
+ cache.LoadPolicyFromFile();
+ PolicyMapType expected;
+ expected.insert(
+ std::make_pair(kPolicyHomepageLocation,
+ Value::CreateStringValue("http://www.example.com")));
+ scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
+ EXPECT_TRUE(Equals(&expected, policy_value.get()));
+}
+
+TEST_F(CloudPolicyCacheTest, SetPolicy) {
+ CloudPolicyCache cache(test_file());
+ scoped_ptr<em::CloudPolicyResponse> policy(
+ CreateHomepagePolicy("http://www.example.com",
+ base::Time::NowFromSystemTime()));
+ EXPECT_TRUE(cache.SetPolicy(*policy));
+ scoped_ptr<em::CloudPolicyResponse> policy2(
+ CreateHomepagePolicy("http://www.example.com",
+ base::Time::NowFromSystemTime()));
+ EXPECT_FALSE(cache.SetPolicy(*policy2));
+ PolicyMapType expected;
+ expected.insert(
+ std::make_pair(kPolicyHomepageLocation,
+ Value::CreateStringValue("http://www.example.com")));
+ scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
+ EXPECT_TRUE(Equals(&expected, policy_value.get()));
+}
+
+TEST_F(CloudPolicyCacheTest, ResetPolicy) {
+ CloudPolicyCache cache(test_file());
+
+ scoped_ptr<em::CloudPolicyResponse> policy(
+ CreateHomepagePolicy("http://www.example.com",
+ base::Time::NowFromSystemTime()));
+ EXPECT_TRUE(cache.SetPolicy(*policy));
+ PolicyMapType expected;
+ expected.insert(
+ std::make_pair(kPolicyHomepageLocation,
+ Value::CreateStringValue("http://www.example.com")));
+ scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
+ EXPECT_TRUE(Equals(&expected, policy_value.get()));
+
+ scoped_ptr<em::CloudPolicyResponse> empty_policy(
+ CreateHomepagePolicy("", base::Time::NowFromSystemTime()));
+ EXPECT_TRUE(cache.SetPolicy(*empty_policy));
+ policy_value.reset(cache.GetPolicy());
+ PolicyMapType empty;
+ EXPECT_TRUE(Equals(&empty, policy_value.get()));
+}
+
+TEST_F(CloudPolicyCacheTest, PersistPolicy) {
+ {
+ CloudPolicyCache cache(test_file());
+ scoped_ptr<em::CloudPolicyResponse> policy(
+ CreateHomepagePolicy("http://www.example.com",
+ base::Time::NowFromSystemTime()));
+ 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")));
+ scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
+ EXPECT_TRUE(Equals(&expected, policy_value.get()));
+}
+
+TEST_F(CloudPolicyCacheTest, FreshPolicyOverride) {
+ scoped_ptr<em::CloudPolicyResponse> policy(
+ CreateHomepagePolicy("http://www.example.com",
+ base::Time::NowFromSystemTime()));
+ WritePolicy(*policy);
+
+ CloudPolicyCache cache(test_file());
+ scoped_ptr<em::CloudPolicyResponse> updated_policy(
+ CreateHomepagePolicy("http://www.chromium.org",
+ base::Time::NowFromSystemTime()));
+ EXPECT_TRUE(cache.SetPolicy(*updated_policy));
+
+ cache.LoadPolicyFromFile();
+ PolicyMapType expected;
+ expected.insert(
+ std::make_pair(kPolicyHomepageLocation,
+ Value::CreateStringValue("http://www.chromium.org")));
+ scoped_ptr<PolicyMapType> policy_value(cache.GetPolicy());
+ EXPECT_TRUE(Equals(&expected, policy_value.get()));
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698