OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 "chrome/browser/policy/cloud_policy_provider.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/policy/cloud_policy_cache_base.h" | |
10 #include "chrome/browser/policy/cloud_policy_provider_impl.h" | |
11 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
12 #include "chrome/browser/policy/mock_configuration_policy_store.h" | |
13 #include "testing/gmock/include/gmock/gmock.h" | |
14 | |
15 using testing::AnyNumber; | |
16 using testing::_; | |
17 | |
18 namespace policy { | |
19 | |
20 class MockCloudPolicyCache : public CloudPolicyCacheBase { | |
21 public: | |
22 MockCloudPolicyCache() {} | |
23 virtual ~MockCloudPolicyCache() {} | |
24 | |
25 // CloudPolicyCacheBase implementation. | |
26 void Load() {} | |
27 void SetPolicy(const em::PolicyFetchResponse& policy) {} | |
28 bool DecodePolicyData(const em::PolicyData& policy_data, | |
29 PolicyMap* mandatory, | |
30 PolicyMap* recommended) { | |
31 return true; | |
32 } | |
33 | |
34 // Non-const accessors for underlying PolicyMaps. | |
35 PolicyMap* raw_mandatory_policy() { | |
36 return &mandatory_policy_; | |
37 } | |
38 | |
39 PolicyMap* raw_recommended_policy() { | |
40 return &recommended_policy_; | |
41 } | |
42 | |
43 void SetUnmanaged() { | |
44 is_unmanaged_ = true; | |
45 } | |
46 | |
47 void set_initialized(bool initialized) { | |
48 initialization_complete_ = initialized; | |
49 } | |
50 | |
51 private: | |
52 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache); | |
53 }; | |
54 | |
55 class CloudPolicyProviderTest : public testing::Test { | |
56 protected: | |
57 void CreateCloudPolicyProvider(CloudPolicyCacheBase::PolicyLevel level) { | |
58 cloud_policy_provider_.reset(new CloudPolicyProviderImpl( | |
59 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), level)); | |
60 } | |
61 | |
62 // Appends the caches to a provider and then provides the policies to | |
63 // |store_|. | |
64 void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n, | |
65 CloudPolicyCacheBase::PolicyLevel level) { | |
66 store_.reset(new MockConfigurationPolicyStore); | |
67 CloudPolicyProviderImpl provider( | |
68 policy::ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | |
69 level); | |
70 for (int i = 0; i < n; i++) { | |
71 provider.AppendCache(&caches[i]); | |
72 } | |
73 EXPECT_CALL(*store_.get(), Apply(_, _)).Times(AnyNumber()); | |
74 provider.Provide(store_.get()); | |
75 } | |
76 | |
77 // Checks a string policy in |store_|. | |
78 void ExpectStringPolicy(const std::string& expected, | |
Mattias Nissler (ping if slow)
2011/06/24 09:16:46
Again, can't we just check the policy map instead
gfeher
2011/06/24 15:32:44
We can, then I would have to extract the private m
Mattias Nissler (ping if slow)
2011/06/24 17:27:17
Actually you are right in not doing it, I had mixe
| |
79 ConfigurationPolicyType type) { | |
80 const Value* value = store_->Get(type); | |
81 std::string string_value; | |
82 EXPECT_TRUE(value != NULL); | |
83 EXPECT_TRUE(value->GetAsString(&string_value)); | |
84 EXPECT_EQ(expected, string_value); | |
85 } | |
86 | |
87 // Checks a boolean policy in |store_|. | |
88 void ExpectBoolPolicy(bool expected, ConfigurationPolicyType type) { | |
89 const Value* value = store_->Get(type); | |
90 bool bool_value; | |
91 EXPECT_TRUE(value != NULL); | |
92 EXPECT_TRUE(value->GetAsBoolean(&bool_value)); | |
93 EXPECT_EQ(expected, bool_value); | |
94 } | |
95 | |
96 void ExpectNoPolicy(ConfigurationPolicyType type) { | |
97 EXPECT_TRUE(NULL == store_->Get(type)); | |
98 } | |
99 | |
100 void CombineTwoPolicyMaps(const PolicyMap& base, | |
101 const PolicyMap& overlay, | |
102 PolicyMap* out_map) { | |
103 DCHECK(cloud_policy_provider_.get()); | |
104 cloud_policy_provider_->CombineTwoPolicyMaps(base, overlay, out_map); | |
105 } | |
106 | |
107 static int simple_list_length(); | |
108 | |
109 ConfigurationPolicyType simple_list_policy_type(int id) { | |
110 DCHECK(id < simple_list_length()); | |
111 DCHECK(!is_proxy_policy(simple_policies[id])); | |
112 return simple_policies[id]; | |
113 } | |
114 | |
115 int proxy_policy_count() { | |
116 return CloudPolicyProviderImpl::proxy_policy_count(); | |
117 } | |
118 | |
119 bool is_proxy_policy(ConfigurationPolicyType policy) { | |
120 return CloudPolicyProviderImpl::is_proxy_policy(policy); | |
121 } | |
122 | |
123 ConfigurationPolicyType get_proxy_policy(int i) { | |
124 DCHECK(i < proxy_policy_count()); | |
125 return CloudPolicyProviderImpl::proxy_policies[i]; | |
126 } | |
127 | |
128 private: | |
129 // Some tests need a list of policies that doesn't contain any proxy | |
130 // policies. Note: these policies will be handled as if they had the | |
131 // type of Value::TYPE_INTEGER. | |
132 static const ConfigurationPolicyType simple_policies[]; | |
133 | |
134 scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_; | |
135 scoped_ptr<MockConfigurationPolicyStore> store_; | |
136 }; | |
137 | |
138 // static | |
139 const ConfigurationPolicyType | |
140 CloudPolicyProviderTest::simple_policies[] = { | |
141 kPolicyHomepageLocation, | |
142 kPolicyHomepageIsNewTabPage, | |
143 kPolicyRestoreOnStartup, | |
144 kPolicyRestoreOnStartupURLs, | |
145 kPolicyDefaultSearchProviderEnabled, | |
146 kPolicyDefaultSearchProviderName, | |
147 kPolicyDefaultSearchProviderKeyword, | |
148 kPolicyDefaultSearchProviderSearchURL, | |
149 kPolicyDefaultSearchProviderSuggestURL, | |
150 kPolicyDefaultSearchProviderInstantURL, | |
151 kPolicyDefaultSearchProviderIconURL, | |
152 kPolicyDefaultSearchProviderEncodings, | |
153 }; | |
154 | |
155 // static | |
156 int CloudPolicyProviderTest::simple_list_length() { | |
157 return arraysize(simple_policies); | |
158 } | |
159 | |
160 TEST_F(CloudPolicyProviderTest, ProxyPolicies) { | |
161 EXPECT_EQ(5, proxy_policy_count()) << | |
162 "Please update CloudPolicyProviderTest with the new proxy policies."; | |
Mattias Nissler (ping if slow)
2011/06/24 09:16:46
Meh! What is this test good for? Just remove.
gfeher
2011/06/24 15:32:44
Done.
| |
163 } | |
164 | |
165 // Proxy setting distributed over multiple caches. | |
166 TEST_F(CloudPolicyProviderTest, | |
167 ProxySettingDistributedOverMultipleCaches) { | |
168 // There are proxy_policy_count()+1 = 6 caches and they are mixed together by | |
169 // one instance of CloudPolicyProvider. The first cache has some policies but | |
170 // no proxy-related ones. The following caches have each one proxy-policy set. | |
171 const int n = 6; | |
172 MockCloudPolicyCache caches[n]; | |
173 | |
174 // Prepare |cache[0]| to serve some non-proxy policies. | |
175 caches[0].raw_mandatory_policy()->Set(kPolicyShowHomeButton, | |
176 Value::CreateBooleanValue(true)); | |
177 caches[0].raw_mandatory_policy()->Set(kPolicyIncognitoEnabled, | |
178 Value::CreateBooleanValue(true)); | |
179 caches[0].raw_mandatory_policy()->Set(kPolicyTranslateEnabled, | |
180 Value::CreateBooleanValue(true)); | |
181 caches[0].set_initialized(true); | |
182 | |
183 // Prepare the other caches to serve one proxy-policy each. | |
184 caches[1].raw_mandatory_policy()->Set(kPolicyProxyMode, | |
185 Value::CreateStringValue("cache 1")); | |
186 caches[1].set_initialized(true); | |
187 caches[2].raw_mandatory_policy()->Set(kPolicyProxyServerMode, | |
188 Value::CreateIntegerValue(2)); | |
189 caches[2].set_initialized(true); | |
190 caches[3].raw_mandatory_policy()->Set(kPolicyProxyServer, | |
191 Value::CreateStringValue("cache 3")); | |
192 caches[3].set_initialized(true); | |
193 caches[4].raw_mandatory_policy()->Set(kPolicyProxyPacUrl, | |
194 Value::CreateStringValue("cache 4")); | |
195 caches[4].set_initialized(true); | |
196 caches[5].raw_mandatory_policy()->Set(kPolicyProxyMode, | |
197 Value::CreateStringValue("cache 5")); | |
198 caches[5].set_initialized(true); | |
199 | |
200 RunCachesThroughProvider( | |
201 caches, n, CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY); | |
202 | |
203 // Verify expectations. | |
204 ExpectStringPolicy("cache 1", kPolicyProxyMode); | |
205 ExpectNoPolicy(kPolicyProxyServerMode); | |
206 ExpectNoPolicy(kPolicyProxyServer); | |
207 ExpectNoPolicy(kPolicyProxyPacUrl); | |
208 ExpectBoolPolicy(true, kPolicyShowHomeButton); | |
209 ExpectBoolPolicy(true, kPolicyIncognitoEnabled); | |
210 ExpectBoolPolicy(true, kPolicyTranslateEnabled); | |
211 } | |
212 | |
213 // Combining two PolicyMaps. | |
214 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsSame) { | |
215 PolicyMap A, B, C; | |
216 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED); | |
217 for (int i = 0; i < simple_list_length(); ++i) { | |
Mattias Nissler (ping if slow)
2011/06/24 09:16:46
Still using loops? Can't we just get rid of the lo
gfeher
2011/06/24 15:32:44
Done.
| |
218 A.Set(simple_list_policy_type(i), Value::CreateIntegerValue(i)); | |
219 B.Set(simple_list_policy_type(i), Value::CreateIntegerValue(-1 * i)); | |
220 } | |
221 CombineTwoPolicyMaps(A, B, &C); | |
222 EXPECT_TRUE(A.Equals(C)); | |
223 } | |
224 | |
225 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsEmpty) { | |
226 PolicyMap A, B, C; | |
227 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED); | |
228 CombineTwoPolicyMaps(A, B, &C); | |
229 EXPECT_TRUE(C.empty()); | |
230 } | |
231 | |
232 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsPartial) { | |
233 PolicyMap A, B, C; | |
234 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED); | |
235 | |
236 for (int i = 0; i < simple_list_length(); ++i) { | |
Mattias Nissler (ping if slow)
2011/06/24 09:16:46
Still using loops?
gfeher
2011/06/24 15:32:44
Done.
| |
237 B.Set(simple_list_policy_type(i), Value::CreateIntegerValue(-i)); | |
238 if (i % 2 == 0) | |
239 A.Set(simple_list_policy_type(i), Value::CreateIntegerValue(i)); | |
240 } | |
241 | |
242 CombineTwoPolicyMaps(A, B, &C); | |
243 | |
244 for (int i = 0; i < simple_list_length(); ++i) { | |
245 if (const Value* value = C.Get(simple_list_policy_type(i))) { | |
246 int int_value; | |
247 EXPECT_TRUE(value->GetAsInteger(&int_value)); | |
248 if (i % 2 == 1) { | |
249 EXPECT_EQ(int_value, -1 * i); | |
250 } else { | |
251 EXPECT_EQ(int_value, i); | |
252 } | |
253 } | |
254 } | |
255 } | |
256 | |
257 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsProxies) { | |
258 const int a_value = 1; | |
259 const int b_value = -1; | |
260 PolicyMap A, B, C; | |
261 CreateCloudPolicyProvider(CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED); | |
262 | |
263 A.Set(policy::kPolicyProxyMode, Value::CreateIntegerValue(a_value)); | |
264 | |
265 B.Set(policy::kPolicyProxyServerMode, Value::CreateIntegerValue(b_value)); | |
266 B.Set(policy::kPolicyProxyServer, Value::CreateIntegerValue(b_value)); | |
267 B.Set(policy::kPolicyProxyPacUrl, Value::CreateIntegerValue(b_value)); | |
268 B.Set(policy::kPolicyProxyBypassList, Value::CreateIntegerValue(b_value)); | |
269 | |
270 CombineTwoPolicyMaps(A, B, &C); | |
271 | |
272 EXPECT_TRUE(A.Equals(C)); | |
273 EXPECT_FALSE(B.Equals(C)); | |
274 } | |
275 | |
276 } // namespace policy | |
OLD | NEW |