File: cloud_external_data_manager_base_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (779 lines) | stat: -rw-r--r-- 32,392 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/policy/cloud_external_data_manager_base.h"

#include <map>
#include <string>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/test_simple_task_runner.h"
#include "base/values.h"
#include "chrome/browser/chromeos/policy/cloud_external_data_store.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "components/policy/core/common/cloud/resource_cache.h"
#include "components/policy/core/common/external_data_fetcher.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_test_utils.h"
#include "components/policy/core/common/policy_types.h"
#include "crypto/sha2.h"
#include "net/http/http_status_code.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_status.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace policy {

namespace {

// A string policy.
const char kStringPolicy[] = "StringPolicy";
// A policy that may reference up to 10 bytes of external data.
const char k10BytePolicy[] = "10BytePolicy";
// A policy that may reference up to 20 bytes of external data.
const char k20BytePolicy[] = "20BytePolicy";
// A nonexistent policy.
const char kUnknownPolicy[] = "UnknownPolicy";

const char k10BytePolicyURL[] = "http://localhost/10_bytes";
const char k20BytePolicyURL[] = "http://localhost/20_bytes";

const char k10ByteData[] = "10 bytes..";
const char k20ByteData[] = "20 bytes............";

const PolicyDetails kPolicyDetails[] = {
//  is_deprecated  is_device_policy  id    max_external_data_size
  { false,         false,             1,                        0 },
  { false,         false,             2,                       10 },
  { false,         false,             3,                       20 },
};

const char kCacheKey[] = "data";

// A variant of net::FakeURLFetcherFactory that makes it an error to request a
// fetcher for an unknown URL.
class FakeURLFetcherFactory : public net::FakeURLFetcherFactory {
 public:
  FakeURLFetcherFactory();
  virtual ~FakeURLFetcherFactory();

  // net::FakeURLFetcherFactory:
  virtual net::URLFetcher* CreateURLFetcher(
      int id,
      const GURL& url,
      net::URLFetcher::RequestType request_type,
      net::URLFetcherDelegate* delegate) override;

 private:
  DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory);
};

FakeURLFetcherFactory::FakeURLFetcherFactory()
    : net::FakeURLFetcherFactory(NULL) {
}

FakeURLFetcherFactory::~FakeURLFetcherFactory() {
}

net::URLFetcher* FakeURLFetcherFactory::CreateURLFetcher(
    int id,
    const GURL& url,
    net::URLFetcher::RequestType request_type,
    net::URLFetcherDelegate* delegate) {
  net::URLFetcher* fetcher = net::FakeURLFetcherFactory::CreateURLFetcher(
      id, url, request_type, delegate);
  EXPECT_TRUE(fetcher);
  return fetcher;
}

}  // namespace

class CloudExternalDataManagerBaseTest : public testing::Test {
 protected:
  CloudExternalDataManagerBaseTest();

  virtual void SetUp() override;
  virtual void TearDown() override;

  void SetUpExternalDataManager();

  scoped_ptr<base::DictionaryValue> ConstructMetadata(const std::string& url,
                                                      const std::string& hash);
  void SetExternalDataReference(const std::string& policy,
                                scoped_ptr<base::DictionaryValue> metadata);

  ExternalDataFetcher::FetchCallback ConstructFetchCallback(int id);
  void ResetCallbackData();

  void OnFetchDone(int id, scoped_ptr<std::string> data);

  void FetchAll();

  void SetFakeResponse(const std::string& url,
                       const std::string& repsonse_data,
                       net::HttpStatusCode response_code,
                       net::URLRequestStatus::Status status);

  base::MessageLoop message_loop_;
  base::ScopedTempDir temp_dir_;
  scoped_ptr<ResourceCache> resource_cache_;
  MockCloudPolicyStore cloud_policy_store_;
  scoped_refptr<net::TestURLRequestContextGetter> request_content_getter_;
  FakeURLFetcherFactory fetcher_factory_;

  scoped_ptr<CloudExternalDataManagerBase> external_data_manager_;

  std::map<int, std::string*> callback_data_;
  PolicyDetailsMap policy_details_;

 private:
  DISALLOW_COPY_AND_ASSIGN(CloudExternalDataManagerBaseTest);
};

