Home | History | Annotate | Download | only in shill
      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_JSON_STORE_H_
     18 #define SHILL_JSON_STORE_H_
     19 
     20 #include <map>
     21 #include <set>
     22 #include <string>
     23 #include <vector>
     24 
     25 #include <base/files/file_path.h>
     26 #include <brillo/variant_dictionary.h>
     27 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
     28 
     29 #include "shill/store_interface.h"
     30 
     31 namespace shill {
     32 
     33 class JsonStore : public StoreInterface {
     34  public:
     35   explicit JsonStore(const base::FilePath& path);
     36   // TODO(quiche): Determine if we need a dtor. In particular, we'll
     37   // need one of StoreInterface implementations are expected to
     38   // automatically Flush() before destruction.
     39 
     40   // Inherited from StoreInterface.
     41   bool IsNonEmpty() const override;
     42   bool Open() override;
     43   bool Close() override;
     44   bool Flush() override;
     45   bool MarkAsCorrupted() override;
     46   std::set<std::string> GetGroups() const override;
     47   std::set<std::string> GetGroupsWithKey(const std::string& key) const override;
     48   std::set<std::string> GetGroupsWithProperties(
     49       const KeyValueStore& properties) const override;
     50   bool ContainsGroup(const std::string& group) const override;
     51   bool DeleteKey(const std::string& group, const std::string& key) override;
     52   bool DeleteGroup(const std::string& group) override;
     53   bool SetHeader(const std::string& header) override;
     54   bool GetString(const std::string& group,
     55                  const std::string& key,
     56                  std::string* value) const override;
     57   bool SetString(const std::string& group,
     58                  const std::string& key,
     59                  const std::string& value) override;
     60   bool GetBool(const std::string& group,
     61                const std::string& key,
     62                bool* value) const override;
     63   bool SetBool(const std::string& group,
     64                const std::string& key,
     65                bool value) override;
     66   bool GetInt(const std::string& group,
     67               const std::string& key,
     68               int* value) const override;
     69   bool SetInt(const std::string& group,
     70               const std::string& key,
     71               int value) override;
     72   bool GetUint64(const std::string& group,
     73                  const std::string& key,
     74                  uint64_t* value) const override;
     75   bool SetUint64(const std::string& group,
     76                  const std::string& key,
     77                  uint64_t value) override;
     78   bool GetStringList(const std::string& group,
     79                      const std::string& key,
     80                      std::vector<std::string>* value) const override;
     81   bool SetStringList(const std::string& group,
     82                      const std::string& key,
     83                      const std::vector<std::string>& value) override;
     84   // GetCryptedString is non-const for legacy reasons. See
     85   // KeyFileStore::SetCryptedString() for details.
     86   bool GetCryptedString(const std::string& group,
     87                         const std::string& key,
     88                         std::string* value) override;
     89   bool SetCryptedString(const std::string& group,
     90                         const std::string& key,
     91                         const std::string& value) override;
     92 
     93  private:
     94   FRIEND_TEST(JsonStoreTest, CanPersistAndRestoreHeader);  // file_description_
     95   // Tests which use |group_name_to_settings_|.
     96   FRIEND_TEST(JsonStoreTest, CanPersistAndRestoreAllTypes);
     97   FRIEND_TEST(JsonStoreTest, CanPersistAndRestoreNonUtf8Strings);
     98   FRIEND_TEST(JsonStoreTest, CanPersistAndRestoreNonUtf8StringList);
     99   FRIEND_TEST(JsonStoreTest, CanPersistAndRestoreMultipleGroups);
    100   FRIEND_TEST(JsonStoreTest, CanPersistAndRestoreMultipleGroupsWithSameKeys);
    101   FRIEND_TEST(JsonStoreTest, CanPersistAndRestoreStringsWithEmbeddedNulls);
    102   FRIEND_TEST(JsonStoreTest, CanPersistAndRestoreStringListWithEmbeddedNulls);
    103   // Tests which modify |path_|.
    104   FRIEND_TEST(JsonStoreTest, FlushFailsWhenPathComponentDoesNotExist);
    105 
    106   template<typename T> bool ReadSetting(
    107       const std::string& group, const std::string& key, T* out) const;
    108   template<typename T> bool WriteSetting(
    109       const std::string& group, const std::string& key, const T& new_value);
    110 
    111   const base::FilePath path_;
    112   std::string file_description_;
    113   std::map<std::string, brillo::VariantDictionary> group_name_to_settings_;
    114 
    115   DISALLOW_COPY_AND_ASSIGN(JsonStore);
    116 };
    117 
    118 }  // namespace shill
    119 
    120 #endif  // SHILL_JSON_STORE_H_
    121