1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/policy/config_dir_policy_provider.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/mock_configuration_policy_store.h"
#include "chrome/common/json_value_serializer.h"
#include "chrome/common/policy_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
template<typename BASE>
class ConfigDirPolicyProviderTestBase : public BASE {
protected:
ConfigDirPolicyProviderTestBase() {}
virtual void SetUp() {
ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
}
// JSON-encode a dictionary and write it to a file.
void WriteConfigFile(const DictionaryValue& dict,
const std::string& file_name) {
std::string data;
JSONStringValueSerializer serializer(&data);
serializer.Serialize(dict);
const FilePath file_path(test_dir().AppendASCII(file_name));
ASSERT_TRUE(file_util::WriteFile(file_path, data.c_str(), data.size()));
}
const FilePath& test_dir() { return test_dir_.path(); }
private:
ScopedTempDir test_dir_;
};
class ConfigDirPolicyLoaderTest
: public ConfigDirPolicyProviderTestBase<testing::Test> {
};
// The preferences dictionary is expected to be empty when there are no files to
// load.
TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsEmpty) {
ConfigDirPolicyProviderDelegate loader(test_dir());
scoped_ptr<DictionaryValue> policy(loader.Load());
EXPECT_TRUE(policy.get());
EXPECT_TRUE(policy->empty());
}
// Reading from a non-existent directory should result in an empty preferences
// dictionary.
TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsNonExistentDirectory) {
FilePath non_existent_dir(test_dir().Append(FILE_PATH_LITERAL("not_there")));
ConfigDirPolicyProviderDelegate loader(non_existent_dir);
scoped_ptr<DictionaryValue> policy(loader.Load());
EXPECT_TRUE(policy.get());
EXPECT_TRUE(policy->empty());
}
// Test reading back a single preference value.
TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsSinglePref) {
DictionaryValue test_dict;
test_dict.SetString("HomepageLocation", "http://www.google.com");
WriteConfigFile(test_dict, "config_file");
ConfigDirPolicyProviderDelegate loader(test_dir());
scoped_ptr<DictionaryValue> policy(loader.Load());
EXPECT_TRUE(policy.get());
EXPECT_TRUE(policy->Equals(&test_dict));
}
// Test merging values from different files.
TEST_F(ConfigDirPolicyLoaderTest, ReadPrefsMergePrefs) {
// Write a bunch of data files in order to increase the chance to detect the
// provider not respecting lexicographic ordering when reading them. Since the
// filesystem may return files in arbitrary order, there is no way to be sure,
// but this is better than nothing.
DictionaryValue test_dict_bar;
test_dict_bar.SetString("HomepageLocation", "http://bar.com");
for (unsigned int i = 1; i <= 4; ++i)
WriteConfigFile(test_dict_bar, base::IntToString(i));
DictionaryValue test_dict_foo;
test_dict_foo.SetString("HomepageLocation", "http://foo.com");
WriteConfigFile(test_dict_foo, "9");
for (unsigned int i = 5; i <= 8; ++i)
WriteConfigFile(test_dict_bar, base::IntToString(i));
ConfigDirPolicyProviderDelegate loader(test_dir());
scoped_ptr<DictionaryValue> policy(loader.Load());
EXPECT_TRUE(policy.get());
EXPECT_TRUE(policy->Equals(&test_dict_foo));
}
// Holds policy type, corresponding policy key string and a valid value for use
// in parametrized value tests.
class ValueTestParams {
public:
// Assumes ownership of |test_value|.
ValueTestParams(ConfigurationPolicyType type,
const char* policy_key,
Value* test_value)
: type_(type),
policy_key_(policy_key),
test_value_(test_value) {}
// testing::TestWithParam does copying, so provide copy constructor and
// assignment operator.
ValueTestParams(const ValueTestParams& other)
: type_(other.type_),
policy_key_(other.policy_key_),
test_value_(other.test_value_->DeepCopy()) {}
const ValueTestParams& operator=(ValueTestParams other) {
swap(other);
return *this;
}
void swap(ValueTestParams& other) {
std::swap(type_, other.type_);
std::swap(policy_key_, other.policy_key_);
test_value_.swap(other.test_value_);
}
ConfigurationPolicyType type() const { return type_; }
const char* policy_key() const { return policy_key_; }
const Value* test_value() const { return test_value_.get(); }
// Factory methods that create parameter objects for different value types.
static ValueTestParams ForStringPolicy(
ConfigurationPolicyType type,
const char* policy_key) {
return ValueTestParams(type, policy_key, Value::CreateStringValue("test"));
}
static ValueTestParams ForBooleanPolicy(
ConfigurationPolicyType type,
const char* policy_key) {
return ValueTestParams(type, policy_key, Value::CreateBooleanValue(true));
}
static ValueTestParams ForIntegerPolicy(
ConfigurationPolicyType type,
const char* policy_key) {
return ValueTestParams(type, policy_key, Value::CreateIntegerValue(42));
}
static ValueTestParams ForListPolicy(
ConfigurationPolicyType type,
const char* policy_key) {
ListValue* value = new ListValue();
value->Set(0U, Value::CreateStringValue("first"));
value->Set(1U, Value::CreateStringValue("second"));
return ValueTestParams(type, policy_key, value);
}
private:
ConfigurationPolicyType type_;
const char* policy_key_;
scoped_ptr<Value> test_value_;
};
// Tests whether the provider correctly reads a value from the file and forwards
// it to the store.
class ConfigDirPolicyProviderValueTest
: public ConfigDirPolicyProviderTestBase<
testing::TestWithParam<ValueTestParams> > {
protected:
ConfigDirPolicyProviderValueTest()
: ui_thread_(BrowserThread::UI, &loop_),
file_thread_(BrowserThread::FILE, &loop_) {}
virtual void TearDown() {
loop_.RunAllPending();
}
MockConfigurationPolicyStore policy_store_;
private:
MessageLoop loop_;
BrowserThread ui_thread_;
BrowserThread file_thread_;
};
TEST_P(ConfigDirPolicyProviderValueTest, Default) {
ConfigDirPolicyProvider provider(
ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
test_dir());
EXPECT_TRUE(provider.Provide(&policy_store_));
EXPECT_TRUE(policy_store_.policy_map().empty());
}
TEST_P(ConfigDirPolicyProviderValueTest, NullValue) {
DictionaryValue dict;
dict.Set(GetParam().policy_key(), Value::CreateNullValue());
WriteConfigFile(dict, "empty");
ConfigDirPolicyProvider provider(
ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
test_dir());
EXPECT_TRUE(provider.Provide(&policy_store_));
EXPECT_TRUE(policy_store_.policy_map().empty());
}
TEST_P(ConfigDirPolicyProviderValueTest, TestValue) {
DictionaryValue dict;
dict.Set(GetParam().policy_key(), GetParam().test_value()->DeepCopy());
WriteConfigFile(dict, "policy");
ConfigDirPolicyProvider provider(
ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
test_dir());
EXPECT_TRUE(provider.Provide(&policy_store_));
EXPECT_EQ(1U, policy_store_.policy_map().size());
const Value* value = policy_store_.Get(GetParam().type());
ASSERT_TRUE(value);
EXPECT_TRUE(GetParam().test_value()->Equals(value));
}
// Test parameters for all supported policies.
INSTANTIATE_TEST_CASE_P(
ConfigDirPolicyProviderValueTestInstance,
ConfigDirPolicyProviderValueTest,
testing::Values(
ValueTestParams::ForStringPolicy(
kPolicyHomePage,
key::kHomepageLocation),
ValueTestParams::ForBooleanPolicy(
kPolicyHomepageIsNewTabPage,
key::kHomepageIsNewTabPage),
ValueTestParams::ForIntegerPolicy(
kPolicyRestoreOnStartup,
key::kRestoreOnStartup),
ValueTestParams::ForListPolicy(
kPolicyURLsToRestoreOnStartup,
key::kURLsToRestoreOnStartup),
ValueTestParams::ForBooleanPolicy(
kPolicyDefaultSearchProviderEnabled,
key::kDefaultSearchProviderEnabled),
ValueTestParams::ForStringPolicy(
kPolicyDefaultSearchProviderName,
key::kDefaultSearchProviderName),
ValueTestParams::ForStringPolicy(
kPolicyDefaultSearchProviderKeyword,
key::kDefaultSearchProviderKeyword),
ValueTestParams::ForStringPolicy(
kPolicyDefaultSearchProviderSearchURL,
key::kDefaultSearchProviderSearchURL),
ValueTestParams::ForStringPolicy(
kPolicyDefaultSearchProviderSuggestURL,
key::kDefaultSearchProviderSuggestURL),
ValueTestParams::ForStringPolicy(
kPolicyDefaultSearchProviderIconURL,
key::kDefaultSearchProviderIconURL),
ValueTestParams::ForStringPolicy(
kPolicyDefaultSearchProviderEncodings,
key::kDefaultSearchProviderEncodings),
ValueTestParams::ForIntegerPolicy(
kPolicyProxyMode,
key::kProxyMode),
ValueTestParams::ForStringPolicy(
kPolicyProxyServer,
key::kProxyServer),
ValueTestParams::ForStringPolicy(
kPolicyProxyPacUrl,
key::kProxyPacUrl),
ValueTestParams::ForStringPolicy(
kPolicyProxyBypassList,
key::kProxyBypassList),
ValueTestParams::ForBooleanPolicy(
kPolicyAlternateErrorPagesEnabled,
key::kAlternateErrorPagesEnabled),
ValueTestParams::ForBooleanPolicy(
kPolicySearchSuggestEnabled,
key::kSearchSuggestEnabled),
ValueTestParams::ForBooleanPolicy(
kPolicyDnsPrefetchingEnabled,
key::kDnsPrefetchingEnabled),
ValueTestParams::ForBooleanPolicy(
kPolicySafeBrowsingEnabled,
key::kSafeBrowsingEnabled),
ValueTestParams::ForBooleanPolicy(
kPolicyMetricsReportingEnabled,
key::kMetricsReportingEnabled),
ValueTestParams::ForBooleanPolicy(
kPolicyPasswordManagerEnabled,
key::kPasswordManagerEnabled),
ValueTestParams::ForBooleanPolicy(
kPolicyPasswordManagerAllowShowPasswords,
key::kPasswordManagerAllowShowPasswords),
ValueTestParams::ForListPolicy(
kPolicyDisabledPlugins,
key::kDisabledPlugins),
ValueTestParams::ForBooleanPolicy(
kPolicyAutoFillEnabled,
key::kAutoFillEnabled),
ValueTestParams::ForStringPolicy(
kPolicyApplicationLocale,
key::kApplicationLocaleValue),
ValueTestParams::ForBooleanPolicy(
kPolicySyncDisabled,
key::kSyncDisabled),
ValueTestParams::ForListPolicy(
kPolicyExtensionInstallAllowList,
key::kExtensionInstallAllowList),
ValueTestParams::ForListPolicy(
kPolicyExtensionInstallDenyList,
key::kExtensionInstallDenyList),
ValueTestParams::ForBooleanPolicy(
kPolicyShowHomeButton,
key::kShowHomeButton),
ValueTestParams::ForBooleanPolicy(
kPolicyPrintingEnabled,
key::kPrintingEnabled)));
} // namespace policy
|