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

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: 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
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;
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'
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_TRUE(map->GetComponents(POLICY_DOMAIN_CHROME).empty());
29 std::string())); 71 EXPECT_TRUE(map->GetComponents(POLICY_DOMAIN_EXTENSIONS).empty());
72 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.
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(POLICY_DOMAIN_CHROME, ""));
80 EXPECT_FALSE(map->GetSchema(POLICY_DOMAIN_CHROME, "extension-1"));
81 EXPECT_FALSE(map->GetSchema(POLICY_DOMAIN_CHROME, "legacy-extension"));
82 EXPECT_FALSE(map->GetSchema(POLICY_DOMAIN_EXTENSIONS, ""));
83 EXPECT_FALSE(map->GetSchema(POLICY_DOMAIN_EXTENSIONS, "extension-3"));
84
85 const Schema* schema =
86 map->GetSchema(POLICY_DOMAIN_EXTENSIONS, "extension-1");
87 ASSERT_TRUE(schema);
88 EXPECT_TRUE(schema->valid());
89
90 schema = map->GetSchema(POLICY_DOMAIN_EXTENSIONS, "legacy-extension");
91 ASSERT_TRUE(schema);
92 EXPECT_FALSE(schema->valid());
93 }
94
95 TEST_F(SchemaMapTest, FilterBundle) {
38 std::string error; 96 std::string error;
39 Schema schema = Schema::Parse( 97 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; 98 ASSERT_TRUE(schema.valid()) << error;
62 99
63 descriptor->RegisterComponent("abc", schema); 100 DomainMap domain_map;
64 101 domain_map[POLICY_DOMAIN_EXTENSIONS]["abc"] = schema;
65 EXPECT_EQ(1u, descriptor->components().size()); 102 scoped_refptr<SchemaMap> schema_map = new SchemaMap(domain_map);
66 EXPECT_EQ(1u, descriptor->components().count("abc"));
67 103
68 PolicyBundle bundle; 104 PolicyBundle bundle;
69 descriptor->FilterBundle(&bundle); 105 schema_map->FilterBundle(&bundle);
70 const PolicyBundle empty_bundle; 106 const PolicyBundle empty_bundle;
71 EXPECT_TRUE(bundle.Equals(empty_bundle)); 107 EXPECT_TRUE(bundle.Equals(empty_bundle));
72 108
73 // Other namespaces aren't filtered. 109 // The Chrome namespace isn't filtered.
74 PolicyBundle expected_bundle; 110 PolicyBundle expected_bundle;
75 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); 111 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
76 expected_bundle.Get(chrome_ns).Set("ChromePolicy", 112 expected_bundle.Get(chrome_ns).Set("ChromePolicy",
77 POLICY_LEVEL_MANDATORY, 113 POLICY_LEVEL_MANDATORY,
78 POLICY_SCOPE_USER, 114 POLICY_SCOPE_USER,
79 base::Value::CreateStringValue("value"), 115 base::Value::CreateStringValue("value"),
80 NULL); 116 NULL);
81 bundle.CopyFrom(expected_bundle); 117 bundle.CopyFrom(expected_bundle);
82 // Unknown components of the domain are filtered out. 118
119 // Unknown components are filtered out.
83 PolicyNamespace another_extension_ns(POLICY_DOMAIN_EXTENSIONS, "xyz"); 120 PolicyNamespace another_extension_ns(POLICY_DOMAIN_EXTENSIONS, "xyz");
84 bundle.Get(another_extension_ns).Set( 121 bundle.Get(another_extension_ns).Set(
85 "AnotherExtensionPolicy", 122 "AnotherExtensionPolicy",
86 POLICY_LEVEL_MANDATORY, 123 POLICY_LEVEL_MANDATORY,
87 POLICY_SCOPE_USER, 124 POLICY_SCOPE_USER,
88 base::Value::CreateStringValue("value"), 125 base::Value::CreateStringValue("value"),
89 NULL); 126 NULL);
90 descriptor->FilterBundle(&bundle); 127 schema_map->FilterBundle(&bundle);
91 EXPECT_TRUE(bundle.Equals(expected_bundle)); 128 EXPECT_TRUE(bundle.Equals(expected_bundle));
92 129
93 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc"); 130 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc");
94 PolicyMap& map = expected_bundle.Get(extension_ns); 131 PolicyMap& map = expected_bundle.Get(extension_ns);
95 base::ListValue list; 132 base::ListValue list;
96 list.AppendString("a"); 133 list.AppendString("a");
97 list.AppendString("b"); 134 list.AppendString("b");
98 map.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 135 map.Set("list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
99 list.DeepCopy(), NULL); 136 list.DeepCopy(), NULL);
100 map.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 137 map.Set("boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
101 base::Value::CreateBooleanValue(true), NULL); 138 base::Value::CreateBooleanValue(true), NULL);
102 map.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 139 map.Set("integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
103 base::Value::CreateIntegerValue(1), NULL); 140 base::Value::CreateIntegerValue(1), NULL);
104 map.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 141 map.Set("null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
105 base::Value::CreateNullValue(), NULL); 142 base::Value::CreateNullValue(), NULL);
106 map.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 143 map.Set("double", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
107 base::Value::CreateDoubleValue(1.2), NULL); 144 base::Value::CreateDoubleValue(1.2), NULL);
108 base::DictionaryValue dict; 145 base::DictionaryValue dict;
109 dict.SetString("a", "b"); 146 dict.SetString("a", "b");
110 dict.SetInteger("b", 2); 147 dict.SetInteger("b", 2);
111 map.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 148 map.Set("object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
112 dict.DeepCopy(), NULL); 149 dict.DeepCopy(), NULL);
113 map.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 150 map.Set("string", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
114 base::Value::CreateStringValue("value"), NULL); 151 base::Value::CreateStringValue("value"), NULL);
115 152
116 bundle.MergeFrom(expected_bundle); 153 bundle.MergeFrom(expected_bundle);
117 bundle.Get(extension_ns).Set("Unexpected", 154 bundle.Get(extension_ns).Set("Unexpected",
118 POLICY_LEVEL_MANDATORY, 155 POLICY_LEVEL_MANDATORY,
119 POLICY_SCOPE_USER, 156 POLICY_SCOPE_USER,
120 base::Value::CreateStringValue("to-be-removed"), 157 base::Value::CreateStringValue("to-be-removed"),
121 NULL); 158 NULL);
122 159
123 descriptor->FilterBundle(&bundle); 160 schema_map->FilterBundle(&bundle);
124 EXPECT_TRUE(bundle.Equals(expected_bundle)); 161 EXPECT_TRUE(bundle.Equals(expected_bundle));
125 162
126 // Mismatched types are also removed. 163 // Mismatched types are also removed.
127 bundle.Clear(); 164 bundle.Clear();
128 PolicyMap& badmap = bundle.Get(extension_ns); 165 PolicyMap& badmap = bundle.Get(extension_ns);
129 badmap.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 166 badmap.Set("list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
130 base::Value::CreateBooleanValue(false), NULL); 167 base::Value::CreateBooleanValue(false), NULL);
131 badmap.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 168 badmap.Set("boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
132 base::Value::CreateIntegerValue(0), NULL); 169 base::Value::CreateIntegerValue(0), NULL);
133 badmap.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 170 badmap.Set("integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
134 base::Value::CreateBooleanValue(false), NULL); 171 base::Value::CreateBooleanValue(false), NULL);
135 badmap.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 172 badmap.Set("null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
136 base::Value::CreateBooleanValue(false), NULL); 173 base::Value::CreateBooleanValue(false), NULL);
137 badmap.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 174 badmap.Set("double", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
138 base::Value::CreateBooleanValue(false), NULL); 175 base::Value::CreateBooleanValue(false), NULL);
139 badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 176 badmap.Set("object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
140 base::Value::CreateBooleanValue(false), NULL); 177 base::Value::CreateBooleanValue(false), NULL);
141 badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 178 badmap.Set("string", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
142 NULL, CreateExternalDataFetcher().release()); 179 NULL,
180 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(),
181 std::string()));
143 182
144 descriptor->FilterBundle(&bundle); 183 schema_map->FilterBundle(&bundle);
145 EXPECT_TRUE(bundle.Equals(empty_bundle)); 184 EXPECT_TRUE(bundle.Equals(empty_bundle));
146 } 185 }
147 186
148 TEST_F(PolicyDomainDescriptorTest, LegacyComponents) { 187 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; 188 std::string error;
155 Schema schema = Schema::Parse( 189 Schema schema = Schema::Parse(
156 "{" 190 "{"
157 " \"type\":\"object\"," 191 " \"type\":\"object\","
158 " \"properties\": {" 192 " \"properties\": {"
159 " \"String\": { \"type\": \"string\" }" 193 " \"String\": { \"type\": \"string\" }"
160 " }" 194 " }"
161 "}", &error); 195 "}", &error);
162 ASSERT_TRUE(schema.valid()) << error; 196 ASSERT_TRUE(schema.valid()) << error;
163 197
164 descriptor->RegisterComponent("with-schema", schema); 198 DomainMap domain_map;
165 descriptor->RegisterComponent("without-schema", Schema()); 199 domain_map[POLICY_DOMAIN_EXTENSIONS]["with-schema"] = schema;
166 200 domain_map[POLICY_DOMAIN_EXTENSIONS]["without-schema"] = Schema();
167 EXPECT_EQ(2u, descriptor->components().size()); 201 scoped_refptr<SchemaMap> schema_map = new SchemaMap(domain_map);
168 202
169 // |bundle| contains policies loaded by a policy provider. 203 // |bundle| contains policies loaded by a policy provider.
170 PolicyBundle bundle; 204 PolicyBundle bundle;
171 205
172 // Known components with schemas are filtered. 206 // Known components with schemas are filtered.
173 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "with-schema"); 207 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "with-schema");
174 bundle.Get(extension_ns).Set("String", 208 bundle.Get(extension_ns).Set("String",
175 POLICY_LEVEL_MANDATORY, 209 POLICY_LEVEL_MANDATORY,
176 POLICY_SCOPE_USER, 210 POLICY_SCOPE_USER,
177 base::Value::CreateStringValue("value 1"), 211 base::Value::CreateStringValue("value 1"),
178 NULL); 212 NULL);
179 213
180 // Known components without a schema are not filtered. 214 // Known components without a schema are not filtered.
181 PolicyNamespace without_schema_ns(POLICY_DOMAIN_EXTENSIONS, "without-schema"); 215 PolicyNamespace without_schema_ns(POLICY_DOMAIN_EXTENSIONS, "without-schema");
182 bundle.Get(without_schema_ns).Set("Schemaless", 216 bundle.Get(without_schema_ns).Set("Schemaless",
183 POLICY_LEVEL_MANDATORY, 217 POLICY_LEVEL_MANDATORY,
184 POLICY_SCOPE_USER, 218 POLICY_SCOPE_USER,
185 base::Value::CreateStringValue("value 2"), 219 base::Value::CreateStringValue("value 2"),
186 NULL); 220 NULL);
187 221
188 // Other namespaces aren't filtered. 222 // The Chrome namespace isn't filtered.
189 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); 223 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
190 bundle.Get(chrome_ns).Set("ChromePolicy", 224 bundle.Get(chrome_ns).Set("ChromePolicy",
191 POLICY_LEVEL_MANDATORY, 225 POLICY_LEVEL_MANDATORY,
192 POLICY_SCOPE_USER, 226 POLICY_SCOPE_USER,
193 base::Value::CreateStringValue("value 3"), 227 base::Value::CreateStringValue("value 3"),
194 NULL); 228 NULL);
195 229
196 PolicyBundle expected_bundle; 230 PolicyBundle expected_bundle;
197 expected_bundle.MergeFrom(bundle); 231 expected_bundle.MergeFrom(bundle);
198 232
199 // Unknown policies of known components with a schema are removed. 233 // Unknown policies of known components with a schema are removed.
200 bundle.Get(extension_ns).Set("Surprise", 234 bundle.Get(extension_ns).Set("Surprise",
201 POLICY_LEVEL_MANDATORY, 235 POLICY_LEVEL_MANDATORY,
202 POLICY_SCOPE_USER, 236 POLICY_SCOPE_USER,
203 base::Value::CreateStringValue("value 4"), 237 base::Value::CreateStringValue("value 4"),
204 NULL); 238 NULL);
205 239
206 // Unknown components are removed. 240 // Unknown components are removed.
207 PolicyNamespace unknown_ns(POLICY_DOMAIN_EXTENSIONS, "unknown"); 241 PolicyNamespace unknown_ns(POLICY_DOMAIN_EXTENSIONS, "unknown");
208 bundle.Get(unknown_ns).Set("Surprise", 242 bundle.Get(unknown_ns).Set("Surprise",
209 POLICY_LEVEL_MANDATORY, 243 POLICY_LEVEL_MANDATORY,
210 POLICY_SCOPE_USER, 244 POLICY_SCOPE_USER,
211 base::Value::CreateStringValue("value 5"), 245 base::Value::CreateStringValue("value 5"),
212 NULL); 246 NULL);
213 247
214 descriptor->FilterBundle(&bundle); 248 schema_map->FilterBundle(&bundle);
215 EXPECT_TRUE(bundle.Equals(expected_bundle)); 249 EXPECT_TRUE(bundle.Equals(expected_bundle));
216 } 250 }
217 251
218 } // namespace policy 252 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698