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

Side by Side Diff: components/policy/core/common/schema_map_unittest.cc

Issue 1304843004: Add source column to chrome://policy showing the origins of policies. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed another test. Created 5 years, 3 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 unified diff | Download patch
OLDNEW
1 // Copyright 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 "components/policy/core/common/schema_map.h" 5 #include "components/policy/core/common/schema_map.h"
6 6
7 #include "base/memory/weak_ptr.h" 7 #include "base/memory/weak_ptr.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "components/policy/core/common/external_data_fetcher.h" 9 #include "components/policy/core/common/external_data_fetcher.h"
10 #include "components/policy/core/common/external_data_manager.h" 10 #include "components/policy/core/common/external_data_manager.h"
11 #include "components/policy/core/common/policy_bundle.h" 11 #include "components/policy/core/common/policy_bundle.h"
12 #include "components/policy/core/common/policy_map.h" 12 #include "components/policy/core/common/policy_map.h"
13 #include "components/policy/core/common/policy_types.h"
13 #include "components/policy/core/common/schema.h" 14 #include "components/policy/core/common/schema.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace policy { 17 namespace policy {
17 18
18 namespace { 19 namespace {
19 20
20 const char kTestSchema[] = 21 const char kTestSchema[] =
21 "{" 22 "{"
22 " \"type\": \"object\"," 23 " \"type\": \"object\","
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 schema_map->FilterBundle(&bundle); 135 schema_map->FilterBundle(&bundle);
135 const PolicyBundle empty_bundle; 136 const PolicyBundle empty_bundle;
136 EXPECT_TRUE(bundle.Equals(empty_bundle)); 137 EXPECT_TRUE(bundle.Equals(empty_bundle));
137 138
138 // The Chrome namespace isn't filtered. 139 // The Chrome namespace isn't filtered.
139 PolicyBundle expected_bundle; 140 PolicyBundle expected_bundle;
140 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); 141 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
141 expected_bundle.Get(chrome_ns).Set("ChromePolicy", 142 expected_bundle.Get(chrome_ns).Set("ChromePolicy",
142 POLICY_LEVEL_MANDATORY, 143 POLICY_LEVEL_MANDATORY,
143 POLICY_SCOPE_USER, 144 POLICY_SCOPE_USER,
145 POLICY_SOURCE_CLOUD,
144 new base::StringValue("value"), 146 new base::StringValue("value"),
145 NULL); 147 NULL);
146 bundle.CopyFrom(expected_bundle); 148 bundle.CopyFrom(expected_bundle);
147 149
148 // Unknown components are filtered out. 150 // Unknown components are filtered out.
149 PolicyNamespace another_extension_ns(POLICY_DOMAIN_EXTENSIONS, "xyz"); 151 PolicyNamespace another_extension_ns(POLICY_DOMAIN_EXTENSIONS, "xyz");
150 bundle.Get(another_extension_ns).Set("AnotherExtensionPolicy", 152 bundle.Get(another_extension_ns).Set("AnotherExtensionPolicy",
151 POLICY_LEVEL_MANDATORY, 153 POLICY_LEVEL_MANDATORY,
152 POLICY_SCOPE_USER, 154 POLICY_SCOPE_USER,
155 POLICY_SOURCE_CLOUD,
153 new base::StringValue("value"), 156 new base::StringValue("value"),
154 NULL); 157 NULL);
155 schema_map->FilterBundle(&bundle); 158 schema_map->FilterBundle(&bundle);
156 EXPECT_TRUE(bundle.Equals(expected_bundle)); 159 EXPECT_TRUE(bundle.Equals(expected_bundle));
157 160
158 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc"); 161 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc");
159 PolicyMap& map = expected_bundle.Get(extension_ns); 162 PolicyMap& map = expected_bundle.Get(extension_ns);
160 base::ListValue list; 163 base::ListValue list;
161 list.AppendString("a"); 164 list.AppendString("a");
162 list.AppendString("b"); 165 list.AppendString("b");
163 map.Set("list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 166 map.Set("list", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
164 list.DeepCopy(), NULL); 167 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr);
165 map.Set("boolean", 168 map.Set("boolean",
166 POLICY_LEVEL_MANDATORY, 169 POLICY_LEVEL_MANDATORY,
167 POLICY_SCOPE_USER, 170 POLICY_SCOPE_USER,
171 POLICY_SOURCE_CLOUD,
168 new base::FundamentalValue(true), 172 new base::FundamentalValue(true),
169 NULL); 173 NULL);
170 map.Set("integer", 174 map.Set("integer",
171 POLICY_LEVEL_MANDATORY, 175 POLICY_LEVEL_MANDATORY,
172 POLICY_SCOPE_USER, 176 POLICY_SCOPE_USER,
177 POLICY_SOURCE_CLOUD,
173 new base::FundamentalValue(1), 178 new base::FundamentalValue(1),
174 NULL); 179 NULL);
175 map.Set("null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 180 map.Set("null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
176 base::Value::CreateNullValue().release(), NULL); 181 POLICY_SOURCE_CLOUD, base::Value::CreateNullValue().release(),
182 nullptr);
177 map.Set("double", 183 map.Set("double",
178 POLICY_LEVEL_MANDATORY, 184 POLICY_LEVEL_MANDATORY,
179 POLICY_SCOPE_USER, 185 POLICY_SCOPE_USER,
186 POLICY_SOURCE_CLOUD,
180 new base::FundamentalValue(1.2), 187 new base::FundamentalValue(1.2),
181 NULL); 188 NULL);
182 base::DictionaryValue dict; 189 base::DictionaryValue dict;
183 dict.SetString("a", "b"); 190 dict.SetString("a", "b");
184 dict.SetInteger("b", 2); 191 dict.SetInteger("b", 2);
185 map.Set("object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 192 map.Set("object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
186 dict.DeepCopy(), NULL); 193 POLICY_SOURCE_CLOUD, dict.DeepCopy(), nullptr);
187 map.Set("string", 194 map.Set("string",
188 POLICY_LEVEL_MANDATORY, 195 POLICY_LEVEL_MANDATORY,
189 POLICY_SCOPE_USER, 196 POLICY_SCOPE_USER,
197 POLICY_SOURCE_CLOUD,
190 new base::StringValue("value"), 198 new base::StringValue("value"),
191 NULL); 199 NULL);
192 200
193 bundle.MergeFrom(expected_bundle); 201 bundle.MergeFrom(expected_bundle);
194 bundle.Get(extension_ns).Set("Unexpected", 202 bundle.Get(extension_ns).Set("Unexpected",
195 POLICY_LEVEL_MANDATORY, 203 POLICY_LEVEL_MANDATORY,
196 POLICY_SCOPE_USER, 204 POLICY_SCOPE_USER,
205 POLICY_SOURCE_CLOUD,
197 new base::StringValue("to-be-removed"), 206 new base::StringValue("to-be-removed"),
198 NULL); 207 NULL);
199 208
200 schema_map->FilterBundle(&bundle); 209 schema_map->FilterBundle(&bundle);
201 EXPECT_TRUE(bundle.Equals(expected_bundle)); 210 EXPECT_TRUE(bundle.Equals(expected_bundle));
202 211
203 // Mismatched types are also removed. 212 // Mismatched types are also removed.
204 bundle.Clear(); 213 bundle.Clear();
205 PolicyMap& badmap = bundle.Get(extension_ns); 214 PolicyMap& badmap = bundle.Get(extension_ns);
206 badmap.Set("list", 215 badmap.Set("list",
207 POLICY_LEVEL_MANDATORY, 216 POLICY_LEVEL_MANDATORY,
208 POLICY_SCOPE_USER, 217 POLICY_SCOPE_USER,
218 POLICY_SOURCE_CLOUD,
209 new base::FundamentalValue(false), 219 new base::FundamentalValue(false),
210 NULL); 220 NULL);
211 badmap.Set("boolean", 221 badmap.Set("boolean",
212 POLICY_LEVEL_MANDATORY, 222 POLICY_LEVEL_MANDATORY,
213 POLICY_SCOPE_USER, 223 POLICY_SCOPE_USER,
224 POLICY_SOURCE_CLOUD,
214 new base::FundamentalValue(0), 225 new base::FundamentalValue(0),
215 NULL); 226 NULL);
216 badmap.Set("integer", 227 badmap.Set("integer",
217 POLICY_LEVEL_MANDATORY, 228 POLICY_LEVEL_MANDATORY,
218 POLICY_SCOPE_USER, 229 POLICY_SCOPE_USER,
230 POLICY_SOURCE_CLOUD,
219 new base::FundamentalValue(false), 231 new base::FundamentalValue(false),
220 NULL); 232 NULL);
221 badmap.Set("null", 233 badmap.Set("null",
222 POLICY_LEVEL_MANDATORY, 234 POLICY_LEVEL_MANDATORY,
223 POLICY_SCOPE_USER, 235 POLICY_SCOPE_USER,
236 POLICY_SOURCE_CLOUD,
224 new base::FundamentalValue(false), 237 new base::FundamentalValue(false),
225 NULL); 238 NULL);
226 badmap.Set("double", 239 badmap.Set("double",
227 POLICY_LEVEL_MANDATORY, 240 POLICY_LEVEL_MANDATORY,
228 POLICY_SCOPE_USER, 241 POLICY_SCOPE_USER,
242 POLICY_SOURCE_CLOUD,
229 new base::FundamentalValue(false), 243 new base::FundamentalValue(false),
230 NULL); 244 NULL);
231 badmap.Set("object", 245 badmap.Set("object",
232 POLICY_LEVEL_MANDATORY, 246 POLICY_LEVEL_MANDATORY,
233 POLICY_SCOPE_USER, 247 POLICY_SCOPE_USER,
248 POLICY_SOURCE_CLOUD,
234 new base::FundamentalValue(false), 249 new base::FundamentalValue(false),
235 NULL); 250 NULL);
236 badmap.Set("string", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 251 badmap.Set("string", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
237 NULL, 252 POLICY_SOURCE_CLOUD, nullptr,
238 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(), 253 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(),
239 std::string())); 254 std::string()));
240 255
241 schema_map->FilterBundle(&bundle); 256 schema_map->FilterBundle(&bundle);
242 EXPECT_TRUE(bundle.Equals(empty_bundle)); 257 EXPECT_TRUE(bundle.Equals(empty_bundle));
243 } 258 }
244 259
245 TEST_F(SchemaMapTest, LegacyComponents) { 260 TEST_F(SchemaMapTest, LegacyComponents) {
246 std::string error; 261 std::string error;
247 Schema schema = Schema::Parse( 262 Schema schema = Schema::Parse(
(...skipping 11 matching lines...) Expand all
259 scoped_refptr<SchemaMap> schema_map = new SchemaMap(domain_map); 274 scoped_refptr<SchemaMap> schema_map = new SchemaMap(domain_map);
260 275
261 // |bundle| contains policies loaded by a policy provider. 276 // |bundle| contains policies loaded by a policy provider.
262 PolicyBundle bundle; 277 PolicyBundle bundle;
263 278
264 // Known components with schemas are filtered. 279 // Known components with schemas are filtered.
265 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "with-schema"); 280 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "with-schema");
266 bundle.Get(extension_ns).Set("String", 281 bundle.Get(extension_ns).Set("String",
267 POLICY_LEVEL_MANDATORY, 282 POLICY_LEVEL_MANDATORY,
268 POLICY_SCOPE_USER, 283 POLICY_SCOPE_USER,
284 POLICY_SOURCE_CLOUD,
269 new base::StringValue("value 1"), 285 new base::StringValue("value 1"),
270 NULL); 286 NULL);
271 287
272 // The Chrome namespace isn't filtered. 288 // The Chrome namespace isn't filtered.
273 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); 289 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
274 bundle.Get(chrome_ns).Set("ChromePolicy", 290 bundle.Get(chrome_ns).Set("ChromePolicy",
275 POLICY_LEVEL_MANDATORY, 291 POLICY_LEVEL_MANDATORY,
276 POLICY_SCOPE_USER, 292 POLICY_SCOPE_USER,
293 POLICY_SOURCE_CLOUD,
277 new base::StringValue("value 3"), 294 new base::StringValue("value 3"),
278 NULL); 295 NULL);
279 296
280 PolicyBundle expected_bundle; 297 PolicyBundle expected_bundle;
281 expected_bundle.MergeFrom(bundle); 298 expected_bundle.MergeFrom(bundle);
282 299
283 // Known components without a schema are filtered out completely. 300 // Known components without a schema are filtered out completely.
284 PolicyNamespace without_schema_ns(POLICY_DOMAIN_EXTENSIONS, "without-schema"); 301 PolicyNamespace without_schema_ns(POLICY_DOMAIN_EXTENSIONS, "without-schema");
285 bundle.Get(without_schema_ns).Set("Schemaless", 302 bundle.Get(without_schema_ns).Set("Schemaless",
286 POLICY_LEVEL_MANDATORY, 303 POLICY_LEVEL_MANDATORY,
287 POLICY_SCOPE_USER, 304 POLICY_SCOPE_USER,
305 POLICY_SOURCE_CLOUD,
288 new base::StringValue("value 2"), 306 new base::StringValue("value 2"),
289 NULL); 307 NULL);
290 308
291 // Unknown policies of known components with a schema are removed. 309 // Unknown policies of known components with a schema are removed.
292 bundle.Get(extension_ns).Set("Surprise", 310 bundle.Get(extension_ns).Set("Surprise",
293 POLICY_LEVEL_MANDATORY, 311 POLICY_LEVEL_MANDATORY,
294 POLICY_SCOPE_USER, 312 POLICY_SCOPE_USER,
313 POLICY_SOURCE_CLOUD,
295 new base::StringValue("value 4"), 314 new base::StringValue("value 4"),
296 NULL); 315 NULL);
297 316
298 // Unknown components are removed. 317 // Unknown components are removed.
299 PolicyNamespace unknown_ns(POLICY_DOMAIN_EXTENSIONS, "unknown"); 318 PolicyNamespace unknown_ns(POLICY_DOMAIN_EXTENSIONS, "unknown");
300 bundle.Get(unknown_ns).Set("Surprise", 319 bundle.Get(unknown_ns).Set("Surprise",
301 POLICY_LEVEL_MANDATORY, 320 POLICY_LEVEL_MANDATORY,
302 POLICY_SCOPE_USER, 321 POLICY_SCOPE_USER,
322 POLICY_SOURCE_CLOUD,
303 new base::StringValue("value 5"), 323 new base::StringValue("value 5"),
304 NULL); 324 NULL);
305 325
306 schema_map->FilterBundle(&bundle); 326 schema_map->FilterBundle(&bundle);
307 EXPECT_TRUE(bundle.Equals(expected_bundle)); 327 EXPECT_TRUE(bundle.Equals(expected_bundle));
308 } 328 }
309 329
310 TEST_F(SchemaMapTest, GetChanges) { 330 TEST_F(SchemaMapTest, GetChanges) {
311 DomainMap map; 331 DomainMap map;
312 map[POLICY_DOMAIN_CHROME][""] = Schema(); 332 map[POLICY_DOMAIN_CHROME][""] = Schema();
(...skipping 20 matching lines...) Expand all
333 newer = new SchemaMap(map); 353 newer = new SchemaMap(map);
334 newer->GetChanges(older, &removed, &added); 354 newer->GetChanges(older, &removed, &added);
335 ASSERT_EQ(2u, removed.size()); 355 ASSERT_EQ(2u, removed.size());
336 EXPECT_EQ(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), removed[0]); 356 EXPECT_EQ(PolicyNamespace(POLICY_DOMAIN_CHROME, ""), removed[0]);
337 EXPECT_EQ(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"), removed[1]); 357 EXPECT_EQ(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"), removed[1]);
338 ASSERT_EQ(1u, added.size()); 358 ASSERT_EQ(1u, added.size());
339 EXPECT_EQ(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), added[0]); 359 EXPECT_EQ(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "abc"), added[0]);
340 } 360 }
341 361
342 } // namespace policy 362 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698