cros_settings_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.h"
12#include "base/stl_util.h"
13#include "base/values.h"
14#include "chrome/browser/chromeos/settings/cros_settings.h"
15#include "chrome/browser/chromeos/settings/cros_settings_names.h"
16#include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
17#include "chrome/browser/policy/cloud_policy_constants.h"
18#include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
19#include "chrome/browser/policy/proto/device_management_backend.pb.h"
20#include "chrome/test/base/testing_browser_process.h"
21#include "chrome/test/base/testing_pref_service.h"
22#include "content/public/test/test_browser_thread.h"
23#include "testing/gtest/include/gtest/gtest.h"
24
25namespace em = enterprise_management;
26
27namespace chromeos {
28
29class CrosSettingsTest : public testing::Test {
30 protected:
31  CrosSettingsTest()
32      : message_loop_(MessageLoop::TYPE_UI),
33        ui_thread_(content::BrowserThread::UI, &message_loop_),
34        local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)),
35        weak_factory_(this) {}
36
37  virtual ~CrosSettingsTest() {}
38
39  virtual void TearDown() OVERRIDE {
40    ASSERT_TRUE(expected_props_.empty());
41    STLDeleteValues(&expected_props_);
42    expected_props_.clear();
43  }
44
45  void FetchPref(const std::string& pref) {
46    DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
47    if (expected_props_.find(pref) == expected_props_.end())
48      return;
49
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));
60      } else {
61        ASSERT_FALSE(pref_value);
62      }
63      expected_props_.erase(pref);
64    }
65  }
66
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);
70  }
71
72  void AddExpectation(const std::string& pref_name, base::Value* value) {
73    base::Value*& entry = expected_props_[pref_name];
74    delete entry;
75    entry = value;
76  }
77
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");
88  }
89
90  MessageLoop message_loop_;
91  content::TestBrowserThread ui_thread_;
92
93  ScopedTestingLocalState local_state_;
94  ScopedDeviceSettingsTestHelper device_settings_test_helper_;
95  CrosSettings settings_;
96
97  base::WeakPtrFactory<CrosSettingsTest> weak_factory_;
98
99  std::map<std::string, base::Value*> expected_props_;
100};
101
102TEST_F(CrosSettingsTest, SetPref) {
103  // Change to something that is not the default.
104  AddExpectation(kAccountsPrefAllowGuest,
105                 base::Value::CreateBooleanValue(false));
106  SetPref(kAccountsPrefAllowGuest, expected_props_[kAccountsPrefAllowGuest]);
107  FetchPref(kAccountsPrefAllowGuest);
108  ASSERT_TRUE(expected_props_.empty());
109}
110
111TEST_F(CrosSettingsTest, GetPref) {
112  // We didn't change the default so look for it.
113  AddExpectation(kAccountsPrefAllowGuest,
114                 base::Value::CreateBooleanValue(true));
115  FetchPref(kAccountsPrefAllowGuest);
116}
117
118TEST_F(CrosSettingsTest, SetWhitelist) {
119  // Setting the whitelist should also switch the value of
120  // kAccountsPrefAllowNewUser to false.
121  base::ListValue whitelist;
122  whitelist.Append(base::Value::CreateStringValue("me@owner"));
123  AddExpectation(kAccountsPrefAllowNewUser,
124                 base::Value::CreateBooleanValue(false));
125  AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
126  SetPref(kAccountsPrefUsers, &whitelist);
127  FetchPref(kAccountsPrefAllowNewUser);
128  FetchPref(kAccountsPrefUsers);
129}
130
131TEST_F(CrosSettingsTest, SetWhitelistWithListOps) {
132  base::ListValue* whitelist = new base::ListValue();
133  base::StringValue hacky_user("h@xxor");
134  whitelist->Append(hacky_user.DeepCopy());
135  AddExpectation(kAccountsPrefAllowNewUser,
136                 base::Value::CreateBooleanValue(false));
137  AddExpectation(kAccountsPrefUsers, whitelist);
138  // Add some user to the whitelist.
139  settings_.AppendToList(kAccountsPrefUsers, &hacky_user);
140  FetchPref(kAccountsPrefAllowNewUser);
141  FetchPref(kAccountsPrefUsers);
142}
143
144TEST_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,
152                 base::Value::CreateBooleanValue(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  base::FundamentalValue disallow_new(false);
170  AddExpectation(kAccountsPrefAllowNewUser,
171                 base::Value::CreateBooleanValue(true));
172  SetPref(kAccountsPrefUsers, &whitelist);
173  SetPref(kAccountsPrefAllowNewUser, &disallow_new);
174  FetchPref(kAccountsPrefAllowNewUser);
175  FetchPref(kAccountsPrefUsers);
176}
177
178TEST_F(CrosSettingsTest, SetWhitelistAndNoNewUsers) {
179  // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to
180  // false (which is the implicit value too).
181  base::ListValue whitelist;
182  whitelist.Append(base::Value::CreateStringValue("me@owner"));
183  AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy());
184  AddExpectation(kAccountsPrefAllowNewUser,
185                 base::Value::CreateBooleanValue(false));
186  SetPref(kAccountsPrefUsers, &whitelist);
187  SetPref(kAccountsPrefAllowNewUser,
188          expected_props_[kAccountsPrefAllowNewUser]);
189  FetchPref(kAccountsPrefAllowNewUser);
190  FetchPref(kAccountsPrefUsers);
191}
192
193TEST_F(CrosSettingsTest, SetAllowNewUsers) {
194  // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok.
195  AddExpectation(kAccountsPrefAllowNewUser,
196                 base::Value::CreateBooleanValue(true));
197  SetPref(kAccountsPrefAllowNewUser,
198          expected_props_[kAccountsPrefAllowNewUser]);
199  FetchPref(kAccountsPrefAllowNewUser);
200}
201
202TEST_F(CrosSettingsTest, SetEphemeralUsersEnabled) {
203  base::FundamentalValue ephemeral_users_enabled(true);
204  AddExpectation(kAccountsPrefEphemeralUsersEnabled,
205                 base::Value::CreateBooleanValue(true));
206  SetPref(kAccountsPrefEphemeralUsersEnabled, &ephemeral_users_enabled);
207  FetchPref(kAccountsPrefEphemeralUsersEnabled);
208}
209
210TEST_F(CrosSettingsTest, FindEmailInList) {
211  base::ListValue list;
212  list.Append(base::Value::CreateStringValue("user@example.com"));
213  list.Append(base::Value::CreateStringValue("nodomain"));
214  list.Append(base::Value::CreateStringValue("with.dots@gmail.com"));
215  list.Append(base::Value::CreateStringValue("Upper@example.com"));
216
217  CrosSettings* cs = &settings_;
218  cs->Set(kAccountsPrefUsers, list);
219
220  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "user@example.com"));
221  EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "us.er@example.com"));
222  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "USER@example.com"));
223  EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "user"));
224
225  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain"));
226  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain@gmail.com"));
227  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "no.domain@gmail.com"));
228  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "NO.DOMAIN"));
229
230  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "with.dots@gmail.com"));
231  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "withdots@gmail.com"));
232  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITH.DOTS@gmail.com"));
233  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITHDOTS"));
234
235  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "Upper@example.com"));
236  EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "U.pper@example.com"));
237  EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "Upper"));
238  EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "upper@example.com"));
239}
240
241}  // namespace chromeos
242