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