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

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. Fixed small bugs. Rebased to ToT. 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 <map>
8 #include <string>
9
10 #include "base/bind.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop.h"
13 #include "base/stl_util.h"
14 #include "base/values.h"
15 #include "chrome/browser/chromeos/cros_settings.h"
16 #include "chrome/browser/chromeos/cros_settings_names.h"
17 #include "chrome/browser/chromeos/cros/cros_library.h"
18 #include "chrome/browser/chromeos/login/signed_settings_cache.h"
19 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
20 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_pref_service.h"
23 #include "content/test/test_browser_thread.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace em = enterprise_management;
27 namespace chromeos {
28
29 class CrosSettingsTest : public testing::Test {
30 protected:
31 CrosSettingsTest()
32 : message_loop_(MessageLoop::TYPE_UI),
33 ui_thread_(content::BrowserThread::UI, &message_loop_),
34 file_thread_(content::BrowserThread::FILE, &message_loop_),
35 pointer_factory_(this),
36 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) {
37 }
38
39 virtual ~CrosSettingsTest() {
40 }
41
42 virtual void SetUp() {
43 // Reset the cache between tests.
44 ApplyEmptyPolicy();
45 }
46
47 virtual void TearDown() {
48 message_loop_.RunAllPending();
49 ASSERT_TRUE(expected_props_.empty());
50 // Reset the cache between tests.
51 ApplyEmptyPolicy();
52 STLDeleteValues(&expected_props_);
53 }
54
55 void FetchPref(const std::string& pref) {
56 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
57 if (expected_props_.find(pref) == expected_props_.end())
58 return;
59
60 if (CrosSettings::Get()->GetTrusted(pref,
61 base::Bind(&CrosSettingsTest::FetchPref,
62 pointer_factory_.GetWeakPtr(), pref))) {
63 scoped_ptr<base::Value> expected_value(
64 expected_props_.find(pref)->second);
65 const base::Value* pref_value = CrosSettings::Get()->GetPref(pref);
66 if (expected_value.get()) {
67 ASSERT_TRUE(pref_value);
68 ASSERT_TRUE(expected_value->Equals(pref_value));
69 } else {
70 ASSERT_FALSE(pref_value);
71 }
72 expected_props_.erase(pref);
73 }
74 }
75
76 void SetPref(const std::string& pref_name, const base::Value* value) {
77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
78 CrosSettings::Get()->Set(pref_name, *value);
79 }
80
81 void AddExpectation(const std::string& pref_name, base::Value* value) {
82 base::Value*& entry = expected_props_[pref_name];
83 delete entry;
84 entry = value;
85 }
86
87 void PrepareEmptyPolicy(em::PolicyData* policy) {
88 // Prepare some policy blob.
89 em::PolicyFetchResponse response;
90 em::ChromeDeviceSettingsProto pol;
91 policy->set_policy_type(chromeos::kDevicePolicyType);
92 policy->set_username("me@owner");
93 policy->set_policy_value(pol.SerializeAsString());
94 // Wipe the signed settings store.
95 response.set_policy_data(policy->SerializeAsString());
96 response.set_policy_data_signature("false");
97 }
98
99 void ApplyEmptyPolicy() {
100 em::PolicyData fake_pol;
101 PrepareEmptyPolicy(&fake_pol);
102 signed_settings_cache::Store(fake_pol, local_state_.Get());
103 CrosSettings::Get()->ReloadProviders();
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 }
135
136 TEST_F(CrosSettingsTest, SetWhitelist) {
137 // Setting the whitelist should also switch the value of
138 // kAccountsPrefAllowNewUser to false.
139 base::ListValue whitelist;
140 whitelist.Append(base::Value::CreateStringValue("me@owner"));
141 AddExpectation(kAccountsPrefAllowNewUser,
142 base::Value::CreateBooleanValue(false));
143 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
144 SetPref(kAccountsPrefUsers, &whitelist);
145 FetchPref(kAccountsPrefAllowNewUser);
146 FetchPref(kAccountsPrefUsers);
147 }
148
149 TEST_F(CrosSettingsTest, SetWhitelistWithListOps) {
150 base::ListValue* whitelist = new base::ListValue();
151 base::StringValue hacky_user("h@xxor");
152 whitelist->Append(hacky_user.DeepCopy());
153 AddExpectation(kAccountsPrefAllowNewUser,
154 base::Value::CreateBooleanValue(false));
155 AddExpectation(kAccountsPrefUsers, whitelist);
156 // Add some user to the whitelist.
157 CrosSettings::Get()->AppendToList(kAccountsPrefUsers, &hacky_user);
158 FetchPref(kAccountsPrefAllowNewUser);
159 FetchPref(kAccountsPrefUsers);
160 }
161
162 TEST_F(CrosSettingsTest, SetWhitelistWithListOps2) {
163 base::ListValue whitelist;
164 base::StringValue hacky_user("h@xxor");
165 base::StringValue lamy_user("l@mer");
166 whitelist.Append(hacky_user.DeepCopy());
167 base::ListValue* expected_list = whitelist.DeepCopy();
168 whitelist.Append(lamy_user.DeepCopy());
169 AddExpectation(kAccountsPrefAllowNewUser,
170 base::Value::CreateBooleanValue(false));
171 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
172 SetPref(kAccountsPrefUsers, &whitelist);
173 FetchPref(kAccountsPrefAllowNewUser);
174 FetchPref(kAccountsPrefUsers);
175 message_loop_.RunAllPending();
176 ASSERT_TRUE(expected_props_.empty());
177 // Now try to remove one element from that list.
178 AddExpectation(kAccountsPrefUsers, expected_list);
179 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, &lamy_user);
180 FetchPref(kAccountsPrefAllowNewUser);
181 FetchPref(kAccountsPrefUsers);
182 }
183
184 TEST_F(CrosSettingsTest, SetEmptyWhitelist) {
185 // Setting the whitelist empty should switch the value of
186 // kAccountsPrefAllowNewUser to true.
187 base::ListValue whitelist;
188 base::FundamentalValue disallow_new(false);
189 AddExpectation(kAccountsPrefAllowNewUser,
190 base::Value::CreateBooleanValue(true));
191 SetPref(kAccountsPrefUsers, &whitelist);
192 SetPref(kAccountsPrefAllowNewUser, &disallow_new);
193 FetchPref(kAccountsPrefAllowNewUser);
194 FetchPref(kAccountsPrefUsers);
195 }
196
197 TEST_F(CrosSettingsTest, SetWhitelistAndNoNewUsers) {
198 // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to
199 // false (which is the implicit value too).
200 base::ListValue whitelist;
201 whitelist.Append(base::Value::CreateStringValue("me@owner"));
202 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
203 AddExpectation(kAccountsPrefAllowNewUser,
204 base::Value::CreateBooleanValue(false));
205 SetPref(kAccountsPrefUsers, &whitelist);
206 SetPref(kAccountsPrefAllowNewUser,
207 expected_props_[kAccountsPrefAllowNewUser]);
208 FetchPref(kAccountsPrefAllowNewUser);
209 FetchPref(kAccountsPrefUsers);
210 }
211
212 TEST_F(CrosSettingsTest, SetAllowNewUsers) {
213 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok.
214 AddExpectation(kAccountsPrefAllowNewUser,
215 base::Value::CreateBooleanValue(true));
216 SetPref(kAccountsPrefAllowNewUser,
217 expected_props_[kAccountsPrefAllowNewUser]);
218 FetchPref(kAccountsPrefAllowNewUser);
219 }
220
221 TEST_F(CrosSettingsTest, SetOwner) {
222 base::StringValue hacky_owner("h@xxor");
223 AddExpectation(kDeviceOwner, base::Value::CreateStringValue("h@xxor"));
224 SetPref(kDeviceOwner, &hacky_owner);
225 FetchPref(kDeviceOwner);
226 }
227
228 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/cros_settings_provider.h ('k') | chrome/browser/chromeos/device_settings_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698