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

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

Issue 50143010: Added a SchemaMap. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chrome-policy-schema-5-ref-counted-schema
Patch Set: rebase Created 7 years, 1 month 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
« no previous file with comments | « chrome/browser/policy/schema_map.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/policy/schema_map_unittest.cc
diff --git a/chrome/browser/policy/policy_domain_descriptor_unittest.cc b/chrome/browser/policy/schema_map_unittest.cc
similarity index 53%
copy from chrome/browser/policy/policy_domain_descriptor_unittest.cc
copy to chrome/browser/policy/schema_map_unittest.cc
index 7841a8a6c687814fac2b3da15346decc6495c381..b72336c1d2b77ea3ddd409cb28e707de21744d99 100644
--- a/chrome/browser/policy/policy_domain_descriptor_unittest.cc
+++ b/chrome/browser/policy/schema_map_unittest.cc
@@ -1,76 +1,118 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Copyright 2013 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/policy_domain_descriptor.h"
+#include "chrome/browser/policy/schema_map.h"
-#include <string>
-
-#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "chrome/browser/policy/external_data_fetcher.h"
#include "chrome/browser/policy/external_data_manager.h"
#include "chrome/browser/policy/policy_bundle.h"
#include "chrome/browser/policy/policy_map.h"
+#include "components/policy/core/common/schema.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
-class PolicyDomainDescriptorTest : public testing::Test {
+namespace {
+
+const char kTestSchema[] =
+ "{"
+ " \"type\": \"object\","
+ " \"properties\": {"
+ " \"string\": { \"type\": \"string\" },"
+ " \"integer\": { \"type\": \"integer\" },"
+ " \"boolean\": { \"type\": \"boolean\" },"
+ " \"null\": { \"type\": \"null\" },"
+ " \"double\": { \"type\": \"number\" },"
+ " \"list\": {"
+ " \"type\": \"array\","
+ " \"items\": { \"type\": \"string\" }"
+ " },"
+ " \"object\": {"
+ " \"type\": \"object\","
+ " \"properties\": {"
+ " \"a\": { \"type\": \"string\" },"
+ " \"b\": { \"type\": \"integer\" }"
+ " }"
+ " }"
+ " }"
+ "}";
+
+} // namespace
+
+class SchemaMapTest : public testing::Test {
protected:
- scoped_ptr<ExternalDataFetcher> CreateExternalDataFetcher() const;
+ scoped_refptr<SchemaMap> CreateTestMap() {
+ std::string error;
+ Schema schema = Schema::Parse(kTestSchema, &error);
+ if (!schema.valid()) {
+ ADD_FAILURE() << error;
+ return NULL;
+ }
+
+ ComponentMap component_map;
+ component_map["extension-1"] = schema;
+ component_map["extension-2"] = schema;
+ component_map["legacy-extension"] = Schema();
+
+ DomainMap domain_map;
+ domain_map[POLICY_DOMAIN_EXTENSIONS] = component_map;
+
+ return new SchemaMap(domain_map);
+ }
};
-scoped_ptr<ExternalDataFetcher>
- PolicyDomainDescriptorTest::CreateExternalDataFetcher() const {
- return make_scoped_ptr(
- new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(),
- std::string()));
+TEST_F(SchemaMapTest, Empty) {
+ scoped_refptr<SchemaMap> map = new SchemaMap();
+ EXPECT_TRUE(map->GetDomains().empty());
+ EXPECT_FALSE(map->GetComponents(POLICY_DOMAIN_CHROME));
+ EXPECT_FALSE(map->GetComponents(POLICY_DOMAIN_EXTENSIONS));
+ EXPECT_FALSE(map->GetSchema(PolicyNamespace(POLICY_DOMAIN_CHROME, "")));
}
-TEST_F(PolicyDomainDescriptorTest, FilterBundle) {
- scoped_refptr<PolicyDomainDescriptor> descriptor =
- new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS);
- EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain());
- EXPECT_TRUE(descriptor->components().empty());
+TEST_F(SchemaMapTest, Lookups) {
+ scoped_refptr<SchemaMap> map = CreateTestMap();
+ ASSERT_TRUE(map);
+
+ EXPECT_FALSE(map->GetSchema(
+ PolicyNamespace(POLICY_DOMAIN_CHROME, "")));
+ EXPECT_FALSE(map->GetSchema(
+ PolicyNamespace(POLICY_DOMAIN_CHROME, "extension-1")));
+ EXPECT_FALSE(map->GetSchema(
+ PolicyNamespace(POLICY_DOMAIN_CHROME, "legacy-extension")));
+ EXPECT_FALSE(map->GetSchema(
+ PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "")));
+ EXPECT_FALSE(map->GetSchema(
+ PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "extension-3")));
+
+ const Schema* schema =
+ map->GetSchema(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "extension-1"));
+ ASSERT_TRUE(schema);
+ EXPECT_TRUE(schema->valid());
+
+ schema = map->GetSchema(
+ PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "legacy-extension"));
+ ASSERT_TRUE(schema);
+ EXPECT_FALSE(schema->valid());
+}
+TEST_F(SchemaMapTest, FilterBundle) {
std::string error;
- Schema schema = Schema::Parse(
- "{"
- " \"type\":\"object\","
- " \"properties\": {"
- " \"Array\": {"
- " \"type\": \"array\","
- " \"items\": { \"type\": \"string\" }"
- " },"
- " \"Boolean\": { \"type\": \"boolean\" },"
- " \"Integer\": { \"type\": \"integer\" },"
- " \"Null\": { \"type\": \"null\" },"
- " \"Number\": { \"type\": \"number\" },"
- " \"Object\": {"
- " \"type\": \"object\","
- " \"properties\": {"
- " \"a\": { \"type\": \"string\" },"
- " \"b\": { \"type\": \"integer\" }"
- " }"
- " },"
- " \"String\": { \"type\": \"string\" }"
- " }"
- "}", &error);
+ Schema schema = Schema::Parse(kTestSchema, &error);
ASSERT_TRUE(schema.valid()) << error;
- descriptor->RegisterComponent("abc", schema);
-
- EXPECT_EQ(1u, descriptor->components().size());
- EXPECT_EQ(1u, descriptor->components().count("abc"));
+ DomainMap domain_map;
+ domain_map[POLICY_DOMAIN_EXTENSIONS]["abc"] = schema;
+ scoped_refptr<SchemaMap> schema_map = new SchemaMap(domain_map);
PolicyBundle bundle;
- descriptor->FilterBundle(&bundle);
+ schema_map->FilterBundle(&bundle);
const PolicyBundle empty_bundle;
EXPECT_TRUE(bundle.Equals(empty_bundle));
- // Other namespaces aren't filtered.
+ // The Chrome namespace isn't filtered.
PolicyBundle expected_bundle;
PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
expected_bundle.Get(chrome_ns).Set("ChromePolicy",
@@ -79,7 +121,8 @@ TEST_F(PolicyDomainDescriptorTest, FilterBundle) {
base::Value::CreateStringValue("value"),
NULL);
bundle.CopyFrom(expected_bundle);
- // Unknown components of the domain are filtered out.
+
+ // Unknown components are filtered out.
PolicyNamespace another_extension_ns(POLICY_DOMAIN_EXTENSIONS, "xyz");
bundle.Get(another_extension_ns).Set(
"AnotherExtensionPolicy",
@@ -87,7 +130,7 @@ TEST_F(PolicyDomainDescriptorTest, FilterBundle) {
POLICY_SCOPE_USER,
base::Value::CreateStringValue("value"),
NULL);
- descriptor->FilterBundle(&bundle);
+ schema_map->FilterBundle(&bundle);
EXPECT_TRUE(bundle.Equals(expected_bundle));
PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc");
@@ -95,22 +138,22 @@ TEST_F(PolicyDomainDescriptorTest, FilterBundle) {
base::ListValue list;
list.AppendString("a");
list.AppendString("b");
- map.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ map.Set("list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
list.DeepCopy(), NULL);
- map.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ map.Set("boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(true), NULL);
- map.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ map.Set("integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(1), NULL);
- map.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ map.Set("null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateNullValue(), NULL);
- map.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ map.Set("double", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateDoubleValue(1.2), NULL);
base::DictionaryValue dict;
dict.SetString("a", "b");
dict.SetInteger("b", 2);
- map.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ map.Set("object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
dict.DeepCopy(), NULL);
- map.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ map.Set("string", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateStringValue("value"), NULL);
bundle.MergeFrom(expected_bundle);
@@ -120,37 +163,34 @@ TEST_F(PolicyDomainDescriptorTest, FilterBundle) {
base::Value::CreateStringValue("to-be-removed"),
NULL);
- descriptor->FilterBundle(&bundle);
+ schema_map->FilterBundle(&bundle);
EXPECT_TRUE(bundle.Equals(expected_bundle));
// Mismatched types are also removed.
bundle.Clear();
PolicyMap& badmap = bundle.Get(extension_ns);
- badmap.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ badmap.Set("list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
- badmap.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ badmap.Set("boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateIntegerValue(0), NULL);
- badmap.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ badmap.Set("integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
- badmap.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ badmap.Set("null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
- badmap.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ badmap.Set("double", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
- badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ badmap.Set("object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
base::Value::CreateBooleanValue(false), NULL);
- badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
- NULL, CreateExternalDataFetcher().release());
+ badmap.Set("string", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
+ NULL,
+ new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(),
+ std::string()));
- descriptor->FilterBundle(&bundle);
+ schema_map->FilterBundle(&bundle);
EXPECT_TRUE(bundle.Equals(empty_bundle));
}
-TEST_F(PolicyDomainDescriptorTest, LegacyComponents) {
- scoped_refptr<PolicyDomainDescriptor> descriptor =
- new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS);
- EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain());
- EXPECT_TRUE(descriptor->components().empty());
-
+TEST_F(SchemaMapTest, LegacyComponents) {
std::string error;
Schema schema = Schema::Parse(
"{"
@@ -161,10 +201,10 @@ TEST_F(PolicyDomainDescriptorTest, LegacyComponents) {
"}", &error);
ASSERT_TRUE(schema.valid()) << error;
- descriptor->RegisterComponent("with-schema", schema);
- descriptor->RegisterComponent("without-schema", Schema());
-
- EXPECT_EQ(2u, descriptor->components().size());
+ DomainMap domain_map;
+ domain_map[POLICY_DOMAIN_EXTENSIONS]["with-schema"] = schema;
+ domain_map[POLICY_DOMAIN_EXTENSIONS]["without-schema"] = Schema();
+ scoped_refptr<SchemaMap> schema_map = new SchemaMap(domain_map);
// |bundle| contains policies loaded by a policy provider.
PolicyBundle bundle;
@@ -185,7 +225,7 @@ TEST_F(PolicyDomainDescriptorTest, LegacyComponents) {
base::Value::CreateStringValue("value 2"),
NULL);
- // Other namespaces aren't filtered.
+ // The Chrome namespace isn't filtered.
PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
bundle.Get(chrome_ns).Set("ChromePolicy",
POLICY_LEVEL_MANDATORY,
@@ -211,7 +251,7 @@ TEST_F(PolicyDomainDescriptorTest, LegacyComponents) {
base::Value::CreateStringValue("value 5"),
NULL);
- descriptor->FilterBundle(&bundle);
+ schema_map->FilterBundle(&bundle);
EXPECT_TRUE(bundle.Equals(expected_bundle));
}
« no previous file with comments | « chrome/browser/policy/schema_map.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698