OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/policy/policy_domain_descriptor.h" |
| 6 |
| 7 #include "chrome/browser/policy/policy_bundle.h" |
| 8 #include "chrome/browser/policy/policy_schema.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace policy { |
| 12 |
| 13 TEST(PolicyDomainDescriptor, FilterBundle) { |
| 14 scoped_refptr<PolicyDomainDescriptor> descriptor = |
| 15 new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS); |
| 16 EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain()); |
| 17 EXPECT_TRUE(descriptor->components().empty()); |
| 18 |
| 19 std::string error; |
| 20 scoped_ptr<PolicySchema> schema = PolicySchema::Parse( |
| 21 "{" |
| 22 " \"$schema\":\"http://json-schema.org/draft-03/schema#\"," |
| 23 " \"type\":\"object\"," |
| 24 " \"properties\": {" |
| 25 " \"Array\": { \"type\": \"array\" }," |
| 26 " \"Boolean\": { \"type\": \"boolean\" }," |
| 27 " \"Integer\": { \"type\": \"integer\" }," |
| 28 " \"Null\": { \"type\": \"null\" }," |
| 29 " \"Number\": { \"type\": \"number\" }," |
| 30 " \"Object\": { \"type\": \"object\" }," |
| 31 " \"String\": { \"type\": \"string\" }" |
| 32 " }" |
| 33 "}", &error); |
| 34 ASSERT_TRUE(schema); |
| 35 |
| 36 descriptor->AddComponent("abc", schema.Pass()); |
| 37 |
| 38 EXPECT_EQ(1u, descriptor->components().size()); |
| 39 EXPECT_NE(descriptor->components().end(), |
| 40 descriptor->components().find("abc")); |
| 41 |
| 42 PolicyBundle bundle; |
| 43 descriptor->FilterBundle(&bundle); |
| 44 const PolicyBundle empty_bundle; |
| 45 EXPECT_TRUE(bundle.Equals(empty_bundle)); |
| 46 |
| 47 // Other namespace aren't filtered. |
| 48 PolicyBundle expected_bundle; |
| 49 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); |
| 50 expected_bundle.Get(chrome_ns).Set("ChromePolicy", |
| 51 POLICY_LEVEL_MANDATORY, |
| 52 POLICY_SCOPE_USER, |
| 53 base::Value::CreateStringValue("value")); |
| 54 bundle.CopyFrom(expected_bundle); |
| 55 descriptor->FilterBundle(&bundle); |
| 56 EXPECT_TRUE(bundle.Equals(expected_bundle)); |
| 57 |
| 58 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc"); |
| 59 PolicyMap& map = expected_bundle.Get(extension_ns); |
| 60 base::ListValue list; |
| 61 list.AppendString("a"); |
| 62 list.AppendString("b"); |
| 63 map.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 64 list.DeepCopy()); |
| 65 map.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 66 base::Value::CreateBooleanValue(true)); |
| 67 map.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 68 base::Value::CreateIntegerValue(1)); |
| 69 map.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 70 base::Value::CreateNullValue()); |
| 71 map.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 72 base::Value::CreateDoubleValue(1.2)); |
| 73 base::DictionaryValue dict; |
| 74 dict.SetString("a", "b"); |
| 75 dict.SetInteger("b", 2); |
| 76 map.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, dict.DeepCopy()); |
| 77 map.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 78 base::Value::CreateStringValue("value")); |
| 79 |
| 80 bundle.MergeFrom(expected_bundle); |
| 81 bundle.Get(extension_ns).Set("Unexpected", |
| 82 POLICY_LEVEL_MANDATORY, |
| 83 POLICY_SCOPE_USER, |
| 84 base::Value::CreateStringValue("to-be-removed")); |
| 85 |
| 86 descriptor->FilterBundle(&bundle); |
| 87 EXPECT_TRUE(bundle.Equals(expected_bundle)); |
| 88 |
| 89 // Mismatched types are also removed. |
| 90 bundle.Clear(); |
| 91 PolicyMap& badmap = bundle.Get(extension_ns); |
| 92 badmap.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 93 base::Value::CreateBooleanValue(false)); |
| 94 badmap.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 95 base::Value::CreateIntegerValue(0)); |
| 96 badmap.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 97 base::Value::CreateBooleanValue(false)); |
| 98 badmap.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 99 base::Value::CreateBooleanValue(false)); |
| 100 badmap.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 101 base::Value::CreateBooleanValue(false)); |
| 102 badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 103 base::Value::CreateBooleanValue(false)); |
| 104 badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 105 base::Value::CreateBooleanValue(false)); |
| 106 |
| 107 descriptor->FilterBundle(&bundle); |
| 108 EXPECT_TRUE(bundle.Equals(empty_bundle)); |
| 109 } |
| 110 |
| 111 } // namespace policy |
OLD | NEW |