blob: a5f46e539a39ba95ed41706ded7f3c5480dbd97b [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 Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/prefs.h"
Alex Deymoaab50e32014-11-10 19:55:35 -080018
Darin Petkov30030592010-07-27 13:53:20 -070019#include <inttypes.h>
20
Alex Deymoa0284ac2016-07-22 12:51:41 -070021#include <limits>
Darin Petkov30030592010-07-27 13:53:20 -070022#include <string>
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070023#include <vector>
Darin Petkov30030592010-07-27 13:53:20 -070024
Ben Chan06c76a42014-09-05 08:21:06 -070025#include <base/files/file_util.h>
Sen Jiang371615b2016-04-13 15:54:29 -070026#include <base/files/scoped_temp_dir.h>
Ben Chan05735a12014-09-03 07:48:22 -070027#include <base/macros.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070028#include <base/strings/string_util.h>
29#include <base/strings/stringprintf.h>
Alex Deymod6f60072015-10-12 12:22:27 -070030#include <gmock/gmock.h>
Ben Chan05735a12014-09-03 07:48:22 -070031#include <gtest/gtest.h>
32
Darin Petkov30030592010-07-27 13:53:20 -070033using std::string;
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070034using std::vector;
Alex Deymod6f60072015-10-12 12:22:27 -070035using testing::_;
Andrew065d78d2020-04-07 15:43:07 -070036using testing::ElementsAre;
Amin Hassanib2689592019-01-13 17:04:28 -080037using testing::Eq;
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070038using testing::UnorderedElementsAre;
Alex Deymod6f60072015-10-12 12:22:27 -070039
40namespace {
41// Test key used along the tests.
42const char kKey[] = "test-key";
Amin Hassanib2689592019-01-13 17:04:28 -080043} // namespace
Darin Petkov30030592010-07-27 13:53:20 -070044
45namespace chromeos_update_engine {
46
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070047class BasePrefsTest : public ::testing::Test {
48 protected:
49 void MultiNamespaceKeyTest() {
50 ASSERT_TRUE(common_prefs_);
51 auto key0 = common_prefs_->CreateSubKey({"ns1", "key"});
52 // Corner case for "ns1".
53 auto key0corner = common_prefs_->CreateSubKey({"ns11", "key"});
54 auto key1A = common_prefs_->CreateSubKey({"ns1", "nsA", "keyA"});
55 auto key1B = common_prefs_->CreateSubKey({"ns1", "nsA", "keyB"});
56 auto key2 = common_prefs_->CreateSubKey({"ns1", "nsB", "key"});
57 // Corner case for "ns1/nsB".
58 auto key2corner = common_prefs_->CreateSubKey({"ns1", "nsB1", "key"});
59 EXPECT_FALSE(common_prefs_->Exists(key0));
60 EXPECT_FALSE(common_prefs_->Exists(key1A));
61 EXPECT_FALSE(common_prefs_->Exists(key1B));
62 EXPECT_FALSE(common_prefs_->Exists(key2));
63
64 EXPECT_TRUE(common_prefs_->SetString(key0, ""));
65 EXPECT_TRUE(common_prefs_->SetString(key0corner, ""));
66 EXPECT_TRUE(common_prefs_->SetString(key1A, ""));
67 EXPECT_TRUE(common_prefs_->SetString(key1B, ""));
68 EXPECT_TRUE(common_prefs_->SetString(key2, ""));
69 EXPECT_TRUE(common_prefs_->SetString(key2corner, ""));
70
71 EXPECT_TRUE(common_prefs_->Exists(key0));
72 EXPECT_TRUE(common_prefs_->Exists(key0corner));
73 EXPECT_TRUE(common_prefs_->Exists(key1A));
74 EXPECT_TRUE(common_prefs_->Exists(key1B));
75 EXPECT_TRUE(common_prefs_->Exists(key2));
76 EXPECT_TRUE(common_prefs_->Exists(key2corner));
77
78 vector<string> keys2;
79 EXPECT_TRUE(common_prefs_->GetSubKeys("ns1/nsB/", &keys2));
80 EXPECT_THAT(keys2, ElementsAre(key2));
81 for (const auto& key : keys2)
82 EXPECT_TRUE(common_prefs_->Delete(key));
83 EXPECT_TRUE(common_prefs_->Exists(key0));
84 EXPECT_TRUE(common_prefs_->Exists(key0corner));
85 EXPECT_TRUE(common_prefs_->Exists(key1A));
86 EXPECT_TRUE(common_prefs_->Exists(key1B));
87 EXPECT_FALSE(common_prefs_->Exists(key2));
88 EXPECT_TRUE(common_prefs_->Exists(key2corner));
89
90 vector<string> keys2corner;
91 EXPECT_TRUE(common_prefs_->GetSubKeys("ns1/nsB", &keys2corner));
92 EXPECT_THAT(keys2corner, ElementsAre(key2corner));
93 for (const auto& key : keys2corner)
94 EXPECT_TRUE(common_prefs_->Delete(key));
95 EXPECT_FALSE(common_prefs_->Exists(key2corner));
96
97 vector<string> keys1;
98 EXPECT_TRUE(common_prefs_->GetSubKeys("ns1/nsA/", &keys1));
99 EXPECT_THAT(keys1, UnorderedElementsAre(key1A, key1B));
100 for (const auto& key : keys1)
101 EXPECT_TRUE(common_prefs_->Delete(key));
102 EXPECT_TRUE(common_prefs_->Exists(key0));
103 EXPECT_TRUE(common_prefs_->Exists(key0corner));
104 EXPECT_FALSE(common_prefs_->Exists(key1A));
105 EXPECT_FALSE(common_prefs_->Exists(key1B));
106
107 vector<string> keys0;
108 EXPECT_TRUE(common_prefs_->GetSubKeys("ns1/", &keys0));
109 EXPECT_THAT(keys0, ElementsAre(key0));
110 for (const auto& key : keys0)
111 EXPECT_TRUE(common_prefs_->Delete(key));
112 EXPECT_FALSE(common_prefs_->Exists(key0));
113 EXPECT_TRUE(common_prefs_->Exists(key0corner));
114
115 vector<string> keys0corner;
116 EXPECT_TRUE(common_prefs_->GetSubKeys("ns1", &keys0corner));
117 EXPECT_THAT(keys0corner, ElementsAre(key0corner));
118 for (const auto& key : keys0corner)
119 EXPECT_TRUE(common_prefs_->Delete(key));
120 EXPECT_FALSE(common_prefs_->Exists(key0corner));
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800121
122 // Test sub directory namespace.
123 const string kDlcPrefsSubDir = "foo-dir";
124 key1A = common_prefs_->CreateSubKey({kDlcPrefsSubDir, "dlc1", "keyA"});
125 EXPECT_TRUE(common_prefs_->SetString(key1A, "fp_1A"));
126 key1B = common_prefs_->CreateSubKey({kDlcPrefsSubDir, "dlc1", "keyB"});
127 EXPECT_TRUE(common_prefs_->SetString(key1B, "fp_1B"));
128 auto key2A = common_prefs_->CreateSubKey({kDlcPrefsSubDir, "dlc2", "keyA"});
129 EXPECT_TRUE(common_prefs_->SetString(key2A, "fp_A2"));
130
131 vector<string> fpKeys;
132 EXPECT_TRUE(common_prefs_->GetSubKeys(kDlcPrefsSubDir, &fpKeys));
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500133 EXPECT_EQ(fpKeys.size(), 3UL);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800134 EXPECT_TRUE(common_prefs_->Delete(fpKeys[0]));
135 EXPECT_TRUE(common_prefs_->Delete(fpKeys[1]));
136 EXPECT_TRUE(common_prefs_->Delete(fpKeys[2]));
137 EXPECT_FALSE(common_prefs_->Exists(key1A));
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700138 }
139
140 PrefsInterface* common_prefs_;
141};
142
143class PrefsTest : public BasePrefsTest {
Darin Petkov30030592010-07-27 13:53:20 -0700144 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800145 void SetUp() override {
Sen Jiang371615b2016-04-13 15:54:29 -0700146 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
Eric Caruso7feac5b2018-01-23 16:20:19 -0800147 prefs_dir_ = temp_dir_.GetPath();
Darin Petkov30030592010-07-27 13:53:20 -0700148 ASSERT_TRUE(prefs_.Init(prefs_dir_));
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700149 common_prefs_ = &prefs_;
Darin Petkov30030592010-07-27 13:53:20 -0700150 }
151
Darin Petkov30030592010-07-27 13:53:20 -0700152 bool SetValue(const string& key, const string& value) {
Amin Hassanib2689592019-01-13 17:04:28 -0800153 return base::WriteFile(prefs_dir_.Append(key),
154 value.data(),
Ben Chan736fcb52014-05-21 18:28:22 -0700155 value.length()) == static_cast<int>(value.length());
Darin Petkov30030592010-07-27 13:53:20 -0700156 }
157
Sen Jiang371615b2016-04-13 15:54:29 -0700158 base::ScopedTempDir temp_dir_;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700159 base::FilePath prefs_dir_;
Darin Petkov30030592010-07-27 13:53:20 -0700160 Prefs prefs_;
161};
162
Andrew065d78d2020-04-07 15:43:07 -0700163TEST(Prefs, Init) {
164 Prefs prefs;
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700165 const string ns1 = "ns1";
166 const string ns2A = "ns2A";
167 const string ns2B = "ns2B";
Andrew065d78d2020-04-07 15:43:07 -0700168 const string sub_pref = "sp";
169
170 base::ScopedTempDir temp_dir;
171 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700172 auto ns1_path = temp_dir.GetPath().Append(ns1);
173 auto ns2A_path = ns1_path.Append(ns2A);
174 auto ns2B_path = ns1_path.Append(ns2B);
175 auto sub_pref_path = ns2A_path.Append(sub_pref);
Andrew065d78d2020-04-07 15:43:07 -0700176
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700177 EXPECT_TRUE(base::CreateDirectory(ns2B_path));
178 EXPECT_TRUE(base::PathExists(ns2B_path));
179
180 EXPECT_TRUE(base::CreateDirectory(sub_pref_path));
181 EXPECT_TRUE(base::PathExists(sub_pref_path));
182
183 EXPECT_TRUE(base::PathExists(ns1_path));
Andrew065d78d2020-04-07 15:43:07 -0700184 ASSERT_TRUE(prefs.Init(temp_dir.GetPath()));
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700185 EXPECT_FALSE(base::PathExists(ns1_path));
Andrew065d78d2020-04-07 15:43:07 -0700186}
187
Darin Petkov30030592010-07-27 13:53:20 -0700188TEST_F(PrefsTest, GetFileNameForKey) {
Alex Deymod6f60072015-10-12 12:22:27 -0700189 const char kAllvalidCharsKey[] =
190 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700191 base::FilePath path;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700192 EXPECT_TRUE(prefs_.file_storage_.GetFileNameForKey(kAllvalidCharsKey, &path));
Alex Deymod6f60072015-10-12 12:22:27 -0700193 EXPECT_EQ(prefs_dir_.Append(kAllvalidCharsKey).value(), path.value());
Darin Petkov30030592010-07-27 13:53:20 -0700194}
195
196TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700197 base::FilePath path;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700198 EXPECT_FALSE(prefs_.file_storage_.GetFileNameForKey("ABC abc", &path));
Darin Petkov30030592010-07-27 13:53:20 -0700199}
200
201TEST_F(PrefsTest, GetFileNameForKeyEmpty) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700202 base::FilePath path;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700203 EXPECT_FALSE(prefs_.file_storage_.GetFileNameForKey("", &path));
Darin Petkov30030592010-07-27 13:53:20 -0700204}
205
Andrew065d78d2020-04-07 15:43:07 -0700206TEST_F(PrefsTest, CreateSubKey) {
207 const string name_space = "ns";
208 const string sub_pref1 = "sp1";
209 const string sub_pref2 = "sp2";
210 const string sub_key = "sk";
211
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700212 EXPECT_EQ(PrefsInterface::CreateSubKey({name_space, sub_pref1, sub_key}),
Andrew065d78d2020-04-07 15:43:07 -0700213 "ns/sp1/sk");
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700214 EXPECT_EQ(PrefsInterface::CreateSubKey({name_space, sub_pref2, sub_key}),
Andrew065d78d2020-04-07 15:43:07 -0700215 "ns/sp2/sk");
216}
217
Darin Petkov30030592010-07-27 13:53:20 -0700218TEST_F(PrefsTest, GetString) {
Darin Petkov30030592010-07-27 13:53:20 -0700219 const string test_data = "test data";
220 ASSERT_TRUE(SetValue(kKey, test_data));
221 string value;
222 EXPECT_TRUE(prefs_.GetString(kKey, &value));
223 EXPECT_EQ(test_data, value);
224}
225
226TEST_F(PrefsTest, GetStringBadKey) {
227 string value;
228 EXPECT_FALSE(prefs_.GetString(",bad", &value));
229}
230
231TEST_F(PrefsTest, GetStringNonExistentKey) {
232 string value;
233 EXPECT_FALSE(prefs_.GetString("non-existent-key", &value));
234}
235
236TEST_F(PrefsTest, SetString) {
Darin Petkov30030592010-07-27 13:53:20 -0700237 const char kValue[] = "some test value\non 2 lines";
238 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
239 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700240 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700241 EXPECT_EQ(kValue, value);
242}
243
244TEST_F(PrefsTest, SetStringBadKey) {
Alex Deymod6f60072015-10-12 12:22:27 -0700245 const char kKeyWithDots[] = ".no-dots";
246 EXPECT_FALSE(prefs_.SetString(kKeyWithDots, "some value"));
247 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKeyWithDots)));
Darin Petkov30030592010-07-27 13:53:20 -0700248}
249
250TEST_F(PrefsTest, SetStringCreateDir) {
Darin Petkov30030592010-07-27 13:53:20 -0700251 const char kValue[] = "test value";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700252 base::FilePath subdir = prefs_dir_.Append("subdir1").Append("subdir2");
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700253 EXPECT_TRUE(prefs_.Init(subdir));
Darin Petkov30030592010-07-27 13:53:20 -0700254 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
255 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700256 EXPECT_TRUE(base::ReadFileToString(subdir.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700257 EXPECT_EQ(kValue, value);
258}
259
260TEST_F(PrefsTest, SetStringDirCreationFailure) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700261 EXPECT_TRUE(prefs_.Init(base::FilePath("/dev/null")));
Darin Petkov30030592010-07-27 13:53:20 -0700262 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
263}
264
265TEST_F(PrefsTest, SetStringFileCreationFailure) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700266 base::CreateDirectory(prefs_dir_.Append(kKey));
Darin Petkov30030592010-07-27 13:53:20 -0700267 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700268 EXPECT_TRUE(base::DirectoryExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700269}
270
271TEST_F(PrefsTest, GetInt64) {
Darin Petkov30030592010-07-27 13:53:20 -0700272 ASSERT_TRUE(SetValue(kKey, " \n 25 \t "));
273 int64_t value;
274 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
275 EXPECT_EQ(25, value);
276}
277
278TEST_F(PrefsTest, GetInt64BadValue) {
Darin Petkov30030592010-07-27 13:53:20 -0700279 ASSERT_TRUE(SetValue(kKey, "30a"));
280 int64_t value;
281 EXPECT_FALSE(prefs_.GetInt64(kKey, &value));
282}
283
284TEST_F(PrefsTest, GetInt64Max) {
Amin Hassanib2689592019-01-13 17:04:28 -0800285 ASSERT_TRUE(SetValue(
286 kKey,
287 base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::max())));
Darin Petkov30030592010-07-27 13:53:20 -0700288 int64_t value;
289 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800290 EXPECT_EQ(std::numeric_limits<int64_t>::max(), value);
Darin Petkov30030592010-07-27 13:53:20 -0700291}
292
293TEST_F(PrefsTest, GetInt64Min) {
Amin Hassanib2689592019-01-13 17:04:28 -0800294 ASSERT_TRUE(SetValue(
295 kKey,
296 base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::min())));
Darin Petkov30030592010-07-27 13:53:20 -0700297 int64_t value;
298 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800299 EXPECT_EQ(std::numeric_limits<int64_t>::min(), value);
Darin Petkov30030592010-07-27 13:53:20 -0700300}
301
302TEST_F(PrefsTest, GetInt64Negative) {
Darin Petkov30030592010-07-27 13:53:20 -0700303 ASSERT_TRUE(SetValue(kKey, " \t -100 \n "));
304 int64_t value;
305 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
306 EXPECT_EQ(-100, value);
307}
308
309TEST_F(PrefsTest, GetInt64NonExistentKey) {
310 int64_t value;
311 EXPECT_FALSE(prefs_.GetInt64("random-key", &value));
312}
313
314TEST_F(PrefsTest, SetInt64) {
Darin Petkov30030592010-07-27 13:53:20 -0700315 EXPECT_TRUE(prefs_.SetInt64(kKey, -123));
316 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700317 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700318 EXPECT_EQ("-123", value);
319}
320
321TEST_F(PrefsTest, SetInt64BadKey) {
Alex Deymod6f60072015-10-12 12:22:27 -0700322 const char kKeyWithSpaces[] = "s p a c e s";
323 EXPECT_FALSE(prefs_.SetInt64(kKeyWithSpaces, 20));
324 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKeyWithSpaces)));
Darin Petkov30030592010-07-27 13:53:20 -0700325}
326
327TEST_F(PrefsTest, SetInt64Max) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800328 EXPECT_TRUE(prefs_.SetInt64(kKey, std::numeric_limits<int64_t>::max()));
Darin Petkov30030592010-07-27 13:53:20 -0700329 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700330 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Vakulenko0103c362016-01-20 07:56:15 -0800331 EXPECT_EQ(base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::max()),
332 value);
Darin Petkov30030592010-07-27 13:53:20 -0700333}
334
335TEST_F(PrefsTest, SetInt64Min) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800336 EXPECT_TRUE(prefs_.SetInt64(kKey, std::numeric_limits<int64_t>::min()));
Darin Petkov30030592010-07-27 13:53:20 -0700337 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700338 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Vakulenko0103c362016-01-20 07:56:15 -0800339 EXPECT_EQ(base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::min()),
340 value);
Darin Petkov30030592010-07-27 13:53:20 -0700341}
342
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700343TEST_F(PrefsTest, GetBooleanFalse) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700344 ASSERT_TRUE(SetValue(kKey, " \n false \t "));
345 bool value;
346 EXPECT_TRUE(prefs_.GetBoolean(kKey, &value));
347 EXPECT_FALSE(value);
348}
349
350TEST_F(PrefsTest, GetBooleanTrue) {
351 const char kKey[] = "test-key";
352 ASSERT_TRUE(SetValue(kKey, " \t true \n "));
353 bool value;
354 EXPECT_TRUE(prefs_.GetBoolean(kKey, &value));
355 EXPECT_TRUE(value);
356}
357
358TEST_F(PrefsTest, GetBooleanBadValue) {
359 const char kKey[] = "test-key";
360 ASSERT_TRUE(SetValue(kKey, "1"));
361 bool value;
362 EXPECT_FALSE(prefs_.GetBoolean(kKey, &value));
363}
364
365TEST_F(PrefsTest, GetBooleanBadEmptyValue) {
366 const char kKey[] = "test-key";
367 ASSERT_TRUE(SetValue(kKey, ""));
368 bool value;
369 EXPECT_FALSE(prefs_.GetBoolean(kKey, &value));
370}
371
372TEST_F(PrefsTest, GetBooleanNonExistentKey) {
373 bool value;
374 EXPECT_FALSE(prefs_.GetBoolean("random-key", &value));
375}
376
377TEST_F(PrefsTest, SetBooleanTrue) {
378 const char kKey[] = "test-bool";
379 EXPECT_TRUE(prefs_.SetBoolean(kKey, true));
380 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700381 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700382 EXPECT_EQ("true", value);
383}
384
385TEST_F(PrefsTest, SetBooleanFalse) {
386 const char kKey[] = "test-bool";
387 EXPECT_TRUE(prefs_.SetBoolean(kKey, false));
388 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700389 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700390 EXPECT_EQ("false", value);
391}
392
393TEST_F(PrefsTest, SetBooleanBadKey) {
394 const char kKey[] = "s p a c e s";
395 EXPECT_FALSE(prefs_.SetBoolean(kKey, true));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700396 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700397}
398
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700399TEST_F(PrefsTest, ExistsWorks) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700400 // test that the key doesn't exist before we set it.
401 EXPECT_FALSE(prefs_.Exists(kKey));
402
403 // test that the key exists after we set it.
404 ASSERT_TRUE(prefs_.SetInt64(kKey, 8));
405 EXPECT_TRUE(prefs_.Exists(kKey));
406}
407
408TEST_F(PrefsTest, DeleteWorks) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700409 // test that it's alright to delete a non-existent key.
410 EXPECT_TRUE(prefs_.Delete(kKey));
411
412 // delete the key after we set it.
413 ASSERT_TRUE(prefs_.SetInt64(kKey, 0));
414 EXPECT_TRUE(prefs_.Delete(kKey));
415
416 // make sure it doesn't exist anymore.
417 EXPECT_FALSE(prefs_.Exists(kKey));
418}
419
Andrew065d78d2020-04-07 15:43:07 -0700420TEST_F(PrefsTest, SetDeleteSubKey) {
421 const string name_space = "ns";
422 const string sub_pref = "sp";
423 const string sub_key1 = "sk1";
424 const string sub_key2 = "sk2";
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700425 auto key1 = prefs_.CreateSubKey({name_space, sub_pref, sub_key1});
426 auto key2 = prefs_.CreateSubKey({name_space, sub_pref, sub_key2});
Andrew065d78d2020-04-07 15:43:07 -0700427 base::FilePath sub_pref_path = prefs_dir_.Append(name_space).Append(sub_pref);
428
429 ASSERT_TRUE(prefs_.SetInt64(key1, 0));
430 ASSERT_TRUE(prefs_.SetInt64(key2, 0));
431 EXPECT_TRUE(base::PathExists(sub_pref_path.Append(sub_key1)));
432 EXPECT_TRUE(base::PathExists(sub_pref_path.Append(sub_key2)));
433
434 ASSERT_TRUE(prefs_.Delete(key1));
435 EXPECT_FALSE(base::PathExists(sub_pref_path.Append(sub_key1)));
436 EXPECT_TRUE(base::PathExists(sub_pref_path.Append(sub_key2)));
437 ASSERT_TRUE(prefs_.Delete(key2));
438 EXPECT_FALSE(base::PathExists(sub_pref_path.Append(sub_key2)));
439 prefs_.Init(prefs_dir_);
440 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(name_space)));
441}
442
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800443TEST_F(PrefsTest, DeletePrefs) {
444 const string kPrefsSubDir = "foo-dir";
445 const string kFpKey = "kPrefFp";
446 const string kNotFpKey = "NotkPrefFp";
447 const string kOtherKey = "kPrefNotFp";
448
449 EXPECT_TRUE(prefs_.SetString(kFpKey, "3.000"));
450 EXPECT_TRUE(prefs_.SetString(kOtherKey, "not_fp_val"));
451
452 auto key1_fp = prefs_.CreateSubKey({kPrefsSubDir, "id-1", kFpKey});
453 EXPECT_TRUE(prefs_.SetString(key1_fp, "3.7"));
454 auto key_not_fp = prefs_.CreateSubKey({kPrefsSubDir, "id-1", kOtherKey});
455 EXPECT_TRUE(prefs_.SetString(key_not_fp, "not_fp_val"));
456 auto key2_fp = prefs_.CreateSubKey({kPrefsSubDir, "id-2", kFpKey});
457 EXPECT_TRUE(prefs_.SetString(key2_fp, "3.9"));
458 auto key3_fp = prefs_.CreateSubKey({kPrefsSubDir, "id-3", kFpKey});
459 EXPECT_TRUE(prefs_.SetString(key3_fp, "3.45"));
460
461 // Pref key does not match full subkey at end, should not delete.
462 auto key_middle_fp = prefs_.CreateSubKey({kPrefsSubDir, kFpKey, kOtherKey});
463 EXPECT_TRUE(prefs_.SetString(key_middle_fp, "not_fp_val"));
464 auto key_end_not_fp = prefs_.CreateSubKey({kPrefsSubDir, "id-1", kNotFpKey});
465 EXPECT_TRUE(prefs_.SetString(key_end_not_fp, "not_fp_val"));
466
467 // Delete key in platform and one namespace.
468 prefs_.Delete(kFpKey, {kPrefsSubDir});
469
470 EXPECT_FALSE(prefs_.Exists(kFpKey));
471 EXPECT_FALSE(prefs_.Exists(key1_fp));
472 EXPECT_FALSE(prefs_.Exists(key2_fp));
473 EXPECT_FALSE(prefs_.Exists(key3_fp));
474
475 // Check other keys are not deleted.
476 EXPECT_TRUE(prefs_.Exists(kOtherKey));
477 EXPECT_TRUE(prefs_.Exists(key_not_fp));
478 EXPECT_TRUE(prefs_.Exists(key_middle_fp));
479 EXPECT_TRUE(prefs_.Exists(key_end_not_fp));
480}
481
482TEST_F(PrefsTest, DeleteMultipleNamespaces) {
483 const string kFirstSubDir = "foo-dir";
484 const string kSecondarySubDir = "bar-dir";
485 const string kTertiarySubDir = "ter-dir";
486 const string kFpKey = "kPrefFp";
487
488 EXPECT_TRUE(prefs_.SetString(kFpKey, "3.000"));
489 // Set pref key in different namespaces.
490 auto key1_fp = prefs_.CreateSubKey({kFirstSubDir, "id-1", kFpKey});
491 EXPECT_TRUE(prefs_.SetString(key1_fp, "3.7"));
492 auto key2_fp = prefs_.CreateSubKey({kSecondarySubDir, "id-3", kFpKey});
493 EXPECT_TRUE(prefs_.SetString(key2_fp, "7.45"));
494 auto key3_fp = prefs_.CreateSubKey({kTertiarySubDir, "id-3", kFpKey});
495 EXPECT_TRUE(prefs_.SetString(key3_fp, "7.45"));
496
497 // Delete key in platform and given namespaces.
498 prefs_.Delete(kFpKey, {kFirstSubDir, kSecondarySubDir});
499
500 EXPECT_FALSE(prefs_.Exists(kFpKey));
501 EXPECT_FALSE(prefs_.Exists(key1_fp));
502 EXPECT_FALSE(prefs_.Exists(key2_fp));
503
504 // Tertiary namespace not given to delete. Key should still exist.
505 EXPECT_TRUE(prefs_.Exists(key3_fp));
506}
507
Alex Deymod6f60072015-10-12 12:22:27 -0700508class MockPrefsObserver : public PrefsInterface::ObserverInterface {
509 public:
510 MOCK_METHOD1(OnPrefSet, void(const string&));
511 MOCK_METHOD1(OnPrefDeleted, void(const string& key));
512};
513
514TEST_F(PrefsTest, ObserversCalled) {
515 MockPrefsObserver mock_obserser;
516 prefs_.AddObserver(kKey, &mock_obserser);
517
518 EXPECT_CALL(mock_obserser, OnPrefSet(Eq(kKey)));
519 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
520 prefs_.SetString(kKey, "value");
521 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
522
523 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
524 EXPECT_CALL(mock_obserser, OnPrefDeleted(Eq(kKey)));
525 prefs_.Delete(kKey);
526 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
527
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700528 auto key1 = prefs_.CreateSubKey({"ns", "sp1", "key1"});
Andrew065d78d2020-04-07 15:43:07 -0700529 prefs_.AddObserver(key1, &mock_obserser);
530
531 EXPECT_CALL(mock_obserser, OnPrefSet(key1));
532 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
533 prefs_.SetString(key1, "value");
534 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
535
536 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
537 EXPECT_CALL(mock_obserser, OnPrefDeleted(Eq(key1)));
538 prefs_.Delete(key1);
539 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
540
Alex Deymod6f60072015-10-12 12:22:27 -0700541 prefs_.RemoveObserver(kKey, &mock_obserser);
542}
543
544TEST_F(PrefsTest, OnlyCalledOnObservedKeys) {
545 MockPrefsObserver mock_obserser;
546 const char kUnusedKey[] = "unused-key";
547 prefs_.AddObserver(kUnusedKey, &mock_obserser);
548
549 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
550 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
551 prefs_.SetString(kKey, "value");
552 prefs_.Delete(kKey);
553
554 prefs_.RemoveObserver(kUnusedKey, &mock_obserser);
555}
556
557TEST_F(PrefsTest, RemovedObserversNotCalled) {
558 MockPrefsObserver mock_obserser_a, mock_obserser_b;
559 prefs_.AddObserver(kKey, &mock_obserser_a);
560 prefs_.AddObserver(kKey, &mock_obserser_b);
561 EXPECT_CALL(mock_obserser_a, OnPrefSet(_)).Times(2);
562 EXPECT_CALL(mock_obserser_b, OnPrefSet(_)).Times(1);
563 EXPECT_TRUE(prefs_.SetString(kKey, "value"));
564 prefs_.RemoveObserver(kKey, &mock_obserser_b);
565 EXPECT_TRUE(prefs_.SetString(kKey, "other value"));
566 prefs_.RemoveObserver(kKey, &mock_obserser_a);
567 EXPECT_TRUE(prefs_.SetString(kKey, "yet another value"));
568}
569
570TEST_F(PrefsTest, UnsuccessfulCallsNotObserved) {
571 MockPrefsObserver mock_obserser;
572 const char kInvalidKey[] = "no spaces or .";
573 prefs_.AddObserver(kInvalidKey, &mock_obserser);
574
575 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
576 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
577 EXPECT_FALSE(prefs_.SetString(kInvalidKey, "value"));
578 EXPECT_FALSE(prefs_.Delete(kInvalidKey));
579
580 prefs_.RemoveObserver(kInvalidKey, &mock_obserser);
581}
582
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700583TEST_F(PrefsTest, MultiNamespaceKeyTest) {
584 MultiNamespaceKeyTest();
585}
586
587class MemoryPrefsTest : public BasePrefsTest {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700588 protected:
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700589 void SetUp() override { common_prefs_ = &prefs_; }
590
Alex Deymoa0284ac2016-07-22 12:51:41 -0700591 MemoryPrefs prefs_;
592};
593
594TEST_F(MemoryPrefsTest, BasicTest) {
595 EXPECT_FALSE(prefs_.Exists(kKey));
596 int64_t value = 0;
597 EXPECT_FALSE(prefs_.GetInt64(kKey, &value));
598
599 EXPECT_TRUE(prefs_.SetInt64(kKey, 1234));
600 EXPECT_TRUE(prefs_.Exists(kKey));
601 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
602 EXPECT_EQ(1234, value);
603
604 EXPECT_TRUE(prefs_.Delete(kKey));
605 EXPECT_FALSE(prefs_.Exists(kKey));
Andrew914f5542020-04-21 10:56:33 -0700606 EXPECT_TRUE(prefs_.Delete(kKey));
Andrew065d78d2020-04-07 15:43:07 -0700607
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700608 auto key = prefs_.CreateSubKey({"ns", "sp", "sk"});
Andrew065d78d2020-04-07 15:43:07 -0700609 ASSERT_TRUE(prefs_.SetInt64(key, 0));
610 EXPECT_TRUE(prefs_.Exists(key));
611 EXPECT_TRUE(prefs_.Delete(kKey));
Alex Deymoa0284ac2016-07-22 12:51:41 -0700612}
613
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700614TEST_F(MemoryPrefsTest, MultiNamespaceKeyTest) {
615 MultiNamespaceKeyTest();
616}
617
Darin Petkov30030592010-07-27 13:53:20 -0700618} // namespace chromeos_update_engine