CloudExternalDataManagerBaseTest::CloudExternalDataManagerBaseTest() {
}

void CloudExternalDataManagerBaseTest::SetUp() {
  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  resource_cache_.reset(new ResourceCache(temp_dir_.path(),
                                          message_loop_.message_loop_proxy()));
  SetUpExternalDataManager();

  // Set |kStringPolicy| to a string value.
  cloud_policy_store_.policy_map_.Set(kStringPolicy,
                                      POLICY_LEVEL_MANDATORY,
                                      POLICY_SCOPE_USER,
                                      new base::StringValue(std::string()),
                                      NULL);
  // Make |k10BytePolicy| reference 10 bytes of external data.
  SetExternalDataReference(
      k10BytePolicy,
      ConstructMetadata(k10BytePolicyURL,
                        crypto::SHA256HashString(k10ByteData)));
  // Make |k20BytePolicy| reference 20 bytes of external data.
  SetExternalDataReference(
      k20BytePolicy,
      ConstructMetadata(k20BytePolicyURL,
                        crypto::SHA256HashString(k20ByteData)));
  cloud_policy_store_.NotifyStoreLoaded();

  request_content_getter_ = new net::TestURLRequestContextGetter(
      base::MessageLoopProxy::current());

  policy_details_.SetDetails(kStringPolicy, &kPolicyDetails[0]);
  policy_details_.SetDetails(k10BytePolicy, &kPolicyDetails[1]);
  policy_details_.SetDetails(k20BytePolicy, &kPolicyDetails[2]);
}

void CloudExternalDataManagerBaseTest::TearDown() {
  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();
  ResetCallbackData();
}

void CloudExternalDataManagerBaseTest::SetUpExternalDataManager() {
  external_data_manager_.reset(new CloudExternalDataManagerBase(
      policy_details_.GetCallback(),
      message_loop_.message_loop_proxy(),
      message_loop_.message_loop_proxy()));
  external_data_manager_->SetExternalDataStore(make_scoped_ptr(
      new CloudExternalDataStore(kCacheKey,
                                 message_loop_.message_loop_proxy(),
                                 resource_cache_.get())));
  external_data_manager_->SetPolicyStore(&cloud_policy_store_);
}

scoped_ptr<base::DictionaryValue>
    CloudExternalDataManagerBaseTest::ConstructMetadata(
        const std::string& url,
        const std::string& hash) {
  scoped_ptr<base::DictionaryValue> metadata(new base::DictionaryValue);
  metadata->SetStringWithoutPathExpansion("url", url);
  metadata->SetStringWithoutPathExpansion("hash", base::HexEncode(hash.c_str(),
                                                                  hash.size()));
  return metadata.Pass();
}

void CloudExternalDataManagerBaseTest::SetExternalDataReference(
    const std::string& policy,
    scoped_ptr<base::DictionaryValue> metadata) {
  cloud_policy_store_.policy_map_.Set(
      policy,
      POLICY_LEVEL_MANDATORY,
      POLICY_SCOPE_USER,
      metadata.release(),
      new ExternalDataFetcher(
          external_data_manager_->weak_factory_.GetWeakPtr(), policy));
}

ExternalDataFetcher::FetchCallback
CloudExternalDataManagerBaseTest::ConstructFetchCallback(int id) {
  return base::Bind(&CloudExternalDataManagerBaseTest::OnFetchDone,
                    base::Unretained(this),
                    id);
}

void CloudExternalDataManagerBaseTest::ResetCallbackData() {
  STLDeleteValues(&callback_data_);
}

void CloudExternalDataManagerBaseTest::OnFetchDone(
    int id,
    scoped_ptr<std::string> data) {
  delete callback_data_[id];
  callback_data_[id] = data.release();
}

void CloudExternalDataManagerBaseTest::FetchAll() {
  external_data_manager_->FetchAll();
}

void CloudExternalDataManagerBaseTest::SetFakeResponse(
    const std::string& url,
    const std::string& response_data,
    net::HttpStatusCode response_code,
    net::URLRequestStatus::Status status) {
  fetcher_factory_.SetFakeResponse(
      GURL(url), response_data, response_code, status);
}

