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" | |
17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
18 | 17 |
19 namespace policy { | 18 namespace policy { |
20 | 19 |
21 class PolicyDomainDescriptorTest : public testing::Test { | 20 class PolicyDomainDescriptorTest : public testing::Test { |
22 protected: | 21 protected: |
23 scoped_ptr<ExternalDataFetcher> CreateExternalDataFetcher() const; | 22 scoped_ptr<ExternalDataFetcher> CreateExternalDataFetcher() const; |
24 }; | 23 }; |
25 | 24 |
26 scoped_ptr<ExternalDataFetcher> | 25 scoped_ptr<ExternalDataFetcher> |
27 PolicyDomainDescriptorTest::CreateExternalDataFetcher() const { | 26 PolicyDomainDescriptorTest::CreateExternalDataFetcher() const { |
28 return make_scoped_ptr( | 27 return make_scoped_ptr( |
29 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(), | 28 new ExternalDataFetcher(base::WeakPtr<ExternalDataManager>(), |
30 std::string())); | 29 std::string())); |
31 } | 30 } |
32 | 31 |
33 TEST_F(PolicyDomainDescriptorTest, FilterBundle) { | 32 TEST_F(PolicyDomainDescriptorTest, FilterBundle) { |
34 scoped_refptr<PolicyDomainDescriptor> descriptor = | 33 scoped_refptr<PolicyDomainDescriptor> descriptor = |
35 new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS); | 34 new PolicyDomainDescriptor(POLICY_DOMAIN_EXTENSIONS); |
36 EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain()); | 35 EXPECT_EQ(POLICY_DOMAIN_EXTENSIONS, descriptor->domain()); |
37 EXPECT_TRUE(descriptor->components().empty()); | 36 EXPECT_TRUE(descriptor->components().empty()); |
38 | 37 |
39 std::string error; | 38 std::string error; |
40 scoped_ptr<PolicySchema> schema = PolicySchema::Parse( | 39 scoped_ptr<SchemaOwner> schema = SchemaOwner::Parse( |
41 "{" | 40 "{" |
42 " \"$schema\":\"http://json-schema.org/draft-03/schema#\"," | |
43 " \"type\":\"object\"," | 41 " \"type\":\"object\"," |
44 " \"properties\": {" | 42 " \"properties\": {" |
45 " \"Array\": {" | 43 " \"Array\": {" |
46 " \"type\": \"array\"," | 44 " \"type\": \"array\"," |
47 " \"items\": { \"type\": \"string\" }" | 45 " \"items\": { \"type\": \"string\" }" |
48 " }," | 46 " }," |
49 " \"Boolean\": { \"type\": \"boolean\" }," | 47 " \"Boolean\": { \"type\": \"boolean\" }," |
50 " \"Integer\": { \"type\": \"integer\" }," | 48 " \"Integer\": { \"type\": \"integer\" }," |
51 " \"Null\": { \"type\": \"null\" }," | 49 " \"Null\": { \"type\": \"null\" }," |
52 " \"Number\": { \"type\": \"number\" }," | 50 " \"Number\": { \"type\": \"number\" }," |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 base::Value::CreateBooleanValue(false), NULL); | 138 base::Value::CreateBooleanValue(false), NULL); |
141 badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 139 badmap.Set("Object", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
142 base::Value::CreateBooleanValue(false), NULL); | 140 base::Value::CreateBooleanValue(false), NULL); |
143 badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 141 badmap.Set("String", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
144 NULL, CreateExternalDataFetcher().release()); | 142 NULL, CreateExternalDataFetcher().release()); |
145 | 143 |
146 descriptor->FilterBundle(&bundle); | 144 descriptor->FilterBundle(&bundle); |
147 EXPECT_TRUE(bundle.Equals(empty_bundle)); | 145 EXPECT_TRUE(bundle.Equals(empty_bundle)); |
148 } | 146 } |
149 | 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 scoped_ptr<SchemaOwner> schema = SchemaOwner::Parse( |
| 156 "{" |
| 157 " \"type\":\"object\"," |
| 158 " \"properties\": {" |
| 159 " \"String\": { \"type\": \"string\" }" |
| 160 " }" |
| 161 "}", &error); |
| 162 ASSERT_TRUE(schema) << error; |
| 163 |
| 164 descriptor->RegisterComponent("with-schema", schema.Pass()); |
| 165 descriptor->RegisterComponent("without-schema", scoped_ptr<SchemaOwner>()); |
| 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 |
150 } // namespace policy | 218 } // namespace policy |
OLD | NEW |