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

Side by Side Diff: chrome/browser/policy/cloud/external_policy_data_fetcher_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 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_fetcher.h"
6
7 #include "base/bind.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"
19
20 namespace policy {
21
22 namespace {
23
24 const char* kExternalPolicyDataURLs[] = {
25 "http://localhost/data_1",
26 "http://localhost/data_2"
27 };
28
29 const int64 kExternalPolicyDataMaxSize = 5 * 1024 * 1024; // 5 MB.
30
31 const char* kExternalPolicyDataPayload = "External policy data";
32
33 } // namespace
34
35 class ExternalPolicyDataFetcherTest : public testing::Test {
36 protected:
37 ExternalPolicyDataFetcherTest();
38 virtual ~ExternalPolicyDataFetcherTest();
39
40 // testing::Test:
41 virtual void SetUp() OVERRIDE;
42
43 void StartJob(int index);
44 void CancelJob(int index);
45
46 void OnJobFinished(int job_index,
47 ExternalPolicyDataFetcher::Result result,
48 scoped_ptr<std::string> data);
49 int GetAndResetCallbackCount();
50
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_;
56
57 std::map<int, ExternalPolicyDataFetcher::Job*> jobs_; // Not owned.
58
59 int callback_count_;
60 int callback_job_index_;
61 ExternalPolicyDataFetcher::Result callback_result_;
62 scoped_ptr<std::string> callback_data_;
63
64 DISALLOW_COPY_AND_ASSIGN(ExternalPolicyDataFetcherTest);
65 };
66
67 ExternalPolicyDataFetcherTest::ExternalPolicyDataFetcherTest()
68 : callback_count_(0) {
69 }
70
71 ExternalPolicyDataFetcherTest::~ExternalPolicyDataFetcherTest() {
72 }
73
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(
79 io_task_runner_,
80 scoped_refptr<net::URLRequestContextGetter>()));
81 fetcher_.reset(
82 fetcher_backend_->CreateFrontend(owner_task_runner_).release());
83 }
84
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();
92 }
93
94 void ExternalPolicyDataFetcherTest::CancelJob(int index) {
95 std::map<int, ExternalPolicyDataFetcher::Job*>::iterator it =
96 jobs_.find(index);
97 ASSERT_TRUE(it != jobs_.end());
98 ExternalPolicyDataFetcher::Job* job = it->second;
99 jobs_.erase(it);
100 fetcher_->CancelJob(job);
101 }
102
103 void ExternalPolicyDataFetcherTest::OnJobFinished(
104 int job_index,
105 ExternalPolicyDataFetcher::Result result,
106 scoped_ptr<std::string> data) {
107 ++callback_count_;
108 callback_job_index_ = job_index;
109 callback_result_ = result;
110 callback_data_.reset(data.release());
111 jobs_.erase(job_index);
112 }
113
114 int ExternalPolicyDataFetcherTest::GetAndResetCallbackCount() {
115 const int callback_count = callback_count_;
116 callback_count_ = 0;
117 return callback_count;
118 }
119
120 TEST_F(ExternalPolicyDataFetcherTest, Success) {
121 // Start a fetch job.
122 StartJob(0);
123
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());
128
129 // Complete the fetch.
130 fetcher->set_response_code(200);
131 fetcher->SetResponseString(kExternalPolicyDataPayload);
132 fetcher->delegate()->OnURLFetchComplete(fetcher);
133
134 // Verify that the fetch is no longer running.
135 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
136
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_);
144 }
145
146 TEST_F(ExternalPolicyDataFetcherTest, MaxSizeExceeded) {
147 // Start a fetch job.
148 StartJob(0);
149
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());
154
155 // Indicate that the data size will exceed maximum allowed.
156 fetcher->delegate()->OnURLFetchDownloadProgress(
157 fetcher,
158 kExternalPolicyDataMaxSize + 1,
159 -1);
160
161 // Verify that the fetch is no longer running.
162 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
163
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_);
170 }
171
172 TEST_F(ExternalPolicyDataFetcherTest, ConnectionInterrupted) {
173 // Start a fetch job.
174 StartJob(0);
175
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());
180
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);
185
186 // Verify that the fetch is no longer running.
187 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
188
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,
194 callback_result_);
195 EXPECT_FALSE(callback_data_);
196 }
197
198 TEST_F(ExternalPolicyDataFetcherTest, NetworkError) {
199 // Start a fetch job.
200 StartJob(0);
201
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());
206
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);
211
212 // Verify that the fetch is no longer running.
213 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
214
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_);
221 }
222
223 TEST_F(ExternalPolicyDataFetcherTest, ServerError) {
224 // Start a fetch job.
225 StartJob(0);
226
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());
231
232 // Make the fetch fail with a server error.
233 fetcher->set_response_code(500);
234 fetcher->delegate()->OnURLFetchComplete(fetcher);
235
236 // Verify that the fetch is no longer running.
237 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
238
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_);
245 }
246
247 TEST_F(ExternalPolicyDataFetcherTest, ClientError) {
248 // Start a fetch job.
249 StartJob(0);
250
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());
255
256 // Make the fetch fail with a client error.
257 fetcher->set_response_code(400);
258 fetcher->delegate()->OnURLFetchComplete(fetcher);
259
260 // Verify that the fetch is no longer running.
261 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
262
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_);
269 }
270
271 TEST_F(ExternalPolicyDataFetcherTest, HTTPError) {
272 // Start a fetch job.
273 StartJob(0);
274
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());
279
280 // Make the fetch fail with an HTTP error.
281 fetcher->set_response_code(300);
282 fetcher->delegate()->OnURLFetchComplete(fetcher);
283
284 // Verify that the fetch is no longer running.
285 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
286
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_);
293 }
294
295 TEST_F(ExternalPolicyDataFetcherTest, Canceled) {
296 // Start a fetch job.
297 StartJob(0);
298
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());
303
304 // Cancel the fetch job.
305 CancelJob(0);
306 io_task_runner_->RunUntilIdle();
307
308 // Verify that the fetch is no longer running.
309 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
310
311 // Verify that the callback is not invoked.
312 owner_task_runner_->RunUntilIdle();
313 EXPECT_EQ(0, GetAndResetCallbackCount());
314 }
315
316 TEST_F(ExternalPolicyDataFetcherTest, SuccessfulCanceled) {
317 // Start a fetch job.
318 StartJob(0);
319
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());
324
325 // Complete the fetch.
326 fetcher->set_response_code(200);
327 fetcher->SetResponseString(kExternalPolicyDataPayload);
328 fetcher->delegate()->OnURLFetchComplete(fetcher);
329
330 // Verify that the fetch is no longer running.
331 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
332
333 // Cancel the fetch job before the successful fetch result has arrived from
334 // the backend.
335 CancelJob(0);
336
337 // Verify that the callback is not invoked.
338 owner_task_runner_->RunUntilIdle();
339 EXPECT_EQ(0, GetAndResetCallbackCount());
340 }
341
342 TEST_F(ExternalPolicyDataFetcherTest, ParallelJobs) {
343 // Start two fetch jobs.
344 StartJob(0);
345 StartJob(1);
346
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());
351
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());
356
357 // Complete the first fetch.
358 fetcher->set_response_code(200);
359 fetcher->SetResponseString(kExternalPolicyDataPayload);
360 fetcher->delegate()->OnURLFetchComplete(fetcher);
361
362 // Verify that the first fetch is no longer running.
363 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
364
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_);
372
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());
377
378 // Complete the second fetch.
379 fetcher->set_response_code(200);
380 fetcher->SetResponseString(kExternalPolicyDataPayload);
381 fetcher->delegate()->OnURLFetchComplete(fetcher);
382
383 // Verify that the second fetch is no longer running.
384 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
385
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_);
393 }
394
395 TEST_F(ExternalPolicyDataFetcherTest, ParallelJobsFinishingOutOfOrder) {
396 // Start two fetch jobs.
397 StartJob(0);
398 StartJob(1);
399
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());
404
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());
409
410 // Complete the second fetch.
411 fetcher->set_response_code(200);
412 fetcher->SetResponseString(kExternalPolicyDataPayload);
413 fetcher->delegate()->OnURLFetchComplete(fetcher);
414
415 // Verify that the second fetch is no longer running.
416 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
417
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_);
425
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());
430
431 // Complete the first fetch.
432 fetcher->set_response_code(200);
433 fetcher->SetResponseString(kExternalPolicyDataPayload);
434 fetcher->delegate()->OnURLFetchComplete(fetcher);
435
436 // Verify that the first fetch is no longer running.
437 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
438
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_);
446 }
447
448 TEST_F(ExternalPolicyDataFetcherTest, ParallelJobsWithCancel) {
449 // Start two fetch jobs.
450 StartJob(0);
451 StartJob(1);
452
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());
457
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());
462
463 // Cancel the first fetch job.
464 CancelJob(0);
465 io_task_runner_->RunUntilIdle();
466
467 // Verify that the first fetch is no longer running.
468 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
469
470 // Verify that the callback is not invoked.
471 owner_task_runner_->RunUntilIdle();
472 EXPECT_EQ(0, GetAndResetCallbackCount());
473
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());
478
479 // Complete the second fetch.
480 fetcher->set_response_code(200);
481 fetcher->SetResponseString(kExternalPolicyDataPayload);
482 fetcher->delegate()->OnURLFetchComplete(fetcher);
483
484 // Verify that the second fetch is no longer running.
485 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
486
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_);
494 }
495
496 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698