blob: f226949c7b08f52620a6b577de2e58cf2bfabc48 [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>
23
Ben Chan06c76a42014-09-05 08:21:06 -070024#include <base/files/file_util.h>
Sen Jiang371615b2016-04-13 15:54:29 -070025#include <base/files/scoped_temp_dir.h>
Ben Chan05735a12014-09-03 07:48:22 -070026#include <base/macros.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070027#include <base/strings/string_util.h>
28#include <base/strings/stringprintf.h>
Alex Deymod6f60072015-10-12 12:22:27 -070029#include <gmock/gmock.h>
Ben Chan05735a12014-09-03 07:48:22 -070030#include <gtest/gtest.h>
31
Darin Petkov30030592010-07-27 13:53:20 -070032using std::string;
Alex Deymod6f60072015-10-12 12:22:27 -070033using testing::_;
Andrew065d78d2020-04-07 15:43:07 -070034using testing::ElementsAre;
Amin Hassanib2689592019-01-13 17:04:28 -080035using testing::Eq;
Alex Deymod6f60072015-10-12 12:22:27 -070036
37namespace {
38// Test key used along the tests.
39const char kKey[] = "test-key";
Amin Hassanib2689592019-01-13 17:04:28 -080040} // namespace
Darin Petkov30030592010-07-27 13:53:20 -070041
42namespace chromeos_update_engine {
43
44class PrefsTest : public ::testing::Test {
45 protected:
Alex Deymo610277e2014-11-11 21:18:11 -080046 void SetUp() override {
Sen Jiang371615b2016-04-13 15:54:29 -070047 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
Eric Caruso7feac5b2018-01-23 16:20:19 -080048 prefs_dir_ = temp_dir_.GetPath();
Darin Petkov30030592010-07-27 13:53:20 -070049 ASSERT_TRUE(prefs_.Init(prefs_dir_));
50 }
51
Darin Petkov30030592010-07-27 13:53:20 -070052 bool SetValue(const string& key, const string& value) {
Amin Hassanib2689592019-01-13 17:04:28 -080053 return base::WriteFile(prefs_dir_.Append(key),
54 value.data(),
Ben Chan736fcb52014-05-21 18:28:22 -070055 value.length()) == static_cast<int>(value.length());
Darin Petkov30030592010-07-27 13:53:20 -070056 }
57
Sen Jiang371615b2016-04-13 15:54:29 -070058 base::ScopedTempDir temp_dir_;
Alex Vakulenko75039d72014-03-25 12:36:28 -070059 base::FilePath prefs_dir_;
Darin Petkov30030592010-07-27 13:53:20 -070060 Prefs prefs_;
61};
62
Andrew065d78d2020-04-07 15:43:07 -070063TEST(Prefs, Init) {
64 Prefs prefs;
65 const string name_space = "ns";
66 const string sub_pref = "sp";
67
68 base::ScopedTempDir temp_dir;
69 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
70 base::FilePath namespace_path = temp_dir.GetPath().Append(name_space);
71
72 EXPECT_TRUE(base::CreateDirectory(namespace_path.Append(sub_pref)));
73 EXPECT_TRUE(base::PathExists(namespace_path.Append(sub_pref)));
74 ASSERT_TRUE(prefs.Init(temp_dir.GetPath()));
75 EXPECT_FALSE(base::PathExists(namespace_path));
76}
77
Darin Petkov30030592010-07-27 13:53:20 -070078TEST_F(PrefsTest, GetFileNameForKey) {
Alex Deymod6f60072015-10-12 12:22:27 -070079 const char kAllvalidCharsKey[] =
80 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
Alex Vakulenko75039d72014-03-25 12:36:28 -070081 base::FilePath path;
Alex Deymoa0284ac2016-07-22 12:51:41 -070082 EXPECT_TRUE(prefs_.file_storage_.GetFileNameForKey(kAllvalidCharsKey, &path));
Alex Deymod6f60072015-10-12 12:22:27 -070083 EXPECT_EQ(prefs_dir_.Append(kAllvalidCharsKey).value(), path.value());
Darin Petkov30030592010-07-27 13:53:20 -070084}
85
86TEST_F(PrefsTest, GetFileNameForKeyBadCharacter) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070087 base::FilePath path;
Alex Deymoa0284ac2016-07-22 12:51:41 -070088 EXPECT_FALSE(prefs_.file_storage_.GetFileNameForKey("ABC abc", &path));
Darin Petkov30030592010-07-27 13:53:20 -070089}
90
91TEST_F(PrefsTest, GetFileNameForKeyEmpty) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070092 base::FilePath path;
Alex Deymoa0284ac2016-07-22 12:51:41 -070093 EXPECT_FALSE(prefs_.file_storage_.GetFileNameForKey("", &path));
Darin Petkov30030592010-07-27 13:53:20 -070094}
95
Andrew065d78d2020-04-07 15:43:07 -070096TEST_F(PrefsTest, CreateSubKey) {
97 const string name_space = "ns";
98 const string sub_pref1 = "sp1";
99 const string sub_pref2 = "sp2";
100 const string sub_key = "sk";
101
102 EXPECT_EQ(PrefsInterface::CreateSubKey(name_space, sub_pref1, sub_key),
103 "ns/sp1/sk");
104 EXPECT_EQ(PrefsInterface::CreateSubKey(name_space, sub_pref2, sub_key),
105 "ns/sp2/sk");
106}
107
Darin Petkov30030592010-07-27 13:53:20 -0700108TEST_F(PrefsTest, GetString) {
Darin Petkov30030592010-07-27 13:53:20 -0700109 const string test_data = "test data";
110 ASSERT_TRUE(SetValue(kKey, test_data));
111 string value;
112 EXPECT_TRUE(prefs_.GetString(kKey, &value));
113 EXPECT_EQ(test_data, value);
114}
115
116TEST_F(PrefsTest, GetStringBadKey) {
117 string value;
118 EXPECT_FALSE(prefs_.GetString(",bad", &value));
119}
120
121TEST_F(PrefsTest, GetStringNonExistentKey) {
122 string value;
123 EXPECT_FALSE(prefs_.GetString("non-existent-key", &value));
124}
125
126TEST_F(PrefsTest, SetString) {
Darin Petkov30030592010-07-27 13:53:20 -0700127 const char kValue[] = "some test value\non 2 lines";
128 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
129 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700130 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700131 EXPECT_EQ(kValue, value);
132}
133
134TEST_F(PrefsTest, SetStringBadKey) {
Alex Deymod6f60072015-10-12 12:22:27 -0700135 const char kKeyWithDots[] = ".no-dots";
136 EXPECT_FALSE(prefs_.SetString(kKeyWithDots, "some value"));
137 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKeyWithDots)));
Darin Petkov30030592010-07-27 13:53:20 -0700138}
139
140TEST_F(PrefsTest, SetStringCreateDir) {
Darin Petkov30030592010-07-27 13:53:20 -0700141 const char kValue[] = "test value";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700142 base::FilePath subdir = prefs_dir_.Append("subdir1").Append("subdir2");
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700143 EXPECT_TRUE(prefs_.Init(subdir));
Darin Petkov30030592010-07-27 13:53:20 -0700144 EXPECT_TRUE(prefs_.SetString(kKey, kValue));
145 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700146 EXPECT_TRUE(base::ReadFileToString(subdir.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700147 EXPECT_EQ(kValue, value);
148}
149
150TEST_F(PrefsTest, SetStringDirCreationFailure) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700151 EXPECT_TRUE(prefs_.Init(base::FilePath("/dev/null")));
Darin Petkov30030592010-07-27 13:53:20 -0700152 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
153}
154
155TEST_F(PrefsTest, SetStringFileCreationFailure) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700156 base::CreateDirectory(prefs_dir_.Append(kKey));
Darin Petkov30030592010-07-27 13:53:20 -0700157 EXPECT_FALSE(prefs_.SetString(kKey, "test value"));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700158 EXPECT_TRUE(base::DirectoryExists(prefs_dir_.Append(kKey)));
Darin Petkov30030592010-07-27 13:53:20 -0700159}
160
161TEST_F(PrefsTest, GetInt64) {
Darin Petkov30030592010-07-27 13:53:20 -0700162 ASSERT_TRUE(SetValue(kKey, " \n 25 \t "));
163 int64_t value;
164 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
165 EXPECT_EQ(25, value);
166}
167
168TEST_F(PrefsTest, GetInt64BadValue) {
Darin Petkov30030592010-07-27 13:53:20 -0700169 ASSERT_TRUE(SetValue(kKey, "30a"));
170 int64_t value;
171 EXPECT_FALSE(prefs_.GetInt64(kKey, &value));
172}
173
174TEST_F(PrefsTest, GetInt64Max) {
Amin Hassanib2689592019-01-13 17:04:28 -0800175 ASSERT_TRUE(SetValue(
176 kKey,
177 base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::max())));
Darin Petkov30030592010-07-27 13:53:20 -0700178 int64_t value;
179 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800180 EXPECT_EQ(std::numeric_limits<int64_t>::max(), value);
Darin Petkov30030592010-07-27 13:53:20 -0700181}
182
183TEST_F(PrefsTest, GetInt64Min) {
Amin Hassanib2689592019-01-13 17:04:28 -0800184 ASSERT_TRUE(SetValue(
185 kKey,
186 base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::min())));
Darin Petkov30030592010-07-27 13:53:20 -0700187 int64_t value;
188 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800189 EXPECT_EQ(std::numeric_limits<int64_t>::min(), value);
Darin Petkov30030592010-07-27 13:53:20 -0700190}
191
192TEST_F(PrefsTest, GetInt64Negative) {
Darin Petkov30030592010-07-27 13:53:20 -0700193 ASSERT_TRUE(SetValue(kKey, " \t -100 \n "));
194 int64_t value;
195 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
196 EXPECT_EQ(-100, value);
197}
198
199TEST_F(PrefsTest, GetInt64NonExistentKey) {
200 int64_t value;
201 EXPECT_FALSE(prefs_.GetInt64("random-key", &value));
202}
203
204TEST_F(PrefsTest, SetInt64) {
Darin Petkov30030592010-07-27 13:53:20 -0700205 EXPECT_TRUE(prefs_.SetInt64(kKey, -123));
206 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700207 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Darin Petkov30030592010-07-27 13:53:20 -0700208 EXPECT_EQ("-123", value);
209}
210
211TEST_F(PrefsTest, SetInt64BadKey) {
Alex Deymod6f60072015-10-12 12:22:27 -0700212 const char kKeyWithSpaces[] = "s p a c e s";
213 EXPECT_FALSE(prefs_.SetInt64(kKeyWithSpaces, 20));
214 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKeyWithSpaces)));
Darin Petkov30030592010-07-27 13:53:20 -0700215}
216
217TEST_F(PrefsTest, SetInt64Max) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800218 EXPECT_TRUE(prefs_.SetInt64(kKey, std::numeric_limits<int64_t>::max()));
Darin Petkov30030592010-07-27 13:53:20 -0700219 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700220 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Vakulenko0103c362016-01-20 07:56:15 -0800221 EXPECT_EQ(base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::max()),
222 value);
Darin Petkov30030592010-07-27 13:53:20 -0700223}
224
225TEST_F(PrefsTest, SetInt64Min) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800226 EXPECT_TRUE(prefs_.SetInt64(kKey, std::numeric_limits<int64_t>::min()));
Darin Petkov30030592010-07-27 13:53:20 -0700227 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700228 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Vakulenko0103c362016-01-20 07:56:15 -0800229 EXPECT_EQ(base::StringPrintf("%" PRIi64, std::numeric_limits<int64_t>::min()),
230 value);
Darin Petkov30030592010-07-27 13:53:20 -0700231}
232
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700233TEST_F(PrefsTest, GetBooleanFalse) {
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700234 ASSERT_TRUE(SetValue(kKey, " \n false \t "));
235 bool value;
236 EXPECT_TRUE(prefs_.GetBoolean(kKey, &value));
237 EXPECT_FALSE(value);
238}
239
240TEST_F(PrefsTest, GetBooleanTrue) {
241 const char kKey[] = "test-key";
242 ASSERT_TRUE(SetValue(kKey, " \t true \n "));
243 bool value;
244 EXPECT_TRUE(prefs_.GetBoolean(kKey, &value));
245 EXPECT_TRUE(value);
246}
247
248TEST_F(PrefsTest, GetBooleanBadValue) {
249 const char kKey[] = "test-key";
250 ASSERT_TRUE(SetValue(kKey, "1"));
251 bool value;
252 EXPECT_FALSE(prefs_.GetBoolean(kKey, &value));
253}
254
255TEST_F(PrefsTest, GetBooleanBadEmptyValue) {
256 const char kKey[] = "test-key";
257 ASSERT_TRUE(SetValue(kKey, ""));
258 bool value;
259 EXPECT_FALSE(prefs_.GetBoolean(kKey, &value));
260}
261
262TEST_F(PrefsTest, GetBooleanNonExistentKey) {
263 bool value;
264 EXPECT_FALSE(prefs_.GetBoolean("random-key", &value));
265}
266
267TEST_F(PrefsTest, SetBooleanTrue) {
268 const char kKey[] = "test-bool";
269 EXPECT_TRUE(prefs_.SetBoolean(kKey, true));
270 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700271 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700272 EXPECT_EQ("true", value);
273}
274
275TEST_F(PrefsTest, SetBooleanFalse) {
276 const char kKey[] = "test-bool";
277 EXPECT_TRUE(prefs_.SetBoolean(kKey, false));
278 string value;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700279 EXPECT_TRUE(base::ReadFileToString(prefs_dir_.Append(kKey), &value));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700280 EXPECT_EQ("false", value);
281}
282
283TEST_F(PrefsTest, SetBooleanBadKey) {
284 const char kKey[] = "s p a c e s";
285 EXPECT_FALSE(prefs_.SetBoolean(kKey, true));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700286 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(kKey)));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700287}
288
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700289TEST_F(PrefsTest, ExistsWorks) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700290 // test that the key doesn't exist before we set it.
291 EXPECT_FALSE(prefs_.Exists(kKey));
292
293 // test that the key exists after we set it.
294 ASSERT_TRUE(prefs_.SetInt64(kKey, 8));
295 EXPECT_TRUE(prefs_.Exists(kKey));
296}
297
298TEST_F(PrefsTest, DeleteWorks) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700299 // test that it's alright to delete a non-existent key.
300 EXPECT_TRUE(prefs_.Delete(kKey));
301
302 // delete the key after we set it.
303 ASSERT_TRUE(prefs_.SetInt64(kKey, 0));
304 EXPECT_TRUE(prefs_.Delete(kKey));
305
306 // make sure it doesn't exist anymore.
307 EXPECT_FALSE(prefs_.Exists(kKey));
308}
309
Andrew065d78d2020-04-07 15:43:07 -0700310TEST_F(PrefsTest, SetDeleteSubKey) {
311 const string name_space = "ns";
312 const string sub_pref = "sp";
313 const string sub_key1 = "sk1";
314 const string sub_key2 = "sk2";
315 auto key1 = prefs_.CreateSubKey(name_space, sub_pref, sub_key1);
316 auto key2 = prefs_.CreateSubKey(name_space, sub_pref, sub_key2);
317 base::FilePath sub_pref_path = prefs_dir_.Append(name_space).Append(sub_pref);
318
319 ASSERT_TRUE(prefs_.SetInt64(key1, 0));
320 ASSERT_TRUE(prefs_.SetInt64(key2, 0));
321 EXPECT_TRUE(base::PathExists(sub_pref_path.Append(sub_key1)));
322 EXPECT_TRUE(base::PathExists(sub_pref_path.Append(sub_key2)));
323
324 ASSERT_TRUE(prefs_.Delete(key1));
325 EXPECT_FALSE(base::PathExists(sub_pref_path.Append(sub_key1)));
326 EXPECT_TRUE(base::PathExists(sub_pref_path.Append(sub_key2)));
327 ASSERT_TRUE(prefs_.Delete(key2));
328 EXPECT_FALSE(base::PathExists(sub_pref_path.Append(sub_key2)));
329 prefs_.Init(prefs_dir_);
330 EXPECT_FALSE(base::PathExists(prefs_dir_.Append(name_space)));
331}
332
Alex Deymod6f60072015-10-12 12:22:27 -0700333class MockPrefsObserver : public PrefsInterface::ObserverInterface {
334 public:
335 MOCK_METHOD1(OnPrefSet, void(const string&));
336 MOCK_METHOD1(OnPrefDeleted, void(const string& key));
337};
338
339TEST_F(PrefsTest, ObserversCalled) {
340 MockPrefsObserver mock_obserser;
341 prefs_.AddObserver(kKey, &mock_obserser);
342
343 EXPECT_CALL(mock_obserser, OnPrefSet(Eq(kKey)));
344 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
345 prefs_.SetString(kKey, "value");
346 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
347
348 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
349 EXPECT_CALL(mock_obserser, OnPrefDeleted(Eq(kKey)));
350 prefs_.Delete(kKey);
351 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
352
Andrew065d78d2020-04-07 15:43:07 -0700353 auto key1 = prefs_.CreateSubKey("ns", "sp1", "key1");
354 prefs_.AddObserver(key1, &mock_obserser);
355
356 EXPECT_CALL(mock_obserser, OnPrefSet(key1));
357 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
358 prefs_.SetString(key1, "value");
359 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
360
361 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
362 EXPECT_CALL(mock_obserser, OnPrefDeleted(Eq(key1)));
363 prefs_.Delete(key1);
364 testing::Mock::VerifyAndClearExpectations(&mock_obserser);
365
Alex Deymod6f60072015-10-12 12:22:27 -0700366 prefs_.RemoveObserver(kKey, &mock_obserser);
367}
368
369TEST_F(PrefsTest, OnlyCalledOnObservedKeys) {
370 MockPrefsObserver mock_obserser;
371 const char kUnusedKey[] = "unused-key";
372 prefs_.AddObserver(kUnusedKey, &mock_obserser);
373
374 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
375 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
376 prefs_.SetString(kKey, "value");
377 prefs_.Delete(kKey);
378
379 prefs_.RemoveObserver(kUnusedKey, &mock_obserser);
380}
381
382TEST_F(PrefsTest, RemovedObserversNotCalled) {
383 MockPrefsObserver mock_obserser_a, mock_obserser_b;
384 prefs_.AddObserver(kKey, &mock_obserser_a);
385 prefs_.AddObserver(kKey, &mock_obserser_b);
386 EXPECT_CALL(mock_obserser_a, OnPrefSet(_)).Times(2);
387 EXPECT_CALL(mock_obserser_b, OnPrefSet(_)).Times(1);
388 EXPECT_TRUE(prefs_.SetString(kKey, "value"));
389 prefs_.RemoveObserver(kKey, &mock_obserser_b);
390 EXPECT_TRUE(prefs_.SetString(kKey, "other value"));
391 prefs_.RemoveObserver(kKey, &mock_obserser_a);
392 EXPECT_TRUE(prefs_.SetString(kKey, "yet another value"));
393}
394
395TEST_F(PrefsTest, UnsuccessfulCallsNotObserved) {
396 MockPrefsObserver mock_obserser;
397 const char kInvalidKey[] = "no spaces or .";
398 prefs_.AddObserver(kInvalidKey, &mock_obserser);
399
400 EXPECT_CALL(mock_obserser, OnPrefSet(_)).Times(0);
401 EXPECT_CALL(mock_obserser, OnPrefDeleted(_)).Times(0);
402 EXPECT_FALSE(prefs_.SetString(kInvalidKey, "value"));
403 EXPECT_FALSE(prefs_.Delete(kInvalidKey));
404
405 prefs_.RemoveObserver(kInvalidKey, &mock_obserser);
406}
407
Alex Deymoa0284ac2016-07-22 12:51:41 -0700408class MemoryPrefsTest : public ::testing::Test {
409 protected:
410 MemoryPrefs prefs_;
411};
412
413TEST_F(MemoryPrefsTest, BasicTest) {
414 EXPECT_FALSE(prefs_.Exists(kKey));
415 int64_t value = 0;
416 EXPECT_FALSE(prefs_.GetInt64(kKey, &value));
417
418 EXPECT_TRUE(prefs_.SetInt64(kKey, 1234));
419 EXPECT_TRUE(prefs_.Exists(kKey));
420 EXPECT_TRUE(prefs_.GetInt64(kKey, &value));
421 EXPECT_EQ(1234, value);
422
423 EXPECT_TRUE(prefs_.Delete(kKey));
424 EXPECT_FALSE(prefs_.Exists(kKey));
Andrew914f5542020-04-21 10:56:33 -0700425 EXPECT_TRUE(prefs_.Delete(kKey));
Andrew065d78d2020-04-07 15:43:07 -0700426
427 auto key = prefs_.CreateSubKey("ns", "sp", "sk");
428 ASSERT_TRUE(prefs_.SetInt64(key, 0));
429 EXPECT_TRUE(prefs_.Exists(key));
430 EXPECT_TRUE(prefs_.Delete(kKey));
Alex Deymoa0284ac2016-07-22 12:51:41 -0700431}
432
Darin Petkov30030592010-07-27 13:53:20 -0700433} // namespace chromeos_update_engine