| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base.h" | 5 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/test/test_simple_task_runner.h" | 19 #include "base/test/test_simple_task_runner.h" |
| 19 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" |
| 20 #include "base/values.h" | 21 #include "base/values.h" |
| 21 #include "chrome/browser/chromeos/policy/cloud_external_data_store.h" | 22 #include "chrome/browser/chromeos/policy/cloud_external_data_store.h" |
| 22 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h" | 23 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h" |
| 23 #include "components/policy/core/common/cloud/resource_cache.h" | 24 #include "components/policy/core/common/cloud/resource_cache.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 const char kCacheKey[] = "data"; | 65 const char kCacheKey[] = "data"; |
| 65 | 66 |
| 66 // A variant of net::FakeURLFetcherFactory that makes it an error to request a | 67 // A variant of net::FakeURLFetcherFactory that makes it an error to request a |
| 67 // fetcher for an unknown URL. | 68 // fetcher for an unknown URL. |
| 68 class FakeURLFetcherFactory : public net::FakeURLFetcherFactory { | 69 class FakeURLFetcherFactory : public net::FakeURLFetcherFactory { |
| 69 public: | 70 public: |
| 70 FakeURLFetcherFactory(); | 71 FakeURLFetcherFactory(); |
| 71 ~FakeURLFetcherFactory() override; | 72 ~FakeURLFetcherFactory() override; |
| 72 | 73 |
| 73 // net::FakeURLFetcherFactory: | 74 // net::FakeURLFetcherFactory: |
| 74 scoped_ptr<net::URLFetcher> CreateURLFetcher( | 75 std::unique_ptr<net::URLFetcher> CreateURLFetcher( |
| 75 int id, | 76 int id, |
| 76 const GURL& url, | 77 const GURL& url, |
| 77 net::URLFetcher::RequestType request_type, | 78 net::URLFetcher::RequestType request_type, |
| 78 net::URLFetcherDelegate* delegate) override; | 79 net::URLFetcherDelegate* delegate) override; |
| 79 | 80 |
| 80 private: | 81 private: |
| 81 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); | 82 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); |
| 82 }; | 83 }; |
| 83 | 84 |
| 84 FakeURLFetcherFactory::FakeURLFetcherFactory() | 85 FakeURLFetcherFactory::FakeURLFetcherFactory() |
| 85 : net::FakeURLFetcherFactory(NULL) { | 86 : net::FakeURLFetcherFactory(NULL) { |
| 86 } | 87 } |
| 87 | 88 |
| 88 FakeURLFetcherFactory::~FakeURLFetcherFactory() { | 89 FakeURLFetcherFactory::~FakeURLFetcherFactory() { |
| 89 } | 90 } |
| 90 | 91 |
| 91 scoped_ptr<net::URLFetcher> FakeURLFetcherFactory::CreateURLFetcher( | 92 std::unique_ptr<net::URLFetcher> FakeURLFetcherFactory::CreateURLFetcher( |
| 92 int id, | 93 int id, |
| 93 const GURL& url, | 94 const GURL& url, |
| 94 net::URLFetcher::RequestType request_type, | 95 net::URLFetcher::RequestType request_type, |
| 95 net::URLFetcherDelegate* delegate) { | 96 net::URLFetcherDelegate* delegate) { |
| 96 scoped_ptr<net::URLFetcher> fetcher = | 97 std::unique_ptr<net::URLFetcher> fetcher = |
| 97 net::FakeURLFetcherFactory::CreateURLFetcher(id, url, request_type, | 98 net::FakeURLFetcherFactory::CreateURLFetcher(id, url, request_type, |
| 98 delegate); | 99 delegate); |
| 99 EXPECT_TRUE(fetcher); | 100 EXPECT_TRUE(fetcher); |
| 100 return fetcher; | 101 return fetcher; |
| 101 } | 102 } |
| 102 | 103 |
| 103 } // namespace | 104 } // namespace |
| 104 | 105 |
| 105 class CloudExternalDataManagerBaseTest : public testing::Test { | 106 class CloudExternalDataManagerBaseTest : public testing::Test { |
| 106 protected: | 107 protected: |
| 107 CloudExternalDataManagerBaseTest(); | 108 CloudExternalDataManagerBaseTest(); |
| 108 | 109 |
| 109 void SetUp() override; | 110 void SetUp() override; |
| 110 void TearDown() override; | 111 void TearDown() override; |
| 111 | 112 |
| 112 void SetUpExternalDataManager(); | 113 void SetUpExternalDataManager(); |
| 113 | 114 |
| 114 scoped_ptr<base::DictionaryValue> ConstructMetadata(const std::string& url, | 115 std::unique_ptr<base::DictionaryValue> ConstructMetadata( |
| 115 const std::string& hash); | 116 const std::string& url, |
| 116 void SetExternalDataReference(const std::string& policy, | 117 const std::string& hash); |
| 117 scoped_ptr<base::DictionaryValue> metadata); | 118 void SetExternalDataReference( |
| 119 const std::string& policy, |
| 120 std::unique_ptr<base::DictionaryValue> metadata); |
| 118 | 121 |
| 119 ExternalDataFetcher::FetchCallback ConstructFetchCallback(int id); | 122 ExternalDataFetcher::FetchCallback ConstructFetchCallback(int id); |
| 120 void ResetCallbackData(); | 123 void ResetCallbackData(); |
| 121 | 124 |
| 122 void OnFetchDone(int id, scoped_ptr<std::string> data); | 125 void OnFetchDone(int id, std::unique_ptr<std::string> data); |
| 123 | 126 |
| 124 void FetchAll(); | 127 void FetchAll(); |
| 125 | 128 |
| 126 void SetFakeResponse(const std::string& url, | 129 void SetFakeResponse(const std::string& url, |
| 127 const std::string& repsonse_data, | 130 const std::string& repsonse_data, |
| 128 net::HttpStatusCode response_code, | 131 net::HttpStatusCode response_code, |
| 129 net::URLRequestStatus::Status status); | 132 net::URLRequestStatus::Status status); |
| 130 | 133 |
| 131 base::MessageLoop message_loop_; | 134 base::MessageLoop message_loop_; |
| 132 base::ScopedTempDir temp_dir_; | 135 base::ScopedTempDir temp_dir_; |
| 133 scoped_ptr<ResourceCache> resource_cache_; | 136 std::unique_ptr<ResourceCache> resource_cache_; |
| 134 MockCloudPolicyStore cloud_policy_store_; | 137 MockCloudPolicyStore cloud_policy_store_; |
| 135 scoped_refptr<net::TestURLRequestContextGetter> request_content_getter_; | 138 scoped_refptr<net::TestURLRequestContextGetter> request_content_getter_; |
| 136 FakeURLFetcherFactory fetcher_factory_; | 139 FakeURLFetcherFactory fetcher_factory_; |
| 137 | 140 |
| 138 scoped_ptr<CloudExternalDataManagerBase> external_data_manager_; | 141 std::unique_ptr<CloudExternalDataManagerBase> external_data_manager_; |
| 139 | 142 |
| 140 std::map<int, std::string*> callback_data_; | 143 std::map<int, std::string*> callback_data_; |
| 141 PolicyDetailsMap policy_details_; | 144 PolicyDetailsMap policy_details_; |
| 142 | 145 |
| 143 private: | 146 private: |
| 144 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataManagerBaseTest); | 147 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataManagerBaseTest); |
| 145 }; | 148 }; |
| 146 | 149 |
| 147 CloudExternalDataManagerBaseTest::CloudExternalDataManagerBaseTest() { | 150 CloudExternalDataManagerBaseTest::CloudExternalDataManagerBaseTest() { |
| 148 } | 151 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 external_data_manager_.reset(); | 187 external_data_manager_.reset(); |
| 185 base::RunLoop().RunUntilIdle(); | 188 base::RunLoop().RunUntilIdle(); |
| 186 ResetCallbackData(); | 189 ResetCallbackData(); |
| 187 } | 190 } |
| 188 | 191 |
| 189 void CloudExternalDataManagerBaseTest::SetUpExternalDataManager() { | 192 void CloudExternalDataManagerBaseTest::SetUpExternalDataManager() { |
| 190 external_data_manager_.reset(new CloudExternalDataManagerBase( | 193 external_data_manager_.reset(new CloudExternalDataManagerBase( |
| 191 policy_details_.GetCallback(), message_loop_.task_runner(), | 194 policy_details_.GetCallback(), message_loop_.task_runner(), |
| 192 message_loop_.task_runner())); | 195 message_loop_.task_runner())); |
| 193 external_data_manager_->SetExternalDataStore( | 196 external_data_manager_->SetExternalDataStore( |
| 194 make_scoped_ptr(new CloudExternalDataStore( | 197 base::WrapUnique(new CloudExternalDataStore( |
| 195 kCacheKey, message_loop_.task_runner(), resource_cache_.get()))); | 198 kCacheKey, message_loop_.task_runner(), resource_cache_.get()))); |
| 196 external_data_manager_->SetPolicyStore(&cloud_policy_store_); | 199 external_data_manager_->SetPolicyStore(&cloud_policy_store_); |
| 197 } | 200 } |
| 198 | 201 |
| 199 scoped_ptr<base::DictionaryValue> | 202 std::unique_ptr<base::DictionaryValue> |
| 200 CloudExternalDataManagerBaseTest::ConstructMetadata( | 203 CloudExternalDataManagerBaseTest::ConstructMetadata(const std::string& url, |
| 201 const std::string& url, | 204 const std::string& hash) { |
| 202 const std::string& hash) { | 205 std::unique_ptr<base::DictionaryValue> metadata(new base::DictionaryValue); |
| 203 scoped_ptr<base::DictionaryValue> metadata(new base::DictionaryValue); | |
| 204 metadata->SetStringWithoutPathExpansion("url", url); | 206 metadata->SetStringWithoutPathExpansion("url", url); |
| 205 metadata->SetStringWithoutPathExpansion("hash", base::HexEncode(hash.c_str(), | 207 metadata->SetStringWithoutPathExpansion("hash", base::HexEncode(hash.c_str(), |
| 206 hash.size())); | 208 hash.size())); |
| 207 return metadata; | 209 return metadata; |
| 208 } | 210 } |
| 209 | 211 |
| 210 void CloudExternalDataManagerBaseTest::SetExternalDataReference( | 212 void CloudExternalDataManagerBaseTest::SetExternalDataReference( |
| 211 const std::string& policy, | 213 const std::string& policy, |
| 212 scoped_ptr<base::DictionaryValue> metadata) { | 214 std::unique_ptr<base::DictionaryValue> metadata) { |
| 213 cloud_policy_store_.policy_map_.Set( | 215 cloud_policy_store_.policy_map_.Set( |
| 214 policy, | 216 policy, |
| 215 POLICY_LEVEL_MANDATORY, | 217 POLICY_LEVEL_MANDATORY, |
| 216 POLICY_SCOPE_USER, | 218 POLICY_SCOPE_USER, |
| 217 POLICY_SOURCE_CLOUD, | 219 POLICY_SOURCE_CLOUD, |
| 218 metadata.release(), | 220 metadata.release(), |
| 219 new ExternalDataFetcher( | 221 new ExternalDataFetcher( |
| 220 external_data_manager_->weak_factory_.GetWeakPtr(), policy)); | 222 external_data_manager_->weak_factory_.GetWeakPtr(), policy)); |
| 221 } | 223 } |
| 222 | 224 |
| 223 ExternalDataFetcher::FetchCallback | 225 ExternalDataFetcher::FetchCallback |
| 224 CloudExternalDataManagerBaseTest::ConstructFetchCallback(int id) { | 226 CloudExternalDataManagerBaseTest::ConstructFetchCallback(int id) { |
| 225 return base::Bind(&CloudExternalDataManagerBaseTest::OnFetchDone, | 227 return base::Bind(&CloudExternalDataManagerBaseTest::OnFetchDone, |
| 226 base::Unretained(this), | 228 base::Unretained(this), |
| 227 id); | 229 id); |
| 228 } | 230 } |
| 229 | 231 |
| 230 void CloudExternalDataManagerBaseTest::ResetCallbackData() { | 232 void CloudExternalDataManagerBaseTest::ResetCallbackData() { |
| 231 STLDeleteValues(&callback_data_); | 233 STLDeleteValues(&callback_data_); |
| 232 } | 234 } |
| 233 | 235 |
| 234 void CloudExternalDataManagerBaseTest::OnFetchDone( | 236 void CloudExternalDataManagerBaseTest::OnFetchDone( |
| 235 int id, | 237 int id, |
| 236 scoped_ptr<std::string> data) { | 238 std::unique_ptr<std::string> data) { |
| 237 delete callback_data_[id]; | 239 delete callback_data_[id]; |
| 238 callback_data_[id] = data.release(); | 240 callback_data_[id] = data.release(); |
| 239 } | 241 } |
| 240 | 242 |
| 241 void CloudExternalDataManagerBaseTest::FetchAll() { | 243 void CloudExternalDataManagerBaseTest::FetchAll() { |
| 242 external_data_manager_->FetchAll(); | 244 external_data_manager_->FetchAll(); |
| 243 } | 245 } |
| 244 | 246 |
| 245 void CloudExternalDataManagerBaseTest::SetFakeResponse( | 247 void CloudExternalDataManagerBaseTest::SetFakeResponse( |
| 246 const std::string& url, | 248 const std::string& url, |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 ASSERT_TRUE(callback_data_[0]); | 546 ASSERT_TRUE(callback_data_[0]); |
| 545 EXPECT_EQ(k10ByteData, *callback_data_[0]); | 547 EXPECT_EQ(k10ByteData, *callback_data_[0]); |
| 546 ResetCallbackData(); | 548 ResetCallbackData(); |
| 547 } | 549 } |
| 548 | 550 |
| 549 // Verifies that cache entries which do not correspond to the external data | 551 // Verifies that cache entries which do not correspond to the external data |
| 550 // referenced by any policy are pruned on startup. | 552 // referenced by any policy are pruned on startup. |
| 551 TEST_F(CloudExternalDataManagerBaseTest, PruneCacheOnStartup) { | 553 TEST_F(CloudExternalDataManagerBaseTest, PruneCacheOnStartup) { |
| 552 external_data_manager_.reset(); | 554 external_data_manager_.reset(); |
| 553 base::RunLoop().RunUntilIdle(); | 555 base::RunLoop().RunUntilIdle(); |
| 554 scoped_ptr<CloudExternalDataStore> cache(new CloudExternalDataStore( | 556 std::unique_ptr<CloudExternalDataStore> cache(new CloudExternalDataStore( |
| 555 kCacheKey, message_loop_.task_runner(), resource_cache_.get())); | 557 kCacheKey, message_loop_.task_runner(), resource_cache_.get())); |
| 556 // Store valid external data for |k10BytePolicy| in the cache. | 558 // Store valid external data for |k10BytePolicy| in the cache. |
| 557 EXPECT_TRUE(cache->Store(k10BytePolicy, | 559 EXPECT_TRUE(cache->Store(k10BytePolicy, |
| 558 crypto::SHA256HashString(k10ByteData), | 560 crypto::SHA256HashString(k10ByteData), |
| 559 k10ByteData)); | 561 k10ByteData)); |
| 560 // Store external data for |k20BytePolicy| that does not match the hash in its | 562 // Store external data for |k20BytePolicy| that does not match the hash in its |
| 561 // external data reference. | 563 // external data reference. |
| 562 EXPECT_TRUE(cache->Store(k20BytePolicy, | 564 EXPECT_TRUE(cache->Store(k20BytePolicy, |
| 563 crypto::SHA256HashString(k10ByteData), | 565 crypto::SHA256HashString(k10ByteData), |
| 564 k10ByteData)); | 566 k10ByteData)); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 595 20, | 597 20, |
| 596 &data)); | 598 &data)); |
| 597 } | 599 } |
| 598 | 600 |
| 599 // Verifies that when the external data referenced by a policy is present in the | 601 // Verifies that when the external data referenced by a policy is present in the |
| 600 // cache and the reference changes, the old data is pruned from the cache. | 602 // cache and the reference changes, the old data is pruned from the cache. |
| 601 TEST_F(CloudExternalDataManagerBaseTest, PruneCacheOnChange) { | 603 TEST_F(CloudExternalDataManagerBaseTest, PruneCacheOnChange) { |
| 602 // Store valid external data for |k20BytePolicy| in the cache. | 604 // Store valid external data for |k20BytePolicy| in the cache. |
| 603 external_data_manager_.reset(); | 605 external_data_manager_.reset(); |
| 604 base::RunLoop().RunUntilIdle(); | 606 base::RunLoop().RunUntilIdle(); |
| 605 scoped_ptr<CloudExternalDataStore> cache(new CloudExternalDataStore( | 607 std::unique_ptr<CloudExternalDataStore> cache(new CloudExternalDataStore( |
| 606 kCacheKey, message_loop_.task_runner(), resource_cache_.get())); | 608 kCacheKey, message_loop_.task_runner(), resource_cache_.get())); |
| 607 EXPECT_TRUE(cache->Store(k20BytePolicy, | 609 EXPECT_TRUE(cache->Store(k20BytePolicy, |
| 608 crypto::SHA256HashString(k20ByteData), | 610 crypto::SHA256HashString(k20ByteData), |
| 609 k20ByteData)); | 611 k20ByteData)); |
| 610 cache.reset(); | 612 cache.reset(); |
| 611 | 613 |
| 612 // Instantiate an ExternalDataManager that uses the primed cache. | 614 // Instantiate an ExternalDataManager that uses the primed cache. |
| 613 SetUpExternalDataManager(); | 615 SetUpExternalDataManager(); |
| 614 external_data_manager_->Connect(request_content_getter_); | 616 external_data_manager_->Connect(request_content_getter_); |
| 615 | 617 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 630 EXPECT_FALSE(cache->Load(k20BytePolicy, | 632 EXPECT_FALSE(cache->Load(k20BytePolicy, |
| 631 crypto::SHA256HashString(k20ByteData), | 633 crypto::SHA256HashString(k20ByteData), |
| 632 20, | 634 20, |
| 633 &data)); | 635 &data)); |
| 634 } | 636 } |
| 635 | 637 |
| 636 // Verifies that corrupt cache entries are detected and deleted when accessed. | 638 // Verifies that corrupt cache entries are detected and deleted when accessed. |
| 637 TEST_F(CloudExternalDataManagerBaseTest, CacheCorruption) { | 639 TEST_F(CloudExternalDataManagerBaseTest, CacheCorruption) { |
| 638 external_data_manager_.reset(); | 640 external_data_manager_.reset(); |
| 639 base::RunLoop().RunUntilIdle(); | 641 base::RunLoop().RunUntilIdle(); |
| 640 scoped_ptr<CloudExternalDataStore> cache(new CloudExternalDataStore( | 642 std::unique_ptr<CloudExternalDataStore> cache(new CloudExternalDataStore( |
| 641 kCacheKey, message_loop_.task_runner(), resource_cache_.get())); | 643 kCacheKey, message_loop_.task_runner(), resource_cache_.get())); |
| 642 // Store external data for |k10BytePolicy| that exceeds the maximal external | 644 // Store external data for |k10BytePolicy| that exceeds the maximal external |
| 643 // data size allowed for that policy. | 645 // data size allowed for that policy. |
| 644 EXPECT_TRUE(cache->Store(k10BytePolicy, | 646 EXPECT_TRUE(cache->Store(k10BytePolicy, |
| 645 crypto::SHA256HashString(k20ByteData), | 647 crypto::SHA256HashString(k20ByteData), |
| 646 k20ByteData)); | 648 k20ByteData)); |
| 647 // Store external data for |k20BytePolicy| that is corrupted and does not | 649 // Store external data for |k20BytePolicy| that is corrupted and does not |
| 648 // match the expected hash. | 650 // match the expected hash. |
| 649 EXPECT_TRUE(cache->Store(k20BytePolicy, | 651 EXPECT_TRUE(cache->Store(k20BytePolicy, |
| 650 crypto::SHA256HashString(k20ByteData), | 652 crypto::SHA256HashString(k20ByteData), |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 crypto::SHA256HashString(k10ByteData))); | 764 crypto::SHA256HashString(k10ByteData))); |
| 763 cloud_policy_store_.NotifyStoreLoaded(); | 765 cloud_policy_store_.NotifyStoreLoaded(); |
| 764 base::RunLoop().RunUntilIdle(); | 766 base::RunLoop().RunUntilIdle(); |
| 765 EXPECT_EQ(1u, callback_data_.size()); | 767 EXPECT_EQ(1u, callback_data_.size()); |
| 766 ASSERT_TRUE(callback_data_[1]); | 768 ASSERT_TRUE(callback_data_[1]); |
| 767 EXPECT_EQ(k10ByteData, *callback_data_[1]); | 769 EXPECT_EQ(k10ByteData, *callback_data_[1]); |
| 768 ResetCallbackData(); | 770 ResetCallbackData(); |
| 769 } | 771 } |
| 770 | 772 |
| 771 } // namespace policy | 773 } // namespace policy |
| OLD | NEW |