OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 "base/scoped_ptr.h" | |
6 #include "base/string16.h" | |
7 #include "base/utf_string_conversions.h" | |
8 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
9 #include "chrome/browser/policy/configuration_policy_reader.h" | |
10 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | |
11 #include "policy/policy_constants.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace policy { | |
15 | |
16 class ConfigurationPolicyReaderTest : public testing::Test { | |
17 protected: | |
18 ConfigurationPolicyReaderTest() : provider_() { | |
19 managed_reader_.reset(new ConfigurationPolicyReader(&provider_, | |
20 PolicyStatusInfo::MANDATORY)); | |
21 recommended_reader_.reset(new ConfigurationPolicyReader(&provider_, | |
22 PolicyStatusInfo::RECOMMENDED)); | |
23 status_ok_ = ASCIIToUTF16("ok"); | |
24 } | |
25 | |
26 DictionaryValue* CreateDictionary(const char* policy_name) { | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
How about accepting an additional parameter to set
simo
2011/08/25 13:29:47
Done.
| |
27 DictionaryValue* dict = new DictionaryValue(); | |
28 dict->SetString( | |
29 PolicyStatusInfo::name_dict_path, ASCIIToUTF16(policy_name)); | |
30 dict->SetString(PolicyStatusInfo::level_dict_path, | |
31 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); | |
32 dict->SetString(PolicyStatusInfo::source_type_dict_path, | |
33 PolicyStatusInfo::GetSourceTypeString(PolicyStatusInfo::USER)); | |
34 dict->SetBoolean(PolicyStatusInfo::set_dict_path, true); | |
35 dict->SetString(PolicyStatusInfo::status_dict_path, status_ok_); | |
36 | |
37 return dict; | |
38 } | |
39 | |
40 MockConfigurationPolicyProvider provider_; | |
41 scoped_ptr<ConfigurationPolicyReader> managed_reader_; | |
42 scoped_ptr<ConfigurationPolicyReader> recommended_reader_; | |
43 string16 status_ok_; | |
44 }; | |
45 | |
46 TEST_F(ConfigurationPolicyReaderTest, GetDefault) { | |
47 EXPECT_EQ(NULL, managed_reader_->GetPolicyStatus(kPolicyHomepageLocation)); | |
48 } | |
49 | |
50 // Test for list-valued policy settings. | |
51 TEST_F(ConfigurationPolicyReaderTest, SetListValue) { | |
52 ListValue* in_value = new ListValue(); | |
53 in_value->Append(Value::CreateStringValue("test1")); | |
54 in_value->Append(Value::CreateStringValue("test2")); | |
55 provider_.AddPolicy(kPolicyRestoreOnStartupURLs, in_value); | |
56 managed_reader_->OnUpdatePolicy(); | |
57 | |
58 scoped_ptr<DictionaryValue> | |
59 dict(CreateDictionary(key::kRestoreOnStartupURLs)); | |
60 dict->Set(PolicyStatusInfo::value_dict_path, in_value->DeepCopy()); | |
61 EXPECT_TRUE( | |
62 dict->Equals(managed_reader_-> | |
63 GetPolicyStatus(kPolicyRestoreOnStartupURLs))); | |
64 | |
65 recommended_reader_->OnUpdatePolicy(); | |
66 dict->SetString("level", | |
67 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
68 EXPECT_TRUE(dict->Equals( | |
69 recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartupURLs))); | |
70 } | |
71 | |
72 // Test for string-valued policy settings. | |
73 TEST_F(ConfigurationPolicyReaderTest, SetStringValue) { | |
74 provider_.AddPolicy(kPolicyHomepageLocation, | |
75 Value::CreateStringValue("http://chromium.org")); | |
76 managed_reader_->OnUpdatePolicy(); | |
77 scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kHomepageLocation)); | |
78 dict->Set(PolicyStatusInfo::value_dict_path, | |
79 Value::CreateStringValue("http://chromium.org")); | |
80 EXPECT_TRUE( | |
81 dict->Equals(managed_reader_->GetPolicyStatus(kPolicyHomepageLocation))); | |
82 | |
83 recommended_reader_->OnUpdatePolicy(); | |
84 dict->SetString("level", | |
85 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
86 EXPECT_TRUE(dict->Equals( | |
87 recommended_reader_->GetPolicyStatus(kPolicyHomepageLocation))); | |
88 } | |
89 | |
90 // Test for boolean-valued policy settings. | |
91 TEST_F(ConfigurationPolicyReaderTest, SetBooleanValue) { | |
92 provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(true)); | |
93 managed_reader_->OnUpdatePolicy(); | |
94 scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kShowHomeButton)); | |
95 dict->Set(PolicyStatusInfo::value_dict_path, Value::CreateBooleanValue(true)); | |
96 EXPECT_TRUE(dict->Equals( | |
97 managed_reader_->GetPolicyStatus(kPolicyShowHomeButton))); | |
98 | |
99 recommended_reader_->OnUpdatePolicy(); | |
100 dict->SetString("level", | |
101 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
102 EXPECT_TRUE(dict->Equals( | |
103 recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton))); | |
104 | |
105 provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(false)); | |
106 managed_reader_->OnUpdatePolicy(); | |
107 dict->Set( | |
108 PolicyStatusInfo::value_dict_path, Value::CreateBooleanValue(false)); | |
109 dict->SetString("level", | |
110 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY)); | |
111 EXPECT_TRUE( | |
112 dict->Equals(managed_reader_->GetPolicyStatus(kPolicyShowHomeButton))); | |
113 | |
114 recommended_reader_->OnUpdatePolicy(); | |
115 dict->SetString("level", | |
116 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
117 EXPECT_TRUE(dict->Equals( | |
118 recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton))); | |
119 } | |
120 | |
121 // Test for integer-valued policy settings. | |
122 TEST_F(ConfigurationPolicyReaderTest, SetValue) { | |
123 provider_.AddPolicy(kPolicyRestoreOnStartup, Value::CreateIntegerValue(3)); | |
124 managed_reader_->OnUpdatePolicy(); | |
125 scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kRestoreOnStartup)); | |
126 dict->Set(PolicyStatusInfo::value_dict_path, Value::CreateIntegerValue(3)); | |
127 | |
128 EXPECT_TRUE(dict->Equals( | |
129 managed_reader_->GetPolicyStatus(kPolicyRestoreOnStartup))); | |
130 | |
131 recommended_reader_->OnUpdatePolicy(); | |
132 dict->SetString("level", | |
133 PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED)); | |
134 EXPECT_TRUE(dict->Equals( | |
135 recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartup))); | |
136 } | |
137 | |
138 class PolicyStatusTest : public testing::Test { | |
139 protected: | |
140 typedef ConfigurationPolicyProvider::PolicyDefinitionList | |
141 PolicyDefinitionList; | |
142 | |
143 PolicyStatusTest() | |
144 : managed_platform_provider_(), | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
4 spaces of indentation
simo
2011/08/25 13:29:47
Done.
| |
145 managed_cloud_provider_(), | |
146 recommended_platform_provider_(), | |
147 recommended_cloud_provider_() { | |
148 managed_platform_ = | |
149 new ConfigurationPolicyReader(&managed_platform_provider_, | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
Instead of actual ConfigurationPolicyReaders, can
simo
2011/08/25 13:29:47
Done.
| |
150 PolicyStatusInfo::MANDATORY); | |
151 managed_cloud_ = | |
152 new ConfigurationPolicyReader(&managed_cloud_provider_, | |
153 PolicyStatusInfo::MANDATORY); | |
154 recommended_platform_ = | |
155 new ConfigurationPolicyReader(&recommended_platform_provider_, | |
156 PolicyStatusInfo::RECOMMENDED); | |
157 recommended_cloud_ = | |
158 new ConfigurationPolicyReader(&recommended_cloud_provider_, | |
159 PolicyStatusInfo::RECOMMENDED); | |
160 | |
161 policy_status_.reset( new PolicyStatus(managed_platform_, | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
no space after (
simo
2011/08/25 13:29:47
Done.
| |
162 managed_cloud_, | |
163 recommended_platform_, | |
164 recommended_cloud_)); | |
165 policy_list_ = | |
166 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); | |
167 status_ok_ = ASCIIToUTF16("ok"); | |
168 } | |
169 | |
170 void UpdatePolicyReaders() { | |
171 managed_platform_->OnUpdatePolicy(); | |
172 managed_cloud_->OnUpdatePolicy(); | |
173 recommended_platform_->OnUpdatePolicy(); | |
174 recommended_cloud_->OnUpdatePolicy(); | |
175 } | |
176 | |
177 void SetDictionaryPaths(DictionaryValue* dict, | |
178 const char* policy_name, | |
179 bool defined, | |
180 PolicyStatusInfo::PolicyLevel level) { | |
181 dict->SetString(PolicyStatusInfo::name_dict_path, | |
182 ASCIIToUTF16(policy_name)); | |
183 if (defined) | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
need braces here (multi-line conditional block)
simo
2011/08/25 13:29:47
Done.
| |
184 dict->SetString(PolicyStatusInfo::level_dict_path, | |
185 PolicyStatusInfo::GetPolicyLevelString(level)); | |
186 } | |
187 | |
188 MockConfigurationPolicyProvider managed_platform_provider_; | |
189 MockConfigurationPolicyProvider managed_cloud_provider_; | |
190 MockConfigurationPolicyProvider recommended_platform_provider_; | |
191 MockConfigurationPolicyProvider recommended_cloud_provider_; | |
192 ConfigurationPolicyReader* managed_platform_; | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
I think you're leaking these, you might want to wr
simo
2011/08/25 13:29:47
They are passed in the the PolicyStatus object in
| |
193 ConfigurationPolicyReader* managed_cloud_; | |
194 ConfigurationPolicyReader* recommended_platform_; | |
195 ConfigurationPolicyReader* recommended_cloud_; | |
196 scoped_ptr<PolicyStatus> policy_status_; | |
197 const PolicyDefinitionList* policy_list_; | |
198 string16 status_ok_; | |
199 }; | |
200 | |
201 TEST_F(PolicyStatusTest, GetPolicyStatusList) { | |
202 bool any_policies_sent; | |
203 scoped_ptr<ListValue> status_list( | |
204 policy_status_->GetPolicyStatusList(&any_policies_sent)); | |
205 | |
206 EXPECT_FALSE(any_policies_sent); | |
207 | |
208 size_t policy_list_size = | |
209 static_cast<size_t>(policy_list_->end - policy_list_->begin); | |
210 | |
211 EXPECT_EQ(policy_list_size, status_list->GetSize()); | |
212 | |
213 managed_platform_provider_.AddPolicy(kPolicyInstantEnabled, | |
214 Value::CreateBooleanValue(true)); | |
215 managed_cloud_provider_.AddPolicy(kPolicyDisablePluginFinder, | |
216 Value::CreateBooleanValue(true)); | |
217 recommended_platform_provider_.AddPolicy(kPolicySyncDisabled, | |
218 Value::CreateBooleanValue(true)); | |
219 recommended_cloud_provider_.AddPolicy(kPolicyTranslateEnabled, | |
220 Value::CreateBooleanValue(true)); | |
221 UpdatePolicyReaders(); | |
222 | |
223 status_list.reset(policy_status_->GetPolicyStatusList(&any_policies_sent)); | |
224 EXPECT_TRUE(any_policies_sent); | |
225 EXPECT_EQ(policy_list_size, status_list->GetSize()); | |
226 | |
227 scoped_ptr<DictionaryValue> undefined_dict(new DictionaryValue()); | |
228 undefined_dict->SetString(PolicyStatusInfo::level_dict_path, | |
229 PolicyStatusInfo::GetPolicyLevelString( | |
230 PolicyStatusInfo::LEVEL_UNDEFINED)); | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
+4 spaces identation
simo
2011/08/25 13:29:47
Done.
| |
231 undefined_dict->SetString(PolicyStatusInfo::source_type_dict_path, | |
232 PolicyStatusInfo::GetSourceTypeString( | |
233 PolicyStatusInfo::SOURCE_TYPE_UNDEFINED)); | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
same here
simo
2011/08/25 13:29:47
Done.
| |
234 undefined_dict->Set(PolicyStatusInfo::value_dict_path, | |
235 Value::CreateNullValue()); | |
236 undefined_dict->SetBoolean(PolicyStatusInfo::set_dict_path, false); | |
237 undefined_dict->SetString(PolicyStatusInfo::status_dict_path, string16()); | |
238 | |
239 scoped_ptr<DictionaryValue> defined_dict(new DictionaryValue()); | |
240 defined_dict->SetString(PolicyStatusInfo::source_type_dict_path, | |
241 PolicyStatusInfo::GetSourceTypeString( | |
242 PolicyStatusInfo::USER)); | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
same here
simo
2011/08/25 13:29:47
Done.
| |
243 defined_dict->Set(PolicyStatusInfo::value_dict_path, | |
244 Value::CreateBooleanValue(true)); | |
245 defined_dict->SetBoolean(PolicyStatusInfo::set_dict_path, true); | |
246 defined_dict->SetString(PolicyStatusInfo::status_dict_path, status_ok_); | |
247 | |
248 for (const PolicyDefinitionList::Entry* entry = policy_list_->begin; | |
249 entry != policy_list_->end; ++entry) { | |
250 Value* status_dict = Value::CreateNullValue(); | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
Can just use NULL here.
simo
2011/08/25 13:29:47
Done.
| |
251 | |
252 // Every policy in policy_list_ has to appear in the returned ListValue. | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
it should be |policy_list_| (just convention for v
simo
2011/08/25 13:29:47
Done.
| |
253 string16 name = ASCIIToUTF16(entry->name); | |
254 for (ListValue::const_iterator status_entry = status_list->begin(); | |
255 status_entry != status_list->end(); | |
256 ++status_entry) { | |
257 string16 value; | |
258 ASSERT_TRUE((*status_entry)->IsType(Value::TYPE_DICTIONARY)); | |
259 DictionaryValue* dict = static_cast<DictionaryValue*>(*status_entry); | |
260 ASSERT_TRUE(dict->GetString(PolicyStatusInfo::name_dict_path, &value)); | |
261 | |
262 if (value == name) | |
263 status_dict = *status_entry; | |
264 } | |
265 | |
266 ASSERT_FALSE(status_dict->Equals(Value::CreateNullValue())); | |
Mattias Nissler (ping if slow)
2011/08/25 11:01:02
You leak the created null value.
| |
267 | |
268 switch (entry->policy_type) { | |
269 case kPolicyInstantEnabled: | |
270 SetDictionaryPaths(defined_dict.get(), | |
271 entry->name, | |
272 true, | |
273 PolicyStatusInfo::MANDATORY); | |
274 EXPECT_TRUE(defined_dict->Equals(status_dict)); | |
275 break; | |
276 case kPolicyDisablePluginFinder: | |
277 SetDictionaryPaths(defined_dict.get(), | |
278 entry->name, | |
279 true, | |
280 PolicyStatusInfo::MANDATORY); | |
281 EXPECT_TRUE(defined_dict->Equals(status_dict)); | |
282 break; | |
283 case kPolicySyncDisabled: | |
284 SetDictionaryPaths(defined_dict.get(), | |
285 entry->name, | |
286 true, | |
287 PolicyStatusInfo::RECOMMENDED); | |
288 EXPECT_TRUE(defined_dict->Equals(status_dict)); | |
289 break; | |
290 case kPolicyTranslateEnabled: | |
291 SetDictionaryPaths(defined_dict.get(), | |
292 entry->name, | |
293 true, | |
294 PolicyStatusInfo::RECOMMENDED); | |
295 EXPECT_TRUE(defined_dict->Equals(status_dict)); | |
296 break; | |
297 default: | |
298 SetDictionaryPaths(undefined_dict.get(), | |
299 entry->name, | |
300 false, | |
301 PolicyStatusInfo::LEVEL_UNDEFINED); | |
302 EXPECT_TRUE(undefined_dict->Equals(status_dict)); | |
303 break; | |
304 } | |
305 } | |
306 } | |
307 | |
308 TEST_F(PolicyStatusTest, GetPolicyName) { | |
309 for (const PolicyDefinitionList::Entry* entry = policy_list_->begin; | |
310 entry != policy_list_->end; ++entry) { | |
311 EXPECT_EQ(ASCIIToUTF16(entry->name), | |
312 PolicyStatus::GetPolicyName(entry->policy_type)); | |
313 } | |
314 } | |
315 | |
316 } // namespace policy | |
OLD | NEW |