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 <string> | |
8 | |
9 #include "base/callback.h" | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/policy/external_data_fetcher.h" | |
13 #include "chrome/browser/policy/external_data_manager.h" | |
14 #include "chrome/browser/policy/policy_bundle.h" | |
15 #include "chrome/browser/policy/policy_map.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace policy { | |
19 | |
20 class PolicyDomainDescriptorTest : public testing::Test { | |
21 protected: | |
22 scoped_ptr<ExternalDataFetcher> CreateExternalDataFetcher() const; | |
23 }; | |
24 | |
25 scoped_ptr<ExternalDataFetcher> | |
26 PolicyDomainDescriptorTest::CreateExternalDataFetcher() const { | |
27 return make_scoped_ptr( | |
28 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(), | |
29 std::string())); | |
30 } | |
31 | |
32 TEST_F(PolicyDomainDescriptorTest, FilterBundle) { | |
33 scoped_refptr<PolicyDomainDescriptor> descriptor = | |
34 new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS); | |
35 EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain()); | |
36 EXPECT_TRUE(descriptor->components().empty()); | |
37 | |
38 std::string error; | |
39 Schema schema = Schema::Parse( | |
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; | |
62 | |
63 descriptor->RegisterComponent("abc", schema); | |
64 | |
65 EXPECT_EQ(1u, descriptor->components().size()); | |
66 EXPECT_EQ(1u, descriptor->components().count("abc")); | |
67 | |
68 PolicyBundle bundle; | |
69 descriptor->FilterBundle(&bundle); | |
70 const PolicyBundle empty_bundle; | |
71 EXPECT_TRUE(bundle.Equals(empty_bundle)); | |
72 | |
73 // Other namespaces aren't filtered. | |
74 PolicyBundle expected_bundle; | |
75 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); | |
76 expected_bundle.Get(chrome_ns).Set("ChromePolicy", | |
77 POLICY_LEVEL_MANDATORY, | |
78 POLICY_SCOPE_USER, | |
79 base::Value::CreateStringValue("value"), | |
80 NULL); | |
81 bundle.CopyFrom(expected_bundle); | |
82 // Unknown components of the domain are filtered out. | |
83 PolicyNamespace another_extension_ns(POLICY_DOMAIN_EXTENSIONS, "xyz"); | |
84 bundle.Get(another_extension_ns).Set( | |
85 "AnotherExtensionPolicy", | |
86 POLICY_LEVEL_MANDATORY, | |
87 POLICY_SCOPE_USER, | |
88 base::Value::CreateStringValue("value"), | |
89 NULL); | |
90 descriptor->FilterBundle(&bundle); | |
91 EXPECT_TRUE(bundle.Equals(expected_bundle)); | |
92 | |
93 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "abc"); | |
94 PolicyMap& map = expected_bundle.Get(extension_ns); | |
95 base::ListValue list; | |
96 list.AppendString("a"); | |
97 list.AppendString("b"); | |
98 map.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
99 list.DeepCopy(), NULL); | |
100 map.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
101 base::Value::CreateBooleanValue(true), NULL); | |
102 map.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
103 base::Value::CreateIntegerValue(1), NULL); | |
104 map.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
105 base::Value::CreateNullValue(), NULL); | |
106 map.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
107 base::Value::CreateDoubleValue(1.2), NULL); | |
108 base::DictionaryValue dict; | |
109 dict.SetString("a", "b"); | |
110 dict.SetInteger("b", 2); | |
111 map.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
112 dict.DeepCopy(), NULL); | |
113 map.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
114 base::Value::CreateStringValue("value"), NULL); | |
115 | |
116 bundle.MergeFrom(expected_bundle); | |
117 bundle.Get(extension_ns).Set("Unexpected", | |
118 POLICY_LEVEL_MANDATORY, | |
119 POLICY_SCOPE_USER, | |
120 base::Value::CreateStringValue("to-be-removed"), | |
121 NULL); | |
122 | |
123 descriptor->FilterBundle(&bundle); | |
124 EXPECT_TRUE(bundle.Equals(expected_bundle)); | |
125 | |
126 // Mismatched types are also removed. | |
127 bundle.Clear(); | |
128 PolicyMap& badmap = bundle.Get(extension_ns); | |
129 badmap.Set("Array", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
130 base::Value::CreateBooleanValue(false), NULL); | |
131 badmap.Set("Boolean", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
132 base::Value::CreateIntegerValue(0), NULL); | |
133 badmap.Set("Integer", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
134 base::Value::CreateBooleanValue(false), NULL); | |
135 badmap.Set("Null", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
136 base::Value::CreateBooleanValue(false), NULL); | |
137 badmap.Set("Number", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
138 base::Value::CreateBooleanValue(false), NULL); | |
139 badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
140 base::Value::CreateBooleanValue(false), NULL); | |
141 badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
142 NULL, CreateExternalDataFetcher().release()); | |
143 | |
144 descriptor->FilterBundle(&bundle); | |
145 EXPECT_TRUE(bundle.Equals(empty_bundle)); | |
146 } | |
147 | |
148 TEST_F(PolicyDomainDescriptorTest, 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; | |
155 Schema schema = Schema::Parse( | |
156 "{" | |
157 " \"type\":\"object\"," | |
158 " \"properties\": {" | |
159 " \"String\": { \"type\": \"string\" }" | |
160 " }" | |
161 "}", &error); | |
162 ASSERT_TRUE(schema.valid()) << error; | |
163 | |
164 descriptor->RegisterComponent("with-schema", schema); | |
165 descriptor->RegisterComponent("without-schema", Schema()); | |
166 | |
167 EXPECT_EQ(2u, descriptor->components().size()); | |
168 | |
169 // |bundle| contains policies loaded by a policy provider. | |
170 PolicyBundle bundle; | |
171 | |
172 // Known components with schemas are filtered. | |
173 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "with-schema"); | |
174 bundle.Get(extension_ns).Set("String", | |
175 POLICY_LEVEL_MANDATORY, | |
176 POLICY_SCOPE_USER, | |
177 base::Value::CreateStringValue("value 1"), | |
178 NULL); | |
179 | |
180 // Known components without a schema are not filtered. | |
181 PolicyNamespace without_schema_ns(POLICY_DOMAIN_EXTENSIONS, "without-schema"); | |
182 bundle.Get(without_schema_ns).Set("Schemaless", | |
183 POLICY_LEVEL_MANDATORY, | |
184 POLICY_SCOPE_USER, | |
185 base::Value::CreateStringValue("value 2"), | |
186 NULL); | |
187 | |
188 // Other namespaces aren't filtered. | |
189 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); | |
190 bundle.Get(chrome_ns).Set("ChromePolicy", | |
191 POLICY_LEVEL_MANDATORY, | |
192 POLICY_SCOPE_USER, | |
193 base::Value::CreateStringValue("value 3"), | |
194 NULL); | |
195 | |
196 PolicyBundle expected_bundle; | |
197 expected_bundle.MergeFrom(bundle); | |
198 | |
199 // Unknown policies of known components with a schema are removed. | |
200 bundle.Get(extension_ns).Set("Surprise", | |
201 POLICY_LEVEL_MANDATORY, | |
202 POLICY_SCOPE_USER, | |
203 base::Value::CreateStringValue("value 4"), | |
204 NULL); | |
205 | |
206 // Unknown components are removed. | |
207 PolicyNamespace unknown_ns(POLICY_DOMAIN_EXTENSIONS, "unknown"); | |
208 bundle.Get(unknown_ns).Set("Surprise", | |
209 POLICY_LEVEL_MANDATORY, | |
210 POLICY_SCOPE_USER, | |
211 base::Value::CreateStringValue("value 5"), | |
212 NULL); | |
213 | |
214 descriptor->FilterBundle(&bundle); | |
215 EXPECT_TRUE(bundle.Equals(expected_bundle)); | |
216 } | |
217 | |
218 } // namespace policy | |
OLD | NEW |