cros_settings_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/policy/proto/chrome_device_policy.pb.h" 15#include "chrome/browser/chromeos/settings/cros_settings.h" 16#include "chrome/browser/chromeos/settings/cros_settings_names.h" 17#include "chrome/browser/chromeos/settings/device_settings_test_helper.h" 18#include "chrome/browser/policy/cloud/cloud_policy_constants.h" 19#include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h" 20#include "chrome/test/base/scoped_testing_local_state.h" 21#include "chrome/test/base/testing_browser_process.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_(TestingBrowserProcess::GetGlobal()), 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(new base::StringValue("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 // An empty whitelist should result in nobody being able to log in. 167 base::ListValue whitelist; 168 base::FundamentalValue disallow_new(false); 169 AddExpectation(kAccountsPrefAllowNewUser, 170 base::Value::CreateBooleanValue(false)); 171 SetPref(kAccountsPrefUsers, &whitelist); 172 SetPref(kAccountsPrefAllowNewUser, &disallow_new); 173 FetchPref(kAccountsPrefAllowNewUser); 174 FetchPref(kAccountsPrefUsers); 175} 176 177TEST_F(CrosSettingsTest, SetWhitelistAndNoNewUsers) { 178 // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to 179 // false (which is the implicit value too). 180 base::ListValue whitelist; 181 whitelist.Append(new base::StringValue("me@owner")); 182 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy()); 183 AddExpectation(kAccountsPrefAllowNewUser, 184 base::Value::CreateBooleanValue(false)); 185 SetPref(kAccountsPrefUsers, &whitelist); 186 SetPref(kAccountsPrefAllowNewUser, 187 expected_props_[kAccountsPrefAllowNewUser]); 188 FetchPref(kAccountsPrefAllowNewUser); 189 FetchPref(kAccountsPrefUsers); 190} 191 192TEST_F(CrosSettingsTest, SetAllowNewUsers) { 193 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok. 194 AddExpectation(kAccountsPrefAllowNewUser, 195 base::Value::CreateBooleanValue(true)); 196 SetPref(kAccountsPrefAllowNewUser, 197 expected_props_[kAccountsPrefAllowNewUser]); 198 FetchPref(kAccountsPrefAllowNewUser); 199} 200 201TEST_F(CrosSettingsTest, SetEphemeralUsersEnabled) { 202 base::FundamentalValue ephemeral_users_enabled(true); 203 AddExpectation(kAccountsPrefEphemeralUsersEnabled, 204 base::Value::CreateBooleanValue(true)); 205 SetPref(kAccountsPrefEphemeralUsersEnabled, &ephemeral_users_enabled); 206 FetchPref(kAccountsPrefEphemeralUsersEnabled); 207} 208 209TEST_F(CrosSettingsTest, FindEmailInList) { 210 base::ListValue list; 211 list.Append(new base::StringValue("user@example.com")); 212 list.Append(new base::StringValue("nodomain")); 213 list.Append(new base::StringValue("with.dots@gmail.com")); 214 list.Append(new base::StringValue("Upper@example.com")); 215 216 CrosSettings* cs = &settings_; 217 cs->Set(kAccountsPrefUsers, list); 218 219 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "user@example.com")); 220 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "us.er@example.com")); 221 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "USER@example.com")); 222 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "user")); 223 224 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain")); 225 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain@gmail.com")); 226 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "no.domain@gmail.com")); 227 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "NO.DOMAIN")); 228 229 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "with.dots@gmail.com")); 230 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "withdots@gmail.com")); 231 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITH.DOTS@gmail.com")); 232 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITHDOTS")); 233 234 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "Upper@example.com")); 235 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "U.pper@example.com")); 236 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "Upper")); 237 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "upper@example.com")); 238} 239 240} // namespace chromeos 241