blob: 0053fd025312823b9211e5e08885716031a0a868 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov30030592010-07-27 13:53:20 -070016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/prefs.h"
18
Darin Petkov30030592010-07-27 13:53:20 -070019#include <inttypes.h>
20
21#include <string>
22
Ben Chan06c76a42014-09-05 08:21:06 -070023#include <base/files/file_util.h>
Ben Chan05735a12014-09-03 07:48:22 -070024#include <base/macros.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070025#include <base/strings/string_util.h>
26#include <base/strings/stringprintf.h>
Ben Chan05735a12014-09-03 07:48:22 -070027#include <gtest/gtest.h>
28
Darin Petkov30030592010-07-27 13:53:20 -070029using std::string;
30
31namespace chromeos_update_engine {
32
33class PrefsTest : public ::testing::Test {
34 protected:
Alex Deymo610277e2014-11-11 21:18:11 -080035 void SetUp() override {
Alex Vakulenko75039d72014-03-25 12:36:28 -070036 ASSERT_TRUE(base::CreateNewTempDirectory("auprefs", &prefs_dir_));
Darin Petkov30030592010-07-27 13:53:20 -070037 ASSERT_TRUE(prefs_.Init(prefs_dir_));
38 }
39
Alex Deymo610277e2014-11-11 21:18:11 -080040 void TearDown() override {
Alex Vakulenko75039d72014-03-25 12:36:28 -070041 base::DeleteFile(prefs_dir_, true); // recursive
Darin Petkov30030592010-07-27 13:53:20 -070042 }
43
44 bool SetValue(const string& key, const string& value) {
Ben Chan736fcb52014-05-21 18:28:22 -070045 return base::WriteFile(prefs_dir_.Append(key), value.data(),
46 value.length()) == static_cast<int>(value.length());
Darin Petkov30030592010-07-27 13:53:20 -070047 }
48
Alex Vakulenko75039d72014-03-25 12:36:28 -070049 base::FilePath prefs_dir_;
Darin Petkov30030592010-07-27 13:53:20 -070050 Prefs prefs_;
51};
52
53TEST_F(PrefsTest, GetFileNameForKey) {
54 const char kKey[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
Alex Vakulenko75039d72014-03-25 12:36:28 -070055 base::FilePath path;
Darin Petkov30030592010-07-27 13:53:20 -070056 EXPECT_TRUE(prefs_.GetFileNameForKey(kKey, &path));
57 EXPECT_EQ(prefs_dir_.Append(kKey).value(), path.value());
58}
59
60TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070061 base::FilePath path;
Darin Petkov30030592010-07-27 13:53:20 -070062 EXPECT_FALSE(prefs_.GetFileNameForKey("ABC abc", &path));
63}
64
65TEST_F(PrefsTest, GetFileNameForKeyEmpty) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070066 base::FilePath path;
Darin Petkov30030592010-07-27 13:53:20 -070067 EXPECT_FALSE(prefs_.GetFileNameForKey("", &path));
68}
69
70TEST_F(PrefsTest, GetString) {
71 const char kKey[] = "test-key";
72 const string test_data = "test data";
73 ASSERT_TRUE(SetValue(kKey, test_data));
74 string value;
75 EXPECT_TRUE(prefs_.GetString(kKey, &value));
76 EXPECT_EQ(test_data, value);
77}
78
79TEST_F(PrefsTest, GetStringBadKey) {
80 string value;
81 EXPECT_FALSE(prefs_.GetString(",bad", &value));
82}
83
84TEST_F(PrefsTest, GetStringNonExistentKey) {
85 string value;
86 EXPECT_FALSE(prefs_.GetString("non-existent-key", &value));
87}
88
89TEST_F(PrefsTest, SetString) {
90 const char kKey[] = "my_test_key";
91 const char kValue[] = "some test value\non 2 lines";
92 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
93 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -070094 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -070095 EXPECT_EQ(kValue, value);
96}
97
98TEST_F(PrefsTest, SetStringBadKey) {
99 const char kKey[] = ".no-dots";
100 EXPECT_FALSE(prefs_.SetString(kKey, "some value"));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700101 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700102}
103
104TEST_F(PrefsTest, SetStringCreateDir) {
105 const char kKey[] = "a-test-key";
106 const char kValue[] = "test value";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700107 base::FilePath subdir = prefs_dir_.Append("subdir1").Append("subdir2");
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700108 EXPECT_TRUE(prefs_.Init(subdir));
Darin Petkov30030592010-07-27 13:53:20 -0700109 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
110 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700111 EXPECT_TRUE(base::ReadFileToString(subdir.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700112 EXPECT_EQ(kValue, value);
113}
114
115TEST_F(PrefsTest, SetStringDirCreationFailure) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700116 EXPECT_TRUE(prefs_.Init(base::FilePath("/dev/null")));
Darin Petkov30030592010-07-27 13:53:20 -0700117 const char kKey[] = "test-key";
118 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
119}
120
121TEST_F(PrefsTest, SetStringFileCreationFailure) {
122 const char kKey[] = "a-test-key";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700123 base::CreateDirectory(prefs_dir_.Append(kKey));
Darin Petkov30030592010-07-27 13:53:20 -0700124 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700125 EXPECT_TRUE(base::DirectoryExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700126}
127
128TEST_F(PrefsTest, GetInt64) {
129 const char kKey[] = "test-key";
130 ASSERT_TRUE(SetValue(kKey, " \n 25 \t "));
131 int64_t value;
132 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
133 EXPECT_EQ(25, value);
134}
135
136TEST_F(PrefsTest, GetInt64BadValue) {
137 const char kKey[] = "test-key";
138 ASSERT_TRUE(SetValue(kKey, "30a"));
139 int64_t value;
140 EXPECT_FALSE(prefs_.GetInt64(kKey, &value));
141}
142
143TEST_F(PrefsTest, GetInt64Max) {
144 const char kKey[] = "test-key";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700145 ASSERT_TRUE(SetValue(kKey, base::StringPrintf("%" PRIi64, kint64max)));
Darin Petkov30030592010-07-27 13:53:20 -0700146 int64_t value;
147 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
148 EXPECT_EQ(kint64max, value);
149}
150
151TEST_F(PrefsTest, GetInt64Min) {
152 const char kKey[] = "test-key";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700153 ASSERT_TRUE(SetValue(kKey, base::StringPrintf("%" PRIi64, kint64min)));
Darin Petkov30030592010-07-27 13:53:20 -0700154 int64_t value;
155 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
156 EXPECT_EQ(kint64min, value);
157}
158
159TEST_F(PrefsTest, GetInt64Negative) {
160 const char kKey[] = "test-key";
161 ASSERT_TRUE(SetValue(kKey, " \t -100 \n "));
162 int64_t value;
163 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
164 EXPECT_EQ(-100, value);
165}
166
167TEST_F(PrefsTest, GetInt64NonExistentKey) {
168 int64_t value;
169 EXPECT_FALSE(prefs_.GetInt64("random-key", &value));
170}
171
172TEST_F(PrefsTest, SetInt64) {
173 const char kKey[] = "test_int";
174 EXPECT_TRUE(prefs_.SetInt64(kKey, -123));
175 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700176 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700177 EXPECT_EQ("-123", value);
178}
179
180TEST_F(PrefsTest, SetInt64BadKey) {
181 const char kKey[] = "s p a c e s";
182 EXPECT_FALSE(prefs_.SetInt64(kKey, 20));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700183 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700184}
185
186TEST_F(PrefsTest, SetInt64Max) {
187 const char kKey[] = "test-max-int";
188 EXPECT_TRUE(prefs_.SetInt64(kKey, kint64max));
189 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700190 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
191 EXPECT_EQ(base::StringPrintf("%" PRIi64, kint64max), value);
Darin Petkov30030592010-07-27 13:53:20 -0700192}
193
194TEST_F(PrefsTest, SetInt64Min) {
195 const char kKey[] = "test-min-int";
196 EXPECT_TRUE(prefs_.SetInt64(kKey, kint64min));
197 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700198 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
199 EXPECT_EQ(base::StringPrintf("%" PRIi64, kint64min), value);
Darin Petkov30030592010-07-27 13:53:20 -0700200}
201
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700202TEST_F(PrefsTest, GetBooleanFalse) {
203 const char kKey[] = "test-key";
204 ASSERT_TRUE(SetValue(kKey, " \n false \t "));
205 bool value;
206 EXPECT_TRUE(prefs_.GetBoolean(kKey, &value));
207 EXPECT_FALSE(value);
208}
209
210TEST_F(PrefsTest, GetBooleanTrue) {
211 const char kKey[] = "test-key";
212 ASSERT_TRUE(SetValue(kKey, " \t true \n "));
213 bool value;
214 EXPECT_TRUE(prefs_.GetBoolean(kKey, &value));
215 EXPECT_TRUE(value);
216}
217
218TEST_F(PrefsTest, GetBooleanBadValue) {
219 const char kKey[] = "test-key";
220 ASSERT_TRUE(SetValue(kKey, "1"));
221 bool value;
222 EXPECT_FALSE(prefs_.GetBoolean(kKey, &value));
223}
224
225TEST_F(PrefsTest, GetBooleanBadEmptyValue) {
226 const char kKey[] = "test-key";
227 ASSERT_TRUE(SetValue(kKey, ""));
228 bool value;
229 EXPECT_FALSE(prefs_.GetBoolean(kKey, &value));
230}
231
232TEST_F(PrefsTest, GetBooleanNonExistentKey) {
233 bool value;
234 EXPECT_FALSE(prefs_.GetBoolean("random-key", &value));
235}
236
237TEST_F(PrefsTest, SetBooleanTrue) {
238 const char kKey[] = "test-bool";
239 EXPECT_TRUE(prefs_.SetBoolean(kKey, true));
240 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700241 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700242 EXPECT_EQ("true", value);
243}
244
245TEST_F(PrefsTest, SetBooleanFalse) {
246 const char kKey[] = "test-bool";
247 EXPECT_TRUE(prefs_.SetBoolean(kKey, false));
248 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700249 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700250 EXPECT_EQ("false", value);
251}
252
253TEST_F(PrefsTest, SetBooleanBadKey) {
254 const char kKey[] = "s p a c e s";
255 EXPECT_FALSE(prefs_.SetBoolean(kKey, true));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700256 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700257}
258
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700259TEST_F(PrefsTest, ExistsWorks) {
260 const char kKey[] = "exists-key";
261
262 // test that the key doesn't exist before we set it.
263 EXPECT_FALSE(prefs_.Exists(kKey));
264
265 // test that the key exists after we set it.
266 ASSERT_TRUE(prefs_.SetInt64(kKey, 8));
267 EXPECT_TRUE(prefs_.Exists(kKey));
268}
269
270TEST_F(PrefsTest, DeleteWorks) {
271 const char kKey[] = "delete-key";
272
273 // test that it's alright to delete a non-existent key.
274 EXPECT_TRUE(prefs_.Delete(kKey));
275
276 // delete the key after we set it.
277 ASSERT_TRUE(prefs_.SetInt64(kKey, 0));
278 EXPECT_TRUE(prefs_.Delete(kKey));
279
280 // make sure it doesn't exist anymore.
281 EXPECT_FALSE(prefs_.Exists(kKey));
282}
283
Darin Petkov30030592010-07-27 13:53:20 -0700284} // namespace chromeos_update_engine