// Verifies that when no valid external data reference has been set for a
// policy, the attempt to retrieve the external data fails immediately.
TEST_F(CloudExternalDataManagerBaseTest, FailToFetchInvalid) {
  external_data_manager_->Connect(request_content_getter_);

  // Attempt to retrieve external data for |kStringPolicy|, which is a string
  // policy that does not reference any external data.
  external_data_manager_->Fetch(kStringPolicy, ConstructFetchCallback(0));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  EXPECT_TRUE(callback_data_.find(0) != callback_data_.end());
  EXPECT_FALSE(callback_data_[0]);
  ResetCallbackData();

  // Attempt to retrieve external data for |kUnknownPolicy|, which is not a
  // known policy.
  external_data_manager_->Fetch(kUnknownPolicy, ConstructFetchCallback(1));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  EXPECT_TRUE(callback_data_.find(1) != callback_data_.end());
  EXPECT_FALSE(callback_data_[1]);
  ResetCallbackData();

  // Set an invalid external data reference for |k10BytePolicy|.
  SetExternalDataReference(k10BytePolicy,
                           ConstructMetadata(std::string(), std::string()));
  cloud_policy_store_.NotifyStoreLoaded();

  // Attempt to retrieve external data for |k10BytePolicy|, which now has an
  // invalid reference.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(2));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  EXPECT_TRUE(callback_data_.find(2) != callback_data_.end());
  EXPECT_FALSE(callback_data_[2]);
  ResetCallbackData();
}

// Verifies that external data referenced by a policy is downloaded and cached
// when first requested. Subsequent requests are served from the cache without
// further download attempts.
TEST_F(CloudExternalDataManagerBaseTest, DownloadAndCache) {
  // Serve valid external data for |k10BytePolicy|.
  SetFakeResponse(k10BytePolicyURL, k10ByteData, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);
  external_data_manager_->Connect(request_content_getter_);

  // Retrieve external data for |k10BytePolicy|. Verify that a download happens
  // and the callback is invoked with the downloaded data.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(0));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  ASSERT_TRUE(callback_data_[0]);
  EXPECT_EQ(k10ByteData, *callback_data_[0]);
  ResetCallbackData();

  // Stop serving external data for |k10BytePolicy|.
  fetcher_factory_.ClearFakeResponses();

  // Retrieve external data for |k10BytePolicy| again. Verify that no download
  // is attempted but the callback is still invoked with the expected data,
  // served from the cache.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(1));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  ASSERT_TRUE(callback_data_[1]);
  EXPECT_EQ(k10ByteData, *callback_data_[1]);
  ResetCallbackData();

  // Explicitly tell the external_data_manager_ to not make any download
  // attempts.
  external_data_manager_->Disconnect();

  // Retrieve external data for |k10BytePolicy| again. Verify that even though
  // downloads are not allowed, the callback is still invoked with the expected
  // data, served from the cache.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(2));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  ASSERT_TRUE(callback_data_[2]);
  EXPECT_EQ(k10ByteData, *callback_data_[2]);
  ResetCallbackData();

  // Verify that the downloaded data is present in the cache.
  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();
  std::string data;
  EXPECT_TRUE(CloudExternalDataStore(kCacheKey,
                                     message_loop_.message_loop_proxy(),
                                     resource_cache_.get()).Load(
      k10BytePolicy, crypto::SHA256HashString(k10ByteData), 10, &data));
  EXPECT_EQ(k10ByteData, data);
}

