OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/extensions/settings/policy_value_store.h" | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/files/scoped_temp_dir.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/message_loop.h" | |
12 #include "chrome/browser/extensions/settings/settings_observer.h" | |
13 #include "chrome/browser/policy/policy_map.h" | |
14 #include "chrome/browser/value_store/leveldb_value_store.h" | |
15 #include "chrome/browser/value_store/value_store_unittest.h" | |
16 #include "content/public/test/test_browser_thread.h" | |
17 #include "testing/gmock/include/gmock/gmock.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using testing::_; | |
21 using testing::Mock; | |
22 | |
23 namespace extensions { | |
24 | |
25 namespace { | |
26 | |
27 const char kTestExtensionId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; | |
28 | |
29 class MockSettingsObserver : public SettingsObserver { | |
30 public: | |
31 MOCK_METHOD3(OnSettingsChanged, void( | |
32 const std::string& extension_id, | |
33 settings_namespace::Namespace settings_namespace, | |
34 const std::string& changes_json)); | |
35 }; | |
36 | |
37 // Extends PolicyValueStore by overriding the mutating methods, so that the | |
38 // Get() base implementation can be tested with the ValueStoreTest parameterized | |
39 // tests. | |
40 class MutablePolicyValueStore : public PolicyValueStore { | |
41 public: | |
42 explicit MutablePolicyValueStore(const FilePath& path) | |
43 : PolicyValueStore(kTestExtensionId, | |
44 make_scoped_refptr(new SettingsObserverList()), | |
45 scoped_ptr<ValueStore>(new LeveldbValueStore(path))) {} | |
46 virtual ~MutablePolicyValueStore() {} | |
47 | |
48 virtual WriteResult Set( | |
49 WriteOptions options, | |
50 const std::string& key, | |
51 const base::Value& value) OVERRIDE { | |
52 return delegate()->Set(options, key, value); | |
53 } | |
54 | |
55 virtual WriteResult Set( | |
56 WriteOptions options, const base::DictionaryValue& values) OVERRIDE { | |
57 return delegate()->Set(options, values); | |
58 } | |
59 | |
60 virtual WriteResult Remove(const std::string& key) OVERRIDE { | |
61 return delegate()->Remove(key); | |
62 } | |
63 | |
64 virtual WriteResult Remove(const std::vector<std::string>& keys) OVERRIDE { | |
65 return delegate()->Remove(keys); | |
66 } | |
67 | |
68 virtual WriteResult Clear() OVERRIDE { | |
69 return delegate()->Clear(); | |
70 } | |
71 | |
72 private: | |
73 DISALLOW_COPY_AND_ASSIGN(MutablePolicyValueStore); | |
74 }; | |
75 | |
76 ValueStore* Param(const FilePath& file_path) { | |
77 return new MutablePolicyValueStore(file_path); | |
78 } | |
79 | |
80 } // namespace | |
81 | |
82 INSTANTIATE_TEST_CASE_P( | |
83 PolicyValueStoreTest, | |
84 ValueStoreTest, | |
85 testing::Values(&Param)); | |
86 | |
87 class PolicyValueStoreTest : public testing::Test { | |
88 public: | |
89 PolicyValueStoreTest() | |
90 : file_thread_(content::BrowserThread::FILE, &loop_) {} | |
91 virtual ~PolicyValueStoreTest() {} | |
92 | |
93 virtual void SetUp() OVERRIDE { | |
94 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | |
95 observers_ = new SettingsObserverList(); | |
96 observers_->AddObserver(&observer_); | |
97 store_.reset(new PolicyValueStore( | |
98 kTestExtensionId, | |
99 observers_, | |
100 scoped_ptr<ValueStore>( | |
101 new LeveldbValueStore(scoped_temp_dir_.path())))); | |
102 } | |
103 | |
104 virtual void TearDown() OVERRIDE { | |
105 observers_->RemoveObserver(&observer_); | |
106 store_.reset(); | |
107 } | |
108 | |
109 protected: | |
110 base::ScopedTempDir scoped_temp_dir_; | |
111 MessageLoop loop_; | |
112 content::TestBrowserThread file_thread_; | |
113 scoped_ptr<PolicyValueStore> store_; | |
114 MockSettingsObserver observer_; | |
115 scoped_refptr<SettingsObserverList> observers_; | |
116 }; | |
117 | |
118 TEST_F(PolicyValueStoreTest, DontProvideRecommendedPolicies) { | |
119 policy::PolicyMap policies; | |
120 base::FundamentalValue expected(123); | |
121 policies.Set("must", policy::POLICY_LEVEL_MANDATORY, | |
122 policy::POLICY_SCOPE_USER, expected.DeepCopy()); | |
123 policies.Set("may", policy::POLICY_LEVEL_RECOMMENDED, | |
124 policy::POLICY_SCOPE_USER, base::Value::CreateIntegerValue(456)); | |
125 store_->SetCurrentPolicy(policies, false); | |
126 ValueStore::ReadResult result = store_->Get(); | |
127 ASSERT_FALSE(result->HasError()); | |
128 EXPECT_EQ(1u, result->settings()->size()); | |
129 base::Value* value = NULL; | |
130 EXPECT_FALSE(result->settings()->Get("may", &value)); | |
131 EXPECT_TRUE(result->settings()->Get("must", &value)); | |
132 EXPECT_TRUE(base::Value::Equals(&expected, value)); | |
133 } | |
134 | |
135 TEST_F(PolicyValueStoreTest, ReadOnly) { | |
136 ValueStore::WriteOptions options = ValueStore::DEFAULTS; | |
137 | |
138 base::StringValue string_value("value"); | |
139 EXPECT_TRUE(store_->Set(options, "key", string_value)->HasError()); | |
140 | |
141 base::DictionaryValue dict; | |
142 dict.SetString("key", "value"); | |
143 EXPECT_TRUE(store_->Set(options, dict)->HasError()); | |
144 | |
145 EXPECT_TRUE(store_->Remove("key")->HasError()); | |
146 std::vector<std::string> keys; | |
147 keys.push_back("key"); | |
148 EXPECT_TRUE(store_->Remove(keys)->HasError()); | |
149 EXPECT_TRUE(store_->Clear()->HasError()); | |
150 } | |
151 | |
152 TEST_F(PolicyValueStoreTest, NotifyOnChanges) { | |
153 policy::PolicyMap policies; | |
154 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
155 base::Value::CreateStringValue("111")); | |
156 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | |
157 // No notification when setting the initial policy. | |
158 store_->SetCurrentPolicy(policies, false); | |
159 loop_.RunUntilIdle(); | |
160 Mock::VerifyAndClearExpectations(&observer_); | |
161 | |
162 // And no notifications on changes when not asked for. | |
163 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
164 base::Value::CreateStringValue("222")); | |
165 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
166 base::Value::CreateStringValue("223")); | |
167 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | |
168 store_->SetCurrentPolicy(policies, false); | |
169 loop_.RunUntilIdle(); | |
170 Mock::VerifyAndClearExpectations(&observer_); | |
171 | |
172 // Notify when new policies are added. | |
173 ValueStoreChangeList changes; | |
174 base::StringValue value("333"); | |
175 changes.push_back(ValueStoreChange("ccc", NULL, value.DeepCopy())); | |
176 policies.Set("ccc", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
177 value.DeepCopy()); | |
178 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | |
179 settings_namespace::MANAGED, | |
180 ValueStoreChange::ToJson(changes))); | |
181 store_->SetCurrentPolicy(policies, true); | |
182 loop_.RunUntilIdle(); | |
183 Mock::VerifyAndClearExpectations(&observer_); | |
184 | |
185 // Notify when policies change. | |
186 changes.clear(); | |
187 base::StringValue new_value("444"); | |
188 changes.push_back( | |
189 ValueStoreChange("ccc", value.DeepCopy(), new_value.DeepCopy())); | |
190 policies.Set("ccc", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | |
191 new_value.DeepCopy()); | |
192 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | |
193 settings_namespace::MANAGED, | |
194 ValueStoreChange::ToJson(changes))); | |
195 store_->SetCurrentPolicy(policies, true); | |
196 loop_.RunUntilIdle(); | |
197 Mock::VerifyAndClearExpectations(&observer_); | |
198 | |
199 // Notify when policies are removed. | |
200 changes.clear(); | |
201 changes.push_back(ValueStoreChange("ccc", new_value.DeepCopy(), NULL)); | |
202 policies.Erase("ccc"); | |
203 EXPECT_CALL(observer_, OnSettingsChanged(kTestExtensionId, | |
204 settings_namespace::MANAGED, | |
205 ValueStoreChange::ToJson(changes))); | |
206 store_->SetCurrentPolicy(policies, true); | |
207 loop_.RunUntilIdle(); | |
208 Mock::VerifyAndClearExpectations(&observer_); | |
209 | |
210 // Don't notify when there aren't changes. | |
211 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | |
212 store_->SetCurrentPolicy(policies, true); | |
213 loop_.RunUntilIdle(); | |
214 Mock::VerifyAndClearExpectations(&observer_); | |
215 } | |
216 | |
217 } // namespace extensions | |
OLD | NEW |