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 ~CrosSettingsTest() override
{}
40 void TearDown() override
{
41 ASSERT_TRUE(expected_props_
.empty());
42 STLDeleteValues(&expected_props_
);
45 void FetchPref(const std::string
& pref
) {
46 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
47 if (expected_props_
.find(pref
) == expected_props_
.end())
50 if (CrosSettingsProvider::TRUSTED
==
51 settings_
.PrepareTrustedValues(
52 base::Bind(&CrosSettingsTest::FetchPref
,
53 weak_factory_
.GetWeakPtr(), pref
))) {
54 scoped_ptr
<base::Value
> expected_value(
55 expected_props_
.find(pref
)->second
);
56 const base::Value
* pref_value
= settings_
.GetPref(pref
);
57 if (expected_value
.get()) {
58 ASSERT_TRUE(pref_value
);
59 ASSERT_TRUE(expected_value
->Equals(pref_value
));
61 ASSERT_FALSE(pref_value
);
63 expected_props_
.erase(pref
);
67 void SetPref(const std::string
& pref_name
, const base::Value
* value
) {
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
69 settings_
.Set(pref_name
, *value
);
72 void AddExpectation(const std::string
& pref_name
, base::Value
* value
) {
73 base::Value
*& entry
= expected_props_
[pref_name
];
78 void PrepareEmptyPolicy(em::PolicyData
* policy
) {
79 // Prepare some policy blob.
80 em::PolicyFetchResponse response
;
81 em::ChromeDeviceSettingsProto pol
;
82 policy
->set_policy_type(policy::dm_protocol::kChromeDevicePolicyType
);
83 policy
->set_username("me@owner");
84 policy
->set_policy_value(pol
.SerializeAsString());
85 // Wipe the signed settings store.
86 response
.set_policy_data(policy
->SerializeAsString());
87 response
.set_policy_data_signature("false");
90 static bool IsWhitelisted(CrosSettings
* cs
, const std::string
& username
) {
91 return cs
->FindEmailInList(kAccountsPrefUsers
, username
, NULL
);
94 base::MessageLoopForUI message_loop_
;
95 content::TestBrowserThread ui_thread_
;
97 ScopedTestingLocalState local_state_
;
98 ScopedDeviceSettingsTestHelper device_settings_test_helper_
;
99 CrosSettings settings_
;
101 std::map
<std::string
, base::Value
*> expected_props_
;
103 base::WeakPtrFactory
<CrosSettingsTest
> weak_factory_
;
106 TEST_F(CrosSettingsTest
, SetPref
) {
107 // Change to something that is not the default.
108 AddExpectation(kAccountsPrefAllowGuest
, new base::FundamentalValue(false));
109 SetPref(kAccountsPrefAllowGuest
, expected_props_
[kAccountsPrefAllowGuest
]);
110 FetchPref(kAccountsPrefAllowGuest
);
111 ASSERT_TRUE(expected_props_
.empty());
114 TEST_F(CrosSettingsTest
, GetPref
) {
115 // We didn't change the default so look for it.
116 AddExpectation(kAccountsPrefAllowGuest
, new base::FundamentalValue(true));
117 FetchPref(kAccountsPrefAllowGuest
);
120 TEST_F(CrosSettingsTest
, SetWhitelist
) {
121 // Setting the whitelist should also switch the value of
122 // kAccountsPrefAllowNewUser to false.
123 base::ListValue whitelist
;
124 whitelist
.Append(new base::StringValue("me@owner"));
125 AddExpectation(kAccountsPrefAllowNewUser
, new base::FundamentalValue(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
, new base::FundamentalValue(false));
137 AddExpectation(kAccountsPrefUsers
, whitelist
);
138 // Add some user to the whitelist.
139 settings_
.AppendToList(kAccountsPrefUsers
, &hacky_user
);
140 FetchPref(kAccountsPrefAllowNewUser
);
141 FetchPref(kAccountsPrefUsers
);
144 TEST_F(CrosSettingsTest
, SetWhitelistWithListOps2
) {
145 base::ListValue whitelist
;
146 base::StringValue
hacky_user("h@xxor");
147 base::StringValue
lamy_user("l@mer");
148 whitelist
.Append(hacky_user
.DeepCopy());
149 base::ListValue
* expected_list
= whitelist
.DeepCopy();
150 whitelist
.Append(lamy_user
.DeepCopy());
151 AddExpectation(kAccountsPrefAllowNewUser
, new base::FundamentalValue(false));
152 AddExpectation(kAccountsPrefUsers
, whitelist
.DeepCopy());
153 SetPref(kAccountsPrefUsers
, &whitelist
);
154 FetchPref(kAccountsPrefAllowNewUser
);
155 FetchPref(kAccountsPrefUsers
);
156 ASSERT_TRUE(expected_props_
.empty());
157 // Now try to remove one element from that list.
158 AddExpectation(kAccountsPrefUsers
, expected_list
);
159 settings_
.RemoveFromList(kAccountsPrefUsers
, &lamy_user
);
160 FetchPref(kAccountsPrefAllowNewUser
);
161 FetchPref(kAccountsPrefUsers
);
164 TEST_F(CrosSettingsTest
, SetEmptyWhitelist
) {
165 // Setting the whitelist empty should switch the value of
166 // kAccountsPrefAllowNewUser to true.
167 base::ListValue whitelist
;
168 AddExpectation(kAccountsPrefAllowNewUser
, new base::FundamentalValue(true));
169 SetPref(kAccountsPrefUsers
, &whitelist
);
170 FetchPref(kAccountsPrefAllowNewUser
);
171 FetchPref(kAccountsPrefUsers
);
174 TEST_F(CrosSettingsTest
, SetEmptyWhitelistAndNoNewUsers
) {
175 // Setting the whitelist empty and disallowing new users should result in no
176 // new users allowed.
177 base::ListValue whitelist
;
178 base::FundamentalValue
disallow_new(false);
179 AddExpectation(kAccountsPrefUsers
, whitelist
.DeepCopy());
180 AddExpectation(kAccountsPrefAllowNewUser
, new base::FundamentalValue(false));
181 SetPref(kAccountsPrefUsers
, &whitelist
);
182 SetPref(kAccountsPrefAllowNewUser
, &disallow_new
);
183 FetchPref(kAccountsPrefAllowNewUser
);
184 FetchPref(kAccountsPrefUsers
);
187 TEST_F(CrosSettingsTest
, SetWhitelistAndNoNewUsers
) {
188 // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to
189 // false (which is the implicit value too).
190 base::ListValue whitelist
;
191 whitelist
.Append(new base::StringValue("me@owner"));
192 AddExpectation(kAccountsPrefUsers
, whitelist
.DeepCopy());
193 AddExpectation(kAccountsPrefAllowNewUser
, new base::FundamentalValue(false));
194 SetPref(kAccountsPrefUsers
, &whitelist
);
195 SetPref(kAccountsPrefAllowNewUser
,
196 expected_props_
[kAccountsPrefAllowNewUser
]);
197 FetchPref(kAccountsPrefAllowNewUser
);
198 FetchPref(kAccountsPrefUsers
);
201 TEST_F(CrosSettingsTest
, SetAllowNewUsers
) {
202 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok.
203 AddExpectation(kAccountsPrefAllowNewUser
, new base::FundamentalValue(true));
204 SetPref(kAccountsPrefAllowNewUser
,
205 expected_props_
[kAccountsPrefAllowNewUser
]);
206 FetchPref(kAccountsPrefAllowNewUser
);
209 TEST_F(CrosSettingsTest
, SetEphemeralUsersEnabled
) {
210 base::FundamentalValue
ephemeral_users_enabled(true);
211 AddExpectation(kAccountsPrefEphemeralUsersEnabled
,
212 new base::FundamentalValue(true));
213 SetPref(kAccountsPrefEphemeralUsersEnabled
, &ephemeral_users_enabled
);
214 FetchPref(kAccountsPrefEphemeralUsersEnabled
);
217 TEST_F(CrosSettingsTest
, FindEmailInList
) {
218 base::ListValue list
;
219 list
.Append(new base::StringValue("user@example.com"));
220 list
.Append(new base::StringValue("nodomain"));
221 list
.Append(new base::StringValue("with.dots@gmail.com"));
222 list
.Append(new base::StringValue("Upper@example.com"));
224 CrosSettings
* cs
= &settings_
;
225 cs
->Set(kAccountsPrefUsers
, list
);
227 EXPECT_TRUE(IsWhitelisted(cs
, "user@example.com"));
228 EXPECT_FALSE(IsWhitelisted(cs
, "us.er@example.com"));
229 EXPECT_TRUE(IsWhitelisted(cs
, "USER@example.com"));
230 EXPECT_FALSE(IsWhitelisted(cs
, "user"));
232 EXPECT_TRUE(IsWhitelisted(cs
, "nodomain"));
233 EXPECT_TRUE(IsWhitelisted(cs
, "nodomain@gmail.com"));
234 EXPECT_TRUE(IsWhitelisted(cs
, "no.domain@gmail.com"));
235 EXPECT_TRUE(IsWhitelisted(cs
, "NO.DOMAIN"));
237 EXPECT_TRUE(IsWhitelisted(cs
, "with.dots@gmail.com"));
238 EXPECT_TRUE(IsWhitelisted(cs
, "withdots@gmail.com"));
239 EXPECT_TRUE(IsWhitelisted(cs
, "WITH.DOTS@gmail.com"));
240 EXPECT_TRUE(IsWhitelisted(cs
, "WITHDOTS"));
242 EXPECT_TRUE(IsWhitelisted(cs
, "Upper@example.com"));
243 EXPECT_FALSE(IsWhitelisted(cs
, "U.pper@example.com"));
244 EXPECT_FALSE(IsWhitelisted(cs
, "Upper"));
245 EXPECT_TRUE(IsWhitelisted(cs
, "upper@example.com"));
248 TEST_F(CrosSettingsTest
, FindEmailInListWildcard
) {
249 base::ListValue list
;
250 list
.Append(new base::StringValue("user@example.com"));
251 list
.Append(new base::StringValue("*@example.com"));
253 CrosSettings
* cs
= &settings_
;
254 cs
->Set(kAccountsPrefUsers
, list
);
256 bool wildcard_match
= false;
257 EXPECT_TRUE(cs
->FindEmailInList(
258 kAccountsPrefUsers
, "test@example.com", &wildcard_match
));
259 EXPECT_TRUE(wildcard_match
);
260 EXPECT_TRUE(cs
->FindEmailInList(
261 kAccountsPrefUsers
, "user@example.com", &wildcard_match
));
262 EXPECT_FALSE(wildcard_match
);
263 EXPECT_TRUE(cs
->FindEmailInList(
264 kAccountsPrefUsers
, "*@example.com", &wildcard_match
));
265 EXPECT_TRUE(wildcard_match
);
268 } // namespace chromeos