• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/properties.h>
22 #include <android-base/strings.h>
23 #include <android-base/unique_fd.h>
24 #include <gtest/gtest.h>
25 #include <openssl/sha.h>
26 #include <payload_consumer/file_descriptor.h>
27 
28 namespace android {
29 namespace snapshot {
30 
31 using android::base::ReadFully;
32 using android::base::unique_fd;
33 using android::base::WriteFully;
34 using android::fiemap::IImageManager;
35 using testing::AssertionFailure;
36 using testing::AssertionSuccess;
37 
DeleteBackingImage(IImageManager * manager,const std::string & name)38 void DeleteBackingImage(IImageManager* manager, const std::string& name) {
39     if (manager->IsImageMapped(name)) {
40         ASSERT_TRUE(manager->UnmapImageDevice(name));
41     }
42     if (manager->BackingImageExists(name)) {
43         ASSERT_TRUE(manager->DeleteBackingImage(name));
44     }
45 }
46 
Open(const std::string & partition_name,int flags) const47 android::base::unique_fd TestPartitionOpener::Open(const std::string& partition_name,
48                                                    int flags) const {
49     if (partition_name == "super") {
50         return PartitionOpener::Open(fake_super_path_, flags);
51     }
52     return PartitionOpener::Open(partition_name, flags);
53 }
54 
GetInfo(const std::string & partition_name,android::fs_mgr::BlockDeviceInfo * info) const55 bool TestPartitionOpener::GetInfo(const std::string& partition_name,
56                                   android::fs_mgr::BlockDeviceInfo* info) const {
57     if (partition_name != "super") {
58         return PartitionOpener::GetInfo(partition_name, info);
59     }
60 
61     if (PartitionOpener::GetInfo(fake_super_path_, info)) {
62         // SnapshotUpdateTest uses a relatively small super partition, which requires a small
63         // alignment and 0 offset to work. For the purpose of this test, hardcode the alignment
64         // and offset. This test isn't about testing liblp or libdm.
65         info->alignment_offset = 0;
66         info->alignment = std::min<uint32_t>(info->alignment, static_cast<uint32_t>(128_KiB));
67         return true;
68     }
69     return false;
70 }
71 
GetDeviceString(const std::string & partition_name) const72 std::string TestPartitionOpener::GetDeviceString(const std::string& partition_name) const {
73     if (partition_name == "super") {
74         return fake_super_path_;
75     }
76     return PartitionOpener::GetDeviceString(partition_name);
77 }
78 
ToHexString(const uint8_t * buf,size_t len)79 std::string ToHexString(const uint8_t* buf, size_t len) {
80     char lookup[] = "0123456789abcdef";
81     std::string out(len * 2 + 1, '\0');
82     char* outp = out.data();
83     for (; len > 0; len--, buf++) {
84         *outp++ = (char)lookup[*buf >> 4];
85         *outp++ = (char)lookup[*buf & 0xf];
86     }
87     return out;
88 }
89 
WriteRandomData(const std::string & path,std::optional<size_t> expect_size,std::string * hash)90 bool WriteRandomData(const std::string& path, std::optional<size_t> expect_size,
91                      std::string* hash) {
92     unique_fd rand(open("/dev/urandom", O_RDONLY));
93     unique_fd fd(open(path.c_str(), O_WRONLY));
94 
95     SHA256_CTX ctx;
96     if (hash) {
97         SHA256_Init(&ctx);
98     }
99 
100     char buf[4096];
101     size_t total_written = 0;
102     while (!expect_size || total_written < *expect_size) {
103         ssize_t n = TEMP_FAILURE_RETRY(read(rand.get(), buf, sizeof(buf)));
104         if (n <= 0) return false;
105         if (!WriteFully(fd.get(), buf, n)) {
106             if (errno == ENOSPC) {
107                 break;
108             }
109             PLOG(ERROR) << "Cannot write " << path;
110             return false;
111         }
112         total_written += n;
113         if (hash) {
114             SHA256_Update(&ctx, buf, n);
115         }
116     }
117 
118     if (expect_size && total_written != *expect_size) {
119         PLOG(ERROR) << "Written " << total_written << " bytes, expected " << *expect_size;
120         return false;
121     }
122 
123     if (hash) {
124         uint8_t out[32];
125         SHA256_Final(out, &ctx);
126         *hash = ToHexString(out, sizeof(out));
127     }
128     return true;
129 }
130 
WriteRandomData(ICowWriter * writer,std::string * hash)131 bool WriteRandomData(ICowWriter* writer, std::string* hash) {
132     unique_fd rand(open("/dev/urandom", O_RDONLY));
133     if (rand < 0) {
134         PLOG(ERROR) << "open /dev/urandom";
135         return false;
136     }
137 
138     SHA256_CTX ctx;
139     if (hash) {
140         SHA256_Init(&ctx);
141     }
142 
143     if (!writer->options().max_blocks) {
144         LOG(ERROR) << "CowWriter must specify maximum number of blocks";
145         return false;
146     }
147     uint64_t num_blocks = writer->options().max_blocks.value();
148 
149     size_t block_size = writer->options().block_size;
150     std::string block(block_size, '\0');
151     for (uint64_t i = 0; i < num_blocks; i++) {
152         if (!ReadFully(rand, block.data(), block.size())) {
153             PLOG(ERROR) << "read /dev/urandom";
154             return false;
155         }
156         if (!writer->AddRawBlocks(i, block.data(), block.size())) {
157             LOG(ERROR) << "Failed to add raw block " << i;
158             return false;
159         }
160         if (hash) {
161             SHA256_Update(&ctx, block.data(), block.size());
162         }
163     }
164 
165     if (hash) {
166         uint8_t out[32];
167         SHA256_Final(out, &ctx);
168         *hash = ToHexString(out, sizeof(out));
169     }
170     return true;
171 }
172 
HashSnapshot(ISnapshotWriter * writer)173 std::string HashSnapshot(ISnapshotWriter* writer) {
174     auto reader = writer->OpenReader();
175     if (!reader) {
176         return {};
177     }
178 
179     SHA256_CTX ctx;
180     SHA256_Init(&ctx);
181 
182     uint64_t remaining = reader->BlockDevSize();
183     char buffer[4096];
184     while (remaining) {
185         size_t to_read =
186                 static_cast<size_t>(std::min(remaining, static_cast<uint64_t>(sizeof(buffer))));
187         ssize_t read = reader->Read(&buffer, to_read);
188         if (read <= 0) {
189             if (read < 0) {
190                 LOG(ERROR) << "Failed to read from snapshot writer";
191                 return {};
192             }
193             break;
194         }
195         SHA256_Update(&ctx, buffer, to_read);
196         remaining -= static_cast<size_t>(read);
197     }
198 
199     uint8_t out[32];
200     SHA256_Final(out, &ctx);
201     return ToHexString(out, sizeof(out));
202 }
203 
GetHash(const std::string & path)204 std::optional<std::string> GetHash(const std::string& path) {
205     std::string content;
206     if (!android::base::ReadFileToString(path, &content, true)) {
207         PLOG(ERROR) << "Cannot access " << path;
208         return std::nullopt;
209     }
210     SHA256_CTX ctx;
211     SHA256_Init(&ctx);
212     SHA256_Update(&ctx, content.c_str(), content.size());
213     uint8_t out[32];
214     SHA256_Final(out, &ctx);
215     return ToHexString(out, sizeof(out));
216 }
217 
FillFakeMetadata(MetadataBuilder * builder,const DeltaArchiveManifest & manifest,const std::string & suffix)218 AssertionResult FillFakeMetadata(MetadataBuilder* builder, const DeltaArchiveManifest& manifest,
219                                  const std::string& suffix) {
220     for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
221         if (!builder->AddGroup(group.name() + suffix, group.size())) {
222             return AssertionFailure()
223                    << "Cannot add group " << group.name() << " with size " << group.size();
224         }
225         for (const auto& partition_name : group.partition_names()) {
226             auto p = builder->AddPartition(partition_name + suffix, group.name() + suffix,
227                                            0 /* attr */);
228             if (!p) {
229                 return AssertionFailure() << "Cannot add partition " << partition_name + suffix
230                                           << " to group " << group.name() << suffix;
231             }
232         }
233     }
234     for (const auto& partition : manifest.partitions()) {
235         auto p = builder->FindPartition(partition.partition_name() + suffix);
236         if (!p) {
237             return AssertionFailure() << "Cannot resize partition " << partition.partition_name()
238                                       << suffix << "; it is not found.";
239         }
240         if (!builder->ResizePartition(p, partition.new_partition_info().size())) {
241             return AssertionFailure()
242                    << "Cannot resize partition " << partition.partition_name() << suffix
243                    << " to size " << partition.new_partition_info().size();
244         }
245     }
246     return AssertionSuccess();
247 }
248 
SetSize(PartitionUpdate * partition_update,uint64_t size)249 void SetSize(PartitionUpdate* partition_update, uint64_t size) {
250     partition_update->mutable_new_partition_info()->set_size(size);
251 }
252 
GetSize(PartitionUpdate * partition_update)253 uint64_t GetSize(PartitionUpdate* partition_update) {
254     return partition_update->mutable_new_partition_info()->size();
255 }
256 
Init(uint64_t max_free_space)257 AssertionResult LowSpaceUserdata::Init(uint64_t max_free_space) {
258     auto res = ReadUserdataStats();
259     if (!res) return res;
260 
261     // Try to fill up the disk as much as possible until free_space_ <= max_free_space.
262     big_file_ = std::make_unique<TemporaryFile>();
263     if (big_file_->fd == -1) {
264         return AssertionFailure() << strerror(errno);
265     }
266     if (!android::base::StartsWith(big_file_->path, kUserDataDevice)) {
267         return AssertionFailure() << "Temp file allocated to " << big_file_->path << ", not in "
268                                   << kUserDataDevice;
269     }
270     uint64_t next_consume =
271             std::min(free_space_ - max_free_space, (uint64_t)std::numeric_limits<off_t>::max());
272     off_t allocated = 0;
273     while (next_consume > 0 && free_space_ > max_free_space) {
274         int status = fallocate(big_file_->fd, 0, allocated, next_consume);
275         if (status == -1 && errno == ENOSPC) {
276             next_consume /= 2;
277             continue;
278         }
279         if (status == -1) {
280             return AssertionFailure() << strerror(errno);
281         }
282         allocated += next_consume;
283 
284         res = ReadUserdataStats();
285         if (!res) return res;
286     }
287 
288     LOG(INFO) << allocated << " bytes allocated to " << big_file_->path;
289     initialized_ = true;
290     return AssertionSuccess();
291 }
292 
ReadUserdataStats()293 AssertionResult LowSpaceUserdata::ReadUserdataStats() {
294     struct statvfs buf;
295     if (statvfs(kUserDataDevice, &buf) == -1) {
296         return AssertionFailure() << strerror(errno);
297     }
298     bsize_ = buf.f_bsize;
299     free_space_ = bsize_ * buf.f_bfree;
300     available_space_ = bsize_ * buf.f_bavail;
301     return AssertionSuccess();
302 }
303 
free_space() const304 uint64_t LowSpaceUserdata::free_space() const {
305     CHECK(initialized_);
306     return free_space_;
307 }
308 
available_space() const309 uint64_t LowSpaceUserdata::available_space() const {
310     CHECK(initialized_);
311     return available_space_;
312 }
313 
bsize() const314 uint64_t LowSpaceUserdata::bsize() const {
315     CHECK(initialized_);
316     return bsize_;
317 }
318 
IsVirtualAbEnabled()319 bool IsVirtualAbEnabled() {
320     return android::base::GetBoolProperty("ro.virtual_ab.enabled", false);
321 }
322 
323 }  // namespace snapshot
324 }  // namespace android
325