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

Side by Side Diff: components/policy/core/common/policy_loader_mac_unittest.cc

Issue 179813008: Move PolicyLoaderMac::CreateValueFromProperty to a generic file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: POLICY_EXPORT Created 6 years, 9 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
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 <CoreFoundation/CoreFoundation.h> 5 #include <CoreFoundation/CoreFoundation.h>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/mac/scoped_cftyperef.h" 10 #include "base/mac/scoped_cftyperef.h"
11 #include "base/strings/sys_string_conversions.h" 11 #include "base/strings/sys_string_conversions.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "components/policy/core/common/async_policy_provider.h" 13 #include "components/policy/core/common/async_policy_provider.h"
14 #include "components/policy/core/common/configuration_policy_provider_test.h" 14 #include "components/policy/core/common/configuration_policy_provider_test.h"
15 #include "components/policy/core/common/external_data_fetcher.h" 15 #include "components/policy/core/common/external_data_fetcher.h"
16 #include "components/policy/core/common/policy_bundle.h" 16 #include "components/policy/core/common/policy_bundle.h"
17 #include "components/policy/core/common/policy_loader_mac.h" 17 #include "components/policy/core/common/policy_loader_mac.h"
18 #include "components/policy/core/common/policy_map.h" 18 #include "components/policy/core/common/policy_map.h"
19 #include "components/policy/core/common/policy_test_utils.h"
19 #include "components/policy/core/common/preferences_mock_mac.h" 20 #include "components/policy/core/common/preferences_mock_mac.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
22 using base::ScopedCFTypeRef; 23 using base::ScopedCFTypeRef;
23 24
24 namespace policy { 25 namespace policy {
25 26
26 namespace { 27 namespace {
27 28
28 // Converts a base::Value to the equivalent CFPropertyListRef.
29 // The returned value is owned by the caller.
30 CFPropertyListRef CreatePropertyFromValue(const base::Value* value) {
31 switch (value->GetType()) {
32 case base::Value::TYPE_NULL:
33 return kCFNull;
34
35 case base::Value::TYPE_BOOLEAN: {
36 bool bool_value;
37 if (value->GetAsBoolean(&bool_value))
38 return bool_value ? kCFBooleanTrue : kCFBooleanFalse;
39 break;
40 }
41
42 case base::Value::TYPE_INTEGER: {
43 int int_value;
44 if (value->GetAsInteger(&int_value)) {
45 return CFNumberCreate(
46 kCFAllocatorDefault, kCFNumberIntType, &int_value);
47 }
48 break;
49 }
50
51 case base::Value::TYPE_DOUBLE: {
52 double double_value;
53 if (value->GetAsDouble(&double_value)) {
54 return CFNumberCreate(
55 kCFAllocatorDefault, kCFNumberDoubleType, &double_value);
56 }
57 break;
58 }
59
60 case base::Value::TYPE_STRING: {
61 std::string string_value;
62 if (value->GetAsString(&string_value))
63 return base::SysUTF8ToCFStringRef(string_value);
64 break;
65 }
66
67 case base::Value::TYPE_DICTIONARY: {
68 const base::DictionaryValue* dict_value;
69 if (value->GetAsDictionary(&dict_value)) {
70 // |dict| is owned by the caller.
71 CFMutableDictionaryRef dict =
72 CFDictionaryCreateMutable(kCFAllocatorDefault,
73 dict_value->size(),
74 &kCFTypeDictionaryKeyCallBacks,
75 &kCFTypeDictionaryValueCallBacks);
76 for (base::DictionaryValue::Iterator iterator(*dict_value);
77 !iterator.IsAtEnd(); iterator.Advance()) {
78 // CFDictionaryAddValue() retains both |key| and |value|, so make sure
79 // the references are balanced.
80 ScopedCFTypeRef<CFStringRef> key(
81 base::SysUTF8ToCFStringRef(iterator.key()));
82 ScopedCFTypeRef<CFPropertyListRef> cf_value(
83 CreatePropertyFromValue(&iterator.value()));
84 if (cf_value)
85 CFDictionaryAddValue(dict, key, cf_value);
86 }
87 return dict;
88 }
89 break;
90 }
91
92 case base::Value::TYPE_LIST: {
93 const base::ListValue* list;
94 if (value->GetAsList(&list)) {
95 CFMutableArrayRef array =
96 CFArrayCreateMutable(NULL, list->GetSize(), &kCFTypeArrayCallBacks);
97 for (base::ListValue::const_iterator it(list->begin());
98 it != list->end(); ++it) {
99 // CFArrayAppendValue() retains |value|, so make sure the reference
100 // created by CreatePropertyFromValue() is released.
101 ScopedCFTypeRef<CFPropertyListRef> cf_value(
102 CreatePropertyFromValue(*it));
103 if (cf_value)
104 CFArrayAppendValue(array, cf_value);
105 }
106 return array;
107 }
108 break;
109 }
110
111 case base::Value::TYPE_BINARY:
112 // This type isn't converted (though it can be represented as CFData)
113 // because there's no equivalent JSON type, and policy values can only
114 // take valid JSON values.
115 break;
116 }
117
118 return NULL;
119 }
120
121 class TestHarness : public PolicyProviderTestHarness { 29 class TestHarness : public PolicyProviderTestHarness {
122 public: 30 public:
123 TestHarness(); 31 TestHarness();
124 virtual ~TestHarness(); 32 virtual ~TestHarness();
125 33
126 virtual void SetUp() OVERRIDE; 34 virtual void SetUp() OVERRIDE;
127 35
128 virtual ConfigurationPolicyProvider* CreateProvider( 36 virtual ConfigurationPolicyProvider* CreateProvider(
129 SchemaRegistry* registry, 37 SchemaRegistry* registry,
130 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE; 38 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 bool policy_value) { 96 bool policy_value) {
189 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); 97 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
190 prefs_->AddTestItem(name, 98 prefs_->AddTestItem(name,
191 policy_value ? kCFBooleanTrue : kCFBooleanFalse, 99 policy_value ? kCFBooleanTrue : kCFBooleanFalse,
192 true); 100 true);
193 } 101 }
194 102
195 void TestHarness::InstallStringListPolicy(const std::string& policy_name, 103 void TestHarness::InstallStringListPolicy(const std::string& policy_name,
196 const base::ListValue* policy_value) { 104 const base::ListValue* policy_value) {
197 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); 105 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
198 ScopedCFTypeRef<CFPropertyListRef> array( 106 ScopedCFTypeRef<CFPropertyListRef> array(ValueToProperty(policy_value));
199 CreatePropertyFromValue(policy_value));
200 ASSERT_TRUE(array); 107 ASSERT_TRUE(array);
201 prefs_->AddTestItem(name, array, true); 108 prefs_->AddTestItem(name, array, true);
202 } 109 }
203 110
204 void TestHarness::InstallDictionaryPolicy( 111 void TestHarness::InstallDictionaryPolicy(
205 const std::string& policy_name, 112 const std::string& policy_name,
206 const base::DictionaryValue* policy_value) { 113 const base::DictionaryValue* policy_value) {
207 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); 114 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
208 ScopedCFTypeRef<CFPropertyListRef> dict( 115 ScopedCFTypeRef<CFPropertyListRef> dict(ValueToProperty(policy_value));
209 CreatePropertyFromValue(policy_value));
210 ASSERT_TRUE(dict); 116 ASSERT_TRUE(dict);
211 prefs_->AddTestItem(name, dict, true); 117 prefs_->AddTestItem(name, dict, true);
212 } 118 }
213 119
214 // static 120 // static
215 PolicyProviderTestHarness* TestHarness::Create() { 121 PolicyProviderTestHarness* TestHarness::Create() {
216 return new TestHarness(); 122 return new TestHarness();
217 } 123 }
218 124
219 } // namespace 125 } // namespace
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 PolicyBundle expected_bundle; 190 PolicyBundle expected_bundle;
285 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) 191 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
286 .Set(test_keys::kKeyString, 192 .Set(test_keys::kKeyString,
287 POLICY_LEVEL_RECOMMENDED, 193 POLICY_LEVEL_RECOMMENDED,
288 POLICY_SCOPE_USER, 194 POLICY_SCOPE_USER,
289 base::Value::CreateStringValue("string value"), 195 base::Value::CreateStringValue("string value"),
290 NULL); 196 NULL);
291 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); 197 EXPECT_TRUE(provider_->policies().Equals(expected_bundle));
292 } 198 }
293 199
294 TEST_F(PolicyLoaderMacTest, TestConversions) {
295 base::DictionaryValue root;
296
297 // base::Value::TYPE_NULL
298 root.Set("null", base::Value::CreateNullValue());
299
300 // base::Value::TYPE_BOOLEAN
301 root.SetBoolean("false", false);
302 root.SetBoolean("true", true);
303
304 // base::Value::TYPE_INTEGER
305 root.SetInteger("int", 123);
306 root.SetInteger("zero", 0);
307
308 // base::Value::TYPE_DOUBLE
309 root.SetDouble("double", 123.456);
310 root.SetDouble("zerod", 0.0);
311
312 // base::Value::TYPE_STRING
313 root.SetString("string", "the fox jumps over something");
314 root.SetString("empty", "");
315
316 // base::Value::TYPE_LIST
317 base::ListValue list;
318 root.Set("emptyl", list.DeepCopy());
319 for (base::DictionaryValue::Iterator it(root); !it.IsAtEnd(); it.Advance())
320 list.Append(it.value().DeepCopy());
321 EXPECT_EQ(root.size(), list.GetSize());
322 list.Append(root.DeepCopy());
323 root.Set("list", list.DeepCopy());
324
325 // base::Value::TYPE_DICTIONARY
326 base::DictionaryValue dict;
327 root.Set("emptyd", dict.DeepCopy());
328 // Very meta.
329 root.Set("dict", root.DeepCopy());
330
331 ScopedCFTypeRef<CFPropertyListRef> property(CreatePropertyFromValue(&root));
332 ASSERT_TRUE(property);
333 scoped_ptr<base::Value> value(
334 PolicyLoaderMac::CreateValueFromProperty(property));
335 ASSERT_TRUE(value.get());
336
337 EXPECT_TRUE(root.Equals(value.get()));
338 }
339
340 } // namespace policy 200 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698