EME test page application.
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / cros_settings_unittest.cc
blob060f732b1a5691ce1375b144d80de2567afbbc54
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.
5 #include <map>
6 #include <string>
8 #include "base/bind.h"
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;
28 namespace chromeos {
30 class CrosSettingsTest : public testing::Test {
31 protected:
32 CrosSettingsTest()
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())
49 return;
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));
61 } else {
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];
75 delete entry;
76 entry = value;
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 static bool IsWhitelisted(CrosSettings* cs, const std::string& username) {
92 return cs->FindEmailInList(kAccountsPrefUsers, username, NULL);
95 base::MessageLoopForUI message_loop_;
96 content::TestBrowserThread ui_thread_;
98 ScopedTestingLocalState local_state_;
99 ScopedDeviceSettingsTestHelper device_settings_test_helper_;
100 CrosSettings settings_;
102 base::WeakPtrFactory<CrosSettingsTest> weak_factory_;
104 std::map<std::string, base::Value*> expected_props_;
107 TEST_F(CrosSettingsTest, SetPref) {
108 // Change to something that is not the default.
109 AddExpectation(kAccountsPrefAllowGuest,
110 base::Value::CreateBooleanValue(false));
111 SetPref(kAccountsPrefAllowGuest, expected_props_[kAccountsPrefAllowGuest]);
112 FetchPref(kAccountsPrefAllowGuest);
113 ASSERT_TRUE(expected_props_.empty());
116 TEST_F(CrosSettingsTest, GetPref) {
117 // We didn't change the default so look for it.
118 AddExpectation(kAccountsPrefAllowGuest,
119 base::Value::CreateBooleanValue(true));
120 FetchPref(kAccountsPrefAllowGuest);
123 TEST_F(CrosSettingsTest, SetWhitelist) {
124 // Setting the whitelist should also switch the value of
125 // kAccountsPrefAllowNewUser to false.
126 base::ListValue whitelist;
127 whitelist.Append(new base::StringValue("me@owner"));
128 AddExpectation(kAccountsPrefAllowNewUser,
129 base::Value::CreateBooleanValue(false));
130 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
131 SetPref(kAccountsPrefUsers, &whitelist);
132 FetchPref(kAccountsPrefAllowNewUser);
133 FetchPref(kAccountsPrefUsers);
136 TEST_F(CrosSettingsTest, SetWhitelistWithListOps) {
137 base::ListValue* whitelist = new base::ListValue();
138 base::StringValue hacky_user("h@xxor");
139 whitelist->Append(hacky_user.DeepCopy());
140 AddExpectation(kAccountsPrefAllowNewUser,
141 base::Value::CreateBooleanValue(false));
142 AddExpectation(kAccountsPrefUsers, whitelist);
143 // Add some user to the whitelist.
144 settings_.AppendToList(kAccountsPrefUsers, &hacky_user);
145 FetchPref(kAccountsPrefAllowNewUser);
146 FetchPref(kAccountsPrefUsers);
149 TEST_F(CrosSettingsTest, SetWhitelistWithListOps2) {
150 base::ListValue whitelist;
151 base::StringValue hacky_user("h@xxor");
152 base::StringValue lamy_user("l@mer");
153 whitelist.Append(hacky_user.DeepCopy());
154 base::ListValue* expected_list = whitelist.DeepCopy();
155 whitelist.Append(lamy_user.DeepCopy());
156 AddExpectation(kAccountsPrefAllowNewUser,
157 base::Value::CreateBooleanValue(false));
158 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
159 SetPref(kAccountsPrefUsers, &whitelist);
160 FetchPref(kAccountsPrefAllowNewUser);
161 FetchPref(kAccountsPrefUsers);
162 ASSERT_TRUE(expected_props_.empty());
163 // Now try to remove one element from that list.
164 AddExpectation(kAccountsPrefUsers, expected_list);
165 settings_.RemoveFromList(kAccountsPrefUsers, &lamy_user);
166 FetchPref(kAccountsPrefAllowNewUser);
167 FetchPref(kAccountsPrefUsers);
170 TEST_F(CrosSettingsTest, SetEmptyWhitelist) {
171 // Setting the whitelist empty should switch the value of
172 // kAccountsPrefAllowNewUser to true.
173 base::ListValue whitelist;
174 AddExpectation(kAccountsPrefAllowNewUser,
175 base::Value::CreateBooleanValue(true));
176 SetPref(kAccountsPrefUsers, &whitelist);
177 FetchPref(kAccountsPrefAllowNewUser);
178 FetchPref(kAccountsPrefUsers);
181 TEST_F(CrosSettingsTest, SetEmptyWhitelistAndNoNewUsers) {
182 // Setting the whitelist empty and disallowing new users should result in no
183 // new users allowed.
184 base::ListValue whitelist;
185 base::FundamentalValue disallow_new(false);
186 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
187 AddExpectation(kAccountsPrefAllowNewUser,
188 base::Value::CreateBooleanValue(false));
189 SetPref(kAccountsPrefUsers, &whitelist);
190 SetPref(kAccountsPrefAllowNewUser, &disallow_new);
191 FetchPref(kAccountsPrefAllowNewUser);
192 FetchPref(kAccountsPrefUsers);
195 TEST_F(CrosSettingsTest, SetWhitelistAndNoNewUsers) {
196 // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to
197 // false (which is the implicit value too).
198 base::ListValue whitelist;
199 whitelist.Append(new base::StringValue("me@owner"));
200 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
201 AddExpectation(kAccountsPrefAllowNewUser,
202 base::Value::CreateBooleanValue(false));
203 SetPref(kAccountsPrefUsers, &whitelist);
204 SetPref(kAccountsPrefAllowNewUser,
205 expected_props_[kAccountsPrefAllowNewUser]);
206 FetchPref(kAccountsPrefAllowNewUser);
207 FetchPref(kAccountsPrefUsers);
210 TEST_F(CrosSettingsTest, SetAllowNewUsers) {
211 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok.
212 AddExpectation(kAccountsPrefAllowNewUser,
213 base::Value::CreateBooleanValue(true));
214 SetPref(kAccountsPrefAllowNewUser,
215 expected_props_[kAccountsPrefAllowNewUser]);
216 FetchPref(kAccountsPrefAllowNewUser);
219 TEST_F(CrosSettingsTest, SetEphemeralUsersEnabled) {
220 base::FundamentalValue ephemeral_users_enabled(true);
221 AddExpectation(kAccountsPrefEphemeralUsersEnabled,
222 base::Value::CreateBooleanValue(true));
223 SetPref(kAccountsPrefEphemeralUsersEnabled, &ephemeral_users_enabled);
224 FetchPref(kAccountsPrefEphemeralUsersEnabled);
227 TEST_F(CrosSettingsTest, FindEmailInList) {
228 base::ListValue list;
229 list.Append(new base::StringValue("user@example.com"));
230 list.Append(new base::StringValue("nodomain"));
231 list.Append(new base::StringValue("with.dots@gmail.com"));
232 list.Append(new base::StringValue("Upper@example.com"));
234 CrosSettings* cs = &settings_;
235 cs->Set(kAccountsPrefUsers, list);
237 EXPECT_TRUE(IsWhitelisted(cs, "user@example.com"));
238 EXPECT_FALSE(IsWhitelisted(cs, "us.er@example.com"));
239 EXPECT_TRUE(IsWhitelisted(cs, "USER@example.com"));
240 EXPECT_FALSE(IsWhitelisted(cs, "user"));
242 EXPECT_TRUE(IsWhitelisted(cs, "nodomain"));
243 EXPECT_TRUE(IsWhitelisted(cs, "nodomain@gmail.com"));
244 EXPECT_TRUE(IsWhitelisted(cs, "no.domain@gmail.com"));
245 EXPECT_TRUE(IsWhitelisted(cs, "NO.DOMAIN"));
247 EXPECT_TRUE(IsWhitelisted(cs, "with.dots@gmail.com"));
248 EXPECT_TRUE(IsWhitelisted(cs, "withdots@gmail.com"));
249 EXPECT_TRUE(IsWhitelisted(cs, "WITH.DOTS@gmail.com"));
250 EXPECT_TRUE(IsWhitelisted(cs, "WITHDOTS"));
252 EXPECT_TRUE(IsWhitelisted(cs, "Upper@example.com"));
253 EXPECT_FALSE(IsWhitelisted(cs, "U.pper@example.com"));
254 EXPECT_FALSE(IsWhitelisted(cs, "Upper"));
255 EXPECT_TRUE(IsWhitelisted(cs, "upper@example.com"));
258 TEST_F(CrosSettingsTest, FindEmailInListWildcard) {
259 base::ListValue list;
260 list.Append(new base::StringValue("user@example.com"));
261 list.Append(new base::StringValue("*@example.com"));
263 CrosSettings* cs = &settings_;
264 cs->Set(kAccountsPrefUsers, list);
266 bool wildcard_match = false;
267 EXPECT_TRUE(cs->FindEmailInList(
268 kAccountsPrefUsers, "test@example.com", &wildcard_match));
269 EXPECT_TRUE(wildcard_match);
270 EXPECT_TRUE(cs->FindEmailInList(
271 kAccountsPrefUsers, "user@example.com", &wildcard_match));
272 EXPECT_FALSE(wildcard_match);
273 EXPECT_TRUE(cs->FindEmailInList(
274 kAccountsPrefUsers, "*@example.com", &wildcard_match));
275 EXPECT_TRUE(wildcard_match);
278 } // namespace chromeos