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

Side by Side Diff: chrome/browser/chromeos/cros_settings_unittest.cc

Issue 8727037: Signed settings refactoring: Proper caching and more tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments. Created 9 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/login/signed_settings.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop.h"
12 #include "base/values.h"
13 #include "chrome/browser/chromeos/cros_settings.h"
14 #include "chrome/browser/chromeos/cros_settings_names.h"
15 #include "chrome/browser/chromeos/cros/cros_library.h"
16 #include "chrome/browser/chromeos/login/signed_settings_cache.h"
17 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
18 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_pref_service.h"
21 #include "content/test/test_browser_thread.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace em = enterprise_management;
25 namespace chromeos {
26
27 class CrosSettingsTest : public testing::Test {
28 protected:
29 CrosSettingsTest()
30 : message_loop_(MessageLoop::TYPE_UI),
31 ui_thread_(content::BrowserThread::UI, &message_loop_),
32 file_thread_(content::BrowserThread::FILE, &message_loop_),
33 pointer_factory_(this),
34 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) {
35 }
36
37 virtual ~CrosSettingsTest() {
38 }
39
40 virtual void TearDown() {
41 // Reset the cache between tests.
42 em::PolicyData fake_pol;
43 PrepareEmptyPolicy(&fake_pol);
44 signed_settings_cache::Store(fake_pol, local_state_.Get());
45 CrosSettings::Get()->ReloadProviders();
46 }
47
48 void FetchPref(const std::string& pref) {
49 content::BrowserThread::PostTask(
50 content::BrowserThread::UI, FROM_HERE,
51 base::Bind(&CrosSettingsTest::FetchPrefOnUIThread,
52 pointer_factory_.GetWeakPtr(), pref));
53 }
54
55 void SetPref(const std::string& pref_name, const base::Value* value) {
56 // TODO(pastarmovj): Use the new base::Owned here to wrap |value|.
57 content::BrowserThread::PostTask(
58 content::BrowserThread::UI, FROM_HERE,
59 base::Bind(&CrosSettingsTest::SetPrefOnUIThread,
60 pointer_factory_.GetWeakPtr(), pref_name, value));
61 }
62
63 void FetchPrefOnUIThread(const std::string& pref) {
64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
65 if (expected_props_.find(pref) == expected_props_.end())
66 return;
67
68 if (CrosSettings::Get()->GetTrusted(pref,
69 base::Bind(&CrosSettingsTest::FetchPrefOnUIThread,
70 pointer_factory_.GetWeakPtr(), pref))) {
71 scoped_ptr<base::Value> expected_value(
72 expected_props_.find(pref)->second);
73 const base::Value* pref_value = CrosSettings::Get()->GetPref(pref);
74 if (expected_value.get()) {
75 ASSERT_TRUE(pref_value);
76 ASSERT_TRUE(expected_value->Equals(pref_value));
77 } else {
78 ASSERT_FALSE(pref_value);
79 }
80 expected_props_.erase(pref);
81 }
82 }
83
84 void SetPrefOnUIThread(const std::string& pref_name,
85 const base::Value* value) {
86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
87 CrosSettings::Get()->Set(pref_name, *value);
88 }
89
90 void AddExpectation(const std::string& pref_name, base::Value* value) {
91 expected_props_[pref_name] = value;
92 }
93
94 void PrepareEmptyPolicy(em::PolicyData* policy) {
95 // Prepare some policy blob.
96 em::PolicyFetchResponse response;
97 em::ChromeDeviceSettingsProto pol;
98 policy->set_policy_type(chromeos::kDevicePolicyType);
99 policy->set_username("me@owner");
100 policy->set_policy_value(pol.SerializeAsString());
101 // Wipe the signed settings store.
102 response.set_policy_data(policy->SerializeAsString());
103 response.set_policy_data_signature("false");
104 }
105
106 std::map<std::string, base::Value*> expected_props_;
107
108 MessageLoop message_loop_;
109 content::TestBrowserThread ui_thread_;
110 content::TestBrowserThread file_thread_;
111
112 base::WeakPtrFactory<CrosSettingsTest> pointer_factory_;
113
114 ScopedTestingLocalState local_state_;
115
116 ScopedStubCrosEnabler stub_cros_enabler_;
117 };
118
119 TEST_F(CrosSettingsTest, SetPref) {
120 // Change to something that is not the default.
121 AddExpectation(kAccountsPrefAllowGuest,
122 base::Value::CreateBooleanValue(false));
123 SetPref(kAccountsPrefAllowGuest, expected_props_[kAccountsPrefAllowGuest]);
124 FetchPref(kAccountsPrefAllowGuest);
125 message_loop_.RunAllPending();
126 ASSERT_TRUE(expected_props_.empty());
127 }
128
129 TEST_F(CrosSettingsTest, GetPref) {
130 // We didn't change the default so look for it.
131 AddExpectation(kAccountsPrefAllowGuest,
132 base::Value::CreateBooleanValue(true));
133 FetchPref(kAccountsPrefAllowGuest);
134 message_loop_.RunAllPending();
135 ASSERT_TRUE(expected_props_.empty());
136 }
137
138 TEST_F(CrosSettingsTest, SetWhitelist) {
139 // Setting the whitelist should also switch the value of
140 // kAccountsPrefAllowNewUser to false.
141 scoped_ptr<base::ListValue> whitelist(new base::ListValue);
142 whitelist->Append(base::Value::CreateStringValue("me@owner"));
143 AddExpectation(kAccountsPrefAllowNewUser,
144 base::Value::CreateBooleanValue(false));
145 AddExpectation(kAccountsPrefUsers, whitelist->DeepCopy());
146 SetPref(kAccountsPrefUsers, whitelist.get());
147 FetchPref(kAccountsPrefAllowNewUser);
148 FetchPref(kAccountsPrefUsers);
149 message_loop_.RunAllPending();
150 ASSERT_TRUE(expected_props_.empty());
151 }
152
153 TEST_F(CrosSettingsTest, SetWhitelistWithListOps) {
154 base::ListValue* whitelist = new base::ListValue();
155 scoped_ptr<base::Value> hacky_user(base::Value::CreateStringValue("h@xxor"));
156 whitelist->Append(hacky_user->DeepCopy());
157 AddExpectation(kAccountsPrefAllowNewUser,
158 base::Value::CreateBooleanValue(false));
159 AddExpectation(kAccountsPrefUsers, whitelist->DeepCopy());
160 // Add some user to the whitelist.
161 content::BrowserThread::PostTask(
162 content::BrowserThread::UI, FROM_HERE,
163 base::Bind(&CrosSettings::AppendToList,
164 base::Unretained(CrosSettings::Get()),
165 kAccountsPrefUsers, hacky_user.get()));
166 FetchPref(kAccountsPrefAllowNewUser);
167 FetchPref(kAccountsPrefUsers);
168 message_loop_.RunAllPending();
169 ASSERT_TRUE(expected_props_.empty());
170 }
171
172 TEST_F(CrosSettingsTest, SetWhitelistWithListOps2) {
173 scoped_ptr<base::ListValue> whitelist(new base::ListValue);
174 scoped_ptr<base::Value> hacky_user(base::Value::CreateStringValue("h@xxor"));
175 scoped_ptr<base::Value> lamy_user(base::Value::CreateStringValue("l@mer"));
176 whitelist->Append(hacky_user->DeepCopy());
177 base::ListValue* expected_list = whitelist->DeepCopy();
178 whitelist->Append(lamy_user->DeepCopy());
179 AddExpectation(kAccountsPrefAllowNewUser,
180 base::Value::CreateBooleanValue(false));
181 AddExpectation(kAccountsPrefUsers, whitelist->DeepCopy());
182 SetPref(kAccountsPrefUsers, whitelist.get());
183 FetchPref(kAccountsPrefAllowNewUser);
184 FetchPref(kAccountsPrefUsers);
185 message_loop_.RunAllPending();
186 // Now try to remove one element from that list.
187 AddExpectation(kAccountsPrefUsers, expected_list);
188 content::BrowserThread::PostTask(
189 content::BrowserThread::UI, FROM_HERE,
190 base::Bind(&CrosSettings::RemoveFromList,
191 base::Unretained(CrosSettings::Get()),
192 kAccountsPrefUsers, lamy_user.get()));
193 FetchPref(kAccountsPrefAllowNewUser);
194 FetchPref(kAccountsPrefUsers);
195 message_loop_.RunAllPending();
196 ASSERT_TRUE(expected_props_.empty());
197 }
198
199 TEST_F(CrosSettingsTest, SetEmptyWhitelist) {
200 // Setting the whitelist empty should switch the value of
201 // kAccountsPrefAllowNewUser to true.
202 scoped_ptr<base::ListValue> whitelist(new base::ListValue);
203 scoped_ptr<base::Value> disallow_new(base::Value::CreateBooleanValue(false));
204 AddExpectation(kAccountsPrefAllowNewUser,
205 base::Value::CreateBooleanValue(true));
206 SetPref(kAccountsPrefUsers, whitelist.get());
207 SetPref(kAccountsPrefAllowNewUser, disallow_new.get());
208 FetchPref(kAccountsPrefAllowNewUser);
209 FetchPref(kAccountsPrefUsers);
210 message_loop_.RunAllPending();
211 ASSERT_TRUE(expected_props_.empty());
212 }
213
214 TEST_F(CrosSettingsTest, SetWhitelistAndNoNewUsers) {
215 // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to
216 // false (which is the implicit value too).
217 scoped_ptr<base::ListValue> whitelist(new base::ListValue);
218 whitelist->Append(base::Value::CreateStringValue("me@owner"));
219 AddExpectation(kAccountsPrefUsers, whitelist->DeepCopy());
220 AddExpectation(kAccountsPrefAllowNewUser,
221 base::Value::CreateBooleanValue(false));
222 SetPref(kAccountsPrefUsers, whitelist.get());
223 SetPref(kAccountsPrefAllowNewUser,
224 expected_props_[kAccountsPrefAllowNewUser]);
225 FetchPref(kAccountsPrefAllowNewUser);
226 FetchPref(kAccountsPrefUsers);
227 message_loop_.RunAllPending();
228 ASSERT_TRUE(expected_props_.empty());
229 }
230
231 TEST_F(CrosSettingsTest, SetAllowNewUsers) {
232 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok.
233 AddExpectation(kAccountsPrefAllowNewUser,
234 base::Value::CreateBooleanValue(true));
235 SetPref(kAccountsPrefAllowNewUser,
236 expected_props_[kAccountsPrefAllowNewUser]);
237 FetchPref(kAccountsPrefAllowNewUser);
238 message_loop_.RunAllPending();
239 ASSERT_TRUE(expected_props_.empty());
240 }
241
242 TEST_F(CrosSettingsTest, SetOwner) {
243 scoped_ptr<base::Value> hacky_owner(base::Value::CreateStringValue("h@xxor"));
244 AddExpectation(kDeviceOwner, base::Value::CreateStringValue("h@xxor"));
245 SetPref(kDeviceOwner, hacky_owner.get());
246 FetchPref(kDeviceOwner);
247 message_loop_.RunAllPending();
248 ASSERT_TRUE(expected_props_.empty());
249 }
250
251 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698