1 //
2 // Copyright (C) 2015 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 SHILL_FAKE_STORE_H_
18 #define SHILL_FAKE_STORE_H_
19 
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include <brillo/variant_dictionary.h>
26 
27 #include "shill/store_interface.h"
28 
29 namespace shill {
30 
31 // A Fake implementation of StoreInterface. Useful when a unit test
32 // for another class ("FooClass") a) does not need to FooClass's use
33 // of StoreInterface, and b) the FooClass test needs a functional
34 // store.
35 class FakeStore : public StoreInterface {
36  public:
37   FakeStore();
38 
39   // Inherited from StoreInterface.
40   bool IsNonEmpty() const override;
41   bool Open() override;
42   bool Close() override;
43   bool Flush() override;
44   bool MarkAsCorrupted() override;
45   std::set<std::string> GetGroups() const override;
46   std::set<std::string> GetGroupsWithKey(const std::string& key) const override;
47   std::set<std::string> GetGroupsWithProperties(
48       const KeyValueStore& properties) const override;
49   bool ContainsGroup(const std::string& group) const override;
50   bool DeleteKey(const std::string& group, const std::string& key) override;
51   bool DeleteGroup(const std::string& group) override;
52   bool SetHeader(const std::string& header) override;
53   bool GetString(const std::string& group,
54                  const std::string& key,
55                  std::string* value) const override;
56   bool SetString(const std::string& group,
57                  const std::string& key,
58                  const std::string& value) override;
59   bool GetBool(const std::string& group,
60                const std::string& key,
61                bool* value) const override;
62   bool SetBool(const std::string& group,
63                const std::string& key,
64                bool value) override;
65   bool GetInt(const std::string& group,
66               const std::string& key,
67               int* value) const override;
68   bool SetInt(const std::string& group,
69               const std::string& key,
70               int value) override;
71   bool GetUint64(const std::string& group,
72                  const std::string& key,
73                  uint64_t* value) const override;
74   bool SetUint64(const std::string& group,
75                  const std::string& key,
76                  uint64_t value) override;
77   bool GetStringList(const std::string& group,
78                      const std::string& key,
79                      std::vector<std::string>* value) const override;
80   bool SetStringList(const std::string& group,
81                      const std::string& key,
82                      const std::vector<std::string>& value) override;
83   // GetCryptedString is non-const for legacy reasons. See
84   // KeyFileStore::SetCryptedString() for details.
85   bool GetCryptedString(const std::string& group,
86                         const std::string& key,
87                         std::string* value) override;
88   bool SetCryptedString(const std::string& group,
89                         const std::string& key,
90                         const std::string& value) override;
91 
92  private:
93   template<typename T> bool ReadSetting(
94       const std::string& group, const std::string& key, T* out) const;
95   template<typename T> bool WriteSetting(
96       const std::string& group, const std::string& key, const T& new_value);
97 
98   std::map<std::string, brillo::VariantDictionary> group_name_to_settings_;
99 
100   DISALLOW_COPY_AND_ASSIGN(FakeStore);
101 };
102 
103 }  // namespace shill
104 
105 #endif  // SHILL_FAKE_STORE_H_
106