OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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/component_cloud_policy_updater.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/files/scoped_temp_dir.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/sha1.h" | |
14 #include "base/test/test_pending_task.h" | |
15 #include "base/test/test_simple_task_runner.h" | |
16 #include "base/time.h" | |
17 #include "chrome/browser/policy/cloud_policy_constants.h" | |
18 #include "chrome/browser/policy/component_cloud_policy_store.h" | |
19 #include "chrome/browser/policy/policy_builder.h" | |
20 #include "chrome/browser/policy/proto/chrome_extension_policy.pb.h" | |
21 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
22 #include "chrome/browser/policy/resource_cache.h" | |
23 #include "net/base/net_errors.h" | |
24 #include "net/url_request/test_url_fetcher_factory.h" | |
25 #include "net/url_request/url_fetcher_delegate.h" | |
26 #include "net/url_request/url_request_context_getter.h" | |
27 #include "testing/gmock/include/gmock/gmock.h" | |
28 #include "testing/gtest/include/gtest/gtest.h" | |
29 | |
30 namespace em = enterprise_management; | |
31 | |
32 using testing::Mock; | |
33 | |
34 namespace policy { | |
35 | |
36 namespace { | |
37 | |
38 const char kTestExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | |
39 const char kTestExtension2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; | |
40 const char kTestDownload[] = "http://example.com/getpolicy?id=123"; | |
41 const char kTestDownload2[] = "http://example.com/getpolicy?id=456"; | |
42 const char kTestDownload3[] = "http://example.com/getpolicy?id=789"; | |
43 const char kTestPolicy[] = | |
44 "{" | |
45 " \"Name\": {" | |
46 " \"Value\": \"disabled\"" | |
47 " }," | |
48 " \"Second\": {" | |
49 " \"Value\": \"maybe\"," | |
50 " \"Level\": \"Recommended\"" | |
51 " }" | |
52 "}"; | |
53 | |
54 class MockComponentCloudPolicyStoreDelegate | |
55 : public ComponentCloudPolicyStore::Delegate { | |
56 public: | |
57 virtual ~MockComponentCloudPolicyStoreDelegate() {} | |
58 | |
59 MOCK_METHOD0(OnComponentCloudPolicyStoreUpdated, void()); | |
60 }; | |
61 | |
62 } // namespace | |
63 | |
64 class ComponentCloudPolicyUpdaterTest : public testing::Test { | |
65 protected: | |
66 virtual void SetUp() OVERRIDE { | |
67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
68 cache_.reset(new ResourceCache(temp_dir_.path())); | |
69 store_.reset(new ComponentCloudPolicyStore(&store_delegate_, cache_.get())); | |
70 store_->SetCredentials(ComponentPolicyBuilder::kFakeUsername, | |
71 ComponentPolicyBuilder::kFakeToken); | |
72 fetcher_factory_.set_remove_fetcher_on_delete(true); | |
73 scoped_refptr<net::URLRequestContextGetter> request_context; | |
74 task_runner_ = new base::TestSimpleTaskRunner(); | |
75 updater_.reset(new ComponentCloudPolicyUpdater( | |
76 task_runner_, request_context, store_.get())); | |
77 ASSERT_FALSE(GetCurrentFetcher()); | |
78 ASSERT_TRUE(store_->policy().begin() == store_->policy().end()); | |
79 | |
80 builder_.policy_data().set_policy_type( | |
81 dm_protocol::kChromeExtensionPolicyType); | |
82 builder_.policy_data().set_settings_entity_id(kTestExtension); | |
83 builder_.payload().set_download_url(kTestDownload); | |
84 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy)); | |
85 | |
86 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | |
87 PolicyMap& policy = expected_bundle_.Get(ns); | |
88 policy.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
89 base::Value::CreateStringValue("disabled")); | |
90 policy.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, | |
91 base::Value::CreateStringValue("maybe")); | |
92 } | |
93 | |
94 net::TestURLFetcher* GetCurrentFetcher() { | |
95 return fetcher_factory_.GetFetcherByID(0); | |
96 } | |
97 | |
98 // Many tests also verify that a second fetcher is scheduled once the first | |
99 // completes, either successfully or with a failure. This helper starts two | |
100 // fetches immediately, so that the second is queued. | |
101 void StartTwoFetches() { | |
102 updater_->UpdateExternalPolicy(CreateResponse()); | |
103 | |
104 builder_.payload().set_download_url(kTestDownload2); | |
105 builder_.policy_data().set_settings_entity_id(kTestExtension2); | |
106 updater_->UpdateExternalPolicy(CreateResponse()); | |
107 } | |
108 | |
109 scoped_ptr<em::PolicyFetchResponse> CreateResponse() { | |
110 builder_.Build(); | |
111 return make_scoped_ptr(new em::PolicyFetchResponse(builder_.policy())); | |
112 } | |
113 | |
114 std::string CreateSerializedResponse() { | |
115 builder_.Build(); | |
116 return builder_.GetBlob(); | |
117 } | |
118 | |
119 base::ScopedTempDir temp_dir_; | |
120 scoped_ptr<ResourceCache> cache_; | |
121 scoped_ptr<ComponentCloudPolicyStore> store_; | |
122 MockComponentCloudPolicyStoreDelegate store_delegate_; | |
123 net::TestURLFetcherFactory fetcher_factory_; | |
124 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
125 scoped_ptr<ComponentCloudPolicyUpdater> updater_; | |
126 ComponentPolicyBuilder builder_; | |
127 PolicyBundle expected_bundle_; | |
128 }; | |
129 | |
130 TEST_F(ComponentCloudPolicyUpdaterTest, FetchAndCache) { | |
131 StartTwoFetches(); | |
132 | |
133 // The first fetch was scheduled; complete it. | |
134 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
135 ASSERT_TRUE(fetcher); | |
136 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
137 fetcher->set_response_code(200); | |
138 fetcher->SetResponseString(kTestPolicy); | |
139 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); | |
140 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
141 Mock::VerifyAndClearExpectations(&store_delegate_); | |
142 | |
143 // The second fetch was scheduled. | |
144 fetcher = GetCurrentFetcher(); | |
145 ASSERT_TRUE(fetcher); | |
146 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); | |
147 | |
148 // The policy is being served. | |
149 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); | |
150 | |
151 // No retries have been scheduled. | |
152 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
153 } | |
154 | |
155 TEST_F(ComponentCloudPolicyUpdaterTest, LargeResponse) { | |
156 std::string long_download("http://example.com/get?id="); | |
157 long_download.append(20 * 1024, '1'); | |
158 builder_.payload().set_download_url(long_download); | |
159 updater_->UpdateExternalPolicy(CreateResponse()); | |
160 | |
161 builder_.policy_data().set_settings_entity_id(kTestExtension2); | |
162 builder_.payload().set_download_url(kTestDownload2); | |
163 updater_->UpdateExternalPolicy(CreateResponse()); | |
164 | |
165 // The first request was dropped, and the second was started. | |
166 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
167 ASSERT_TRUE(fetcher); | |
168 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); | |
169 | |
170 // No retry is scheduled for the first request, because the policy was | |
171 // rejected (too large). | |
172 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
173 } | |
174 | |
175 TEST_F(ComponentCloudPolicyUpdaterTest, InvalidPolicy) { | |
176 builder_.policy_data().set_username("wronguser@example.com"); | |
177 updater_->UpdateExternalPolicy(CreateResponse()); | |
178 | |
179 builder_.policy_data().set_username(ComponentPolicyBuilder::kFakeUsername); | |
180 builder_.policy_data().set_settings_entity_id(kTestExtension2); | |
181 builder_.payload().set_download_url(kTestDownload2); | |
182 updater_->UpdateExternalPolicy(CreateResponse()); | |
183 | |
184 // The first request was dropped, and the second was started. | |
185 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
186 ASSERT_TRUE(fetcher); | |
187 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); | |
188 | |
189 // No retry is scheduled for the first request, because the policy was | |
190 // rejected (invalid). | |
191 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
192 } | |
193 | |
194 TEST_F(ComponentCloudPolicyUpdaterTest, AlreadyCached) { | |
195 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); | |
196 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); | |
197 EXPECT_TRUE(store_->Store(ns, | |
198 CreateSerializedResponse(), | |
199 base::SHA1HashString(kTestPolicy), | |
200 kTestPolicy)); | |
201 Mock::VerifyAndClearExpectations(&store_delegate_); | |
202 | |
203 // Seeing the same policy data again won't trigger a new fetch. | |
204 updater_->UpdateExternalPolicy(CreateResponse()); | |
205 EXPECT_FALSE(GetCurrentFetcher()); | |
206 | |
207 // And no retry is scheduled either. | |
208 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
209 } | |
210 | |
211 TEST_F(ComponentCloudPolicyUpdaterTest, LargeFetch) { | |
212 StartTwoFetches(); | |
213 | |
214 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
215 ASSERT_TRUE(fetcher); | |
216 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
217 fetcher->delegate()->OnURLFetchDownloadProgress(fetcher, 6 * 1024 * 1024, -1); | |
218 | |
219 // The second fetch was scheduled next. | |
220 fetcher = GetCurrentFetcher(); | |
221 ASSERT_TRUE(fetcher); | |
222 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); | |
223 | |
224 // A retry is scheduled for the first fetcher. | |
225 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
226 } | |
227 | |
228 TEST_F(ComponentCloudPolicyUpdaterTest, FetchFailed) { | |
229 StartTwoFetches(); | |
230 | |
231 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
232 ASSERT_TRUE(fetcher); | |
233 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
234 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
235 net::ERR_NETWORK_CHANGED)); | |
236 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
237 | |
238 // The second fetch was scheduled next. | |
239 fetcher = GetCurrentFetcher(); | |
240 ASSERT_TRUE(fetcher); | |
241 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); | |
242 | |
243 // A retry is scheduled for the first fetcher. | |
244 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
245 } | |
246 | |
247 TEST_F(ComponentCloudPolicyUpdaterTest, ServerFailed) { | |
248 StartTwoFetches(); | |
249 | |
250 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
251 ASSERT_TRUE(fetcher); | |
252 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
253 fetcher->set_response_code(500); | |
254 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
255 | |
256 // The second fetch was scheduled next. | |
257 fetcher = GetCurrentFetcher(); | |
258 ASSERT_TRUE(fetcher); | |
259 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); | |
260 | |
261 // A retry is scheduled for the first fetcher. | |
262 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
263 } | |
264 | |
265 TEST_F(ComponentCloudPolicyUpdaterTest, RetryLimit) { | |
266 updater_->UpdateExternalPolicy(CreateResponse()); | |
267 | |
268 // Failing due to client errors retries up to 3 times. | |
269 for (int i = 0; i < 3; ++i) { | |
270 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
271 ASSERT_TRUE(fetcher); | |
272 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
273 | |
274 // Make it fail with a 400 (bad request) client error. | |
275 fetcher->set_response_code(400); | |
276 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
277 | |
278 // A retry is scheduled. | |
279 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
280 // Make it retry immediately. | |
281 task_runner_->RunPendingTasks(); | |
282 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
283 } | |
284 | |
285 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
286 ASSERT_TRUE(fetcher); | |
287 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
288 | |
289 // Make the last retry fail too; it won't retry anymore. | |
290 fetcher->set_response_code(400); | |
291 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
292 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
293 } | |
294 | |
295 TEST_F(ComponentCloudPolicyUpdaterTest, RetryWithBackoff) { | |
296 updater_->UpdateExternalPolicy(CreateResponse()); | |
297 | |
298 base::TimeDelta expected_delay = base::TimeDelta::FromSeconds(60); | |
299 const base::TimeDelta delay_cap = base::TimeDelta::FromHours(12); | |
300 | |
301 // The backoff delay is capped at 12 hours, which is reached after 10 retries: | |
302 // 60 * 2^10 == 61440 > 43200 == 12 * 60 * 60 | |
303 for (int i = 0; i < 20; ++i) { | |
304 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
305 ASSERT_TRUE(fetcher); | |
306 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
307 | |
308 // Make it fail with a 500 server error. | |
309 fetcher->set_response_code(500); | |
310 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
311 | |
312 // A retry is scheduled. The delay is twice the last delay, with random | |
313 // jitter from 80% to 100%. | |
314 ASSERT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
315 const base::TestPendingTask& task = task_runner_->GetPendingTasks().front(); | |
316 EXPECT_GT(task.delay, | |
317 base::TimeDelta::FromMilliseconds( | |
318 0.799 * expected_delay.InMilliseconds())); | |
319 EXPECT_LE(task.delay, expected_delay); | |
320 | |
321 if (i < 10) { | |
322 // The delay cap hasn't been reached yet. | |
323 EXPECT_LT(expected_delay, delay_cap); | |
324 expected_delay *= 2; | |
325 | |
326 if (i == 9) { | |
327 // The last doubling reached the cap. | |
328 EXPECT_GT(expected_delay, delay_cap); | |
329 expected_delay = delay_cap; | |
330 } | |
331 } | |
332 | |
333 // Make it retry immediately. | |
334 task_runner_->RunPendingTasks(); | |
335 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
336 } | |
337 } | |
338 | |
339 TEST_F(ComponentCloudPolicyUpdaterTest, DataValidationFails) { | |
340 StartTwoFetches(); | |
341 | |
342 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
343 ASSERT_TRUE(fetcher); | |
344 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
345 fetcher->set_response_code(200); | |
346 fetcher->SetResponseString("{ won't hash }"); | |
347 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
348 | |
349 // The second fetch was scheduled next. | |
350 fetcher = GetCurrentFetcher(); | |
351 ASSERT_TRUE(fetcher); | |
352 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); | |
353 | |
354 // A retry is scheduled for the first fetcher. | |
355 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); | |
356 } | |
357 | |
358 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedData) { | |
359 updater_->UpdateExternalPolicy(CreateResponse()); | |
360 | |
361 // Same extension, but the download location has changed. | |
362 builder_.payload().set_download_url(kTestDownload2); | |
363 updater_->UpdateExternalPolicy(CreateResponse()); | |
364 | |
365 // The first was cancelled and overridden by the second. | |
366 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
367 ASSERT_TRUE(fetcher); | |
368 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); | |
369 | |
370 // No retries are scheduled. | |
371 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); | |
372 } | |
373 | |
374 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedDataWithoutPolicy) { | |
375 // Fetch the initial policy data. | |
376 updater_->UpdateExternalPolicy(CreateResponse()); | |
377 | |
378 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
379 ASSERT_TRUE(fetcher); | |
380 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
381 fetcher->set_response_code(200); | |
382 fetcher->SetResponseString(kTestPolicy); | |
383 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); | |
384 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
385 Mock::VerifyAndClearExpectations(&store_delegate_); | |
386 EXPECT_FALSE(GetCurrentFetcher()); | |
387 | |
388 // The policy is being served. | |
389 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); | |
390 | |
391 // Same extension, but the download location has changed and is empty, meaning | |
392 // that no policy should be served anymore. | |
393 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); | |
394 builder_.payload().clear_download_url(); | |
395 builder_.payload().clear_secure_hash(); | |
396 updater_->UpdateExternalPolicy(CreateResponse()); | |
397 Mock::VerifyAndClearExpectations(&store_delegate_); | |
398 | |
399 // No fetcher was started for that. | |
400 EXPECT_FALSE(GetCurrentFetcher()); | |
401 | |
402 // And the policy has been removed. | |
403 const PolicyBundle empty_bundle; | |
404 EXPECT_TRUE(store_->policy().Equals(empty_bundle)); | |
405 } | |
406 | |
407 TEST_F(ComponentCloudPolicyUpdaterTest, InvalidatedJob) { | |
408 StartTwoFetches(); | |
409 | |
410 // Start a new fetch for the second extension with a new download URL; the | |
411 // queued job is invalidated. | |
412 builder_.payload().set_download_url(kTestDownload3); | |
413 updater_->UpdateExternalPolicy(CreateResponse()); | |
414 | |
415 // The first request is still pending. | |
416 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
417 ASSERT_TRUE(fetcher); | |
418 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); | |
419 | |
420 // Make it fail. | |
421 fetcher->set_response_code(500); | |
422 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
423 | |
424 // Now the second job was invalidated, and the third job (for the same | |
425 // extension) is the next one. | |
426 fetcher = GetCurrentFetcher(); | |
427 ASSERT_TRUE(fetcher); | |
428 EXPECT_EQ(GURL(kTestDownload3), fetcher->GetOriginalURL()); | |
429 } | |
430 | |
431 TEST_F(ComponentCloudPolicyUpdaterTest, NoPolicy) { | |
432 // Start a fetch with a valid download url. | |
433 updater_->UpdateExternalPolicy(CreateResponse()); | |
434 net::TestURLFetcher* fetcher = GetCurrentFetcher(); | |
435 ASSERT_TRUE(fetcher); | |
436 | |
437 // Now update the policy fetch response before the fetch completes. The new | |
438 // data does not have a download url. | |
439 builder_.payload().Clear(); | |
440 updater_->UpdateExternalPolicy(CreateResponse()); | |
441 | |
442 // The download has been cancelled. | |
443 fetcher = GetCurrentFetcher(); | |
444 ASSERT_FALSE(fetcher); | |
445 } | |
446 | |
447 } // namespace policy | |
OLD | NEW |