OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/json/json_string_value_serializer.h" | 7 #include "base/json/json_string_value_serializer.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 virtual void InstallIntegerPolicy(const std::string& policy_name, | 38 virtual void InstallIntegerPolicy(const std::string& policy_name, |
39 int policy_value) OVERRIDE; | 39 int policy_value) OVERRIDE; |
40 virtual void InstallBooleanPolicy(const std::string& policy_name, | 40 virtual void InstallBooleanPolicy(const std::string& policy_name, |
41 bool policy_value) OVERRIDE; | 41 bool policy_value) OVERRIDE; |
42 virtual void InstallStringListPolicy( | 42 virtual void InstallStringListPolicy( |
43 const std::string& policy_name, | 43 const std::string& policy_name, |
44 const base::ListValue* policy_value) OVERRIDE; | 44 const base::ListValue* policy_value) OVERRIDE; |
45 virtual void InstallDictionaryPolicy( | 45 virtual void InstallDictionaryPolicy( |
46 const std::string& policy_name, | 46 const std::string& policy_name, |
47 const base::DictionaryValue* policy_value) OVERRIDE; | 47 const base::DictionaryValue* policy_value) OVERRIDE; |
| 48 virtual void Install3rdPartyPolicy( |
| 49 const base::DictionaryValue* policies) OVERRIDE; |
48 | 50 |
49 const FilePath& test_dir() { return test_dir_.path(); } | 51 const FilePath& test_dir() { return test_dir_.path(); } |
50 | 52 |
51 // JSON-encode a dictionary and write it to a file. | 53 // JSON-encode a dictionary and write it to a file. |
52 void WriteConfigFile(const base::DictionaryValue& dict, | 54 void WriteConfigFile(const base::DictionaryValue& dict, |
53 const std::string& file_name); | 55 const std::string& file_name); |
54 | 56 |
| 57 // Returns a unique name for a policy file. Each subsequent call returns a new |
| 58 // name that comes lexicographically after the previous one. |
| 59 std::string NextConfigFileName(); |
| 60 |
55 static PolicyProviderTestHarness* Create(); | 61 static PolicyProviderTestHarness* Create(); |
56 | 62 |
57 private: | 63 private: |
58 ScopedTempDir test_dir_; | 64 ScopedTempDir test_dir_; |
| 65 int next_policy_file_index_; |
59 | 66 |
60 DISALLOW_COPY_AND_ASSIGN(TestHarness); | 67 DISALLOW_COPY_AND_ASSIGN(TestHarness); |
61 }; | 68 }; |
62 | 69 |
63 TestHarness::TestHarness() | 70 TestHarness::TestHarness() |
64 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE) {} | 71 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE), |
| 72 next_policy_file_index_(100) {} |
65 | 73 |
66 TestHarness::~TestHarness() {} | 74 TestHarness::~TestHarness() {} |
67 | 75 |
68 void TestHarness::SetUp() { | 76 void TestHarness::SetUp() { |
69 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); | 77 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); |
70 } | 78 } |
71 | 79 |
72 ConfigurationPolicyProvider* TestHarness::CreateProvider( | 80 ConfigurationPolicyProvider* TestHarness::CreateProvider( |
73 const PolicyDefinitionList* policy_definition_list) { | 81 const PolicyDefinitionList* policy_definition_list) { |
74 scoped_ptr<AsyncPolicyLoader> loader( | 82 scoped_ptr<AsyncPolicyLoader> loader( |
75 new ConfigDirPolicyLoader(test_dir(), POLICY_SCOPE_MACHINE)); | 83 new ConfigDirPolicyLoader(test_dir(), POLICY_SCOPE_MACHINE)); |
76 return new AsyncPolicyProvider(loader.Pass()); | 84 return new AsyncPolicyProvider(loader.Pass()); |
77 } | 85 } |
78 | 86 |
79 void TestHarness::InstallEmptyPolicy() { | 87 void TestHarness::InstallEmptyPolicy() { |
80 base::DictionaryValue dict; | 88 base::DictionaryValue dict; |
81 WriteConfigFile(dict, "policy"); | 89 WriteConfigFile(dict, NextConfigFileName()); |
82 } | 90 } |
83 | 91 |
84 void TestHarness::InstallStringPolicy(const std::string& policy_name, | 92 void TestHarness::InstallStringPolicy(const std::string& policy_name, |
85 const std::string& policy_value) { | 93 const std::string& policy_value) { |
86 base::DictionaryValue dict; | 94 base::DictionaryValue dict; |
87 dict.SetString(policy_name, policy_value); | 95 dict.SetString(policy_name, policy_value); |
88 WriteConfigFile(dict, "policy"); | 96 WriteConfigFile(dict, NextConfigFileName()); |
89 } | 97 } |
90 | 98 |
91 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, | 99 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, |
92 int policy_value) { | 100 int policy_value) { |
93 base::DictionaryValue dict; | 101 base::DictionaryValue dict; |
94 dict.SetInteger(policy_name, policy_value); | 102 dict.SetInteger(policy_name, policy_value); |
95 WriteConfigFile(dict, "policy"); | 103 WriteConfigFile(dict, NextConfigFileName()); |
96 } | 104 } |
97 | 105 |
98 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, | 106 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, |
99 bool policy_value) { | 107 bool policy_value) { |
100 base::DictionaryValue dict; | 108 base::DictionaryValue dict; |
101 dict.SetBoolean(policy_name, policy_value); | 109 dict.SetBoolean(policy_name, policy_value); |
102 WriteConfigFile(dict, "policy"); | 110 WriteConfigFile(dict, NextConfigFileName()); |
103 } | 111 } |
104 | 112 |
105 void TestHarness::InstallStringListPolicy(const std::string& policy_name, | 113 void TestHarness::InstallStringListPolicy(const std::string& policy_name, |
106 const base::ListValue* policy_value) { | 114 const base::ListValue* policy_value) { |
107 base::DictionaryValue dict; | 115 base::DictionaryValue dict; |
108 dict.Set(policy_name, policy_value->DeepCopy()); | 116 dict.Set(policy_name, policy_value->DeepCopy()); |
109 WriteConfigFile(dict, "policy"); | 117 WriteConfigFile(dict, NextConfigFileName()); |
110 } | 118 } |
111 | 119 |
112 void TestHarness::InstallDictionaryPolicy( | 120 void TestHarness::InstallDictionaryPolicy( |
113 const std::string& policy_name, | 121 const std::string& policy_name, |
114 const base::DictionaryValue* policy_value) { | 122 const base::DictionaryValue* policy_value) { |
115 base::DictionaryValue dict; | 123 base::DictionaryValue dict; |
116 dict.Set(policy_name, policy_value->DeepCopy()); | 124 dict.Set(policy_name, policy_value->DeepCopy()); |
117 WriteConfigFile(dict, "policy"); | 125 WriteConfigFile(dict, NextConfigFileName()); |
| 126 } |
| 127 |
| 128 void TestHarness::Install3rdPartyPolicy(const base::DictionaryValue* policies) { |
| 129 base::DictionaryValue dict; |
| 130 dict.Set("3rdparty", policies->DeepCopy()); |
| 131 WriteConfigFile(dict, NextConfigFileName()); |
118 } | 132 } |
119 | 133 |
120 void TestHarness::WriteConfigFile(const base::DictionaryValue& dict, | 134 void TestHarness::WriteConfigFile(const base::DictionaryValue& dict, |
121 const std::string& file_name) { | 135 const std::string& file_name) { |
122 std::string data; | 136 std::string data; |
123 JSONStringValueSerializer serializer(&data); | 137 JSONStringValueSerializer serializer(&data); |
124 serializer.Serialize(dict); | 138 serializer.Serialize(dict); |
125 const FilePath mandatory_dir(test_dir().Append(kMandatoryPath)); | 139 const FilePath mandatory_dir(test_dir().Append(kMandatoryPath)); |
126 ASSERT_TRUE(file_util::CreateDirectory(mandatory_dir)); | 140 ASSERT_TRUE(file_util::CreateDirectory(mandatory_dir)); |
127 const FilePath file_path(mandatory_dir.AppendASCII(file_name)); | 141 const FilePath file_path(mandatory_dir.AppendASCII(file_name)); |
128 ASSERT_EQ((int) data.size(), | 142 ASSERT_EQ((int) data.size(), |
129 file_util::WriteFile(file_path, data.c_str(), data.size())); | 143 file_util::WriteFile(file_path, data.c_str(), data.size())); |
130 } | 144 } |
131 | 145 |
| 146 std::string TestHarness::NextConfigFileName() { |
| 147 EXPECT_LE(next_policy_file_index_, 999); |
| 148 return std::string("policy") + base::IntToString(next_policy_file_index_++); |
| 149 } |
| 150 |
132 // static | 151 // static |
133 PolicyProviderTestHarness* TestHarness::Create() { | 152 PolicyProviderTestHarness* TestHarness::Create() { |
134 return new TestHarness(); | 153 return new TestHarness(); |
135 } | 154 } |
136 | 155 |
137 } // namespace | 156 } // namespace |
138 | 157 |
139 // Instantiate abstract test case for basic policy reading tests. | 158 // Instantiate abstract test case for basic policy reading tests. |
140 INSTANTIATE_TEST_CASE_P( | 159 INSTANTIATE_TEST_CASE_P( |
141 ConfigDirPolicyLoaderTest, | 160 ConfigDirPolicyLoaderTest, |
142 ConfigurationPolicyProviderTest, | 161 ConfigurationPolicyProviderTest, |
143 testing::Values(TestHarness::Create)); | 162 testing::Values(TestHarness::Create)); |
144 | 163 |
| 164 // Instantiate abstract test case for 3rd party policy reading tests. |
| 165 INSTANTIATE_TEST_CASE_P( |
| 166 ConfigDir3rdPartyPolicyLoaderTest, |
| 167 Configuration3rdPartyPolicyProviderTest, |
| 168 testing::Values(TestHarness::Create)); |
| 169 |
145 // Some tests that exercise special functionality in ConfigDirPolicyLoader. | 170 // Some tests that exercise special functionality in ConfigDirPolicyLoader. |
146 class ConfigDirPolicyLoaderTest : public PolicyTestBase { | 171 class ConfigDirPolicyLoaderTest : public PolicyTestBase { |
147 protected: | 172 protected: |
148 void SetUp() OVERRIDE { | 173 void SetUp() OVERRIDE { |
149 PolicyTestBase::SetUp(); | 174 PolicyTestBase::SetUp(); |
150 harness_.SetUp(); | 175 harness_.SetUp(); |
151 } | 176 } |
152 | 177 |
153 TestHarness harness_; | 178 TestHarness harness_; |
154 }; | 179 }; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 218 |
194 ConfigDirPolicyLoader loader(harness_.test_dir(), POLICY_SCOPE_USER); | 219 ConfigDirPolicyLoader loader(harness_.test_dir(), POLICY_SCOPE_USER); |
195 scoped_ptr<PolicyBundle> bundle(loader.Load()); | 220 scoped_ptr<PolicyBundle> bundle(loader.Load()); |
196 ASSERT_TRUE(bundle.get()); | 221 ASSERT_TRUE(bundle.get()); |
197 PolicyBundle expected_bundle; | 222 PolicyBundle expected_bundle; |
198 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") | 223 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") |
199 .LoadFrom(&test_dict_foo, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 224 .LoadFrom(&test_dict_foo, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
200 EXPECT_TRUE(bundle->Equals(expected_bundle)); | 225 EXPECT_TRUE(bundle->Equals(expected_bundle)); |
201 } | 226 } |
202 | 227 |
203 // Tests loading of policy for 3rd parties. | |
204 TEST_F(ConfigDirPolicyLoaderTest, Load3rdParty) { | |
205 base::DictionaryValue policy_dict; | |
206 policy_dict.SetBoolean("bool", true); | |
207 policy_dict.SetString("str", "string value"); | |
208 policy_dict.SetDouble("double", 123.456); | |
209 policy_dict.SetInteger("int", 789); | |
210 | |
211 base::ListValue* list = new base::ListValue(); | |
212 for (int i = 0; i < 5; ++i) { | |
213 base::DictionaryValue* dict = new base::DictionaryValue(); | |
214 dict->SetInteger("subdictindex", i); | |
215 dict->Set("subdict", policy_dict.DeepCopy()); | |
216 list->Append(dict); | |
217 } | |
218 policy_dict.Set("list", list); | |
219 | |
220 base::DictionaryValue json_dict; | |
221 // Merge |policy_dict|, which will become the chrome policies. | |
222 json_dict.MergeDictionary(&policy_dict); | |
223 json_dict.Set("3rdparty.extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | |
224 policy_dict.DeepCopy()); | |
225 json_dict.Set("3rdparty.extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", | |
226 policy_dict.DeepCopy()); | |
227 | |
228 harness_.WriteConfigFile(json_dict, "policy.json"); | |
229 ConfigDirPolicyLoader loader(harness_.test_dir(), POLICY_SCOPE_USER); | |
230 scoped_ptr<PolicyBundle> bundle(loader.Load()); | |
231 ASSERT_TRUE(bundle.get()); | |
232 PolicyMap expected_policy; | |
233 expected_policy.LoadFrom(&policy_dict, | |
234 POLICY_LEVEL_MANDATORY, | |
235 POLICY_SCOPE_USER); | |
236 PolicyBundle expected_bundle; | |
237 expected_bundle.Get(POLICY_DOMAIN_CHROME, "").CopyFrom(expected_policy); | |
238 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, | |
239 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") | |
240 .CopyFrom(expected_policy); | |
241 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, | |
242 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") | |
243 .CopyFrom(expected_policy); | |
244 EXPECT_TRUE(bundle->Equals(expected_bundle)); | |
245 } | |
246 | |
247 } // namespace policy | 228 } // namespace policy |
OLD | NEW |