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.
5 #include <CoreFoundation/CoreFoundation.h>
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 "components/policy/core/common/async_policy_provider.h"
14 #include "components/policy/core/common/configuration_policy_provider_test.h"
15 #include "components/policy/core/common/external_data_fetcher.h"
16 #include "components/policy/core/common/policy_bundle.h"
17 #include "components/policy/core/common/policy_loader_mac.h"
18 #include "components/policy/core/common/policy_map.h"
19 #include "components/policy/core/common/policy_test_utils.h"
20 #include "components/policy/core/common/preferences_mock_mac.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using base::ScopedCFTypeRef
;
29 class TestHarness
: public PolicyProviderTestHarness
{
32 virtual ~TestHarness();
34 virtual void SetUp() OVERRIDE
;
36 virtual ConfigurationPolicyProvider
* CreateProvider(
37 SchemaRegistry
* registry
,
38 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) OVERRIDE
;
40 virtual void InstallEmptyPolicy() OVERRIDE
;
41 virtual void InstallStringPolicy(const std::string
& policy_name
,
42 const std::string
& policy_value
) OVERRIDE
;
43 virtual void InstallIntegerPolicy(const std::string
& policy_name
,
44 int policy_value
) OVERRIDE
;
45 virtual void InstallBooleanPolicy(const std::string
& policy_name
,
46 bool policy_value
) OVERRIDE
;
47 virtual void InstallStringListPolicy(
48 const std::string
& policy_name
,
49 const base::ListValue
* policy_value
) OVERRIDE
;
50 virtual void InstallDictionaryPolicy(
51 const std::string
& policy_name
,
52 const base::DictionaryValue
* policy_value
) OVERRIDE
;
54 static PolicyProviderTestHarness
* Create();
57 MockPreferences
* prefs_
;
59 DISALLOW_COPY_AND_ASSIGN(TestHarness
);
62 TestHarness::TestHarness()
63 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
) {}
65 TestHarness::~TestHarness() {}
67 void TestHarness::SetUp() {}
69 ConfigurationPolicyProvider
* TestHarness::CreateProvider(
70 SchemaRegistry
* registry
,
71 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) {
72 prefs_
= new MockPreferences();
73 scoped_ptr
<AsyncPolicyLoader
> loader(
74 new PolicyLoaderMac(task_runner
, base::FilePath(), prefs_
));
75 return new AsyncPolicyProvider(registry
, loader
.Pass());
78 void TestHarness::InstallEmptyPolicy() {}
80 void TestHarness::InstallStringPolicy(const std::string
& policy_name
,
81 const std::string
& policy_value
) {
82 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
83 ScopedCFTypeRef
<CFStringRef
> value(base::SysUTF8ToCFStringRef(policy_value
));
84 prefs_
->AddTestItem(name
, value
, true);
87 void TestHarness::InstallIntegerPolicy(const std::string
& policy_name
,
89 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
90 ScopedCFTypeRef
<CFNumberRef
> value(
91 CFNumberCreate(NULL
, kCFNumberIntType
, &policy_value
));
92 prefs_
->AddTestItem(name
, value
, true);
95 void TestHarness::InstallBooleanPolicy(const std::string
& policy_name
,
97 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
98 prefs_
->AddTestItem(name
,
99 policy_value
? kCFBooleanTrue
: kCFBooleanFalse
,
103 void TestHarness::InstallStringListPolicy(const std::string
& policy_name
,
104 const base::ListValue
* policy_value
) {
105 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
106 ScopedCFTypeRef
<CFPropertyListRef
> array(ValueToProperty(policy_value
));
108 prefs_
->AddTestItem(name
, array
, true);
111 void TestHarness::InstallDictionaryPolicy(
112 const std::string
& policy_name
,
113 const base::DictionaryValue
* policy_value
) {
114 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
115 ScopedCFTypeRef
<CFPropertyListRef
> dict(ValueToProperty(policy_value
));
117 prefs_
->AddTestItem(name
, dict
, true);
121 PolicyProviderTestHarness
* TestHarness::Create() {
122 return new TestHarness();
127 // Instantiate abstract test case for basic policy reading tests.
128 INSTANTIATE_TEST_CASE_P(
129 PolicyProviderMacTest
,
130 ConfigurationPolicyProviderTest
,
131 testing::Values(TestHarness::Create
));
133 // TODO(joaodasilva): instantiate Configuration3rdPartyPolicyProviderTest too
134 // once the mac loader supports 3rd party policy. http://crbug.com/108995
136 // Special test cases for some mac preferences details.
137 class PolicyLoaderMacTest
: public PolicyTestBase
{
139 PolicyLoaderMacTest()
140 : prefs_(new MockPreferences()) {}
141 virtual ~PolicyLoaderMacTest() {}
143 virtual void SetUp() OVERRIDE
{
144 PolicyTestBase::SetUp();
145 scoped_ptr
<AsyncPolicyLoader
> loader(new PolicyLoaderMac(
146 loop_
.message_loop_proxy(), base::FilePath(), prefs_
));
147 provider_
.reset(new AsyncPolicyProvider(&schema_registry_
, loader
.Pass()));
148 provider_
->Init(&schema_registry_
);
151 virtual void TearDown() OVERRIDE
{
152 provider_
->Shutdown();
153 PolicyTestBase::TearDown();
156 MockPreferences
* prefs_
;
157 scoped_ptr
<AsyncPolicyProvider
> provider_
;
160 TEST_F(PolicyLoaderMacTest
, Invalid
) {
161 ScopedCFTypeRef
<CFStringRef
> name(
162 base::SysUTF8ToCFStringRef(test_keys::kKeyString
));
163 const char buffer
[] = "binary \xde\xad\xbe\xef data";
164 ScopedCFTypeRef
<CFDataRef
> invalid_data(
165 CFDataCreate(kCFAllocatorDefault
,
166 reinterpret_cast<const UInt8
*>(buffer
),
168 ASSERT_TRUE(invalid_data
);
169 prefs_
->AddTestItem(name
, invalid_data
.get(), true);
170 prefs_
->AddTestItem(name
, invalid_data
.get(), false);
172 // Make the provider read the updated |prefs_|.
173 provider_
->RefreshPolicies();
174 loop_
.RunUntilIdle();
175 const PolicyBundle kEmptyBundle
;
176 EXPECT_TRUE(provider_
->policies().Equals(kEmptyBundle
));
179 TEST_F(PolicyLoaderMacTest
, TestNonForcedValue
) {
180 ScopedCFTypeRef
<CFStringRef
> name(
181 base::SysUTF8ToCFStringRef(test_keys::kKeyString
));
182 ScopedCFTypeRef
<CFPropertyListRef
> test_value(
183 base::SysUTF8ToCFStringRef("string value"));
184 ASSERT_TRUE(test_value
.get());
185 prefs_
->AddTestItem(name
, test_value
.get(), false);
187 // Make the provider read the updated |prefs_|.
188 provider_
->RefreshPolicies();
189 loop_
.RunUntilIdle();
190 PolicyBundle expected_bundle
;
191 expected_bundle
.Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
192 .Set(test_keys::kKeyString
,
193 POLICY_LEVEL_RECOMMENDED
,
195 base::Value::CreateStringValue("string value"),
197 EXPECT_TRUE(provider_
->policies().Equals(expected_bundle
));
200 } // namespace policy