Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 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 | // |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/fake_prefs.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 21 | using std::string; |
| 22 | |
| 23 | using chromeos_update_engine::FakePrefs; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | void CheckNotNull(const string& key, void* ptr) { |
| 28 | EXPECT_NE(nullptr, ptr) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 29 | << "Called Get*() for key \"" << key << "\" with a null parameter."; |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | namespace chromeos_update_engine { |
| 35 | |
| 36 | // Compile-time type-dependent constants definitions. |
| 37 | template<> |
| 38 | FakePrefs::PrefType const FakePrefs::PrefConsts<string>::type = |
| 39 | FakePrefs::PrefType::kString; |
| 40 | template<> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 41 | string FakePrefs::PrefValue::* const // NOLINT(runtime/string), not static str. |
| 42 | FakePrefs::PrefConsts<string>::member = &FakePrefs::PrefValue::as_str; |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 43 | |
| 44 | template<> |
| 45 | FakePrefs::PrefType const FakePrefs::PrefConsts<int64_t>::type = |
| 46 | FakePrefs::PrefType::kInt64; |
| 47 | template<> |
| 48 | int64_t FakePrefs::PrefValue::* const FakePrefs::PrefConsts<int64_t>::member = |
| 49 | &FakePrefs::PrefValue::as_int64; |
| 50 | |
| 51 | template<> |
| 52 | FakePrefs::PrefType const FakePrefs::PrefConsts<bool>::type = |
| 53 | FakePrefs::PrefType::kBool; |
| 54 | template<> |
| 55 | bool FakePrefs::PrefValue::* const FakePrefs::PrefConsts<bool>::member = |
| 56 | &FakePrefs::PrefValue::as_bool; |
| 57 | |
| 58 | |
| 59 | bool FakePrefs::GetString(const string& key, string* value) { |
| 60 | return GetValue(key, value); |
| 61 | } |
| 62 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 63 | bool FakePrefs::SetString(const string& key, const string& value) { |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 64 | SetValue(key, value); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool FakePrefs::GetInt64(const string& key, int64_t* value) { |
| 69 | return GetValue(key, value); |
| 70 | } |
| 71 | |
| 72 | bool FakePrefs::SetInt64(const string& key, const int64_t value) { |
| 73 | SetValue(key, value); |
| 74 | return true; |
| 75 | } |
| 76 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 77 | bool FakePrefs::GetBoolean(const string& key, bool* value) { |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 78 | return GetValue(key, value); |
| 79 | } |
| 80 | |
| 81 | bool FakePrefs::SetBoolean(const string& key, const bool value) { |
| 82 | SetValue(key, value); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | bool FakePrefs::Exists(const string& key) { |
| 87 | return values_.find(key) != values_.end(); |
| 88 | } |
| 89 | |
| 90 | bool FakePrefs::Delete(const string& key) { |
| 91 | if (values_.find(key) == values_.end()) |
| 92 | return false; |
| 93 | values_.erase(key); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | string FakePrefs::GetTypeName(PrefType type) { |
| 98 | switch (type) { |
| 99 | case PrefType::kString: |
| 100 | return "string"; |
| 101 | case PrefType::kInt64: |
| 102 | return "int64_t"; |
| 103 | case PrefType::kBool: |
| 104 | return "bool"; |
| 105 | } |
| 106 | return "Unknown"; |
| 107 | } |
| 108 | |
| 109 | void FakePrefs::CheckKeyType(const string& key, PrefType type) const { |
| 110 | auto it = values_.find(key); |
| 111 | EXPECT_TRUE(it == values_.end() || it->second.type == type) |
| 112 | << "Key \"" << key << "\" if defined as " << GetTypeName(it->second.type) |
| 113 | << " but is accessed as a " << GetTypeName(type); |
| 114 | } |
| 115 | |
| 116 | template<typename T> |
| 117 | void FakePrefs::SetValue(const string& key, const T& value) { |
| 118 | CheckKeyType(key, PrefConsts<T>::type); |
| 119 | values_[key].type = PrefConsts<T>::type; |
| 120 | values_[key].value.*(PrefConsts<T>::member) = value; |
| 121 | } |
| 122 | |
| 123 | template<typename T> |
| 124 | bool FakePrefs::GetValue(const string& key, T* value) const { |
| 125 | CheckKeyType(key, PrefConsts<T>::type); |
| 126 | auto it = values_.find(key); |
| 127 | if (it == values_.end()) |
| 128 | return false; |
| 129 | CheckNotNull(key, value); |
| 130 | *value = it->second.value.*(PrefConsts<T>::member); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | } // namespace chromeos_update_engine |