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

Side by Side Diff: chrome/browser/policy/cloud/external_policy_data_updater_unittest.cc

Issue 109743002: Move policy code into components/policy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: moar fixes Created 7 years 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
OLDNEW
(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/cloud/external_policy_data_updater.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/sha1.h"
13 #include "base/test/test_pending_task.h"
14 #include "base/test/test_simple_task_runner.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/policy/cloud/external_policy_data_fetcher.h"
17 #include "net/base/net_errors.h"
18 #include "net/url_request/test_url_fetcher_factory.h"
19 #include "net/url_request/url_fetcher_delegate.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "net/url_request/url_request_status.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "url/gurl.h"
25
26 using testing::Mock;
27 using testing::Return;
28
29 namespace policy {
30
31 namespace {
32
33 const char* kExternalPolicyDataKeys[] = {"external_policy_data_1",
34 "external_policy_data_2",
35 "external_policy_data_3"};
36 const char* kExternalPolicyDataURLs[] = {"http://example.com/data_1",
37 "http://example.com/data_2",
38 "http://example.com/data_3"};
39 const char* kExternalPolicyDataPayload = "External policy data";
40
41 const int64 kExternalPolicyDataMaxSize = 5 * 1024 * 1024; // 5 MB.
42
43 class MockFetchSuccessCallbackListener {
44 public:
45 MOCK_METHOD2(OnFetchSuccess, bool(const std::string&, const std::string&));
46
47 ExternalPolicyDataUpdater::FetchSuccessCallback CreateCallback(
48 const std::string& key);
49 };
50
51 ExternalPolicyDataUpdater::FetchSuccessCallback
52 MockFetchSuccessCallbackListener::CreateCallback(const std::string& key) {
53 return base::Bind(&MockFetchSuccessCallbackListener::OnFetchSuccess,
54 base::Unretained(this),
55 key);
56 }
57
58 } // namespace
59
60 class ExternalPolicyDataUpdaterTest : public testing::Test {
61 protected:
62 virtual void SetUp() OVERRIDE;
63
64 void CreateUpdater(size_t max_parallel_fetches);
65 ExternalPolicyDataUpdater::Request CreateRequest(
66 const std::string& url) const;
67 void RequestExternalDataFetch(int key_index, int url_index);
68 void RequestExternalDataFetch(int index);
69
70 net::TestURLFetcherFactory fetcher_factory_;
71 MockFetchSuccessCallbackListener callback_listener_;
72 scoped_refptr<base::TestSimpleTaskRunner> backend_task_runner_;
73 scoped_refptr<base::TestSimpleTaskRunner> io_task_runner_;
74 scoped_ptr<ExternalPolicyDataFetcherBackend> fetcher_backend_;
75 scoped_ptr<ExternalPolicyDataUpdater> updater_;
76 };
77
78 void ExternalPolicyDataUpdaterTest::SetUp() {
79 fetcher_factory_.set_remove_fetcher_on_delete(true);
80 backend_task_runner_ = new base::TestSimpleTaskRunner();
81 io_task_runner_ = new base::TestSimpleTaskRunner();
82 }
83
84 void ExternalPolicyDataUpdaterTest::CreateUpdater(size_t max_parallel_fetches) {
85 fetcher_backend_.reset(new ExternalPolicyDataFetcherBackend(
86 io_task_runner_,
87 scoped_refptr<net::URLRequestContextGetter>()));
88 updater_.reset(new ExternalPolicyDataUpdater(
89 backend_task_runner_,
90 fetcher_backend_->CreateFrontend(backend_task_runner_),
91 max_parallel_fetches));
92 }
93
94 void ExternalPolicyDataUpdaterTest::RequestExternalDataFetch(int key_index,
95 int url_index) {
96 updater_->FetchExternalData(
97 kExternalPolicyDataKeys[key_index],
98 CreateRequest(kExternalPolicyDataURLs[url_index]),
99 callback_listener_.CreateCallback(kExternalPolicyDataKeys[key_index]));
100 io_task_runner_->RunUntilIdle();
101 backend_task_runner_->RunPendingTasks();
102 }
103
104 void ExternalPolicyDataUpdaterTest::RequestExternalDataFetch(int index) {
105 RequestExternalDataFetch(index, index);
106 }
107
108 ExternalPolicyDataUpdater::Request
109 ExternalPolicyDataUpdaterTest::CreateRequest(const std::string& url) const {
110 return ExternalPolicyDataUpdater::Request(
111 url,
112 base::SHA1HashString(kExternalPolicyDataPayload),
113 kExternalPolicyDataMaxSize);
114 }
115
116 TEST_F(ExternalPolicyDataUpdaterTest, FetchSuccess) {
117 // Create an updater that runs one fetch at a time.
118 CreateUpdater(1);
119
120 // Make two fetch requests.
121 RequestExternalDataFetch(0);
122 RequestExternalDataFetch(1);
123
124 // Verify that the second fetch has not been started yet.
125 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
126
127 // Verify that the first fetch has been started.
128 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
129 ASSERT_TRUE(fetcher);
130 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
131
132 // Complete the first fetch.
133 fetcher->set_response_code(200);
134 fetcher->SetResponseString(kExternalPolicyDataPayload);
135 fetcher->delegate()->OnURLFetchComplete(fetcher);
136
137 // Accept the data when the callback is invoked.
138 EXPECT_CALL(callback_listener_,
139 OnFetchSuccess(kExternalPolicyDataKeys[0],
140 kExternalPolicyDataPayload))
141 .Times(1)
142 .WillOnce(Return(true));
143 backend_task_runner_->RunPendingTasks();
144 Mock::VerifyAndClearExpectations(&callback_listener_);
145 io_task_runner_->RunUntilIdle();
146
147 // Verify that the first fetch is no longer running.
148 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
149
150 // Verify that the second fetch has been started.
151 fetcher = fetcher_factory_.GetFetcherByID(1);
152 ASSERT_TRUE(fetcher);
153 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
154
155 // Verify that no retries have been scheduled.
156 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
157 }
158
159 TEST_F(ExternalPolicyDataUpdaterTest, PayloadSizeExceedsLimit) {
160 // Create an updater that runs one fetch at a time.
161 CreateUpdater(1);
162
163 // Make two fetch requests.
164 RequestExternalDataFetch(0);
165 RequestExternalDataFetch(1);
166
167 // Verify that the second fetch has not been started yet.
168 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
169
170 // Verify that the first fetch has been started.
171 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
172 ASSERT_TRUE(fetcher);
173 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
174
175 // Indicate that the payload size will exceed allowed maximum.
176 fetcher->delegate()->OnURLFetchDownloadProgress(
177 fetcher,
178 kExternalPolicyDataMaxSize + 1,
179 -1);
180 backend_task_runner_->RunPendingTasks();
181 io_task_runner_->RunUntilIdle();
182
183 // Verify that the first fetch is no longer running.
184 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
185
186 // Verify that the second fetch has been started.
187 fetcher = fetcher_factory_.GetFetcherByID(1);
188 ASSERT_TRUE(fetcher);
189 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
190
191 // Verify that a retry has been scheduled for the first fetch.
192 EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
193 }
194
195 TEST_F(ExternalPolicyDataUpdaterTest, FetchFailure) {
196 // Create an updater that runs one fetch at a time.
197 CreateUpdater(1);
198
199 // Make two fetch requests.
200 RequestExternalDataFetch(0);
201 RequestExternalDataFetch(1);
202
203 // Verify that the second fetch has not been started yet.
204 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
205
206 // Verify that the first fetch has been started.
207 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
208 ASSERT_TRUE(fetcher);
209 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
210
211 // Make the first fetch fail due to an interrupted connection.
212 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
213 net::ERR_NETWORK_CHANGED));
214 fetcher->delegate()->OnURLFetchComplete(fetcher);
215 backend_task_runner_->RunPendingTasks();
216 io_task_runner_->RunUntilIdle();
217
218 // Verify that the first fetch is no longer running.
219 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
220
221 // Verify that the second fetch has been started.
222 fetcher = fetcher_factory_.GetFetcherByID(1);
223 ASSERT_TRUE(fetcher);
224 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
225
226 // Verify that a retry has been scheduled for the first fetch.
227 EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
228 }
229
230 TEST_F(ExternalPolicyDataUpdaterTest, ServerFailure) {
231 // Create an updater that runs one fetch at a time.
232 CreateUpdater(1);
233
234 // Make two fetch requests.
235 RequestExternalDataFetch(0);
236 RequestExternalDataFetch(1);
237
238 // Verify that the second fetch has not been started yet.
239 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
240
241 // Verify that the first fetch has been started.
242 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
243 ASSERT_TRUE(fetcher);
244 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
245
246 // Make the first fetch fail with a server error.
247 fetcher->set_response_code(500);
248 fetcher->delegate()->OnURLFetchComplete(fetcher);
249 backend_task_runner_->RunPendingTasks();
250 io_task_runner_->RunUntilIdle();
251
252 // Verify that the first fetch is no longer running.
253 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
254
255 // Verify that the second fetch has been started.
256 fetcher = fetcher_factory_.GetFetcherByID(1);
257 ASSERT_TRUE(fetcher);
258 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
259
260 // Verify that a retry has been scheduled for the first fetch.
261 EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
262 }
263
264 TEST_F(ExternalPolicyDataUpdaterTest, RetryLimit) {
265 // Create an updater that runs one fetch at a time.
266 CreateUpdater(1);
267
268 // Make a fetch request.
269 RequestExternalDataFetch(0);
270
271 int fetcher_id = 0;
272
273 // Verify that client failures cause the fetch to be retried three times.
274 for (int i = 0; i < 3; ++i) {
275 // Verify that the fetch has been (re)started.
276 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(fetcher_id);
277 ASSERT_TRUE(fetcher);
278 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
279
280 // Make the fetch fail with a client error.
281 fetcher->set_response_code(400);
282 fetcher->delegate()->OnURLFetchComplete(fetcher);
283 backend_task_runner_->RunPendingTasks();
284 io_task_runner_->RunUntilIdle();
285
286 // Verify that the fetch is no longer running.
287 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(fetcher_id));
288
289 // Verify that a retry has been scheduled.
290 EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
291
292 // Fast-forward time to the scheduled retry.
293 backend_task_runner_->RunPendingTasks();
294 io_task_runner_->RunUntilIdle();
295 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
296 ++fetcher_id;
297 }
298
299 // Verify that the fetch has been restarted.
300 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(fetcher_id);
301 ASSERT_TRUE(fetcher);
302 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
303
304 // Make the fetch fail once more.
305 fetcher->set_response_code(400);
306 fetcher->delegate()->OnURLFetchComplete(fetcher);
307 backend_task_runner_->RunPendingTasks();
308 io_task_runner_->RunUntilIdle();
309 ++fetcher_id;
310
311 // Verify that the fetch is no longer running.
312 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(fetcher_id));
313
314 // Verify that no further retries have been scheduled.
315 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
316 }
317
318 TEST_F(ExternalPolicyDataUpdaterTest, RetryWithBackoff) {
319 // Create an updater that runs one fetch at a time.
320 CreateUpdater(1);
321
322 // Make a fetch request.
323 RequestExternalDataFetch(0);
324
325 base::TimeDelta expected_delay = base::TimeDelta::FromSeconds(60);
326 const base::TimeDelta delay_cap = base::TimeDelta::FromHours(12);
327
328 int fetcher_id = 0;
329
330 // The backoff delay is capped at 12 hours, which is reached after 10 retries:
331 // 60 * 2^10 == 61440 > 43200 == 12 * 60 * 60
332 for (int i = 0; i < 20; ++i) {
333 // Verify that the fetch has been (re)started.
334 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(fetcher_id);
335 ASSERT_TRUE(fetcher);
336 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
337
338 // Make the fetch fail with a server error.
339 fetcher->set_response_code(500);
340 fetcher->delegate()->OnURLFetchComplete(fetcher);
341 backend_task_runner_->RunPendingTasks();
342 io_task_runner_->RunUntilIdle();
343
344 // Verify that the fetch is no longer running.
345 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(fetcher_id));
346
347 // Verify that a retry has been scheduled.
348 EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
349
350 // Verify that the retry delay has been doubled, with random jitter from 80%
351 // to 100%.
352 const base::TestPendingTask& task =
353 backend_task_runner_->GetPendingTasks().front();
354 EXPECT_GT(task.delay,
355 base::TimeDelta::FromMilliseconds(
356 0.799 * expected_delay.InMilliseconds()));
357 EXPECT_LE(task.delay, expected_delay);
358
359 if (i < 10) {
360 // The delay cap has not been reached yet.
361 EXPECT_LT(expected_delay, delay_cap);
362 expected_delay *= 2;
363
364 if (i == 9) {
365 // The last doubling reached the cap.
366 EXPECT_GT(expected_delay, delay_cap);
367 expected_delay = delay_cap;
368 }
369 }
370
371 // Fast-forward time to the scheduled retry.
372 backend_task_runner_->RunPendingTasks();
373 io_task_runner_->RunUntilIdle();
374 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
375 ++fetcher_id;
376 }
377 }
378
379 TEST_F(ExternalPolicyDataUpdaterTest, HashInvalid) {
380 // Create an updater that runs one fetch at a time.
381 CreateUpdater(1);
382
383 // Make two fetch requests.
384 RequestExternalDataFetch(0);
385 RequestExternalDataFetch(1);
386
387 // Verify that the second fetch has not been started yet.
388 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
389
390 // Verify that the first fetch has been started.
391 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
392 ASSERT_TRUE(fetcher);
393 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
394
395 // Make the first fetch retrieve data whose hash does not match the expected
396 // value.
397 fetcher->set_response_code(200);
398 fetcher->SetResponseString("Invalid external policy data");
399 fetcher->delegate()->OnURLFetchComplete(fetcher);
400 backend_task_runner_->RunPendingTasks();
401 io_task_runner_->RunUntilIdle();
402
403 // Verify that the first fetch is no longer running.
404 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
405
406 // Verify that the second fetch has been started.
407 fetcher = fetcher_factory_.GetFetcherByID(1);
408 ASSERT_TRUE(fetcher);
409 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
410
411 // Verify that a retry has been scheduled for the first fetch.
412 EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
413 }
414
415 TEST_F(ExternalPolicyDataUpdaterTest, DataRejectedByCallback) {
416 // Create an updater that runs one fetch at a time.
417 CreateUpdater(1);
418
419 // Make a fetch request.
420 RequestExternalDataFetch(0);
421
422 // Verify that the fetch has been started.
423 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
424 ASSERT_TRUE(fetcher);
425 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
426
427 // Complete the fetch.
428 fetcher->set_response_code(200);
429 fetcher->SetResponseString(kExternalPolicyDataPayload);
430 fetcher->delegate()->OnURLFetchComplete(fetcher);
431
432 // Reject the data when the callback is invoked.
433 EXPECT_CALL(callback_listener_,
434 OnFetchSuccess(kExternalPolicyDataKeys[0],
435 kExternalPolicyDataPayload))
436 .Times(1)
437 .WillOnce(Return(false));
438 backend_task_runner_->RunPendingTasks();
439 Mock::VerifyAndClearExpectations(&callback_listener_);
440 io_task_runner_->RunUntilIdle();
441
442 // Verify that the fetch is no longer running.
443 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
444
445 // Verify that a retry has been scheduled.
446 EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
447
448 // Fast-forward time to the scheduled retry.
449 backend_task_runner_->RunPendingTasks();
450 io_task_runner_->RunUntilIdle();
451 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
452
453 // Verify that the fetch has been restarted.
454 fetcher = fetcher_factory_.GetFetcherByID(1);
455 ASSERT_TRUE(fetcher);
456 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
457
458 // Complete the fetch.
459 fetcher->set_response_code(200);
460 fetcher->SetResponseString(kExternalPolicyDataPayload);
461 fetcher->delegate()->OnURLFetchComplete(fetcher);
462
463 // Accept the data when the callback is invoked this time.
464 EXPECT_CALL(callback_listener_,
465 OnFetchSuccess(kExternalPolicyDataKeys[0],
466 kExternalPolicyDataPayload))
467 .Times(1)
468 .WillOnce(Return(true));
469 backend_task_runner_->RunPendingTasks();
470 Mock::VerifyAndClearExpectations(&callback_listener_);
471 io_task_runner_->RunUntilIdle();
472
473 // Verify that no retries have been scheduled.
474 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
475 }
476
477 TEST_F(ExternalPolicyDataUpdaterTest, URLChanged) {
478 // Create an updater that runs one fetch at a time.
479 CreateUpdater(1);
480
481 // Make a fetch request.
482 RequestExternalDataFetch(0);
483
484 // Verify that the fetch has been started.
485 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
486 ASSERT_TRUE(fetcher);
487 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
488
489 // Make another fetch request with the same key but an updated URL.
490 RequestExternalDataFetch(0, 1);
491
492 // Verify that the original fetch is no longer running.
493 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
494
495 // Verify that a new fetch has been started with the updated URL.
496 fetcher = fetcher_factory_.GetFetcherByID(1);
497 ASSERT_TRUE(fetcher);
498 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
499
500 // Verify that no retries have been scheduled.
501 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
502 }
503
504 TEST_F(ExternalPolicyDataUpdaterTest, JobInvalidated) {
505 // Create an updater that runs one fetch at a time.
506 CreateUpdater(1);
507
508 // Make two fetch requests.
509 RequestExternalDataFetch(0);
510 RequestExternalDataFetch(1);
511
512 // Verify that the second fetch has not been started yet.
513 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
514
515 // Verify that the first fetch has been started.
516 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
517 ASSERT_TRUE(fetcher);
518 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
519
520 // Make another fetch request with the same key as the second request but an
521 // updated URL.
522 RequestExternalDataFetch(1, 2);
523
524 // Verify that the first fetch is still running.
525 fetcher = fetcher_factory_.GetFetcherByID(0);
526 ASSERT_TRUE(fetcher);
527 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
528
529 // Verify that the second fetch has still not been started.
530 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
531
532 // Make the first fetch fail with a server error.
533 fetcher->set_response_code(500);
534 fetcher->delegate()->OnURLFetchComplete(fetcher);
535 backend_task_runner_->RunPendingTasks();
536 io_task_runner_->RunUntilIdle();
537
538 // Verify that the first fetch is no longer running.
539 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
540
541 // Verify that the second fetch was invalidated and the third fetch has been
542 // started instead.
543 fetcher = fetcher_factory_.GetFetcherByID(1);
544 ASSERT_TRUE(fetcher);
545 EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
546 }
547
548 TEST_F(ExternalPolicyDataUpdaterTest, FetchCanceled) {
549 // Create an updater that runs one fetch at a time.
550 CreateUpdater(1);
551
552 // Make a fetch request.
553 RequestExternalDataFetch(0);
554
555 // Verify that the fetch has been started.
556 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
557 ASSERT_TRUE(fetcher);
558 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
559
560 // Cancel the fetch request.
561 updater_->CancelExternalDataFetch(kExternalPolicyDataKeys[0]);
562 io_task_runner_->RunUntilIdle();
563 backend_task_runner_->RunPendingTasks();
564
565 // Verify that the fetch is no longer running.
566 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
567
568 // Verify that no retries have been scheduled.
569 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
570 }
571
572 TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobs) {
573 // Create an updater that runs up to two fetches in parallel.
574 CreateUpdater(2);
575
576 // Make three fetch requests.
577 RequestExternalDataFetch(0);
578 RequestExternalDataFetch(1);
579 RequestExternalDataFetch(2);
580
581 // Verify that the third fetch has not been started yet.
582 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
583
584 // Verify that the second fetch has been started.
585 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
586 ASSERT_TRUE(fetcher);
587 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
588
589 // Verify that the first fetch has been started.
590 fetcher = fetcher_factory_.GetFetcherByID(0);
591 ASSERT_TRUE(fetcher);
592 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
593
594 // Complete the first fetch.
595 fetcher->set_response_code(200);
596 fetcher->SetResponseString(kExternalPolicyDataPayload);
597 fetcher->delegate()->OnURLFetchComplete(fetcher);
598
599 // Accept the data when the callback is invoked.
600 EXPECT_CALL(callback_listener_,
601 OnFetchSuccess(kExternalPolicyDataKeys[0],
602 kExternalPolicyDataPayload))
603 .Times(1)
604 .WillOnce(Return(true));
605 backend_task_runner_->RunPendingTasks();
606 Mock::VerifyAndClearExpectations(&callback_listener_);
607 io_task_runner_->RunUntilIdle();
608
609 // Verify that the first fetch is no longer running.
610 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
611
612 // Verify that the third fetch has been started.
613 fetcher = fetcher_factory_.GetFetcherByID(2);
614 ASSERT_TRUE(fetcher);
615 EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
616
617 // Verify that the second fetch is still running.
618 fetcher = fetcher_factory_.GetFetcherByID(1);
619 ASSERT_TRUE(fetcher);
620 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
621
622 // Complete the second fetch.
623 fetcher->set_response_code(200);
624 fetcher->SetResponseString(kExternalPolicyDataPayload);
625 fetcher->delegate()->OnURLFetchComplete(fetcher);
626
627 // Accept the data when the callback is invoked.
628 EXPECT_CALL(callback_listener_,
629 OnFetchSuccess(kExternalPolicyDataKeys[1],
630 kExternalPolicyDataPayload))
631 .Times(1)
632 .WillOnce(Return(true));
633 backend_task_runner_->RunPendingTasks();
634 Mock::VerifyAndClearExpectations(&callback_listener_);
635 io_task_runner_->RunUntilIdle();
636
637 // Verify that the second fetch is no longer running.
638 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
639
640 // Verify that the third fetch is still running.
641 fetcher = fetcher_factory_.GetFetcherByID(2);
642 ASSERT_TRUE(fetcher);
643 EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
644
645 // Complete the third fetch.
646 fetcher->set_response_code(200);
647 fetcher->SetResponseString(kExternalPolicyDataPayload);
648 fetcher->delegate()->OnURLFetchComplete(fetcher);
649
650 // Accept the data when the callback is invoked.
651 EXPECT_CALL(callback_listener_,
652 OnFetchSuccess(kExternalPolicyDataKeys[2],
653 kExternalPolicyDataPayload))
654 .Times(1)
655 .WillOnce(Return(true));
656 backend_task_runner_->RunPendingTasks();
657 Mock::VerifyAndClearExpectations(&callback_listener_);
658 io_task_runner_->RunUntilIdle();
659
660 // Verify that the third fetch is no longer running.
661 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
662
663 // Verify that no retries have been scheduled.
664 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
665 }
666
667 TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobsFinishingOutOfOrder) {
668 // Create an updater that runs up to two fetches in parallel.
669 CreateUpdater(2);
670
671 // Make three fetch requests.
672 RequestExternalDataFetch(0);
673 RequestExternalDataFetch(1);
674 RequestExternalDataFetch(2);
675
676 // Verify that the third fetch has not been started yet.
677 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
678
679 // Verify that the first fetch has been started.
680 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
681 ASSERT_TRUE(fetcher);
682 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
683
684 // Verify that the second fetch has been started.
685 fetcher = fetcher_factory_.GetFetcherByID(1);
686 ASSERT_TRUE(fetcher);
687 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
688
689 // Complete the second fetch.
690 fetcher->set_response_code(200);
691 fetcher->SetResponseString(kExternalPolicyDataPayload);
692 fetcher->delegate()->OnURLFetchComplete(fetcher);
693
694 // Accept the data when the callback is invoked.
695 EXPECT_CALL(callback_listener_,
696 OnFetchSuccess(kExternalPolicyDataKeys[1],
697 kExternalPolicyDataPayload))
698 .Times(1)
699 .WillOnce(Return(true));
700 backend_task_runner_->RunPendingTasks();
701 Mock::VerifyAndClearExpectations(&callback_listener_);
702 io_task_runner_->RunUntilIdle();
703
704 // Verify that the second fetch is no longer running.
705 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
706
707 // Verify that the third fetch has been started.
708 fetcher = fetcher_factory_.GetFetcherByID(2);
709 ASSERT_TRUE(fetcher);
710 EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
711
712 // Verify that the first fetch is still running.
713 fetcher = fetcher_factory_.GetFetcherByID(0);
714 ASSERT_TRUE(fetcher);
715 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
716
717 // Complete the first fetch.
718 fetcher->set_response_code(200);
719 fetcher->SetResponseString(kExternalPolicyDataPayload);
720 fetcher->delegate()->OnURLFetchComplete(fetcher);
721
722 // Accept the data when the callback is invoked.
723 EXPECT_CALL(callback_listener_,
724 OnFetchSuccess(kExternalPolicyDataKeys[0],
725 kExternalPolicyDataPayload))
726 .Times(1)
727 .WillOnce(Return(true));
728 backend_task_runner_->RunPendingTasks();
729 Mock::VerifyAndClearExpectations(&callback_listener_);
730 io_task_runner_->RunUntilIdle();
731
732 // Verify that the first fetch is no longer running.
733 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
734
735 // Verify that the third fetch is still running.
736 fetcher = fetcher_factory_.GetFetcherByID(2);
737 ASSERT_TRUE(fetcher);
738 EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
739
740 // Complete the third fetch.
741 fetcher->set_response_code(200);
742 fetcher->SetResponseString(kExternalPolicyDataPayload);
743 fetcher->delegate()->OnURLFetchComplete(fetcher);
744
745 // Accept the data when the callback is invoked.
746 EXPECT_CALL(callback_listener_,
747 OnFetchSuccess(kExternalPolicyDataKeys[2],
748 kExternalPolicyDataPayload))
749 .Times(1)
750 .WillOnce(Return(true));
751 backend_task_runner_->RunPendingTasks();
752 Mock::VerifyAndClearExpectations(&callback_listener_);
753 io_task_runner_->RunUntilIdle();
754
755 // Verify that the third fetch is no longer running.
756 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
757
758 // Verify that no retries have been scheduled.
759 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
760 }
761
762 TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobsWithRetry) {
763 // Create an updater that runs up to two fetches in parallel.
764 CreateUpdater(2);
765
766 // Make three fetch requests.
767 RequestExternalDataFetch(0);
768 RequestExternalDataFetch(1);
769 RequestExternalDataFetch(2);
770
771 // Verify that the third fetch has not been started yet.
772 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
773
774 // Verify that the second fetch has been started.
775 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
776 ASSERT_TRUE(fetcher);
777 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
778
779 // Verify that the first fetch has been started.
780 fetcher = fetcher_factory_.GetFetcherByID(0);
781 ASSERT_TRUE(fetcher);
782 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
783
784 // Make the first fetch fail with a client error.
785 fetcher->set_response_code(400);
786 fetcher->delegate()->OnURLFetchComplete(fetcher);
787 backend_task_runner_->RunPendingTasks();
788 io_task_runner_->RunUntilIdle();
789
790 // Verify that the first fetch is no longer running.
791 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
792
793 // Verify that the third fetch has been started.
794 fetcher = fetcher_factory_.GetFetcherByID(2);
795 ASSERT_TRUE(fetcher);
796 EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
797
798 // Verify that a retry has been scheduled for the first fetch.
799 EXPECT_EQ(1u, backend_task_runner_->GetPendingTasks().size());
800
801 // Fast-forward time to the scheduled retry.
802 backend_task_runner_->RunPendingTasks();
803 io_task_runner_->RunUntilIdle();
804 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
805
806 // Verify that the first fetch has not been restarted yet.
807 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(3));
808
809 // Complete the third fetch.
810 fetcher->set_response_code(200);
811 fetcher->SetResponseString(kExternalPolicyDataPayload);
812 fetcher->delegate()->OnURLFetchComplete(fetcher);
813
814 // Accept the data when the callback is invoked.
815 EXPECT_CALL(callback_listener_,
816 OnFetchSuccess(kExternalPolicyDataKeys[2],
817 kExternalPolicyDataPayload))
818 .Times(1)
819 .WillOnce(Return(true));
820 backend_task_runner_->RunPendingTasks();
821 Mock::VerifyAndClearExpectations(&callback_listener_);
822 io_task_runner_->RunUntilIdle();
823
824 // Verify that the third fetch is no longer running.
825 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
826
827 // Verify that the second fetch is still running
828 fetcher = fetcher_factory_.GetFetcherByID(1);
829 ASSERT_TRUE(fetcher);
830 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
831
832 // Verify that the first fetch has been restarted.
833 fetcher = fetcher_factory_.GetFetcherByID(3);
834 ASSERT_TRUE(fetcher);
835 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
836
837 // Verify that no further retries have been scheduled.
838 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
839 }
840
841 TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobsWithCancel) {
842 // Create an updater that runs up to two fetches in parallel.
843 CreateUpdater(2);
844
845 // Make three fetch requests.
846 RequestExternalDataFetch(0);
847 RequestExternalDataFetch(1);
848 RequestExternalDataFetch(2);
849
850 // Verify that the third fetch has not been started yet.
851 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
852
853 // Verify that the second fetch has been started.
854 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
855 ASSERT_TRUE(fetcher);
856 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
857
858 // Verify that the first fetch has been started.
859 fetcher = fetcher_factory_.GetFetcherByID(0);
860 ASSERT_TRUE(fetcher);
861 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
862
863 // Cancel the fetch request.
864 updater_->CancelExternalDataFetch(kExternalPolicyDataKeys[0]);
865 io_task_runner_->RunUntilIdle();
866 backend_task_runner_->RunPendingTasks();
867
868 // Verify that the fetch is no longer running.
869 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
870
871 // Verify that the third fetch has been started.
872 fetcher = fetcher_factory_.GetFetcherByID(2);
873 ASSERT_TRUE(fetcher);
874 EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
875
876 // Verify that the second fetch is still running.
877 fetcher = fetcher_factory_.GetFetcherByID(1);
878 ASSERT_TRUE(fetcher);
879 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
880
881 // Complete the second fetch.
882 fetcher->set_response_code(200);
883 fetcher->SetResponseString(kExternalPolicyDataPayload);
884 fetcher->delegate()->OnURLFetchComplete(fetcher);
885
886 // Accept the data when the callback is invoked.
887 EXPECT_CALL(callback_listener_,
888 OnFetchSuccess(kExternalPolicyDataKeys[1],
889 kExternalPolicyDataPayload))
890 .Times(1)
891 .WillOnce(Return(true));
892 backend_task_runner_->RunPendingTasks();
893 Mock::VerifyAndClearExpectations(&callback_listener_);
894 io_task_runner_->RunUntilIdle();
895
896 // Verify that the second fetch is no longer running.
897 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
898
899 // Verify that the third fetch is still running.
900 fetcher = fetcher_factory_.GetFetcherByID(2);
901 ASSERT_TRUE(fetcher);
902 EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
903
904 // Complete the third fetch.
905 fetcher->set_response_code(200);
906 fetcher->SetResponseString(kExternalPolicyDataPayload);
907 fetcher->delegate()->OnURLFetchComplete(fetcher);
908
909 // Accept the data when the callback is invoked.
910 EXPECT_CALL(callback_listener_,
911 OnFetchSuccess(kExternalPolicyDataKeys[2],
912 kExternalPolicyDataPayload))
913 .Times(1)
914 .WillOnce(Return(true));
915 backend_task_runner_->RunPendingTasks();
916 Mock::VerifyAndClearExpectations(&callback_listener_);
917 io_task_runner_->RunUntilIdle();
918
919 // Verify that the third fetch is no longer running.
920 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(2));
921
922 // Verify that no retries have been scheduled.
923 EXPECT_TRUE(backend_task_runner_->GetPendingTasks().empty());
924 }
925
926 TEST_F(ExternalPolicyDataUpdaterTest, ParallelJobsWithInvalidatedJob) {
927 // Create an updater that runs up to two fetches in parallel.
928 CreateUpdater(2);
929
930 // Make two fetch requests.
931 RequestExternalDataFetch(0);
932 RequestExternalDataFetch(1);
933
934 // Verify that the first fetch has been started.
935 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
936 ASSERT_TRUE(fetcher);
937 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
938
939 // Verify that the second fetch has been started.
940 fetcher = fetcher_factory_.GetFetcherByID(1);
941 ASSERT_TRUE(fetcher);
942 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
943
944 // Make another fetch request with the same key as the second request but an
945 // updated URL.
946 RequestExternalDataFetch(1, 2);
947
948 // Verify that the first fetch is still running.
949 fetcher = fetcher_factory_.GetFetcherByID(0);
950 ASSERT_TRUE(fetcher);
951 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
952
953 // Verify that the second fetch is no longer running.
954 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
955
956 // Verify that the third fetch has been started.
957 fetcher = fetcher_factory_.GetFetcherByID(2);
958 ASSERT_TRUE(fetcher);
959 EXPECT_EQ(GURL(kExternalPolicyDataURLs[2]), fetcher->GetOriginalURL());
960 }
961
962 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud/external_policy_data_updater.cc ('k') | chrome/browser/policy/cloud/message_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698