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 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/fake_prefs.h" |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 18 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 23 | using std::string; |
Jae Hoon Kim | 29a80e0 | 2020-05-11 20:18:49 -0700 | [diff] [blame] | 24 | using std::vector; |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 25 | |
| 26 | using chromeos_update_engine::FakePrefs; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | void CheckNotNull(const string& key, void* ptr) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 31 | EXPECT_NE(nullptr, ptr) << "Called Get*() for key \"" << key |
| 32 | << "\" with a null parameter."; |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 39 | FakePrefs::~FakePrefs() { |
| 40 | EXPECT_TRUE(observers_.empty()); |
| 41 | } |
| 42 | |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 43 | // Compile-time type-dependent constants definitions. |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 44 | template <> |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 45 | FakePrefs::PrefType const FakePrefs::PrefConsts<string>::type = |
| 46 | FakePrefs::PrefType::kString; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 47 | template <> |
| 48 | string FakePrefs::PrefValue::*const // NOLINT(runtime/string), not static str. |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 49 | FakePrefs::PrefConsts<string>::member = &FakePrefs::PrefValue::as_str; |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 50 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 51 | template <> |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 52 | FakePrefs::PrefType const FakePrefs::PrefConsts<int64_t>::type = |
| 53 | FakePrefs::PrefType::kInt64; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 54 | template <> |
| 55 | int64_t FakePrefs::PrefValue::*const FakePrefs::PrefConsts<int64_t>::member = |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 56 | &FakePrefs::PrefValue::as_int64; |
| 57 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 58 | template <> |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 59 | FakePrefs::PrefType const FakePrefs::PrefConsts<bool>::type = |
| 60 | FakePrefs::PrefType::kBool; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 61 | template <> |
| 62 | bool FakePrefs::PrefValue::*const FakePrefs::PrefConsts<bool>::member = |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 63 | &FakePrefs::PrefValue::as_bool; |
| 64 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 65 | bool FakePrefs::GetString(const string& key, string* value) const { |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 66 | return GetValue(key, value); |
| 67 | } |
| 68 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 69 | bool FakePrefs::SetString(const string& key, const string& value) { |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 70 | SetValue(key, value); |
| 71 | return true; |
| 72 | } |
| 73 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 74 | bool FakePrefs::GetInt64(const string& key, int64_t* value) const { |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 75 | return GetValue(key, value); |
| 76 | } |
| 77 | |
| 78 | bool FakePrefs::SetInt64(const string& key, const int64_t value) { |
| 79 | SetValue(key, value); |
| 80 | return true; |
| 81 | } |
| 82 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 83 | bool FakePrefs::GetBoolean(const string& key, bool* value) const { |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 84 | return GetValue(key, value); |
| 85 | } |
| 86 | |
| 87 | bool FakePrefs::SetBoolean(const string& key, const bool value) { |
| 88 | SetValue(key, value); |
| 89 | return true; |
| 90 | } |
| 91 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 92 | bool FakePrefs::Exists(const string& key) const { |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 93 | return values_.find(key) != values_.end(); |
| 94 | } |
| 95 | |
| 96 | bool FakePrefs::Delete(const string& key) { |
| 97 | if (values_.find(key) == values_.end()) |
| 98 | return false; |
| 99 | values_.erase(key); |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 100 | const auto observers_for_key = observers_.find(key); |
| 101 | if (observers_for_key != observers_.end()) { |
| 102 | std::vector<ObserverInterface*> copy_observers(observers_for_key->second); |
| 103 | for (ObserverInterface* observer : copy_observers) |
| 104 | observer->OnPrefDeleted(key); |
| 105 | } |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
Vyshu Khota | 4c5413d | 2020-11-04 16:17:25 -0800 | [diff] [blame^] | 109 | bool FakePrefs::Delete(const string& key, const vector<string>& nss) { |
| 110 | bool success = Delete(key); |
| 111 | for (const auto& ns : nss) { |
| 112 | vector<string> ns_keys; |
| 113 | success = GetSubKeys(ns, &ns_keys) && success; |
| 114 | for (const auto& sub_key : ns_keys) { |
| 115 | auto last_key_seperator = sub_key.find_last_of(kKeySeparator); |
| 116 | if (last_key_seperator != string::npos && |
| 117 | key == sub_key.substr(last_key_seperator + 1)) { |
| 118 | success = Delete(sub_key) && success; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | return success; |
| 123 | } |
| 124 | |
Jae Hoon Kim | 29a80e0 | 2020-05-11 20:18:49 -0700 | [diff] [blame] | 125 | bool FakePrefs::GetSubKeys(const string& ns, vector<string>* keys) const { |
| 126 | for (const auto& pr : values_) |
| 127 | if (pr.first.compare(0, ns.length(), ns) == 0) |
| 128 | keys->push_back(pr.first); |
| 129 | return true; |
| 130 | } |
| 131 | |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 132 | string FakePrefs::GetTypeName(PrefType type) { |
| 133 | switch (type) { |
| 134 | case PrefType::kString: |
| 135 | return "string"; |
| 136 | case PrefType::kInt64: |
| 137 | return "int64_t"; |
| 138 | case PrefType::kBool: |
| 139 | return "bool"; |
| 140 | } |
| 141 | return "Unknown"; |
| 142 | } |
| 143 | |
| 144 | void FakePrefs::CheckKeyType(const string& key, PrefType type) const { |
| 145 | auto it = values_.find(key); |
| 146 | EXPECT_TRUE(it == values_.end() || it->second.type == type) |
| 147 | << "Key \"" << key << "\" if defined as " << GetTypeName(it->second.type) |
| 148 | << " but is accessed as a " << GetTypeName(type); |
| 149 | } |
| 150 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 151 | template <typename T> |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 152 | void FakePrefs::SetValue(const string& key, const T& value) { |
| 153 | CheckKeyType(key, PrefConsts<T>::type); |
| 154 | values_[key].type = PrefConsts<T>::type; |
| 155 | values_[key].value.*(PrefConsts<T>::member) = value; |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 156 | const auto observers_for_key = observers_.find(key); |
| 157 | if (observers_for_key != observers_.end()) { |
| 158 | std::vector<ObserverInterface*> copy_observers(observers_for_key->second); |
| 159 | for (ObserverInterface* observer : copy_observers) |
| 160 | observer->OnPrefSet(key); |
| 161 | } |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 164 | template <typename T> |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 165 | bool FakePrefs::GetValue(const string& key, T* value) const { |
| 166 | CheckKeyType(key, PrefConsts<T>::type); |
| 167 | auto it = values_.find(key); |
| 168 | if (it == values_.end()) |
| 169 | return false; |
| 170 | CheckNotNull(key, value); |
| 171 | *value = it->second.value.*(PrefConsts<T>::member); |
| 172 | return true; |
| 173 | } |
| 174 | |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 175 | void FakePrefs::AddObserver(const string& key, ObserverInterface* observer) { |
| 176 | observers_[key].push_back(observer); |
| 177 | } |
| 178 | |
| 179 | void FakePrefs::RemoveObserver(const string& key, ObserverInterface* observer) { |
| 180 | std::vector<ObserverInterface*>& observers_for_key = observers_[key]; |
| 181 | auto observer_it = |
| 182 | std::find(observers_for_key.begin(), observers_for_key.end(), observer); |
| 183 | EXPECT_NE(observer_it, observers_for_key.end()) |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 184 | << "Trying to remove an observer instance not watching the key " << key; |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 185 | if (observer_it != observers_for_key.end()) |
| 186 | observers_for_key.erase(observer_it); |
| 187 | if (observers_for_key.empty()) |
| 188 | observers_.erase(key); |
| 189 | } |
| 190 | |
Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 191 | } // namespace chromeos_update_engine |