1 // Copyright 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.
5 #include "components/policy/core/common/cloud/external_policy_data_fetcher.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/test/test_simple_task_runner.h"
12 #include "net/base/net_errors.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_fetcher.h"
15 #include "net/url_request/url_fetcher_delegate.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "net/url_request/url_request_status.h"
18 #include "testing/gtest/include/gtest/gtest.h"
24 const char* kExternalPolicyDataURLs[] = {
25 "http://localhost/data_1",
26 "http://localhost/data_2"
29 const int64 kExternalPolicyDataMaxSize = 5 * 1024 * 1024; // 5 MB.
31 const char* kExternalPolicyDataPayload = "External policy data";
35 class ExternalPolicyDataFetcherTest : public testing::Test {
37 ExternalPolicyDataFetcherTest();
38 ~ExternalPolicyDataFetcherTest() override;
41 void SetUp() override;
43 void StartJob(int index);
44 void CancelJob(int index);
46 void OnJobFinished(int job_index,
47 ExternalPolicyDataFetcher::Result result,
48 scoped_ptr<std::string> data);
49 int GetAndResetCallbackCount();
51 net::TestURLFetcherFactory fetcher_factory_;
52 scoped_refptr<base::TestSimpleTaskRunner> owner_task_runner_;
53 scoped_refptr<base::TestSimpleTaskRunner> io_task_runner_;
54 scoped_ptr<ExternalPolicyDataFetcherBackend> fetcher_backend_;
55 scoped_ptr<ExternalPolicyDataFetcher> fetcher_;
57 std::map<int, ExternalPolicyDataFetcher::Job*> jobs_; // Not owned.
60 int callback_job_index_;
61 ExternalPolicyDataFetcher::Result callback_result_;
62 scoped_ptr<std::string> callback_data_;
64 DISALLOW_COPY_AND_ASSIGN(ExternalPolicyDataFetcherTest);
67 ExternalPolicyDataFetcherTest::ExternalPolicyDataFetcherTest()
68 : callback_count_(0) {
71 ExternalPolicyDataFetcherTest::~ExternalPolicyDataFetcherTest() {
74 void ExternalPolicyDataFetcherTest::SetUp() {
75 fetcher_factory_.set_remove_fetcher_on_delete(true);
76 io_task_runner_ = new base::TestSimpleTaskRunner();
77 owner_task_runner_ = new base::TestSimpleTaskRunner();
78 fetcher_backend_.reset(new ExternalPolicyDataFetcherBackend(
80 scoped_refptr<net::URLRequestContextGetter>()));
82 fetcher_backend_->CreateFrontend(owner_task_runner_).release());
85 void ExternalPolicyDataFetcherTest::StartJob(int index) {
86 jobs_[index] = fetcher_->StartJob(
87 GURL(kExternalPolicyDataURLs[index]),
88 kExternalPolicyDataMaxSize,
89 base::Bind(&ExternalPolicyDataFetcherTest::OnJobFinished,
90 base::Unretained(this), index));
91 io_task_runner_->RunUntilIdle();
94 void ExternalPolicyDataFetcherTest::CancelJob(int index) {
95 std::map<int, ExternalPolicyDataFetcher::Job*>::iterator it =
97 ASSERT_TRUE(it != jobs_.end());
98 ExternalPolicyDataFetcher::Job* job = it->second;
100 fetcher_->CancelJob(job);
103 void ExternalPolicyDataFetcherTest::OnJobFinished(
105 ExternalPolicyDataFetcher::Result result,
106 scoped_ptr<std::string> data) {
108 callback_job_index_ = job_index;
109 callback_result_ = result;
110 callback_data_.reset(data.release());
111 jobs_.erase(job_index);
114 int ExternalPolicyDataFetcherTest::GetAndResetCallbackCount() {
115 const int callback_count = callback_count_;
117 return callback_count;
120 TEST_F(ExternalPolicyDataFetcherTest, Success) {
121 // Start a fetch job.
124 // Verify that the fetch has been started.
125 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
126 ASSERT_TRUE(fetcher);
127 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
129 // Complete the fetch.
130 fetcher->set_response_code(200);
131 fetcher->SetResponseString(kExternalPolicyDataPayload);
132 fetcher->delegate()->OnURLFetchComplete(fetcher);
134 // Verify that the fetch is no longer running.
135 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
137 // Verify that the callback is invoked with the retrieved data.
138 owner_task_runner_->RunUntilIdle();
139 EXPECT_EQ(1, GetAndResetCallbackCount());
140 EXPECT_EQ(0, callback_job_index_);
141 EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
142 ASSERT_TRUE(callback_data_);
143 EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
146 TEST_F(ExternalPolicyDataFetcherTest, MaxSizeExceeded) {
147 // Start a fetch job.
150 // Verify that the fetch has been started.
151 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
152 ASSERT_TRUE(fetcher);
153 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
155 // Indicate that the data size will exceed maximum allowed.
156 fetcher->delegate()->OnURLFetchDownloadProgress(
158 kExternalPolicyDataMaxSize + 1,
161 // Verify that the fetch is no longer running.
162 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
164 // Verify that the callback is invoked with the correct error code.
165 owner_task_runner_->RunUntilIdle();
166 EXPECT_EQ(1, GetAndResetCallbackCount());
167 EXPECT_EQ(0, callback_job_index_);
168 EXPECT_EQ(ExternalPolicyDataFetcher::MAX_SIZE_EXCEEDED, callback_result_);
169 EXPECT_FALSE(callback_data_);
172 TEST_F(ExternalPolicyDataFetcherTest, ConnectionInterrupted) {
173 // Start a fetch job.
176 // Verify that the fetch has been started.
177 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
178 ASSERT_TRUE(fetcher);
179 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
181 // Make the fetch fail due to an interrupted connection.
182 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
183 net::ERR_CONNECTION_RESET));
184 fetcher->delegate()->OnURLFetchComplete(fetcher);
186 // Verify that the fetch is no longer running.
187 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
189 // Verify that the callback is invoked with the correct error code.
190 owner_task_runner_->RunUntilIdle();
191 EXPECT_EQ(1, GetAndResetCallbackCount());
192 EXPECT_EQ(0, callback_job_index_);
193 EXPECT_EQ(ExternalPolicyDataFetcher::CONNECTION_INTERRUPTED,
195 EXPECT_FALSE(callback_data_);
198 TEST_F(ExternalPolicyDataFetcherTest, NetworkError) {
199 // Start a fetch job.
202 // Verify that the fetch has been started.
203 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
204 ASSERT_TRUE(fetcher);
205 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
207 // Make the fetch fail due to a network error.
208 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
209 net::ERR_NETWORK_CHANGED));
210 fetcher->delegate()->OnURLFetchComplete(fetcher);
212 // Verify that the fetch is no longer running.
213 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
215 // Verify that the callback is invoked with the correct error code.
216 owner_task_runner_->RunUntilIdle();
217 EXPECT_EQ(1, GetAndResetCallbackCount());
218 EXPECT_EQ(0, callback_job_index_);
219 EXPECT_EQ(ExternalPolicyDataFetcher::NETWORK_ERROR, callback_result_);
220 EXPECT_FALSE(callback_data_);
223 TEST_F(ExternalPolicyDataFetcherTest, ServerError) {
224 // Start a fetch job.
227 // Verify that the fetch has been started.
228 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
229 ASSERT_TRUE(fetcher);
230 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
232 // Make the fetch fail with a server error.
233 fetcher->set_response_code(500);
234 fetcher->delegate()->OnURLFetchComplete(fetcher);
236 // Verify that the fetch is no longer running.
237 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
239 // Verify that the callback is invoked with the correct error code.
240 owner_task_runner_->RunUntilIdle();
241 EXPECT_EQ(1, GetAndResetCallbackCount());
242 EXPECT_EQ(0, callback_job_index_);
243 EXPECT_EQ(ExternalPolicyDataFetcher::SERVER_ERROR, callback_result_);
244 EXPECT_FALSE(callback_data_);
247 TEST_F(ExternalPolicyDataFetcherTest, ClientError) {
248 // Start a fetch job.
251 // Verify that the fetch has been started.
252 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
253 ASSERT_TRUE(fetcher);
254 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
256 // Make the fetch fail with a client error.
257 fetcher->set_response_code(400);
258 fetcher->delegate()->OnURLFetchComplete(fetcher);
260 // Verify that the fetch is no longer running.
261 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
263 // Verify that the callback is invoked with the correct error code.
264 owner_task_runner_->RunUntilIdle();
265 EXPECT_EQ(1, GetAndResetCallbackCount());
266 EXPECT_EQ(0, callback_job_index_);
267 EXPECT_EQ(ExternalPolicyDataFetcher::CLIENT_ERROR, callback_result_);
268 EXPECT_FALSE(callback_data_);
271 TEST_F(ExternalPolicyDataFetcherTest, HTTPError) {
272 // Start a fetch job.
275 // Verify that the fetch has been started.
276 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
277 ASSERT_TRUE(fetcher);
278 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
280 // Make the fetch fail with an HTTP error.
281 fetcher->set_response_code(300);
282 fetcher->delegate()->OnURLFetchComplete(fetcher);
284 // Verify that the fetch is no longer running.
285 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
287 // Verify that the callback is invoked with the correct error code.
288 owner_task_runner_->RunUntilIdle();
289 EXPECT_EQ(1, GetAndResetCallbackCount());
290 EXPECT_EQ(0, callback_job_index_);
291 EXPECT_EQ(ExternalPolicyDataFetcher::HTTP_ERROR, callback_result_);
292 EXPECT_FALSE(callback_data_);
295 TEST_F(ExternalPolicyDataFetcherTest, Canceled) {
296 // Start a fetch job.
299 // Verify that the fetch has been started.
300 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
301 ASSERT_TRUE(fetcher);
302 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
304 // Cancel the fetch job.
306 io_task_runner_->RunUntilIdle();
308 // Verify that the fetch is no longer running.
309 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
311 // Verify that the callback is not invoked.
312 owner_task_runner_->RunUntilIdle();
313 EXPECT_EQ(0, GetAndResetCallbackCount());
316 TEST_F(ExternalPolicyDataFetcherTest, SuccessfulCanceled) {
317 // Start a fetch job.
320 // Verify that the fetch has been started.
321 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
322 ASSERT_TRUE(fetcher);
323 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
325 // Complete the fetch.
326 fetcher->set_response_code(200);
327 fetcher->SetResponseString(kExternalPolicyDataPayload);
328 fetcher->delegate()->OnURLFetchComplete(fetcher);
330 // Verify that the fetch is no longer running.
331 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
333 // Cancel the fetch job before the successful fetch result has arrived from
337 // Verify that the callback is not invoked.
338 owner_task_runner_->RunUntilIdle();
339 EXPECT_EQ(0, GetAndResetCallbackCount());
342 TEST_F(ExternalPolicyDataFetcherTest, ParallelJobs) {
343 // Start two fetch jobs.
347 // Verify that the second fetch has been started.
348 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
349 ASSERT_TRUE(fetcher);
350 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
352 // Verify that the first fetch has been started.
353 fetcher = fetcher_factory_.GetFetcherByID(0);
354 ASSERT_TRUE(fetcher);
355 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
357 // Complete the first fetch.
358 fetcher->set_response_code(200);
359 fetcher->SetResponseString(kExternalPolicyDataPayload);
360 fetcher->delegate()->OnURLFetchComplete(fetcher);
362 // Verify that the first fetch is no longer running.
363 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
365 // Verify that the callback is invoked with the retrieved data.
366 owner_task_runner_->RunUntilIdle();
367 EXPECT_EQ(1, GetAndResetCallbackCount());
368 EXPECT_EQ(0, callback_job_index_);
369 EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
370 ASSERT_TRUE(callback_data_);
371 EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
373 // Verify that the second fetch is still running.
374 fetcher = fetcher_factory_.GetFetcherByID(1);
375 ASSERT_TRUE(fetcher);
376 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
378 // Complete the second fetch.
379 fetcher->set_response_code(200);
380 fetcher->SetResponseString(kExternalPolicyDataPayload);
381 fetcher->delegate()->OnURLFetchComplete(fetcher);
383 // Verify that the second fetch is no longer running.
384 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
386 // Verify that the callback is invoked with the retrieved data.
387 owner_task_runner_->RunUntilIdle();
388 EXPECT_EQ(1, GetAndResetCallbackCount());
389 EXPECT_EQ(1, callback_job_index_);
390 EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
391 ASSERT_TRUE(callback_data_);
392 EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
395 TEST_F(ExternalPolicyDataFetcherTest, ParallelJobsFinishingOutOfOrder) {
396 // Start two fetch jobs.
400 // Verify that the first fetch has been started.
401 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
402 ASSERT_TRUE(fetcher);
403 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
405 // Verify that the second fetch has been started.
406 fetcher = fetcher_factory_.GetFetcherByID(1);
407 ASSERT_TRUE(fetcher);
408 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
410 // Complete the second fetch.
411 fetcher->set_response_code(200);
412 fetcher->SetResponseString(kExternalPolicyDataPayload);
413 fetcher->delegate()->OnURLFetchComplete(fetcher);
415 // Verify that the second fetch is no longer running.
416 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
418 // Verify that the callback is invoked with the retrieved data.
419 owner_task_runner_->RunUntilIdle();
420 EXPECT_EQ(1, GetAndResetCallbackCount());
421 EXPECT_EQ(1, callback_job_index_);
422 EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
423 ASSERT_TRUE(callback_data_);
424 EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
426 // Verify that the first fetch is still running.
427 fetcher = fetcher_factory_.GetFetcherByID(0);
428 ASSERT_TRUE(fetcher);
429 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
431 // Complete the first fetch.
432 fetcher->set_response_code(200);
433 fetcher->SetResponseString(kExternalPolicyDataPayload);
434 fetcher->delegate()->OnURLFetchComplete(fetcher);
436 // Verify that the first fetch is no longer running.
437 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
439 // Verify that the callback is invoked with the retrieved data.
440 owner_task_runner_->RunUntilIdle();
441 EXPECT_EQ(1, GetAndResetCallbackCount());
442 EXPECT_EQ(0, callback_job_index_);
443 EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
444 ASSERT_TRUE(callback_data_);
445 EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
448 TEST_F(ExternalPolicyDataFetcherTest, ParallelJobsWithCancel) {
449 // Start two fetch jobs.
453 // Verify that the second fetch has been started.
454 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
455 ASSERT_TRUE(fetcher);
456 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
458 // Verify that the first fetch has been started.
459 fetcher = fetcher_factory_.GetFetcherByID(0);
460 ASSERT_TRUE(fetcher);
461 EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
463 // Cancel the first fetch job.
465 io_task_runner_->RunUntilIdle();
467 // Verify that the first fetch is no longer running.
468 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
470 // Verify that the callback is not invoked.
471 owner_task_runner_->RunUntilIdle();
472 EXPECT_EQ(0, GetAndResetCallbackCount());
474 // Verify that the second fetch is still running.
475 fetcher = fetcher_factory_.GetFetcherByID(1);
476 ASSERT_TRUE(fetcher);
477 EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
479 // Complete the second fetch.
480 fetcher->set_response_code(200);
481 fetcher->SetResponseString(kExternalPolicyDataPayload);
482 fetcher->delegate()->OnURLFetchComplete(fetcher);
484 // Verify that the second fetch is no longer running.
485 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
487 // Verify that the callback is invoked with the retrieved data.
488 owner_task_runner_->RunUntilIdle();
489 EXPECT_EQ(1, GetAndResetCallbackCount());
490 EXPECT_EQ(1, callback_job_index_);
491 EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
492 ASSERT_TRUE(callback_data_);
493 EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
496 } // namespace policy