Chromium Code Reviews| 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 54% |
| copy from chrome/browser/policy/policy_domain_descriptor_unittest.cc |
| copy to chrome/browser/policy/schema_map_unittest.cc |
| index 7841a8a6c687814fac2b3da15346decc6495c381..1cff580d7e0b7a7b1bc00e44d1580df99dbea76b 100644 |
| --- a/chrome/browser/policy/policy_domain_descriptor_unittest.cc |
| +++ b/chrome/browser/policy/schema_map_unittest.cc |
| @@ -1,76 +1,112 @@ |
| -// 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; |
|
dconnelly
2013/10/31 10:04:16
this shouldn't ever happen, right? why not ASSERT_
Joao da Silva
2013/10/31 13:34:56
Because ASSERT_* is a glorified return, and doesn'
|
| + } |
| + |
| + 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_TRUE(map->GetComponents(POLICY_DOMAIN_CHROME).empty()); |
| + EXPECT_TRUE(map->GetComponents(POLICY_DOMAIN_EXTENSIONS).empty()); |
| + EXPECT_TRUE(map->GetSchema(POLICY_DOMAIN_CHROME, "") == NULL); |
|
bartfab (slow)
2013/10/30 12:34:49
Why not:
EXPECT_FALSE(map->GetSchema(POLICY_DOMAI
Joao da Silva
2013/10/31 13:34:56
Done.
|
| } |
| -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(POLICY_DOMAIN_CHROME, "")); |
| + EXPECT_FALSE(map->GetSchema(POLICY_DOMAIN_CHROME, "extension-1")); |
| + EXPECT_FALSE(map->GetSchema(POLICY_DOMAIN_CHROME, "legacy-extension")); |
| + EXPECT_FALSE(map->GetSchema(POLICY_DOMAIN_EXTENSIONS, "")); |
| + EXPECT_FALSE(map->GetSchema(POLICY_DOMAIN_EXTENSIONS, "extension-3")); |
| + |
| + const Schema* schema = |
| + map->GetSchema(POLICY_DOMAIN_EXTENSIONS, "extension-1"); |
| + ASSERT_TRUE(schema); |
| + EXPECT_TRUE(schema->valid()); |
| + |
| + schema = map->GetSchema(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 +115,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 +124,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 +132,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 +157,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 +195,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 +219,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 +245,7 @@ TEST_F(PolicyDomainDescriptorTest, LegacyComponents) { |
| base::Value::CreateStringValue("value 5"), |
| NULL); |
| - descriptor->FilterBundle(&bundle); |
| + schema_map->FilterBundle(&bundle); |
| EXPECT_TRUE(bundle.Equals(expected_bundle)); |
| } |