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/settings/cros_settings.h" 15 #include "chrome/browser/chromeos/settings/cros_settings_names.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/browser/policy/cloud/cloud_policy_constants.h" 19 #include "chrome/browser/policy/proto/chromeos/chrome_device_policy.pb.h" 20 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" 21 #include "chrome/test/base/scoped_testing_local_state.h" 22 #include "chrome/test/base/testing_browser_process.h" 23 #include "content/public/test/test_browser_thread.h" 24 #include "testing/gtest/include/gtest/gtest.h" 25 26 namespace em = enterprise_management; 27 28 namespace chromeos { 29 30 class CrosSettingsTest : public testing::Test { 31 protected: 32 CrosSettingsTest() 33 : message_loop_(base::MessageLoop::TYPE_UI), 34 ui_thread_(content::BrowserThread::UI, &message_loop_), 35 local_state_(TestingBrowserProcess::GetGlobal()), 36 settings_(DeviceSettingsService::Get()), 37 weak_factory_(this) {} 38 39 virtual ~CrosSettingsTest() {} 40 41 virtual void TearDown() OVERRIDE { 42 ASSERT_TRUE(expected_props_.empty()); 43 STLDeleteValues(&expected_props_); 44 expected_props_.clear(); 45 } 46 47 void FetchPref(const std::string& pref) { 48 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 49 if (expected_props_.find(pref) == expected_props_.end()) 50 return; 51 52 if (CrosSettingsProvider::TRUSTED == 53 settings_.PrepareTrustedValues( 54 base::Bind(&CrosSettingsTest::FetchPref, 55 weak_factory_.GetWeakPtr(), pref))) { 56 scoped_ptr<base::Value> expected_value( 57 expected_props_.find(pref)->second); 58 const base::Value* pref_value = settings_.GetPref(pref); 59 if (expected_value.get()) { 60 ASSERT_TRUE(pref_value); 61 ASSERT_TRUE(expected_value->Equals(pref_value)); 62 } else { 63 ASSERT_FALSE(pref_value); 64 } 65 expected_props_.erase(pref); 66 } 67 } 68 69 void SetPref(const std::string& pref_name, const base::Value* value) { 70 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 71 settings_.Set(pref_name, *value); 72 } 73 74 void AddExpectation(const std::string& pref_name, base::Value* value) { 75 base::Value*& entry = expected_props_[pref_name]; 76 delete entry; 77 entry = value; 78 } 79 80 void PrepareEmptyPolicy(em::PolicyData* policy) { 81 // Prepare some policy blob. 82 em::PolicyFetchResponse response; 83 em::ChromeDeviceSettingsProto pol; 84 policy->set_policy_type(policy::dm_protocol::kChromeDevicePolicyType); 85 policy->set_username("me@owner"); 86 policy->set_policy_value(pol.SerializeAsString()); 87 // Wipe the signed settings store. 88 response.set_policy_data(policy->SerializeAsString()); 89 response.set_policy_data_signature("false"); 90 } 91 92 base::MessageLoop message_loop_; 93 content::TestBrowserThread ui_thread_; 94 95 ScopedTestingLocalState local_state_; 96 ScopedDeviceSettingsTestHelper device_settings_test_helper_; 97 CrosSettings settings_; 98 99 base::WeakPtrFactory<CrosSettingsTest> weak_factory_; 100 101 std::map<std::string, base::Value*> expected_props_; 102 }; 103 104 TEST_F(CrosSettingsTest, SetPref) { 105 // Change to something that is not the default. 106 AddExpectation(kAccountsPrefAllowGuest, 107 base::Value::CreateBooleanValue(false)); 108 SetPref(kAccountsPrefAllowGuest, expected_props_[kAccountsPrefAllowGuest]); 109 FetchPref(kAccountsPrefAllowGuest); 110 ASSERT_TRUE(expected_props_.empty()); 111 } 112 113 TEST_F(CrosSettingsTest, GetPref) { 114 // We didn't change the default so look for it. 115 AddExpectation(kAccountsPrefAllowGuest, 116 base::Value::CreateBooleanValue(true)); 117 FetchPref(kAccountsPrefAllowGuest); 118 } 119 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, 126 base::Value::CreateBooleanValue(false)); 127 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy()); 128 SetPref(kAccountsPrefUsers, &whitelist); 129 FetchPref(kAccountsPrefAllowNewUser); 130 FetchPref(kAccountsPrefUsers); 131 } 132 133 TEST_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, 138 base::Value::CreateBooleanValue(false)); 139 AddExpectation(kAccountsPrefUsers, whitelist); 140 // Add some user to the whitelist. 141 settings_.AppendToList(kAccountsPrefUsers, &hacky_user); 142 FetchPref(kAccountsPrefAllowNewUser); 143 FetchPref(kAccountsPrefUsers); 144 } 145 146 TEST_F(CrosSettingsTest, SetWhitelistWithListOps2) { 147 base::ListValue whitelist; 148 base::StringValue hacky_user("h@xxor"); 149 base::StringValue lamy_user("l@mer"); 150 whitelist.Append(hacky_user.DeepCopy()); 151 base::ListValue* expected_list = whitelist.DeepCopy(); 152 whitelist.Append(lamy_user.DeepCopy()); 153 AddExpectation(kAccountsPrefAllowNewUser, 154 base::Value::CreateBooleanValue(false)); 155 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy()); 156 SetPref(kAccountsPrefUsers, &whitelist); 157 FetchPref(kAccountsPrefAllowNewUser); 158 FetchPref(kAccountsPrefUsers); 159 ASSERT_TRUE(expected_props_.empty()); 160 // Now try to remove one element from that list. 161 AddExpectation(kAccountsPrefUsers, expected_list); 162 settings_.RemoveFromList(kAccountsPrefUsers, &lamy_user); 163 FetchPref(kAccountsPrefAllowNewUser); 164 FetchPref(kAccountsPrefUsers); 165 } 166 167 TEST_F(CrosSettingsTest, SetEmptyWhitelist) { 168 // Setting the whitelist empty should switch the value of 169 // kAccountsPrefAllowNewUser to true. 170 base::ListValue whitelist; 171 AddExpectation(kAccountsPrefAllowNewUser, 172 base::Value::CreateBooleanValue(true)); 173 SetPref(kAccountsPrefUsers, &whitelist); 174 FetchPref(kAccountsPrefAllowNewUser); 175 FetchPref(kAccountsPrefUsers); 176 } 177 178 TEST_F(CrosSettingsTest, SetEmptyWhitelistAndNoNewUsers) { 179 // Setting the whitelist empty and disallowing new users should result in no 180 // new users allowed. 181 base::ListValue whitelist; 182 base::FundamentalValue disallow_new(false); 183 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy()); 184 AddExpectation(kAccountsPrefAllowNewUser, 185 base::Value::CreateBooleanValue(false)); 186 SetPref(kAccountsPrefUsers, &whitelist); 187 SetPref(kAccountsPrefAllowNewUser, &disallow_new); 188 FetchPref(kAccountsPrefAllowNewUser); 189 FetchPref(kAccountsPrefUsers); 190 } 191 192 TEST_F(CrosSettingsTest, SetWhitelistAndNoNewUsers) { 193 // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to 194 // false (which is the implicit value too). 195 base::ListValue whitelist; 196 whitelist.Append(new base::StringValue("me@owner")); 197 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy()); 198 AddExpectation(kAccountsPrefAllowNewUser, 199 base::Value::CreateBooleanValue(false)); 200 SetPref(kAccountsPrefUsers, &whitelist); 201 SetPref(kAccountsPrefAllowNewUser, 202 expected_props_[kAccountsPrefAllowNewUser]); 203 FetchPref(kAccountsPrefAllowNewUser); 204 FetchPref(kAccountsPrefUsers); 205 } 206 207 TEST_F(CrosSettingsTest, SetAllowNewUsers) { 208 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok. 209 AddExpectation(kAccountsPrefAllowNewUser, 210 base::Value::CreateBooleanValue(true)); 211 SetPref(kAccountsPrefAllowNewUser, 212 expected_props_[kAccountsPrefAllowNewUser]); 213 FetchPref(kAccountsPrefAllowNewUser); 214 } 215 216 TEST_F(CrosSettingsTest, SetEphemeralUsersEnabled) { 217 base::FundamentalValue ephemeral_users_enabled(true); 218 AddExpectation(kAccountsPrefEphemeralUsersEnabled, 219 base::Value::CreateBooleanValue(true)); 220 SetPref(kAccountsPrefEphemeralUsersEnabled, &ephemeral_users_enabled); 221 FetchPref(kAccountsPrefEphemeralUsersEnabled); 222 } 223 224 TEST_F(CrosSettingsTest, FindEmailInList) { 225 base::ListValue list; 226 list.Append(new base::StringValue("user (at) example.com")); 227 list.Append(new base::StringValue("nodomain")); 228 list.Append(new base::StringValue("with.dots (at) gmail.com")); 229 list.Append(new base::StringValue("Upper (at) example.com")); 230 231 CrosSettings* cs = &settings_; 232 cs->Set(kAccountsPrefUsers, list); 233 234 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "user (at) example.com")); 235 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "us.er (at) example.com")); 236 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "USER (at) example.com")); 237 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "user")); 238 239 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain")); 240 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain (at) gmail.com")); 241 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "no.domain (at) gmail.com")); 242 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "NO.DOMAIN")); 243 244 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "with.dots (at) gmail.com")); 245 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "withdots (at) gmail.com")); 246 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITH.DOTS (at) gmail.com")); 247 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITHDOTS")); 248 249 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "Upper (at) example.com")); 250 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "U.pper (at) example.com")); 251 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "Upper")); 252 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "upper (at) example.com")); 253 } 254 255 } // namespace chromeos 256