1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <libsnapshot/test_helpers.h>
16 
17 #include <sys/statvfs.h>
18 
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <android-base/parsebool.h>
22 #include <android-base/properties.h>
23 #include <android-base/strings.h>
24 #include <android-base/unique_fd.h>
25 #include <gtest/gtest.h>
26 #include <liblp/property_fetcher.h>
27 #include <openssl/sha.h>
28 #include <payload_consumer/file_descriptor.h>
29 
30 namespace android {
31 namespace snapshot {
32 
33 using android::base::ReadFully;
34 using android::base::unique_fd;
35 using android::base::WriteFully;
36 using android::fiemap::IImageManager;
37 using testing::AssertionFailure;
38 using testing::AssertionSuccess;
39 
DeleteBackingImage(IImageManager * manager,const std::string & name)40 void DeleteBackingImage(IImageManager* manager, const std::string& name) {
41     if (manager->IsImageMapped(name)) {
42         ASSERT_TRUE(manager->UnmapImageDevice(name));
43     }
44     if (manager->BackingImageExists(name)) {
45         ASSERT_TRUE(manager->DeleteBackingImage(name));
46     }
47 }
48 
Open(const std::string & partition_name,int flags) const49 android::base::unique_fd TestPartitionOpener::Open(const std::string& partition_name,
50                                                    int flags) const {
51     if (partition_name == "super") {
52         return PartitionOpener::Open(fake_super_path_, flags);
53     }
54     return PartitionOpener::Open(partition_name, flags);
55 }
56 
GetInfo(const std::string & partition_name,android::fs_mgr::BlockDeviceInfo * info) const57 bool TestPartitionOpener::GetInfo(const std::string& partition_name,
58                                   android::fs_mgr::BlockDeviceInfo* info) const {
59     if (partition_name != "super") {
60         return PartitionOpener::GetInfo(partition_name, info);
61     }
62 
63     if (PartitionOpener::GetInfo(fake_super_path_, info)) {
64         // SnapshotUpdateTest uses a relatively small super partition, which requires a small
65         // alignment and 0 offset to work. For the purpose of this test, hardcode the alignment
66         // and offset. This test isn't about testing liblp or libdm.
67         info->alignment_offset = 0;
68         info->alignment = std::min<uint32_t>(info->alignment, static_cast<uint32_t>(128_KiB));
69         return true;
70     }
71     return false;
72 }
73 
GetDeviceString(const std::string & partition_name) const74 std::string TestPartitionOpener::GetDeviceString(const std::string& partition_name) const {
75     if (partition_name == "super") {
76         return fake_super_path_;
77     }
78     return PartitionOpener::GetDeviceString(partition_name);
79 }
80 
ToHexString(const uint8_t * buf,size_t len)81 std::string ToHexString(const uint8_t* buf, size_t len) {
82     char lookup[] = "0123456789abcdef";
83     std::string out(len * 2 + 1, '\0');
84     char* outp = out.data();
85     for (; len > 0; len--, buf++) {
86         *outp++ = (char)lookup[*buf >> 4];
87         *outp++ = (char)lookup[*buf & 0xf];
88     }
89     return out;
90 }
91 
WriteRandomData(const std::string & path,std::optional<size_t> expect_size,std::string * hash)92 bool WriteRandomData(const std::string& path, std::optional<size_t> expect_size,
93                      std::string* hash) {
94     unique_fd rand(open("/dev/urandom", O_RDONLY));
95     unique_fd fd(open(path.c_str(), O_WRONLY));
96 
97     SHA256_CTX ctx;
98     if (hash) {
99         SHA256_Init(&ctx);
100     }
101 
102     char buf[4096];
103     size_t total_written = 0;
104     while (!expect_size || total_written < *expect_size) {
105         ssize_t n = TEMP_FAILURE_RETRY(read(rand.get(), buf, sizeof(buf)));
106         if (n <= 0) return false;
107         if (!WriteFully(fd.get(), buf, n)) {
108             if (errno == ENOSPC) {
109                 break;
110             }
111             PLOG(ERROR) << "Cannot write " << path;
112             return false;
113         }
114         total_written += n;
115         if (hash) {
116             SHA256_Update(&ctx, buf, n);
117         }
118     }
119 
120     if (expect_size && total_written != *expect_size) {
121         PLOG(ERROR) << "Written " << total_written << " bytes, expected " << *expect_size;
122         return false;
123     }
124 
125     if (hash) {
126         uint8_t out[32];
127         SHA256_Final(out, &ctx);
128         *hash = ToHexString(out, sizeof(out));
129     }
130     return true;
131 }
132 
HashSnapshot(ISnapshotWriter * writer)133 std::string HashSnapshot(ISnapshotWriter* writer) {
134     auto reader = writer->OpenReader();
135     if (!reader) {
136         return {};
137     }
138 
139     SHA256_CTX ctx;
140     SHA256_Init(&ctx);
141 
142     uint64_t remaining = reader->BlockDevSize();
143     char buffer[4096];
144     while (remaining) {
145         size_t to_read =
146                 static_cast<size_t>(std::min(remaining, static_cast<uint64_t>(sizeof(buffer))));
147         ssize_t read = reader->Read(&buffer, to_read);
148         if (read <= 0) {
149             if (read < 0) {
150                 LOG(ERROR) << "Failed to read from snapshot writer";
151                 return {};
152             }
153             break;
154         }
155         SHA256_Update(&ctx, buffer, to_read);
156         remaining -= static_cast<size_t>(read);
157     }
158 
159     uint8_t out[32];
160     SHA256_Final(out, &ctx);
161     return ToHexString(out, sizeof(out));
162 }
163 
GetHash(const std::string & path)164 std::optional<std::string> GetHash(const std::string& path) {
165     std::string content;
166     if (!android::base::ReadFileToString(path, &content, true)) {
167         PLOG(ERROR) << "Cannot access " << path;
168         return std::nullopt;
169     }
170     SHA256_CTX ctx;
171     SHA256_Init(&ctx);
172     SHA256_Update(&ctx, content.c_str(), content.size());
173     uint8_t out[32];
174     SHA256_Final(out, &ctx);
175     return ToHexString(out, sizeof(out));
176 }
177 
FillFakeMetadata(MetadataBuilder * builder,const DeltaArchiveManifest & manifest,const std::string & suffix)178 AssertionResult FillFakeMetadata(MetadataBuilder* builder, const DeltaArchiveManifest& manifest,
179                                  const std::string& suffix) {
180     for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
181         if (!builder->AddGroup(group.name() + suffix, group.size())) {
182             return AssertionFailure()
183                    << "Cannot add group " << group.name() << " with size " << group.size();
184         }
185         for (const auto& partition_name : group.partition_names()) {
186             auto p = builder->AddPartition(partition_name + suffix, group.name() + suffix,
187                                            0 /* attr */);
188             if (!p) {
189                 return AssertionFailure() << "Cannot add partition " << partition_name + suffix
190                                           << " to group " << group.name() << suffix;
191             }
192         }
193     }
194     for (const auto& partition : manifest.partitions()) {
195         auto p = builder->FindPartition(partition.partition_name() + suffix);
196         if (!p) {
197             return AssertionFailure() << "Cannot resize partition " << partition.partition_name()
198                                       << suffix << "; it is not found.";
199         }
200         if (!builder->ResizePartition(p, partition.new_partition_info().size())) {
201             return AssertionFailure()
202                    << "Cannot resize partition " << partition.partition_name() << suffix
203                    << " to size " << partition.new_partition_info().size();
204         }
205     }
206     return AssertionSuccess();
207 }
208 
SetSize(PartitionUpdate * partition_update,uint64_t size)209 void SetSize(PartitionUpdate* partition_update, uint64_t size) {
210     partition_update->mutable_new_partition_info()->set_size(size);
211 }
212 
GetSize(PartitionUpdate * partition_update)213 uint64_t GetSize(PartitionUpdate* partition_update) {
214     return partition_update->mutable_new_partition_info()->size();
215 }
216 
Init(uint64_t max_free_space)217 AssertionResult LowSpaceUserdata::Init(uint64_t max_free_space) {
218     auto res = ReadUserdataStats();
219     if (!res) return res;
220 
221     // Try to fill up the disk as much as possible until free_space_ <= max_free_space.
222     big_file_ = std::make_unique<TemporaryFile>();
223     if (big_file_->fd == -1) {
224         return AssertionFailure() << strerror(errno);
225     }
226     if (!android::base::StartsWith(big_file_->path, kUserDataDevice)) {
227         return AssertionFailure() << "Temp file allocated to " << big_file_->path << ", not in "
228                                   << kUserDataDevice;
229     }
230     uint64_t next_consume = std::min(std::max(available_space_, max_free_space) - max_free_space,
231                                      (uint64_t)std::numeric_limits<off_t>::max());
232     off_t allocated = 0;
233     while (next_consume > 0 && free_space_ > max_free_space) {
234         int status = fallocate(big_file_->fd, 0, allocated, next_consume);
235         if (status == -1 && errno == ENOSPC) {
236             next_consume /= 2;
237             continue;
238         }
239         if (status == -1) {
240             return AssertionFailure() << strerror(errno);
241         }
242         allocated += next_consume;
243 
244         res = ReadUserdataStats();
245         if (!res) return res;
246     }
247 
248     LOG(INFO) << allocated << " bytes allocated to " << big_file_->path;
249     initialized_ = true;
250     return AssertionSuccess();
251 }
252 
ReadUserdataStats()253 AssertionResult LowSpaceUserdata::ReadUserdataStats() {
254     struct statvfs buf;
255     if (statvfs(kUserDataDevice, &buf) == -1) {
256         return AssertionFailure() << strerror(errno);
257     }
258     bsize_ = buf.f_bsize;
259     free_space_ = bsize_ * buf.f_bfree;
260     available_space_ = bsize_ * buf.f_bavail;
261     return AssertionSuccess();
262 }
263 
free_space() const264 uint64_t LowSpaceUserdata::free_space() const {
265     CHECK(initialized_);
266     return free_space_;
267 }
268 
available_space() const269 uint64_t LowSpaceUserdata::available_space() const {
270     CHECK(initialized_);
271     return available_space_;
272 }
273 
bsize() const274 uint64_t LowSpaceUserdata::bsize() const {
275     CHECK(initialized_);
276     return bsize_;
277 }
278 
IsVirtualAbEnabled()279 bool IsVirtualAbEnabled() {
280     return android::base::GetBoolProperty("ro.virtual_ab.enabled", false);
281 }
282 
SnapshotTestPropertyFetcher(const std::string & slot_suffix,std::unordered_map<std::string,std::string> && props)283 SnapshotTestPropertyFetcher::SnapshotTestPropertyFetcher(
284         const std::string& slot_suffix, std::unordered_map<std::string, std::string>&& props)
285     : properties_(std::move(props)) {
286     properties_["ro.boot.slot_suffix"] = slot_suffix;
287     properties_["ro.boot.dynamic_partitions"] = "true";
288     properties_["ro.boot.dynamic_partitions_retrofit"] = "false";
289     properties_["ro.virtual_ab.enabled"] = "true";
290 }
291 
GetProperty(const std::string & key,const std::string & defaultValue)292 std::string SnapshotTestPropertyFetcher::GetProperty(const std::string& key,
293                                                      const std::string& defaultValue) {
294     auto iter = properties_.find(key);
295     if (iter == properties_.end()) {
296         return android::base::GetProperty(key, defaultValue);
297     }
298     return iter->second;
299 }
300 
GetBoolProperty(const std::string & key,bool defaultValue)301 bool SnapshotTestPropertyFetcher::GetBoolProperty(const std::string& key, bool defaultValue) {
302     auto iter = properties_.find(key);
303     if (iter == properties_.end()) {
304         return android::base::GetBoolProperty(key, defaultValue);
305     }
306     switch (android::base::ParseBool(iter->second)) {
307         case android::base::ParseBoolResult::kTrue:
308             return true;
309         case android::base::ParseBoolResult::kFalse:
310             return false;
311         default:
312             return defaultValue;
313     }
314 }
315 
316 }  // namespace snapshot
317 }  // namespace android
318