Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: chrome/browser/policy/config_dir_policy_loader_unittest.cc

Issue 10443108: Implement the ConfigDirPolicyProvider based on the AsyncPolicyLoader (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments, added FilePathWatchers Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "base/compiler_specific.h" 5 #include "base/compiler_specific.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/json/json_string_value_serializer.h" 7 #include "base/json/json_string_value_serializer.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.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 "base/values.h" 11 #include "base/values.h"
12 #include "chrome/browser/policy/config_dir_policy_provider.h" 12 #include "chrome/browser/policy/async_policy_provider.h"
13 #include "chrome/browser/policy/config_dir_policy_loader.h"
13 #include "chrome/browser/policy/configuration_policy_provider_test.h" 14 #include "chrome/browser/policy/configuration_policy_provider_test.h"
14 #include "chrome/browser/policy/policy_bundle.h" 15 #include "chrome/browser/policy/policy_bundle.h"
15 #include "chrome/browser/policy/policy_map.h" 16 #include "chrome/browser/policy/policy_map.h"
16 17
17 namespace policy { 18 namespace policy {
18 19
19 namespace { 20 namespace {
20 21
22 // Subdirectory of the config dir that contains mandatory policies.
23 const FilePath::CharType kMandatoryPath[] = FILE_PATH_LITERAL("managed");
24
21 class TestHarness : public PolicyProviderTestHarness { 25 class TestHarness : public PolicyProviderTestHarness {
22 public: 26 public:
23 TestHarness(); 27 TestHarness();
24 virtual ~TestHarness(); 28 virtual ~TestHarness();
25 29
26 virtual void SetUp() OVERRIDE; 30 virtual void SetUp() OVERRIDE;
27 31
28 virtual ConfigurationPolicyProvider* CreateProvider( 32 virtual ConfigurationPolicyProvider* CreateProvider(
29 const PolicyDefinitionList* policy_definition_list) OVERRIDE; 33 const PolicyDefinitionList* policy_definition_list) OVERRIDE;
30 34
(...skipping 29 matching lines...) Expand all
60 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE) {} 64 : PolicyProviderTestHarness(POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE) {}
61 65
62 TestHarness::~TestHarness() {} 66 TestHarness::~TestHarness() {}
63 67
64 void TestHarness::SetUp() { 68 void TestHarness::SetUp() {
65 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); 69 ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
66 } 70 }
67 71
68 ConfigurationPolicyProvider* TestHarness::CreateProvider( 72 ConfigurationPolicyProvider* TestHarness::CreateProvider(
69 const PolicyDefinitionList* policy_definition_list) { 73 const PolicyDefinitionList* policy_definition_list) {
70 return new ConfigDirPolicyProvider(policy_definition_list, 74 scoped_ptr<AsyncPolicyLoader> loader(
71 POLICY_LEVEL_MANDATORY, 75 new ConfigDirPolicyLoader(test_dir(), POLICY_SCOPE_MACHINE));
72 POLICY_SCOPE_MACHINE, 76 return new AsyncPolicyProvider(policy_definition_list, loader.Pass());
73 test_dir());
74 } 77 }
75 78
76 void TestHarness::InstallEmptyPolicy() { 79 void TestHarness::InstallEmptyPolicy() {
77 base::DictionaryValue dict; 80 base::DictionaryValue dict;
78 WriteConfigFile(dict, "policy"); 81 WriteConfigFile(dict, "policy");
79 } 82 }
80 83
81 void TestHarness::InstallStringPolicy(const std::string& policy_name, 84 void TestHarness::InstallStringPolicy(const std::string& policy_name,
82 const std::string& policy_value) { 85 const std::string& policy_value) {
83 base::DictionaryValue dict; 86 base::DictionaryValue dict;
(...skipping 28 matching lines...) Expand all
112 base::DictionaryValue dict; 115 base::DictionaryValue dict;
113 dict.Set(policy_name, policy_value->DeepCopy()); 116 dict.Set(policy_name, policy_value->DeepCopy());
114 WriteConfigFile(dict, "policy"); 117 WriteConfigFile(dict, "policy");
115 } 118 }
116 119
117 void TestHarness::WriteConfigFile(const base::DictionaryValue& dict, 120 void TestHarness::WriteConfigFile(const base::DictionaryValue& dict,
118 const std::string& file_name) { 121 const std::string& file_name) {
119 std::string data; 122 std::string data;
120 JSONStringValueSerializer serializer(&data); 123 JSONStringValueSerializer serializer(&data);
121 serializer.Serialize(dict); 124 serializer.Serialize(dict);
122 const FilePath file_path(test_dir().AppendASCII(file_name)); 125 const FilePath mandatory_dir(test_dir().Append(kMandatoryPath));
123 ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size())); 126 ASSERT_TRUE(file_util::CreateDirectory(mandatory_dir));
127 const FilePath file_path(mandatory_dir.AppendASCII(file_name));
128 ASSERT_EQ((int) data.size(),
129 file_util::WriteFile(file_path, data.c_str(), data.size()));
124 } 130 }
125 131
126 // static 132 // static
127 PolicyProviderTestHarness* TestHarness::Create() { 133 PolicyProviderTestHarness* TestHarness::Create() {
128 return new TestHarness(); 134 return new TestHarness();
129 } 135 }
130 136
131 } // namespace 137 } // namespace
132 138
133 // Instantiate abstract test case for basic policy reading tests. 139 // Instantiate abstract test case for basic policy reading tests.
134 INSTANTIATE_TEST_CASE_P( 140 INSTANTIATE_TEST_CASE_P(
135 ConfigDirPolicyProviderTest, 141 ConfigDirPolicyLoaderTest,
136 ConfigurationPolicyProviderTest, 142 ConfigurationPolicyProviderTest,
137 testing::Values(TestHarness::Create)); 143 testing::Values(TestHarness::Create));
138 144
139 // Some tests that exercise special functionality in ConfigDirPolicyLoader. 145 // Some tests that exercise special functionality in ConfigDirPolicyLoader.
140 class ConfigDirPolicyLoaderTest : public testing::Test { 146 class ConfigDirPolicyLoaderTest : public PolicyTestBase {
141 protected: 147 protected:
142 void SetUp() { 148 void SetUp() OVERRIDE {
149 PolicyTestBase::SetUp();
143 harness_.SetUp(); 150 harness_.SetUp();
144 } 151 }
145 152
146 TestHarness harness_; 153 TestHarness harness_;
147 }; 154 };
148 155
149 // The preferences dictionary is expected to be empty when there are no files to 156 // The preferences dictionary is expected to be empty when there are no files to
150 // load. 157 // load.
151 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsEmpty) { 158 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsEmpty) {
152 ConfigDirPolicyProviderDelegate loader(harness_.test_dir(), 159 ConfigDirPolicyLoader loader(harness_.test_dir(), POLICY_SCOPE_MACHINE);
153 POLICY_LEVEL_MANDATORY,
154 POLICY_SCOPE_MACHINE);
155 scoped_ptr<PolicyBundle> bundle(loader.Load()); 160 scoped_ptr<PolicyBundle> bundle(loader.Load());
156 ASSERT_TRUE(bundle.get()); 161 ASSERT_TRUE(bundle.get());
157 const PolicyBundle kEmptyBundle; 162 const PolicyBundle kEmptyBundle;
158 EXPECT_TRUE(bundle->Equals(kEmptyBundle)); 163 EXPECT_TRUE(bundle->Equals(kEmptyBundle));
159 } 164 }
160 165
161 // Reading from a non-existent directory should result in an empty preferences 166 // Reading from a non-existent directory should result in an empty preferences
162 // dictionary. 167 // dictionary.
163 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsNonExistentDirectory) { 168 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsNonExistentDirectory) {
164 FilePath non_existent_dir( 169 FilePath non_existent_dir(
165 harness_.test_dir().Append(FILE_PATH_LITERAL("not_there"))); 170 harness_.test_dir().Append(FILE_PATH_LITERAL("not_there")));
166 ConfigDirPolicyProviderDelegate loader(non_existent_dir, 171 ConfigDirPolicyLoader loader(non_existent_dir, POLICY_SCOPE_MACHINE);
167 POLICY_LEVEL_MANDATORY,
168 POLICY_SCOPE_MACHINE);
169 scoped_ptr<PolicyBundle> bundle(loader.Load()); 172 scoped_ptr<PolicyBundle> bundle(loader.Load());
170 ASSERT_TRUE(bundle.get()); 173 ASSERT_TRUE(bundle.get());
171 const PolicyBundle kEmptyBundle; 174 const PolicyBundle kEmptyBundle;
172 EXPECT_TRUE(bundle->Equals(kEmptyBundle)); 175 EXPECT_TRUE(bundle->Equals(kEmptyBundle));
173 } 176 }
174 177
175 // Test merging values from different files. 178 // Test merging values from different files.
176 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsMergePrefs) { 179 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsMergePrefs) {
177 // Write a bunch of data files in order to increase the chance to detect the 180 // 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 181 // 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, 182 // filesystem may return files in arbitrary order, there is no way to be sure,
180 // but this is better than nothing. 183 // but this is better than nothing.
181 base::DictionaryValue test_dict_bar; 184 base::DictionaryValue test_dict_bar;
182 test_dict_bar.SetString("HomepageLocation", "http://bar.com"); 185 test_dict_bar.SetString("HomepageLocation", "http://bar.com");
183 for (unsigned int i = 1; i <= 4; ++i) 186 for (unsigned int i = 1; i <= 4; ++i)
184 harness_.WriteConfigFile(test_dict_bar, base::IntToString(i)); 187 harness_.WriteConfigFile(test_dict_bar, base::IntToString(i));
185 base::DictionaryValue test_dict_foo; 188 base::DictionaryValue test_dict_foo;
186 test_dict_foo.SetString("HomepageLocation", "http://foo.com"); 189 test_dict_foo.SetString("HomepageLocation", "http://foo.com");
187 harness_.WriteConfigFile(test_dict_foo, "9"); 190 harness_.WriteConfigFile(test_dict_foo, "9");
188 for (unsigned int i = 5; i <= 8; ++i) 191 for (unsigned int i = 5; i <= 8; ++i)
189 harness_.WriteConfigFile(test_dict_bar, base::IntToString(i)); 192 harness_.WriteConfigFile(test_dict_bar, base::IntToString(i));
190 193
191 ConfigDirPolicyProviderDelegate loader(harness_.test_dir(), 194 ConfigDirPolicyLoader loader(harness_.test_dir(), POLICY_SCOPE_USER);
192 POLICY_LEVEL_MANDATORY,
193 POLICY_SCOPE_USER);
194 scoped_ptr<PolicyBundle> bundle(loader.Load()); 195 scoped_ptr<PolicyBundle> bundle(loader.Load());
195 ASSERT_TRUE(bundle.get()); 196 ASSERT_TRUE(bundle.get());
196 PolicyBundle expected_bundle; 197 PolicyBundle expected_bundle;
197 expected_bundle.Get(POLICY_DOMAIN_CHROME, "") 198 expected_bundle.Get(POLICY_DOMAIN_CHROME, "")
198 .LoadFrom(&test_dict_foo, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); 199 .LoadFrom(&test_dict_foo, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
199 EXPECT_TRUE(bundle->Equals(expected_bundle)); 200 EXPECT_TRUE(bundle->Equals(expected_bundle));
200 } 201 }
201 202
202 // Tests loading of policy for 3rd parties. 203 // Tests loading of policy for 3rd parties.
203 TEST_F(ConfigDirPolicyLoaderTest, Load3rdParty) { 204 TEST_F(ConfigDirPolicyLoaderTest, Load3rdParty) {
(...skipping 14 matching lines...) Expand all
218 219
219 base::DictionaryValue json_dict; 220 base::DictionaryValue json_dict;
220 // Merge |policy_dict|, which will become the chrome policies. 221 // Merge |policy_dict|, which will become the chrome policies.
221 json_dict.MergeDictionary(&policy_dict); 222 json_dict.MergeDictionary(&policy_dict);
222 json_dict.Set("3rdparty.extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 223 json_dict.Set("3rdparty.extensions.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
223 policy_dict.DeepCopy()); 224 policy_dict.DeepCopy());
224 json_dict.Set("3rdparty.extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 225 json_dict.Set("3rdparty.extensions.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
225 policy_dict.DeepCopy()); 226 policy_dict.DeepCopy());
226 227
227 harness_.WriteConfigFile(json_dict, "policy.json"); 228 harness_.WriteConfigFile(json_dict, "policy.json");
228 ConfigDirPolicyProviderDelegate loader(harness_.test_dir(), 229 ConfigDirPolicyLoader loader(harness_.test_dir(), POLICY_SCOPE_USER);
229 POLICY_LEVEL_MANDATORY,
230 POLICY_SCOPE_USER);
231 scoped_ptr<PolicyBundle> bundle(loader.Load()); 230 scoped_ptr<PolicyBundle> bundle(loader.Load());
232 ASSERT_TRUE(bundle.get()); 231 ASSERT_TRUE(bundle.get());
233 PolicyMap expected_policy; 232 PolicyMap expected_policy;
234 expected_policy.LoadFrom(&policy_dict, 233 expected_policy.LoadFrom(&policy_dict,
235 POLICY_LEVEL_MANDATORY, 234 POLICY_LEVEL_MANDATORY,
236 POLICY_SCOPE_USER); 235 POLICY_SCOPE_USER);
237 PolicyBundle expected_bundle; 236 PolicyBundle expected_bundle;
238 expected_bundle.Get(POLICY_DOMAIN_CHROME, "").CopyFrom(expected_policy); 237 expected_bundle.Get(POLICY_DOMAIN_CHROME, "").CopyFrom(expected_policy);
239 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, 238 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS,
240 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") 239 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
241 .CopyFrom(expected_policy); 240 .CopyFrom(expected_policy);
242 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS, 241 expected_bundle.Get(POLICY_DOMAIN_EXTENSIONS,
243 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") 242 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
244 .CopyFrom(expected_policy); 243 .CopyFrom(expected_policy);
245 EXPECT_TRUE(bundle->Equals(expected_bundle)); 244 EXPECT_TRUE(bundle->Equals(expected_bundle));
246 } 245 }
247 246
248 } // namespace policy 247 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698