blob: fd36acc2c0d77df4e1eecb791f6cb7698eb39cf4 [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
Alex Deymoaab50e32014-11-10 19:55:35 -08005#include "update_engine/prefs.h"
6
Darin Petkov30030592010-07-27 13:53:20 -07007#include <inttypes.h>
8
9#include <string>
10
Ben Chan06c76a42014-09-05 08:21:06 -070011#include <base/files/file_util.h>
Ben Chan05735a12014-09-03 07:48:22 -070012#include <base/macros.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070013#include <base/strings/string_util.h>
14#include <base/strings/stringprintf.h>
Ben Chan05735a12014-09-03 07:48:22 -070015#include <gtest/gtest.h>
16
Darin Petkov30030592010-07-27 13:53:20 -070017using std::string;
18
19namespace chromeos_update_engine {
20
21class PrefsTest : public ::testing::Test {
22 protected:
Alex Deymo610277e2014-11-11 21:18:11 -080023 void SetUp() override {
Alex Vakulenko75039d72014-03-25 12:36:28 -070024 ASSERT_TRUE(base::CreateNewTempDirectory("auprefs", &prefs_dir_));
Darin Petkov30030592010-07-27 13:53:20 -070025 ASSERT_TRUE(prefs_.Init(prefs_dir_));
26 }
27
Alex Deymo610277e2014-11-11 21:18:11 -080028 void TearDown() override {
Alex Vakulenko75039d72014-03-25 12:36:28 -070029 base::DeleteFile(prefs_dir_, true); // recursive
Darin Petkov30030592010-07-27 13:53:20 -070030 }
31
32 bool SetValue(const string& key, const string& value) {
Ben Chan736fcb52014-05-21 18:28:22 -070033 return base::WriteFile(prefs_dir_.Append(key), value.data(),
34 value.length()) == static_cast<int>(value.length());
Darin Petkov30030592010-07-27 13:53:20 -070035 }
36
Alex Vakulenko75039d72014-03-25 12:36:28 -070037 base::FilePath prefs_dir_;
Darin Petkov30030592010-07-27 13:53:20 -070038 Prefs prefs_;
39};
40
41TEST_F(PrefsTest, GetFileNameForKey) {
42 const char kKey[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
Alex Vakulenko75039d72014-03-25 12:36:28 -070043 base::FilePath path;
Darin Petkov30030592010-07-27 13:53:20 -070044 EXPECT_TRUE(prefs_.GetFileNameForKey(kKey, &path));
45 EXPECT_EQ(prefs_dir_.Append(kKey).value(), path.value());
46}
47
48TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070049 base::FilePath path;
Darin Petkov30030592010-07-27 13:53:20 -070050 EXPECT_FALSE(prefs_.GetFileNameForKey("ABC abc", &path));
51}
52
53TEST_F(PrefsTest, GetFileNameForKeyEmpty) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070054 base::FilePath path;
Darin Petkov30030592010-07-27 13:53:20 -070055 EXPECT_FALSE(prefs_.GetFileNameForKey("", &path));
56}
57
58TEST_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
67TEST_F(PrefsTest, GetStringBadKey) {
68 string value;
69 EXPECT_FALSE(prefs_.GetString(",bad", &value));
70}
71
72TEST_F(PrefsTest, GetStringNonExistentKey) {
73 string value;
74 EXPECT_FALSE(prefs_.GetString("non-existent-key", &value));
75}
76
77TEST_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;
Alex Vakulenko75039d72014-03-25 12:36:28 -070082 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -070083 EXPECT_EQ(kValue, value);
84}
85
86TEST_F(PrefsTest, SetStringBadKey) {
87 const char kKey[] = ".no-dots";
88 EXPECT_FALSE(prefs_.SetString(kKey, "some value"));
Alex Vakulenko75039d72014-03-25 12:36:28 -070089 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -070090}
91
92TEST_F(PrefsTest, SetStringCreateDir) {
93 const char kKey[] = "a-test-key";
94 const char kValue[] = "test value";
Alex Vakulenko75039d72014-03-25 12:36:28 -070095 base::FilePath subdir = prefs_dir_.Append("subdir1").Append("subdir2");
Darin Petkov1cbd78f2010-07-29 12:38:34 -070096 EXPECT_TRUE(prefs_.Init(subdir));
Darin Petkov30030592010-07-27 13:53:20 -070097 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
98 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -070099 EXPECT_TRUE(base::ReadFileToString(subdir.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700100 EXPECT_EQ(kValue, value);
101}
102
103TEST_F(PrefsTest, SetStringDirCreationFailure) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700104 EXPECT_TRUE(prefs_.Init(base::FilePath("/dev/null")));
Darin Petkov30030592010-07-27 13:53:20 -0700105 const char kKey[] = "test-key";
106 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
107}
108
109TEST_F(PrefsTest, SetStringFileCreationFailure) {
110 const char kKey[] = "a-test-key";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700111 base::CreateDirectory(prefs_dir_.Append(kKey));
Darin Petkov30030592010-07-27 13:53:20 -0700112 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700113 EXPECT_TRUE(base::DirectoryExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700114}
115
116TEST_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
124TEST_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
131TEST_F(PrefsTest, GetInt64Max) {
132 const char kKey[] = "test-key";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700133 ASSERT_TRUE(SetValue(kKey, base::StringPrintf("%" PRIi64, kint64max)));
Darin Petkov30030592010-07-27 13:53:20 -0700134 int64_t value;
135 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
136 EXPECT_EQ(kint64max, value);
137}
138
139TEST_F(PrefsTest, GetInt64Min) {
140 const char kKey[] = "test-key";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700141 ASSERT_TRUE(SetValue(kKey, base::StringPrintf("%" PRIi64, kint64min)));
Darin Petkov30030592010-07-27 13:53:20 -0700142 int64_t value;
143 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
144 EXPECT_EQ(kint64min, value);
145}
146
147TEST_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
155TEST_F(PrefsTest, GetInt64NonExistentKey) {
156 int64_t value;
157 EXPECT_FALSE(prefs_.GetInt64("random-key", &value));
158}
159
160TEST_F(PrefsTest, SetInt64) {
161 const char kKey[] = "test_int";
162 EXPECT_TRUE(prefs_.SetInt64(kKey, -123));
163 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700164 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700165 EXPECT_EQ("-123", value);
166}
167
168TEST_F(PrefsTest, SetInt64BadKey) {
169 const char kKey[] = "s p a c e s";
170 EXPECT_FALSE(prefs_.SetInt64(kKey, 20));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700171 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700172}
173
174TEST_F(PrefsTest, SetInt64Max) {
175 const char kKey[] = "test-max-int";
176 EXPECT_TRUE(prefs_.SetInt64(kKey, kint64max));
177 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700178 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
179 EXPECT_EQ(base::StringPrintf("%" PRIi64, kint64max), value);
Darin Petkov30030592010-07-27 13:53:20 -0700180}
181
182TEST_F(PrefsTest, SetInt64Min) {
183 const char kKey[] = "test-min-int";
184 EXPECT_TRUE(prefs_.SetInt64(kKey, kint64min));
185 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700186 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
187 EXPECT_EQ(base::StringPrintf("%" PRIi64, kint64min), value);
Darin Petkov30030592010-07-27 13:53:20 -0700188}
189
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700190TEST_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
198TEST_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
206TEST_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
213TEST_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
220TEST_F(PrefsTest, GetBooleanNonExistentKey) {
221 bool value;
222 EXPECT_FALSE(prefs_.GetBoolean("random-key", &value));
223}
224
225TEST_F(PrefsTest, SetBooleanTrue) {
226 const char kKey[] = "test-bool";
227 EXPECT_TRUE(prefs_.SetBoolean(kKey, true));
228 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700229 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700230 EXPECT_EQ("true", value);
231}
232
233TEST_F(PrefsTest, SetBooleanFalse) {
234 const char kKey[] = "test-bool";
235 EXPECT_TRUE(prefs_.SetBoolean(kKey, false));
236 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700237 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700238 EXPECT_EQ("false", value);
239}
240
241TEST_F(PrefsTest, SetBooleanBadKey) {
242 const char kKey[] = "s p a c e s";
243 EXPECT_FALSE(prefs_.SetBoolean(kKey, true));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700244 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700245}
246
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700247TEST_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
258TEST_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 Petkov30030592010-07-27 13:53:20 -0700272} // namespace chromeos_update_engine