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 SHILL_KEY_FILE_STORE_H_
18 #define SHILL_KEY_FILE_STORE_H_
19 
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include <glib.h>  // Can't forward-declare GKeyFile due to typedef.
25 #include <base/files/file_path.h>
26 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
27 
28 #include "shill/crypto_provider.h"
29 #include "shill/store_interface.h"
30 
31 namespace shill {
32 
33 // A key file store implementation of the store interface. See
34 // http://www.gtk.org/api/2.6/glib/glib-Key-value-file-parser.html for details
35 // of the key file format.
36 class KeyFileStore : public StoreInterface {
37  public:
38   explicit KeyFileStore(const base::FilePath& path);
39   ~KeyFileStore() override;
40 
41   // Inherited from StoreInterface.
42   bool IsNonEmpty() const override;
43   bool Open() override;
44   bool Close() override;
45   bool Flush() override;
46   bool MarkAsCorrupted() override;
47   std::set<std::string> GetGroups() const override;
48   std::set<std::string> GetGroupsWithKey(const std::string& key) const override;
49   std::set<std::string> GetGroupsWithProperties(
50       const KeyValueStore& properties) const override;
51   bool ContainsGroup(const std::string& group) const override;
52   bool DeleteKey(const std::string& group, const std::string& key) override;
53   bool DeleteGroup(const std::string& group) override;
54   bool SetHeader(const std::string& header) override;
55   bool GetString(const std::string& group,
56                  const std::string& key,
57                  std::string* value) const override;
58   bool SetString(const std::string& group,
59                  const std::string& key,
60                  const std::string& value) override;
61   bool GetBool(const std::string& group,
62                const std::string& key,
63                bool* value) const override;
64   bool SetBool(const std::string& group,
65                const std::string& key,
66                bool value) override;
67   bool GetInt(const std::string& group,
68               const std::string& key,
69               int* value) const override;
70   bool SetInt(const std::string& group,
71               const std::string& key,
72               int value) override;
73   bool GetUint64(const std::string& group,
74                  const std::string& key,
75                  uint64_t* value) const override;
76   bool SetUint64(const std::string& group,
77                  const std::string& key,
78                  uint64_t value) override;
79   bool GetStringList(const std::string& group,
80                      const std::string& key,
81                      std::vector<std::string>* value) const override;
82   bool SetStringList(const std::string& group,
83                      const std::string& key,
84                      const std::vector<std::string>& value) override;
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   FRIEND_TEST(KeyFileStoreTest, OpenClose);
94   FRIEND_TEST(KeyFileStoreTest, OpenFail);
95 
96   static const char kCorruptSuffix[];
97 
98   void ReleaseKeyFile();
99   bool DoesGroupMatchProperties(const std::string& group,
100                                 const KeyValueStore& properties) const;
101 
102   CryptoProvider crypto_;
103   GKeyFile* key_file_;
104   const base::FilePath path_;
105 
106   DISALLOW_COPY_AND_ASSIGN(KeyFileStore);
107 };
108 
109 }  // namespace shill
110 
111 #endif  // SHILL_KEY_FILE_STORE_H_
112