Chromium Code Reviews| 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 |
|
bartfab (slow)
2013/11/05 18:18:33
Nit: policy/policy_constants.h now implicitly pull
Joao da Silva
2013/11/07 20:27:27
Done.
| |
| 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/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/sha1.h" | 16 #include "base/sha1.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/test/test_simple_task_runner.h" | 19 #include "base/test/test_simple_task_runner.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "chrome/browser/chromeos/policy/cloud_external_data_store.h" | 21 #include "chrome/browser/chromeos/policy/cloud_external_data_store.h" |
| 22 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | 22 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" |
| 23 #include "chrome/browser/policy/cloud/resource_cache.h" | 23 #include "chrome/browser/policy/cloud/resource_cache.h" |
| 24 #include "chrome/browser/policy/external_data_fetcher.h" | 24 #include "chrome/browser/policy/external_data_fetcher.h" |
| 25 #include "chrome/browser/policy/policy_map.h" | 25 #include "chrome/browser/policy/policy_map.h" |
| 26 #include "chrome/browser/policy/policy_types.h" | 26 #include "chrome/browser/policy/policy_types.h" |
| 27 #include "chrome/browser/policy/test/policy_test_utils.h" | |
| 27 #include "net/url_request/test_url_fetcher_factory.h" | 28 #include "net/url_request/test_url_fetcher_factory.h" |
| 28 #include "net/url_request/url_fetcher.h" | 29 #include "net/url_request/url_fetcher.h" |
| 29 #include "net/url_request/url_fetcher_delegate.h" | 30 #include "net/url_request/url_fetcher_delegate.h" |
| 30 #include "net/url_request/url_request_test_util.h" | 31 #include "net/url_request/url_request_test_util.h" |
| 31 #include "policy/policy_constants.h" | 32 #include "policy/policy_constants.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| 35 namespace policy { | 36 namespace policy { |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 // A string policy. | 40 // A string policy. |
| 40 const char kStringPolicy[] = "StringPolicy"; | 41 const char kStringPolicy[] = "StringPolicy"; |
| 41 // A policy that may reference up to 10 bytes of external data. | 42 // A policy that may reference up to 10 bytes of external data. |
| 42 const char k10BytePolicy[] = "10BytePolicy"; | 43 const char k10BytePolicy[] = "10BytePolicy"; |
| 43 // A policy that may reference up to 20 bytes of external data. | 44 // A policy that may reference up to 20 bytes of external data. |
| 44 const char k20BytePolicy[] = "20BytePolicy"; | 45 const char k20BytePolicy[] = "20BytePolicy"; |
| 45 // A nonexistent policy. | 46 // A nonexistent policy. |
| 46 const char kUnknownPolicy[] = "UnknownPolicy"; | 47 const char kUnknownPolicy[] = "UnknownPolicy"; |
| 47 | 48 |
| 48 const char k10BytePolicyURL[] = "http://localhost/10_bytes"; | 49 const char k10BytePolicyURL[] = "http://localhost/10_bytes"; |
| 49 const char k20BytePolicyURL[] = "http://localhost/20_bytes"; | 50 const char k20BytePolicyURL[] = "http://localhost/20_bytes"; |
| 50 | 51 |
| 51 const char k10ByteData[] = "10 bytes.."; | 52 const char k10ByteData[] = "10 bytes.."; |
| 52 const char k20ByteData[] = "20 bytes............"; | 53 const char k20ByteData[] = "20 bytes............"; |
| 53 | 54 |
| 54 const PolicyDefinitionList::Entry kPolicyDefinitionListEntries[] = { | 55 const PolicyDetails kPolicyDetails[] = { |
| 55 { kStringPolicy, base::Value::TYPE_STRING, false, 1, 0 }, | 56 // is_deprecated is_device_policy id max_external_data_size |
| 56 { k10BytePolicy, base::Value::TYPE_DICTIONARY, false, 2, 10 }, | 57 { false, false, 1, 0 }, |
| 57 { k20BytePolicy, base::Value::TYPE_DICTIONARY, false, 3, 20 }, | 58 { false, false, 2, 10 }, |
| 58 }; | 59 { false, false, 3, 20 }, |
| 59 | |
| 60 const PolicyDefinitionList kPolicyDefinitionList = { | |
| 61 kPolicyDefinitionListEntries, | |
| 62 kPolicyDefinitionListEntries + arraysize(kPolicyDefinitionListEntries), | |
| 63 }; | 60 }; |
| 64 | 61 |
| 65 const char kCacheKey[] = "data"; | 62 const char kCacheKey[] = "data"; |
| 66 | 63 |
| 67 // A variant of net::FakeURLFetcherFactory that makes it an error to request a | 64 // A variant of net::FakeURLFetcherFactory that makes it an error to request a |
| 68 // fetcher for an unknown URL. | 65 // fetcher for an unknown URL. |
| 69 class FakeURLFetcherFactory : public net::FakeURLFetcherFactory { | 66 class FakeURLFetcherFactory : public net::FakeURLFetcherFactory { |
| 70 public: | 67 public: |
| 71 FakeURLFetcherFactory(); | 68 FakeURLFetcherFactory(); |
| 72 virtual ~FakeURLFetcherFactory(); | 69 virtual ~FakeURLFetcherFactory(); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 base::MessageLoop message_loop_; | 127 base::MessageLoop message_loop_; |
| 131 base::ScopedTempDir temp_dir_; | 128 base::ScopedTempDir temp_dir_; |
| 132 scoped_ptr<ResourceCache> resource_cache_; | 129 scoped_ptr<ResourceCache> resource_cache_; |
| 133 MockCloudPolicyStore cloud_policy_store_; | 130 MockCloudPolicyStore cloud_policy_store_; |
| 134 scoped_refptr<net::TestURLRequestContextGetter> request_content_getter_; | 131 scoped_refptr<net::TestURLRequestContextGetter> request_content_getter_; |
| 135 FakeURLFetcherFactory fetcher_factory_; | 132 FakeURLFetcherFactory fetcher_factory_; |
| 136 | 133 |
| 137 scoped_ptr<CloudExternalDataManagerBase> external_data_manager_; | 134 scoped_ptr<CloudExternalDataManagerBase> external_data_manager_; |
| 138 | 135 |
| 139 std::map<int, std::string*> callback_data_; | 136 std::map<int, std::string*> callback_data_; |
| 137 PolicyDetailsMap policy_details_; | |
| 140 | 138 |
| 141 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataManagerBaseTest); | 139 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataManagerBaseTest); |
| 142 }; | 140 }; |
| 143 | 141 |
| 144 CloudExternalDataManagerBaseTest::CloudExternalDataManagerBaseTest() { | 142 CloudExternalDataManagerBaseTest::CloudExternalDataManagerBaseTest() { |
| 145 } | 143 } |
| 146 | 144 |
| 147 void CloudExternalDataManagerBaseTest::SetUp() { | 145 void CloudExternalDataManagerBaseTest::SetUp() { |
| 148 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 146 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 149 resource_cache_.reset(new ResourceCache(temp_dir_.path(), | 147 resource_cache_.reset(new ResourceCache(temp_dir_.path(), |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 161 k10BytePolicy, | 159 k10BytePolicy, |
| 162 ConstructMetadata(k10BytePolicyURL, base::SHA1HashString(k10ByteData))); | 160 ConstructMetadata(k10BytePolicyURL, base::SHA1HashString(k10ByteData))); |
| 163 // Make |k20BytePolicy| reference 20 bytes of external data. | 161 // Make |k20BytePolicy| reference 20 bytes of external data. |
| 164 SetExternalDataReference( | 162 SetExternalDataReference( |
| 165 k20BytePolicy, | 163 k20BytePolicy, |
| 166 ConstructMetadata(k20BytePolicyURL, base::SHA1HashString(k20ByteData))); | 164 ConstructMetadata(k20BytePolicyURL, base::SHA1HashString(k20ByteData))); |
| 167 cloud_policy_store_.NotifyStoreLoaded(); | 165 cloud_policy_store_.NotifyStoreLoaded(); |
| 168 | 166 |
| 169 request_content_getter_ = new net::TestURLRequestContextGetter( | 167 request_content_getter_ = new net::TestURLRequestContextGetter( |
| 170 base::MessageLoopProxy::current()); | 168 base::MessageLoopProxy::current()); |
| 169 | |
| 170 policy_details_.SetDetails(kStringPolicy, &kPolicyDetails[0]); | |
| 171 policy_details_.SetDetails(k10BytePolicy, &kPolicyDetails[1]); | |
| 172 policy_details_.SetDetails(k20BytePolicy, &kPolicyDetails[2]); | |
| 171 } | 173 } |
| 172 | 174 |
| 173 void CloudExternalDataManagerBaseTest::TearDown() { | 175 void CloudExternalDataManagerBaseTest::TearDown() { |
| 174 external_data_manager_.reset(); | 176 external_data_manager_.reset(); |
| 175 base::RunLoop().RunUntilIdle(); | 177 base::RunLoop().RunUntilIdle(); |
| 176 ResetCallbackData(); | 178 ResetCallbackData(); |
| 177 } | 179 } |
| 178 | 180 |
| 179 void CloudExternalDataManagerBaseTest::SetUpExternalDataManager() { | 181 void CloudExternalDataManagerBaseTest::SetUpExternalDataManager() { |
| 180 external_data_manager_.reset(new CloudExternalDataManagerBase( | 182 external_data_manager_.reset(new CloudExternalDataManagerBase( |
| 181 &kPolicyDefinitionList, | 183 policy_details_.GetCallback(), |
| 182 message_loop_.message_loop_proxy(), | 184 message_loop_.message_loop_proxy(), |
| 183 message_loop_.message_loop_proxy())); | 185 message_loop_.message_loop_proxy())); |
| 184 external_data_manager_->SetExternalDataStore(make_scoped_ptr( | 186 external_data_manager_->SetExternalDataStore(make_scoped_ptr( |
| 185 new CloudExternalDataStore(kCacheKey, | 187 new CloudExternalDataStore(kCacheKey, |
| 186 message_loop_.message_loop_proxy(), | 188 message_loop_.message_loop_proxy(), |
| 187 resource_cache_.get()))); | 189 resource_cache_.get()))); |
| 188 external_data_manager_->SetPolicyStore(&cloud_policy_store_); | 190 external_data_manager_->SetPolicyStore(&cloud_policy_store_); |
| 189 } | 191 } |
| 190 | 192 |
| 191 scoped_ptr<base::DictionaryValue> | 193 scoped_ptr<base::DictionaryValue> |
| (...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 725 ConstructMetadata(k20BytePolicyURL, base::SHA1HashString(k10ByteData))); | 727 ConstructMetadata(k20BytePolicyURL, base::SHA1HashString(k10ByteData))); |
| 726 cloud_policy_store_.NotifyStoreLoaded(); | 728 cloud_policy_store_.NotifyStoreLoaded(); |
| 727 base::RunLoop().RunUntilIdle(); | 729 base::RunLoop().RunUntilIdle(); |
| 728 EXPECT_EQ(1u, callback_data_.size()); | 730 EXPECT_EQ(1u, callback_data_.size()); |
| 729 ASSERT_TRUE(callback_data_[1]); | 731 ASSERT_TRUE(callback_data_[1]); |
| 730 EXPECT_EQ(k10ByteData, *callback_data_[1]); | 732 EXPECT_EQ(k10ByteData, *callback_data_[1]); |
| 731 ResetCallbackData(); | 733 ResetCallbackData(); |
| 732 } | 734 } |
| 733 | 735 |
| 734 } // namespace policy | 736 } // namespace policy |
| OLD | NEW |