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