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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/storage/policy_value_store.h"
7 #include "base/callback.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "components/policy/core/common/external_data_fetcher.h"
14 #include "components/policy/core/common/policy_map.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "extensions/browser/api/storage/settings_observer.h"
17 #include "extensions/browser/value_store/leveldb_value_store.h"
18 #include "extensions/browser/value_store/value_store_unittest.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
25 namespace extensions
{
29 const char kTestExtensionId
[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
30 const char kDatabaseUMAClientName
[] = "Test";
32 class MockSettingsObserver
: public SettingsObserver
{
34 MOCK_METHOD3(OnSettingsChanged
, void(
35 const std::string
& extension_id
,
36 settings_namespace::Namespace settings_namespace
,
37 const std::string
& changes_json
));
40 // Extends PolicyValueStore by overriding the mutating methods, so that the
41 // Get() base implementation can be tested with the ValueStoreTest parameterized
43 class MutablePolicyValueStore
: public PolicyValueStore
{
45 explicit MutablePolicyValueStore(const base::FilePath
& path
)
48 make_scoped_refptr(new SettingsObserverList()),
50 new LeveldbValueStore(kDatabaseUMAClientName
, path
))) {}
51 ~MutablePolicyValueStore() override
{}
53 WriteResult
Set(WriteOptions options
,
54 const std::string
& key
,
55 const base::Value
& value
) override
{
56 return delegate()->Set(options
, key
, value
);
59 WriteResult
Set(WriteOptions options
,
60 const base::DictionaryValue
& values
) override
{
61 return delegate()->Set(options
, values
);
64 WriteResult
Remove(const std::string
& key
) override
{
65 return delegate()->Remove(key
);
68 WriteResult
Remove(const std::vector
<std::string
>& keys
) override
{
69 return delegate()->Remove(keys
);
72 WriteResult
Clear() override
{ return delegate()->Clear(); }
75 DISALLOW_COPY_AND_ASSIGN(MutablePolicyValueStore
);
78 ValueStore
* Param(const base::FilePath
& file_path
) {
79 return new MutablePolicyValueStore(file_path
);
84 INSTANTIATE_TEST_CASE_P(
87 testing::Values(&Param
));
89 class PolicyValueStoreTest
: public testing::Test
{
91 PolicyValueStoreTest()
92 : file_thread_(content::BrowserThread::FILE, &loop_
) {}
93 ~PolicyValueStoreTest() override
{}
95 void SetUp() override
{
96 ASSERT_TRUE(scoped_temp_dir_
.CreateUniqueTempDir());
97 observers_
= new SettingsObserverList();
98 observers_
->AddObserver(&observer_
);
99 store_
.reset(new PolicyValueStore(
100 kTestExtensionId
, observers_
,
101 make_scoped_ptr(new LeveldbValueStore(kDatabaseUMAClientName
,
102 scoped_temp_dir_
.path()))));
105 void TearDown() override
{
106 observers_
->RemoveObserver(&observer_
);
111 base::ScopedTempDir scoped_temp_dir_
;
112 base::MessageLoop loop_
;
113 content::TestBrowserThread file_thread_
;
114 scoped_ptr
<PolicyValueStore
> store_
;
115 MockSettingsObserver observer_
;
116 scoped_refptr
<SettingsObserverList
> observers_
;
119 TEST_F(PolicyValueStoreTest
, DontProvideRecommendedPolicies
) {
120 policy::PolicyMap policies
;
121 base::FundamentalValue
expected(123);
122 policies
.Set("must", policy::POLICY_LEVEL_MANDATORY
,
123 policy::POLICY_SCOPE_USER
, expected
.DeepCopy(), NULL
);
124 policies
.Set("may", policy::POLICY_LEVEL_RECOMMENDED
,
125 policy::POLICY_SCOPE_USER
,
126 new base::FundamentalValue(456), NULL
);
127 store_
->SetCurrentPolicy(policies
);
128 ValueStore::ReadResult result
= store_
->Get();
129 ASSERT_FALSE(result
->HasError());
130 EXPECT_EQ(1u, result
->settings().size());
131 base::Value
* value
= NULL
;
132 EXPECT_FALSE(result
->settings().Get("may", &value
));
133 EXPECT_TRUE(result
->settings().Get("must", &value
));
134 EXPECT_TRUE(base::Value::Equals(&expected
, value
));
137 TEST_F(PolicyValueStoreTest
, ReadOnly
) {
138 ValueStore::WriteOptions options
= ValueStore::DEFAULTS
;
140 base::StringValue
string_value("value");
141 EXPECT_TRUE(store_
->Set(options
, "key", string_value
)->HasError());
143 base::DictionaryValue dict
;
144 dict
.SetString("key", "value");
145 EXPECT_TRUE(store_
->Set(options
, dict
)->HasError());
147 EXPECT_TRUE(store_
->Remove("key")->HasError());
148 std::vector
<std::string
> keys
;
149 keys
.push_back("key");
150 EXPECT_TRUE(store_
->Remove(keys
)->HasError());
151 EXPECT_TRUE(store_
->Clear()->HasError());
154 TEST_F(PolicyValueStoreTest
, NotifyOnChanges
) {
155 // Notify when setting the initial policy.
156 const base::StringValue
value("111");
158 ValueStoreChangeList changes
;
159 changes
.push_back(ValueStoreChange("aaa", NULL
, value
.DeepCopy()));
160 EXPECT_CALL(observer_
,
161 OnSettingsChanged(kTestExtensionId
,
162 settings_namespace::MANAGED
,
163 ValueStoreChange::ToJson(changes
)));
166 policy::PolicyMap policies
;
167 policies
.Set("aaa", policy::POLICY_LEVEL_MANDATORY
, policy::POLICY_SCOPE_USER
,
168 value
.DeepCopy(), NULL
);
169 store_
->SetCurrentPolicy(policies
);
170 loop_
.RunUntilIdle();
171 Mock::VerifyAndClearExpectations(&observer_
);
173 // Notify when new policies are added.
175 ValueStoreChangeList changes
;
176 changes
.push_back(ValueStoreChange("bbb", NULL
, value
.DeepCopy()));
177 EXPECT_CALL(observer_
,
178 OnSettingsChanged(kTestExtensionId
,
179 settings_namespace::MANAGED
,
180 ValueStoreChange::ToJson(changes
)));
183 policies
.Set("bbb", policy::POLICY_LEVEL_MANDATORY
, policy::POLICY_SCOPE_USER
,
184 value
.DeepCopy(), NULL
);
185 store_
->SetCurrentPolicy(policies
);
186 loop_
.RunUntilIdle();
187 Mock::VerifyAndClearExpectations(&observer_
);
189 // Notify when policies change.
190 const base::StringValue
new_value("222");
192 ValueStoreChangeList changes
;
194 ValueStoreChange("bbb", value
.DeepCopy(), new_value
.DeepCopy()));
195 EXPECT_CALL(observer_
,
196 OnSettingsChanged(kTestExtensionId
,
197 settings_namespace::MANAGED
,
198 ValueStoreChange::ToJson(changes
)));
201 policies
.Set("bbb", policy::POLICY_LEVEL_MANDATORY
, policy::POLICY_SCOPE_USER
,
202 new_value
.DeepCopy(), NULL
);
203 store_
->SetCurrentPolicy(policies
);
204 loop_
.RunUntilIdle();
205 Mock::VerifyAndClearExpectations(&observer_
);
207 // Notify when policies are removed.
209 ValueStoreChangeList changes
;
210 changes
.push_back(ValueStoreChange("bbb", new_value
.DeepCopy(), NULL
));
211 EXPECT_CALL(observer_
,
212 OnSettingsChanged(kTestExtensionId
,
213 settings_namespace::MANAGED
,
214 ValueStoreChange::ToJson(changes
)));
217 policies
.Erase("bbb");
218 store_
->SetCurrentPolicy(policies
);
219 loop_
.RunUntilIdle();
220 Mock::VerifyAndClearExpectations(&observer_
);
222 // Don't notify when there aren't any changes.
223 EXPECT_CALL(observer_
, OnSettingsChanged(_
, _
, _
)).Times(0);
224 store_
->SetCurrentPolicy(policies
);
225 loop_
.RunUntilIdle();
226 Mock::VerifyAndClearExpectations(&observer_
);
229 } // namespace extensions