blob: 6dd26c09d0f0c7ca95ccf2bad4f4a8d3b8a7f819 [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));
121 }
122
123 PrefsInterface* common_prefs_;
124};
125
126class PrefsTest : public BasePrefsTest {
Darin Petkov30030592010-07-27 13:53:20 -0700127 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800128 void SetUp() override {
Sen Jiang371615b2016-04-13 15:54:29 -0700129 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
Eric Caruso7feac5b2018-01-23 16:20:19 -0800130 prefs_dir_ = temp_dir_.GetPath();
Darin Petkov30030592010-07-27 13:53:20 -0700131 ASSERT_TRUE(prefs_.Init(prefs_dir_));
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700132 common_prefs_ = &prefs_;
Darin Petkov30030592010-07-27 13:53:20 -0700133 }
134
Darin Petkov30030592010-07-27 13:53:20 -0700135 bool SetValue(const string& key, const string& value) {
Amin Hassanib2689592019-01-13 17:04:28 -0800136 return base::WriteFile(prefs_dir_.Append(key),
137 value.data(),
Ben Chan736fcb52014-05-21 18:28:22 -0700138 value.length()) == static_cast<int>(value.length());
Darin Petkov30030592010-07-27 13:53:20 -0700139 }
140
Sen Jiang371615b2016-04-13 15:54:29 -0700141 base::ScopedTempDir temp_dir_;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700142 base::FilePath prefs_dir_;
Darin Petkov30030592010-07-27 13:53:20 -0700143 Prefs prefs_;
144};
145
Andrew065d78d2020-04-07 15:43:07 -0700146TEST(Prefs, Init) {
147 Prefs prefs;
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700148 const string ns1 = "ns1";
149 const string ns2A = "ns2A";
150 const string ns2B = "ns2B";
Andrew065d78d2020-04-07 15:43:07 -0700151 const string sub_pref = "sp";
152
153 base::ScopedTempDir temp_dir;
154 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700155 auto ns1_path = temp_dir.GetPath().Append(ns1);
156 auto ns2A_path = ns1_path.Append(ns2A);
157 auto ns2B_path = ns1_path.Append(ns2B);
158 auto sub_pref_path = ns2A_path.Append(sub_pref);
Andrew065d78d2020-04-07 15:43:07 -0700159
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700160 EXPECT_TRUE(base::CreateDirectory(ns2B_path));
161 EXPECT_TRUE(base::PathExists(ns2B_path));
162
163 EXPECT_TRUE(base::CreateDirectory(sub_pref_path));
164 EXPECT_TRUE(base::PathExists(sub_pref_path));
165
166 EXPECT_TRUE(base::PathExists(ns1_path));
Andrew065d78d2020-04-07 15:43:07 -0700167 ASSERT_TRUE(prefs.Init(temp_dir.GetPath()));
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700168 EXPECT_FALSE(base::PathExists(ns1_path));
Andrew065d78d2020-04-07 15:43:07 -0700169}
170
Darin Petkov30030592010-07-27 13:53:20 -0700171TEST_F(PrefsTest, GetFileNameForKey) {
Alex Deymod6f60072015-10-12 12:22:27 -0700172 const char kAllvalidCharsKey[] =
173 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700174 base::FilePath path;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700175 EXPECT_TRUE(prefs_.file_storage_.GetFileNameForKey(kAllvalidCharsKey, &path));
Alex Deymod6f60072015-10-12 12:22:27 -0700176 EXPECT_EQ(prefs_dir_.Append(kAllvalidCharsKey).value(), path.value());
Darin Petkov30030592010-07-27 13:53:20 -0700177}
178
179TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700180 base::FilePath path;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700181 EXPECT_FALSE(prefs_.file_storage_.GetFileNameForKey("ABC abc", &path));
Darin Petkov30030592010-07-27 13:53:20 -0700182}
183
184TEST_F(PrefsTest, GetFileNameForKeyEmpty) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700185 base::FilePath path;
Alex Deymoa0284ac2016-07-22 12:51:41 -0700186 EXPECT_FALSE(prefs_.file_storage_.GetFileNameForKey("", &path));
Darin Petkov30030592010-07-27 13:53:20 -0700187}
188
Andrew065d78d2020-04-07 15:43:07 -0700189TEST_F(PrefsTest, CreateSubKey) {
190 const string name_space = "ns";
191 const string sub_pref1 = "sp1";
192 const string sub_pref2 = "sp2";
193 const string sub_key = "sk";
194
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700195 EXPECT_EQ(PrefsInterface::CreateSubKey({name_space, sub_pref1, sub_key}),
Andrew065d78d2020-04-07 15:43:07 -0700196 "ns/sp1/sk");
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700197 EXPECT_EQ(PrefsInterface::CreateSubKey({name_space, sub_pref2, sub_key}),
Andrew065d78d2020-04-07 15:43:07 -0700198 "ns/sp2/sk");
199}
200
Darin Petkov30030592010-07-27 13:53:20 -0700201TEST_F(PrefsTest, GetString) {
Darin Petkov30030592010-07-27 13:53:20 -0700202 const string test_data = "test data";
203 ASSERT_TRUE(SetValue(kKey, test_data));
204 string value;
205 EXPECT_TRUE(prefs_.GetString(kKey, &value));
206 EXPECT_EQ(test_data, value);
207}
208
209TEST_F(PrefsTest, GetStringBadKey) {
210 string value;
211 EXPECT_FALSE(prefs_.GetString(",bad", &value));
212}
213
214TEST_F(PrefsTest, GetStringNonExistentKey) {
215 string value;
216 EXPECT_FALSE(prefs_.GetString("non-existent-key", &value));
217}
218
219TEST_F(PrefsTest, SetString) {
Darin Petkov30030592010-07-27 13:53:20 -0700220 const char kValue[] = "some test value\non 2 lines";
221 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
222 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700223 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700224 EXPECT_EQ(kValue, value);
225}
226
227TEST_F(PrefsTest, SetStringBadKey) {
Alex Deymod6f60072015-10-12 12:22:27 -0700228 const char kKeyWithDots[] = ".no-dots";
229 EXPECT_FALSE(prefs_.SetString(kKeyWithDots, "some value"));
230 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKeyWithDots)));
Darin Petkov30030592010-07-27 13:53:20 -0700231}
232
233TEST_F(PrefsTest, SetStringCreateDir) {
Darin Petkov30030592010-07-27 13:53:20 -0700234 const char kValue[] = "test value";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700235 base::FilePath subdir = prefs_dir_.Append("subdir1").Append("subdir2");
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700236 EXPECT_TRUE(prefs_.Init(subdir));
Darin Petkov30030592010-07-27 13:53:20 -0700237 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
238 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700239 EXPECT_TRUE(base::ReadFileToString(subdir.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700240 EXPECT_EQ(kValue, value);
241}
242
243TEST_F(PrefsTest, SetStringDirCreationFailure) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700244 EXPECT_TRUE(prefs_.Init(base::FilePath("/dev/null")));
Darin Petkov30030592010-07-27 13:53:20 -0700245 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
246}
247
248TEST_F(PrefsTest, SetStringFileCreationFailure) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700249 base::CreateDirectory(prefs_dir_.Append(kKey));
Darin Petkov30030592010-07-27 13:53:20 -0700250 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700251 EXPECT_TRUE(base::DirectoryExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700252}
253
254TEST_F(PrefsTest, GetInt64) {
Darin Petkov30030592010-07-27 13:53:20 -0700255 ASSERT_TRUE(SetValue(kKey, " \n 25 \t "));
256 int64_t value;
257 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
258 EXPECT_EQ(25, value);
259}
260
261TEST_F(PrefsTest, GetInt64BadValue) {
Darin Petkov30030592010-07-27 13:53:20 -0700262 ASSERT_TRUE(SetValue(kKey, "30a"));
263 int64_t value;
264 EXPECT_FALSE(prefs_.GetInt64(kKey, &value));
265}
266
267TEST_F(PrefsTest, GetInt64Max) {
Amin Hassanib2689592019-01-13 17:04:28 -0800268 ASSERT_TRUE(SetValue(
269 kKey,
270 base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::max())));
Darin Petkov30030592010-07-27 13:53:20 -0700271 int64_t value;
272 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800273 EXPECT_EQ(std::numeric_limits<int64_t>::max(), value);
Darin Petkov30030592010-07-27 13:53:20 -0700274}
275
276TEST_F(PrefsTest, GetInt64Min) {
Amin Hassanib2689592019-01-13 17:04:28 -0800277 ASSERT_TRUE(SetValue(
278 kKey,
279 base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::min())));
Darin Petkov30030592010-07-27 13:53:20 -0700280 int64_t value;
281 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800282 EXPECT_EQ(std::numeric_limits<int64_t>::min(), value);
Darin Petkov30030592010-07-27 13:53:20 -0700283}
284
285TEST_F(PrefsTest, GetInt64Negative) {
Darin Petkov30030592010-07-27 13:53:20 -0700286 ASSERT_TRUE(SetValue(kKey, " \t -100 \n "));
287 int64_t value;
288 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
289 EXPECT_EQ(-100, value);
290}
291
292TEST_F(PrefsTest, GetInt64NonExistentKey) {
293 int64_t value;
294 EXPECT_FALSE(prefs_.GetInt64("random-key", &value));
295}
296
297TEST_F(PrefsTest, SetInt64) {
Darin Petkov30030592010-07-27 13:53:20 -0700298 EXPECT_TRUE(prefs_.SetInt64(kKey, -123));
299 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700300 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700301 EXPECT_EQ("-123", value);
302}
303
304TEST_F(PrefsTest, SetInt64BadKey) {
Alex Deymod6f60072015-10-12 12:22:27 -0700305 const char kKeyWithSpaces[] = "s p a c e s";
306 EXPECT_FALSE(prefs_.SetInt64(kKeyWithSpaces, 20));
307 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKeyWithSpaces)));
Darin Petkov30030592010-07-27 13:53:20 -0700308}
309
310TEST_F(PrefsTest, SetInt64Max) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800311 EXPECT_TRUE(prefs_.SetInt64(kKey, std::numeric_limits<int64_t>::max()));
Darin Petkov30030592010-07-27 13:53:20 -0700312 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700313 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Vakulenko0103c362016-01-20 07:56:15 -0800314 EXPECT_EQ(base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::max()),
315 value);
Darin Petkov30030592010-07-27 13:53:20 -0700316}
317
318TEST_F(PrefsTest, SetInt64Min) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800319 EXPECT_TRUE(prefs_.SetInt64(kKey, std::numeric_limits<int64_t>::min()));
Darin Petkov30030592010-07-27 13:53:20 -0700320 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700321 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Vakulenko0103c362016-01-20 07:56:15 -0800322 EXPECT_EQ(base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::min()),
323 value);
Darin Petkov30030592010-07-27 13:53:20 -0700324}
325
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700326TEST_F(PrefsTest, GetBooleanFalse) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700327 ASSERT_TRUE(SetValue(kKey, " \n false \t "));
328 bool value;
329 EXPECT_TRUE(prefs_.GetBoolean(kKey, &value));
330 EXPECT_FALSE(value);
331}
332
333TEST_F(PrefsTest, GetBooleanTrue) {
334 const char kKey[] = "test-key";
335 ASSERT_TRUE(SetValue(kKey, " \t true \n "));
336 bool value;
337 EXPECT_TRUE(prefs_.GetBoolean(kKey, &value));
338 EXPECT_TRUE(value);
339}
340
341TEST_F(PrefsTest, GetBooleanBadValue) {
342 const char kKey[] = "test-key";
343 ASSERT_TRUE(SetValue(kKey, "1"));
344 bool value;
345 EXPECT_FALSE(prefs_.GetBoolean(kKey, &value));
346}
347
348TEST_F(PrefsTest, GetBooleanBadEmptyValue) {
349 const char kKey[] = "test-key";
350 ASSERT_TRUE(SetValue(kKey, ""));
351 bool value;
352 EXPECT_FALSE(prefs_.GetBoolean(kKey, &value));
353}
354
355TEST_F(PrefsTest, GetBooleanNonExistentKey) {
356 bool value;
357 EXPECT_FALSE(prefs_.GetBoolean("random-key", &value));
358}
359
360TEST_F(PrefsTest, SetBooleanTrue) {
361 const char kKey[] = "test-bool";
362 EXPECT_TRUE(prefs_.SetBoolean(kKey, true));
363 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700364 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700365 EXPECT_EQ("true", value);
366}
367
368TEST_F(PrefsTest, SetBooleanFalse) {
369 const char kKey[] = "test-bool";
370 EXPECT_TRUE(prefs_.SetBoolean(kKey, false));
371 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700372 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700373 EXPECT_EQ("false", value);
374}
375
376TEST_F(PrefsTest, SetBooleanBadKey) {
377 const char kKey[] = "s p a c e s";
378 EXPECT_FALSE(prefs_.SetBoolean(kKey, true));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700379 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700380}
381
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700382TEST_F(PrefsTest, ExistsWorks) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700383 // test that the key doesn't exist before we set it.
384 EXPECT_FALSE(prefs_.Exists(kKey));
385
386 // test that the key exists after we set it.
387 ASSERT_TRUE(prefs_.SetInt64(kKey, 8));
388 EXPECT_TRUE(prefs_.Exists(kKey));
389}
390
391TEST_F(PrefsTest, DeleteWorks) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700392 // test that it's alright to delete a non-existent key.
393 EXPECT_TRUE(prefs_.Delete(kKey));
394
395 // delete the key after we set it.
396 ASSERT_TRUE(prefs_.SetInt64(kKey, 0));
397 EXPECT_TRUE(prefs_.Delete(kKey));
398
399 // make sure it doesn't exist anymore.
400 EXPECT_FALSE(prefs_.Exists(kKey));
401}
402
Andrew065d78d2020-04-07 15:43:07 -0700403TEST_F(PrefsTest, SetDeleteSubKey) {
404 const string name_space = "ns";
405 const string sub_pref = "sp";
406 const string sub_key1 = "sk1";
407 const string sub_key2 = "sk2";
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700408 auto key1 = prefs_.CreateSubKey({name_space, sub_pref, sub_key1});
409 auto key2 = prefs_.CreateSubKey({name_space, sub_pref, sub_key2});
Andrew065d78d2020-04-07 15:43:07 -0700410 base::FilePath sub_pref_path = prefs_dir_.Append(name_space).Append(sub_pref);
411
412 ASSERT_TRUE(prefs_.SetInt64(key1, 0));
413 ASSERT_TRUE(prefs_.SetInt64(key2, 0));
414 EXPECT_TRUE(base::PathExists(sub_pref_path.Append(sub_key1)));
415 EXPECT_TRUE(base::PathExists(sub_pref_path.Append(sub_key2)));
416
417 ASSERT_TRUE(prefs_.Delete(key1));
418 EXPECT_FALSE(base::PathExists(sub_pref_path.Append(sub_key1)));
419 EXPECT_TRUE(base::PathExists(sub_pref_path.Append(sub_key2)));
420 ASSERT_TRUE(prefs_.Delete(key2));
421 EXPECT_FALSE(base::PathExists(sub_pref_path.Append(sub_key2)));
422 prefs_.Init(prefs_dir_);
423 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(name_space)));
424}
425
Alex Deymod6f60072015-10-12 12:22:27 -0700426class MockPrefsObserver : public PrefsInterface::ObserverInterface {
427 public:
428 MOCK_METHOD1(OnPrefSet, void(const string&));
429 MOCK_METHOD1(OnPrefDeleted, void(const string& key));
430};
431
432TEST_F(PrefsTest, ObserversCalled) {
433 MockPrefsObserver mock_obserser;
434 prefs_.AddObserver(kKey, &mock_obserser);
435
436 EXPECT_CALL(mock_obserser, OnPrefSet(Eq(kKey)));
437 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
438 prefs_.SetString(kKey, "value");
439 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
440
441 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
442 EXPECT_CALL(mock_obserser, OnPrefDeleted(Eq(kKey)));
443 prefs_.Delete(kKey);
444 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
445
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700446 auto key1 = prefs_.CreateSubKey({"ns", "sp1", "key1"});
Andrew065d78d2020-04-07 15:43:07 -0700447 prefs_.AddObserver(key1, &mock_obserser);
448
449 EXPECT_CALL(mock_obserser, OnPrefSet(key1));
450 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
451 prefs_.SetString(key1, "value");
452 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
453
454 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
455 EXPECT_CALL(mock_obserser, OnPrefDeleted(Eq(key1)));
456 prefs_.Delete(key1);
457 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
458
Alex Deymod6f60072015-10-12 12:22:27 -0700459 prefs_.RemoveObserver(kKey, &mock_obserser);
460}
461
462TEST_F(PrefsTest, OnlyCalledOnObservedKeys) {
463 MockPrefsObserver mock_obserser;
464 const char kUnusedKey[] = "unused-key";
465 prefs_.AddObserver(kUnusedKey, &mock_obserser);
466
467 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
468 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
469 prefs_.SetString(kKey, "value");
470 prefs_.Delete(kKey);
471
472 prefs_.RemoveObserver(kUnusedKey, &mock_obserser);
473}
474
475TEST_F(PrefsTest, RemovedObserversNotCalled) {
476 MockPrefsObserver mock_obserser_a, mock_obserser_b;
477 prefs_.AddObserver(kKey, &mock_obserser_a);
478 prefs_.AddObserver(kKey, &mock_obserser_b);
479 EXPECT_CALL(mock_obserser_a, OnPrefSet(_)).Times(2);
480 EXPECT_CALL(mock_obserser_b, OnPrefSet(_)).Times(1);
481 EXPECT_TRUE(prefs_.SetString(kKey, "value"));
482 prefs_.RemoveObserver(kKey, &mock_obserser_b);
483 EXPECT_TRUE(prefs_.SetString(kKey, "other value"));
484 prefs_.RemoveObserver(kKey, &mock_obserser_a);
485 EXPECT_TRUE(prefs_.SetString(kKey, "yet another value"));
486}
487
488TEST_F(PrefsTest, UnsuccessfulCallsNotObserved) {
489 MockPrefsObserver mock_obserser;
490 const char kInvalidKey[] = "no spaces or .";
491 prefs_.AddObserver(kInvalidKey, &mock_obserser);
492
493 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
494 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
495 EXPECT_FALSE(prefs_.SetString(kInvalidKey, "value"));
496 EXPECT_FALSE(prefs_.Delete(kInvalidKey));
497
498 prefs_.RemoveObserver(kInvalidKey, &mock_obserser);
499}
500
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700501TEST_F(PrefsTest, MultiNamespaceKeyTest) {
502 MultiNamespaceKeyTest();
503}
504
505class MemoryPrefsTest : public BasePrefsTest {
Alex Deymoa0284ac2016-07-22 12:51:41 -0700506 protected:
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700507 void SetUp() override { common_prefs_ = &prefs_; }
508
Alex Deymoa0284ac2016-07-22 12:51:41 -0700509 MemoryPrefs prefs_;
510};
511
512TEST_F(MemoryPrefsTest, BasicTest) {
513 EXPECT_FALSE(prefs_.Exists(kKey));
514 int64_t value = 0;
515 EXPECT_FALSE(prefs_.GetInt64(kKey, &value));
516
517 EXPECT_TRUE(prefs_.SetInt64(kKey, 1234));
518 EXPECT_TRUE(prefs_.Exists(kKey));
519 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
520 EXPECT_EQ(1234, value);
521
522 EXPECT_TRUE(prefs_.Delete(kKey));
523 EXPECT_FALSE(prefs_.Exists(kKey));
Andrew914f5542020-04-21 10:56:33 -0700524 EXPECT_TRUE(prefs_.Delete(kKey));
Andrew065d78d2020-04-07 15:43:07 -0700525
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700526 auto key = prefs_.CreateSubKey({"ns", "sp", "sk"});
Andrew065d78d2020-04-07 15:43:07 -0700527 ASSERT_TRUE(prefs_.SetInt64(key, 0));
528 EXPECT_TRUE(prefs_.Exists(key));
529 EXPECT_TRUE(prefs_.Delete(kKey));
Alex Deymoa0284ac2016-07-22 12:51:41 -0700530}
531
Jae Hoon Kim29a80e02020-05-11 20:18:49 -0700532TEST_F(MemoryPrefsTest, MultiNamespaceKeyTest) {
533 MultiNamespaceKeyTest();
534}
535
Darin Petkov30030592010-07-27 13:53:20 -0700536} // namespace chromeos_update_engine