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(ICowWriter::FileDescriptor * reader)133 std::string HashSnapshot(ICowWriter::FileDescriptor* reader) {
134 SHA256_CTX ctx;
135 SHA256_Init(&ctx);
136
137 uint64_t remaining = reader->BlockDevSize();
138 char buffer[4096];
139 while (remaining) {
140 size_t to_read =
141 static_cast<size_t>(std::min(remaining, static_cast<uint64_t>(sizeof(buffer))));
142 ssize_t read = reader->Read(&buffer, to_read);
143 if (read <= 0) {
144 if (read < 0) {
145 LOG(ERROR) << "Failed to read from snapshot writer";
146 return {};
147 }
148 break;
149 }
150 SHA256_Update(&ctx, buffer, to_read);
151 remaining -= static_cast<size_t>(read);
152 }
153
154 uint8_t out[32];
155 SHA256_Final(out, &ctx);
156 return ToHexString(out, sizeof(out));
157 }
158
GetHash(const std::string & path)159 std::optional<std::string> GetHash(const std::string& path) {
160 std::string content;
161 if (!android::base::ReadFileToString(path, &content, true)) {
162 PLOG(ERROR) << "Cannot access " << path;
163 return std::nullopt;
164 }
165 SHA256_CTX ctx;
166 SHA256_Init(&ctx);
167 SHA256_Update(&ctx, content.c_str(), content.size());
168 uint8_t out[32];
169 SHA256_Final(out, &ctx);
170 return ToHexString(out, sizeof(out));
171 }
172
FillFakeMetadata(MetadataBuilder * builder,const DeltaArchiveManifest & manifest,const std::string & suffix)173 AssertionResult FillFakeMetadata(MetadataBuilder* builder, const DeltaArchiveManifest& manifest,
174 const std::string& suffix) {
175 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
176 if (!builder->AddGroup(group.name() + suffix, group.size())) {
177 return AssertionFailure()
178 << "Cannot add group " << group.name() << " with size " << group.size();
179 }
180 for (const auto& partition_name : group.partition_names()) {
181 auto p = builder->AddPartition(partition_name + suffix, group.name() + suffix,
182 0 /* attr */);
183 if (!p) {
184 return AssertionFailure() << "Cannot add partition " << partition_name + suffix
185 << " to group " << group.name() << suffix;
186 }
187 }
188 }
189 for (const auto& partition : manifest.partitions()) {
190 auto p = builder->FindPartition(partition.partition_name() + suffix);
191 if (!p) {
192 return AssertionFailure() << "Cannot resize partition " << partition.partition_name()
193 << suffix << "; it is not found.";
194 }
195 if (!builder->ResizePartition(p, partition.new_partition_info().size())) {
196 return AssertionFailure()
197 << "Cannot resize partition " << partition.partition_name() << suffix
198 << " to size " << partition.new_partition_info().size();
199 }
200 }
201 return AssertionSuccess();
202 }
203
SetSize(PartitionUpdate * partition_update,uint64_t size)204 void SetSize(PartitionUpdate* partition_update, uint64_t size) {
205 partition_update->mutable_new_partition_info()->set_size(size);
206 }
207
GetSize(PartitionUpdate * partition_update)208 uint64_t GetSize(PartitionUpdate* partition_update) {
209 return partition_update->mutable_new_partition_info()->size();
210 }
211
IsVirtualAbEnabled()212 bool IsVirtualAbEnabled() {
213 return android::base::GetBoolProperty("ro.virtual_ab.enabled", false);
214 }
215
SnapshotTestPropertyFetcher(const std::string & slot_suffix,std::unordered_map<std::string,std::string> && props)216 SnapshotTestPropertyFetcher::SnapshotTestPropertyFetcher(
217 const std::string& slot_suffix, std::unordered_map<std::string, std::string>&& props)
218 : properties_(std::move(props)) {
219 properties_["ro.boot.slot_suffix"] = slot_suffix;
220 properties_["ro.boot.dynamic_partitions"] = "true";
221 properties_["ro.boot.dynamic_partitions_retrofit"] = "false";
222 properties_["ro.virtual_ab.enabled"] = "true";
223 }
224
GetProperty(const std::string & key,const std::string & defaultValue)225 std::string SnapshotTestPropertyFetcher::GetProperty(const std::string& key,
226 const std::string& defaultValue) {
227 auto iter = properties_.find(key);
228 if (iter == properties_.end()) {
229 return android::base::GetProperty(key, defaultValue);
230 }
231 return iter->second;
232 }
233
GetBoolProperty(const std::string & key,bool defaultValue)234 bool SnapshotTestPropertyFetcher::GetBoolProperty(const std::string& key, bool defaultValue) {
235 auto iter = properties_.find(key);
236 if (iter == properties_.end()) {
237 return android::base::GetBoolProperty(key, defaultValue);
238 }
239 switch (android::base::ParseBool(iter->second)) {
240 case android::base::ParseBoolResult::kTrue:
241 return true;
242 case android::base::ParseBoolResult::kFalse:
243 return false;
244 default:
245 return defaultValue;
246 }
247 }
248
249 } // namespace snapshot
250 } // namespace android
251