| Darin Petkov | 3003059 | 2010-07-27 13:53:20 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS 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 <inttypes.h> | 
 | 6 |  | 
 | 7 | #include <string> | 
 | 8 |  | 
 | 9 | #include "base/basictypes.h" | 
 | 10 | #include "base/file_util.h" | 
 | 11 | #include "base/string_util.h" | 
 | 12 | #include "gtest/gtest.h" | 
 | 13 | #include "update_engine/prefs.h" | 
 | 14 |  | 
 | 15 | using std::string; | 
 | 16 |  | 
 | 17 | namespace chromeos_update_engine { | 
 | 18 |  | 
 | 19 | class PrefsTest : public ::testing::Test { | 
 | 20 |  protected: | 
 | 21 |   virtual void SetUp() { | 
 | 22 |     ASSERT_TRUE(file_util::CreateNewTempDirectory("auprefs", &prefs_dir_)); | 
 | 23 |     ASSERT_TRUE(prefs_.Init(prefs_dir_)); | 
 | 24 |   } | 
 | 25 |  | 
 | 26 |   virtual void TearDown() { | 
 | 27 |     file_util::Delete(prefs_dir_, true);  // recursive | 
 | 28 |   } | 
 | 29 |  | 
 | 30 |   bool SetValue(const string& key, const string& value) { | 
 | 31 |     return file_util::WriteFile(prefs_dir_.Append(key), | 
 | 32 |                                 value.data(), value.length()) == | 
 | 33 |         static_cast<int>(value.length()); | 
 | 34 |   } | 
 | 35 |  | 
 | 36 |   FilePath prefs_dir_; | 
 | 37 |   Prefs prefs_; | 
 | 38 | }; | 
 | 39 |  | 
 | 40 | TEST_F(PrefsTest, GetFileNameForKey) { | 
 | 41 |   const char kKey[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-"; | 
 | 42 |   FilePath path; | 
 | 43 |   EXPECT_TRUE(prefs_.GetFileNameForKey(kKey, &path)); | 
 | 44 |   EXPECT_EQ(prefs_dir_.Append(kKey).value(), path.value()); | 
 | 45 | } | 
 | 46 |  | 
 | 47 | TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) { | 
 | 48 |   FilePath path; | 
 | 49 |   EXPECT_FALSE(prefs_.GetFileNameForKey("ABC abc", &path)); | 
 | 50 | } | 
 | 51 |  | 
 | 52 | TEST_F(PrefsTest, GetFileNameForKeyEmpty) { | 
 | 53 |   FilePath path; | 
 | 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; | 
 | 81 |   EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value)); | 
 | 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")); | 
 | 88 |   EXPECT_FALSE(file_util::PathExists(prefs_dir_.Append(kKey))); | 
 | 89 | } | 
 | 90 |  | 
 | 91 | TEST_F(PrefsTest, SetStringCreateDir) { | 
 | 92 |   const char kKey[] = "a-test-key"; | 
 | 93 |   const char kValue[] = "test value"; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 94 |   FilePath subdir = prefs_dir_.Append("subdir1").Append("subdir2"); | 
 | 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; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 98 |   EXPECT_TRUE(file_util::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) { | 
 | 103 |   EXPECT_TRUE(prefs_.Init(FilePath("/dev/null"))); | 
 | 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"; | 
 | 110 |   file_util::CreateDirectory(prefs_dir_.Append(kKey)); | 
 | 111 |   EXPECT_FALSE(prefs_.SetString(kKey, "test value")); | 
 | 112 |   EXPECT_TRUE(file_util::DirectoryExists(prefs_dir_.Append(kKey))); | 
 | 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"; | 
 | 132 |   ASSERT_TRUE(SetValue(kKey, StringPrintf("%" PRIi64, kint64max))); | 
 | 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"; | 
 | 140 |   ASSERT_TRUE(SetValue(kKey, StringPrintf("%" PRIi64, kint64min))); | 
 | 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; | 
 | 163 |   EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value)); | 
 | 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)); | 
 | 170 |   EXPECT_FALSE(file_util::PathExists(prefs_dir_.Append(kKey))); | 
 | 171 | } | 
 | 172 |  | 
 | 173 | TEST_F(PrefsTest, SetInt64Max) { | 
 | 174 |   const char kKey[] = "test-max-int"; | 
 | 175 |   EXPECT_TRUE(prefs_.SetInt64(kKey, kint64max)); | 
 | 176 |   string value; | 
 | 177 |   EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value)); | 
 | 178 |   EXPECT_EQ(StringPrintf("%" PRIi64, kint64max), value); | 
 | 179 | } | 
 | 180 |  | 
 | 181 | TEST_F(PrefsTest, SetInt64Min) { | 
 | 182 |   const char kKey[] = "test-min-int"; | 
 | 183 |   EXPECT_TRUE(prefs_.SetInt64(kKey, kint64min)); | 
 | 184 |   string value; | 
 | 185 |   EXPECT_TRUE(file_util::ReadFileToString(prefs_dir_.Append(kKey), &value)); | 
 | 186 |   EXPECT_EQ(StringPrintf("%" PRIi64, kint64min), value); | 
 | 187 | } | 
 | 188 |  | 
 | 189 | }  // namespace chromeos_update_engine |