OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/policy_domain_descriptor.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/policy/external_data_fetcher.h" | 12 #include "chrome/browser/policy/external_data_fetcher.h" |
13 #include "chrome/browser/policy/external_data_manager.h" | 13 #include "chrome/browser/policy/external_data_manager.h" |
14 #include "chrome/browser/policy/policy_bundle.h" | 14 #include "chrome/browser/policy/policy_bundle.h" |
15 #include "chrome/browser/policy/policy_map.h" | 15 #include "chrome/browser/policy/policy_map.h" |
16 #include "components/policy/core/common/policy_schema.h" | 16 #include "components/policy/core/common/schema.h" |
bartfab (slow)
2013/09/24 10:03:55
Nit: No need for that here. policy_domain_descript
Joao da Silva
2013/09/24 10:16:31
Done.
| |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace policy { | 19 namespace policy { |
20 | 20 |
21 class PolicyDomainDescriptorTest : public testing::Test { | 21 class PolicyDomainDescriptorTest : public testing::Test { |
22 protected: | 22 protected: |
23 scoped_ptr<ExternalDataFetcher> CreateExternalDataFetcher() const; | 23 scoped_ptr<ExternalDataFetcher> CreateExternalDataFetcher() const; |
24 }; | 24 }; |
25 | 25 |
26 scoped_ptr<ExternalDataFetcher> | 26 scoped_ptr<ExternalDataFetcher> |
27 PolicyDomainDescriptorTest::CreateExternalDataFetcher() const { | 27 PolicyDomainDescriptorTest::CreateExternalDataFetcher() const { |
28 return make_scoped_ptr( | 28 return make_scoped_ptr( |
29 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(), | 29 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(), |
30 std::string())); | 30 std::string())); |
31 } | 31 } |
32 | 32 |
33 TEST_F(PolicyDomainDescriptorTest, FilterBundle) { | 33 TEST_F(PolicyDomainDescriptorTest, FilterBundle) { |
34 scoped_refptr<PolicyDomainDescriptor> descriptor = | 34 scoped_refptr<PolicyDomainDescriptor> descriptor = |
35 new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS); | 35 new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS); |
36 EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain()); | 36 EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain()); |
37 EXPECT_TRUE(descriptor->components().empty()); | 37 EXPECT_TRUE(descriptor->components().empty()); |
38 | 38 |
39 std::string error; | 39 std::string error; |
40 scoped_ptr<PolicySchema> schema = PolicySchema::Parse( | 40 scoped_ptr<SchemaOwner> schema = SchemaOwner::Parse( |
41 "{" | 41 "{" |
42 " \"$schema\":\"http://json-schema.org/draft-03/schema#\"," | |
43 " \"type\":\"object\"," | 42 " \"type\":\"object\"," |
44 " \"properties\": {" | 43 " \"properties\": {" |
45 " \"Array\": {" | 44 " \"Array\": {" |
46 " \"type\": \"array\"," | 45 " \"type\": \"array\"," |
47 " \"items\": { \"type\": \"string\" }" | 46 " \"items\": { \"type\": \"string\" }" |
48 " }," | 47 " }," |
49 " \"Boolean\": { \"type\": \"boolean\" }," | 48 " \"Boolean\": { \"type\": \"boolean\" }," |
50 " \"Integer\": { \"type\": \"integer\" }," | 49 " \"Integer\": { \"type\": \"integer\" }," |
51 " \"Null\": { \"type\": \"null\" }," | 50 " \"Null\": { \"type\": \"null\" }," |
52 " \"Number\": { \"type\": \"number\" }," | 51 " \"Number\": { \"type\": \"number\" }," |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 base::Value::CreateBooleanValue(false), NULL); | 139 base::Value::CreateBooleanValue(false), NULL); |
141 badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 140 badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
142 base::Value::CreateBooleanValue(false), NULL); | 141 base::Value::CreateBooleanValue(false), NULL); |
143 badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 142 badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
144 NULL, CreateExternalDataFetcher().release()); | 143 NULL, CreateExternalDataFetcher().release()); |
145 | 144 |
146 descriptor->FilterBundle(&bundle); | 145 descriptor->FilterBundle(&bundle); |
147 EXPECT_TRUE(bundle.Equals(empty_bundle)); | 146 EXPECT_TRUE(bundle.Equals(empty_bundle)); |
148 } | 147 } |
149 | 148 |
149 TEST_F(PolicyDomainDescriptorTest, LegacyComponents) { | |
150 scoped_refptr<PolicyDomainDescriptor> descriptor = | |
151 new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS); | |
152 EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain()); | |
153 EXPECT_TRUE(descriptor->components().empty()); | |
154 | |
155 std::string error; | |
156 scoped_ptr<SchemaOwner> schema = SchemaOwner::Parse( | |
157 "{" | |
158 " \"type\":\"object\"," | |
159 " \"properties\": {" | |
160 " \"String\": { \"type\": \"string\" }" | |
161 " }" | |
162 "}", &error); | |
163 ASSERT_TRUE(schema) << error; | |
164 | |
165 descriptor->RegisterComponent("with-schema", schema.Pass()); | |
166 descriptor->RegisterComponent("without-schema", scoped_ptr<SchemaOwner>()); | |
167 | |
168 EXPECT_EQ(2u, descriptor->components().size()); | |
169 | |
170 // |bundle| contains policies loaded by a policy provider. | |
171 PolicyBundle bundle; | |
172 | |
173 // Known components with schemas are filtered. | |
174 PolicyNamespace extension_ns(POLICY_DOMAIN_EXTENSIONS, "with-schema"); | |
175 bundle.Get(extension_ns).Set("String", | |
176 POLICY_LEVEL_MANDATORY, | |
177 POLICY_SCOPE_USER, | |
178 base::Value::CreateStringValue("value 1"), | |
179 NULL); | |
180 | |
181 // Known components without a schema are not filtered. | |
182 PolicyNamespace without_schema_ns(POLICY_DOMAIN_EXTENSIONS, "without-schema"); | |
183 bundle.Get(without_schema_ns).Set("Schemaless", | |
184 POLICY_LEVEL_MANDATORY, | |
185 POLICY_SCOPE_USER, | |
186 base::Value::CreateStringValue("value 2"), | |
187 NULL); | |
188 | |
189 // Other namespaces aren't filtered. | |
190 PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); | |
191 bundle.Get(chrome_ns).Set("ChromePolicy", | |
192 POLICY_LEVEL_MANDATORY, | |
193 POLICY_SCOPE_USER, | |
194 base::Value::CreateStringValue("value 3"), | |
195 NULL); | |
196 | |
197 PolicyBundle expected_bundle; | |
198 expected_bundle.MergeFrom(bundle); | |
199 | |
200 // Unknown policies of known components with a schema are removed. | |
201 bundle.Get(extension_ns).Set("Surprise", | |
202 POLICY_LEVEL_MANDATORY, | |
203 POLICY_SCOPE_USER, | |
204 base::Value::CreateStringValue("value 4"), | |
205 NULL); | |
206 | |
207 // Unknown components are removed. | |
208 PolicyNamespace unknown_ns(POLICY_DOMAIN_EXTENSIONS, "unknown"); | |
209 bundle.Get(unknown_ns).Set("Surprise", | |
210 POLICY_LEVEL_MANDATORY, | |
211 POLICY_SCOPE_USER, | |
212 base::Value::CreateStringValue("value 5"), | |
213 NULL); | |
214 | |
215 descriptor->FilterBundle(&bundle); | |
216 EXPECT_TRUE(bundle.Equals(expected_bundle)); | |
217 } | |
218 | |
150 } // namespace policy | 219 } // namespace policy |
OLD | NEW |