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_PROPERTY_STORE_UNITTEST_H_ 18 #define SHILL_PROPERTY_STORE_UNITTEST_H_ 19 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include <base/files/scoped_temp_dir.h> 26 #include <brillo/any.h> 27 #include <gmock/gmock.h> 28 #include <gtest/gtest.h> 29 30 #include "shill/error.h" 31 #include "shill/key_value_store.h" 32 #include "shill/manager.h" 33 #include "shill/mock_control.h" 34 #include "shill/mock_metrics.h" 35 #include "shill/property_store.h" 36 #include "shill/test_event_dispatcher.h" 37 38 namespace shill { 39 40 class PropertyStoreTest : public testing::TestWithParam<brillo::Any> { 41 public: 42 typedef ::testing::Types<bool, int16_t, int32_t, std::string, Stringmap, 43 Stringmaps, Strings, uint8_t, uint16_t, Uint16s, 44 uint32_t> PropertyTypes; 45 46 // In real code, it's frowned upon to have non-POD static members, as there 47 // can be ordering issues if your constructors have side effects. 48 // These constructors don't, and declaring these as static lets me 49 // autogenerate a bunch of unit test code that I would otherwise need to 50 // copypaste. So I think it's safe and worth it. 51 static const brillo::Any kBoolV; 52 static const brillo::Any kByteV; 53 static const brillo::Any kInt16V; 54 static const brillo::Any kInt32V; 55 static const brillo::Any kKeyValueStoreV; 56 static const brillo::Any kStringV; 57 static const brillo::Any kStringmapV; 58 static const brillo::Any kStringmapsV; 59 static const brillo::Any kStringsV; 60 static const brillo::Any kUint16V; 61 static const brillo::Any kUint16sV; 62 static const brillo::Any kUint32V; 63 static const brillo::Any kUint64V; 64 65 PropertyStoreTest(); 66 ~PropertyStoreTest() override; 67 68 void SetUp() override; 69 MOCK_METHOD1(TestCallback, void(const std::string& property_name)); 70 MOCK_METHOD1(GetKeyValueStoreCallback, KeyValueStore(Error* error)); 71 MOCK_METHOD2(SetKeyValueStoreCallback, bool(const KeyValueStore& value, 72 Error* error)); 73 74 // Convenience overloads for GetProperty. Normally, we don't overload 75 // functions. But this is extremely useful for type-parameterized tests. GetProperty(const PropertyStore & store,const std::string & name,bool * storage,Error * error)76 static bool GetProperty(const PropertyStore& store, const std::string& name, 77 bool* storage, Error* error) { 78 return store.GetBoolProperty(name, storage, error); 79 } 80 GetProperty(const PropertyStore & store,const std::string & name,int16_t * storage,Error * error)81 static bool GetProperty(const PropertyStore& store, const std::string& name, 82 int16_t* storage, Error* error) { 83 return store.GetInt16Property(name, storage, error); 84 } 85 GetProperty(const PropertyStore & store,const std::string & name,int32_t * storage,Error * error)86 static bool GetProperty(const PropertyStore& store, const std::string& name, 87 int32_t* storage, Error* error) { 88 return store.GetInt32Property(name, storage, error); 89 } 90 GetProperty(const PropertyStore & store,const std::string & name,std::string * storage,Error * error)91 static bool GetProperty(const PropertyStore& store, const std::string& name, 92 std::string* storage, Error* error) { 93 return store.GetStringProperty(name, storage, error); 94 } 95 GetProperty(const PropertyStore & store,const std::string & name,Stringmap * storage,Error * error)96 static bool GetProperty(const PropertyStore& store, const std::string& name, 97 Stringmap* storage, Error* error) { 98 return store.GetStringmapProperty(name, storage, error); 99 } 100 GetProperty(const PropertyStore & store,const std::string & name,Stringmaps * storage,Error * error)101 static bool GetProperty(const PropertyStore& store, const std::string& name, 102 Stringmaps* storage, Error* error) { 103 return store.GetStringmapsProperty(name, storage, error); 104 } 105 GetProperty(const PropertyStore & store,const std::string & name,Strings * storage,Error * error)106 static bool GetProperty(const PropertyStore& store, const std::string& name, 107 Strings* storage, Error* error) { 108 return store.GetStringsProperty(name, storage, error); 109 } 110 GetProperty(const PropertyStore & store,const std::string & name,uint8_t * storage,Error * error)111 static bool GetProperty(const PropertyStore& store, const std::string& name, 112 uint8_t* storage, Error* error) { 113 return store.GetUint8Property(name, storage, error); 114 } 115 GetProperty(const PropertyStore & store,const std::string & name,uint16_t * storage,Error * error)116 static bool GetProperty(const PropertyStore& store, const std::string& name, 117 uint16_t* storage, Error* error) { 118 return store.GetUint16Property(name, storage, error); 119 } 120 GetProperty(const PropertyStore & store,const std::string & name,Uint16s * storage,Error * error)121 static bool GetProperty(const PropertyStore& store, const std::string& name, 122 Uint16s* storage, Error* error) { 123 return store.GetUint16sProperty(name, storage, error); 124 } 125 GetProperty(const PropertyStore & store,const std::string & name,uint32_t * storage,Error * error)126 static bool GetProperty(const PropertyStore& store, const std::string& name, 127 uint32_t* storage, Error* error) { 128 return store.GetUint32Property(name, storage, error); 129 } 130 131 // Convenience overloads for RegisterProperty. Normally, we don't overload 132 // functions. But this is extremely useful for type-parameterized tests. RegisterProperty(PropertyStore * store,const std::string & name,bool * storage)133 static void RegisterProperty( 134 PropertyStore* store, const std::string& name, bool* storage) { 135 store->RegisterBool(name, storage); 136 } 137 RegisterProperty(PropertyStore * store,const std::string & name,int16_t * storage)138 static void RegisterProperty( 139 PropertyStore* store, const std::string& name, int16_t* storage) { 140 store->RegisterInt16(name, storage); 141 } 142 RegisterProperty(PropertyStore * store,const std::string & name,int32_t * storage)143 static void RegisterProperty( 144 PropertyStore* store, const std::string& name, int32_t* storage) { 145 store->RegisterInt32(name, storage); 146 } 147 RegisterProperty(PropertyStore * store,const std::string & name,std::string * storage)148 static void RegisterProperty( 149 PropertyStore* store, const std::string& name, std::string* storage) { 150 store->RegisterString(name, storage); 151 } 152 RegisterProperty(PropertyStore * store,const std::string & name,Stringmap * storage)153 static void RegisterProperty( 154 PropertyStore* store, const std::string& name, Stringmap* storage) { 155 store->RegisterStringmap(name, storage); 156 } 157 RegisterProperty(PropertyStore * store,const std::string & name,Stringmaps * storage)158 static void RegisterProperty( 159 PropertyStore* store, const std::string& name, Stringmaps* storage) { 160 store->RegisterStringmaps(name, storage); 161 } 162 RegisterProperty(PropertyStore * store,const std::string & name,Strings * storage)163 static void RegisterProperty( 164 PropertyStore* store, const std::string& name, Strings* storage) { 165 store->RegisterStrings(name, storage); 166 } 167 RegisterProperty(PropertyStore * store,const std::string & name,uint8_t * storage)168 static void RegisterProperty( 169 PropertyStore* store, const std::string& name, uint8_t* storage) { 170 store->RegisterUint8(name, storage); 171 } 172 RegisterProperty(PropertyStore * store,const std::string & name,uint16_t * storage)173 static void RegisterProperty( 174 PropertyStore* store, const std::string& name, uint16_t* storage) { 175 store->RegisterUint16(name, storage); 176 } 177 RegisterProperty(PropertyStore * store,const std::string & name,Uint16s * storage)178 static void RegisterProperty( 179 PropertyStore* store, const std::string& name, Uint16s* storage) { 180 store->RegisterUint16s(name, storage); 181 } 182 RegisterProperty(PropertyStore * store,const std::string & name,uint32_t * storage)183 static void RegisterProperty( 184 PropertyStore* store, const std::string& name, uint32_t* storage) { 185 store->RegisterUint32(name, storage); 186 } 187 188 protected: manager()189 Manager* manager() { return &manager_; } control_interface()190 MockControl* control_interface() { return &control_interface_; } dispatcher()191 EventDispatcher* dispatcher() { return &dispatcher_; } metrics()192 MockMetrics* metrics() { return &metrics_; } default_technology_order()193 const std::vector<Technology::Identifier>& default_technology_order() { 194 return default_technology_order_; 195 } 196 run_path()197 const std::string& run_path() const { return path_; } storage_path()198 const std::string& storage_path() const { return path_; } 199 internal_error()200 const std::string& internal_error() const { return internal_error_; } invalid_args()201 const std::string& invalid_args() const { return invalid_args_; } invalid_prop()202 const std::string& invalid_prop() const { return invalid_prop_; } 203 204 private: 205 const std::string internal_error_; 206 const std::string invalid_args_; 207 const std::string invalid_prop_; 208 base::ScopedTempDir dir_; 209 const std::string path_; 210 211 MockControl control_interface_; 212 EventDispatcherForTest dispatcher_; 213 testing::NiceMock<MockMetrics> metrics_; 214 const std::vector<Technology::Identifier> default_technology_order_; 215 Manager manager_; 216 }; 217 218 } // namespace shill 219 220 #endif // SHILL_PROPERTY_STORE_UNITTEST_H_ 221