Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / common / policy_loader_ios_unittest.mm
blobdba3991a800a217d809e1c836a554ab108eaf415
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"
22 namespace policy {
24 namespace {
26 // Key in the NSUserDefaults that contains the managed app configuration.
27 NSString* const kConfigurationKey = @"com.apple.configuration.managed";
29 class TestHarness : public PolicyProviderTestHarness {
30  public:
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();
58  private:
59   // Merges the policies in |policy| into the current policy dictionary
60   // in NSUserDefaults, after making sure that the policy dictionary
61   // exists.
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() {
94   AddPolicies(@{});
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);
101   AddPolicies(@{
102       key: value
103   });
106 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
107                                        int policy_value) {
108   NSString* key = base::SysUTF8ToNSString(policy_name);
109   AddPolicies(@{
110       key: [NSNumber numberWithInt:policy_value]
111   });
114 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
115                                        bool policy_value) {
116   NSString* key = base::SysUTF8ToNSString(policy_name);
117   AddPolicies(@{
118       key: [NSNumber numberWithBool:policy_value]
119   });
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));
126   AddPolicies(@{
127       key: static_cast<NSArray*>(value.get())
128   });
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));
136   AddPolicies(@{
137       key: static_cast<NSDictionary*>(value.get())
138   });
141 // static
142 PolicyProviderTestHarness* TestHarness::Create() {
143   return new TestHarness(false);
146 // static
147 PolicyProviderTestHarness* TestHarness::CreateWithEncodedKey() {
148   return new TestHarness(true);
151 void TestHarness::AddPolicies(NSDictionary* policy) {
152   if (use_encoded_key_)
153     AddEncodedChromePolicy(policy);
154   else
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];
165   if (previous)
166     [chromePolicy addEntriesFromDictionary:previous];
168   [chromePolicy addEntriesFromDictionary:policy];
170   NSDictionary* wrapper = @{
171       key: chromePolicy
172   };
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];
185   if (previous) {
186     base::scoped_nsobject<NSData> data(
187         [[NSData alloc] initWithBase64EncodedString:previous options:0]);
188     NSDictionary* properties = [NSPropertyListSerialization
189         propertyListWithData:data.get()
190                      options:NSPropertyListImmutable
191                       format:NULL
192                        error:NULL];
193     [chromePolicy addEntriesFromDictionary:properties];
194   }
196   [chromePolicy addEntriesFromDictionary:policy];
198   NSData* data = [NSPropertyListSerialization
199       dataWithPropertyList:chromePolicy
200                     format:NSPropertyListXMLFormat_v1_0
201                    options:0
202                      error:NULL];
203   NSString* encoded = [data base64EncodedStringWithOptions:0];
205   NSDictionary* wrapper = @{
206       key: encoded
207   };
208   [[NSUserDefaults standardUserDefaults] setObject:wrapper
209                                             forKey:kConfigurationKey];
212 }  // namespace
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 = @{
229     @"shared": @"wrong",
230     @"key1": @"value1",
231   };
232   NSData* data = [NSPropertyListSerialization
233       dataWithPropertyList:policy
234                     format:NSPropertyListXMLFormat_v1_0
235                    options:0
236                      error:NULL];
237   NSString* encodedChromePolicy = [data base64EncodedStringWithOptions:0];
239   NSDictionary* chromePolicy = @{
240     @"shared": @"right",
241     @"key2": @"value2",
242   };
244   NSDictionary* wrapper = @{
245     @"ChromePolicy": chromePolicy,
246     @"EncodedChromePolicy": encodedChromePolicy,
247   };
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"),
257                   nullptr);
258   expectedMap.Set("key2", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
259                   POLICY_SOURCE_PLATFORM, new base::StringValue("value2"),
260                   nullptr);
262   scoped_refptr<base::TestSimpleTaskRunner> taskRunner =
263       new base::TestSimpleTaskRunner();
264   PolicyLoaderIOS loader(taskRunner);
265   scoped_ptr<PolicyBundle> bundle = loader.Load();
266   ASSERT_TRUE(bundle);
267   EXPECT_TRUE(bundle->Equals(expected));
270 }  // namespace policy