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

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

Issue 10918027: Revert 154457 - Switch from SignedSettings to DeviceSettingsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 3 months 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/settings/signed_settings.h"
6
5 #include <map> 7 #include <map>
6 #include <string> 8 #include <string>
7 9
8 #include "base/bind.h" 10 #include "base/bind.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop.h" 12 #include "base/message_loop.h"
12 #include "base/stl_util.h" 13 #include "base/stl_util.h"
13 #include "base/values.h" 14 #include "base/values.h"
15 #include "chrome/browser/chromeos/cros/cros_library.h"
16 #include "chrome/browser/chromeos/login/mock_user_manager.h"
14 #include "chrome/browser/chromeos/settings/cros_settings.h" 17 #include "chrome/browser/chromeos/settings/cros_settings.h"
15 #include "chrome/browser/chromeos/settings/cros_settings_names.h" 18 #include "chrome/browser/chromeos/settings/cros_settings_names.h"
16 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" 19 #include "chrome/browser/chromeos/settings/signed_settings_cache.h"
17 #include "chrome/browser/policy/cloud_policy_constants.h"
18 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" 20 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
19 #include "chrome/browser/policy/proto/device_management_backend.pb.h" 21 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
20 #include "chrome/test/base/testing_browser_process.h" 22 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_pref_service.h" 23 #include "chrome/test/base/testing_pref_service.h"
22 #include "content/public/test/test_browser_thread.h" 24 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
24 26
27 using ::testing::AnyNumber;
28 using ::testing::Return;
29
25 namespace em = enterprise_management; 30 namespace em = enterprise_management;
26
27 namespace chromeos { 31 namespace chromeos {
28 32
29 class CrosSettingsTest : public testing::Test { 33 class CrosSettingsTest : public testing::Test {
30 protected: 34 protected:
31 CrosSettingsTest() 35 CrosSettingsTest()
32 : message_loop_(MessageLoop::TYPE_UI), 36 : message_loop_(MessageLoop::TYPE_UI),
33 ui_thread_(content::BrowserThread::UI, &message_loop_), 37 ui_thread_(content::BrowserThread::UI, &message_loop_),
34 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), 38 file_thread_(content::BrowserThread::FILE, &message_loop_),
35 weak_factory_(this) {} 39 pointer_factory_(this),
40 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) {
41 }
36 42
37 virtual ~CrosSettingsTest() {} 43 virtual ~CrosSettingsTest() {
44 }
38 45
39 virtual void TearDown() OVERRIDE { 46 virtual void SetUp() {
47 EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner())
48 .Times(AnyNumber())
49 .WillRepeatedly(Return(true));
50 // Reset the cache between tests.
51 ApplyEmptyPolicy();
52 }
53
54 virtual void TearDown() {
55 message_loop_.RunAllPending();
40 ASSERT_TRUE(expected_props_.empty()); 56 ASSERT_TRUE(expected_props_.empty());
57 // Reset the cache between tests.
58 ApplyEmptyPolicy();
41 STLDeleteValues(&expected_props_); 59 STLDeleteValues(&expected_props_);
42 expected_props_.clear();
43 } 60 }
44 61
45 void FetchPref(const std::string& pref) { 62 void FetchPref(const std::string& pref) {
46 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 63 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
47 if (expected_props_.find(pref) == expected_props_.end()) 64 if (expected_props_.find(pref) == expected_props_.end())
48 return; 65 return;
49 66
50 if (CrosSettingsProvider::TRUSTED == 67 if (CrosSettingsProvider::TRUSTED ==
51 settings_.PrepareTrustedValues( 68 CrosSettings::Get()->PrepareTrustedValues(
52 base::Bind(&CrosSettingsTest::FetchPref, 69 base::Bind(&CrosSettingsTest::FetchPref,
53 weak_factory_.GetWeakPtr(), pref))) { 70 pointer_factory_.GetWeakPtr(), pref))) {
54 scoped_ptr<base::Value> expected_value( 71 scoped_ptr<base::Value> expected_value(
55 expected_props_.find(pref)->second); 72 expected_props_.find(pref)->second);
56 const base::Value* pref_value = settings_.GetPref(pref); 73 const base::Value* pref_value = CrosSettings::Get()->GetPref(pref);
57 if (expected_value.get()) { 74 if (expected_value.get()) {
58 ASSERT_TRUE(pref_value); 75 ASSERT_TRUE(pref_value);
59 ASSERT_TRUE(expected_value->Equals(pref_value)); 76 ASSERT_TRUE(expected_value->Equals(pref_value));
60 } else { 77 } else {
61 ASSERT_FALSE(pref_value); 78 ASSERT_FALSE(pref_value);
62 } 79 }
63 expected_props_.erase(pref); 80 expected_props_.erase(pref);
64 } 81 }
65 } 82 }
66 83
67 void SetPref(const std::string& pref_name, const base::Value* value) { 84 void SetPref(const std::string& pref_name, const base::Value* value) {
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
69 settings_.Set(pref_name, *value); 86 CrosSettings::Get()->Set(pref_name, *value);
70 } 87 }
71 88
72 void AddExpectation(const std::string& pref_name, base::Value* value) { 89 void AddExpectation(const std::string& pref_name, base::Value* value) {
73 base::Value*& entry = expected_props_[pref_name]; 90 base::Value*& entry = expected_props_[pref_name];
74 delete entry; 91 delete entry;
75 entry = value; 92 entry = value;
76 } 93 }
77 94
78 void PrepareEmptyPolicy(em::PolicyData* policy) { 95 void PrepareEmptyPolicy(em::PolicyData* policy) {
79 // Prepare some policy blob. 96 // Prepare some policy blob.
80 em::PolicyFetchResponse response; 97 em::PolicyFetchResponse response;
81 em::ChromeDeviceSettingsProto pol; 98 em::ChromeDeviceSettingsProto pol;
82 policy->set_policy_type(policy::dm_protocol::kChromeDevicePolicyType); 99 policy->set_policy_type(chromeos::kDevicePolicyType);
83 policy->set_username("me@owner"); 100 policy->set_username("me@owner");
84 policy->set_policy_value(pol.SerializeAsString()); 101 policy->set_policy_value(pol.SerializeAsString());
85 // Wipe the signed settings store. 102 // Wipe the signed settings store.
86 response.set_policy_data(policy->SerializeAsString()); 103 response.set_policy_data(policy->SerializeAsString());
87 response.set_policy_data_signature("false"); 104 response.set_policy_data_signature("false");
88 } 105 }
89 106
107 void ApplyEmptyPolicy() {
108 em::PolicyData fake_pol;
109 PrepareEmptyPolicy(&fake_pol);
110 signed_settings_cache::Store(fake_pol, local_state_.Get());
111 CrosSettings::Get()->ReloadProviders();
112 }
113
114 std::map<std::string, base::Value*> expected_props_;
115
90 MessageLoop message_loop_; 116 MessageLoop message_loop_;
91 content::TestBrowserThread ui_thread_; 117 content::TestBrowserThread ui_thread_;
118 content::TestBrowserThread file_thread_;
119
120 base::WeakPtrFactory<CrosSettingsTest> pointer_factory_;
92 121
93 ScopedTestingLocalState local_state_; 122 ScopedTestingLocalState local_state_;
94 ScopedDeviceSettingsTestHelper device_settings_test_helper_;
95 CrosSettings settings_;
96 123
97 base::WeakPtrFactory<CrosSettingsTest> weak_factory_; 124 ScopedMockUserManagerEnabler mock_user_manager_;
98 125 ScopedStubCrosEnabler stub_cros_enabler_;
99 std::map<std::string, base::Value*> expected_props_;
100 }; 126 };
101 127
102 TEST_F(CrosSettingsTest, SetPref) { 128 TEST_F(CrosSettingsTest, SetPref) {
103 // Change to something that is not the default. 129 // Change to something that is not the default.
104 AddExpectation(kAccountsPrefAllowGuest, 130 AddExpectation(kAccountsPrefAllowGuest,
105 base::Value::CreateBooleanValue(false)); 131 base::Value::CreateBooleanValue(false));
106 SetPref(kAccountsPrefAllowGuest, expected_props_[kAccountsPrefAllowGuest]); 132 SetPref(kAccountsPrefAllowGuest, expected_props_[kAccountsPrefAllowGuest]);
107 FetchPref(kAccountsPrefAllowGuest); 133 FetchPref(kAccountsPrefAllowGuest);
134 message_loop_.RunAllPending();
108 ASSERT_TRUE(expected_props_.empty()); 135 ASSERT_TRUE(expected_props_.empty());
109 } 136 }
110 137
111 TEST_F(CrosSettingsTest, GetPref) { 138 TEST_F(CrosSettingsTest, GetPref) {
112 // We didn't change the default so look for it. 139 // We didn't change the default so look for it.
113 AddExpectation(kAccountsPrefAllowGuest, 140 AddExpectation(kAccountsPrefAllowGuest,
114 base::Value::CreateBooleanValue(true)); 141 base::Value::CreateBooleanValue(true));
115 FetchPref(kAccountsPrefAllowGuest); 142 FetchPref(kAccountsPrefAllowGuest);
116 } 143 }
117 144
(...skipping 11 matching lines...) Expand all
129 } 156 }
130 157
131 TEST_F(CrosSettingsTest, SetWhitelistWithListOps) { 158 TEST_F(CrosSettingsTest, SetWhitelistWithListOps) {
132 base::ListValue* whitelist = new base::ListValue(); 159 base::ListValue* whitelist = new base::ListValue();
133 base::StringValue hacky_user("h@xxor"); 160 base::StringValue hacky_user("h@xxor");
134 whitelist->Append(hacky_user.DeepCopy()); 161 whitelist->Append(hacky_user.DeepCopy());
135 AddExpectation(kAccountsPrefAllowNewUser, 162 AddExpectation(kAccountsPrefAllowNewUser,
136 base::Value::CreateBooleanValue(false)); 163 base::Value::CreateBooleanValue(false));
137 AddExpectation(kAccountsPrefUsers, whitelist); 164 AddExpectation(kAccountsPrefUsers, whitelist);
138 // Add some user to the whitelist. 165 // Add some user to the whitelist.
139 settings_.AppendToList(kAccountsPrefUsers, &hacky_user); 166 CrosSettings::Get()->AppendToList(kAccountsPrefUsers, &hacky_user);
140 FetchPref(kAccountsPrefAllowNewUser); 167 FetchPref(kAccountsPrefAllowNewUser);
141 FetchPref(kAccountsPrefUsers); 168 FetchPref(kAccountsPrefUsers);
142 } 169 }
143 170
144 TEST_F(CrosSettingsTest, SetWhitelistWithListOps2) { 171 TEST_F(CrosSettingsTest, SetWhitelistWithListOps2) {
145 base::ListValue whitelist; 172 base::ListValue whitelist;
146 base::StringValue hacky_user("h@xxor"); 173 base::StringValue hacky_user("h@xxor");
147 base::StringValue lamy_user("l@mer"); 174 base::StringValue lamy_user("l@mer");
148 whitelist.Append(hacky_user.DeepCopy()); 175 whitelist.Append(hacky_user.DeepCopy());
149 base::ListValue* expected_list = whitelist.DeepCopy(); 176 base::ListValue* expected_list = whitelist.DeepCopy();
150 whitelist.Append(lamy_user.DeepCopy()); 177 whitelist.Append(lamy_user.DeepCopy());
151 AddExpectation(kAccountsPrefAllowNewUser, 178 AddExpectation(kAccountsPrefAllowNewUser,
152 base::Value::CreateBooleanValue(false)); 179 base::Value::CreateBooleanValue(false));
153 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy()); 180 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
154 SetPref(kAccountsPrefUsers, &whitelist); 181 SetPref(kAccountsPrefUsers, &whitelist);
155 FetchPref(kAccountsPrefAllowNewUser); 182 FetchPref(kAccountsPrefAllowNewUser);
156 FetchPref(kAccountsPrefUsers); 183 FetchPref(kAccountsPrefUsers);
184 message_loop_.RunAllPending();
157 ASSERT_TRUE(expected_props_.empty()); 185 ASSERT_TRUE(expected_props_.empty());
158 // Now try to remove one element from that list. 186 // Now try to remove one element from that list.
159 AddExpectation(kAccountsPrefUsers, expected_list); 187 AddExpectation(kAccountsPrefUsers, expected_list);
160 settings_.RemoveFromList(kAccountsPrefUsers, &lamy_user); 188 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, &lamy_user);
161 FetchPref(kAccountsPrefAllowNewUser); 189 FetchPref(kAccountsPrefAllowNewUser);
162 FetchPref(kAccountsPrefUsers); 190 FetchPref(kAccountsPrefUsers);
163 } 191 }
164 192
165 TEST_F(CrosSettingsTest, SetEmptyWhitelist) { 193 TEST_F(CrosSettingsTest, SetEmptyWhitelist) {
166 // Setting the whitelist empty should switch the value of 194 // Setting the whitelist empty should switch the value of
167 // kAccountsPrefAllowNewUser to true. 195 // kAccountsPrefAllowNewUser to true.
168 base::ListValue whitelist; 196 base::ListValue whitelist;
169 base::FundamentalValue disallow_new(false); 197 base::FundamentalValue disallow_new(false);
170 AddExpectation(kAccountsPrefAllowNewUser, 198 AddExpectation(kAccountsPrefAllowNewUser,
(...skipping 21 matching lines...) Expand all
192 220
193 TEST_F(CrosSettingsTest, SetAllowNewUsers) { 221 TEST_F(CrosSettingsTest, SetAllowNewUsers) {
194 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok. 222 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok.
195 AddExpectation(kAccountsPrefAllowNewUser, 223 AddExpectation(kAccountsPrefAllowNewUser,
196 base::Value::CreateBooleanValue(true)); 224 base::Value::CreateBooleanValue(true));
197 SetPref(kAccountsPrefAllowNewUser, 225 SetPref(kAccountsPrefAllowNewUser,
198 expected_props_[kAccountsPrefAllowNewUser]); 226 expected_props_[kAccountsPrefAllowNewUser]);
199 FetchPref(kAccountsPrefAllowNewUser); 227 FetchPref(kAccountsPrefAllowNewUser);
200 } 228 }
201 229
230 TEST_F(CrosSettingsTest, SetOwner) {
231 base::StringValue hacky_owner("h@xxor");
232 AddExpectation(kDeviceOwner, base::Value::CreateStringValue("h@xxor"));
233 SetPref(kDeviceOwner, &hacky_owner);
234 FetchPref(kDeviceOwner);
235 }
236
202 TEST_F(CrosSettingsTest, SetEphemeralUsersEnabled) { 237 TEST_F(CrosSettingsTest, SetEphemeralUsersEnabled) {
203 base::FundamentalValue ephemeral_users_enabled(true); 238 base::FundamentalValue ephemeral_users_enabled(true);
204 AddExpectation(kAccountsPrefEphemeralUsersEnabled, 239 AddExpectation(kAccountsPrefEphemeralUsersEnabled,
205 base::Value::CreateBooleanValue(true)); 240 base::Value::CreateBooleanValue(true));
206 SetPref(kAccountsPrefEphemeralUsersEnabled, &ephemeral_users_enabled); 241 SetPref(kAccountsPrefEphemeralUsersEnabled, &ephemeral_users_enabled);
207 FetchPref(kAccountsPrefEphemeralUsersEnabled); 242 FetchPref(kAccountsPrefEphemeralUsersEnabled);
208 } 243 }
209 244
210 TEST_F(CrosSettingsTest, FindEmailInList) { 245 TEST_F(CrosSettingsTest, FindEmailInList) {
211 base::ListValue list; 246 base::ListValue list;
212 list.Append(base::Value::CreateStringValue("user@example.com")); 247 list.Append(base::Value::CreateStringValue("user@example.com"));
213 list.Append(base::Value::CreateStringValue("nodomain")); 248 list.Append(base::Value::CreateStringValue("nodomain"));
214 list.Append(base::Value::CreateStringValue("with.dots@gmail.com")); 249 list.Append(base::Value::CreateStringValue("with.dots@gmail.com"));
215 list.Append(base::Value::CreateStringValue("Upper@example.com")); 250 list.Append(base::Value::CreateStringValue("Upper@example.com"));
216 251
217 CrosSettings* cs = &settings_; 252 CrosSettings* cs = CrosSettings::Get();
218 cs->Set(kAccountsPrefUsers, list); 253 cs->Set(kAccountsPrefUsers, list);
219 254
220 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "user@example.com")); 255 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "user@example.com"));
221 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "us.er@example.com")); 256 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "us.er@example.com"));
222 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "USER@example.com")); 257 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "USER@example.com"));
223 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "user")); 258 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "user"));
224 259
225 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain")); 260 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain"));
226 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain@gmail.com")); 261 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain@gmail.com"));
227 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "no.domain@gmail.com")); 262 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "no.domain@gmail.com"));
228 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "NO.DOMAIN")); 263 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "NO.DOMAIN"));
229 264
230 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "with.dots@gmail.com")); 265 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "with.dots@gmail.com"));
231 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "withdots@gmail.com")); 266 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "withdots@gmail.com"));
232 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITH.DOTS@gmail.com")); 267 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITH.DOTS@gmail.com"));
233 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITHDOTS")); 268 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITHDOTS"));
234 269
235 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "Upper@example.com")); 270 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "Upper@example.com"));
236 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "U.pper@example.com")); 271 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "U.pper@example.com"));
237 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "Upper")); 272 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "Upper"));
238 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "upper@example.com")); 273 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "upper@example.com"));
239 } 274 }
240 275
241 } // namespace chromeos 276 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/settings/cros_settings_provider.h ('k') | chrome/browser/chromeos/settings/device_settings_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698