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/chromeos/policy/cloud_external_data_manager_base_test_util.h" 6 7 #include "base/callback.h" 8 #include "base/memory/weak_ptr.h" 9 #include "base/sha1.h" 10 #include "base/strings/string_number_conversions.h" 11 #include "base/values.h" 12 #include "components/policy/core/common/cloud/cloud_external_data_manager.h" 13 #include "components/policy/core/common/cloud/cloud_policy_core.h" 14 #include "components/policy/core/common/cloud/cloud_policy_store.h" 15 #include "components/policy/core/common/external_data_fetcher.h" 16 #include "components/policy/core/common/policy_map.h" 17 #include "components/policy/core/common/policy_types.h" 18 #include "testing/gtest/include/gtest/gtest.h" 19 20 namespace policy { 21 namespace test { 22 23 void ExternalDataFetchCallback(scoped_ptr<std::string>* destination, 24 const base::Closure& done_callback, 25 scoped_ptr<std::string> data) { 26 *destination = data.Pass(); 27 done_callback.Run(); 28 } 29 30 scoped_ptr<base::DictionaryValue> ConstructExternalDataReference( 31 const std::string& url, 32 const std::string& data) { 33 const std::string hash = base::SHA1HashString(data); 34 scoped_ptr<base::DictionaryValue> metadata(new base::DictionaryValue); 35 metadata->SetStringWithoutPathExpansion("url", url); 36 metadata->SetStringWithoutPathExpansion("hash", base::HexEncode(hash.c_str(), 37 hash.size())); 38 return metadata.Pass(); 39 } 40 41 void SetExternalDataReference(CloudPolicyCore* core, 42 const std::string& policy, 43 scoped_ptr<base::DictionaryValue> metadata) { 44 CloudPolicyStore* store = core->store(); 45 ASSERT_TRUE(store); 46 PolicyMap policy_map; 47 policy_map.Set(policy, 48 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 49 metadata.release(), 50 new ExternalDataFetcher(store->external_data_manager(), 51 policy)); 52 store->SetPolicyMapForTesting(policy_map); 53 } 54 55 } // namespace test 56 } // namespace policy 57