Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: chrome/browser/policy/configuration_policy_provider_mac_unittest.cc

Issue 10545033: Removed the PolicyDefinitionList from the ConfigurationPolicyProvider interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <CoreFoundation/CoreFoundation.h>
6
7 #include "base/basictypes.h"
8 #include "base/mac/scoped_cftyperef.h"
9 #include "base/sys_string_conversions.h"
10 #include "base/values.h"
11 #include "chrome/browser/policy/asynchronous_policy_test_base.h"
12 #include "chrome/browser/policy/configuration_policy_provider_mac.h"
13 #include "chrome/browser/policy/configuration_policy_provider_test.h"
14 #include "chrome/browser/policy/policy_bundle.h"
15 #include "chrome/browser/policy/policy_map.h"
16 #include "chrome/browser/preferences_mock_mac.h"
17 #include "policy/policy_constants.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using base::mac::ScopedCFTypeRef;
21
22 namespace policy {
23
24 namespace {
25
26 // Converts a base::Value to the equivalent CFPropertyListRef.
27 // The returned value is owned by the caller.
28 CFPropertyListRef CreatePropertyFromValue(const base::Value* value) {
29 switch (value->GetType()) {
30 case base::Value::TYPE_NULL:
31 return kCFNull;
32
33 case base::Value::TYPE_BOOLEAN: {
34 bool bool_value;
35 if (value->GetAsBoolean(&bool_value))
36 return bool_value ? kCFBooleanTrue : kCFBooleanFalse;
37 break;
38 }
39
40 case base::Value::TYPE_INTEGER: {
41 int int_value;
42 if (value->GetAsInteger(&int_value)) {
43 return CFNumberCreate(
44 kCFAllocatorDefault, kCFNumberIntType, &int_value);
45 }
46 break;
47 }
48
49 case base::Value::TYPE_DOUBLE: {
50 double double_value;
51 if (value->GetAsDouble(&double_value)) {
52 return CFNumberCreate(
53 kCFAllocatorDefault, kCFNumberDoubleType, &double_value);
54 }
55 break;
56 }
57
58 case base::Value::TYPE_STRING: {
59 std::string string_value;
60 if (value->GetAsString(&string_value))
61 return base::SysUTF8ToCFStringRef(string_value);
62 break;
63 }
64
65 case base::Value::TYPE_DICTIONARY: {
66 const base::DictionaryValue* dict_value;
67 if (value->GetAsDictionary(&dict_value)) {
68 // |dict| is owned by the caller.
69 CFMutableDictionaryRef dict =
70 CFDictionaryCreateMutable(kCFAllocatorDefault,
71 dict_value->size(),
72 &kCFTypeDictionaryKeyCallBacks,
73 &kCFTypeDictionaryValueCallBacks);
74 for (base::DictionaryValue::Iterator iterator(*dict_value);
75 iterator.HasNext(); iterator.Advance()) {
76 // CFDictionaryAddValue() retains both |key| and |value|, so make sure
77 // the references are balanced.
78 ScopedCFTypeRef<CFStringRef> key(
79 base::SysUTF8ToCFStringRef(iterator.key()));
80 ScopedCFTypeRef<CFPropertyListRef> cf_value(
81 CreatePropertyFromValue(&iterator.value()));
82 if (cf_value)
83 CFDictionaryAddValue(dict, key, cf_value);
84 }
85 return dict;
86 }
87 break;
88 }
89
90 case base::Value::TYPE_LIST: {
91 const base::ListValue* list;
92 if (value->GetAsList(&list)) {
93 CFMutableArrayRef array =
94 CFArrayCreateMutable(NULL, list->GetSize(), &kCFTypeArrayCallBacks);
95 for (base::ListValue::const_iterator it(list->begin());
96 it != list->end(); ++it) {
97 // CFArrayAppendValue() retains |value|, so make sure the reference
98 // created by CreatePropertyFromValue() is released.
99 ScopedCFTypeRef<CFPropertyListRef> cf_value(
100 CreatePropertyFromValue(*it));
101 if (cf_value)
102 CFArrayAppendValue(array, cf_value);
103 }
104 return array;
105 }
106 break;
107 }
108
109 case base::Value::TYPE_BINARY:
110 // This type isn't converted (though it can be represented as CFData)
111 // because there's no equivalent JSON type, and policy values can only
112 // take valid JSON values.
113 break;
114 }
115
116 return NULL;
117 }
118
119 class TestHarness : public PolicyProviderTestHarness {
120 public:
121 TestHarness();
122 virtual ~TestHarness();
123
124 virtual void SetUp() OVERRIDE;
125
126 virtual ConfigurationPolicyProvider* CreateProvider(
127 const PolicyDefinitionList* policy_definition_list) OVERRIDE;
128
129 virtual void InstallEmptyPolicy() OVERRIDE;
130 virtual void InstallStringPolicy(const std::string& policy_name,
131 const std::string& policy_value) OVERRIDE;
132 virtual void InstallIntegerPolicy(const std::string& policy_name,
133 int policy_value) OVERRIDE;
134 virtual void InstallBooleanPolicy(const std::string& policy_name,
135 bool policy_value) OVERRIDE;
136 virtual void InstallStringListPolicy(
137 const std::string& policy_name,
138 const base::ListValue* policy_value) OVERRIDE;
139 virtual void InstallDictionaryPolicy(
140 const std::string& policy_name,
141 const base::DictionaryValue* policy_value) OVERRIDE;
142
143 static PolicyProviderTestHarness* Create();
144
145 private:
146 MockPreferences* prefs_;
147
148 DISALLOW_COPY_AND_ASSIGN(TestHarness);
149 };
150
151 TestHarness::TestHarness()
152 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER) {}
153
154 TestHarness::~TestHarness() {}
155
156 void TestHarness::SetUp() {}
157
158 ConfigurationPolicyProvider* TestHarness::CreateProvider(
159 const PolicyDefinitionList* policy_definition_list) {
160 prefs_ = new MockPreferences();
161 return new ConfigurationPolicyProviderMac(policy_definition_list, prefs_);
162 }
163
164 void TestHarness::InstallEmptyPolicy() {}
165
166 void TestHarness::InstallStringPolicy(const std::string& policy_name,
167 const std::string& policy_value) {
168 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
169 ScopedCFTypeRef<CFStringRef> value(base::SysUTF8ToCFStringRef(policy_value));
170 prefs_->AddTestItem(name, value, true);
171 }
172
173 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
174 int policy_value) {
175 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
176 ScopedCFTypeRef<CFNumberRef> value(
177 CFNumberCreate(NULL, kCFNumberIntType, &policy_value));
178 prefs_->AddTestItem(name, value, true);
179 }
180
181 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
182 bool policy_value) {
183 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
184 prefs_->AddTestItem(name,
185 policy_value ? kCFBooleanTrue : kCFBooleanFalse,
186 true);
187 }
188
189 void TestHarness::InstallStringListPolicy(const std::string& policy_name,
190 const base::ListValue* policy_value) {
191 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
192 ScopedCFTypeRef<CFPropertyListRef> array(
193 CreatePropertyFromValue(policy_value));
194 ASSERT_TRUE(array);
195 prefs_->AddTestItem(name, array, true);
196 }
197
198 void TestHarness::InstallDictionaryPolicy(
199 const std::string& policy_name,
200 const base::DictionaryValue* policy_value) {
201 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
202 ScopedCFTypeRef<CFPropertyListRef> dict(
203 CreatePropertyFromValue(policy_value));
204 ASSERT_TRUE(dict);
205 prefs_->AddTestItem(name, dict, true);
206 }
207
208 // static
209 PolicyProviderTestHarness* TestHarness::Create() {
210 return new TestHarness();
211 }
212
213 } // namespace
214
215 // Instantiate abstract test case for basic policy reading tests.
216 INSTANTIATE_TEST_CASE_P(
217 ConfigurationPolicyProviderMacTest,
218 ConfigurationPolicyProviderTest,
219 testing::Values(TestHarness::Create));
220
221 // Special test cases for some mac preferences details.
222 class ConfigurationPolicyProviderMacTest : public AsynchronousPolicyTestBase {
223 protected:
224 ConfigurationPolicyProviderMacTest()
225 : prefs_(new MockPreferences()),
226 provider_(&test_policy_definitions::kList, prefs_) {}
227 virtual ~ConfigurationPolicyProviderMacTest() {}
228
229 MockPreferences* prefs_;
230 ConfigurationPolicyProviderMac provider_;
231 };
232
233 TEST_F(ConfigurationPolicyProviderMacTest, Invalid) {
234 ScopedCFTypeRef<CFStringRef> name(
235 base::SysUTF8ToCFStringRef(test_policy_definitions::kKeyString));
236 const char buffer[] = "binary \xde\xad\xbe\xef data";
237 ScopedCFTypeRef<CFDataRef> invalid_data(
238 CFDataCreate(kCFAllocatorDefault,
239 reinterpret_cast<const UInt8 *>(buffer),
240 arraysize(buffer)));
241 ASSERT_TRUE(invalid_data);
242 prefs_->AddTestItem(name, invalid_data.get(), true);
243 prefs_->AddTestItem(name, invalid_data.get(), false);
244
245 // Make the provider read the updated |prefs_|.
246 provider_.RefreshPolicies();
247 loop_.RunAllPending();
248 const PolicyBundle kEmptyBundle;
249 EXPECT_TRUE(provider_.policies().Equals(kEmptyBundle));
250 }
251
252 TEST_F(ConfigurationPolicyProviderMacTest, TestNonForcedValue) {
253 ScopedCFTypeRef<CFStringRef> name(
254 base::SysUTF8ToCFStringRef(test_policy_definitions::kKeyString));
255 ScopedCFTypeRef<CFPropertyListRef> test_value(
256 base::SysUTF8ToCFStringRef("string value"));
257 ASSERT_TRUE(test_value.get());
258 prefs_->AddTestItem(name, test_value.get(), false);
259
260 // Make the provider read the updated |prefs_|.
261 provider_.RefreshPolicies();
262 loop_.RunAllPending();
263 PolicyBundle expected_bundle;
264 expected_bundle.Get(POLICY_DOMAIN_CHROME, "")
265 .Set(test_policy_definitions::kKeyString, POLICY_LEVEL_RECOMMENDED,
266 POLICY_SCOPE_USER, base::Value::CreateStringValue("string value"));
267 EXPECT_TRUE(provider_.policies().Equals(expected_bundle));
268 }
269
270 TEST_F(ConfigurationPolicyProviderMacTest, TestConversions) {
271 base::DictionaryValue root;
272
273 // base::Value::TYPE_NULL
274 root.Set("null", base::Value::CreateNullValue());
275
276 // base::Value::TYPE_BOOLEAN
277 root.SetBoolean("false", false);
278 root.SetBoolean("true", true);
279
280 // base::Value::TYPE_INTEGER
281 root.SetInteger("int", 123);
282 root.SetInteger("zero", 0);
283
284 // base::Value::TYPE_DOUBLE
285 root.SetDouble("double", 123.456);
286 root.SetDouble("zerod", 0.0);
287
288 // base::Value::TYPE_STRING
289 root.SetString("string", "the fox jumps over something");
290 root.SetString("empty", "");
291
292 // base::Value::TYPE_LIST
293 base::ListValue list;
294 root.Set("emptyl", list.DeepCopy());
295 for (base::DictionaryValue::Iterator it(root); it.HasNext(); it.Advance())
296 list.Append(it.value().DeepCopy());
297 EXPECT_EQ(root.size(), list.GetSize());
298 list.Append(root.DeepCopy());
299 root.Set("list", list.DeepCopy());
300
301 // base::Value::TYPE_DICTIONARY
302 base::DictionaryValue dict;
303 root.Set("emptyd", dict.DeepCopy());
304 // Very meta.
305 root.Set("dict", root.DeepCopy());
306
307 ScopedCFTypeRef<CFPropertyListRef> property(CreatePropertyFromValue(&root));
308 ASSERT_TRUE(property);
309 scoped_ptr<base::Value> value(
310 MacPreferencesPolicyProviderDelegate::CreateValueFromProperty(property));
311 ASSERT_TRUE(value.get());
312
313 EXPECT_TRUE(root.Equals(value.get()));
314 }
315
316 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698