blob: b24ff4d8e462e566e24a4f444b0ae80ea278fb4a [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
20#include <map>
21#include <string>
Alex Deymod6f60072015-10-12 12:22:27 -070022#include <vector>
Alex Deymo608a3652014-04-15 13:26:35 -070023
Ben Chan05735a12014-09-03 07:48:22 -070024#include <base/macros.h>
Alex Deymo608a3652014-04-15 13:26:35 -070025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/prefs_interface.h"
Alex Deymo608a3652014-04-15 13:26:35 -070027
28namespace chromeos_update_engine {
29
30// Implements a fake preference store by storing the value associated with
31// a key in a std::map, suitable for testing. It doesn't allow to set a value on
32// a key with a different type than the previously set type. This enforces the
33// type of a given key to be fixed. Also the class checks that the Get*()
34// methods aren't called on a key set with a different type.
35
36class FakePrefs : public PrefsInterface {
37 public:
Alex Deymod6f60072015-10-12 12:22:27 -070038 FakePrefs() = default;
39 ~FakePrefs();
Alex Deymo608a3652014-04-15 13:26:35 -070040
41 // PrefsInterface methods.
Alex Deymod6f60072015-10-12 12:22:27 -070042 bool GetString(const std::string& key, std::string* value) const override;
Alex Deymo608a3652014-04-15 13:26:35 -070043 bool SetString(const std::string& key, const std::string& value) override;
Alex Deymod6f60072015-10-12 12:22:27 -070044 bool GetInt64(const std::string& key, int64_t* value) const override;
Alex Deymo608a3652014-04-15 13:26:35 -070045 bool SetInt64(const std::string& key, const int64_t value) override;
Alex Deymod6f60072015-10-12 12:22:27 -070046 bool GetBoolean(const std::string& key, bool* value) const override;
Alex Deymo608a3652014-04-15 13:26:35 -070047 bool SetBoolean(const std::string& key, const bool value) override;
48
Alex Deymod6f60072015-10-12 12:22:27 -070049 bool Exists(const std::string& key) const override;
Alex Deymo608a3652014-04-15 13:26:35 -070050 bool Delete(const std::string& key) override;
51
Jae Hoon Kim29a80e02020-05-11 20:18:49 -070052 bool GetSubKeys(const std::string& ns,
53 std::vector<std::string>* keys) const override;
54
Alex Deymod6f60072015-10-12 12:22:27 -070055 void AddObserver(const std::string& key,
56 ObserverInterface* observer) override;
57 void RemoveObserver(const std::string& key,
58 ObserverInterface* observer) override;
59
Alex Deymo608a3652014-04-15 13:26:35 -070060 private:
61 enum class PrefType {
62 kString,
63 kInt64,
64 kBool,
65 };
66 struct PrefValue {
67 std::string as_str;
68 int64_t as_int64;
69 bool as_bool;
70 };
71
72 struct PrefTypeValue {
73 PrefType type;
74 PrefValue value;
75 };
76
Alex Vakulenko072359c2014-07-18 11:41:07 -070077 // Class to store compile-time type-dependent constants.
Amin Hassanib2689592019-01-13 17:04:28 -080078 template <typename T>
Alex Deymo608a3652014-04-15 13:26:35 -070079 class PrefConsts {
80 public:
81 // The PrefType associated with T.
82 static FakePrefs::PrefType const type;
83
84 // The data member pointer to PrefValue associated with T.
Amin Hassanib2689592019-01-13 17:04:28 -080085 static T FakePrefs::PrefValue::*const member;
Alex Deymo608a3652014-04-15 13:26:35 -070086 };
87
88 // Returns a string representation of the PrefType useful for logging.
89 static std::string GetTypeName(PrefType type);
90
91 // Checks that the |key| is either not present or has the given |type|.
92 void CheckKeyType(const std::string& key, PrefType type) const;
93
94 // Helper function to set a value of the passed |key|. It sets the type based
95 // on the template parameter T.
Amin Hassanib2689592019-01-13 17:04:28 -080096 template <typename T>
Alex Deymo608a3652014-04-15 13:26:35 -070097 void SetValue(const std::string& key, const T& value);
98
99 // Helper function to get a value from the map checking for invalid calls.
100 // The function fails the test if you attempt to read a value defined as a
101 // different type. Returns whether the get succeeded.
Amin Hassanib2689592019-01-13 17:04:28 -0800102 template <typename T>
Alex Deymo608a3652014-04-15 13:26:35 -0700103 bool GetValue(const std::string& key, T* value) const;
104
105 // Container for all the key/value pairs.
106 std::map<std::string, PrefTypeValue> values_;
107
Alex Deymod6f60072015-10-12 12:22:27 -0700108 // The registered observers watching for changes.
109 std::map<std::string, std::vector<ObserverInterface*>> observers_;
110
Alex Deymo608a3652014-04-15 13:26:35 -0700111 DISALLOW_COPY_AND_ASSIGN(FakePrefs);
112};
113
114} // namespace chromeos_update_engine
115
Alex Deymo39910dc2015-11-09 17:04:30 -0800116#endif // UPDATE_ENGINE_COMMON_FAKE_PREFS_H_