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.
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/stl_util.h"
13 #include "base/values.h"
14 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
15 #include "chrome/browser/chromeos/settings/cros_settings.h"
16 #include "chrome/browser/chromeos/settings/device_settings_service.h"
17 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
18 #include "chrome/test/base/scoped_testing_local_state.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chromeos/settings/cros_settings_names.h"
21 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "policy/proto/device_management_backend.pb.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 namespace em
= enterprise_management
;
30 class CrosSettingsTest
: public testing::Test
{
33 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
34 local_state_(TestingBrowserProcess::GetGlobal()),
35 settings_(DeviceSettingsService::Get()),
36 weak_factory_(this) {}
38 virtual ~CrosSettingsTest() {}
40 virtual void TearDown() OVERRIDE
{
41 ASSERT_TRUE(expected_props_
.empty());
42 STLDeleteValues(&expected_props_
);
43 expected_props_
.clear();
46 void FetchPref(const std::string
& pref
) {
47 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
48 if (expected_props_
.find(pref
) == expected_props_
.end())
51 if (CrosSettingsProvider::TRUSTED
==
52 settings_
.PrepareTrustedValues(
53 base::Bind(&CrosSettingsTest::FetchPref
,
54 weak_factory_
.GetWeakPtr(), pref
))) {
55 scoped_ptr
<base::Value
> expected_value(
56 expected_props_
.find(pref
)->second
);
57 const base::Value
* pref_value
= settings_
.GetPref(pref
);
58 if (expected_value
.get()) {
59 ASSERT_TRUE(pref_value
);
60 ASSERT_TRUE(expected_value
->Equals(pref_value
));
62 ASSERT_FALSE(pref_value
);
64 expected_props_
.erase(pref
);
68 void SetPref(const std::string
& pref_name
, const base::Value
* value
) {
69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
70 settings_
.Set(pref_name
, *value
);
73 void AddExpectation(const std::string
& pref_name
, base::Value
* value
) {
74 base::Value
*& entry
= expected_props_
[pref_name
];
79 void PrepareEmptyPolicy(em::PolicyData
* policy
) {
80 // Prepare some policy blob.
81 em::PolicyFetchResponse response
;
82 em::ChromeDeviceSettingsProto pol
;
83 policy
->set_policy_type(policy::dm_protocol::kChromeDevicePolicyType
);
84 policy
->set_username("me@owner");
85 policy
->set_policy_value(pol
.SerializeAsString());
86 // Wipe the signed settings store.
87 response
.set_policy_data(policy
->SerializeAsString());
88 response
.set_policy_data_signature("false");
91 base::MessageLoopForUI message_loop_
;
92 content::TestBrowserThread ui_thread_
;
94 ScopedTestingLocalState local_state_
;
95 ScopedDeviceSettingsTestHelper device_settings_test_helper_
;
96 CrosSettings settings_
;
98 base::WeakPtrFactory
<CrosSettingsTest
> weak_factory_
;
100 std::map
<std::string
, base::Value
*> expected_props_
;
103 TEST_F(CrosSettingsTest
, SetPref
) {
104 // Change to something that is not the default.
105 AddExpectation(kAccountsPrefAllowGuest
,
106 base::Value::CreateBooleanValue(false));
107 SetPref(kAccountsPrefAllowGuest
, expected_props_
[kAccountsPrefAllowGuest
]);
108 FetchPref(kAccountsPrefAllowGuest
);
109 ASSERT_TRUE(expected_props_
.empty());
112 TEST_F(CrosSettingsTest
, GetPref
) {
113 // We didn't change the default so look for it.
114 AddExpectation(kAccountsPrefAllowGuest
,
115 base::Value::CreateBooleanValue(true));
116 FetchPref(kAccountsPrefAllowGuest
);
119 TEST_F(CrosSettingsTest
, SetWhitelist
) {
120 // Setting the whitelist should also switch the value of
121 // kAccountsPrefAllowNewUser to false.
122 base::ListValue whitelist
;
123 whitelist
.Append(new base::StringValue("me@owner"));
124 AddExpectation(kAccountsPrefAllowNewUser
,
125 base::Value::CreateBooleanValue(false));
126 AddExpectation(kAccountsPrefUsers
, whitelist
.DeepCopy());
127 SetPref(kAccountsPrefUsers
, &whitelist
);
128 FetchPref(kAccountsPrefAllowNewUser
);
129 FetchPref(kAccountsPrefUsers
);
132 TEST_F(CrosSettingsTest
, SetWhitelistWithListOps
) {
133 base::ListValue
* whitelist
= new base::ListValue();
134 base::StringValue
hacky_user("h@xxor");
135 whitelist
->Append(hacky_user
.DeepCopy());
136 AddExpectation(kAccountsPrefAllowNewUser
,
137 base::Value::CreateBooleanValue(false));
138 AddExpectation(kAccountsPrefUsers
, whitelist
);
139 // Add some user to the whitelist.
140 settings_
.AppendToList(kAccountsPrefUsers
, &hacky_user
);
141 FetchPref(kAccountsPrefAllowNewUser
);
142 FetchPref(kAccountsPrefUsers
);
145 TEST_F(CrosSettingsTest
, SetWhitelistWithListOps2
) {
146 base::ListValue whitelist
;
147 base::StringValue
hacky_user("h@xxor");
148 base::StringValue
lamy_user("l@mer");
149 whitelist
.Append(hacky_user
.DeepCopy());
150 base::ListValue
* expected_list
= whitelist
.DeepCopy();
151 whitelist
.Append(lamy_user
.DeepCopy());
152 AddExpectation(kAccountsPrefAllowNewUser
,
153 base::Value::CreateBooleanValue(false));
154 AddExpectation(kAccountsPrefUsers
, whitelist
.DeepCopy());
155 SetPref(kAccountsPrefUsers
, &whitelist
);
156 FetchPref(kAccountsPrefAllowNewUser
);
157 FetchPref(kAccountsPrefUsers
);
158 ASSERT_TRUE(expected_props_
.empty());
159 // Now try to remove one element from that list.
160 AddExpectation(kAccountsPrefUsers
, expected_list
);
161 settings_
.RemoveFromList(kAccountsPrefUsers
, &lamy_user
);
162 FetchPref(kAccountsPrefAllowNewUser
);
163 FetchPref(kAccountsPrefUsers
);
166 TEST_F(CrosSettingsTest
, SetEmptyWhitelist
) {
167 // Setting the whitelist empty should switch the value of
168 // kAccountsPrefAllowNewUser to true.
169 base::ListValue whitelist
;
170 AddExpectation(kAccountsPrefAllowNewUser
,
171 base::Value::CreateBooleanValue(true));
172 SetPref(kAccountsPrefUsers
, &whitelist
);
173 FetchPref(kAccountsPrefAllowNewUser
);
174 FetchPref(kAccountsPrefUsers
);
177 TEST_F(CrosSettingsTest
, SetEmptyWhitelistAndNoNewUsers
) {
178 // Setting the whitelist empty and disallowing new users should result in no
179 // new users allowed.
180 base::ListValue whitelist
;
181 base::FundamentalValue
disallow_new(false);
182 AddExpectation(kAccountsPrefUsers
, whitelist
.DeepCopy());
183 AddExpectation(kAccountsPrefAllowNewUser
,
184 base::Value::CreateBooleanValue(false));
185 SetPref(kAccountsPrefUsers
, &whitelist
);
186 SetPref(kAccountsPrefAllowNewUser
, &disallow_new
);
187 FetchPref(kAccountsPrefAllowNewUser
);
188 FetchPref(kAccountsPrefUsers
);
191 TEST_F(CrosSettingsTest
, SetWhitelistAndNoNewUsers
) {
192 // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to
193 // false (which is the implicit value too).
194 base::ListValue whitelist
;
195 whitelist
.Append(new base::StringValue("me@owner"));
196 AddExpectation(kAccountsPrefUsers
, whitelist
.DeepCopy());
197 AddExpectation(kAccountsPrefAllowNewUser
,
198 base::Value::CreateBooleanValue(false));
199 SetPref(kAccountsPrefUsers
, &whitelist
);
200 SetPref(kAccountsPrefAllowNewUser
,
201 expected_props_
[kAccountsPrefAllowNewUser
]);
202 FetchPref(kAccountsPrefAllowNewUser
);
203 FetchPref(kAccountsPrefUsers
);
206 TEST_F(CrosSettingsTest
, SetAllowNewUsers
) {
207 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok.
208 AddExpectation(kAccountsPrefAllowNewUser
,
209 base::Value::CreateBooleanValue(true));
210 SetPref(kAccountsPrefAllowNewUser
,
211 expected_props_
[kAccountsPrefAllowNewUser
]);
212 FetchPref(kAccountsPrefAllowNewUser
);
215 TEST_F(CrosSettingsTest
, SetEphemeralUsersEnabled
) {
216 base::FundamentalValue
ephemeral_users_enabled(true);
217 AddExpectation(kAccountsPrefEphemeralUsersEnabled
,
218 base::Value::CreateBooleanValue(true));
219 SetPref(kAccountsPrefEphemeralUsersEnabled
, &ephemeral_users_enabled
);
220 FetchPref(kAccountsPrefEphemeralUsersEnabled
);
223 TEST_F(CrosSettingsTest
, FindEmailInList
) {
224 base::ListValue list
;
225 list
.Append(new base::StringValue("user@example.com"));
226 list
.Append(new base::StringValue("nodomain"));
227 list
.Append(new base::StringValue("with.dots@gmail.com"));
228 list
.Append(new base::StringValue("Upper@example.com"));
230 CrosSettings
* cs
= &settings_
;
231 cs
->Set(kAccountsPrefUsers
, list
);
233 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "user@example.com"));
234 EXPECT_FALSE(cs
->FindEmailInList(kAccountsPrefUsers
, "us.er@example.com"));
235 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "USER@example.com"));
236 EXPECT_FALSE(cs
->FindEmailInList(kAccountsPrefUsers
, "user"));
238 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "nodomain"));
239 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "nodomain@gmail.com"));
240 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "no.domain@gmail.com"));
241 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "NO.DOMAIN"));
243 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "with.dots@gmail.com"));
244 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "withdots@gmail.com"));
245 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "WITH.DOTS@gmail.com"));
246 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "WITHDOTS"));
248 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "Upper@example.com"));
249 EXPECT_FALSE(cs
->FindEmailInList(kAccountsPrefUsers
, "U.pper@example.com"));
250 EXPECT_FALSE(cs
->FindEmailInList(kAccountsPrefUsers
, "Upper"));
251 EXPECT_TRUE(cs
->FindEmailInList(kAccountsPrefUsers
, "upper@example.com"));
254 } // namespace chromeos