// Verifies that a request to download and cache all external data referenced by
// policies is carried out correctly. Subsequent requests for the data are
// served from the cache without further download attempts.
TEST_F(CloudExternalDataManagerBaseTest, DownloadAndCacheAll) {
  // Serve valid external data for |k10BytePolicy| and |k20BytePolicy|.
  SetFakeResponse(k10BytePolicyURL, k10ByteData, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);
  SetFakeResponse(k20BytePolicyURL, k20ByteData, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);
  external_data_manager_->Connect(request_content_getter_);

  // Request that external data referenced by all policies be downloaded.
  FetchAll();
  base::RunLoop().RunUntilIdle();

  // Stop serving external data for |k10BytePolicy| and |k20BytePolicy|.
  fetcher_factory_.ClearFakeResponses();

  // Retrieve external data for |k10BytePolicy| and |k20BytePolicy|. Verify that
  // no downloads are attempted but the callbacks are still invoked with the
  // expected data, served from the cache.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(0));
  external_data_manager_->Fetch(k20BytePolicy, ConstructFetchCallback(1));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(2u, callback_data_.size());
  ASSERT_TRUE(callback_data_[0]);
  EXPECT_EQ(k10ByteData, *callback_data_[0]);
  ASSERT_TRUE(callback_data_[1]);
  EXPECT_EQ(k20ByteData, *callback_data_[1]);
  ResetCallbackData();

  // Explicitly tell the external_data_manager_ to not make any download
  // attempts.
  external_data_manager_->Disconnect();

  // Retrieve external data for |k10BytePolicy| and |k20BytePolicy|. Verify that
  // even though downloads are not allowed, the callbacks are still invoked with
  // the expected data, served from the cache.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(2));
  external_data_manager_->Fetch(k20BytePolicy, ConstructFetchCallback(3));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(2u, callback_data_.size());
  ASSERT_TRUE(callback_data_[2]);
  EXPECT_EQ(k10ByteData, *callback_data_[2]);
  ASSERT_TRUE(callback_data_[3]);
  EXPECT_EQ(k20ByteData, *callback_data_[3]);
  ResetCallbackData();

  // Verify that the downloaded data is present in the cache.
  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();
  CloudExternalDataStore cache(kCacheKey,
                               message_loop_.message_loop_proxy(),
                               resource_cache_.get());
  std::string data;
  EXPECT_TRUE(cache.Load(k10BytePolicy,
                         crypto::SHA256HashString(k10ByteData),
                         10,
                         &data));
  EXPECT_EQ(k10ByteData, data);
  EXPECT_TRUE(cache.Load(k20BytePolicy,
                         crypto::SHA256HashString(k20ByteData),
                         20,
                         &data));
  EXPECT_EQ(k20ByteData, data);
}

// Verifies that when the external data referenced by a policy is not present in
// the cache and downloads are not allowed, a request to retrieve the data is
// enqueued and carried out when downloads become possible.
TEST_F(CloudExternalDataManagerBaseTest, DownloadAfterConnect) {
  // Attempt to retrieve external data for |k10BytePolicy|. Verify that the
  // callback is not invoked as the request remains pending.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(0));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(callback_data_.empty());
  ResetCallbackData();

  // Serve valid external data for |k10BytePolicy| and allow the
  // external_data_manager_ to perform downloads.
  SetFakeResponse(k10BytePolicyURL, k10ByteData, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);
  external_data_manager_->Connect(request_content_getter_);

  // Verify that a download happens and the callback is invoked with the
  // downloaded data.
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  ASSERT_TRUE(callback_data_[0]);
  EXPECT_EQ(k10ByteData, *callback_data_[0]);
  ResetCallbackData();
}

