OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/file_util.h" |
| 6 #include "base/path_service.h" |
| 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/config_dir_policy_provider.h" |
| 9 #include "chrome/browser/mock_configuration_policy_store.h" |
| 10 #include "chrome/common/json_value_serializer.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 class ConfigDirPolicyProviderTest : public testing::Test { |
| 14 protected: |
| 15 virtual void SetUp() { |
| 16 // Determine the directory to use for testing. |
| 17 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
| 18 test_dir_ = |
| 19 test_dir_.Append(FILE_PATH_LITERAL("ConfigDirPolicyProviderTest")); |
| 20 |
| 21 // Make sure the directory is fresh. |
| 22 file_util::Delete(test_dir_, true); |
| 23 file_util::CreateDirectory(test_dir_); |
| 24 ASSERT_TRUE(file_util::DirectoryExists(test_dir_)); |
| 25 |
| 26 // Create a fresh policy store mock. |
| 27 policy_store_.reset(new MockConfigurationPolicyStore()); |
| 28 } |
| 29 |
| 30 virtual void TearDown() { |
| 31 // Clean up test directory. |
| 32 ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
| 33 ASSERT_FALSE(file_util::PathExists(test_dir_)); |
| 34 } |
| 35 |
| 36 // JSON-encode a dictionary and write it to a file. |
| 37 void WriteConfigFile(const DictionaryValue& dict, |
| 38 const std::string& file_name) { |
| 39 std::string data; |
| 40 JSONStringValueSerializer serializer(&data); |
| 41 serializer.Serialize(dict); |
| 42 FilePath file_path(test_dir_.AppendASCII(file_name)); |
| 43 file_util::WriteFile(file_path, data.c_str(), data.size()); |
| 44 } |
| 45 |
| 46 FilePath test_dir_; |
| 47 scoped_ptr<MockConfigurationPolicyStore> policy_store_; |
| 48 }; |
| 49 |
| 50 // The preferences dictionary is expected to be empty when there are no files to |
| 51 // load. |
| 52 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsEmpty) { |
| 53 ConfigDirPolicyProvider provider(test_dir_); |
| 54 EXPECT_TRUE(provider.Provide(policy_store_.get())); |
| 55 EXPECT_TRUE(policy_store_->policy_map().empty()); |
| 56 } |
| 57 |
| 58 // Reading from a non-existent directory should result in an empty preferences |
| 59 // dictionary. |
| 60 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsNonExistentDirectory) { |
| 61 FilePath non_existent_dir(test_dir_.Append(FILE_PATH_LITERAL("not_there"))); |
| 62 ConfigDirPolicyProvider provider(non_existent_dir); |
| 63 EXPECT_TRUE(provider.Provide(policy_store_.get())); |
| 64 EXPECT_TRUE(policy_store_->policy_map().empty()); |
| 65 } |
| 66 |
| 67 // Test reading back a single preference value. |
| 68 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsSinglePref) { |
| 69 DictionaryValue test_dict; |
| 70 test_dict.SetString(L"homepage", L"http://www.google.com"); |
| 71 WriteConfigFile(test_dict, "config_file"); |
| 72 ConfigDirPolicyProvider provider(test_dir_); |
| 73 |
| 74 EXPECT_TRUE(provider.Provide(policy_store_.get())); |
| 75 const MockConfigurationPolicyStore::PolicyMap& policy_map( |
| 76 policy_store_->policy_map()); |
| 77 EXPECT_EQ(1U, policy_map.size()); |
| 78 MockConfigurationPolicyStore::PolicyMap::const_iterator entry = |
| 79 policy_map.find(ConfigurationPolicyStore::kPolicyHomePage); |
| 80 ASSERT_TRUE(entry != policy_map.end()); |
| 81 |
| 82 std::wstring str_value; |
| 83 EXPECT_TRUE(entry->second->GetAsString(&str_value)); |
| 84 EXPECT_EQ(L"http://www.google.com", str_value); |
| 85 } |
| 86 |
| 87 // Test merging values from different files. |
| 88 TEST_F(ConfigDirPolicyProviderTest, ReadPrefsMergePrefs) { |
| 89 // Write a bunch of data files in order to increase the chance to detect the |
| 90 // provider not respecting lexicographic ordering when reading them. Since the |
| 91 // filesystem may return files in arbitrary order, there is no way to be sure, |
| 92 // but this is better than nothing. |
| 93 DictionaryValue test_dict_bar; |
| 94 test_dict_bar.SetString(L"homepage", L"http://bar.com"); |
| 95 for (unsigned int i = 1; i <= 4; ++i) |
| 96 WriteConfigFile(test_dict_bar, IntToString(i)); |
| 97 DictionaryValue test_dict_foo; |
| 98 test_dict_foo.SetString(L"homepage", L"http://foo.com"); |
| 99 WriteConfigFile(test_dict_foo, "9"); |
| 100 for (unsigned int i = 5; i <= 8; ++i) |
| 101 WriteConfigFile(test_dict_bar, IntToString(i)); |
| 102 ConfigDirPolicyProvider provider(test_dir_); |
| 103 |
| 104 EXPECT_TRUE(provider.Provide(policy_store_.get())); |
| 105 const MockConfigurationPolicyStore::PolicyMap& policy_map( |
| 106 policy_store_->policy_map()); |
| 107 EXPECT_EQ(1U, policy_map.size()); |
| 108 MockConfigurationPolicyStore::PolicyMap::const_iterator entry = |
| 109 policy_map.find(ConfigurationPolicyStore::kPolicyHomePage); |
| 110 ASSERT_TRUE(entry != policy_map.end()); |
| 111 |
| 112 std::wstring str_value; |
| 113 EXPECT_TRUE(entry->second->GetAsString(&str_value)); |
| 114 EXPECT_EQ(L"http://foo.com", str_value); |
| 115 } |
OLD | NEW |