| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "base/compiler_specific.h" | |
| 6 #include "base/file_util.h" | |
| 7 #include "base/json/json_string_value_serializer.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/scoped_temp_dir.h" | |
| 10 #include "base/string_number_conversions.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/policy/config_dir_policy_provider.h" | |
| 13 #include "chrome/browser/policy/configuration_policy_provider_test.h" | |
| 14 #include "chrome/browser/policy/policy_bundle.h" | |
| 15 #include "chrome/browser/policy/policy_map.h" | |
| 16 | |
| 17 namespace policy { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 class TestHarness : public PolicyProviderTestHarness { | |
| 22 public: | |
| 23 TestHarness(); | |
| 24 virtual ~TestHarness(); | |
| 25 | |
| 26 virtual void SetUp() OVERRIDE; | |
| 27 | |
| 28 virtual ConfigurationPolicyProvider* CreateProvider( | |
| 29 const PolicyDefinitionList* policy_definition_list) OVERRIDE; | |
| 30 | |
| 31 virtual void InstallEmptyPolicy() OVERRIDE; | |
| 32 virtual void InstallStringPolicy(const std::string& policy_name, | |
| 33 const std::string& policy_value) OVERRIDE; | |
| 34 virtual void InstallIntegerPolicy(const std::string& policy_name, | |
| 35 int policy_value) OVERRIDE; | |
| 36 virtual void InstallBooleanPolicy(const std::string& policy_name, | |
| 37 bool policy_value) OVERRIDE; | |
| 38 virtual void InstallStringListPolicy( | |
| 39 const std::string& policy_name, | |
| 40 const base::ListValue* policy_value) OVERRIDE; | |
| 41 virtual void InstallDictionaryPolicy( | |
| 42 const std::string& policy_name, | |
| 43 const base::DictionaryValue* policy_value) OVERRIDE; | |
| 44 | |
| 45 const FilePath& test_dir() { return test_dir_.path(); } | |
| 46 | |
| 47 // JSON-encode a dictionary and write it to a file. | |
| 48 void WriteConfigFile(const base::DictionaryValue& dict, | |
| 49 const std::string& file_name); | |
| 50 | |
| 51 static PolicyProviderTestHarness* Create(); | |
| 52 | |
| 53 private: | |
| 54 ScopedTempDir test_dir_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(TestHarness); | |
| 57 }; | |
| 58 | |
| 59 TestHarness::TestHarness() | |
| 60 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE) {} | |
| 61 | |
| 62 TestHarness::~TestHarness() {} | |
| 63 | |
| 64 void TestHarness::SetUp() { | |
| 65 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); | |
| 66 } | |
| 67 | |
| 68 ConfigurationPolicyProvider* TestHarness::CreateProvider( | |
| 69 const PolicyDefinitionList* policy_definition_list) { | |
| 70 return new ConfigDirPolicyProvider(policy_definition_list, | |
| 71 POLICY_LEVEL_MANDATORY, | |
| 72 POLICY_SCOPE_MACHINE, | |
| 73 test_dir()); | |
| 74 } | |
| 75 | |
| 76 void TestHarness::InstallEmptyPolicy() { | |
| 77 base::DictionaryValue dict; | |
| 78 WriteConfigFile(dict, "policy"); | |
| 79 } | |
| 80 | |
| 81 void TestHarness::InstallStringPolicy(const std::string& policy_name, | |
| 82 const std::string& policy_value) { | |
| 83 base::DictionaryValue dict; | |
| 84 dict.SetString(policy_name, policy_value); | |
| 85 WriteConfigFile(dict, "policy"); | |
| 86 } | |
| 87 | |
| 88 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, | |
| 89 int policy_value) { | |
| 90 base::DictionaryValue dict; | |
| 91 dict.SetInteger(policy_name, policy_value); | |
| 92 WriteConfigFile(dict, "policy"); | |
| 93 } | |
| 94 | |
| 95 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, | |
| 96 bool policy_value) { | |
| 97 base::DictionaryValue dict; | |
| 98 dict.SetBoolean(policy_name, policy_value); | |
| 99 WriteConfigFile(dict, "policy"); | |
| 100 } | |
| 101 | |
| 102 void TestHarness::InstallStringListPolicy(const std::string& policy_name, | |
| 103 const base::ListValue* policy_value) { | |
| 104 base::DictionaryValue dict; | |
| 105 dict.Set(policy_name, policy_value->DeepCopy()); | |
| 106 WriteConfigFile(dict, "policy"); | |
| 107 } | |
| 108 | |
| 109 void TestHarness::InstallDictionaryPolicy( | |
| 110 const std::string& policy_name, | |
| 111 const base::DictionaryValue* policy_value) { | |
| 112 base::DictionaryValue dict; | |
| 113 dict.Set(policy_name, policy_value->DeepCopy()); | |
| 114 WriteConfigFile(dict, "policy"); | |
| 115 } | |
| 116 | |
| 117 void TestHarness::WriteConfigFile(const base::DictionaryValue& dict, | |
| 118 const std::string& file_name) { | |
| 119 std::string data; | |
| 120 JSONStringValueSerializer serializer(&data); | |
| 121 serializer.Serialize(dict); | |
| 122 const FilePath file_path(test_dir().AppendASCII(file_name)); | |
| 123 ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size())); | |
| 124 } | |
| 125 | |
| 126 // static | |
| 127 PolicyProviderTestHarness* TestHarness::Create() { | |
| 128 return new TestHarness(); | |
| 129 } | |
| 130 | |
| 131 } // namespace | |
| 132 | |
| 133 // Instantiate abstract test case for basic policy reading tests. | |
| 134 INSTANTIATE_TEST_CASE_P( | |
| 135 ConfigDirPolicyProviderTest, | |
| 136 ConfigurationPolicyProviderTest, | |
| 137 testing::Values(TestHarness::Create)); | |
| 138 | |
| 139 // Some tests that exercise special functionality in ConfigDirPolicyLoader. | |
| 140 class ConfigDirPolicyLoaderTest : public testing::Test { | |
| 141 protected: | |
| 142 void SetUp() { | |
| 143 harness_.SetUp(); | |
| 144 } | |
| 145 | |
| 146 TestHarness harness_; | |
| 147 }; | |
| 148 | |
| 149 // The preferences dictionary is expected to be empty when there are no files to | |
| 150 // load. | |
| 151 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsEmpty) { | |
| 152 ConfigDirPolicyProviderDelegate loader(harness_.test_dir(), | |
| 153 POLICY_LEVEL_MANDATORY, | |
| 154 POLICY_SCOPE_MACHINE); | |
| 155 scoped_ptr<PolicyBundle> bundle(loader.Load()); | |
| 156 ASSERT_TRUE(bundle.get()); | |
| 157 const PolicyBundle kEmptyBundle; | |
| 158 EXPECT_TRUE(bundle->Equals(kEmptyBundle)); | |
| 159 } | |
| 160 | |
| 161 // Reading from a non-existent directory should result in an empty preferences | |
| 162 // dictionary. | |
| 163 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsNonExistentDirectory) { | |
| 164 FilePath non_existent_dir( | |
| 165 harness_.test_dir().Append(FILE_PATH_LITERAL("not_there"))); | |
| 166 ConfigDirPolicyProviderDelegate loader(non_existent_dir, | |
| 167 POLICY_LEVEL_MANDATORY, | |
| 168 POLICY_SCOPE_MACHINE); | |
| 169 scoped_ptr<PolicyBundle> bundle(loader.Load()); | |
| 170 ASSERT_TRUE(bundle.get()); | |
| 171 const PolicyBundle kEmptyBundle; | |
| 172 EXPECT_TRUE(bundle->Equals(kEmptyBundle)); | |
| 173 } | |
| 174 | |
| 175 // Test merging values from different files. | |
| 176 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsMergePrefs) { | |
| 177 // Write a bunch of data files in order to increase the chance to detect the | |
| 178 // provider not respecting lexicographic ordering when reading them. Since the | |
| 179 // filesystem may return files in arbitrary order, there is no way to be sure, | |
| 180 // but this is better than nothing. | |
| 181 base::DictionaryValue test_dict_bar; | |
| 182 test_dict_bar.SetString("HomepageLocation", "http://bar.com"); | |
| 183 for (unsigned int i = 1; i <= 4; ++i) | |
| 184 harness_.WriteConfigFile(test_dict_bar, base::IntToString(i)); | |
| 185 base::DictionaryValue test_dict_foo; | |
| 186 test_dict_foo.SetString("HomepageLocation", "http://foo.com"); | |
| 187 harness_.WriteConfigFile(test_dict_foo, "9"); | |
| 188 for (unsigned int i = 5; i <= 8; ++i) | |
| 189 harness_.WriteConfigFile(test_dict_bar, base::IntToString(i)); | |
| 190 | |
| 191 ConfigDirPolicyProviderDelegate loader(harness_.test_dir(), | |
| 192 POLICY_LEVEL_MANDATORY, | |
| 193 POLICY_SCOPE_USER); | |
| 194 scoped_ptr<PolicyBundle> bundle(loader.Load()); | |
| 195 ASSERT_TRUE(bundle.get()); | |
| 196 PolicyBundle expected_bundle; | |
| 197 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") | |
| 198 .LoadFrom(&test_dict_foo, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | |
| 199 EXPECT_TRUE(bundle->Equals(expected_bundle)); | |
| 200 } | |
| 201 | |
| 202 // Tests loading of policy for 3rd parties. | |
| 203 TEST_F(ConfigDirPolicyLoaderTest, Load3rdParty) { | |
| 204 base::DictionaryValue policy_dict; | |
| 205 policy_dict.SetBoolean("bool", true); | |
| 206 policy_dict.SetString("str", "string value"); | |
| 207 policy_dict.SetDouble("double", 123.456); | |
| 208 policy_dict.SetInteger("int", 789); | |
| 209 | |
| 210 base::ListValue* list = new base::ListValue(); | |
| 211 for (int i = 0; i < 5; ++i) { | |
| 212 base::DictionaryValue* dict = new base::DictionaryValue(); | |
| 213 dict->SetInteger("subdictindex", i); | |
| 214 dict->Set("subdict", policy_dict.DeepCopy()); | |
| 215 list->Append(dict); | |
| 216 } | |
| 217 policy_dict.Set("list", list); | |
| 218 | |
| 219 base::DictionaryValue json_dict; | |
| 220 // Merge |policy_dict|, which will become the chrome policies. | |
| 221 json_dict.MergeDictionary(&policy_dict); | |
| 222 json_dict.Set("3rdparty.extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | |
| 223 policy_dict.DeepCopy()); | |
| 224 json_dict.Set("3rdparty.extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", | |
| 225 policy_dict.DeepCopy()); | |
| 226 | |
| 227 harness_.WriteConfigFile(json_dict, "policy.json"); | |
| 228 ConfigDirPolicyProviderDelegate loader(harness_.test_dir(), | |
| 229 POLICY_LEVEL_MANDATORY, | |
| 230 POLICY_SCOPE_USER); | |
| 231 scoped_ptr<PolicyBundle> bundle(loader.Load()); | |
| 232 ASSERT_TRUE(bundle.get()); | |
| 233 PolicyMap expected_policy; | |
| 234 expected_policy.LoadFrom(&policy_dict, | |
| 235 POLICY_LEVEL_MANDATORY, | |
| 236 POLICY_SCOPE_USER); | |
| 237 PolicyBundle expected_bundle; | |
| 238 expected_bundle.Get(POLICY_DOMAIN_CHROME, "").CopyFrom(expected_policy); | |
| 239 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, | |
| 240 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") | |
| 241 .CopyFrom(expected_policy); | |
| 242 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, | |
| 243 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") | |
| 244 .CopyFrom(expected_policy); | |
| 245 EXPECT_TRUE(bundle->Equals(expected_bundle)); | |
| 246 } | |
| 247 | |
| 248 } // namespace policy | |
| OLD | NEW |