// Verifies that when the external data referenced by a policy is not present in
// the cache and cannot be downloaded at this time, a request to retrieve the
// data is enqueued to be retried later.
TEST_F(CloudExternalDataManagerBaseTest, DownloadError) {
  // Make attempts to download the external data for |k20BytePolicy| fail with
  // an error.
  SetFakeResponse(k20BytePolicyURL, std::string(),
                  net::HTTP_INTERNAL_SERVER_ERROR,
                  net::URLRequestStatus::FAILED);
  external_data_manager_->Connect(request_content_getter_);

  // Attempt to retrieve external data for |k20BytePolicy|. Verify that the
  // callback is not invoked as the download attempt fails and the request
  // remains pending.
  external_data_manager_->Fetch(k20BytePolicy, ConstructFetchCallback(0));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(callback_data_.empty());
  ResetCallbackData();

  // Modify the external data reference for |k20BytePolicy|, allowing the
  // download to be retried immediately.
  SetExternalDataReference(
      k20BytePolicy,
      ConstructMetadata(k20BytePolicyURL,
                        crypto::SHA256HashString(k10ByteData)));
  cloud_policy_store_.NotifyStoreLoaded();

  // Attempt to retrieve external data for |k20BytePolicy| again. Verify that
  // no callback is invoked still as the download attempt fails again and the
  // request remains pending.
  external_data_manager_->Fetch(k20BytePolicy, ConstructFetchCallback(1));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(callback_data_.empty());
  ResetCallbackData();

  // Modify the external data reference for |k20BytePolicy|, allowing the
  // download to be retried immediately.
  SetExternalDataReference(
      k20BytePolicy,
      ConstructMetadata(k20BytePolicyURL,
                        crypto::SHA256HashString(k20ByteData)));
  cloud_policy_store_.NotifyStoreLoaded();

  // Serve external data for |k20BytePolicy| that does not match the hash
  // specified in its current external data reference.
  SetFakeResponse(k20BytePolicyURL, k10ByteData, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);

  // Attempt to retrieve external data for |k20BytePolicy| again. Verify that
  // no callback is invoked still as the downloaded succeeds but returns data
  // that does not match the external data reference.
  external_data_manager_->Fetch(k20BytePolicy, ConstructFetchCallback(2));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(callback_data_.empty());
  ResetCallbackData();

  // Modify the external data reference for |k20BytePolicy|, allowing the
  // download to be retried immediately. The external data reference now matches
  // the data being served.
  SetExternalDataReference(
      k20BytePolicy,
      ConstructMetadata(k20BytePolicyURL,
                        crypto::SHA256HashString(k10ByteData)));
  cloud_policy_store_.NotifyStoreLoaded();

  // Attempt to retrieve external data for |k20BytePolicy| again. Verify that
  // the current callback and the three previously enqueued callbacks are
  // invoked with the downloaded data now.
  external_data_manager_->Fetch(k20BytePolicy, ConstructFetchCallback(3));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(4u, callback_data_.size());
  ASSERT_TRUE(callback_data_[0]);
  EXPECT_EQ(k10ByteData, *callback_data_[0]);
  ASSERT_TRUE(callback_data_[1]);
  EXPECT_EQ(k10ByteData, *callback_data_[1]);
  ASSERT_TRUE(callback_data_[2]);
  EXPECT_EQ(k10ByteData, *callback_data_[2]);
  ASSERT_TRUE(callback_data_[3]);
  EXPECT_EQ(k10ByteData, *callback_data_[3]);
  ResetCallbackData();
}

// Verifies that when the external data referenced by a policy is present in the
// cache, a request to retrieve it is served from the cache without any download
// attempts.
TEST_F(CloudExternalDataManagerBaseTest, LoadFromCache) {
  // Store valid external data for |k10BytePolicy| in the cache.
  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(CloudExternalDataStore(kCacheKey,
                                     message_loop_.message_loop_proxy(),
                                     resource_cache_.get()).Store(
      k10BytePolicy, crypto::SHA256HashString(k10ByteData), k10ByteData));

  // Instantiate an external_data_manager_ that uses the primed cache.
  SetUpExternalDataManager();
  external_data_manager_->Connect(request_content_getter_);

  // Retrieve external data for |k10BytePolicy|. Verify that no download is
  // attempted but the callback is still invoked with the expected data, served
  // from the cache.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(0));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  ASSERT_TRUE(callback_data_[0]);
  EXPECT_EQ(k10ByteData, *callback_data_[0]);
  ResetCallbackData();
}

// Verifies that cache entries which do not correspond to the external data
// referenced by any policy are pruned on startup.
TEST_F(CloudExternalDataManagerBaseTest, PruneCacheOnStartup) {
  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();
  scoped_ptr<CloudExternalDataStore>
      cache(new CloudExternalDataStore(kCacheKey,
                                       message_loop_.message_loop_proxy(),
                                       resource_cache_.get()));
  // Store valid external data for |k10BytePolicy| in the cache.
  EXPECT_TRUE(cache->Store(k10BytePolicy,
                           crypto::SHA256HashString(k10ByteData),
                           k10ByteData));
  // Store external data for |k20BytePolicy| that does not match the hash in its
  // external data reference.
  EXPECT_TRUE(cache->Store(k20BytePolicy,
                           crypto::SHA256HashString(k10ByteData),
                           k10ByteData));
  // Store external data for |kUnknownPolicy|, which is not a known policy and
  // therefore, cannot be referencing any external data.
  EXPECT_TRUE(cache->Store(kUnknownPolicy,
                           crypto::SHA256HashString(k10ByteData),
                           k10ByteData));
  cache.reset();

  // Instantiate and destroy an ExternalDataManager that uses the primed cache.
  SetUpExternalDataManager();
  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();

  cache.reset(new CloudExternalDataStore(kCacheKey,
                                         message_loop_.message_loop_proxy(),
                                         resource_cache_.get()));
  std::string data;
  // Verify that the valid external data for |k10BytePolicy| is still in the
  // cache.
  EXPECT_TRUE(cache->Load(k10BytePolicy,
                          crypto::SHA256HashString(k10ByteData),
                          10,
                          &data));
  EXPECT_EQ(k10ByteData, data);
  // Verify that the external data for |k20BytePolicy| and |kUnknownPolicy| has
  // been pruned from the cache.
  EXPECT_FALSE(cache->Load(k20BytePolicy,
                           crypto::SHA256HashString(k10ByteData),
                           20,
                           &data));
  EXPECT_FALSE(cache->Load(kUnknownPolicy,
                           crypto::SHA256HashString(k10ByteData),
                           20,
                           &data));
}

