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