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 "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"
22
23 using base::ScopedCFTypeRef;
24
25 namespace policy {
26
27 namespace {
28
29 class TestHarness : public PolicyProviderTestHarness {
30 public:
31 TestHarness();
32 virtual ~TestHarness();
33
34 virtual void SetUp() OVERRIDE;
35
36 virtual ConfigurationPolicyProvider* CreateProvider(
37 SchemaRegistry* registry,
38 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE;
39
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;
53
54 static PolicyProviderTestHarness* Create();
55
56 private:
57 MockPreferences* prefs_;
58
59 DISALLOW_COPY_AND_ASSIGN(TestHarness);
60 };
61
TestHarness()62 TestHarness::TestHarness()
63 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER) {}
64
~TestHarness()65 TestHarness::~TestHarness() {}
66
SetUp()67 void TestHarness::SetUp() {}
68
CreateProvider(SchemaRegistry * registry,scoped_refptr<base::SequencedTaskRunner> task_runner)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());
76 }
77
InstallEmptyPolicy()78 void TestHarness::InstallEmptyPolicy() {}
79
InstallStringPolicy(const std::string & policy_name,const std::string & policy_value)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);
85 }
86
InstallIntegerPolicy(const std::string & policy_name,int policy_value)87 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
88 int policy_value) {
89 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
90 ScopedCFTypeRef<CFNumberRef> value(
91 CFNumberCreate(NULL, kCFNumberIntType, &policy_value));
92 prefs_->AddTestItem(name, value, true);
93 }
94
InstallBooleanPolicy(const std::string & policy_name,bool policy_value)95 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
96 bool policy_value) {
97 ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name));
98 prefs_->AddTestItem(name,
99 policy_value ? kCFBooleanTrue : kCFBooleanFalse,
100 true);
101 }
102
InstallStringListPolicy(const std::string & policy_name,const base::ListValue * policy_value)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));
107 ASSERT_TRUE(array);
108 prefs_->AddTestItem(name, array, true);
109 }
110
InstallDictionaryPolicy(const std::string & policy_name,const base::DictionaryValue * policy_value)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));
116 ASSERT_TRUE(dict);
117 prefs_->AddTestItem(name, dict, true);
118 }
119
120 // static
Create()121 PolicyProviderTestHarness* TestHarness::Create() {
122 return new TestHarness();
123 }
124
125 } // namespace
126
127 // Instantiate abstract test case for basic policy reading tests.
128 INSTANTIATE_TEST_CASE_P(
129 PolicyProviderMacTest,
130 ConfigurationPolicyProviderTest,
131 testing::Values(TestHarness::Create));
132
133 // TODO(joaodasilva): instantiate Configuration3rdPartyPolicyProviderTest too
134 // once the mac loader supports 3rd party policy. http://crbug.com/108995
135
136 // Special test cases for some mac preferences details.
137 class PolicyLoaderMacTest : public PolicyTestBase {
138 protected:
PolicyLoaderMacTest()139 PolicyLoaderMacTest()
140 : prefs_(new MockPreferences()) {}
~PolicyLoaderMacTest()141 virtual ~PolicyLoaderMacTest() {}
142
SetUp()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_);
149 }
150
TearDown()151 virtual void TearDown() OVERRIDE {
152 provider_->Shutdown();
153 PolicyTestBase::TearDown();
154 }
155
156 MockPreferences* prefs_;
157 scoped_ptr<AsyncPolicyProvider> provider_;
158 };
159
TEST_F(PolicyLoaderMacTest,Invalid)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),
167 arraysize(buffer)));
168 ASSERT_TRUE(invalid_data);
169 prefs_->AddTestItem(name, invalid_data.get(), true);
170 prefs_->AddTestItem(name, invalid_data.get(), false);
171
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));
177 }
178
TEST_F(PolicyLoaderMacTest,TestNonForcedValue)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);
186
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,
194 POLICY_SCOPE_USER,
195 new base::StringValue("string value"),
196 NULL);
197 EXPECT_TRUE(provider_->policies().Equals(expected_bundle));
198 }
199
200 } // namespace policy
201