// Verifies that when the external data referenced by a policy is present in the
// cache and the reference changes, the old data is pruned from the cache.
TEST_F(CloudExternalDataManagerBaseTest, PruneCacheOnChange) {
  // Store valid external data for |k20BytePolicy| in the cache.
  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();
  scoped_ptr<CloudExternalDataStore>
      cache(new CloudExternalDataStore(kCacheKey,
                                       message_loop_.message_loop_proxy(),
                                       resource_cache_.get()));
  EXPECT_TRUE(cache->Store(k20BytePolicy,
                           crypto::SHA256HashString(k20ByteData),
                           k20ByteData));
  cache.reset();

  // Instantiate an ExternalDataManager that uses the primed cache.
  SetUpExternalDataManager();
  external_data_manager_->Connect(request_content_getter_);

  // Modify the external data reference for |k20BytePolicy|.
  SetExternalDataReference(
      k20BytePolicy,
      ConstructMetadata(k20BytePolicyURL,
                        crypto::SHA256HashString(k10ByteData)));
  cloud_policy_store_.NotifyStoreLoaded();

  // Verify that the old external data for |k20BytePolicy| has been pruned from
  // the cache.
  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();
  cache.reset(new CloudExternalDataStore(kCacheKey,
                                         message_loop_.message_loop_proxy(),
                                         resource_cache_.get()));
  std::string data;
  EXPECT_FALSE(cache->Load(k20BytePolicy,
                           crypto::SHA256HashString(k20ByteData),
                           20,
                           &data));
}

// Verifies that corrupt cache entries are detected and deleted when accessed.
TEST_F(CloudExternalDataManagerBaseTest, CacheCorruption) {
  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();
  scoped_ptr<CloudExternalDataStore>
      cache(new CloudExternalDataStore(kCacheKey,
                                       message_loop_.message_loop_proxy(),
                                       resource_cache_.get()));
  // Store external data for |k10BytePolicy| that exceeds the maximal external
  // data size allowed for that policy.
  EXPECT_TRUE(cache->Store(k10BytePolicy,
                           crypto::SHA256HashString(k20ByteData),
                           k20ByteData));
  // Store external data for |k20BytePolicy| that is corrupted and does not
  // match the expected hash.
  EXPECT_TRUE(cache->Store(k20BytePolicy,
                           crypto::SHA256HashString(k20ByteData),
                           k10ByteData));
  cache.reset();

  SetUpExternalDataManager();
  // Serve external data for |k10BytePolicy| that exceeds the maximal external
  // data size allowed for that policy.
  SetFakeResponse(k10BytePolicyURL, k20ByteData, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);
  external_data_manager_->Connect(request_content_getter_);

  // Modify the external data reference for |k10BytePolicy| to match the
  // external data being served.
  SetExternalDataReference(
      k10BytePolicy,
      ConstructMetadata(k10BytePolicyURL,
                        crypto::SHA256HashString(k20ByteData)));
  cloud_policy_store_.NotifyStoreLoaded();

  // Retrieve external data for |k10BytePolicy|. Verify that the callback is
  // not invoked as the cached and downloaded external data exceed the maximal
  // size allowed for this policy and the request remains pending.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(0));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(callback_data_.empty());
  ResetCallbackData();

  // Serve valid external data for |k20BytePolicy|.
  SetFakeResponse(k20BytePolicyURL, k20ByteData, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);

  // Retrieve external data for |k20BytePolicy|. Verify that the callback is
  // invoked with the valid downloaded data, not the invalid data in the cache.
  external_data_manager_->Fetch(k20BytePolicy, ConstructFetchCallback(1));
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  ASSERT_TRUE(callback_data_[1]);
  EXPECT_EQ(k20ByteData, *callback_data_[1]);
  ResetCallbackData();

  external_data_manager_.reset();
  base::RunLoop().RunUntilIdle();
  cache.reset(new CloudExternalDataStore(kCacheKey,
                                         message_loop_.message_loop_proxy(),
                                         resource_cache_.get()));
  std::string data;
  // Verify that the invalid external data for |k10BytePolicy| has been pruned
  // from the cache. Load() will return |false| in two cases:
  // 1) The cache entry for |k10BytePolicy| has been pruned.
  // 2) The cache entry for |k10BytePolicy| still exists but the cached data
  //    does not match the expected hash or exceeds the maximum size allowed.
  // To test for the former, Load() is called with a maximum data size and hash
  // that would allow the data originally written to the cache to be loaded.
  // When this fails, it is certain that the original data is no longer present
  // in the cache.
  EXPECT_FALSE(cache->Load(k10BytePolicy,
                           crypto::SHA256HashString(k20ByteData),
                           20,
                           &data));
  // Verify that the invalid external data for |k20BytePolicy| has been replaced
  // with the downloaded valid data in the cache.
  EXPECT_TRUE(cache->Load(k20BytePolicy,
                          crypto::SHA256HashString(k20ByteData),
                          20,
                          &data));
  EXPECT_EQ(k20ByteData, data);
}

