| 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/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/http/http_status_code.h" | 28 #include "net/http/http_status_code.h" |
| 28 #include "net/url_request/test_url_fetcher_factory.h" | 29 #include "net/url_request/test_url_fetcher_factory.h" |
| 29 #include "net/url_request/url_fetcher.h" | 30 #include "net/url_request/url_fetcher.h" |
| 30 #include "net/url_request/url_fetcher_delegate.h" | 31 #include "net/url_request/url_fetcher_delegate.h" |
| 31 #include "net/url_request/url_request_status.h" | 32 #include "net/url_request/url_request_status.h" |
| 32 #include "net/url_request/url_request_test_util.h" | 33 #include "net/url_request/url_request_test_util.h" |
| 33 #include "policy/policy_constants.h" | |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 35 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 36 | 36 |
| 37 namespace policy { | 37 namespace policy { |
| 38 | 38 |
| 39 namespace { | 39 namespace { |
| 40 | 40 |
| 41 // A string policy. | 41 // A string policy. |
| 42 const char kStringPolicy[] = "StringPolicy"; | 42 const char kStringPolicy[] = "StringPolicy"; |
| 43 // A policy that may reference up to 10 bytes of external data. | 43 // A policy that may reference up to 10 bytes of external data. |
| 44 const char k10BytePolicy[] = "10BytePolicy"; | 44 const char k10BytePolicy[] = "10BytePolicy"; |
| 45 // A policy that may reference up to 20 bytes of external data. | 45 // A policy that may reference up to 20 bytes of external data. |
| 46 const char k20BytePolicy[] = "20BytePolicy"; | 46 const char k20BytePolicy[] = "20BytePolicy"; |
| 47 // A nonexistent policy. | 47 // A nonexistent policy. |
| 48 const char kUnknownPolicy[] = "UnknownPolicy"; | 48 const char kUnknownPolicy[] = "UnknownPolicy"; |
| 49 | 49 |
| 50 const char k10BytePolicyURL[] = "http://localhost/10_bytes"; | 50 const char k10BytePolicyURL[] = "http://localhost/10_bytes"; |
| 51 const char k20BytePolicyURL[] = "http://localhost/20_bytes"; | 51 const char k20BytePolicyURL[] = "http://localhost/20_bytes"; |
| 52 | 52 |
| 53 const char k10ByteData[] = "10 bytes.."; | 53 const char k10ByteData[] = "10 bytes.."; |
| 54 const char k20ByteData[] = "20 bytes............"; | 54 const char k20ByteData[] = "20 bytes............"; |
| 55 | 55 |
| 56 const PolicyDefinitionList::Entry kPolicyDefinitionListEntries[] = { | 56 const PolicyDetails kPolicyDetails[] = { |
| 57 { kStringPolicy, base::Value::TYPE_STRING, false, 1, 0 }, | 57 // is_deprecated is_device_policy id max_external_data_size |
| 58 { k10BytePolicy, base::Value::TYPE_DICTIONARY, false, 2, 10 }, | 58 { false, false, 1, 0 }, |
| 59 { k20BytePolicy, base::Value::TYPE_DICTIONARY, false, 3, 20 }, | 59 { false, false, 2, 10 }, |
| 60 }; | 60 { false, false, 3, 20 }, |
| 61 | |
| 62 const PolicyDefinitionList kPolicyDefinitionList = { | |
| 63 kPolicyDefinitionListEntries, | |
| 64 kPolicyDefinitionListEntries + arraysize(kPolicyDefinitionListEntries), | |
| 65 }; | 61 }; |
| 66 | 62 |
| 67 const char kCacheKey[] = "data"; | 63 const char kCacheKey[] = "data"; |
| 68 | 64 |
| 69 // A variant of net::FakeURLFetcherFactory that makes it an error to request a | 65 // A variant of net::FakeURLFetcherFactory that makes it an error to request a |
| 70 // fetcher for an unknown URL. | 66 // fetcher for an unknown URL. |
| 71 class FakeURLFetcherFactory : public net::FakeURLFetcherFactory { | 67 class FakeURLFetcherFactory : public net::FakeURLFetcherFactory { |
| 72 public: | 68 public: |
| 73 FakeURLFetcherFactory(); | 69 FakeURLFetcherFactory(); |
| 74 virtual ~FakeURLFetcherFactory(); | 70 virtual ~FakeURLFetcherFactory(); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 base::MessageLoop message_loop_; | 129 base::MessageLoop message_loop_; |
| 134 base::ScopedTempDir temp_dir_; | 130 base::ScopedTempDir temp_dir_; |
| 135 scoped_ptr<ResourceCache> resource_cache_; | 131 scoped_ptr<ResourceCache> resource_cache_; |
| 136 MockCloudPolicyStore cloud_policy_store_; | 132 MockCloudPolicyStore cloud_policy_store_; |
| 137 scoped_refptr<net::TestURLRequestContextGetter> request_content_getter_; | 133 scoped_refptr<net::TestURLRequestContextGetter> request_content_getter_; |
| 138 FakeURLFetcherFactory fetcher_factory_; | 134 FakeURLFetcherFactory fetcher_factory_; |
| 139 | 135 |
| 140 scoped_ptr<CloudExternalDataManagerBase> external_data_manager_; | 136 scoped_ptr<CloudExternalDataManagerBase> external_data_manager_; |
| 141 | 137 |
| 142 std::map<int, std::string*> callback_data_; | 138 std::map<int, std::string*> callback_data_; |
| 139 PolicyDetailsMap policy_details_; |
| 143 | 140 |
| 144 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataManagerBaseTest); | 141 DISALLOW_COPY_AND_ASSIGN(CloudExternalDataManagerBaseTest); |
| 145 }; | 142 }; |
| 146 | 143 |
| 147 CloudExternalDataManagerBaseTest::CloudExternalDataManagerBaseTest() { | 144 CloudExternalDataManagerBaseTest::CloudExternalDataManagerBaseTest() { |
| 148 } | 145 } |
| 149 | 146 |
| 150 void CloudExternalDataManagerBaseTest::SetUp() { | 147 void CloudExternalDataManagerBaseTest::SetUp() { |
| 151 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 148 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 152 resource_cache_.reset(new ResourceCache(temp_dir_.path(), | 149 resource_cache_.reset(new ResourceCache(temp_dir_.path(), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 164 k10BytePolicy, | 161 k10BytePolicy, |
| 165 ConstructMetadata(k10BytePolicyURL, base::SHA1HashString(k10ByteData))); | 162 ConstructMetadata(k10BytePolicyURL, base::SHA1HashString(k10ByteData))); |
| 166 // Make |k20BytePolicy| reference 20 bytes of external data. | 163 // Make |k20BytePolicy| reference 20 bytes of external data. |
| 167 SetExternalDataReference( | 164 SetExternalDataReference( |
| 168 k20BytePolicy, | 165 k20BytePolicy, |
| 169 ConstructMetadata(k20BytePolicyURL, base::SHA1HashString(k20ByteData))); | 166 ConstructMetadata(k20BytePolicyURL, base::SHA1HashString(k20ByteData))); |
| 170 cloud_policy_store_.NotifyStoreLoaded(); | 167 cloud_policy_store_.NotifyStoreLoaded(); |
| 171 | 168 |
| 172 request_content_getter_ = new net::TestURLRequestContextGetter( | 169 request_content_getter_ = new net::TestURLRequestContextGetter( |
| 173 base::MessageLoopProxy::current()); | 170 base::MessageLoopProxy::current()); |
| 171 |
| 172 policy_details_.SetDetails(kStringPolicy, &kPolicyDetails[0]); |
| 173 policy_details_.SetDetails(k10BytePolicy, &kPolicyDetails[1]); |
| 174 policy_details_.SetDetails(k20BytePolicy, &kPolicyDetails[2]); |
| 174 } | 175 } |
| 175 | 176 |
| 176 void CloudExternalDataManagerBaseTest::TearDown() { | 177 void CloudExternalDataManagerBaseTest::TearDown() { |
| 177 external_data_manager_.reset(); | 178 external_data_manager_.reset(); |
| 178 base::RunLoop().RunUntilIdle(); | 179 base::RunLoop().RunUntilIdle(); |
| 179 ResetCallbackData(); | 180 ResetCallbackData(); |
| 180 } | 181 } |
| 181 | 182 |
| 182 void CloudExternalDataManagerBaseTest::SetUpExternalDataManager() { | 183 void CloudExternalDataManagerBaseTest::SetUpExternalDataManager() { |
| 183 external_data_manager_.reset(new CloudExternalDataManagerBase( | 184 external_data_manager_.reset(new CloudExternalDataManagerBase( |
| 184 &kPolicyDefinitionList, | 185 policy_details_.GetCallback(), |
| 185 message_loop_.message_loop_proxy(), | 186 message_loop_.message_loop_proxy(), |
| 186 message_loop_.message_loop_proxy())); | 187 message_loop_.message_loop_proxy())); |
| 187 external_data_manager_->SetExternalDataStore(make_scoped_ptr( | 188 external_data_manager_->SetExternalDataStore(make_scoped_ptr( |
| 188 new CloudExternalDataStore(kCacheKey, | 189 new CloudExternalDataStore(kCacheKey, |
| 189 message_loop_.message_loop_proxy(), | 190 message_loop_.message_loop_proxy(), |
| 190 resource_cache_.get()))); | 191 resource_cache_.get()))); |
| 191 external_data_manager_->SetPolicyStore(&cloud_policy_store_); | 192 external_data_manager_->SetPolicyStore(&cloud_policy_store_); |
| 192 } | 193 } |
| 193 | 194 |
| 194 scoped_ptr<base::DictionaryValue> | 195 scoped_ptr<base::DictionaryValue> |
| (...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 744 ConstructMetadata(k20BytePolicyURL, base::SHA1HashString(k10ByteData))); | 745 ConstructMetadata(k20BytePolicyURL, base::SHA1HashString(k10ByteData))); |
| 745 cloud_policy_store_.NotifyStoreLoaded(); | 746 cloud_policy_store_.NotifyStoreLoaded(); |
| 746 base::RunLoop().RunUntilIdle(); | 747 base::RunLoop().RunUntilIdle(); |
| 747 EXPECT_EQ(1u, callback_data_.size()); | 748 EXPECT_EQ(1u, callback_data_.size()); |
| 748 ASSERT_TRUE(callback_data_[1]); | 749 ASSERT_TRUE(callback_data_[1]); |
| 749 EXPECT_EQ(k10ByteData, *callback_data_[1]); | 750 EXPECT_EQ(k10ByteData, *callback_data_[1]); |
| 750 ResetCallbackData(); | 751 ResetCallbackData(); |
| 751 } | 752 } |
| 752 | 753 |
| 753 } // namespace policy | 754 } // namespace policy |
| OLD | NEW |