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

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

Powered by Google App Engine
This is Rietveld 408576698