Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2019 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 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_TEST_UTILS_H_ |
| 18 | #define UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_TEST_UTILS_H_ |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <map> |
| 24 | #include <memory> |
| 25 | #include <string> |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 26 | |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 27 | #include <fs_mgr.h> |
| 28 | #include <gmock/gmock.h> |
| 29 | #include <gtest/gtest.h> |
| 30 | #include <liblp/builder.h> |
Yifan Hong | 87ea73f | 2019-09-12 13:07:37 -0700 | [diff] [blame] | 31 | #include <storage_literals/storage_literals.h> |
Kelvin Zhang | 0c18424 | 2024-10-25 11:19:27 -0700 | [diff] [blame] | 32 | #include <android-base/strings.h> |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 33 | |
| 34 | #include "update_engine/common/boot_control_interface.h" |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 35 | #include "update_engine/update_metadata.pb.h" |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
| 39 | using android::fs_mgr::MetadataBuilder; |
| 40 | using testing::_; |
| 41 | using testing::MakeMatcher; |
| 42 | using testing::Matcher; |
| 43 | using testing::MatcherInterface; |
| 44 | using testing::MatchResultListener; |
Yifan Hong | 87ea73f | 2019-09-12 13:07:37 -0700 | [diff] [blame] | 45 | using namespace android::storage_literals; // NOLINT(build/namespaces) |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 46 | |
| 47 | constexpr const uint32_t kMaxNumSlots = 2; |
| 48 | constexpr const char* kSlotSuffixes[kMaxNumSlots] = {"_a", "_b"}; |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 49 | constexpr std::string_view kFakeDevicePath = "/fake/dev/path/"; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 50 | constexpr const char* kFakeDmDevicePath = "/fake/dm/dev/path/"; |
| 51 | constexpr const uint32_t kFakeMetadataSize = 65536; |
| 52 | constexpr const char* kDefaultGroup = "foo"; |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 53 | constexpr const char* kFakeSuper = "fake_super"; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 54 | |
| 55 | // A map describing the size of each partition. |
| 56 | // "{name, size}" |
| 57 | using PartitionSizes = std::map<std::string, uint64_t>; |
| 58 | |
| 59 | // "{name_a, size}" |
| 60 | using PartitionSuffixSizes = std::map<std::string, uint64_t>; |
| 61 | |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 62 | constexpr uint64_t kDefaultGroupSize = 5_GiB; |
| 63 | // Super device size. 1 MiB for metadata. |
| 64 | constexpr uint64_t kDefaultSuperSize = kDefaultGroupSize * 2 + 1_MiB; |
| 65 | |
| 66 | template <typename U, typename V> |
| 67 | inline std::ostream& operator<<(std::ostream& os, const std::map<U, V>& param) { |
| 68 | os << "{"; |
| 69 | bool first = true; |
| 70 | for (const auto& pair : param) { |
| 71 | if (!first) |
| 72 | os << ", "; |
| 73 | os << pair.first << ":" << pair.second; |
| 74 | first = false; |
| 75 | } |
| 76 | return os << "}"; |
| 77 | } |
| 78 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 79 | template <typename V> |
| 80 | inline void VectorToStream(std::ostream& os, const V& param) { |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 81 | os << "["; |
| 82 | bool first = true; |
| 83 | for (const auto& e : param) { |
| 84 | if (!first) |
| 85 | os << ", "; |
| 86 | os << e; |
| 87 | first = false; |
| 88 | } |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 89 | os << "]"; |
| 90 | } |
| 91 | |
| 92 | inline std::ostream& operator<<(std::ostream& os, const PartitionUpdate& p) { |
| 93 | return os << "{" << p.partition_name() << ", " |
| 94 | << p.new_partition_info().size() << "}"; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | inline std::ostream& operator<<(std::ostream& os, |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 98 | const DynamicPartitionGroup& g) { |
| 99 | os << "{" << g.name() << ", " << g.size() << ", "; |
| 100 | VectorToStream(os, g.partition_names()); |
| 101 | return os << "}"; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | inline std::ostream& operator<<(std::ostream& os, |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 105 | const DeltaArchiveManifest& m) { |
| 106 | os << "{.groups = "; |
| 107 | VectorToStream(os, m.dynamic_partition_metadata().groups()); |
| 108 | os << ", .partitions = "; |
| 109 | VectorToStream(os, m.partitions()); |
| 110 | return os; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | inline std::string GetDevice(const std::string& name) { |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 114 | return std::string(kFakeDevicePath) + name; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | inline std::string GetDmDevice(const std::string& name) { |
| 118 | return kFakeDmDevicePath + name; |
| 119 | } |
| 120 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 121 | inline DynamicPartitionGroup* AddGroup(DeltaArchiveManifest* manifest, |
| 122 | const std::string& group, |
| 123 | uint64_t group_size) { |
| 124 | auto* g = manifest->mutable_dynamic_partition_metadata()->add_groups(); |
| 125 | g->set_name(group); |
| 126 | g->set_size(group_size); |
| 127 | return g; |
| 128 | } |
| 129 | |
| 130 | inline void AddPartition(DeltaArchiveManifest* manifest, |
| 131 | DynamicPartitionGroup* group, |
| 132 | const std::string& partition, |
| 133 | uint64_t partition_size) { |
| 134 | group->add_partition_names(partition); |
| 135 | auto* p = manifest->add_partitions(); |
| 136 | p->set_partition_name(partition); |
| 137 | p->mutable_new_partition_info()->set_size(partition_size); |
| 138 | } |
| 139 | |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 140 | // To support legacy tests, auto-convert {name_a: size} map to |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 141 | // DeltaArchiveManifest. |
| 142 | inline DeltaArchiveManifest PartitionSuffixSizesToManifest( |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 143 | const PartitionSuffixSizes& partition_sizes) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 144 | DeltaArchiveManifest manifest; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 145 | for (const char* suffix : kSlotSuffixes) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 146 | AddGroup(&manifest, std::string(kDefaultGroup) + suffix, kDefaultGroupSize); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 147 | } |
| 148 | for (const auto& pair : partition_sizes) { |
| 149 | for (size_t suffix_idx = 0; suffix_idx < kMaxNumSlots; ++suffix_idx) { |
Kelvin Zhang | 0c18424 | 2024-10-25 11:19:27 -0700 | [diff] [blame] | 150 | if (android::base::EndsWith(pair.first, kSlotSuffixes[suffix_idx])) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 151 | AddPartition( |
| 152 | &manifest, |
| 153 | manifest.mutable_dynamic_partition_metadata()->mutable_groups( |
| 154 | suffix_idx), |
| 155 | pair.first, |
| 156 | pair.second); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 160 | return manifest; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // To support legacy tests, auto-convert {name: size} map to PartitionMetadata. |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 164 | inline DeltaArchiveManifest PartitionSizesToManifest( |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 165 | const PartitionSizes& partition_sizes) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 166 | DeltaArchiveManifest manifest; |
| 167 | auto* g = AddGroup(&manifest, std::string(kDefaultGroup), kDefaultGroupSize); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 168 | for (const auto& pair : partition_sizes) { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 169 | AddPartition(&manifest, g, pair.first, pair.second); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 170 | } |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 171 | return manifest; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | inline std::unique_ptr<MetadataBuilder> NewFakeMetadata( |
Yifan Hong | 8d6df9a | 2020-08-13 13:59:54 -0700 | [diff] [blame] | 175 | const DeltaArchiveManifest& manifest, |
| 176 | uint32_t partition_attr = 0, |
| 177 | uint64_t super_size = kDefaultSuperSize) { |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 178 | auto builder = |
Yifan Hong | 8d6df9a | 2020-08-13 13:59:54 -0700 | [diff] [blame] | 179 | MetadataBuilder::New(super_size, kFakeMetadataSize, kMaxNumSlots); |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 180 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { |
| 181 | EXPECT_TRUE(builder->AddGroup(group.name(), group.size())); |
| 182 | for (const auto& partition_name : group.partition_names()) { |
| 183 | EXPECT_NE( |
| 184 | nullptr, |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 185 | builder->AddPartition(partition_name, group.name(), partition_attr)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 188 | for (const auto& partition : manifest.partitions()) { |
| 189 | auto p = builder->FindPartition(partition.partition_name()); |
| 190 | EXPECT_TRUE(p && builder->ResizePartition( |
| 191 | p, partition.new_partition_info().size())); |
| 192 | } |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 193 | return builder; |
| 194 | } |
| 195 | |
| 196 | class MetadataMatcher : public MatcherInterface<MetadataBuilder*> { |
| 197 | public: |
| 198 | explicit MetadataMatcher(const PartitionSuffixSizes& partition_sizes) |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 199 | : manifest_(PartitionSuffixSizesToManifest(partition_sizes)) {} |
| 200 | explicit MetadataMatcher(const DeltaArchiveManifest& manifest) |
| 201 | : manifest_(manifest) {} |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 202 | |
| 203 | bool MatchAndExplain(MetadataBuilder* metadata, |
| 204 | MatchResultListener* listener) const override { |
| 205 | bool success = true; |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 206 | for (const auto& group : manifest_.dynamic_partition_metadata().groups()) { |
| 207 | for (const auto& partition_name : group.partition_names()) { |
| 208 | auto p = metadata->FindPartition(partition_name); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 209 | if (p == nullptr) { |
| 210 | if (!success) |
| 211 | *listener << "; "; |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 212 | *listener << "No partition " << partition_name; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 213 | success = false; |
| 214 | continue; |
| 215 | } |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 216 | const auto& partition_updates = manifest_.partitions(); |
| 217 | auto it = std::find_if(partition_updates.begin(), |
| 218 | partition_updates.end(), |
| 219 | [&](const auto& p) { |
| 220 | return p.partition_name() == partition_name; |
| 221 | }); |
| 222 | if (it == partition_updates.end()) { |
| 223 | *listener << "Can't find partition update " << partition_name; |
| 224 | success = false; |
| 225 | continue; |
| 226 | } |
| 227 | auto partition_size = it->new_partition_info().size(); |
| 228 | if (p->size() != partition_size) { |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 229 | if (!success) |
| 230 | *listener << "; "; |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 231 | *listener << "Partition " << partition_name << " has size " |
| 232 | << p->size() << ", expected " << partition_size; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 233 | success = false; |
| 234 | } |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 235 | if (p->group_name() != group.name()) { |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 236 | if (!success) |
| 237 | *listener << "; "; |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 238 | *listener << "Partition " << partition_name << " has group " |
| 239 | << p->group_name() << ", expected " << group.name(); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 240 | success = false; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | return success; |
| 245 | } |
| 246 | |
| 247 | void DescribeTo(std::ostream* os) const override { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 248 | *os << "expect: " << manifest_; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void DescribeNegationTo(std::ostream* os) const override { |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 252 | *os << "expect not: " << manifest_; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | private: |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 256 | DeltaArchiveManifest manifest_; |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | inline Matcher<MetadataBuilder*> MetadataMatches( |
| 260 | const PartitionSuffixSizes& partition_sizes) { |
| 261 | return MakeMatcher(new MetadataMatcher(partition_sizes)); |
| 262 | } |
| 263 | |
| 264 | inline Matcher<MetadataBuilder*> MetadataMatches( |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 265 | const DeltaArchiveManifest& manifest) { |
| 266 | return MakeMatcher(new MetadataMatcher(manifest)); |
Yifan Hong | c049f93 | 2019-07-23 15:06:05 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | MATCHER_P(HasGroup, group, " has group " + group) { |
| 270 | auto groups = arg->ListGroups(); |
| 271 | return std::find(groups.begin(), groups.end(), group) != groups.end(); |
| 272 | } |
| 273 | |
| 274 | struct TestParam { |
| 275 | uint32_t source; |
| 276 | uint32_t target; |
| 277 | }; |
| 278 | inline std::ostream& operator<<(std::ostream& os, const TestParam& param) { |
| 279 | return os << "{source: " << param.source << ", target:" << param.target |
| 280 | << "}"; |
| 281 | } |
| 282 | |
| 283 | } // namespace chromeos_update_engine |
| 284 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 285 | #endif // UPDATE_ENGINE_AOSP_DYNAMIC_PARTITION_TEST_UTILS_H_ |