blob: f4174bfd89d855508247674988184243bf13f844 [file] [log] [blame]
Alex Deymo608a3652014-04-15 13:26:35 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_FAKE_PREFS_H_
6#define UPDATE_ENGINE_FAKE_PREFS_H_
Alex Deymo608a3652014-04-15 13:26:35 -07007
8#include <map>
9#include <string>
10
Ben Chan05735a12014-09-03 07:48:22 -070011#include <base/macros.h>
Alex Deymo608a3652014-04-15 13:26:35 -070012
13#include "update_engine/prefs_interface.h"
14
15namespace chromeos_update_engine {
16
17// Implements a fake preference store by storing the value associated with
18// a key in a std::map, suitable for testing. It doesn't allow to set a value on
19// a key with a different type than the previously set type. This enforces the
20// type of a given key to be fixed. Also the class checks that the Get*()
21// methods aren't called on a key set with a different type.
22
23class FakePrefs : public PrefsInterface {
24 public:
25 FakePrefs() {}
26
27 // PrefsInterface methods.
28 bool GetString(const std::string& key, std::string* value) override;
29 bool SetString(const std::string& key, const std::string& value) override;
30 bool GetInt64(const std::string& key, int64_t* value) override;
31 bool SetInt64(const std::string& key, const int64_t value) override;
32 bool GetBoolean(const std::string& key, bool* value) override;
33 bool SetBoolean(const std::string& key, const bool value) override;
34
35 bool Exists(const std::string& key) override;
36 bool Delete(const std::string& key) override;
37
38 private:
39 enum class PrefType {
40 kString,
41 kInt64,
42 kBool,
43 };
44 struct PrefValue {
45 std::string as_str;
46 int64_t as_int64;
47 bool as_bool;
48 };
49
50 struct PrefTypeValue {
51 PrefType type;
52 PrefValue value;
53 };
54
Alex Vakulenko072359c2014-07-18 11:41:07 -070055 // Class to store compile-time type-dependent constants.
Alex Deymo608a3652014-04-15 13:26:35 -070056 template<typename T>
57 class PrefConsts {
58 public:
59 // The PrefType associated with T.
60 static FakePrefs::PrefType const type;
61
62 // The data member pointer to PrefValue associated with T.
63 static T FakePrefs::PrefValue::* const member;
64 };
65
66 // Returns a string representation of the PrefType useful for logging.
67 static std::string GetTypeName(PrefType type);
68
69 // Checks that the |key| is either not present or has the given |type|.
70 void CheckKeyType(const std::string& key, PrefType type) const;
71
72 // Helper function to set a value of the passed |key|. It sets the type based
73 // on the template parameter T.
74 template<typename T>
75 void SetValue(const std::string& key, const T& value);
76
77 // Helper function to get a value from the map checking for invalid calls.
78 // The function fails the test if you attempt to read a value defined as a
79 // different type. Returns whether the get succeeded.
80 template<typename T>
81 bool GetValue(const std::string& key, T* value) const;
82
83 // Container for all the key/value pairs.
84 std::map<std::string, PrefTypeValue> values_;
85
86 DISALLOW_COPY_AND_ASSIGN(FakePrefs);
87};
88
89} // namespace chromeos_update_engine
90
Gilad Arnoldcf175a02014-07-10 16:48:47 -070091#endif // UPDATE_ENGINE_FAKE_PREFS_H_