1 // Copyright 2014 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 <UIKit/UIKit.h>
7 #include "base/basictypes.h"
8 #include "base/callback.h"
9 #include "base/files/file_path.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/test/test_simple_task_runner.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/policy_bundle.h"
16 #include "components/policy/core/common/policy_loader_ios.h"
17 #include "components/policy/core/common/policy_map.h"
18 #include "components/policy/core/common/policy_test_utils.h"
19 #include "components/policy/core/common/policy_types.h"
20 #include "testing/gtest/include/gtest/gtest.h"
26 // Key in the NSUserDefaults that contains the managed app configuration.
27 NSString* const kConfigurationKey = @"com.apple.configuration.managed";
29 class TestHarness : public PolicyProviderTestHarness {
31 // If |use_encoded_key| is true then AddPolicies() serializes and encodes
32 // the policies, and publishes them under the EncodedChromePolicy key.
33 explicit TestHarness(bool use_encoded_key);
34 ~TestHarness() override;
36 void SetUp() override;
38 ConfigurationPolicyProvider* CreateProvider(
39 SchemaRegistry* registry,
40 scoped_refptr<base::SequencedTaskRunner> task_runner) override;
42 void InstallEmptyPolicy() override;
43 void InstallStringPolicy(const std::string& policy_name,
44 const std::string& policy_value) override;
45 void InstallIntegerPolicy(const std::string& policy_name,
46 int policy_value) override;
47 void InstallBooleanPolicy(const std::string& policy_name,
48 bool policy_value) override;
49 void InstallStringListPolicy(const std::string& policy_name,
50 const base::ListValue* policy_value) override;
51 void InstallDictionaryPolicy(
52 const std::string& policy_name,
53 const base::DictionaryValue* policy_value) override;
55 static PolicyProviderTestHarness* Create();
56 static PolicyProviderTestHarness* CreateWithEncodedKey();
59 // Merges the policies in |policy| into the current policy dictionary
60 // in NSUserDefaults, after making sure that the policy dictionary
62 void AddPolicies(NSDictionary* policy);
63 void AddChromePolicy(NSDictionary* policy);
64 void AddEncodedChromePolicy(NSDictionary* policy);
66 bool use_encoded_key_;
68 DISALLOW_COPY_AND_ASSIGN(TestHarness);
71 TestHarness::TestHarness(bool use_encoded_key)
72 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
73 POLICY_SOURCE_PLATFORM),
74 use_encoded_key_(use_encoded_key) {}
76 TestHarness::~TestHarness() {
77 // Cleanup any policies left from the test.
78 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kConfigurationKey];
81 void TestHarness::SetUp() {
82 // Make sure there is no pre-existing policy present.
83 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kConfigurationKey];
86 ConfigurationPolicyProvider* TestHarness::CreateProvider(
87 SchemaRegistry* registry,
88 scoped_refptr<base::SequencedTaskRunner> task_runner) {
89 scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderIOS(task_runner));
90 return new AsyncPolicyProvider(registry, loader.Pass());
93 void TestHarness::InstallEmptyPolicy() {
97 void TestHarness::InstallStringPolicy(const std::string& policy_name,
98 const std::string& policy_value) {
99 NSString* key = base::SysUTF8ToNSString(policy_name);
100 NSString* value = base::SysUTF8ToNSString(policy_value);
106 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
108 NSString* key = base::SysUTF8ToNSString(policy_name);
110 key: [NSNumber numberWithInt:policy_value]
114 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
116 NSString* key = base::SysUTF8ToNSString(policy_name);
118 key: [NSNumber numberWithBool:policy_value]
122 void TestHarness::InstallStringListPolicy(const std::string& policy_name,
123 const base::ListValue* policy_value) {
124 NSString* key = base::SysUTF8ToNSString(policy_name);
125 base::ScopedCFTypeRef<CFPropertyListRef> value(ValueToProperty(policy_value));
127 key: static_cast<NSArray*>(value.get())
131 void TestHarness::InstallDictionaryPolicy(
132 const std::string& policy_name,
133 const base::DictionaryValue* policy_value) {
134 NSString* key = base::SysUTF8ToNSString(policy_name);
135 base::ScopedCFTypeRef<CFPropertyListRef> value(ValueToProperty(policy_value));
137 key: static_cast<NSDictionary*>(value.get())
142 PolicyProviderTestHarness* TestHarness::Create() {
143 return new TestHarness(false);
147 PolicyProviderTestHarness* TestHarness::CreateWithEncodedKey() {
148 return new TestHarness(true);
151 void TestHarness::AddPolicies(NSDictionary* policy) {
152 if (use_encoded_key_)
153 AddEncodedChromePolicy(policy);
155 AddChromePolicy(policy);
158 void TestHarness::AddChromePolicy(NSDictionary* policy) {
159 NSString* key = @"ChromePolicy";
160 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
161 base::scoped_nsobject<NSMutableDictionary> chromePolicy(
162 [[NSMutableDictionary alloc] init]);
164 NSDictionary* previous = [defaults dictionaryForKey:key];
166 [chromePolicy addEntriesFromDictionary:previous];
168 [chromePolicy addEntriesFromDictionary:policy];
170 NSDictionary* wrapper = @{
173 [[NSUserDefaults standardUserDefaults] setObject:wrapper
174 forKey:kConfigurationKey];
177 void TestHarness::AddEncodedChromePolicy(NSDictionary* policy) {
178 NSString* key = @"EncodedChromePolicy";
179 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
181 base::scoped_nsobject<NSMutableDictionary> chromePolicy(
182 [[NSMutableDictionary alloc] init]);
184 NSString* previous = [defaults stringForKey:key];
186 base::scoped_nsobject<NSData> data(
187 [[NSData alloc] initWithBase64EncodedString:previous options:0]);
188 NSDictionary* properties = [NSPropertyListSerialization
189 propertyListWithData:data.get()
190 options:NSPropertyListImmutable
193 [chromePolicy addEntriesFromDictionary:properties];
196 [chromePolicy addEntriesFromDictionary:policy];
198 NSData* data = [NSPropertyListSerialization
199 dataWithPropertyList:chromePolicy
200 format:NSPropertyListXMLFormat_v1_0
203 NSString* encoded = [data base64EncodedStringWithOptions:0];
205 NSDictionary* wrapper = @{
208 [[NSUserDefaults standardUserDefaults] setObject:wrapper
209 forKey:kConfigurationKey];
214 INSTANTIATE_TEST_CASE_P(
215 PolicyProviderIOSChromePolicyTest,
216 ConfigurationPolicyProviderTest,
217 testing::Values(TestHarness::Create));
219 INSTANTIATE_TEST_CASE_P(
220 PolicyProviderIOSEncodedChromePolicyTest,
221 ConfigurationPolicyProviderTest,
222 testing::Values(TestHarness::CreateWithEncodedKey));
224 TEST(PolicyProviderIOSTest, ChromePolicyOverEncodedChromePolicy) {
225 // This test verifies that if the "ChromePolicy" key is present then the
226 // "EncodedChromePolicy" key is ignored.
228 NSDictionary* policy = @{
232 NSData* data = [NSPropertyListSerialization
233 dataWithPropertyList:policy
234 format:NSPropertyListXMLFormat_v1_0
237 NSString* encodedChromePolicy = [data base64EncodedStringWithOptions:0];
239 NSDictionary* chromePolicy = @{
244 NSDictionary* wrapper = @{
245 @"ChromePolicy": chromePolicy,
246 @"EncodedChromePolicy": encodedChromePolicy,
249 [[NSUserDefaults standardUserDefaults] setObject:wrapper
250 forKey:kConfigurationKey];
252 PolicyBundle expected;
253 PolicyMap& expectedMap =
254 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, ""));
255 expectedMap.Set("shared", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
256 POLICY_SOURCE_PLATFORM, new base::StringValue("right"),
258 expectedMap.Set("key2", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
259 POLICY_SOURCE_PLATFORM, new base::StringValue("value2"),
262 scoped_refptr<base::TestSimpleTaskRunner> taskRunner =
263 new base::TestSimpleTaskRunner();
264 PolicyLoaderIOS loader(taskRunner);
265 scoped_ptr<PolicyBundle> bundle = loader.Load();
267 EXPECT_TRUE(bundle->Equals(expected));
270 } // namespace policy