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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « chrome/browser/policy/schema_map.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/policy_domain_descriptor.h" 5 #include "chrome/browser/policy/schema_map.h"
6 6
7 #include <string>
8
9 #include "base/callback.h"
10 #include "base/memory/weak_ptr.h" 7 #include "base/memory/weak_ptr.h"
11 #include "base/values.h" 8 #include "base/values.h"
12 #include "chrome/browser/policy/external_data_fetcher.h" 9 #include "chrome/browser/policy/external_data_fetcher.h"
13 #include "chrome/browser/policy/external_data_manager.h" 10 #include "chrome/browser/policy/external_data_manager.h"
14 #include "chrome/browser/policy/policy_bundle.h" 11 #include "chrome/browser/policy/policy_bundle.h"
15 #include "chrome/browser/policy/policy_map.h" 12 #include "chrome/browser/policy/policy_map.h"
13 #include "components/policy/core/common/schema.h"
16 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
17 15
18 namespace policy { 16 namespace policy {
19 17
20 class PolicyDomainDescriptorTest : public testing::Test { 18 namespace {
19
20 const char kTestSchema[] =
21 "{"
22 " \"type\": \"object\","
23 " \"properties\": {"
24 " \"string\": { \"type\": \"string\" },"
25 " \"integer\": { \"type\": \"integer\" },"
26 " \"boolean\": { \"type\": \"boolean\" },"
27 " \"null\": { \"type\": \"null\" },"
28 " \"double\": { \"type\": \"number\" },"
29 " \"list\": {"
30 " \"type\": \"array\","
31 " \"items\": { \"type\": \"string\" }"
32 " },"
33 " \"object\": {"
34 " \"type\": \"object\","
35 " \"properties\": {"
36 " \"a\": { \"type\": \"string\" },"
37 " \"b\": { \"type\": \"integer\" }"
38 " }"
39 " }"
40 " }"
41 "}";
42
43 } // namespace
44
45 class SchemaMapTest : public testing::Test {
21 protected: 46 protected:
22 scoped_ptr<ExternalDataFetcher> CreateExternalDataFetcher() const; 47 scoped_refptr<SchemaMap> CreateTestMap() {
48 std::string error;
49 Schema schema = Schema::Parse(kTestSchema, &error);
50 if (!schema.valid()) {
51 ADD_FAILURE() << error;
52 return NULL;
53 }
54
55 ComponentMap component_map;
56 component_map["extension-1"] = schema;
57 component_map["extension-2"] = schema;
58 component_map["legacy-extension"] = Schema();
59
60 DomainMap domain_map;
61 domain_map[POLICY_DOMAIN_EXTENSIONS] = component_map;
62
63 return new SchemaMap(domain_map);
64 }
23 }; 65 };
24 66
25 scoped_ptr<ExternalDataFetcher> 67 TEST_F(SchemaMapTest, Empty) {
26 PolicyDomainDescriptorTest::CreateExternalDataFetcher() const { 68 scoped_refptr<SchemaMap> map = new SchemaMap();
27 return make_scoped_ptr( 69 EXPECT_TRUE(map->GetDomains().empty());
28 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(), 70 EXPECT_FALSE(map->GetComponents(POLICY_DOMAIN_CHROME));
29 std::string())); 71 EXPECT_FALSE(map->GetComponents(POLICY_DOMAIN_EXTENSIONS));
72 EXPECT_FALSE(map->GetSchema(PolicyNamespace(POLICY_DOMAIN_CHROME, "")));
30 } 73 }
31 74
32 TEST_F(PolicyDomainDescriptorTest, FilterBundle) { 75 TEST_F(SchemaMapTest, Lookups) {
33 scoped_refptr<PolicyDomainDescriptor> descriptor = 76 scoped_refptr<SchemaMap> map = CreateTestMap();
34 new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS); 77 ASSERT_TRUE(map);
35 EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain());
36 EXPECT_TRUE(descriptor->components().empty());
37 78
79 EXPECT_FALSE(map->GetSchema(
80 PolicyNamespace(POLICY_DOMAIN_CHROME, "")));
81 EXPECT_FALSE(map->GetSchema(
82 PolicyNamespace(POLICY_DOMAIN_CHROME, "extension-1")));
83 EXPECT_FALSE(map->GetSchema(
84 PolicyNamespace(POLICY_DOMAIN_CHROME, "legacy-extension")));
85 EXPECT_FALSE(map->GetSchema(
86 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "")));
87 EXPECT_FALSE(map->GetSchema(
88 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "extension-3")));
89
90 const Schema* schema =
91 map->GetSchema(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "extension-1"));
92 ASSERT_TRUE(schema);
93 EXPECT_TRUE(schema->valid());
94
95 schema = map->GetSchema(
96 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "legacy-extension"));
97 ASSERT_TRUE(schema);
98 EXPECT_FALSE(schema->valid());
99 }
100
101 TEST_F(SchemaMapTest, FilterBundle) {
38 std::string error; 102 std::string error;
39 Schema schema = Schema::Parse( 103 Schema schema = Schema::Parse(kTestSchema, &error);
40 "{"
41 " \"type\":\"object\","
42 " \"properties\": {"
43 " \"Array\": {"
44 " \"type\": \"array\","
45 " \"items\": { \"type\": \"string\" }"
46 " },"
47 " \"Boolean\": { \"type\": \"boolean\" },"
48 " \"Integer\": { \"type\": \"integer\" },"
49 " \"Null\": { \"type\": \"null\" },"
50 " \"Number\": { \"type\": \"number\" },"
51 " \"Object\": {"
52 " \"type\": \"object\","
53 " \"properties\": {"
54 " \"a\": { \"type\": \"string\" },"
55 " \"b\": { \"type\": \"integer\" }"
56 " }"
57 " },"
58 " \"String\": { \"type\": \"string\" }"
59 " }"
60 "}", &error);
61 ASSERT_TRUE(schema.valid()) << error; 104 ASSERT_TRUE(schema.valid()) << error;
62 105
63 descriptor->RegisterComponent("abc", schema); 106 DomainMap domain_map;
64 107 domain_map[POLICY_DOMAIN_EXTENSIONS]["abc"] = schema;
65 EXPECT_EQ(1u, descriptor->components().size()); 108 scoped_refptr<SchemaMap> schema_map = new SchemaMap(domain_map);
66 EXPECT_EQ(1u, descriptor->components().count("abc"));
67 109
68 PolicyBundle bundle; 110 PolicyBundle bundle;
69 descriptor->FilterBundle(&bundle); 111 schema_map->FilterBundle(&bundle);
70 const PolicyBundle empty_bundle; 112 const PolicyBundle empty_bundle;
71 EXPECT_TRUE(bundle.Equals(empty_bundle)); 113 EXPECT_TRUE(bundle.Equals(empty_bundle));
72 114
73 // Other namespaces aren't filtered. 115 // The Chrome namespace isn't filtered.
74 PolicyBundle expected_bundle; 116 PolicyBundle expected_bundle;
75 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); 117 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
76 expected_bundle.Get(chrome_ns).Set("ChromePolicy", 118 expected_bundle.Get(chrome_ns).Set("ChromePolicy",
77 POLICY_LEVEL_MANDATORY, 119 POLICY_LEVEL_MANDATORY,
78 POLICY_SCOPE_USER, 120 POLICY_SCOPE_USER,
79 base::Value::CreateStringValue("value"), 121 base::Value::CreateStringValue("value"),
80 NULL); 122 NULL);
81 bundle.CopyFrom(expected_bundle); 123 bundle.CopyFrom(expected_bundle);
82 // Unknown components of the domain are filtered out. 124
125 // Unknown components are filtered out.
83 PolicyNamespace another_extension_ns(POLICY_DOMAIN_EXTENSIONS, "xyz"); 126 PolicyNamespace another_extension_ns(POLICY_DOMAIN_EXTENSIONS, "xyz");
84 bundle.Get(another_extension_ns).Set( 127 bundle.Get(another_extension_ns).Set(
85 "AnotherExtensionPolicy", 128 "AnotherExtensionPolicy",
86 POLICY_LEVEL_MANDATORY, 129 POLICY_LEVEL_MANDATORY,
87 POLICY_SCOPE_USER, 130 POLICY_SCOPE_USER,
88 base::Value::CreateStringValue("value"), 131 base::Value::CreateStringValue("value"),
89 NULL); 132 NULL);
90 descriptor->FilterBundle(&bundle); 133 schema_map->FilterBundle(&bundle);
91 EXPECT_TRUE(bundle.Equals(expected_bundle)); 134 EXPECT_TRUE(bundle.Equals(expected_bundle));
92 135
93 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc"); 136 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc");
94 PolicyMap& map = expected_bundle.Get(extension_ns); 137 PolicyMap& map = expected_bundle.Get(extension_ns);
95 base::ListValue list; 138 base::ListValue list;
96 list.AppendString("a"); 139 list.AppendString("a");
97 list.AppendString("b"); 140 list.AppendString("b");
98 map.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 141 map.Set("list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
99 list.DeepCopy(), NULL); 142 list.DeepCopy(), NULL);
100 map.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 143 map.Set("boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
101 base::Value::CreateBooleanValue(true), NULL); 144 base::Value::CreateBooleanValue(true), NULL);
102 map.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 145 map.Set("integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
103 base::Value::CreateIntegerValue(1), NULL); 146 base::Value::CreateIntegerValue(1), NULL);
104 map.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 147 map.Set("null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
105 base::Value::CreateNullValue(), NULL); 148 base::Value::CreateNullValue(), NULL);
106 map.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 149 map.Set("double", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
107 base::Value::CreateDoubleValue(1.2), NULL); 150 base::Value::CreateDoubleValue(1.2), NULL);
108 base::DictionaryValue dict; 151 base::DictionaryValue dict;
109 dict.SetString("a", "b"); 152 dict.SetString("a", "b");
110 dict.SetInteger("b", 2); 153 dict.SetInteger("b", 2);
111 map.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 154 map.Set("object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
112 dict.DeepCopy(), NULL); 155 dict.DeepCopy(), NULL);
113 map.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 156 map.Set("string", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
114 base::Value::CreateStringValue("value"), NULL); 157 base::Value::CreateStringValue("value"), NULL);
115 158
116 bundle.MergeFrom(expected_bundle); 159 bundle.MergeFrom(expected_bundle);
117 bundle.Get(extension_ns).Set("Unexpected", 160 bundle.Get(extension_ns).Set("Unexpected",
118 POLICY_LEVEL_MANDATORY, 161 POLICY_LEVEL_MANDATORY,
119 POLICY_SCOPE_USER, 162 POLICY_SCOPE_USER,
120 base::Value::CreateStringValue("to-be-removed"), 163 base::Value::CreateStringValue("to-be-removed"),
121 NULL); 164 NULL);
122 165
123 descriptor->FilterBundle(&bundle); 166 schema_map->FilterBundle(&bundle);
124 EXPECT_TRUE(bundle.Equals(expected_bundle)); 167 EXPECT_TRUE(bundle.Equals(expected_bundle));
125 168
126 // Mismatched types are also removed. 169 // Mismatched types are also removed.
127 bundle.Clear(); 170 bundle.Clear();
128 PolicyMap& badmap = bundle.Get(extension_ns); 171 PolicyMap& badmap = bundle.Get(extension_ns);
129 badmap.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 172 badmap.Set("list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
130 base::Value::CreateBooleanValue(false), NULL); 173 base::Value::CreateBooleanValue(false), NULL);
131 badmap.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 174 badmap.Set("boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
132 base::Value::CreateIntegerValue(0), NULL); 175 base::Value::CreateIntegerValue(0), NULL);
133 badmap.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 176 badmap.Set("integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
134 base::Value::CreateBooleanValue(false), NULL); 177 base::Value::CreateBooleanValue(false), NULL);
135 badmap.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 178 badmap.Set("null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
136 base::Value::CreateBooleanValue(false), NULL); 179 base::Value::CreateBooleanValue(false), NULL);
137 badmap.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 180 badmap.Set("double", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
138 base::Value::CreateBooleanValue(false), NULL); 181 base::Value::CreateBooleanValue(false), NULL);
139 badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 182 badmap.Set("object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
140 base::Value::CreateBooleanValue(false), NULL); 183 base::Value::CreateBooleanValue(false), NULL);
141 badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 184 badmap.Set("string", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
142 NULL, CreateExternalDataFetcher().release()); 185 NULL,
186 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(),
187 std::string()));
143 188
144 descriptor->FilterBundle(&bundle); 189 schema_map->FilterBundle(&bundle);
145 EXPECT_TRUE(bundle.Equals(empty_bundle)); 190 EXPECT_TRUE(bundle.Equals(empty_bundle));
146 } 191 }
147 192
148 TEST_F(PolicyDomainDescriptorTest, LegacyComponents) { 193 TEST_F(SchemaMapTest, LegacyComponents) {
149 scoped_refptr<PolicyDomainDescriptor> descriptor =
150 new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS);
151 EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain());
152 EXPECT_TRUE(descriptor->components().empty());
153
154 std::string error; 194 std::string error;
155 Schema schema = Schema::Parse( 195 Schema schema = Schema::Parse(
156 "{" 196 "{"
157 " \"type\":\"object\"," 197 " \"type\":\"object\","
158 " \"properties\": {" 198 " \"properties\": {"
159 " \"String\": { \"type\": \"string\" }" 199 " \"String\": { \"type\": \"string\" }"
160 " }" 200 " }"
161 "}", &error); 201 "}", &error);
162 ASSERT_TRUE(schema.valid()) << error; 202 ASSERT_TRUE(schema.valid()) << error;
163 203
164 descriptor->RegisterComponent("with-schema", schema); 204 DomainMap domain_map;
165 descriptor->RegisterComponent("without-schema", Schema()); 205 domain_map[POLICY_DOMAIN_EXTENSIONS]["with-schema"] = schema;
166 206 domain_map[POLICY_DOMAIN_EXTENSIONS]["without-schema"] = Schema();
167 EXPECT_EQ(2u, descriptor->components().size()); 207 scoped_refptr<SchemaMap> schema_map = new SchemaMap(domain_map);
168 208
169 // |bundle| contains policies loaded by a policy provider. 209 // |bundle| contains policies loaded by a policy provider.
170 PolicyBundle bundle; 210 PolicyBundle bundle;
171 211
172 // Known components with schemas are filtered. 212 // Known components with schemas are filtered.
173 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "with-schema"); 213 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "with-schema");
174 bundle.Get(extension_ns).Set("String", 214 bundle.Get(extension_ns).Set("String",
175 POLICY_LEVEL_MANDATORY, 215 POLICY_LEVEL_MANDATORY,
176 POLICY_SCOPE_USER, 216 POLICY_SCOPE_USER,
177 base::Value::CreateStringValue("value 1"), 217 base::Value::CreateStringValue("value 1"),
178 NULL); 218 NULL);
179 219
180 // Known components without a schema are not filtered. 220 // Known components without a schema are not filtered.
181 PolicyNamespace without_schema_ns(POLICY_DOMAIN_EXTENSIONS, "without-schema"); 221 PolicyNamespace without_schema_ns(POLICY_DOMAIN_EXTENSIONS, "without-schema");
182 bundle.Get(without_schema_ns).Set("Schemaless", 222 bundle.Get(without_schema_ns).Set("Schemaless",
183 POLICY_LEVEL_MANDATORY, 223 POLICY_LEVEL_MANDATORY,
184 POLICY_SCOPE_USER, 224 POLICY_SCOPE_USER,
185 base::Value::CreateStringValue("value 2"), 225 base::Value::CreateStringValue("value 2"),
186 NULL); 226 NULL);
187 227
188 // Other namespaces aren't filtered. 228 // The Chrome namespace isn't filtered.
189 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); 229 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
190 bundle.Get(chrome_ns).Set("ChromePolicy", 230 bundle.Get(chrome_ns).Set("ChromePolicy",
191 POLICY_LEVEL_MANDATORY, 231 POLICY_LEVEL_MANDATORY,
192 POLICY_SCOPE_USER, 232 POLICY_SCOPE_USER,
193 base::Value::CreateStringValue("value 3"), 233 base::Value::CreateStringValue("value 3"),
194 NULL); 234 NULL);
195 235
196 PolicyBundle expected_bundle; 236 PolicyBundle expected_bundle;
197 expected_bundle.MergeFrom(bundle); 237 expected_bundle.MergeFrom(bundle);
198 238
199 // Unknown policies of known components with a schema are removed. 239 // Unknown policies of known components with a schema are removed.
200 bundle.Get(extension_ns).Set("Surprise", 240 bundle.Get(extension_ns).Set("Surprise",
201 POLICY_LEVEL_MANDATORY, 241 POLICY_LEVEL_MANDATORY,
202 POLICY_SCOPE_USER, 242 POLICY_SCOPE_USER,
203 base::Value::CreateStringValue("value 4"), 243 base::Value::CreateStringValue("value 4"),
204 NULL); 244 NULL);
205 245
206 // Unknown components are removed. 246 // Unknown components are removed.
207 PolicyNamespace unknown_ns(POLICY_DOMAIN_EXTENSIONS, "unknown"); 247 PolicyNamespace unknown_ns(POLICY_DOMAIN_EXTENSIONS, "unknown");
208 bundle.Get(unknown_ns).Set("Surprise", 248 bundle.Get(unknown_ns).Set("Surprise",
209 POLICY_LEVEL_MANDATORY, 249 POLICY_LEVEL_MANDATORY,
210 POLICY_SCOPE_USER, 250 POLICY_SCOPE_USER,
211 base::Value::CreateStringValue("value 5"), 251 base::Value::CreateStringValue("value 5"),
212 NULL); 252 NULL);
213 253
214 descriptor->FilterBundle(&bundle); 254 schema_map->FilterBundle(&bundle);
215 EXPECT_TRUE(bundle.Equals(expected_bundle)); 255 EXPECT_TRUE(bundle.Equals(expected_bundle));
216 } 256 }
217 257
218 } // namespace policy 258 } // namespace policy
OLDNEW
« 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