// Verifies that when the external data reference for a policy changes while a
// download of the external data for that policy is pending, the download is
// immediately retried using the new reference.
TEST_F(CloudExternalDataManagerBaseTest, PolicyChangeWhileDownloadPending) {
  // Make attempts to download the external data for |k10BytePolicy| and
  // |k20BytePolicy| fail with an error.
  SetFakeResponse(k10BytePolicyURL, std::string(),
                  net::HTTP_INTERNAL_SERVER_ERROR,
                  net::URLRequestStatus::FAILED);
  SetFakeResponse(k20BytePolicyURL, std::string(),
                  net::HTTP_INTERNAL_SERVER_ERROR,
                  net::URLRequestStatus::FAILED);
  external_data_manager_->Connect(request_content_getter_);

  // Attempt to retrieve external data for |k10BytePolicy| and |k20BytePolicy|.
  // Verify that no callbacks are invoked as the download attempts fail and the
  // requests remain pending.
  external_data_manager_->Fetch(k10BytePolicy, ConstructFetchCallback(0));
  external_data_manager_->Fetch(k20BytePolicy, ConstructFetchCallback(1));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(callback_data_.empty());
  ResetCallbackData();

  // Modify the external data reference for |k10BytePolicy| to be invalid.
  // Verify that the callback is invoked as the policy no longer has a valid
  // external data reference.
  cloud_policy_store_.policy_map_.Erase(k10BytePolicy);
  cloud_policy_store_.NotifyStoreLoaded();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  EXPECT_TRUE(callback_data_.find(0) != callback_data_.end());
  EXPECT_FALSE(callback_data_[0]);
  ResetCallbackData();

  // Serve valid external data for |k20BytePolicy|.
  fetcher_factory_.ClearFakeResponses();
  SetFakeResponse(k20BytePolicyURL, k10ByteData, net::HTTP_OK,
                  net::URLRequestStatus::SUCCESS);

  // Modify the external data reference for |k20BytePolicy| to match the
  // external data now being served. Verify that the callback is invoked with
  // the downloaded data.
  SetExternalDataReference(
      k20BytePolicy,
      ConstructMetadata(k20BytePolicyURL,
                        crypto::SHA256HashString(k10ByteData)));
  cloud_policy_store_.NotifyStoreLoaded();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1u, callback_data_.size());
  ASSERT_TRUE(callback_data_[1]);
  EXPECT_EQ(k10ByteData, *callback_data_[1]);
  ResetCallbackData();
}

}  // namespace policy