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

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

Issue 8467011: Include only policy definitions that apply to the platfrom in the policy definition list. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed dead declarations. Created 9 years, 1 month 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/compiler_specific.h"
6
7 #include "base/file_util.h" 6 #include "base/file_util.h"
8 #include "base/json/json_value_serializer.h" 7 #include "base/json/json_value_serializer.h"
9 #include "base/path_service.h"
10 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
11 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/values.h"
12 #include "chrome/browser/policy/config_dir_policy_provider.h" 11 #include "chrome/browser/policy/config_dir_policy_provider.h"
13 #include "chrome/browser/policy/configuration_policy_pref_store.h" 12 #include "chrome/browser/policy/configuration_policy_provider_test.h"
14 #include "chrome/browser/policy/policy_map.h"
15 #include "content/test/test_browser_thread.h"
16 #include "policy/policy_constants.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using content::BrowserThread;
20 13
21 namespace policy { 14 namespace policy {
22 15
23 template<typename BASE> 16 namespace {
24 class ConfigDirPolicyProviderTestBase : public BASE {
25 protected:
26 ConfigDirPolicyProviderTestBase() {}
27 17
28 virtual void SetUp() { 18 class TestHarness : public PolicyProviderTestHarness {
29 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); 19 public:
30 } 20 TestHarness();
21 virtual ~TestHarness();
31 22
32 // JSON-encode a dictionary and write it to a file. 23 virtual void SetUp() OVERRIDE;
33 void WriteConfigFile(const DictionaryValue& dict, 24
34 const std::string& file_name) { 25 virtual AsynchronousPolicyProvider* CreateProvider(
35 std::string data; 26 const PolicyDefinitionList* policy_definition_list) OVERRIDE;
36 JSONStringValueSerializer serializer(&data); 27
37 serializer.Serialize(dict); 28 virtual void InstallEmptyPolicy() OVERRIDE;
38 const FilePath file_path(test_dir().AppendASCII(file_name)); 29 virtual void InstallStringPolicy(const std::string& policy_name,
39 ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size())); 30 const std::string& policy_value) OVERRIDE;
40 } 31 virtual void InstallIntegerPolicy(const std::string& policy_name,
32 int policy_value) OVERRIDE;
33 virtual void InstallBooleanPolicy(const std::string& policy_name,
34 bool policy_value) OVERRIDE;
35 virtual void InstallStringListPolicy(const std::string& policy_name,
36 const ListValue* policy_value) OVERRIDE;
41 37
42 const FilePath& test_dir() { return test_dir_.path(); } 38 const FilePath& test_dir() { return test_dir_.path(); }
43 39
40 // JSON-encode a dictionary and write it to a file.
41 void WriteConfigFile(const base::DictionaryValue& dict,
42 const std::string& file_name);
43
44 static PolicyProviderTestHarness* Create();
45
44 private: 46 private:
45 ScopedTempDir test_dir_; 47 ScopedTempDir test_dir_;
48
49 DISALLOW_COPY_AND_ASSIGN(TestHarness);
46 }; 50 };
47 51
48 class ConfigDirPolicyLoaderTest 52 TestHarness::TestHarness() {}
49 : public ConfigDirPolicyProviderTestBase<testing::Test> { 53
54 TestHarness::~TestHarness() {}
55
56 void TestHarness::SetUp() {
57 ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
58 }
59
60 AsynchronousPolicyProvider* TestHarness::CreateProvider(
61 const PolicyDefinitionList* policy_definition_list) {
62 return new ConfigDirPolicyProvider(policy_definition_list, test_dir());
63 }
64
65 void TestHarness::InstallEmptyPolicy() {
66 DictionaryValue dict;
67 WriteConfigFile(dict, "policy");
68 }
69
70 void TestHarness::InstallStringPolicy(const std::string& policy_name,
71 const std::string& policy_value) {
72 DictionaryValue dict;
73 dict.SetString(policy_name, policy_value);
74 WriteConfigFile(dict, "policy");
75 }
76
77 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
78 int policy_value) {
79 DictionaryValue dict;
80 dict.SetInteger(policy_name, policy_value);
81 WriteConfigFile(dict, "policy");
82 }
83
84 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
85 bool policy_value) {
86 DictionaryValue dict;
87 dict.SetBoolean(policy_name, policy_value);
88 WriteConfigFile(dict, "policy");
89 }
90
91 void TestHarness::InstallStringListPolicy(const std::string& policy_name,
92 const ListValue* policy_value) {
93 DictionaryValue dict;
94 dict.Set(policy_name, policy_value->DeepCopy());
95 WriteConfigFile(dict, "policy");
96 }
97
98 void TestHarness::WriteConfigFile(const base::DictionaryValue& dict,
99 const std::string& file_name) {
100 std::string data;
101 JSONStringValueSerializer serializer(&data);
102 serializer.Serialize(dict);
103 const FilePath file_path(test_dir().AppendASCII(file_name));
104 ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size()));
105 }
106
107 // static
108 PolicyProviderTestHarness* TestHarness::Create() {
109 return new TestHarness();
110 }
111
112 } // namespace
113
114 // Instantiate abstract test case for basic policy reading tests.
115 INSTANTIATE_TEST_CASE_P(
116 ConfigDirPolicyProviderTest,
117 ConfigurationPolicyProviderTest,
118 testing::Values(TestHarness::Create));
119
120 // Some tests that exercise special functionality in ConfigDirPolicyLoader.
121 class ConfigDirPolicyLoaderTest : public testing::Test {
122 protected:
123 void SetUp() {
124 harness_.SetUp();
125 }
126
127 TestHarness harness_;
50 }; 128 };
51 129
52 // The preferences dictionary is expected to be empty when there are no files to 130 // The preferences dictionary is expected to be empty when there are no files to
53 // load. 131 // load.
54 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsEmpty) { 132 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsEmpty) {
55 ConfigDirPolicyProviderDelegate loader(test_dir()); 133 ConfigDirPolicyProviderDelegate loader(harness_.test_dir());
56 scoped_ptr<DictionaryValue> policy(loader.Load()); 134 scoped_ptr<base::DictionaryValue> policy(loader.Load());
57 EXPECT_TRUE(policy.get()); 135 EXPECT_TRUE(policy.get());
58 EXPECT_TRUE(policy->empty()); 136 EXPECT_TRUE(policy->empty());
59 } 137 }
60 138
61 // Reading from a non-existent directory should result in an empty preferences 139 // Reading from a non-existent directory should result in an empty preferences
62 // dictionary. 140 // dictionary.
63 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsNonExistentDirectory) { 141 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsNonExistentDirectory) {
64 FilePath non_existent_dir(test_dir().Append(FILE_PATH_LITERAL("not_there"))); 142 FilePath non_existent_dir(
143 harness_.test_dir().Append(FILE_PATH_LITERAL("not_there")));
65 ConfigDirPolicyProviderDelegate loader(non_existent_dir); 144 ConfigDirPolicyProviderDelegate loader(non_existent_dir);
66 scoped_ptr<DictionaryValue> policy(loader.Load()); 145 scoped_ptr<base::DictionaryValue> policy(loader.Load());
67 EXPECT_TRUE(policy.get()); 146 EXPECT_TRUE(policy.get());
68 EXPECT_TRUE(policy->empty()); 147 EXPECT_TRUE(policy->empty());
69 } 148 }
70 149
71 // Test reading back a single preference value.
72 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsSinglePref) {
73 DictionaryValue test_dict;
74 test_dict.SetString("HomepageLocation", "http://www.google.com");
75 WriteConfigFile(test_dict, "config_file");
76
77 ConfigDirPolicyProviderDelegate loader(test_dir());
78 scoped_ptr<DictionaryValue> policy(loader.Load());
79 EXPECT_TRUE(policy.get());
80 EXPECT_TRUE(policy->Equals(&test_dict));
81 }
82
83 // Test merging values from different files. 150 // Test merging values from different files.
84 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsMergePrefs) { 151 TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsMergePrefs) {
85 // Write a bunch of data files in order to increase the chance to detect the 152 // Write a bunch of data files in order to increase the chance to detect the
86 // provider not respecting lexicographic ordering when reading them. Since the 153 // provider not respecting lexicographic ordering when reading them. Since the
87 // filesystem may return files in arbitrary order, there is no way to be sure, 154 // filesystem may return files in arbitrary order, there is no way to be sure,
88 // but this is better than nothing. 155 // but this is better than nothing.
89 DictionaryValue test_dict_bar; 156 base::DictionaryValue test_dict_bar;
90 test_dict_bar.SetString("HomepageLocation", "http://bar.com"); 157 test_dict_bar.SetString("HomepageLocation", "http://bar.com");
91 for (unsigned int i = 1; i <= 4; ++i) 158 for (unsigned int i = 1; i <= 4; ++i)
92 WriteConfigFile(test_dict_bar, base::IntToString(i)); 159 harness_.WriteConfigFile(test_dict_bar, base::IntToString(i));
93 DictionaryValue test_dict_foo; 160 base::DictionaryValue test_dict_foo;
94 test_dict_foo.SetString("HomepageLocation", "http://foo.com"); 161 test_dict_foo.SetString("HomepageLocation", "http://foo.com");
95 WriteConfigFile(test_dict_foo, "9"); 162 harness_.WriteConfigFile(test_dict_foo, "9");
96 for (unsigned int i = 5; i <= 8; ++i) 163 for (unsigned int i = 5; i <= 8; ++i)
97 WriteConfigFile(test_dict_bar, base::IntToString(i)); 164 harness_.WriteConfigFile(test_dict_bar, base::IntToString(i));
98 165
99 ConfigDirPolicyProviderDelegate loader(test_dir()); 166 ConfigDirPolicyProviderDelegate loader(harness_.test_dir());
100 scoped_ptr<DictionaryValue> policy(loader.Load()); 167 scoped_ptr<base::DictionaryValue> policy(loader.Load());
101 EXPECT_TRUE(policy.get()); 168 EXPECT_TRUE(policy.get());
102 EXPECT_TRUE(policy->Equals(&test_dict_foo)); 169 EXPECT_TRUE(policy->Equals(&test_dict_foo));
103 } 170 }
104 171
105 // Holds policy type, corresponding policy key string and a valid value for use
106 // in parametrized value tests.
107 class ValueTestParams {
108 public:
109 // Assumes ownership of |test_value|.
110 ValueTestParams(ConfigurationPolicyType type,
111 const char* policy_key,
112 Value* test_value)
113 : type_(type),
114 policy_key_(policy_key),
115 test_value_(test_value) {}
116
117 // testing::TestWithParam does copying, so provide copy constructor and
118 // assignment operator.
119 ValueTestParams(const ValueTestParams& other)
120 : type_(other.type_),
121 policy_key_(other.policy_key_),
122 test_value_(other.test_value_->DeepCopy()) {}
123
124 const ValueTestParams& operator=(ValueTestParams other) {
125 swap(other);
126 return *this;
127 }
128
129 void swap(ValueTestParams& other) {
130 std::swap(type_, other.type_);
131 std::swap(policy_key_, other.policy_key_);
132 test_value_.swap(other.test_value_);
133 }
134
135 ConfigurationPolicyType type() const { return type_; }
136 const char* policy_key() const { return policy_key_; }
137 const Value* test_value() const { return test_value_.get(); }
138
139 // Factory methods that create parameter objects for different value types.
140 static ValueTestParams ForStringPolicy(
141 ConfigurationPolicyType type,
142 const char* policy_key) {
143 return ValueTestParams(type, policy_key, Value::CreateStringValue("test"));
144 }
145 static ValueTestParams ForBooleanPolicy(
146 ConfigurationPolicyType type,
147 const char* policy_key) {
148 return ValueTestParams(type, policy_key, Value::CreateBooleanValue(true));
149 }
150 static ValueTestParams ForIntegerPolicy(
151 ConfigurationPolicyType type,
152 const char* policy_key) {
153 return ValueTestParams(type, policy_key, Value::CreateIntegerValue(42));
154 }
155 static ValueTestParams ForListPolicy(
156 ConfigurationPolicyType type,
157 const char* policy_key) {
158 ListValue* value = new ListValue();
159 value->Set(0U, Value::CreateStringValue("first"));
160 value->Set(1U, Value::CreateStringValue("second"));
161 return ValueTestParams(type, policy_key, value);
162 }
163
164 private:
165 ConfigurationPolicyType type_;
166 const char* policy_key_;
167 scoped_ptr<Value> test_value_;
168 };
169
170 // Tests whether the provider correctly reads a value from the file and forwards
171 // it to the store.
172 class ConfigDirPolicyProviderValueTest
173 : public ConfigDirPolicyProviderTestBase<
174 testing::TestWithParam<ValueTestParams> > {
175 protected:
176 ConfigDirPolicyProviderValueTest()
177 : ui_thread_(BrowserThread::UI, &loop_),
178 file_thread_(BrowserThread::FILE, &loop_) {}
179
180 virtual void TearDown() {
181 loop_.RunAllPending();
182 }
183
184 private:
185 MessageLoop loop_;
186 content::TestBrowserThread ui_thread_;
187 content::TestBrowserThread file_thread_;
188 };
189
190 TEST_P(ConfigDirPolicyProviderValueTest, Default) {
191 ConfigDirPolicyProvider provider(GetChromePolicyDefinitionList(), test_dir());
192 PolicyMap policy_map;
193 EXPECT_TRUE(provider.Provide(&policy_map));
194 EXPECT_TRUE(policy_map.empty());
195 }
196
197 TEST_P(ConfigDirPolicyProviderValueTest, TestValue) {
198 DictionaryValue dict;
199 dict.Set(GetParam().policy_key(), GetParam().test_value()->DeepCopy());
200 WriteConfigFile(dict, "policy");
201 ConfigDirPolicyProvider provider(GetChromePolicyDefinitionList(), test_dir());
202 PolicyMap policy_map;
203 EXPECT_TRUE(provider.Provide(&policy_map));
204 EXPECT_EQ(1U, policy_map.size());
205 const Value* value = policy_map.Get(GetParam().type());
206 ASSERT_TRUE(value);
207 EXPECT_TRUE(GetParam().test_value()->Equals(value));
208 }
209
210 // Test parameters for all supported policies. testing::Values() has a limit of
211 // 50 parameters which is reached in this instantiation; new policies should go
212 // in a new instantiation.
213 INSTANTIATE_TEST_CASE_P(
214 ConfigDirPolicyProviderValueTestInstance,
215 ConfigDirPolicyProviderValueTest,
216 testing::Values(
217 ValueTestParams::ForStringPolicy(
218 kPolicyHomepageLocation,
219 key::kHomepageLocation),
220 ValueTestParams::ForBooleanPolicy(
221 kPolicyHomepageIsNewTabPage,
222 key::kHomepageIsNewTabPage),
223 ValueTestParams::ForIntegerPolicy(
224 kPolicyRestoreOnStartup,
225 key::kRestoreOnStartup),
226 ValueTestParams::ForListPolicy(
227 kPolicyRestoreOnStartupURLs,
228 key::kRestoreOnStartupURLs),
229 ValueTestParams::ForBooleanPolicy(
230 kPolicyDefaultSearchProviderEnabled,
231 key::kDefaultSearchProviderEnabled),
232 ValueTestParams::ForStringPolicy(
233 kPolicyDefaultSearchProviderName,
234 key::kDefaultSearchProviderName),
235 ValueTestParams::ForStringPolicy(
236 kPolicyDefaultSearchProviderKeyword,
237 key::kDefaultSearchProviderKeyword),
238 ValueTestParams::ForStringPolicy(
239 kPolicyDefaultSearchProviderSearchURL,
240 key::kDefaultSearchProviderSearchURL),
241 ValueTestParams::ForStringPolicy(
242 kPolicyDefaultSearchProviderSuggestURL,
243 key::kDefaultSearchProviderSuggestURL),
244 ValueTestParams::ForStringPolicy(
245 kPolicyDefaultSearchProviderInstantURL,
246 key::kDefaultSearchProviderInstantURL),
247 ValueTestParams::ForStringPolicy(
248 kPolicyDefaultSearchProviderIconURL,
249 key::kDefaultSearchProviderIconURL),
250 ValueTestParams::ForListPolicy(
251 kPolicyDefaultSearchProviderEncodings,
252 key::kDefaultSearchProviderEncodings),
253 ValueTestParams::ForStringPolicy(
254 kPolicyProxyMode,
255 key::kProxyMode),
256 ValueTestParams::ForIntegerPolicy(
257 kPolicyProxyServerMode,
258 key::kProxyServerMode),
259 ValueTestParams::ForStringPolicy(
260 kPolicyProxyServer,
261 key::kProxyServer),
262 ValueTestParams::ForStringPolicy(
263 kPolicyProxyPacUrl,
264 key::kProxyPacUrl),
265 ValueTestParams::ForStringPolicy(
266 kPolicyProxyBypassList,
267 key::kProxyBypassList),
268 ValueTestParams::ForBooleanPolicy(
269 kPolicyAlternateErrorPagesEnabled,
270 key::kAlternateErrorPagesEnabled),
271 ValueTestParams::ForBooleanPolicy(
272 kPolicySearchSuggestEnabled,
273 key::kSearchSuggestEnabled),
274 ValueTestParams::ForBooleanPolicy(
275 kPolicyDnsPrefetchingEnabled,
276 key::kDnsPrefetchingEnabled),
277 ValueTestParams::ForBooleanPolicy(
278 kPolicySafeBrowsingEnabled,
279 key::kSafeBrowsingEnabled),
280 ValueTestParams::ForBooleanPolicy(
281 kPolicyMetricsReportingEnabled,
282 key::kMetricsReportingEnabled),
283 ValueTestParams::ForBooleanPolicy(
284 kPolicyPasswordManagerEnabled,
285 key::kPasswordManagerEnabled),
286 ValueTestParams::ForBooleanPolicy(
287 kPolicyPasswordManagerAllowShowPasswords,
288 key::kPasswordManagerAllowShowPasswords),
289 ValueTestParams::ForListPolicy(
290 kPolicyDisabledPlugins,
291 key::kDisabledPlugins),
292 ValueTestParams::ForListPolicy(
293 kPolicyDisabledPluginsExceptions,
294 key::kDisabledPluginsExceptions),
295 ValueTestParams::ForListPolicy(
296 kPolicyEnabledPlugins,
297 key::kEnabledPlugins),
298 ValueTestParams::ForBooleanPolicy(
299 kPolicyAutoFillEnabled,
300 key::kAutoFillEnabled),
301 ValueTestParams::ForStringPolicy(
302 kPolicyApplicationLocaleValue,
303 key::kApplicationLocaleValue),
304 ValueTestParams::ForBooleanPolicy(
305 kPolicySyncDisabled,
306 key::kSyncDisabled),
307 ValueTestParams::ForListPolicy(
308 kPolicyExtensionInstallWhitelist,
309 key::kExtensionInstallWhitelist),
310 ValueTestParams::ForListPolicy(
311 kPolicyExtensionInstallBlacklist,
312 key::kExtensionInstallBlacklist),
313 ValueTestParams::ForBooleanPolicy(
314 kPolicyShowHomeButton,
315 key::kShowHomeButton),
316 ValueTestParams::ForBooleanPolicy(
317 kPolicyPrintingEnabled,
318 key::kPrintingEnabled),
319 ValueTestParams::ForBooleanPolicy(
320 kPolicyInstantEnabled,
321 key::kInstantEnabled),
322 ValueTestParams::ForIntegerPolicy(
323 kPolicyIncognitoModeAvailability,
324 key::kIncognitoModeAvailability),
325 ValueTestParams::ForBooleanPolicy(
326 kPolicyDisablePluginFinder,
327 key::kDisablePluginFinder),
328 ValueTestParams::ForBooleanPolicy(
329 kPolicyClearSiteDataOnExit,
330 key::kClearSiteDataOnExit),
331 ValueTestParams::ForStringPolicy(
332 kPolicyDownloadDirectory,
333 key::kDownloadDirectory),
334 ValueTestParams::ForBooleanPolicy(
335 kPolicyDefaultBrowserSettingEnabled,
336 key::kDefaultBrowserSettingEnabled),
337 ValueTestParams::ForBooleanPolicy(
338 kPolicyCloudPrintProxyEnabled,
339 key::kCloudPrintProxyEnabled),
340 ValueTestParams::ForBooleanPolicy(
341 kPolicyTranslateEnabled,
342 key::kTranslateEnabled),
343 ValueTestParams::ForBooleanPolicy(
344 kPolicyAllowOutdatedPlugins,
345 key::kAllowOutdatedPlugins),
346 ValueTestParams::ForBooleanPolicy(
347 kPolicyAlwaysAuthorizePlugins,
348 key::kAlwaysAuthorizePlugins),
349 ValueTestParams::ForBooleanPolicy(
350 kPolicyBookmarkBarEnabled,
351 key::kBookmarkBarEnabled),
352 ValueTestParams::ForBooleanPolicy(
353 kPolicyEditBookmarksEnabled,
354 key::kEditBookmarksEnabled),
355 ValueTestParams::ForListPolicy(
356 kPolicyDisabledSchemes,
357 key::kDisabledSchemes),
358 ValueTestParams::ForStringPolicy(
359 kPolicyDiskCacheDir,
360 key::kDiskCacheDir),
361 ValueTestParams::ForListPolicy(
362 kPolicyURLBlacklist,
363 key::kURLBlacklist),
364 ValueTestParams::ForListPolicy(
365 kPolicyURLWhitelist,
366 key::kURLWhitelist)));
367
368 // Adds additional cases that can't be included in
369 // ConfigDirPolicyProviderValueTestInstance since testing::Values is limited
370 // to 50 entries.
371 INSTANTIATE_TEST_CASE_P(
372 ConfigDirPolicyProviderValueTestInstanceContinued,
373 ConfigDirPolicyProviderValueTest,
374 testing::Values(
375 ValueTestParams::ForBooleanPolicy(
376 kPolicyCloudPrintSubmitEnabled,
377 key::kCloudPrintSubmitEnabled)));
378
379 // Test parameters for all policies that are supported on ChromeOS only.
380 #if defined(OS_CHROMEOS)
381 INSTANTIATE_TEST_CASE_P(
382 ConfigDirPolicyProviderValueTestChromeOSInstance,
383 ConfigDirPolicyProviderValueTest,
384 testing::Values(
385 ValueTestParams::ForIntegerPolicy(
386 kPolicyPolicyRefreshRate,
387 key::kPolicyRefreshRate)));
388 #endif
389
390 } // namespace policy 172 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/asynchronous_policy_test_base.cc ('k') | chrome/browser/policy/configuration_policy_loader_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698