Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "chrome/browser/browser_thread.h" | |
| 11 #include "chrome/browser/policy/config_dir_policy_provider.h" | 12 #include "chrome/browser/policy/config_dir_policy_provider.h" |
| 12 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 13 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 13 #include "chrome/browser/policy/mock_configuration_policy_store.h" | 14 #include "chrome/browser/policy/mock_configuration_policy_store.h" |
| 14 #include "chrome/common/json_value_serializer.h" | 15 #include "chrome/common/json_value_serializer.h" |
| 15 #include "chrome/common/policy_constants.h" | 16 #include "chrome/common/policy_constants.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace policy { | 19 namespace policy { |
| 19 | 20 |
| 20 template<typename BASE> | 21 template<typename BASE> |
| 21 class ConfigDirPolicyProviderTestBase : public BASE { | 22 class ConfigDirPolicyProviderTestBase : public BASE { |
| 22 protected: | 23 protected: |
| 23 ConfigDirPolicyProviderTestBase() {} | 24 ConfigDirPolicyProviderTestBase() {} |
| 24 | 25 |
| 25 virtual void SetUp() { | 26 virtual void SetUp() { |
| 26 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); | 27 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); |
| 27 } | 28 } |
| 28 | 29 |
| 30 virtual void TearDown() { | |
| 31 file_util::Delete(test_dir_.path(), true); | |
|
Mattias Nissler (ping if slow)
2010/12/09 09:27:17
This is not necessary, since ScopedTempDir deletes
danno
2010/12/09 10:23:02
You're looking at an old version. This code is not
| |
| 32 } | |
| 33 | |
| 29 // JSON-encode a dictionary and write it to a file. | 34 // JSON-encode a dictionary and write it to a file. |
| 30 void WriteConfigFile(const DictionaryValue& dict, | 35 void WriteConfigFile(const DictionaryValue& dict, |
| 31 const std::string& file_name) { | 36 const std::string& file_name) { |
| 32 std::string data; | 37 std::string data; |
| 33 JSONStringValueSerializer serializer(&data); | 38 JSONStringValueSerializer serializer(&data); |
| 34 serializer.Serialize(dict); | 39 serializer.Serialize(dict); |
| 35 const FilePath file_path(test_dir().AppendASCII(file_name)); | 40 const FilePath file_path(test_dir().AppendASCII(file_name)); |
| 36 ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size())); | 41 ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size())); |
| 37 } | 42 } |
| 38 | 43 |
| 39 const FilePath& test_dir() { return test_dir_.path(); } | 44 const FilePath& test_dir() { return test_dir_.path(); } |
| 40 | 45 |
| 41 private: | 46 private: |
| 42 ScopedTempDir test_dir_; | 47 ScopedTempDir test_dir_; |
| 43 }; | 48 }; |
| 44 | 49 |
| 45 class ConfigDirPolicyLoaderTest | 50 class ConfigDirPolicyLoaderTest |
| 46 : public ConfigDirPolicyProviderTestBase<testing::Test> { | 51 : public ConfigDirPolicyProviderTestBase<testing::Test> { |
| 47 }; | 52 }; |
| 48 | 53 |
| 49 // The preferences dictionary is expected to be empty when there are no files to | 54 // The preferences dictionary is expected to be empty when there are no files to |
| 50 // load. | 55 // load. |
| 51 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsEmpty) { | 56 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsEmpty) { |
| 52 ConfigDirPolicyLoader loader(test_dir()); | 57 ConfigDirPolicyProviderDelegate loader(test_dir()); |
| 53 scoped_ptr<DictionaryValue> policy(loader.Load()); | 58 scoped_ptr<DictionaryValue> policy(loader.Load()); |
| 54 EXPECT_TRUE(policy.get()); | 59 EXPECT_TRUE(policy.get()); |
| 55 EXPECT_TRUE(policy->empty()); | 60 EXPECT_TRUE(policy->empty()); |
| 56 } | 61 } |
| 57 | 62 |
| 58 // Reading from a non-existent directory should result in an empty preferences | 63 // Reading from a non-existent directory should result in an empty preferences |
| 59 // dictionary. | 64 // dictionary. |
| 60 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsNonExistentDirectory) { | 65 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsNonExistentDirectory) { |
| 61 FilePath non_existent_dir(test_dir().Append(FILE_PATH_LITERAL("not_there"))); | 66 FilePath non_existent_dir(test_dir().Append(FILE_PATH_LITERAL("not_there"))); |
| 62 ConfigDirPolicyLoader loader(non_existent_dir); | 67 ConfigDirPolicyProviderDelegate loader(non_existent_dir); |
| 63 scoped_ptr<DictionaryValue> policy(loader.Load()); | 68 scoped_ptr<DictionaryValue> policy(loader.Load()); |
| 64 EXPECT_TRUE(policy.get()); | 69 EXPECT_TRUE(policy.get()); |
| 65 EXPECT_TRUE(policy->empty()); | 70 EXPECT_TRUE(policy->empty()); |
| 66 } | 71 } |
| 67 | 72 |
| 68 // Test reading back a single preference value. | 73 // Test reading back a single preference value. |
| 69 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsSinglePref) { | 74 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsSinglePref) { |
| 70 DictionaryValue test_dict; | 75 DictionaryValue test_dict; |
| 71 test_dict.SetString("HomepageLocation", "http://www.google.com"); | 76 test_dict.SetString("HomepageLocation", "http://www.google.com"); |
| 72 WriteConfigFile(test_dict, "config_file"); | 77 WriteConfigFile(test_dict, "config_file"); |
| 73 | 78 |
| 74 ConfigDirPolicyLoader loader(test_dir()); | 79 ConfigDirPolicyProviderDelegate loader(test_dir()); |
| 75 scoped_ptr<DictionaryValue> policy(loader.Load()); | 80 scoped_ptr<DictionaryValue> policy(loader.Load()); |
| 76 EXPECT_TRUE(policy.get()); | 81 EXPECT_TRUE(policy.get()); |
| 77 EXPECT_TRUE(policy->Equals(&test_dict)); | 82 EXPECT_TRUE(policy->Equals(&test_dict)); |
| 78 } | 83 } |
| 79 | 84 |
| 80 // Test merging values from different files. | 85 // Test merging values from different files. |
| 81 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsMergePrefs) { | 86 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsMergePrefs) { |
| 82 // Write a bunch of data files in order to increase the chance to detect the | 87 // Write a bunch of data files in order to increase the chance to detect the |
| 83 // provider not respecting lexicographic ordering when reading them. Since the | 88 // provider not respecting lexicographic ordering when reading them. Since the |
| 84 // filesystem may return files in arbitrary order, there is no way to be sure, | 89 // filesystem may return files in arbitrary order, there is no way to be sure, |
| 85 // but this is better than nothing. | 90 // but this is better than nothing. |
| 86 DictionaryValue test_dict_bar; | 91 DictionaryValue test_dict_bar; |
| 87 test_dict_bar.SetString("HomepageLocation", "http://bar.com"); | 92 test_dict_bar.SetString("HomepageLocation", "http://bar.com"); |
| 88 for (unsigned int i = 1; i <= 4; ++i) | 93 for (unsigned int i = 1; i <= 4; ++i) |
| 89 WriteConfigFile(test_dict_bar, base::IntToString(i)); | 94 WriteConfigFile(test_dict_bar, base::IntToString(i)); |
| 90 DictionaryValue test_dict_foo; | 95 DictionaryValue test_dict_foo; |
| 91 test_dict_foo.SetString("HomepageLocation", "http://foo.com"); | 96 test_dict_foo.SetString("HomepageLocation", "http://foo.com"); |
| 92 WriteConfigFile(test_dict_foo, "9"); | 97 WriteConfigFile(test_dict_foo, "9"); |
| 93 for (unsigned int i = 5; i <= 8; ++i) | 98 for (unsigned int i = 5; i <= 8; ++i) |
| 94 WriteConfigFile(test_dict_bar, base::IntToString(i)); | 99 WriteConfigFile(test_dict_bar, base::IntToString(i)); |
| 95 | 100 |
| 96 ConfigDirPolicyLoader loader(test_dir()); | 101 ConfigDirPolicyProviderDelegate loader(test_dir()); |
| 97 scoped_ptr<DictionaryValue> policy(loader.Load()); | 102 scoped_ptr<DictionaryValue> policy(loader.Load()); |
| 98 EXPECT_TRUE(policy.get()); | 103 EXPECT_TRUE(policy.get()); |
| 99 EXPECT_TRUE(policy->Equals(&test_dict_foo)); | 104 EXPECT_TRUE(policy->Equals(&test_dict_foo)); |
| 100 } | 105 } |
| 101 | 106 |
| 102 // Holds policy type, corresponding policy key string and a valid value for use | 107 // Holds policy type, corresponding policy key string and a valid value for use |
| 103 // in parametrized value tests. | 108 // in parametrized value tests. |
| 104 class ValueTestParams { | 109 class ValueTestParams { |
| 105 public: | 110 public: |
| 106 // Assumes ownership of |test_value|. | 111 // Assumes ownership of |test_value|. |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 309 kPolicyExtensionInstallDenyList, | 314 kPolicyExtensionInstallDenyList, |
| 310 key::kExtensionInstallDenyList), | 315 key::kExtensionInstallDenyList), |
| 311 ValueTestParams::ForBooleanPolicy( | 316 ValueTestParams::ForBooleanPolicy( |
| 312 kPolicyShowHomeButton, | 317 kPolicyShowHomeButton, |
| 313 key::kShowHomeButton), | 318 key::kShowHomeButton), |
| 314 ValueTestParams::ForBooleanPolicy( | 319 ValueTestParams::ForBooleanPolicy( |
| 315 kPolicyPrintingEnabled, | 320 kPolicyPrintingEnabled, |
| 316 key::kPrintingEnabled))); | 321 key::kPrintingEnabled))); |
| 317 | 322 |
| 318 } // namespace policy | 323 } // namespace policy |
| OLD | NEW |