Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 16 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "update_engine/prefs.h" |
| 18 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
| 20 | |
| 21 | #include <string> |
| 22 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 23 | #include <base/files/file_util.h> |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 24 | #include <base/macros.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 25 | #include <base/strings/string_util.h> |
| 26 | #include <base/strings/stringprintf.h> |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 27 | #include <gtest/gtest.h> |
| 28 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 29 | using std::string; |
| 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
| 33 | class PrefsTest : public ::testing::Test { |
| 34 | protected: |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 35 | void SetUp() override { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 36 | ASSERT_TRUE(base::CreateNewTempDirectory("auprefs", &prefs_dir_)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 37 | ASSERT_TRUE(prefs_.Init(prefs_dir_)); |
| 38 | } |
| 39 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 40 | void TearDown() override { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 41 | base::DeleteFile(prefs_dir_, true); // recursive |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool SetValue(const string& key, const string& value) { |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 45 | return base::WriteFile(prefs_dir_.Append(key), value.data(), |
| 46 | value.length()) == static_cast<int>(value.length()); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 49 | base::FilePath prefs_dir_; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 50 | Prefs prefs_; |
| 51 | }; |
| 52 | |
| 53 | TEST_F(PrefsTest, GetFileNameForKey) { |
| 54 | const char kKey[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-"; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 55 | base::FilePath path; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 56 | EXPECT_TRUE(prefs_.GetFileNameForKey(kKey, &path)); |
| 57 | EXPECT_EQ(prefs_dir_.Append(kKey).value(), path.value()); |
| 58 | } |
| 59 | |
| 60 | TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 61 | base::FilePath path; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 62 | EXPECT_FALSE(prefs_.GetFileNameForKey("ABC abc", &path)); |
| 63 | } |
| 64 | |
| 65 | TEST_F(PrefsTest, GetFileNameForKeyEmpty) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 66 | base::FilePath path; |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 67 | EXPECT_FALSE(prefs_.GetFileNameForKey("", &path)); |
| 68 | } |
| 69 | |
| 70 | TEST_F(PrefsTest, GetString) { |
| 71 | const char kKey[] = "test-key"; |
| 72 | const string test_data = "test data"; |
| 73 | ASSERT_TRUE(SetValue(kKey, test_data)); |
| 74 | string value; |
| 75 | EXPECT_TRUE(prefs_.GetString(kKey, &value)); |
| 76 | EXPECT_EQ(test_data, value); |
| 77 | } |
| 78 | |
| 79 | TEST_F(PrefsTest, GetStringBadKey) { |
| 80 | string value; |
| 81 | EXPECT_FALSE(prefs_.GetString(",bad", &value)); |
| 82 | } |
| 83 | |
| 84 | TEST_F(PrefsTest, GetStringNonExistentKey) { |
| 85 | string value; |
| 86 | EXPECT_FALSE(prefs_.GetString("non-existent-key", &value)); |
| 87 | } |
| 88 | |
| 89 | TEST_F(PrefsTest, SetString) { |
| 90 | const char kKey[] = "my_test_key"; |
| 91 | const char kValue[] = "some test value\non 2 lines"; |
| 92 | EXPECT_TRUE(prefs_.SetString(kKey, kValue)); |
| 93 | string value; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 94 | EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 95 | EXPECT_EQ(kValue, value); |
| 96 | } |
| 97 | |
| 98 | TEST_F(PrefsTest, SetStringBadKey) { |
| 99 | const char kKey[] = ".no-dots"; |
| 100 | EXPECT_FALSE(prefs_.SetString(kKey, "some value")); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 101 | EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey))); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | TEST_F(PrefsTest, SetStringCreateDir) { |
| 105 | const char kKey[] = "a-test-key"; |
| 106 | const char kValue[] = "test value"; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 107 | base::FilePath subdir = prefs_dir_.Append("subdir1").Append("subdir2"); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 108 | EXPECT_TRUE(prefs_.Init(subdir)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 109 | EXPECT_TRUE(prefs_.SetString(kKey, kValue)); |
| 110 | string value; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 111 | EXPECT_TRUE(base::ReadFileToString(subdir.Append(kKey), &value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 112 | EXPECT_EQ(kValue, value); |
| 113 | } |
| 114 | |
| 115 | TEST_F(PrefsTest, SetStringDirCreationFailure) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 116 | EXPECT_TRUE(prefs_.Init(base::FilePath("/dev/null"))); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 117 | const char kKey[] = "test-key"; |
| 118 | EXPECT_FALSE(prefs_.SetString(kKey, "test value")); |
| 119 | } |
| 120 | |
| 121 | TEST_F(PrefsTest, SetStringFileCreationFailure) { |
| 122 | const char kKey[] = "a-test-key"; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 123 | base::CreateDirectory(prefs_dir_.Append(kKey)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 124 | EXPECT_FALSE(prefs_.SetString(kKey, "test value")); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 125 | EXPECT_TRUE(base::DirectoryExists(prefs_dir_.Append(kKey))); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(PrefsTest, GetInt64) { |
| 129 | const char kKey[] = "test-key"; |
| 130 | ASSERT_TRUE(SetValue(kKey, " \n 25 \t ")); |
| 131 | int64_t value; |
| 132 | EXPECT_TRUE(prefs_.GetInt64(kKey, &value)); |
| 133 | EXPECT_EQ(25, value); |
| 134 | } |
| 135 | |
| 136 | TEST_F(PrefsTest, GetInt64BadValue) { |
| 137 | const char kKey[] = "test-key"; |
| 138 | ASSERT_TRUE(SetValue(kKey, "30a")); |
| 139 | int64_t value; |
| 140 | EXPECT_FALSE(prefs_.GetInt64(kKey, &value)); |
| 141 | } |
| 142 | |
| 143 | TEST_F(PrefsTest, GetInt64Max) { |
| 144 | const char kKey[] = "test-key"; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 145 | ASSERT_TRUE(SetValue(kKey, base::StringPrintf("%" PRIi64, kint64max))); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 146 | int64_t value; |
| 147 | EXPECT_TRUE(prefs_.GetInt64(kKey, &value)); |
| 148 | EXPECT_EQ(kint64max, value); |
| 149 | } |
| 150 | |
| 151 | TEST_F(PrefsTest, GetInt64Min) { |
| 152 | const char kKey[] = "test-key"; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 153 | ASSERT_TRUE(SetValue(kKey, base::StringPrintf("%" PRIi64, kint64min))); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 154 | int64_t value; |
| 155 | EXPECT_TRUE(prefs_.GetInt64(kKey, &value)); |
| 156 | EXPECT_EQ(kint64min, value); |
| 157 | } |
| 158 | |
| 159 | TEST_F(PrefsTest, GetInt64Negative) { |
| 160 | const char kKey[] = "test-key"; |
| 161 | ASSERT_TRUE(SetValue(kKey, " \t -100 \n ")); |
| 162 | int64_t value; |
| 163 | EXPECT_TRUE(prefs_.GetInt64(kKey, &value)); |
| 164 | EXPECT_EQ(-100, value); |
| 165 | } |
| 166 | |
| 167 | TEST_F(PrefsTest, GetInt64NonExistentKey) { |
| 168 | int64_t value; |
| 169 | EXPECT_FALSE(prefs_.GetInt64("random-key", &value)); |
| 170 | } |
| 171 | |
| 172 | TEST_F(PrefsTest, SetInt64) { |
| 173 | const char kKey[] = "test_int"; |
| 174 | EXPECT_TRUE(prefs_.SetInt64(kKey, -123)); |
| 175 | string value; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 176 | EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value)); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 177 | EXPECT_EQ("-123", value); |
| 178 | } |
| 179 | |
| 180 | TEST_F(PrefsTest, SetInt64BadKey) { |
| 181 | const char kKey[] = "s p a c e s"; |
| 182 | EXPECT_FALSE(prefs_.SetInt64(kKey, 20)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 183 | EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey))); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | TEST_F(PrefsTest, SetInt64Max) { |
| 187 | const char kKey[] = "test-max-int"; |
| 188 | EXPECT_TRUE(prefs_.SetInt64(kKey, kint64max)); |
| 189 | string value; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 190 | EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value)); |
| 191 | EXPECT_EQ(base::StringPrintf("%" PRIi64, kint64max), value); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | TEST_F(PrefsTest, SetInt64Min) { |
| 195 | const char kKey[] = "test-min-int"; |
| 196 | EXPECT_TRUE(prefs_.SetInt64(kKey, kint64min)); |
| 197 | string value; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 198 | EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value)); |
| 199 | EXPECT_EQ(base::StringPrintf("%" PRIi64, kint64min), value); |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 202 | TEST_F(PrefsTest, GetBooleanFalse) { |
| 203 | const char kKey[] = "test-key"; |
| 204 | ASSERT_TRUE(SetValue(kKey, " \n false \t ")); |
| 205 | bool value; |
| 206 | EXPECT_TRUE(prefs_.GetBoolean(kKey, &value)); |
| 207 | EXPECT_FALSE(value); |
| 208 | } |
| 209 | |
| 210 | TEST_F(PrefsTest, GetBooleanTrue) { |
| 211 | const char kKey[] = "test-key"; |
| 212 | ASSERT_TRUE(SetValue(kKey, " \t true \n ")); |
| 213 | bool value; |
| 214 | EXPECT_TRUE(prefs_.GetBoolean(kKey, &value)); |
| 215 | EXPECT_TRUE(value); |
| 216 | } |
| 217 | |
| 218 | TEST_F(PrefsTest, GetBooleanBadValue) { |
| 219 | const char kKey[] = "test-key"; |
| 220 | ASSERT_TRUE(SetValue(kKey, "1")); |
| 221 | bool value; |
| 222 | EXPECT_FALSE(prefs_.GetBoolean(kKey, &value)); |
| 223 | } |
| 224 | |
| 225 | TEST_F(PrefsTest, GetBooleanBadEmptyValue) { |
| 226 | const char kKey[] = "test-key"; |
| 227 | ASSERT_TRUE(SetValue(kKey, "")); |
| 228 | bool value; |
| 229 | EXPECT_FALSE(prefs_.GetBoolean(kKey, &value)); |
| 230 | } |
| 231 | |
| 232 | TEST_F(PrefsTest, GetBooleanNonExistentKey) { |
| 233 | bool value; |
| 234 | EXPECT_FALSE(prefs_.GetBoolean("random-key", &value)); |
| 235 | } |
| 236 | |
| 237 | TEST_F(PrefsTest, SetBooleanTrue) { |
| 238 | const char kKey[] = "test-bool"; |
| 239 | EXPECT_TRUE(prefs_.SetBoolean(kKey, true)); |
| 240 | string value; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 241 | EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value)); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 242 | EXPECT_EQ("true", value); |
| 243 | } |
| 244 | |
| 245 | TEST_F(PrefsTest, SetBooleanFalse) { |
| 246 | const char kKey[] = "test-bool"; |
| 247 | EXPECT_TRUE(prefs_.SetBoolean(kKey, false)); |
| 248 | string value; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 249 | EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value)); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 250 | EXPECT_EQ("false", value); |
| 251 | } |
| 252 | |
| 253 | TEST_F(PrefsTest, SetBooleanBadKey) { |
| 254 | const char kKey[] = "s p a c e s"; |
| 255 | EXPECT_FALSE(prefs_.SetBoolean(kKey, true)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 256 | EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey))); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 259 | TEST_F(PrefsTest, ExistsWorks) { |
| 260 | const char kKey[] = "exists-key"; |
| 261 | |
| 262 | // test that the key doesn't exist before we set it. |
| 263 | EXPECT_FALSE(prefs_.Exists(kKey)); |
| 264 | |
| 265 | // test that the key exists after we set it. |
| 266 | ASSERT_TRUE(prefs_.SetInt64(kKey, 8)); |
| 267 | EXPECT_TRUE(prefs_.Exists(kKey)); |
| 268 | } |
| 269 | |
| 270 | TEST_F(PrefsTest, DeleteWorks) { |
| 271 | const char kKey[] = "delete-key"; |
| 272 | |
| 273 | // test that it's alright to delete a non-existent key. |
| 274 | EXPECT_TRUE(prefs_.Delete(kKey)); |
| 275 | |
| 276 | // delete the key after we set it. |
| 277 | ASSERT_TRUE(prefs_.SetInt64(kKey, 0)); |
| 278 | EXPECT_TRUE(prefs_.Delete(kKey)); |
| 279 | |
| 280 | // make sure it doesn't exist anymore. |
| 281 | EXPECT_FALSE(prefs_.Exists(kKey)); |
| 282 | } |
| 283 | |
Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 284 | } // namespace chromeos_update_engine |