blob: 7ae9fb920a4df63f6480ea346c59d751b7daa216 [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//
Alex Deymo608a3652014-04-15 13:26:35 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_FAKE_PREFS_H_
18#define UPDATE_ENGINE_COMMON_FAKE_PREFS_H_
Alex Deymo608a3652014-04-15 13:26:35 -070019
Kelvin Zhang1c86a922021-05-13 10:30:48 -040020#include <functional>
Alex Deymo608a3652014-04-15 13:26:35 -070021#include <map>
22#include <string>
Kelvin Zhangcf4600e2020-10-27 15:50:33 -040023#include <string_view>
Alex Deymod6f60072015-10-12 12:22:27 -070024#include <vector>
Alex Deymo608a3652014-04-15 13:26:35 -070025
Ben Chan05735a12014-09-03 07:48:22 -070026#include <base/macros.h>
Alex Deymo608a3652014-04-15 13:26:35 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/prefs_interface.h"
Alex Deymo608a3652014-04-15 13:26:35 -070029
30namespace chromeos_update_engine {
31
32// Implements a fake preference store by storing the value associated with
33// a key in a std::map, suitable for testing. It doesn't allow to set a value on
34// a key with a different type than the previously set type. This enforces the
35// type of a given key to be fixed. Also the class checks that the Get*()
36// methods aren't called on a key set with a different type.
37
38class FakePrefs : public PrefsInterface {
39 public:
Alex Deymod6f60072015-10-12 12:22:27 -070040 FakePrefs() = default;
41 ~FakePrefs();
Alex Deymo608a3652014-04-15 13:26:35 -070042
43 // PrefsInterface methods.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040044 bool GetString(std::string_view key, std::string* value) const override;
45 bool SetString(std::string_view key, std::string_view value) override;
46 bool GetInt64(std::string_view key, int64_t* value) const override;
47 bool SetInt64(std::string_view key, const int64_t value) override;
48 bool GetBoolean(std::string_view key, bool* value) const override;
49 bool SetBoolean(std::string_view key, const bool value) override;
Alex Deymo608a3652014-04-15 13:26:35 -070050
Kelvin Zhang1c86a922021-05-13 10:30:48 -040051 bool Exists(std::string_view key) const override;
52 bool Delete(std::string_view key) override;
53 bool Delete(std::string_view key,
Vyshu Khota4c5413d2020-11-04 16:17:25 -080054 const std::vector<std::string>& nss) override;
Alex Deymo608a3652014-04-15 13:26:35 -070055
Kelvin Zhang1c86a922021-05-13 10:30:48 -040056 bool GetSubKeys(std::string_view ns,
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070057 std::vector<std::string>* keys) const override;
58
Kelvin Zhang1c86a922021-05-13 10:30:48 -040059 void AddObserver(std::string_view key, ObserverInterface* observer) override;
60 void RemoveObserver(std::string_view key,
Alex Deymod6f60072015-10-12 12:22:27 -070061 ObserverInterface* observer) override;
62
Alex Deymo608a3652014-04-15 13:26:35 -070063 private:
64 enum class PrefType {
65 kString,
66 kInt64,
67 kBool,
68 };
69 struct PrefValue {
70 std::string as_str;
71 int64_t as_int64;
72 bool as_bool;
73 };
74
75 struct PrefTypeValue {
76 PrefType type;
77 PrefValue value;
78 };
79
Alex Vakulenko072359c2014-07-18 11:41:07 -070080 // Class to store compile-time type-dependent constants.
Amin Hassanib2689592019-01-13 17:04:28 -080081 template <typename T>
Alex Deymo608a3652014-04-15 13:26:35 -070082 class PrefConsts {
83 public:
84 // The PrefType associated with T.
85 static FakePrefs::PrefType const type;
86
87 // The data member pointer to PrefValue associated with T.
Amin Hassanib2689592019-01-13 17:04:28 -080088 static T FakePrefs::PrefValue::*const member;
Alex Deymo608a3652014-04-15 13:26:35 -070089 };
90
91 // Returns a string representation of the PrefType useful for logging.
92 static std::string GetTypeName(PrefType type);
93
94 // Checks that the |key| is either not present or has the given |type|.
Kelvin Zhang1c86a922021-05-13 10:30:48 -040095 void CheckKeyType(std::string_view key, PrefType type) const;
Alex Deymo608a3652014-04-15 13:26:35 -070096
97 // Helper function to set a value of the passed |key|. It sets the type based
98 // on the template parameter T.
Amin Hassanib2689592019-01-13 17:04:28 -080099 template <typename T>
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400100 void SetValue(std::string_view key, T value);
Alex Deymo608a3652014-04-15 13:26:35 -0700101
102 // Helper function to get a value from the map checking for invalid calls.
103 // The function fails the test if you attempt to read a value defined as a
104 // different type. Returns whether the get succeeded.
Amin Hassanib2689592019-01-13 17:04:28 -0800105 template <typename T>
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400106 bool GetValue(std::string_view key, T* value) const;
Alex Deymo608a3652014-04-15 13:26:35 -0700107
108 // Container for all the key/value pairs.
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400109 std::map<std::string, PrefTypeValue, std::less<>> values_;
Alex Deymo608a3652014-04-15 13:26:35 -0700110
Alex Deymod6f60072015-10-12 12:22:27 -0700111 // The registered observers watching for changes.
Kelvin Zhang1c86a922021-05-13 10:30:48 -0400112 std::map<std::string, std::vector<ObserverInterface*>, std::less<>>
113 observers_;
Alex Deymod6f60072015-10-12 12:22:27 -0700114
Alex Deymo608a3652014-04-15 13:26:35 -0700115 DISALLOW_COPY_AND_ASSIGN(FakePrefs);
116};
117
118} // namespace chromeos_update_engine
119
Alex Deymo39910dc2015-11-09 17:04:30 -0800120#endif // UPDATE_ENGINE_COMMON_FAKE_PREFS_H_