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 ~TestHarness() override
;
34 void SetUp() override
;
36 ConfigurationPolicyProvider
* CreateProvider(
37 SchemaRegistry
* registry
,
38 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) override
;
40 void InstallEmptyPolicy() override
;
41 void InstallStringPolicy(const std::string
& policy_name
,
42 const std::string
& policy_value
) override
;
43 void InstallIntegerPolicy(const std::string
& policy_name
,
44 int policy_value
) override
;
45 void InstallBooleanPolicy(const std::string
& policy_name
,
46 bool policy_value
) override
;
47 void InstallStringListPolicy(const std::string
& policy_name
,
48 const base::ListValue
* policy_value
) override
;
49 void InstallDictionaryPolicy(
50 const std::string
& policy_name
,
51 const base::DictionaryValue
* policy_value
) override
;
53 static PolicyProviderTestHarness
* Create();
56 MockPreferences
* prefs_
;
58 DISALLOW_COPY_AND_ASSIGN(TestHarness
);
61 TestHarness::TestHarness()
62 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
) {}
64 TestHarness::~TestHarness() {}
66 void TestHarness::SetUp() {}
68 ConfigurationPolicyProvider
* TestHarness::CreateProvider(
69 SchemaRegistry
* registry
,
70 scoped_refptr
<base::SequencedTaskRunner
> task_runner
) {
71 prefs_
= new MockPreferences();
72 scoped_ptr
<AsyncPolicyLoader
> loader(
73 new PolicyLoaderMac(task_runner
, base::FilePath(), prefs_
));
74 return new AsyncPolicyProvider(registry
, loader
.Pass());
77 void TestHarness::InstallEmptyPolicy() {}
79 void TestHarness::InstallStringPolicy(const std::string
& policy_name
,
80 const std::string
& policy_value
) {
81 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
82 ScopedCFTypeRef
<CFStringRef
> value(base::SysUTF8ToCFStringRef(policy_value
));
83 prefs_
->AddTestItem(name
, value
, true);
86 void TestHarness::InstallIntegerPolicy(const std::string
& policy_name
,
88 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
89 ScopedCFTypeRef
<CFNumberRef
> value(
90 CFNumberCreate(NULL
, kCFNumberIntType
, &policy_value
));
91 prefs_
->AddTestItem(name
, value
, true);
94 void TestHarness::InstallBooleanPolicy(const std::string
& policy_name
,
96 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
97 prefs_
->AddTestItem(name
,
98 policy_value
? kCFBooleanTrue
: kCFBooleanFalse
,
102 void TestHarness::InstallStringListPolicy(const std::string
& policy_name
,
103 const base::ListValue
* policy_value
) {
104 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
105 ScopedCFTypeRef
<CFPropertyListRef
> array(ValueToProperty(policy_value
));
107 prefs_
->AddTestItem(name
, array
, true);
110 void TestHarness::InstallDictionaryPolicy(
111 const std::string
& policy_name
,
112 const base::DictionaryValue
* policy_value
) {
113 ScopedCFTypeRef
<CFStringRef
> name(base::SysUTF8ToCFStringRef(policy_name
));
114 ScopedCFTypeRef
<CFPropertyListRef
> dict(ValueToProperty(policy_value
));
116 prefs_
->AddTestItem(name
, dict
, true);
120 PolicyProviderTestHarness
* TestHarness::Create() {
121 return new TestHarness();
126 // Instantiate abstract test case for basic policy reading tests.
127 INSTANTIATE_TEST_CASE_P(
128 PolicyProviderMacTest
,
129 ConfigurationPolicyProviderTest
,
130 testing::Values(TestHarness::Create
));
132 // TODO(joaodasilva): instantiate Configuration3rdPartyPolicyProviderTest too
133 // once the mac loader supports 3rd party policy. http://crbug.com/108995
135 // Special test cases for some mac preferences details.
136 class PolicyLoaderMacTest
: public PolicyTestBase
{
138 PolicyLoaderMacTest()
139 : prefs_(new MockPreferences()) {}
141 void SetUp() override
{
142 PolicyTestBase::SetUp();
143 scoped_ptr
<AsyncPolicyLoader
> loader(
144 new PolicyLoaderMac(loop_
.task_runner(), base::FilePath(), prefs_
));
145 provider_
.reset(new AsyncPolicyProvider(&schema_registry_
, loader
.Pass()));
146 provider_
->Init(&schema_registry_
);
149 void TearDown() override
{
150 provider_
->Shutdown();
151 PolicyTestBase::TearDown();
154 MockPreferences
* prefs_
;
155 scoped_ptr
<AsyncPolicyProvider
> provider_
;
158 TEST_F(PolicyLoaderMacTest
, Invalid
) {
159 ScopedCFTypeRef
<CFStringRef
> name(
160 base::SysUTF8ToCFStringRef(test_keys::kKeyString
));
161 const char buffer
[] = "binary \xde\xad\xbe\xef data";
162 ScopedCFTypeRef
<CFDataRef
> invalid_data(
163 CFDataCreate(kCFAllocatorDefault
,
164 reinterpret_cast<const UInt8
*>(buffer
),
166 ASSERT_TRUE(invalid_data
);
167 prefs_
->AddTestItem(name
, invalid_data
.get(), true);
168 prefs_
->AddTestItem(name
, invalid_data
.get(), false);
170 // Make the provider read the updated |prefs_|.
171 provider_
->RefreshPolicies();
172 loop_
.RunUntilIdle();
173 const PolicyBundle kEmptyBundle
;
174 EXPECT_TRUE(provider_
->policies().Equals(kEmptyBundle
));
177 TEST_F(PolicyLoaderMacTest
, TestNonForcedValue
) {
178 ScopedCFTypeRef
<CFStringRef
> name(
179 base::SysUTF8ToCFStringRef(test_keys::kKeyString
));
180 ScopedCFTypeRef
<CFPropertyListRef
> test_value(
181 base::SysUTF8ToCFStringRef("string value"));
182 ASSERT_TRUE(test_value
.get());
183 prefs_
->AddTestItem(name
, test_value
.get(), false);
185 // Make the provider read the updated |prefs_|.
186 provider_
->RefreshPolicies();
187 loop_
.RunUntilIdle();
188 PolicyBundle expected_bundle
;
189 expected_bundle
.Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
190 .Set(test_keys::kKeyString
,
191 POLICY_LEVEL_RECOMMENDED
,
193 new base::StringValue("string value"),
195 EXPECT_TRUE(provider_
->policies().Equals(expected_bundle
));
198 } // namespace policy