| OLD | NEW |
| (Empty) |
| 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 "chrome/browser/chromeos/login/signed_settings.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/message_loop.h" | |
| 13 #include "base/stl_util.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 16 #include "chrome/browser/chromeos/cros_settings.h" | |
| 17 #include "chrome/browser/chromeos/cros_settings_names.h" | |
| 18 #include "chrome/browser/chromeos/login/mock_user_manager.h" | |
| 19 #include "chrome/browser/chromeos/login/signed_settings_cache.h" | |
| 20 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | |
| 21 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 22 #include "chrome/test/base/testing_browser_process.h" | |
| 23 #include "chrome/test/base/testing_pref_service.h" | |
| 24 #include "content/public/test/test_browser_thread.h" | |
| 25 #include "testing/gtest/include/gtest/gtest.h" | |
| 26 | |
| 27 using ::testing::AnyNumber; | |
| 28 using ::testing::Return; | |
| 29 | |
| 30 namespace em = enterprise_management; | |
| 31 namespace chromeos { | |
| 32 | |
| 33 class CrosSettingsTest : public testing::Test { | |
| 34 protected: | |
| 35 CrosSettingsTest() | |
| 36 : message_loop_(MessageLoop::TYPE_UI), | |
| 37 ui_thread_(content::BrowserThread::UI, &message_loop_), | |
| 38 file_thread_(content::BrowserThread::FILE, &message_loop_), | |
| 39 pointer_factory_(this), | |
| 40 local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) { | |
| 41 } | |
| 42 | |
| 43 virtual ~CrosSettingsTest() { | |
| 44 } | |
| 45 | |
| 46 virtual void SetUp() { | |
| 47 EXPECT_CALL(*mock_user_manager_.user_manager(), IsCurrentUserOwner()) | |
| 48 .Times(AnyNumber()) | |
| 49 .WillRepeatedly(Return(true)); | |
| 50 // Reset the cache between tests. | |
| 51 ApplyEmptyPolicy(); | |
| 52 } | |
| 53 | |
| 54 virtual void TearDown() { | |
| 55 message_loop_.RunAllPending(); | |
| 56 ASSERT_TRUE(expected_props_.empty()); | |
| 57 // Reset the cache between tests. | |
| 58 ApplyEmptyPolicy(); | |
| 59 STLDeleteValues(&expected_props_); | |
| 60 } | |
| 61 | |
| 62 void FetchPref(const std::string& pref) { | |
| 63 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 64 if (expected_props_.find(pref) == expected_props_.end()) | |
| 65 return; | |
| 66 | |
| 67 if (CrosSettingsProvider::TRUSTED == | |
| 68 CrosSettings::Get()->PrepareTrustedValues( | |
| 69 base::Bind(&CrosSettingsTest::FetchPref, | |
| 70 pointer_factory_.GetWeakPtr(), pref))) { | |
| 71 scoped_ptr<base::Value> expected_value( | |
| 72 expected_props_.find(pref)->second); | |
| 73 const base::Value* pref_value = CrosSettings::Get()->GetPref(pref); | |
| 74 if (expected_value.get()) { | |
| 75 ASSERT_TRUE(pref_value); | |
| 76 ASSERT_TRUE(expected_value->Equals(pref_value)); | |
| 77 } else { | |
| 78 ASSERT_FALSE(pref_value); | |
| 79 } | |
| 80 expected_props_.erase(pref); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void SetPref(const std::string& pref_name, const base::Value* value) { | |
| 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 86 CrosSettings::Get()->Set(pref_name, *value); | |
| 87 } | |
| 88 | |
| 89 void AddExpectation(const std::string& pref_name, base::Value* value) { | |
| 90 base::Value*& entry = expected_props_[pref_name]; | |
| 91 delete entry; | |
| 92 entry = value; | |
| 93 } | |
| 94 | |
| 95 void PrepareEmptyPolicy(em::PolicyData* policy) { | |
| 96 // Prepare some policy blob. | |
| 97 em::PolicyFetchResponse response; | |
| 98 em::ChromeDeviceSettingsProto pol; | |
| 99 policy->set_policy_type(chromeos::kDevicePolicyType); | |
| 100 policy->set_username("me@owner"); | |
| 101 policy->set_policy_value(pol.SerializeAsString()); | |
| 102 // Wipe the signed settings store. | |
| 103 response.set_policy_data(policy->SerializeAsString()); | |
| 104 response.set_policy_data_signature("false"); | |
| 105 } | |
| 106 | |
| 107 void ApplyEmptyPolicy() { | |
| 108 em::PolicyData fake_pol; | |
| 109 PrepareEmptyPolicy(&fake_pol); | |
| 110 signed_settings_cache::Store(fake_pol, local_state_.Get()); | |
| 111 CrosSettings::Get()->ReloadProviders(); | |
| 112 } | |
| 113 | |
| 114 std::map<std::string, base::Value*> expected_props_; | |
| 115 | |
| 116 MessageLoop message_loop_; | |
| 117 content::TestBrowserThread ui_thread_; | |
| 118 content::TestBrowserThread file_thread_; | |
| 119 | |
| 120 base::WeakPtrFactory<CrosSettingsTest> pointer_factory_; | |
| 121 | |
| 122 ScopedTestingLocalState local_state_; | |
| 123 | |
| 124 ScopedMockUserManagerEnabler mock_user_manager_; | |
| 125 ScopedStubCrosEnabler stub_cros_enabler_; | |
| 126 }; | |
| 127 | |
| 128 TEST_F(CrosSettingsTest, SetPref) { | |
| 129 // Change to something that is not the default. | |
| 130 AddExpectation(kAccountsPrefAllowGuest, | |
| 131 base::Value::CreateBooleanValue(false)); | |
| 132 SetPref(kAccountsPrefAllowGuest, expected_props_[kAccountsPrefAllowGuest]); | |
| 133 FetchPref(kAccountsPrefAllowGuest); | |
| 134 message_loop_.RunAllPending(); | |
| 135 ASSERT_TRUE(expected_props_.empty()); | |
| 136 } | |
| 137 | |
| 138 TEST_F(CrosSettingsTest, GetPref) { | |
| 139 // We didn't change the default so look for it. | |
| 140 AddExpectation(kAccountsPrefAllowGuest, | |
| 141 base::Value::CreateBooleanValue(true)); | |
| 142 FetchPref(kAccountsPrefAllowGuest); | |
| 143 } | |
| 144 | |
| 145 TEST_F(CrosSettingsTest, SetWhitelist) { | |
| 146 // Setting the whitelist should also switch the value of | |
| 147 // kAccountsPrefAllowNewUser to false. | |
| 148 base::ListValue whitelist; | |
| 149 whitelist.Append(base::Value::CreateStringValue("me@owner")); | |
| 150 AddExpectation(kAccountsPrefAllowNewUser, | |
| 151 base::Value::CreateBooleanValue(false)); | |
| 152 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy()); | |
| 153 SetPref(kAccountsPrefUsers, &whitelist); | |
| 154 FetchPref(kAccountsPrefAllowNewUser); | |
| 155 FetchPref(kAccountsPrefUsers); | |
| 156 } | |
| 157 | |
| 158 TEST_F(CrosSettingsTest, SetWhitelistWithListOps) { | |
| 159 base::ListValue* whitelist = new base::ListValue(); | |
| 160 base::StringValue hacky_user("h@xxor"); | |
| 161 whitelist->Append(hacky_user.DeepCopy()); | |
| 162 AddExpectation(kAccountsPrefAllowNewUser, | |
| 163 base::Value::CreateBooleanValue(false)); | |
| 164 AddExpectation(kAccountsPrefUsers, whitelist); | |
| 165 // Add some user to the whitelist. | |
| 166 CrosSettings::Get()->AppendToList(kAccountsPrefUsers, &hacky_user); | |
| 167 FetchPref(kAccountsPrefAllowNewUser); | |
| 168 FetchPref(kAccountsPrefUsers); | |
| 169 } | |
| 170 | |
| 171 TEST_F(CrosSettingsTest, SetWhitelistWithListOps2) { | |
| 172 base::ListValue whitelist; | |
| 173 base::StringValue hacky_user("h@xxor"); | |
| 174 base::StringValue lamy_user("l@mer"); | |
| 175 whitelist.Append(hacky_user.DeepCopy()); | |
| 176 base::ListValue* expected_list = whitelist.DeepCopy(); | |
| 177 whitelist.Append(lamy_user.DeepCopy()); | |
| 178 AddExpectation(kAccountsPrefAllowNewUser, | |
| 179 base::Value::CreateBooleanValue(false)); | |
| 180 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy()); | |
| 181 SetPref(kAccountsPrefUsers, &whitelist); | |
| 182 FetchPref(kAccountsPrefAllowNewUser); | |
| 183 FetchPref(kAccountsPrefUsers); | |
| 184 message_loop_.RunAllPending(); | |
| 185 ASSERT_TRUE(expected_props_.empty()); | |
| 186 // Now try to remove one element from that list. | |
| 187 AddExpectation(kAccountsPrefUsers, expected_list); | |
| 188 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, &lamy_user); | |
| 189 FetchPref(kAccountsPrefAllowNewUser); | |
| 190 FetchPref(kAccountsPrefUsers); | |
| 191 } | |
| 192 | |
| 193 TEST_F(CrosSettingsTest, SetEmptyWhitelist) { | |
| 194 // Setting the whitelist empty should switch the value of | |
| 195 // kAccountsPrefAllowNewUser to true. | |
| 196 base::ListValue whitelist; | |
| 197 base::FundamentalValue disallow_new(false); | |
| 198 AddExpectation(kAccountsPrefAllowNewUser, | |
| 199 base::Value::CreateBooleanValue(true)); | |
| 200 SetPref(kAccountsPrefUsers, &whitelist); | |
| 201 SetPref(kAccountsPrefAllowNewUser, &disallow_new); | |
| 202 FetchPref(kAccountsPrefAllowNewUser); | |
| 203 FetchPref(kAccountsPrefUsers); | |
| 204 } | |
| 205 | |
| 206 TEST_F(CrosSettingsTest, SetWhitelistAndNoNewUsers) { | |
| 207 // Setting the whitelist should allow us to set kAccountsPrefAllowNewUser to | |
| 208 // false (which is the implicit value too). | |
| 209 base::ListValue whitelist; | |
| 210 whitelist.Append(base::Value::CreateStringValue("me@owner")); | |
| 211 AddExpectation(kAccountsPrefUsers, whitelist.DeepCopy()); | |
| 212 AddExpectation(kAccountsPrefAllowNewUser, | |
| 213 base::Value::CreateBooleanValue(false)); | |
| 214 SetPref(kAccountsPrefUsers, &whitelist); | |
| 215 SetPref(kAccountsPrefAllowNewUser, | |
| 216 expected_props_[kAccountsPrefAllowNewUser]); | |
| 217 FetchPref(kAccountsPrefAllowNewUser); | |
| 218 FetchPref(kAccountsPrefUsers); | |
| 219 } | |
| 220 | |
| 221 TEST_F(CrosSettingsTest, SetAllowNewUsers) { | |
| 222 // Setting kAccountsPrefAllowNewUser to true with no whitelist should be ok. | |
| 223 AddExpectation(kAccountsPrefAllowNewUser, | |
| 224 base::Value::CreateBooleanValue(true)); | |
| 225 SetPref(kAccountsPrefAllowNewUser, | |
| 226 expected_props_[kAccountsPrefAllowNewUser]); | |
| 227 FetchPref(kAccountsPrefAllowNewUser); | |
| 228 } | |
| 229 | |
| 230 TEST_F(CrosSettingsTest, SetOwner) { | |
| 231 base::StringValue hacky_owner("h@xxor"); | |
| 232 AddExpectation(kDeviceOwner, base::Value::CreateStringValue("h@xxor")); | |
| 233 SetPref(kDeviceOwner, &hacky_owner); | |
| 234 FetchPref(kDeviceOwner); | |
| 235 } | |
| 236 | |
| 237 TEST_F(CrosSettingsTest, SetEphemeralUsersEnabled) { | |
| 238 base::FundamentalValue ephemeral_users_enabled(true); | |
| 239 AddExpectation(kAccountsPrefEphemeralUsersEnabled, | |
| 240 base::Value::CreateBooleanValue(true)); | |
| 241 SetPref(kAccountsPrefEphemeralUsersEnabled, &ephemeral_users_enabled); | |
| 242 FetchPref(kAccountsPrefEphemeralUsersEnabled); | |
| 243 } | |
| 244 | |
| 245 TEST_F(CrosSettingsTest, FindEmailInList) { | |
| 246 base::ListValue list; | |
| 247 list.Append(base::Value::CreateStringValue("user@example.com")); | |
| 248 list.Append(base::Value::CreateStringValue("nodomain")); | |
| 249 list.Append(base::Value::CreateStringValue("with.dots@gmail.com")); | |
| 250 list.Append(base::Value::CreateStringValue("Upper@example.com")); | |
| 251 | |
| 252 CrosSettings* cs = CrosSettings::Get(); | |
| 253 cs->Set(kAccountsPrefUsers, list); | |
| 254 | |
| 255 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "user@example.com")); | |
| 256 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "us.er@example.com")); | |
| 257 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "USER@example.com")); | |
| 258 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "user")); | |
| 259 | |
| 260 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain")); | |
| 261 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "nodomain@gmail.com")); | |
| 262 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "no.domain@gmail.com")); | |
| 263 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "NO.DOMAIN")); | |
| 264 | |
| 265 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "with.dots@gmail.com")); | |
| 266 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "withdots@gmail.com")); | |
| 267 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITH.DOTS@gmail.com")); | |
| 268 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "WITHDOTS")); | |
| 269 | |
| 270 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "Upper@example.com")); | |
| 271 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "U.pper@example.com")); | |
| 272 EXPECT_FALSE(cs->FindEmailInList(kAccountsPrefUsers, "Upper")); | |
| 273 EXPECT_TRUE(cs->FindEmailInList(kAccountsPrefUsers, "upper@example.com")); | |
| 274 } | |
| 275 | |
| 276 } // namespace chromeos | |
| OLD | NEW |