blob: fa66bfde92361e3bcc28cbfe3780bb38ab96a00f [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov30030592010-07-27 13:53:20 -07002// 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 Vakulenko75039d72014-03-25 12:36:28 -070011#include <base/strings/string_util.h>
12#include <base/strings/stringprintf.h>
Darin Petkov30030592010-07-27 13:53:20 -070013#include "gtest/gtest.h"
14#include "update_engine/prefs.h"
15
16using std::string;
17
18namespace chromeos_update_engine {
19
20class PrefsTest : public ::testing::Test {
21 protected:
22 virtual void SetUp() {
Alex Vakulenko75039d72014-03-25 12:36:28 -070023 ASSERT_TRUE(base::CreateNewTempDirectory("auprefs", &prefs_dir_));
Darin Petkov30030592010-07-27 13:53:20 -070024 ASSERT_TRUE(prefs_.Init(prefs_dir_));
25 }
26
27 virtual void TearDown() {
Alex Vakulenko75039d72014-03-25 12:36:28 -070028 base::DeleteFile(prefs_dir_, true); // recursive
Darin Petkov30030592010-07-27 13:53:20 -070029 }
30
31 bool SetValue(const string& key, const string& value) {
Ben Chan736fcb52014-05-21 18:28:22 -070032 return base::WriteFile(prefs_dir_.Append(key), value.data(),
33 value.length()) == static_cast<int>(value.length());
Darin Petkov30030592010-07-27 13:53:20 -070034 }
35
Alex Vakulenko75039d72014-03-25 12:36:28 -070036 base::FilePath prefs_dir_;
Darin Petkov30030592010-07-27 13:53:20 -070037 Prefs prefs_;
38};
39
40TEST_F(PrefsTest, GetFileNameForKey) {
41 const char kKey[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
Alex Vakulenko75039d72014-03-25 12:36:28 -070042 base::FilePath path;
Darin Petkov30030592010-07-27 13:53:20 -070043 EXPECT_TRUE(prefs_.GetFileNameForKey(kKey, &path));
44 EXPECT_EQ(prefs_dir_.Append(kKey).value(), path.value());
45}
46
47TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070048 base::FilePath path;
Darin Petkov30030592010-07-27 13:53:20 -070049 EXPECT_FALSE(prefs_.GetFileNameForKey("ABC abc", &path));
50}
51
52TEST_F(PrefsTest, GetFileNameForKeyEmpty) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070053 base::FilePath path;
Darin Petkov30030592010-07-27 13:53:20 -070054 EXPECT_FALSE(prefs_.GetFileNameForKey("", &path));
55}
56
57TEST_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
66TEST_F(PrefsTest, GetStringBadKey) {
67 string value;
68 EXPECT_FALSE(prefs_.GetString(",bad", &value));
69}
70
71TEST_F(PrefsTest, GetStringNonExistentKey) {
72 string value;
73 EXPECT_FALSE(prefs_.GetString("non-existent-key", &value));
74}
75
76TEST_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 Vakulenko75039d72014-03-25 12:36:28 -070081 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -070082 EXPECT_EQ(kValue, value);
83}
84
85TEST_F(PrefsTest, SetStringBadKey) {
86 const char kKey[] = ".no-dots";
87 EXPECT_FALSE(prefs_.SetString(kKey, "some value"));
Alex Vakulenko75039d72014-03-25 12:36:28 -070088 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -070089}
90
91TEST_F(PrefsTest, SetStringCreateDir) {
92 const char kKey[] = "a-test-key";
93 const char kValue[] = "test value";
Alex Vakulenko75039d72014-03-25 12:36:28 -070094 base::FilePath subdir = prefs_dir_.Append("subdir1").Append("subdir2");
Darin Petkov1cbd78f2010-07-29 12:38:34 -070095 EXPECT_TRUE(prefs_.Init(subdir));
Darin Petkov30030592010-07-27 13:53:20 -070096 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
97 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -070098 EXPECT_TRUE(base::ReadFileToString(subdir.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -070099 EXPECT_EQ(kValue, value);
100}
101
102TEST_F(PrefsTest, SetStringDirCreationFailure) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700103 EXPECT_TRUE(prefs_.Init(base::FilePath("/dev/null")));
Darin Petkov30030592010-07-27 13:53:20 -0700104 const char kKey[] = "test-key";
105 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
106}
107
108TEST_F(PrefsTest, SetStringFileCreationFailure) {
109 const char kKey[] = "a-test-key";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700110 base::CreateDirectory(prefs_dir_.Append(kKey));
Darin Petkov30030592010-07-27 13:53:20 -0700111 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700112 EXPECT_TRUE(base::DirectoryExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700113}
114
115TEST_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
123TEST_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
130TEST_F(PrefsTest, GetInt64Max) {
131 const char kKey[] = "test-key";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700132 ASSERT_TRUE(SetValue(kKey, base::StringPrintf("%" PRIi64, kint64max)));
Darin Petkov30030592010-07-27 13:53:20 -0700133 int64_t value;
134 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
135 EXPECT_EQ(kint64max, value);
136}
137
138TEST_F(PrefsTest, GetInt64Min) {
139 const char kKey[] = "test-key";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700140 ASSERT_TRUE(SetValue(kKey, base::StringPrintf("%" PRIi64, kint64min)));
Darin Petkov30030592010-07-27 13:53:20 -0700141 int64_t value;
142 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
143 EXPECT_EQ(kint64min, value);
144}
145
146TEST_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
154TEST_F(PrefsTest, GetInt64NonExistentKey) {
155 int64_t value;
156 EXPECT_FALSE(prefs_.GetInt64("random-key", &value));
157}
158
159TEST_F(PrefsTest, SetInt64) {
160 const char kKey[] = "test_int";
161 EXPECT_TRUE(prefs_.SetInt64(kKey, -123));
162 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700163 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700164 EXPECT_EQ("-123", value);
165}
166
167TEST_F(PrefsTest, SetInt64BadKey) {
168 const char kKey[] = "s p a c e s";
169 EXPECT_FALSE(prefs_.SetInt64(kKey, 20));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700170 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700171}
172
173TEST_F(PrefsTest, SetInt64Max) {
174 const char kKey[] = "test-max-int";
175 EXPECT_TRUE(prefs_.SetInt64(kKey, kint64max));
176 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700177 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
178 EXPECT_EQ(base::StringPrintf("%" PRIi64, kint64max), value);
Darin Petkov30030592010-07-27 13:53:20 -0700179}
180
181TEST_F(PrefsTest, SetInt64Min) {
182 const char kKey[] = "test-min-int";
183 EXPECT_TRUE(prefs_.SetInt64(kKey, kint64min));
184 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700185 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
186 EXPECT_EQ(base::StringPrintf("%" PRIi64, kint64min), value);
Darin Petkov30030592010-07-27 13:53:20 -0700187}
188
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700189TEST_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
197TEST_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
205TEST_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
212TEST_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
219TEST_F(PrefsTest, GetBooleanNonExistentKey) {
220 bool value;
221 EXPECT_FALSE(prefs_.GetBoolean("random-key", &value));
222}
223
224TEST_F(PrefsTest, SetBooleanTrue) {
225 const char kKey[] = "test-bool";
226 EXPECT_TRUE(prefs_.SetBoolean(kKey, true));
227 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700228 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700229 EXPECT_EQ("true", value);
230}
231
232TEST_F(PrefsTest, SetBooleanFalse) {
233 const char kKey[] = "test-bool";
234 EXPECT_TRUE(prefs_.SetBoolean(kKey, false));
235 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700236 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700237 EXPECT_EQ("false", value);
238}
239
240TEST_F(PrefsTest, SetBooleanBadKey) {
241 const char kKey[] = "s p a c e s";
242 EXPECT_FALSE(prefs_.SetBoolean(kKey, true));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700243 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700244}
245
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700246TEST_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
257TEST_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 Petkov30030592010-07-27 13:53:20 -0700271} // namespace chromeos_update_engine