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/strings.h>
22 #include <android-base/unique_fd.h>
23 #include <gtest/gtest.h>
24 #include <openssl/sha.h>
25 
26 namespace android {
27 namespace snapshot {
28 
29 using android::base::ReadFully;
30 using android::base::unique_fd;
31 using android::base::WriteFully;
32 using android::fiemap::IImageManager;
33 using testing::AssertionFailure;
34 using testing::AssertionSuccess;
35 
DeleteBackingImage(IImageManager * manager,const std::string & name)36 void DeleteBackingImage(IImageManager* manager, const std::string& name) {
37     if (manager->IsImageMapped(name)) {
38         ASSERT_TRUE(manager->UnmapImageDevice(name));
39     }
40     if (manager->BackingImageExists(name)) {
41         ASSERT_TRUE(manager->DeleteBackingImage(name));
42     }
43 }
44 
Open(const std::string & partition_name,int flags) const45 android::base::unique_fd TestPartitionOpener::Open(const std::string& partition_name,
46                                                    int flags) const {
47     if (partition_name == "super") {
48         return PartitionOpener::Open(fake_super_path_, flags);
49     }
50     return PartitionOpener::Open(partition_name, flags);
51 }
52 
GetInfo(const std::string & partition_name,android::fs_mgr::BlockDeviceInfo * info) const53 bool TestPartitionOpener::GetInfo(const std::string& partition_name,
54                                   android::fs_mgr::BlockDeviceInfo* info) const {
55     if (partition_name != "super") {
56         return PartitionOpener::GetInfo(partition_name, info);
57     }
58 
59     if (PartitionOpener::GetInfo(fake_super_path_, info)) {
60         // SnapshotUpdateTest uses a relatively small super partition, which requires a small
61         // alignment and 0 offset to work. For the purpose of this test, hardcode the alignment
62         // and offset. This test isn't about testing liblp or libdm.
63         info->alignment_offset = 0;
64         info->alignment = std::min<uint32_t>(info->alignment, static_cast<uint32_t>(128_KiB));
65         return true;
66     }
67     return false;
68 }
69 
GetDeviceString(const std::string & partition_name) const70 std::string TestPartitionOpener::GetDeviceString(const std::string& partition_name) const {
71     if (partition_name == "super") {
72         return fake_super_path_;
73     }
74     return PartitionOpener::GetDeviceString(partition_name);
75 }
76 
ToHexString(const uint8_t * buf,size_t len)77 std::string ToHexString(const uint8_t* buf, size_t len) {
78     char lookup[] = "0123456789abcdef";
79     std::string out(len * 2 + 1, '\0');
80     char* outp = out.data();
81     for (; len > 0; len--, buf++) {
82         *outp++ = (char)lookup[*buf >> 4];
83         *outp++ = (char)lookup[*buf & 0xf];
84     }
85     return out;
86 }
87 
WriteRandomData(const std::string & path,std::optional<size_t> expect_size,std::string * hash)88 bool WriteRandomData(const std::string& path, std::optional<size_t> expect_size,
89                      std::string* hash) {
90     unique_fd rand(open("/dev/urandom", O_RDONLY));
91     unique_fd fd(open(path.c_str(), O_WRONLY));
92 
93     SHA256_CTX ctx;
94     if (hash) {
95         SHA256_Init(&ctx);
96     }
97 
98     char buf[4096];
99     size_t total_written = 0;
100     while (!expect_size || total_written < *expect_size) {
101         ssize_t n = TEMP_FAILURE_RETRY(read(rand.get(), buf, sizeof(buf)));
102         if (n <= 0) return false;
103         if (!WriteFully(fd.get(), buf, n)) {
104             if (errno == ENOSPC) {
105                 break;
106             }
107             PLOG(ERROR) << "Cannot write " << path;
108             return false;
109         }
110         total_written += n;
111         if (hash) {
112             SHA256_Update(&ctx, buf, n);
113         }
114     }
115 
116     if (expect_size && total_written != *expect_size) {
117         PLOG(ERROR) << "Written " << total_written << " bytes, expected " << *expect_size;
118         return false;
119     }
120 
121     if (hash) {
122         uint8_t out[32];
123         SHA256_Final(out, &ctx);
124         *hash = ToHexString(out, sizeof(out));
125     }
126     return true;
127 }
128 
GetHash(const std::string & path)129 std::optional<std::string> GetHash(const std::string& path) {
130     std::string content;
131     if (!android::base::ReadFileToString(path, &content, true)) {
132         PLOG(ERROR) << "Cannot access " << path;
133         return std::nullopt;
134     }
135     SHA256_CTX ctx;
136     SHA256_Init(&ctx);
137     SHA256_Update(&ctx, content.c_str(), content.size());
138     uint8_t out[32];
139     SHA256_Final(out, &ctx);
140     return ToHexString(out, sizeof(out));
141 }
142 
FillFakeMetadata(MetadataBuilder * builder,const DeltaArchiveManifest & manifest,const std::string & suffix)143 AssertionResult FillFakeMetadata(MetadataBuilder* builder, const DeltaArchiveManifest& manifest,
144                                  const std::string& suffix) {
145     for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
146         if (!builder->AddGroup(group.name() + suffix, group.size())) {
147             return AssertionFailure()
148                    << "Cannot add group " << group.name() << " with size " << group.size();
149         }
150         for (const auto& partition_name : group.partition_names()) {
151             auto p = builder->AddPartition(partition_name + suffix, group.name() + suffix,
152                                            0 /* attr */);
153             if (!p) {
154                 return AssertionFailure() << "Cannot add partition " << partition_name + suffix
155                                           << " to group " << group.name() << suffix;
156             }
157         }
158     }
159     for (const auto& partition : manifest.partitions()) {
160         auto p = builder->FindPartition(partition.partition_name() + suffix);
161         if (!p) {
162             return AssertionFailure() << "Cannot resize partition " << partition.partition_name()
163                                       << suffix << "; it is not found.";
164         }
165         if (!builder->ResizePartition(p, partition.new_partition_info().size())) {
166             return AssertionFailure()
167                    << "Cannot resize partition " << partition.partition_name() << suffix
168                    << " to size " << partition.new_partition_info().size();
169         }
170     }
171     return AssertionSuccess();
172 }
173 
SetSize(PartitionUpdate * partition_update,uint64_t size)174 void SetSize(PartitionUpdate* partition_update, uint64_t size) {
175     partition_update->mutable_new_partition_info()->set_size(size);
176 }
177 
GetSize(PartitionUpdate * partition_update)178 uint64_t GetSize(PartitionUpdate* partition_update) {
179     return partition_update->mutable_new_partition_info()->size();
180 }
181 
Init(uint64_t max_free_space)182 AssertionResult LowSpaceUserdata::Init(uint64_t max_free_space) {
183     auto res = ReadUserdataStats();
184     if (!res) return res;
185 
186     // Try to fill up the disk as much as possible until free_space_ <= max_free_space.
187     big_file_ = std::make_unique<TemporaryFile>();
188     if (big_file_->fd == -1) {
189         return AssertionFailure() << strerror(errno);
190     }
191     if (!android::base::StartsWith(big_file_->path, kUserDataDevice)) {
192         return AssertionFailure() << "Temp file allocated to " << big_file_->path << ", not in "
193                                   << kUserDataDevice;
194     }
195     uint64_t next_consume =
196             std::min(free_space_ - max_free_space, (uint64_t)std::numeric_limits<off_t>::max());
197     off_t allocated = 0;
198     while (next_consume > 0 && free_space_ > max_free_space) {
199         int status = fallocate(big_file_->fd, 0, allocated, next_consume);
200         if (status == -1 && errno == ENOSPC) {
201             next_consume /= 2;
202             continue;
203         }
204         if (status == -1) {
205             return AssertionFailure() << strerror(errno);
206         }
207         allocated += next_consume;
208 
209         res = ReadUserdataStats();
210         if (!res) return res;
211     }
212 
213     LOG(INFO) << allocated << " bytes allocated to " << big_file_->path;
214     initialized_ = true;
215     return AssertionSuccess();
216 }
217 
ReadUserdataStats()218 AssertionResult LowSpaceUserdata::ReadUserdataStats() {
219     struct statvfs buf;
220     if (statvfs(kUserDataDevice, &buf) == -1) {
221         return AssertionFailure() << strerror(errno);
222     }
223     bsize_ = buf.f_bsize;
224     free_space_ = bsize_ * buf.f_bfree;
225     available_space_ = bsize_ * buf.f_bavail;
226     return AssertionSuccess();
227 }
228 
free_space() const229 uint64_t LowSpaceUserdata::free_space() const {
230     CHECK(initialized_);
231     return free_space_;
232 }
233 
available_space() const234 uint64_t LowSpaceUserdata::available_space() const {
235     CHECK(initialized_);
236     return available_space_;
237 }
238 
bsize() const239 uint64_t LowSpaceUserdata::bsize() const {
240     CHECK(initialized_);
241     return bsize_;
242 }
243 
244 }  // namespace snapshot
245 }  // namespace android
246