blob: fbc8c67849c63449e8cbb62aef3c28801148e900 [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
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_FAKE_PREFS_H_
18#define UPDATE_ENGINE_FAKE_PREFS_H_
Alex Deymo608a3652014-04-15 13:26:35 -070019
20#include <map>
21#include <string>
22
Ben Chan05735a12014-09-03 07:48:22 -070023#include <base/macros.h>
Alex Deymo608a3652014-04-15 13:26:35 -070024
25#include "update_engine/prefs_interface.h"
26
27namespace chromeos_update_engine {
28
29// Implements a fake preference store by storing the value associated with
30// a key in a std::map, suitable for testing. It doesn't allow to set a value on
31// a key with a different type than the previously set type. This enforces the
32// type of a given key to be fixed. Also the class checks that the Get*()
33// methods aren't called on a key set with a different type.
34
35class FakePrefs : public PrefsInterface {
36 public:
37 FakePrefs() {}
38
39 // PrefsInterface methods.
40 bool GetString(const std::string& key, std::string* value) override;
41 bool SetString(const std::string& key, const std::string& value) override;
42 bool GetInt64(const std::string& key, int64_t* value) override;
43 bool SetInt64(const std::string& key, const int64_t value) override;
44 bool GetBoolean(const std::string& key, bool* value) override;
45 bool SetBoolean(const std::string& key, const bool value) override;
46
47 bool Exists(const std::string& key) override;
48 bool Delete(const std::string& key) override;
49
50 private:
51 enum class PrefType {
52 kString,
53 kInt64,
54 kBool,
55 };
56 struct PrefValue {
57 std::string as_str;
58 int64_t as_int64;
59 bool as_bool;
60 };
61
62 struct PrefTypeValue {
63 PrefType type;
64 PrefValue value;
65 };
66
Alex Vakulenko072359c2014-07-18 11:41:07 -070067 // Class to store compile-time type-dependent constants.
Alex Deymo608a3652014-04-15 13:26:35 -070068 template<typename T>
69 class PrefConsts {
70 public:
71 // The PrefType associated with T.
72 static FakePrefs::PrefType const type;
73
74 // The data member pointer to PrefValue associated with T.
75 static T FakePrefs::PrefValue::* const member;
76 };
77
78 // Returns a string representation of the PrefType useful for logging.
79 static std::string GetTypeName(PrefType type);
80
81 // Checks that the |key| is either not present or has the given |type|.
82 void CheckKeyType(const std::string& key, PrefType type) const;
83
84 // Helper function to set a value of the passed |key|. It sets the type based
85 // on the template parameter T.
86 template<typename T>
87 void SetValue(const std::string& key, const T& value);
88
89 // Helper function to get a value from the map checking for invalid calls.
90 // The function fails the test if you attempt to read a value defined as a
91 // different type. Returns whether the get succeeded.
92 template<typename T>
93 bool GetValue(const std::string& key, T* value) const;
94
95 // Container for all the key/value pairs.
96 std::map<std::string, PrefTypeValue> values_;
97
98 DISALLOW_COPY_AND_ASSIGN(FakePrefs);
99};
100
101} // namespace chromeos_update_engine
102
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700103#endif // UPDATE_ENGINE_FAKE_PREFS_H_