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