| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/bind.h" | 7 #include "base/bind.h" |
| 9 #include "base/callback.h" | 8 #include "base/callback.h" |
| 10 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 11 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 15 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 16 #include "base/sha1.h" | |
| 17 #include "base/strings/string_number_conversions.h" | |
| 18 #include "base/values.h" | 14 #include "base/values.h" |
| 19 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base.h" | 15 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base.h" |
| 16 #include "chrome/browser/chromeos/policy/cloud_external_data_manager_base_test_u
til.h" |
| 20 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" | 17 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" |
| 21 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom
eos.h" | 18 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom
eos.h" |
| 22 #include "chrome/browser/policy/cloud/cloud_external_data_manager.h" | |
| 23 #include "chrome/browser/policy/cloud/cloud_policy_core.h" | 19 #include "chrome/browser/policy/cloud/cloud_policy_core.h" |
| 24 #include "chrome/browser/policy/cloud/cloud_policy_store.h" | |
| 25 #include "chrome/browser/policy/external_data_fetcher.h" | 20 #include "chrome/browser/policy/external_data_fetcher.h" |
| 26 #include "chrome/browser/policy/policy_map.h" | 21 #include "chrome/browser/policy/policy_map.h" |
| 27 #include "chrome/browser/policy/policy_service.h" | 22 #include "chrome/browser/policy/policy_service.h" |
| 28 #include "chrome/browser/policy/policy_types.h" | |
| 29 #include "chrome/browser/policy/profile_policy_connector.h" | 23 #include "chrome/browser/policy/profile_policy_connector.h" |
| 30 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 24 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 31 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 32 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 33 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
| 34 #include "chrome/test/base/in_process_browser_test.h" | 28 #include "chrome/test/base/in_process_browser_test.h" |
| 35 #include "content/public/test/test_utils.h" | 29 #include "content/public/test/test_utils.h" |
| 36 #include "net/test/embedded_test_server/embedded_test_server.h" | 30 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 37 #include "policy/policy_constants.h" | 31 #include "policy/policy_constants.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 39 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 40 | 34 |
| 41 namespace policy { | 35 namespace policy { |
| 42 | 36 |
| 43 namespace { | 37 namespace { |
| 44 | 38 |
| 45 const char kExternalDataPath[] = "policy/blank.html"; | 39 const char kExternalDataPath[] = "policy/blank.html"; |
| 46 | 40 |
| 47 void ExternalDataFetchCallback(scoped_ptr<std::string>* destination, | |
| 48 const base::Closure& done_callback, | |
| 49 scoped_ptr<std::string> data) { | |
| 50 destination->reset(data.release()); | |
| 51 done_callback.Run(); | |
| 52 } | |
| 53 | |
| 54 } // namespace | 41 } // namespace |
| 55 | 42 |
| 56 class UserCloudExternalDataManagerTest : public InProcessBrowserTest { | 43 typedef InProcessBrowserTest UserCloudExternalDataManagerTest; |
| 57 protected: | |
| 58 UserCloudExternalDataManagerTest(); | |
| 59 virtual ~UserCloudExternalDataManagerTest(); | |
| 60 | |
| 61 scoped_ptr<base::DictionaryValue> ConstructMetadata(const std::string& url, | |
| 62 const std::string& hash); | |
| 63 void SetExternalDataReference(const std::string& policy, | |
| 64 scoped_ptr<base::DictionaryValue> metadata); | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(UserCloudExternalDataManagerTest); | |
| 67 }; | |
| 68 | |
| 69 UserCloudExternalDataManagerTest::UserCloudExternalDataManagerTest() { | |
| 70 } | |
| 71 | |
| 72 UserCloudExternalDataManagerTest::~UserCloudExternalDataManagerTest() { | |
| 73 } | |
| 74 | |
| 75 scoped_ptr<base::DictionaryValue> | |
| 76 UserCloudExternalDataManagerTest::ConstructMetadata( | |
| 77 const std::string& url, | |
| 78 const std::string& hash) { | |
| 79 scoped_ptr<base::DictionaryValue> metadata(new base::DictionaryValue); | |
| 80 metadata->SetStringWithoutPathExpansion("url", url); | |
| 81 metadata->SetStringWithoutPathExpansion("hash", base::HexEncode(hash.c_str(), | |
| 82 hash.size())); | |
| 83 return metadata.Pass(); | |
| 84 } | |
| 85 | |
| 86 void UserCloudExternalDataManagerTest::SetExternalDataReference( | |
| 87 const std::string& policy, | |
| 88 scoped_ptr<base::DictionaryValue> metadata) { | |
| 89 #if defined(OS_CHROMEOS) | |
| 90 UserCloudPolicyManagerChromeOS* policy_manager = | |
| 91 UserCloudPolicyManagerFactoryChromeOS::GetForProfile( | |
| 92 browser()->profile()); | |
| 93 #else | |
| 94 UserCloudPolicyManager* policy_manager = | |
| 95 UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); | |
| 96 #endif | |
| 97 ASSERT_TRUE(policy_manager); | |
| 98 CloudPolicyStore* store = policy_manager->core()->store(); | |
| 99 ASSERT_TRUE(store); | |
| 100 PolicyMap policy_map; | |
| 101 policy_map.Set(policy, | |
| 102 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
| 103 metadata.release(), | |
| 104 new ExternalDataFetcher(store->external_data_manager(), | |
| 105 policy)); | |
| 106 store->SetPolicyMapForTesting(policy_map); | |
| 107 } | |
| 108 | 44 |
| 109 IN_PROC_BROWSER_TEST_F(UserCloudExternalDataManagerTest, FetchExternalData) { | 45 IN_PROC_BROWSER_TEST_F(UserCloudExternalDataManagerTest, FetchExternalData) { |
| 110 CloudExternalDataManagerBase::SetMaxExternalDataSizeForTesting(1000); | 46 CloudExternalDataManagerBase::SetMaxExternalDataSizeForTesting(1000); |
| 111 | 47 |
| 112 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 48 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 113 const GURL url = | 49 const GURL url = |
| 114 embedded_test_server()->GetURL(std::string("/") + kExternalDataPath); | 50 embedded_test_server()->GetURL(std::string("/") + kExternalDataPath); |
| 115 | 51 |
| 116 base::FilePath test_dir; | 52 base::FilePath test_dir; |
| 117 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); | 53 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir)); |
| 118 std::string external_data; | 54 std::string external_data; |
| 119 ASSERT_TRUE(base::ReadFileToString(test_dir.AppendASCII(kExternalDataPath), | 55 ASSERT_TRUE(base::ReadFileToString(test_dir.AppendASCII(kExternalDataPath), |
| 120 &external_data)); | 56 &external_data)); |
| 121 ASSERT_FALSE(external_data.empty()); | 57 ASSERT_FALSE(external_data.empty()); |
| 122 | 58 |
| 123 scoped_ptr<base::DictionaryValue> metadata = | 59 scoped_ptr<base::DictionaryValue> metadata = |
| 124 ConstructMetadata(url.spec(), base::SHA1HashString(external_data)); | 60 test::ConstructExternalDataReference(url.spec(), external_data); |
| 61 #if defined(OS_CHROMEOS) |
| 62 UserCloudPolicyManagerChromeOS* policy_manager = |
| 63 UserCloudPolicyManagerFactoryChromeOS::GetForProfile( |
| 64 browser()->profile()); |
| 65 #else |
| 66 UserCloudPolicyManager* policy_manager = |
| 67 UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); |
| 68 #endif |
| 69 ASSERT_TRUE(policy_manager); |
| 125 // TODO(bartfab): The test injects an ExternalDataFetcher for an arbitrary | 70 // TODO(bartfab): The test injects an ExternalDataFetcher for an arbitrary |
| 126 // policy. This is only done because there are no policies that reference | 71 // policy. This is only done because there are no policies that reference |
| 127 // external data yet. Once the first such policy is added, switch the test to | 72 // external data yet. Once the first such policy is added, switch the test to |
| 128 // that policy and stop injecting a manually instantiated ExternalDataFetcher. | 73 // that policy and stop injecting a manually instantiated ExternalDataFetcher. |
| 129 SetExternalDataReference(key::kHomepageLocation, | 74 test::SetExternalDataReference(policy_manager->core(), |
| 130 make_scoped_ptr(metadata->DeepCopy())); | 75 key::kHomepageLocation, |
| 76 make_scoped_ptr(metadata->DeepCopy())); |
| 131 content::RunAllPendingInMessageLoop(); | 77 content::RunAllPendingInMessageLoop(); |
| 132 | 78 |
| 133 ProfilePolicyConnector* policy_connector = | 79 ProfilePolicyConnector* policy_connector = |
| 134 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); | 80 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); |
| 135 ASSERT_TRUE(policy_connector); | 81 ASSERT_TRUE(policy_connector); |
| 136 const PolicyMap& policies = policy_connector->policy_service()->GetPolicies( | 82 const PolicyMap& policies = policy_connector->policy_service()->GetPolicies( |
| 137 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | 83 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); |
| 138 const PolicyMap::Entry* policy_entry = policies.Get(key::kHomepageLocation); | 84 const PolicyMap::Entry* policy_entry = policies.Get(key::kHomepageLocation); |
| 139 ASSERT_TRUE(policy_entry); | 85 ASSERT_TRUE(policy_entry); |
| 140 EXPECT_TRUE(base::Value::Equals(metadata.get(), policy_entry->value)); | 86 EXPECT_TRUE(base::Value::Equals(metadata.get(), policy_entry->value)); |
| 141 ASSERT_TRUE(policy_entry->external_data_fetcher); | 87 ASSERT_TRUE(policy_entry->external_data_fetcher); |
| 142 | 88 |
| 143 base::RunLoop run_loop; | 89 base::RunLoop run_loop; |
| 144 scoped_ptr<std::string> fetched_external_data; | 90 scoped_ptr<std::string> fetched_external_data; |
| 145 policy_entry->external_data_fetcher->Fetch(base::Bind( | 91 policy_entry->external_data_fetcher->Fetch(base::Bind( |
| 146 &ExternalDataFetchCallback, | 92 &test::ExternalDataFetchCallback, |
| 147 &fetched_external_data, | 93 &fetched_external_data, |
| 148 run_loop.QuitClosure())); | 94 run_loop.QuitClosure())); |
| 149 run_loop.Run(); | 95 run_loop.Run(); |
| 150 | 96 |
| 151 ASSERT_TRUE(fetched_external_data); | 97 ASSERT_TRUE(fetched_external_data); |
| 152 EXPECT_EQ(external_data, *fetched_external_data); | 98 EXPECT_EQ(external_data, *fetched_external_data); |
| 153 } | 99 } |
| 154 | 100 |
| 155 } // namespace policy | 101 } // namespace policy |
| OLD | NEW |