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.
4
5#include <map>
6#include <string>
7
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"
25
26namespace em = enterprise_management;
27
28namespace chromeos {
29
30class 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) {}
37
38  virtual ~CrosSettingsTest() {}
39
40  virtual void TearDown() OVERRIDE {
41    ASSERT_TRUE(expected_props_.empty());
42    STLDeleteValues(&expected_props_);
43    expected_props_.clear();
44  }
45
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;
50
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);
63      }
64      expected_props_.erase(pref);
65    }
66  }
67
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);
71  }
72
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;
77  }
78
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");
89  }
90
91  static bool IsWhitelisted(CrosSettings* cs, const std::string& username) {
92    return cs->FindEmailInList(kAccountsPrefUsers, username, NULL);
93  }
94
95  base::MessageLoopForUI message_loop_;
96  content::TestBrowserThread ui_thread_;
97
98  ScopedTestingLocalState local_state_;
99  ScopedDeviceSettingsTestHelper device_settings_test_helper_;
100  CrosSettings settings_;
101
102  base::WeakPtrFactory<CrosSettingsTest> weak_factory_;
103
104  std::map<std::string, base::Value*> expected_props_;
105};
106
107TEST_F(CrosSettingsTest, SetPref) {
108  // Change to something that is not the default.
109  AddExpectation(kAccountsPrefAllowGuest, new base::FundamentalValue(false));
110  SetPref(kAccountsPrefAllowGuest, expected_props_[kAccountsPrefAllowGuest]);
111  FetchPref(kAccountsPrefAllowGuest);
112  ASSERT_TRUE(expected_props_.empty());
113}
114
115TEST_F(CrosSettingsTest, GetPref) {
116  // We didn't change the default so look for it.
117  AddExpectation(kAccountsPrefAllowGuest, new base::FundamentalValue(true));
118  FetchPref(kAccountsPrefAllowGuest);
119}
120
121TEST_F(CrosSettingsTest, SetWhitelist) {
122  // Setting the whitelist should also switch the value of
123  // kAccountsPrefAllowNewUser to false.
124  base::ListValue whitelist;
125  whitelist.Append(new base::StringValue("me@owner"));
126  AddExpectation(kAccountsPrefAllowNewUser, new base::FundamentalValue(false));
127  AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
128  SetPref(kAccountsPrefUsers, &whitelist);
129  FetchPref(kAccountsPrefAllowNewUser);
130  FetchPref(kAccountsPrefUsers);
131}
132
133TEST_F(CrosSettingsTest, SetWhitelistWithListOps) {
134  base::ListValue* whitelist = new base::ListValue();
135  base::StringValue hacky_user("h@xxor");
136  whitelist->Append(hacky_user.DeepCopy());
137  AddExpectation(kAccountsPrefAllowNewUser, new base::FundamentalValue(false));
138  AddExpectation(kAccountsPrefUsers, whitelist);
139  // Add some user to the whitelist.
140  settings_.AppendToList(kAccountsPrefUsers, &hacky_user);
141  FetchPref(kAccountsPrefAllowNewUser);
142  FetchPref(kAccountsPrefUsers);
143}
144
145TEST_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, new base::FundamentalValue(false));
153  AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
154  SetPref(kAccountsPrefUsers, &whitelist);
155  FetchPref(kAccountsPrefAllowNewUser);
156  FetchPref(kAccountsPrefUsers);
157  ASSERT_TRUE(expected_props_.empty());
158  // Now try to remove one element from that list.
159  AddExpectation(kAccountsPrefUsers, expected_list);
160  settings_.RemoveFromList(kAccountsPrefUsers, &lamy_user);
161  FetchPref(kAccountsPrefAllowNewUser);
162  FetchPref(kAccountsPrefUsers);
163}
164
165TEST_F(CrosSettingsTest, SetEmptyWhitelist) {
166  // Setting the whitelist empty should switch the value of
167  // kAccountsPrefAllowNewUser to true.
168  base::ListValue whitelist;
169  AddExpectation(kAccountsPrefAllowNewUser, new base::FundamentalValue(true));
170  SetPref(kAccountsPrefUsers, &whitelist);
171  FetchPref(kAccountsPrefAllowNewUser);
172  FetchPref(kAccountsPrefUsers);
173}
174
175TEST_F(CrosSettingsTest, SetEmptyWhitelistAndNoNewUsers) {
176  // Setting the whitelist empty and disallowing new users should result in no
177  // new users allowed.
178  base::ListValue whitelist;
179  base::FundamentalValue disallow_new(false);
180  AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
181  AddExpectation(kAccountsPrefAllowNewUser, new base::FundamentalValue(false));
182  SetPref(kAccountsPrefUsers, &whitelist);
183  SetPref(kAccountsPrefAllowNewUser, &disallow_new);
184  FetchPref(kAccountsPrefAllowNewUser);
185  FetchPref(kAccountsPrefUsers);
186}
187
188TEST_F(CrosSettingsTest, SetWhitelistAndNoNewUsers) {
189  // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to
190  // false (which is the implicit value too).
191  base::ListValue whitelist;
192  whitelist.Append(new base::StringValue("me@owner"));
193  AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
194  AddExpectation(kAccountsPrefAllowNewUser, new base::FundamentalValue(false));
195  SetPref(kAccountsPrefUsers, &whitelist);
196  SetPref(kAccountsPrefAllowNewUser,
197          expected_props_[kAccountsPrefAllowNewUser]);
198  FetchPref(kAccountsPrefAllowNewUser);
199  FetchPref(kAccountsPrefUsers);
200}
201
202TEST_F(CrosSettingsTest, SetAllowNewUsers) {
203  // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok.
204  AddExpectation(kAccountsPrefAllowNewUser, new base::FundamentalValue(true));
205  SetPref(kAccountsPrefAllowNewUser,
206          expected_props_[kAccountsPrefAllowNewUser]);
207  FetchPref(kAccountsPrefAllowNewUser);
208}
209
210TEST_F(CrosSettingsTest, SetEphemeralUsersEnabled) {
211  base::FundamentalValue ephemeral_users_enabled(true);
212  AddExpectation(kAccountsPrefEphemeralUsersEnabled,
213                 new base::FundamentalValue(true));
214  SetPref(kAccountsPrefEphemeralUsersEnabled, &ephemeral_users_enabled);
215  FetchPref(kAccountsPrefEphemeralUsersEnabled);
216}
217
218TEST_F(CrosSettingsTest, FindEmailInList) {
219  base::ListValue list;
220  list.Append(new base::StringValue("user@example.com"));
221  list.Append(new base::StringValue("nodomain"));
222  list.Append(new base::StringValue("with.dots@gmail.com"));
223  list.Append(new base::StringValue("Upper@example.com"));
224
225  CrosSettings* cs = &settings_;
226  cs->Set(kAccountsPrefUsers, list);
227
228  EXPECT_TRUE(IsWhitelisted(cs, "user@example.com"));
229  EXPECT_FALSE(IsWhitelisted(cs, "us.er@example.com"));
230  EXPECT_TRUE(IsWhitelisted(cs, "USER@example.com"));
231  EXPECT_FALSE(IsWhitelisted(cs, "user"));
232
233  EXPECT_TRUE(IsWhitelisted(cs, "nodomain"));
234  EXPECT_TRUE(IsWhitelisted(cs, "nodomain@gmail.com"));
235  EXPECT_TRUE(IsWhitelisted(cs, "no.domain@gmail.com"));
236  EXPECT_TRUE(IsWhitelisted(cs, "NO.DOMAIN"));
237
238  EXPECT_TRUE(IsWhitelisted(cs, "with.dots@gmail.com"));
239  EXPECT_TRUE(IsWhitelisted(cs, "withdots@gmail.com"));
240  EXPECT_TRUE(IsWhitelisted(cs, "WITH.DOTS@gmail.com"));
241  EXPECT_TRUE(IsWhitelisted(cs, "WITHDOTS"));
242
243  EXPECT_TRUE(IsWhitelisted(cs, "Upper@example.com"));
244  EXPECT_FALSE(IsWhitelisted(cs, "U.pper@example.com"));
245  EXPECT_FALSE(IsWhitelisted(cs, "Upper"));
246  EXPECT_TRUE(IsWhitelisted(cs, "upper@example.com"));
247}
248
249TEST_F(CrosSettingsTest, FindEmailInListWildcard) {
250  base::ListValue list;
251  list.Append(new base::StringValue("user@example.com"));
252  list.Append(new base::StringValue("*@example.com"));
253
254  CrosSettings* cs = &settings_;
255  cs->Set(kAccountsPrefUsers, list);
256
257  bool wildcard_match = false;
258  EXPECT_TRUE(cs->FindEmailInList(
259      kAccountsPrefUsers, "test@example.com", &wildcard_match));
260  EXPECT_TRUE(wildcard_match);
261  EXPECT_TRUE(cs->FindEmailInList(
262      kAccountsPrefUsers, "user@example.com", &wildcard_match));
263  EXPECT_FALSE(wildcard_match);
264  EXPECT_TRUE(cs->FindEmailInList(
265      kAccountsPrefUsers, "*@example.com", &wildcard_match));
266  EXPECT_TRUE(wildcard_match);
267}
268
269}  // namespace chromeos
270