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