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

Side by Side Diff: chrome/browser/policy/cloud_policy_provider_unittest.cc

Issue 9911029: Refactored the CloudPolicyProvider so that it becomes initialized once and stays initialized. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Updated expectations in failing browser_tests Created 8 years, 8 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/policy/cloud_policy_provider.h" 5 #include "chrome/browser/policy/cloud_policy_provider.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h"
11 #include "base/scoped_temp_dir.h"
8 #include "base/values.h" 12 #include "base/values.h"
9 #include "chrome/browser/policy/browser_policy_connector.h" 13 #include "chrome/browser/policy/browser_policy_connector.h"
10 #include "chrome/browser/policy/cloud_policy_cache_base.h" 14 #include "chrome/browser/policy/cloud_policy_provider.h"
11 #include "chrome/browser/policy/cloud_policy_provider_impl.h"
12 #include "chrome/browser/policy/configuration_policy_provider.h" 15 #include "chrome/browser/policy/configuration_policy_provider.h"
13 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 16 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
17 #include "chrome/browser/policy/proto/cloud_policy.pb.h"
18 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
19 #include "chrome/browser/policy/user_policy_cache.h"
20 #include "content/test/test_browser_thread.h"
14 #include "policy/policy_constants.h" 21 #include "policy/policy_constants.h"
15 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 #if defined(OS_CHROMEOS)
26 #include "chrome/browser/policy/device_policy_cache.h"
27 #include "chrome/browser/policy/device_policy_cache_test_base.h"
28 #endif
16 29
17 using testing::Mock; 30 using testing::Mock;
18 using testing::_;
19 31
20 namespace em = enterprise_management; 32 namespace em = enterprise_management;
21 33
22 namespace policy { 34 namespace policy {
23 35
24 namespace { 36 class DevicePolicyCache;
25
26 // Utility function for tests.
27 void SetPolicy(PolicyMap* map, const char* policy, Value* value) {
28 map->Set(policy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, value);
29 }
30
31 } // namespace
32
33 class MockCloudPolicyCache : public CloudPolicyCacheBase {
34 public:
35 MockCloudPolicyCache() {}
36 virtual ~MockCloudPolicyCache() {}
37
38 // CloudPolicyCacheBase implementation.
39 void Load() OVERRIDE {}
40 bool SetPolicy(const em::PolicyFetchResponse& policy) OVERRIDE {
41 return true;
42 }
43 bool DecodePolicyData(const em::PolicyData& policy_data,
44 PolicyMap* policies) OVERRIDE {
45 return true;
46 }
47
48 void SetUnmanaged() OVERRIDE {
49 is_unmanaged_ = true;
50 }
51
52 PolicyMap* mutable_policy() {
53 return &policies_;
54 }
55
56 void set_initialized(bool initialized) {
57 initialization_complete_ = initialized;
58 }
59
60 void Set(const char *name, Value* value) {
61 policies_.Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, value);
62 }
63
64 private:
65 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyCache);
66 };
67 37
68 class CloudPolicyProviderTest : public testing::Test { 38 class CloudPolicyProviderTest : public testing::Test {
69 protected: 39 protected:
70 void CreateCloudPolicyProvider() { 40 CloudPolicyProviderTest()
41 : message_loop_(MessageLoop::TYPE_UI),
42 ui_thread_(content::BrowserThread::UI, &message_loop_),
43 file_thread_(content::BrowserThread::FILE, &message_loop_) {}
44
45 void SetUp() OVERRIDE {
46 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
47 }
48
49 void ResetProvider() {
50 registrar_.reset();
51 cloud_policy_provider_.reset();
52 user_policy_cache_.reset();
53 #if defined(OS_CHROMEOS)
54 device_cache_helper_.reset();
55 device_policy_cache_ = NULL;
Mattias Nissler (ping if slow) 2012/03/30 08:48:46 Wait, so you're running this test with actual user
Joao da Silva 2012/03/30 10:09:23 This was done when the cloud provider was owning t
56 #endif
57 message_loop_.RunAllPending();
58
71 cloud_policy_provider_.reset( 59 cloud_policy_provider_.reset(
72 new CloudPolicyProviderImpl(&browser_policy_connector_, 60 new CloudPolicyProvider(&browser_policy_connector_,
73 GetChromePolicyDefinitionList(), 61 GetChromePolicyDefinitionList(),
74 POLICY_LEVEL_MANDATORY)); 62 POLICY_LEVEL_MANDATORY));
75 } 63 registrar_.reset(new ConfigurationPolicyObserverRegistrar());
76 64 registrar_->Init(cloud_policy_provider_.get(), &observer_);
77 // Appends the caches to a provider and then provides the policies to 65 }
78 // |result|. 66
79 void RunCachesThroughProvider(MockCloudPolicyCache caches[], int n, 67 void AddUserCache() {
80 PolicyMap* result) { 68 if (user_policy_cache_.get()) {
81 CloudPolicyProviderImpl provider( 69 FAIL();
82 &browser_policy_connector_, 70 return;
83 GetChromePolicyDefinitionList(),
84 POLICY_LEVEL_MANDATORY);
85 for (int i = 0; i < n; i++) {
86 provider.AppendCache(&caches[i]);
87 } 71 }
88 provider.Provide(result); 72 FilePath path = scoped_temp_dir_.path().AppendASCII("test_policy_cache");
89 } 73 file_util::Delete(path, false);
90 74 user_policy_cache_.reset(new UserPolicyCache(path, true));
91 void CombineTwoPolicyMaps(const PolicyMap& base, 75 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get()));
92 const PolicyMap& overlay, 76 cloud_policy_provider_->SetUserPolicyCache(user_policy_cache_.get());
93 PolicyMap* out_map) { 77 Mock::VerifyAndClearExpectations(&observer_);
94 MockCloudPolicyCache caches[2]; 78 }
95 caches[0].mutable_policy()->CopyFrom(base); 79
96 caches[0].set_initialized(true); 80 void AddDeviceCache() {
97 caches[1].mutable_policy()->CopyFrom(overlay); 81 #if defined(OS_CHROMEOS)
98 caches[1].set_initialized(true); 82 if (device_policy_cache_) {
99 RunCachesThroughProvider(caches, 2, out_map); 83 FAIL();
100 } 84 return;
101 85 }
102 void FixDeprecatedPolicies(PolicyMap* policies) { 86 device_cache_helper_.reset(new DevicePolicyCacheTestHelper());
103 CloudPolicyProviderImpl::FixDeprecatedPolicies(policies); 87 device_policy_cache_ = device_cache_helper_->cache();
104 } 88 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get()));
105 89 cloud_policy_provider_->SetDevicePolicyCache(device_policy_cache_);
106 scoped_ptr<CloudPolicyProviderImpl> cloud_policy_provider_; 90 Mock::VerifyAndClearExpectations(&observer_);
107 91 #endif
108 private: 92 }
93
94
95 void SetUserCacheReady() {
96 if (!user_policy_cache_.get()) {
97 FAIL();
98 } else if (!user_policy_cache_->IsReady()) {
99 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get()));
100 user_policy_cache_->SetFetchingDone();
101 user_policy_cache_->Load();
102 message_loop_.RunAllPending();
103 Mock::VerifyAndClearExpectations(&observer_);
104 EXPECT_TRUE(user_policy_cache_->IsReady());
105 }
106 }
107
108 void SetDeviceCacheReady() {
109 #if defined(OS_CHROMEOS)
110 if (!device_policy_cache_) {
111 FAIL();
112 } else if (!device_policy_cache_->IsReady()) {
113 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get()));
114 // Initialize the cache with an empty policy. This also makes the cache
115 // ready.
116 device_cache_helper_->ResetPolicy();
117 ASSERT_TRUE(device_cache_helper_->LoadPolicy());
118 Mock::VerifyAndClearExpectations(&observer_);
119 EXPECT_TRUE(device_policy_cache_->IsReady());
120 }
121 #endif
122 }
123
124 bool SetUserPolicy(const em::CloudPolicySettings& settings) {
125 em::PolicyData signed_response;
126 EXPECT_TRUE(
127 settings.SerializeToString(signed_response.mutable_policy_value()));
128 base::Time now = base::Time::NowFromSystemTime();
129 signed_response.set_timestamp(
130 (now - base::Time::UnixEpoch()).InMilliseconds());
131 std::string serialized_signed_response;
132 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response));
133
134 em::PolicyFetchResponse response;
135 response.set_policy_data(serialized_signed_response);
136 response.set_policy_data_signature("signature");
137 response.set_new_public_key("key");
138
139 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get()));
140 bool result = user_policy_cache_->SetPolicy(response);
141 // SetFetchingDone() is what triggers notifications. It is always invoked
142 // by the controller after a fetch, regardless of the result of SetPolicy().
143 user_policy_cache_->SetFetchingDone();
144 Mock::VerifyAndClearExpectations(&observer_);
145
146 return result;
147 }
148
149 bool SetHomepageUserPolicy(const std::string& url) {
150 em::CloudPolicySettings settings;
151 em::HomepageLocationProto* homepage = settings.mutable_homepagelocation();
152 homepage->set_homepagelocation(url);
153 homepage->mutable_policy_options()->set_mode(em::PolicyOptions::MANDATORY);
154 return SetUserPolicy(settings);
155 }
156
157 bool SetProxyUserPolicy(const std::string& proxy_mode,
158 const std::string& proxy_server,
159 const std::string& proxy_pac_url,
160 const std::string& proxy_bypass_list) {
161 em::CloudPolicySettings settings;
162 settings.mutable_proxymode()->set_proxymode(proxy_mode);
163 settings.mutable_proxymode()->mutable_policy_options()->set_mode(
164 em::PolicyOptions::MANDATORY);
165 settings.mutable_proxyserver()->set_proxyserver(proxy_server);
166 settings.mutable_proxyserver()->mutable_policy_options()->set_mode(
167 em::PolicyOptions::MANDATORY);
168 settings.mutable_proxypacurl()->set_proxypacurl(proxy_pac_url);
169 settings.mutable_proxypacurl()->mutable_policy_options()->set_mode(
170 em::PolicyOptions::MANDATORY);
171 settings.mutable_proxybypasslist()->set_proxybypasslist(proxy_bypass_list);
172 settings.mutable_proxybypasslist()->mutable_policy_options()->set_mode(
173 em::PolicyOptions::MANDATORY);
174 return SetUserPolicy(settings);
175 }
176
177 MessageLoop message_loop_;
178 content::TestBrowserThread ui_thread_;
179 content::TestBrowserThread file_thread_;
180
181 ScopedTempDir scoped_temp_dir_;
109 BrowserPolicyConnector browser_policy_connector_; 182 BrowserPolicyConnector browser_policy_connector_;
183
184 scoped_ptr<UserPolicyCache> user_policy_cache_;
185
186 #if defined(OS_CHROMEOS)
187 scoped_ptr<DevicePolicyCacheTestHelper> device_cache_helper_;
188 DevicePolicyCache* device_policy_cache_;
189 #endif
190
191 scoped_ptr<CloudPolicyProvider> cloud_policy_provider_;
192 MockConfigurationPolicyObserver observer_;
193 scoped_ptr<ConfigurationPolicyObserverRegistrar> registrar_;
110 }; 194 };
111 195
112 // Proxy setting distributed over multiple caches. 196 TEST_F(CloudPolicyProviderTest, UserPolicy) {
113 TEST_F(CloudPolicyProviderTest, 197 ResetProvider();
114 ProxySettingDistributedOverMultipleCaches) { 198 EXPECT_FALSE(cloud_policy_provider_->IsInitializationComplete());
115 // There are proxy_policy_count()+1 = 6 caches and they are mixed together by 199
116 // one instance of CloudPolicyProvider. The first cache has some policies but 200 // The provider only becomes initialized when it has all caches, and the
117 // no proxy-related ones. The following caches have each one proxy-policy set. 201 // caches are ready too.
118 const int n = 6; 202 AddDeviceCache();
119 MockCloudPolicyCache caches[n]; 203 EXPECT_FALSE(cloud_policy_provider_->IsInitializationComplete());
120 204 SetDeviceCacheReady();
121 // Prepare |cache[0]| to serve some non-proxy policies. 205 EXPECT_FALSE(cloud_policy_provider_->IsInitializationComplete());
122 caches[0].Set(key::kShowHomeButton, Value::CreateBooleanValue(true)); 206 AddUserCache();
123 caches[0].Set(key::kIncognitoEnabled, Value::CreateBooleanValue(true)); 207 EXPECT_FALSE(user_policy_cache_->IsReady());
124 caches[0].Set(key::kTranslateEnabled, Value::CreateBooleanValue(true)); 208 EXPECT_FALSE(cloud_policy_provider_->IsInitializationComplete());
125 caches[0].set_initialized(true); 209
126 210 PolicyMap policy;
127 // Prepare the other caches to serve one proxy-policy each. 211 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
128 caches[1].Set(key::kProxyMode, Value::CreateStringValue("cache 1")); 212 EXPECT_TRUE(policy.empty());
129 caches[1].set_initialized(true); 213
130 caches[2].Set(key::kProxyServerMode, Value::CreateIntegerValue(2)); 214 SetUserCacheReady();
131 caches[2].set_initialized(true); 215 EXPECT_TRUE(cloud_policy_provider_->IsInitializationComplete());
132 caches[3].Set(key::kProxyServer, Value::CreateStringValue("cache 3")); 216 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
133 caches[3].set_initialized(true); 217 EXPECT_TRUE(policy.empty());
134 caches[4].Set(key::kProxyPacUrl, Value::CreateStringValue("cache 4")); 218
135 caches[4].set_initialized(true); 219 const std::string kUrl("http://chromium.org");
136 caches[5].Set(key::kProxyMode, Value::CreateStringValue("cache 5")); 220 EXPECT_TRUE(SetHomepageUserPolicy(kUrl));
137 caches[5].set_initialized(true); 221 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
138 222
139 PolicyMap policies; 223 PolicyMap expected;
140 RunCachesThroughProvider(caches, n, &policies); 224 expected.Set(key::kHomepageLocation,
141 225 POLICY_LEVEL_MANDATORY,
142 // Verify expectations. 226 POLICY_SCOPE_USER,
143 EXPECT_TRUE(policies.Get(key::kProxyMode) == NULL); 227 Value::CreateStringValue(kUrl));
144 EXPECT_TRUE(policies.Get(key::kProxyServerMode) == NULL); 228 EXPECT_TRUE(policy.Equals(expected));
145 EXPECT_TRUE(policies.Get(key::kProxyServer) == NULL); 229 }
146 EXPECT_TRUE(policies.Get(key::kProxyPacUrl) == NULL); 230
147 231 TEST_F(CloudPolicyProviderTest, RefreshPolicies) {
148 const Value* value = policies.GetValue(key::kProxySettings); 232 // OnUpdatePolicy is called when the provider doesn't have any caches.
149 ASSERT_TRUE(value != NULL); 233 ResetProvider();
150 ASSERT_TRUE(value->IsType(Value::TYPE_DICTIONARY)); 234 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
151 const DictionaryValue* settings = static_cast<const DictionaryValue*>(value); 235 cloud_policy_provider_->RefreshPolicies();
236 Mock::VerifyAndClearExpectations(&observer_);
237
238 // OnUpdatePolicy is called when all the caches have updated.
239 AddUserCache();
240
241 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
242 cloud_policy_provider_->RefreshPolicies();
243 Mock::VerifyAndClearExpectations(&observer_);
244
245 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
246 cloud_policy_provider_->OnCacheUpdate(user_policy_cache_.get());
247 Mock::VerifyAndClearExpectations(&observer_);
248
249 #if defined(OS_CHROMEOS)
250 AddDeviceCache();
251
252 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
253 cloud_policy_provider_->RefreshPolicies();
254 Mock::VerifyAndClearExpectations(&observer_);
255
256 // Updating one of the caches is not enough, both must be updated.
257 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
258 cloud_policy_provider_->OnCacheUpdate(user_policy_cache_.get());
259 Mock::VerifyAndClearExpectations(&observer_);
260
261 // If a cache refreshes more than once, the provider should still wait for
262 // the others before firing the update.
263 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
264 cloud_policy_provider_->OnCacheUpdate(user_policy_cache_.get());
265 Mock::VerifyAndClearExpectations(&observer_);
266
267 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
268 cloud_policy_provider_->OnCacheUpdate(device_policy_cache_);
269 Mock::VerifyAndClearExpectations(&observer_);
270 #endif
271 }
272
273 #if defined(OS_CHROMEOS)
274
275 TEST_F(CloudPolicyProviderTest, DevicePolicy) {
276 ResetProvider();
277 AddUserCache();
278 SetUserCacheReady();
279 AddDeviceCache();
280 device_cache_helper_->MakeEnterpriseDevice();
281
282 // The provider only becomes initialized when all caches are ready, and the
283 // device cache isn't ready yet.
284 EXPECT_FALSE(device_policy_cache_->IsReady());
285 EXPECT_FALSE(cloud_policy_provider_->IsInitializationComplete());
286 PolicyMap policy;
287 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
288 EXPECT_TRUE(policy.empty());
289
290 SetDeviceCacheReady();
291 EXPECT_TRUE(cloud_policy_provider_->IsInitializationComplete());
292 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
293 EXPECT_TRUE(policy.empty());
294
295 const int kRefreshRate = 123;
296 device_cache_helper_->SetRefreshRatePolicy(kRefreshRate);
297 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get()));
298 EXPECT_TRUE(device_cache_helper_->SetPolicy(true));
299 Mock::VerifyAndClearExpectations(&observer_);
300 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
301
302 PolicyMap expected;
303 expected.Set(key::kDevicePolicyRefreshRate,
304 POLICY_LEVEL_MANDATORY,
305 POLICY_SCOPE_MACHINE,
306 Value::CreateIntegerValue(kRefreshRate));
307 EXPECT_TRUE(policy.Equals(expected));
308 }
309
310 TEST_F(CloudPolicyProviderTest, Initialization) {
311 ResetProvider();
312 AddDeviceCache();
313 AddUserCache();
314 device_cache_helper_->MakeEnterpriseDevice();
315
316 // The provider only becomes initialized when all caches are ready.
317 EXPECT_FALSE(user_policy_cache_->IsReady());
318 EXPECT_FALSE(device_policy_cache_->IsReady());
319 EXPECT_FALSE(cloud_policy_provider_->IsInitializationComplete());
320 PolicyMap policy;
321 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
322 EXPECT_TRUE(policy.empty());
323
324 SetDeviceCacheReady();
325 EXPECT_FALSE(cloud_policy_provider_->IsInitializationComplete());
326 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
327 EXPECT_TRUE(policy.empty());
328
329 SetUserCacheReady();
330 EXPECT_TRUE(cloud_policy_provider_->IsInitializationComplete());
331 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
332 EXPECT_TRUE(policy.empty());
333 }
334
335 TEST_F(CloudPolicyProviderTest, MergeProxyPolicies) {
336 ResetProvider();
337 AddDeviceCache();
338 AddUserCache();
339 device_cache_helper_->MakeEnterpriseDevice();
340 SetDeviceCacheReady();
341 SetUserCacheReady();
342 EXPECT_TRUE(cloud_policy_provider_->IsInitializationComplete());
343 PolicyMap policy;
344 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
345 EXPECT_TRUE(policy.empty());
346
347 // User policy takes precedence over device policy.
348 EXPECT_CALL(observer_, OnUpdatePolicy(cloud_policy_provider_.get()));
349 device_cache_helper_->SetProxyPolicy("device mode", "", "", "");
350 EXPECT_TRUE(device_cache_helper_->SetPolicy(true));
351 Mock::VerifyAndClearExpectations(&observer_);
352
353 EXPECT_TRUE(SetProxyUserPolicy("user mode", "", "", ""));
354
355 EXPECT_TRUE(cloud_policy_provider_->Provide(&policy));
356
357 // The deprecated proxy policies are converted to the new ProxySettings.
358 EXPECT_TRUE(policy.Get(key::kProxyMode) == NULL);
359 EXPECT_TRUE(policy.Get(key::kProxyServerMode) == NULL);
360 EXPECT_TRUE(policy.Get(key::kProxyServer) == NULL);
361 EXPECT_TRUE(policy.Get(key::kProxyPacUrl) == NULL);
362
363 EXPECT_EQ(1u, policy.size());
364 const PolicyMap::Entry* entry = policy.Get(key::kProxySettings);
365 ASSERT_TRUE(entry != NULL);
366 ASSERT_TRUE(entry->value != NULL);
367 EXPECT_EQ(POLICY_LEVEL_MANDATORY, entry->level);
368 EXPECT_EQ(POLICY_SCOPE_USER, entry->scope);
369 const DictionaryValue* settings;
370 ASSERT_TRUE(entry->value->GetAsDictionary(&settings));
152 std::string mode; 371 std::string mode;
153 EXPECT_TRUE(settings->GetString(key::kProxyMode, &mode)); 372 EXPECT_TRUE(settings->GetString(key::kProxyMode, &mode));
154 EXPECT_EQ("cache 1", mode); 373 EXPECT_EQ("user mode", mode);
155 374 }
156 base::FundamentalValue expected(true); 375
157 EXPECT_TRUE(base::Value::Equals(&expected, 376 #endif
158 policies.GetValue(key::kShowHomeButton)));
159 EXPECT_TRUE(base::Value::Equals(&expected,
160 policies.GetValue(key::kIncognitoEnabled)));
161 EXPECT_TRUE(base::Value::Equals(&expected,
162 policies.GetValue(key::kTranslateEnabled)));
163 }
164
165 // Combining two PolicyMaps.
166 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsSame) {
167 PolicyMap A, B, C;
168 SetPolicy(&A, key::kHomepageLocation,
169 Value::CreateStringValue("http://www.chromium.org"));
170 SetPolicy(&B, key::kHomepageLocation,
171 Value::CreateStringValue("http://www.google.com"));
172 SetPolicy(&A, key::kApplicationLocaleValue, Value::CreateStringValue("hu"));
173 SetPolicy(&B, key::kApplicationLocaleValue, Value::CreateStringValue("us"));
174 SetPolicy(&A, key::kDevicePolicyRefreshRate, new base::FundamentalValue(100));
175 SetPolicy(&B, key::kDevicePolicyRefreshRate, new base::FundamentalValue(200));
176 CombineTwoPolicyMaps(A, B, &C);
177 EXPECT_TRUE(A.Equals(C));
178 }
179
180 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsEmpty) {
181 PolicyMap A, B, C;
182 CombineTwoPolicyMaps(A, B, &C);
183 EXPECT_TRUE(C.empty());
184 }
185
186 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsPartial) {
187 PolicyMap A, B, C;
188
189 SetPolicy(&A, key::kHomepageLocation,
190 Value::CreateStringValue("http://www.chromium.org"));
191 SetPolicy(&B, key::kHomepageLocation,
192 Value::CreateStringValue("http://www.google.com"));
193 SetPolicy(&B, key::kApplicationLocaleValue, Value::CreateStringValue("us"));
194 SetPolicy(&A, key::kDevicePolicyRefreshRate, new base::FundamentalValue(100));
195 SetPolicy(&B, key::kDevicePolicyRefreshRate, new base::FundamentalValue(200));
196 CombineTwoPolicyMaps(A, B, &C);
197
198 const Value* value;
199 std::string string_value;
200 int int_value;
201 value = C.GetValue(key::kHomepageLocation);
202 ASSERT_TRUE(NULL != value);
203 EXPECT_TRUE(value->GetAsString(&string_value));
204 EXPECT_EQ("http://www.chromium.org", string_value);
205 value = C.GetValue(key::kApplicationLocaleValue);
206 ASSERT_TRUE(NULL != value);
207 EXPECT_TRUE(value->GetAsString(&string_value));
208 EXPECT_EQ("us", string_value);
209 value = C.GetValue(key::kDevicePolicyRefreshRate);
210 ASSERT_TRUE(NULL != value);
211 EXPECT_TRUE(value->GetAsInteger(&int_value));
212 EXPECT_EQ(100, int_value);
213 }
214
215 TEST_F(CloudPolicyProviderTest, CombineTwoPolicyMapsProxies) {
216 const int a_value = 1;
217 const int b_value = -1;
218 PolicyMap A, B, C;
219
220 SetPolicy(&A, key::kProxyMode, Value::CreateIntegerValue(a_value));
221 SetPolicy(&B, key::kProxyServerMode, Value::CreateIntegerValue(b_value));
222 SetPolicy(&B, key::kProxyServer, Value::CreateIntegerValue(b_value));
223 SetPolicy(&B, key::kProxyPacUrl, Value::CreateIntegerValue(b_value));
224 SetPolicy(&B, key::kProxyBypassList, Value::CreateIntegerValue(b_value));
225
226 CombineTwoPolicyMaps(A, B, &C);
227
228 FixDeprecatedPolicies(&A);
229 FixDeprecatedPolicies(&B);
230 EXPECT_TRUE(A.Equals(C));
231 EXPECT_FALSE(B.Equals(C));
232 }
233
234 TEST_F(CloudPolicyProviderTest, RefreshPolicies) {
235 CreateCloudPolicyProvider();
236 MockCloudPolicyCache cache0;
237 MockCloudPolicyCache cache1;
238 MockCloudPolicyCache cache2;
239 MockConfigurationPolicyObserver observer;
240 ConfigurationPolicyObserverRegistrar registrar;
241 registrar.Init(cloud_policy_provider_.get(), &observer);
242
243 // OnUpdatePolicy is called when the provider doesn't have any caches.
244 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
245 cloud_policy_provider_->RefreshPolicies();
246 Mock::VerifyAndClearExpectations(&observer);
247
248 // OnUpdatePolicy is called when all the caches have updated.
249 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(2);
250 cloud_policy_provider_->AppendCache(&cache0);
251 cloud_policy_provider_->AppendCache(&cache1);
252 Mock::VerifyAndClearExpectations(&observer);
253
254 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
255 cloud_policy_provider_->RefreshPolicies();
256 Mock::VerifyAndClearExpectations(&observer);
257
258 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
259 // Updating just one of the caches is not enough.
260 cloud_policy_provider_->OnCacheUpdate(&cache0);
261 Mock::VerifyAndClearExpectations(&observer);
262
263 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
264 // This cache wasn't available when RefreshPolicies was called, so it isn't
265 // required to fire the update.
266 cloud_policy_provider_->AppendCache(&cache2);
267 Mock::VerifyAndClearExpectations(&observer);
268
269 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
270 cloud_policy_provider_->OnCacheUpdate(&cache1);
271 Mock::VerifyAndClearExpectations(&observer);
272
273 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
274 cloud_policy_provider_->RefreshPolicies();
275 Mock::VerifyAndClearExpectations(&observer);
276
277 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
278 cloud_policy_provider_->OnCacheUpdate(&cache0);
279 cloud_policy_provider_->OnCacheUpdate(&cache1);
280 Mock::VerifyAndClearExpectations(&observer);
281
282 // If a cache refreshes more than once, the provider should still wait for
283 // the others before firing the update.
284 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(0);
285 cloud_policy_provider_->OnCacheUpdate(&cache0);
286 Mock::VerifyAndClearExpectations(&observer);
287
288 // Fire updates if one of the required caches goes away while waiting.
289 EXPECT_CALL(observer, OnUpdatePolicy(cloud_policy_provider_.get())).Times(1);
290 cloud_policy_provider_->OnCacheGoingAway(&cache2);
291 Mock::VerifyAndClearExpectations(&observer);
292 }
293 377
294 } // namespace policy 378 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698