Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(752)

Side by Side Diff: components/policy/core/common/policy_loader_ios_unittest.mm

Issue 213483004: Extended the iOS platform policy loader to load an additional key. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « components/policy/core/common/policy_loader_ios.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <UIKit/UIKit.h> 5 #include <UIKit/UIKit.h>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
11 #include "base/test/test_simple_task_runner.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "components/policy/core/common/async_policy_provider.h" 13 #include "components/policy/core/common/async_policy_provider.h"
13 #include "components/policy/core/common/configuration_policy_provider_test.h" 14 #include "components/policy/core/common/configuration_policy_provider_test.h"
14 #include "components/policy/core/common/policy_bundle.h" 15 #include "components/policy/core/common/policy_bundle.h"
15 #include "components/policy/core/common/policy_loader_ios.h" 16 #include "components/policy/core/common/policy_loader_ios.h"
16 #include "components/policy/core/common/policy_map.h" 17 #include "components/policy/core/common/policy_map.h"
17 #include "components/policy/core/common/policy_test_utils.h" 18 #include "components/policy/core/common/policy_test_utils.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 namespace policy { 21 namespace policy {
21 22
22 namespace { 23 namespace {
23 24
24 // Key in the NSUserDefaults that contains the managed app configuration. 25 // Key in the NSUserDefaults that contains the managed app configuration.
25 NSString* const kConfigurationKey = @"com.apple.configuration.managed"; 26 NSString* const kConfigurationKey = @"com.apple.configuration.managed";
26 27
27 class TestHarness : public PolicyProviderTestHarness { 28 class TestHarness : public PolicyProviderTestHarness {
28 public: 29 public:
29 TestHarness(); 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);
30 virtual ~TestHarness(); 33 virtual ~TestHarness();
31 34
32 virtual void SetUp() OVERRIDE; 35 virtual void SetUp() OVERRIDE;
33 36
34 virtual ConfigurationPolicyProvider* CreateProvider( 37 virtual ConfigurationPolicyProvider* CreateProvider(
35 SchemaRegistry* registry, 38 SchemaRegistry* registry,
36 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE; 39 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE;
37 40
38 virtual void InstallEmptyPolicy() OVERRIDE; 41 virtual void InstallEmptyPolicy() OVERRIDE;
39 virtual void InstallStringPolicy(const std::string& policy_name, 42 virtual void InstallStringPolicy(const std::string& policy_name,
40 const std::string& policy_value) OVERRIDE; 43 const std::string& policy_value) OVERRIDE;
41 virtual void InstallIntegerPolicy(const std::string& policy_name, 44 virtual void InstallIntegerPolicy(const std::string& policy_name,
42 int policy_value) OVERRIDE; 45 int policy_value) OVERRIDE;
43 virtual void InstallBooleanPolicy(const std::string& policy_name, 46 virtual void InstallBooleanPolicy(const std::string& policy_name,
44 bool policy_value) OVERRIDE; 47 bool policy_value) OVERRIDE;
45 virtual void InstallStringListPolicy( 48 virtual void InstallStringListPolicy(
46 const std::string& policy_name, 49 const std::string& policy_name,
47 const base::ListValue* policy_value) OVERRIDE; 50 const base::ListValue* policy_value) OVERRIDE;
48 virtual void InstallDictionaryPolicy( 51 virtual void InstallDictionaryPolicy(
49 const std::string& policy_name, 52 const std::string& policy_name,
50 const base::DictionaryValue* policy_value) OVERRIDE; 53 const base::DictionaryValue* policy_value) OVERRIDE;
51 54
52 static PolicyProviderTestHarness* Create(); 55 static PolicyProviderTestHarness* Create();
56 static PolicyProviderTestHarness* CreateWithEncodedKey();
53 57
54 private: 58 private:
55 // Merges the policies in |policy| into the current policy dictionary 59 // Merges the policies in |policy| into the current policy dictionary
56 // in NSUserDefaults, after making sure that the policy dictionary 60 // in NSUserDefaults, after making sure that the policy dictionary
57 // exists. 61 // exists.
58 void AddPolicies(NSDictionary* policy); 62 void AddPolicies(NSDictionary* policy);
63 void AddChromePolicy(NSDictionary* policy);
64 void AddEncodedChromePolicy(NSDictionary* policy);
65
66 bool use_encoded_key_;
59 67
60 DISALLOW_COPY_AND_ASSIGN(TestHarness); 68 DISALLOW_COPY_AND_ASSIGN(TestHarness);
61 }; 69 };
62 70
63 TestHarness::TestHarness() 71 TestHarness::TestHarness(bool use_encoded_key)
64 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE) {} 72 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE),
73 use_encoded_key_(use_encoded_key) {}
65 74
66 TestHarness::~TestHarness() { 75 TestHarness::~TestHarness() {
67 // Cleanup any policies left from the test. 76 // Cleanup any policies left from the test.
68 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kConfigurationKey]; 77 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kConfigurationKey];
69 } 78 }
70 79
71 void TestHarness::SetUp() { 80 void TestHarness::SetUp() {
72 // Make sure there is no pre-existing policy present. 81 // Make sure there is no pre-existing policy present.
73 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kConfigurationKey]; 82 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kConfigurationKey];
74 } 83 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 const base::DictionaryValue* policy_value) { 132 const base::DictionaryValue* policy_value) {
124 NSString* key = base::SysUTF8ToNSString(policy_name); 133 NSString* key = base::SysUTF8ToNSString(policy_name);
125 base::ScopedCFTypeRef<CFPropertyListRef> value(ValueToProperty(policy_value)); 134 base::ScopedCFTypeRef<CFPropertyListRef> value(ValueToProperty(policy_value));
126 AddPolicies(@{ 135 AddPolicies(@{
127 key: static_cast<NSDictionary*>(value.get()) 136 key: static_cast<NSDictionary*>(value.get())
128 }); 137 });
129 } 138 }
130 139
131 // static 140 // static
132 PolicyProviderTestHarness* TestHarness::Create() { 141 PolicyProviderTestHarness* TestHarness::Create() {
133 return new TestHarness(); 142 return new TestHarness(false);
143 }
144
145 // static
146 PolicyProviderTestHarness* TestHarness::CreateWithEncodedKey() {
147 return new TestHarness(true);
134 } 148 }
135 149
136 void TestHarness::AddPolicies(NSDictionary* policy) { 150 void TestHarness::AddPolicies(NSDictionary* policy) {
151 if (use_encoded_key_)
152 AddEncodedChromePolicy(policy);
153 else
154 AddChromePolicy(policy);
155 }
156
157 void TestHarness::AddChromePolicy(NSDictionary* policy) {
137 NSString* key = @"ChromePolicy"; 158 NSString* key = @"ChromePolicy";
138 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 159 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
139 base::scoped_nsobject<NSMutableDictionary> chromePolicy( 160 base::scoped_nsobject<NSMutableDictionary> chromePolicy(
140 [[NSMutableDictionary alloc] init]); 161 [[NSMutableDictionary alloc] init]);
141 162
142 NSDictionary* previous = [defaults dictionaryForKey:key]; 163 NSDictionary* previous = [defaults dictionaryForKey:key];
143 if (previous) 164 if (previous)
144 [chromePolicy addEntriesFromDictionary:previous]; 165 [chromePolicy addEntriesFromDictionary:previous];
145 166
146 [chromePolicy addEntriesFromDictionary:policy]; 167 [chromePolicy addEntriesFromDictionary:policy];
147 168
148 NSDictionary* wrapper = @{ 169 NSDictionary* wrapper = @{
149 key: chromePolicy 170 key: chromePolicy
150 }; 171 };
151 [[NSUserDefaults standardUserDefaults] setObject:wrapper 172 [[NSUserDefaults standardUserDefaults] setObject:wrapper
152 forKey:kConfigurationKey]; 173 forKey:kConfigurationKey];
153 } 174 }
154 175
176 void TestHarness::AddEncodedChromePolicy(NSDictionary* policy) {
177 NSString* key = @"EncodedChromePolicy";
178 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
179
180 base::scoped_nsobject<NSMutableDictionary> chromePolicy(
181 [[NSMutableDictionary alloc] init]);
182
183 NSString* previous = [defaults stringForKey:key];
184 if (previous) {
185 base::scoped_nsobject<NSData> data(
186 [[NSData alloc] initWithBase64EncodedString:previous options:0]);
187 NSDictionary* properties = [NSPropertyListSerialization
188 propertyListWithData:data.get()
189 options:NSPropertyListImmutable
190 format:NULL
191 error:NULL];
192 [chromePolicy addEntriesFromDictionary:properties];
193 }
194
195 [chromePolicy addEntriesFromDictionary:policy];
196
197 NSData* data = [NSPropertyListSerialization
198 dataWithPropertyList:chromePolicy
199 format:NSPropertyListXMLFormat_v1_0
200 options:0
201 error:NULL];
202 NSString* encoded = [data base64EncodedStringWithOptions:0];
203
204 NSDictionary* wrapper = @{
205 key: encoded
206 };
207 [[NSUserDefaults standardUserDefaults] setObject:wrapper
208 forKey:kConfigurationKey];
209 }
210
155 } // namespace 211 } // namespace
156 212
157 // Instantiate abstract test case for basic policy reading tests.
158 INSTANTIATE_TEST_CASE_P( 213 INSTANTIATE_TEST_CASE_P(
159 PolicyProviderIOSTest, 214 PolicyProviderIOSChromePolicyTest,
160 ConfigurationPolicyProviderTest, 215 ConfigurationPolicyProviderTest,
161 testing::Values(TestHarness::Create)); 216 testing::Values(TestHarness::Create));
162 217
218 INSTANTIATE_TEST_CASE_P(
219 PolicyProviderIOSEncodedChromePolicyTest,
220 ConfigurationPolicyProviderTest,
221 testing::Values(TestHarness::CreateWithEncodedKey));
222
223 TEST(PolicyProviderIOSTest, ChromePolicyOverEncodedChromePolicy) {
224 // This test verifies that if the "ChromePolicy" key is present then the
225 // "EncodedChromePolicy" key is ignored.
226
227 NSDictionary* policy = @{
228 @"shared": @"wrong",
229 @"key1": @"value1",
230 };
231 NSData* data = [NSPropertyListSerialization
232 dataWithPropertyList:policy
233 format:NSPropertyListXMLFormat_v1_0
234 options:0
235 error:NULL];
236 NSString* encodedChromePolicy = [data base64EncodedStringWithOptions:0];
237
238 NSDictionary* chromePolicy = @{
239 @"shared": @"right",
240 @"key2": @"value2",
241 };
242
243 NSDictionary* wrapper = @{
244 @"ChromePolicy": chromePolicy,
245 @"EncodedChromePolicy": encodedChromePolicy,
246 };
247
248 [[NSUserDefaults standardUserDefaults] setObject:wrapper
249 forKey:kConfigurationKey];
250
251 PolicyBundle expected;
252 PolicyMap& expectedMap =
253 expected.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, ""));
254 expectedMap.Set("shared", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
255 new base::StringValue("right"), NULL);
256 expectedMap.Set("key2", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
257 new base::StringValue("value2"), NULL);
258
259 scoped_refptr<base::TestSimpleTaskRunner> taskRunner =
260 new base::TestSimpleTaskRunner();
261 PolicyLoaderIOS loader(taskRunner);
262 scoped_ptr<PolicyBundle> bundle = loader.Load();
263 ASSERT_TRUE(bundle);
264 EXPECT_TRUE(bundle->Equals(expected));
265 }
266
163 } // namespace policy 267 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/policy_loader_ios.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698