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
|
// Copyright (c) 2012 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 <string>
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/prefs/pref_store_observer_mock.h"
#include "base/run_loop.h"
#include "chrome/browser/policy/configuration_policy_pref_store_test.h"
#include "components/policy/core/browser/configuration_policy_handler.h"
#include "components/policy/core/browser/configuration_policy_pref_store.h"
#include "components/policy/core/common/external_data_fetcher.h"
#include "components/policy/core/common/policy_details.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/policy/core/common/policy_service_impl.h"
#include "testing/gmock/include/gmock/gmock.h"
// Note: this file should move to components/policy/core/browser, but the
// components_unittests runner does not load the ResourceBundle as
// ChromeTestSuite::Initialize does, which leads to failures using
// PolicyErrorMap.
using testing::Mock;
using testing::Return;
using testing::_;
namespace {
const char kTestPolicy[] = "test.policy";
const char kTestPref[] = "test.pref";
} // namespace
namespace policy {
// Test cases for list-valued policy settings.
class ConfigurationPolicyPrefStoreListTest
: public ConfigurationPolicyPrefStoreTest {
virtual void SetUp() OVERRIDE {
handler_list_.AddHandler(
make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
kTestPolicy, kTestPref, base::Value::TYPE_LIST)));
}
};
TEST_F(ConfigurationPolicyPrefStoreListTest, GetDefault) {
EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
}
TEST_F(ConfigurationPolicyPrefStoreListTest, SetValue) {
base::ListValue* in_value = new base::ListValue();
in_value->Append(base::Value::CreateStringValue("test1"));
in_value->Append(base::Value::CreateStringValue("test2,"));
PolicyMap policy;
policy.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, in_value, NULL);
UpdateProviderPolicy(policy);
const base::Value* value = NULL;
EXPECT_TRUE(store_->GetValue(kTestPref, &value));
ASSERT_TRUE(value);
EXPECT_TRUE(in_value->Equals(value));
}
// Test cases for string-valued policy settings.
class ConfigurationPolicyPrefStoreStringTest
: public ConfigurationPolicyPrefStoreTest {
virtual void SetUp() OVERRIDE {
handler_list_.AddHandler(
make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
kTestPolicy, kTestPref, base::Value::TYPE_STRING)));
}
};
TEST_F(ConfigurationPolicyPrefStoreStringTest, GetDefault) {
EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
}
TEST_F(ConfigurationPolicyPrefStoreStringTest, SetValue) {
PolicyMap policy;
policy.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateStringValue("http://chromium.org"), NULL);
UpdateProviderPolicy(policy);
const base::Value* value = NULL;
EXPECT_TRUE(store_->GetValue(kTestPref, &value));
ASSERT_TRUE(value);
EXPECT_TRUE(base::StringValue("http://chromium.org").Equals(value));
}
// Test cases for boolean-valued policy settings.
class ConfigurationPolicyPrefStoreBooleanTest
: public ConfigurationPolicyPrefStoreTest {
virtual void SetUp() OVERRIDE {
handler_list_.AddHandler(
make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
kTestPolicy, kTestPref, base::Value::TYPE_BOOLEAN)));
}
};
TEST_F(ConfigurationPolicyPrefStoreBooleanTest, GetDefault) {
EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
}
TEST_F(ConfigurationPolicyPrefStoreBooleanTest, SetValue) {
PolicyMap policy;
policy.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(false), NULL);
UpdateProviderPolicy(policy);
const base::Value* value = NULL;
EXPECT_TRUE(store_->GetValue(kTestPref, &value));
ASSERT_TRUE(value);
bool boolean_value = true;
bool result = value->GetAsBoolean(&boolean_value);
ASSERT_TRUE(result);
EXPECT_FALSE(boolean_value);
policy.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true), NULL);
UpdateProviderPolicy(policy);
value = NULL;
EXPECT_TRUE(store_->GetValue(kTestPref, &value));
boolean_value = false;
result = value->GetAsBoolean(&boolean_value);
ASSERT_TRUE(result);
EXPECT_TRUE(boolean_value);
}
// Test cases for integer-valued policy settings.
class ConfigurationPolicyPrefStoreIntegerTest
: public ConfigurationPolicyPrefStoreTest {
virtual void SetUp() OVERRIDE {
handler_list_.AddHandler(
make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
kTestPolicy, kTestPref, base::Value::TYPE_INTEGER)));
}
};
TEST_F(ConfigurationPolicyPrefStoreIntegerTest, GetDefault) {
EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
}
TEST_F(ConfigurationPolicyPrefStoreIntegerTest, SetValue) {
PolicyMap policy;
policy.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, base::Value::CreateIntegerValue(2), NULL);
UpdateProviderPolicy(policy);
const base::Value* value = NULL;
EXPECT_TRUE(store_->GetValue(kTestPref, &value));
EXPECT_TRUE(base::FundamentalValue(2).Equals(value));
}
// Exercises the policy refresh mechanism.
class ConfigurationPolicyPrefStoreRefreshTest
: public ConfigurationPolicyPrefStoreTest {
protected:
virtual void SetUp() OVERRIDE {
ConfigurationPolicyPrefStoreTest::SetUp();
store_->AddObserver(&observer_);
handler_list_.AddHandler(
make_scoped_ptr<ConfigurationPolicyHandler>(new SimplePolicyHandler(
kTestPolicy, kTestPref, base::Value::TYPE_STRING)));
}
virtual void TearDown() OVERRIDE {
store_->RemoveObserver(&observer_);
ConfigurationPolicyPrefStoreTest::TearDown();
}
PrefStoreObserverMock observer_;
};
TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Refresh) {
const base::Value* value = NULL;
EXPECT_FALSE(store_->GetValue(kTestPolicy, NULL));
EXPECT_CALL(observer_, OnPrefValueChanged(kTestPref)).Times(1);
PolicyMap policy;
policy.Set(kTestPolicy,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
base::Value::CreateStringValue("http://www.chromium.org"),
NULL);
UpdateProviderPolicy(policy);
Mock::VerifyAndClearExpectations(&observer_);
EXPECT_TRUE(store_->GetValue(kTestPref, &value));
EXPECT_TRUE(base::StringValue("http://www.chromium.org").Equals(value));
EXPECT_CALL(observer_, OnPrefValueChanged(_)).Times(0);
UpdateProviderPolicy(policy);
Mock::VerifyAndClearExpectations(&observer_);
EXPECT_CALL(observer_, OnPrefValueChanged(kTestPref)).Times(1);
policy.Erase(kTestPolicy);
UpdateProviderPolicy(policy);
Mock::VerifyAndClearExpectations(&observer_);
EXPECT_FALSE(store_->GetValue(kTestPref, NULL));
}
TEST_F(ConfigurationPolicyPrefStoreRefreshTest, Initialization) {
EXPECT_FALSE(store_->IsInitializationComplete());
EXPECT_CALL(provider_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
.WillRepeatedly(Return(true));
EXPECT_CALL(observer_, OnInitializationCompleted(true)).Times(1);
PolicyMap policy;
UpdateProviderPolicy(policy);
Mock::VerifyAndClearExpectations(&observer_);
EXPECT_TRUE(store_->IsInitializationComplete());
}
} // namespace policy
|