• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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 //
16 
17 #ifndef UPDATE_ENGINE_COMMON_FAKE_PREFS_H_
18 #define UPDATE_ENGINE_COMMON_FAKE_PREFS_H_
19 
20 #include <map>
21 #include <string>
22 #include <string_view>
23 #include <vector>
24 
25 #include <base/macros.h>
26 
27 #include "update_engine/common/prefs_interface.h"
28 
29 namespace chromeos_update_engine {
30 
31 // Implements a fake preference store by storing the value associated with
32 // a key in a std::map, suitable for testing. It doesn't allow to set a value on
33 // a key with a different type than the previously set type. This enforces the
34 // type of a given key to be fixed. Also the class checks that the Get*()
35 // methods aren't called on a key set with a different type.
36 
37 class FakePrefs : public PrefsInterface {
38  public:
39   FakePrefs() = default;
40   ~FakePrefs();
41 
42   // PrefsInterface methods.
43   bool GetString(const std::string& key, std::string* value) const override;
44   bool SetString(const std::string& key, std::string_view value) override;
45   bool GetInt64(const std::string& key, int64_t* value) const override;
46   bool SetInt64(const std::string& key, const int64_t value) override;
47   bool GetBoolean(const std::string& key, bool* value) const override;
48   bool SetBoolean(const std::string& key, const bool value) override;
49 
50   bool Exists(const std::string& key) const override;
51   bool Delete(const std::string& key) override;
52   bool Delete(const std::string& key,
53               const std::vector<std::string>& nss) override;
54 
55   bool GetSubKeys(const std::string& ns,
56                   std::vector<std::string>* keys) const override;
57 
58   void AddObserver(const std::string& key,
59                    ObserverInterface* observer) override;
60   void RemoveObserver(const std::string& key,
61                       ObserverInterface* observer) override;
62 
63  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 
80   // Class to store compile-time type-dependent constants.
81   template <typename T>
82   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.
88     static T FakePrefs::PrefValue::*const member;
89   };
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|.
95   void CheckKeyType(const std::string& key, PrefType type) const;
96 
97   // Helper function to set a value of the passed |key|. It sets the type based
98   // on the template parameter T.
99   template <typename T>
100   void SetValue(const std::string& key, T value);
101 
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.
105   template <typename T>
106   bool GetValue(const std::string& key, T* value) const;
107 
108   // Container for all the key/value pairs.
109   std::map<std::string, PrefTypeValue> values_;
110 
111   // The registered observers watching for changes.
112   std::map<std::string, std::vector<ObserverInterface*>> observers_;
113 
114   DISALLOW_COPY_AND_ASSIGN(FakePrefs);
115 };
116 
117 }  // namespace chromeos_update_engine
118 
119 #endif  // UPDATE_ENGINE_COMMON_FAKE_PREFS_H_
120