| OLD | NEW |
| (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/callback.h" | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/mac/scoped_cftyperef.h" | |
| 11 #include "base/strings/sys_string_conversions.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/policy/policy_loader_mac.h" | |
| 14 #include "components/policy/core/common/async_policy_provider.h" | |
| 15 #include "components/policy/core/common/configuration_policy_provider_test.h" | |
| 16 #include "components/policy/core/common/external_data_fetcher.h" | |
| 17 #include "components/policy/core/common/policy_bundle.h" | |
| 18 #include "components/policy/core/common/policy_map.h" | |
| 19 #include "components/policy/core/common/preferences_mock_mac.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 using base::ScopedCFTypeRef; | |
| 23 | |
| 24 namespace policy { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 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 { | |
| 122 public: | |
| 123 TestHarness(); | |
| 124 virtual ~TestHarness(); | |
| 125 | |
| 126 virtual void SetUp() OVERRIDE; | |
| 127 | |
| 128 virtual ConfigurationPolicyProvider* CreateProvider( | |
| 129 SchemaRegistry* registry, | |
| 130 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE; | |
| 131 | |
| 132 virtual void InstallEmptyPolicy() OVERRIDE; | |
| 133 virtual void InstallStringPolicy(const std::string& policy_name, | |
| 134 const std::string& policy_value) OVERRIDE; | |
| 135 virtual void InstallIntegerPolicy(const std::string& policy_name, | |
| 136 int policy_value) OVERRIDE; | |
| 137 virtual void InstallBooleanPolicy(const std::string& policy_name, | |
| 138 bool policy_value) OVERRIDE; | |
| 139 virtual void InstallStringListPolicy( | |
| 140 const std::string& policy_name, | |
| 141 const base::ListValue* policy_value) OVERRIDE; | |
| 142 virtual void InstallDictionaryPolicy( | |
| 143 const std::string& policy_name, | |
| 144 const base::DictionaryValue* policy_value) OVERRIDE; | |
| 145 | |
| 146 static PolicyProviderTestHarness* Create(); | |
| 147 | |
| 148 private: | |
| 149 MockPreferences* prefs_; | |
| 150 | |
| 151 DISALLOW_COPY_AND_ASSIGN(TestHarness); | |
| 152 }; | |
| 153 | |
| 154 TestHarness::TestHarness() | |
| 155 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER) {} | |
| 156 | |
| 157 TestHarness::~TestHarness() {} | |
| 158 | |
| 159 void TestHarness::SetUp() {} | |
| 160 | |
| 161 ConfigurationPolicyProvider* TestHarness::CreateProvider( | |
| 162 SchemaRegistry* registry, | |
| 163 scoped_refptr<base::SequencedTaskRunner> task_runner) { | |
| 164 prefs_ = new MockPreferences(); | |
| 165 scoped_ptr<AsyncPolicyLoader> loader( | |
| 166 new PolicyLoaderMac(task_runner, base::FilePath(), prefs_)); | |
| 167 return new AsyncPolicyProvider(registry, loader.Pass()); | |
| 168 } | |
| 169 | |
| 170 void TestHarness::InstallEmptyPolicy() {} | |
| 171 | |
| 172 void TestHarness::InstallStringPolicy(const std::string& policy_name, | |
| 173 const std::string& policy_value) { | |
| 174 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); | |
| 175 ScopedCFTypeRef<CFStringRef> value(base::SysUTF8ToCFStringRef(policy_value)); | |
| 176 prefs_->AddTestItem(name, value, true); | |
| 177 } | |
| 178 | |
| 179 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, | |
| 180 int policy_value) { | |
| 181 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); | |
| 182 ScopedCFTypeRef<CFNumberRef> value( | |
| 183 CFNumberCreate(NULL, kCFNumberIntType, &policy_value)); | |
| 184 prefs_->AddTestItem(name, value, true); | |
| 185 } | |
| 186 | |
| 187 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, | |
| 188 bool policy_value) { | |
| 189 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); | |
| 190 prefs_->AddTestItem(name, | |
| 191 policy_value ? kCFBooleanTrue : kCFBooleanFalse, | |
| 192 true); | |
| 193 } | |
| 194 | |
| 195 void TestHarness::InstallStringListPolicy(const std::string& policy_name, | |
| 196 const base::ListValue* policy_value) { | |
| 197 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); | |
| 198 ScopedCFTypeRef<CFPropertyListRef> array( | |
| 199 CreatePropertyFromValue(policy_value)); | |
| 200 ASSERT_TRUE(array); | |
| 201 prefs_->AddTestItem(name, array, true); | |
| 202 } | |
| 203 | |
| 204 void TestHarness::InstallDictionaryPolicy( | |
| 205 const std::string& policy_name, | |
| 206 const base::DictionaryValue* policy_value) { | |
| 207 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); | |
| 208 ScopedCFTypeRef<CFPropertyListRef> dict( | |
| 209 CreatePropertyFromValue(policy_value)); | |
| 210 ASSERT_TRUE(dict); | |
| 211 prefs_->AddTestItem(name, dict, true); | |
| 212 } | |
| 213 | |
| 214 // static | |
| 215 PolicyProviderTestHarness* TestHarness::Create() { | |
| 216 return new TestHarness(); | |
| 217 } | |
| 218 | |
| 219 } // namespace | |
| 220 | |
| 221 // Instantiate abstract test case for basic policy reading tests. | |
| 222 INSTANTIATE_TEST_CASE_P( | |
| 223 PolicyProviderMacTest, | |
| 224 ConfigurationPolicyProviderTest, | |
| 225 testing::Values(TestHarness::Create)); | |
| 226 | |
| 227 // TODO(joaodasilva): instantiate Configuration3rdPartyPolicyProviderTest too | |
| 228 // once the mac loader supports 3rd party policy. http://crbug.com/108995 | |
| 229 | |
| 230 // Special test cases for some mac preferences details. | |
| 231 class PolicyLoaderMacTest : public PolicyTestBase { | |
| 232 protected: | |
| 233 PolicyLoaderMacTest() | |
| 234 : prefs_(new MockPreferences()) {} | |
| 235 virtual ~PolicyLoaderMacTest() {} | |
| 236 | |
| 237 virtual void SetUp() OVERRIDE { | |
| 238 PolicyTestBase::SetUp(); | |
| 239 scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderMac( | |
| 240 loop_.message_loop_proxy(), base::FilePath(), prefs_)); | |
| 241 provider_.reset(new AsyncPolicyProvider(&schema_registry_, loader.Pass())); | |
| 242 provider_->Init(&schema_registry_); | |
| 243 } | |
| 244 | |
| 245 virtual void TearDown() OVERRIDE { | |
| 246 provider_->Shutdown(); | |
| 247 PolicyTestBase::TearDown(); | |
| 248 } | |
| 249 | |
| 250 MockPreferences* prefs_; | |
| 251 scoped_ptr<AsyncPolicyProvider> provider_; | |
| 252 }; | |
| 253 | |
| 254 TEST_F(PolicyLoaderMacTest, Invalid) { | |
| 255 ScopedCFTypeRef<CFStringRef> name( | |
| 256 base::SysUTF8ToCFStringRef(test_keys::kKeyString)); | |
| 257 const char buffer[] = "binary \xde\xad\xbe\xef data"; | |
| 258 ScopedCFTypeRef<CFDataRef> invalid_data( | |
| 259 CFDataCreate(kCFAllocatorDefault, | |
| 260 reinterpret_cast<const UInt8 *>(buffer), | |
| 261 arraysize(buffer))); | |
| 262 ASSERT_TRUE(invalid_data); | |
| 263 prefs_->AddTestItem(name, invalid_data.get(), true); | |
| 264 prefs_->AddTestItem(name, invalid_data.get(), false); | |
| 265 | |
| 266 // Make the provider read the updated |prefs_|. | |
| 267 provider_->RefreshPolicies(); | |
| 268 loop_.RunUntilIdle(); | |
| 269 const PolicyBundle kEmptyBundle; | |
| 270 EXPECT_TRUE(provider_->policies().Equals(kEmptyBundle)); | |
| 271 } | |
| 272 | |
| 273 TEST_F(PolicyLoaderMacTest, TestNonForcedValue) { | |
| 274 ScopedCFTypeRef<CFStringRef> name( | |
| 275 base::SysUTF8ToCFStringRef(test_keys::kKeyString)); | |
| 276 ScopedCFTypeRef<CFPropertyListRef> test_value( | |
| 277 base::SysUTF8ToCFStringRef("string value")); | |
| 278 ASSERT_TRUE(test_value.get()); | |
| 279 prefs_->AddTestItem(name, test_value.get(), false); | |
| 280 | |
| 281 // Make the provider read the updated |prefs_|. | |
| 282 provider_->RefreshPolicies(); | |
| 283 loop_.RunUntilIdle(); | |
| 284 PolicyBundle expected_bundle; | |
| 285 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | |
| 286 .Set(test_keys::kKeyString, | |
| 287 POLICY_LEVEL_RECOMMENDED, | |
| 288 POLICY_SCOPE_USER, | |
| 289 base::Value::CreateStringValue("string value"), | |
| 290 NULL); | |
| 291 EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); | |
| 292 } | |
| 293 | |
| 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 | |
| OLD | NEW |