cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / policy / core / common / policy_loader_ios_unittest.mm
blobf22d37f41caecb97a91e8b43290d0a474fa7175b
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 "testing/gtest/include/gtest/gtest.h"
21 namespace policy {
23 namespace {
25 // Key in the NSUserDefaults that contains the managed app configuration.
26 NSString* const kConfigurationKey = @"com.apple.configuration.managed";
28 class TestHarness : public PolicyProviderTestHarness {
29  public:
30   // If |use_encoded_key| is true then AddPolicies() serializes and encodes
31   // the policies, and publishes them under the EncodedChromePolicy key.
32   explicit TestHarness(bool use_encoded_key);
33   ~TestHarness() override;
35   void SetUp() override;
37   ConfigurationPolicyProvider* CreateProvider(
38       SchemaRegistry* registry,
39       scoped_refptr<base::SequencedTaskRunner> task_runner) override;
41   void InstallEmptyPolicy() override;
42   void InstallStringPolicy(const std::string& policy_name,
43                            const std::string& policy_value) override;
44   void InstallIntegerPolicy(const std::string& policy_name,
45                             int policy_value) override;
46   void InstallBooleanPolicy(const std::string& policy_name,
47                             bool policy_value) override;
48   void InstallStringListPolicy(const std::string& policy_name,
49                                const base::ListValue* policy_value) override;
50   void InstallDictionaryPolicy(
51       const std::string& policy_name,
52       const base::DictionaryValue* policy_value) override;
54   static PolicyProviderTestHarness* Create();
55   static PolicyProviderTestHarness* CreateWithEncodedKey();
57  private:
58   // Merges the policies in |policy| into the current policy dictionary
59   // in NSUserDefaults, after making sure that the policy dictionary
60   // exists.
61   void AddPolicies(NSDictionary* policy);
62   void AddChromePolicy(NSDictionary* policy);
63   void AddEncodedChromePolicy(NSDictionary* policy);
65   bool use_encoded_key_;
67   DISALLOW_COPY_AND_ASSIGN(TestHarness);
70 TestHarness::TestHarness(bool use_encoded_key)
71     : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE),
72       use_encoded_key_(use_encoded_key) {}
74 TestHarness::~TestHarness() {
75   // Cleanup any policies left from the test.
76   [[NSUserDefaults standardUserDefaults] removeObjectForKey:kConfigurationKey];
79 void TestHarness::SetUp() {
80   // Make sure there is no pre-existing policy present.
81   [[NSUserDefaults standardUserDefaults] removeObjectForKey:kConfigurationKey];
84 ConfigurationPolicyProvider* TestHarness::CreateProvider(
85     SchemaRegistry* registry,
86     scoped_refptr<base::SequencedTaskRunner> task_runner) {
87   scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderIOS(task_runner));
88   return new AsyncPolicyProvider(registry, loader.Pass());
91 void TestHarness::InstallEmptyPolicy() {
92   AddPolicies(@{});
95 void TestHarness::InstallStringPolicy(const std::string& policy_name,
96                                       const std::string& policy_value) {
97   NSString* key = base::SysUTF8ToNSString(policy_name);
98   NSString* value = base::SysUTF8ToNSString(policy_value);
99   AddPolicies(@{
100       key: value
101   });
104 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
105                                        int policy_value) {
106   NSString* key = base::SysUTF8ToNSString(policy_name);
107   AddPolicies(@{
108       key: [NSNumber numberWithInt:policy_value]
109   });
112 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
113                                        bool policy_value) {
114   NSString* key = base::SysUTF8ToNSString(policy_name);
115   AddPolicies(@{
116       key: [NSNumber numberWithBool:policy_value]
117   });
120 void TestHarness::InstallStringListPolicy(const std::string& policy_name,
121                                           const base::ListValue* policy_value) {
122   NSString* key = base::SysUTF8ToNSString(policy_name);
123   base::ScopedCFTypeRef<CFPropertyListRef> value(ValueToProperty(policy_value));
124   AddPolicies(@{
125       key: static_cast<NSArray*>(value.get())
126   });
129 void TestHarness::InstallDictionaryPolicy(
130     const std::string& policy_name,
131     const base::DictionaryValue* policy_value) {
132   NSString* key = base::SysUTF8ToNSString(policy_name);
133   base::ScopedCFTypeRef<CFPropertyListRef> value(ValueToProperty(policy_value));
134   AddPolicies(@{
135       key: static_cast<NSDictionary*>(value.get())
136   });
139 // static
140 PolicyProviderTestHarness* TestHarness::Create() {
141   return new TestHarness(false);
144 // static
145 PolicyProviderTestHarness* TestHarness::CreateWithEncodedKey() {
146   return new TestHarness(true);
149 void TestHarness::AddPolicies(NSDictionary* policy) {
150   if (use_encoded_key_)
151     AddEncodedChromePolicy(policy);
152   else
153     AddChromePolicy(policy);
156 void TestHarness::AddChromePolicy(NSDictionary* policy) {
157   NSString* key = @"ChromePolicy";
158   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
159   base::scoped_nsobject<NSMutableDictionary> chromePolicy(
160       [[NSMutableDictionary alloc] init]);
162   NSDictionary* previous = [defaults dictionaryForKey:key];
163   if (previous)
164     [chromePolicy addEntriesFromDictionary:previous];
166   [chromePolicy addEntriesFromDictionary:policy];
168   NSDictionary* wrapper = @{
169       key: chromePolicy
170   };
171   [[NSUserDefaults standardUserDefaults] setObject:wrapper
172                                             forKey:kConfigurationKey];
175 void TestHarness::AddEncodedChromePolicy(NSDictionary* policy) {
176   NSString* key = @"EncodedChromePolicy";
177   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
179   base::scoped_nsobject<NSMutableDictionary> chromePolicy(
180       [[NSMutableDictionary alloc] init]);
182   NSString* previous = [defaults stringForKey:key];
183   if (previous) {
184     base::scoped_nsobject<NSData> data(
185         [[NSData alloc] initWithBase64EncodedString:previous options:0]);
186     NSDictionary* properties = [NSPropertyListSerialization
187         propertyListWithData:data.get()
188                      options:NSPropertyListImmutable
189                       format:NULL
190                        error:NULL];
191     [chromePolicy addEntriesFromDictionary:properties];
192   }
194   [chromePolicy addEntriesFromDictionary:policy];
196   NSData* data = [NSPropertyListSerialization
197       dataWithPropertyList:chromePolicy
198                     format:NSPropertyListXMLFormat_v1_0
199                    options:0
200                      error:NULL];
201   NSString* encoded = [data base64EncodedStringWithOptions:0];
203   NSDictionary* wrapper = @{
204       key: encoded
205   };
206   [[NSUserDefaults standardUserDefaults] setObject:wrapper
207                                             forKey:kConfigurationKey];
210 }  // namespace
212 INSTANTIATE_TEST_CASE_P(
213     PolicyProviderIOSChromePolicyTest,
214     ConfigurationPolicyProviderTest,
215     testing::Values(TestHarness::Create));
217 INSTANTIATE_TEST_CASE_P(
218     PolicyProviderIOSEncodedChromePolicyTest,
219     ConfigurationPolicyProviderTest,
220     testing::Values(TestHarness::CreateWithEncodedKey));
222 TEST(PolicyProviderIOSTest, ChromePolicyOverEncodedChromePolicy) {
223   // This test verifies that if the "ChromePolicy" key is present then the
224   // "EncodedChromePolicy" key is ignored.
226   NSDictionary* policy = @{
227     @"shared": @"wrong",
228     @"key1": @"value1",
229   };
230   NSData* data = [NSPropertyListSerialization
231       dataWithPropertyList:policy
232                     format:NSPropertyListXMLFormat_v1_0
233                    options:0
234                      error:NULL];
235   NSString* encodedChromePolicy = [data base64EncodedStringWithOptions:0];
237   NSDictionary* chromePolicy = @{
238     @"shared": @"right",
239     @"key2": @"value2",
240   };
242   NSDictionary* wrapper = @{
243     @"ChromePolicy": chromePolicy,
244     @"EncodedChromePolicy": encodedChromePolicy,
245   };
247   [[NSUserDefaults standardUserDefaults] setObject:wrapper
248                                             forKey:kConfigurationKey];
250   PolicyBundle expected;
251   PolicyMap& expectedMap =
252       expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, ""));
253   expectedMap.Set("shared", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
254                   new base::StringValue("right"), NULL);
255   expectedMap.Set("key2", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
256                   new base::StringValue("value2"), NULL);
258   scoped_refptr<base::TestSimpleTaskRunner> taskRunner =
259       new base::TestSimpleTaskRunner();
260   PolicyLoaderIOS loader(taskRunner);
261   scoped_ptr<PolicyBundle> bundle = loader.Load();
262   ASSERT_TRUE(bundle);
263   EXPECT_TRUE(bundle->Equals(expected));
266 }  // namespace policy