• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
17 #include <fcntl.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/utsname.h>
23 #include <time.h>
24 #include <unistd.h>
25 
26 #include <chrono>
27 #include <ctime>
28 #include <iostream>
29 #include <map>
30 #include <thread>
31 
32 #include <android-base/file.h>
33 #include <android-base/logging.h>
34 #include <android-base/scopeguard.h>
35 #include <android-base/strings.h>
36 #include <android-base/unique_fd.h>
37 #include <gtest/gtest.h>
38 #include <libdm/dm.h>
39 #include <libdm/loop_control.h>
40 #include "test_util.h"
41 #include "utility.h"
42 
43 using namespace std;
44 using namespace std::chrono_literals;
45 using namespace android::dm;
46 using android::base::make_scope_guard;
47 using android::base::unique_fd;
48 
49 class DmTest : public ::testing::Test {
50   protected:
SetUp()51     void SetUp() override {
52         const testing::TestInfo* const test_info =
53                 testing::UnitTest::GetInstance()->current_test_info();
54         test_name_ = test_info->name();
55         test_full_name_ = test_info->test_suite_name() + "/"s + test_name_;
56 
57         LOG(INFO) << "Starting test: " << test_full_name_;
58     }
TearDown()59     void TearDown() override {
60         LOG(INFO) << "Tearing down test: " << test_full_name_;
61 
62         auto& dm = DeviceMapper::Instance();
63         ASSERT_TRUE(dm.DeleteDeviceIfExists(test_name_));
64 
65         LOG(INFO) << "Teardown complete for test: " << test_full_name_;
66     }
67 
68     std::string test_name_;
69     std::string test_full_name_;
70 };
71 
TEST_F(DmTest,HasMinimumTargets)72 TEST_F(DmTest, HasMinimumTargets) {
73     DmTargetTypeInfo info;
74 
75     DeviceMapper& dm = DeviceMapper::Instance();
76     ASSERT_TRUE(dm.GetTargetByName("linear", &info));
77 }
78 
TEST_F(DmTest,DmLinear)79 TEST_F(DmTest, DmLinear) {
80     unique_fd tmp1(CreateTempFile("file_1", 4096));
81     ASSERT_GE(tmp1, 0);
82     unique_fd tmp2(CreateTempFile("file_2", 4096));
83     ASSERT_GE(tmp2, 0);
84 
85     // Create two different files. These will back two separate loop devices.
86     const char message1[] = "Hello! This is sector 1.";
87     const char message2[] = "Goodbye. This is sector 2.";
88     ASSERT_TRUE(android::base::WriteFully(tmp1, message1, sizeof(message1)));
89     ASSERT_TRUE(android::base::WriteFully(tmp2, message2, sizeof(message2)));
90 
91     LoopDevice loop_a(tmp1, 10s);
92     ASSERT_TRUE(loop_a.valid());
93     LoopDevice loop_b(tmp2, 10s);
94     ASSERT_TRUE(loop_b.valid());
95 
96     // Define a 2-sector device, with each sector mapping to the first sector
97     // of one of our loop devices.
98     DmTable table;
99     ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop_a.device(), 0));
100     ASSERT_TRUE(table.Emplace<DmTargetLinear>(1, 1, loop_b.device(), 0));
101     ASSERT_TRUE(table.valid());
102     ASSERT_EQ(2u, table.num_sectors());
103 
104     TempDevice dev("libdm-test-dm-linear", table);
105     ASSERT_TRUE(dev.valid());
106     ASSERT_FALSE(dev.path().empty());
107 
108     auto& dm = DeviceMapper::Instance();
109 
110     dev_t dev_number;
111     ASSERT_TRUE(dm.GetDeviceNumber(dev.name(), &dev_number));
112     ASSERT_NE(dev_number, 0);
113 
114     std::string dev_string;
115     ASSERT_TRUE(dm.GetDeviceString(dev.name(), &dev_string));
116     ASSERT_FALSE(dev_string.empty());
117 
118     // Note: a scope is needed to ensure that there are no open descriptors
119     // when we go to close the device.
120     {
121         unique_fd dev_fd(open(dev.path().c_str(), O_RDWR));
122         ASSERT_GE(dev_fd, 0);
123 
124         // Test that each sector of our device is correctly mapped to each loop
125         // device.
126         char sector[512];
127         ASSERT_TRUE(android::base::ReadFully(dev_fd, sector, sizeof(sector)));
128         ASSERT_EQ(strncmp(sector, message1, sizeof(message1)), 0);
129         ASSERT_TRUE(android::base::ReadFully(dev_fd, sector, sizeof(sector)));
130         ASSERT_EQ(strncmp(sector, message2, sizeof(message2)), 0);
131     }
132 
133     // Test GetTableStatus.
134     vector<DeviceMapper::TargetInfo> targets;
135     ASSERT_TRUE(dm.GetTableStatus(dev.name(), &targets));
136     ASSERT_EQ(targets.size(), 2);
137     EXPECT_EQ(strcmp(targets[0].spec.target_type, "linear"), 0);
138     EXPECT_TRUE(targets[0].data.empty());
139     EXPECT_EQ(targets[0].spec.sector_start, 0);
140     EXPECT_EQ(targets[0].spec.length, 1);
141     EXPECT_EQ(strcmp(targets[1].spec.target_type, "linear"), 0);
142     EXPECT_TRUE(targets[1].data.empty());
143     EXPECT_EQ(targets[1].spec.sector_start, 1);
144     EXPECT_EQ(targets[1].spec.length, 1);
145 
146     // Test GetTargetType().
147     EXPECT_EQ(DeviceMapper::GetTargetType(targets[0].spec), std::string{"linear"});
148     EXPECT_EQ(DeviceMapper::GetTargetType(targets[1].spec), std::string{"linear"});
149 
150     // Normally the TestDevice destructor would delete this, but at least one
151     // test should ensure that device deletion works.
152     ASSERT_TRUE(dev.Destroy());
153 }
154 
TEST_F(DmTest,DmSuspendResume)155 TEST_F(DmTest, DmSuspendResume) {
156     unique_fd tmp1(CreateTempFile("file_suspend_resume", 512));
157     ASSERT_GE(tmp1, 0);
158 
159     LoopDevice loop_a(tmp1, 10s);
160     ASSERT_TRUE(loop_a.valid());
161 
162     DmTable table;
163     ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop_a.device(), 0));
164     ASSERT_TRUE(table.valid());
165     ASSERT_EQ(1u, table.num_sectors());
166 
167     TempDevice dev("libdm-test-dm-suspend-resume", table);
168     ASSERT_TRUE(dev.valid());
169     ASSERT_FALSE(dev.path().empty());
170 
171     auto& dm = DeviceMapper::Instance();
172 
173     // Test Set and Get status of device.
174     vector<DeviceMapper::TargetInfo> targets;
175     ASSERT_EQ(dm.GetState(dev.name()), DmDeviceState::ACTIVE);
176 
177     ASSERT_TRUE(dm.ChangeState(dev.name(), DmDeviceState::SUSPENDED));
178     ASSERT_EQ(dm.GetState(dev.name()), DmDeviceState::SUSPENDED);
179 
180     ASSERT_TRUE(dm.ChangeState(dev.name(), DmDeviceState::ACTIVE));
181     ASSERT_EQ(dm.GetState(dev.name()), DmDeviceState::ACTIVE);
182 }
183 
TEST_F(DmTest,DmVerityArgsAvb2)184 TEST_F(DmTest, DmVerityArgsAvb2) {
185     std::string device = "/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a";
186     std::string algorithm = "sha1";
187     std::string digest = "4be7e823b8c40f7bd5c8ccd5123f0722c5baca21";
188     std::string salt = "cc99f81ecb9484220a003b0719ee59dcf9be7e5d";
189 
190     DmTargetVerity target(0, 10000, 1, device, device, 4096, 4096, 125961, 125961, algorithm,
191                           digest, salt);
192     target.UseFec(device, 2, 126955, 126955);
193     target.SetVerityMode("restart_on_corruption");
194     target.IgnoreZeroBlocks();
195 
196     // Verity table from a walleye build.
197     std::string expected =
198             "1 /dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a "
199             "/dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a 4096 4096 125961 125961 sha1 "
200             "4be7e823b8c40f7bd5c8ccd5123f0722c5baca21 cc99f81ecb9484220a003b0719ee59dcf9be7e5d 10 "
201             "use_fec_from_device /dev/block/platform/soc/1da4000.ufshc/by-name/vendor_a fec_roots "
202             "2 fec_blocks 126955 fec_start 126955 restart_on_corruption ignore_zero_blocks";
203     EXPECT_EQ(target.GetParameterString(), expected);
204 }
205 
TEST_F(DmTest,DmSnapshotArgs)206 TEST_F(DmTest, DmSnapshotArgs) {
207     DmTargetSnapshot target1(0, 512, "base", "cow", SnapshotStorageMode::Persistent, 8);
208     if (DmTargetSnapshot::ReportsOverflow("snapshot")) {
209         EXPECT_EQ(target1.GetParameterString(), "base cow PO 8");
210     } else {
211         EXPECT_EQ(target1.GetParameterString(), "base cow P 8");
212     }
213     EXPECT_EQ(target1.name(), "snapshot");
214 
215     DmTargetSnapshot target2(0, 512, "base", "cow", SnapshotStorageMode::Transient, 8);
216     EXPECT_EQ(target2.GetParameterString(), "base cow N 8");
217     EXPECT_EQ(target2.name(), "snapshot");
218 
219     DmTargetSnapshot target3(0, 512, "base", "cow", SnapshotStorageMode::Merge, 8);
220     if (DmTargetSnapshot::ReportsOverflow("snapshot-merge")) {
221         EXPECT_EQ(target3.GetParameterString(), "base cow PO 8");
222     } else {
223         EXPECT_EQ(target3.GetParameterString(), "base cow P 8");
224     }
225     EXPECT_EQ(target3.name(), "snapshot-merge");
226 }
227 
TEST_F(DmTest,DmSnapshotOriginArgs)228 TEST_F(DmTest, DmSnapshotOriginArgs) {
229     DmTargetSnapshotOrigin target(0, 512, "base");
230     EXPECT_EQ(target.GetParameterString(), "base");
231     EXPECT_EQ(target.name(), "snapshot-origin");
232 }
233 
234 class SnapshotTestHarness final {
235   public:
236     bool Setup();
237     bool Merge();
238 
origin_dev() const239     std::string origin_dev() const { return origin_dev_->path(); }
snapshot_dev() const240     std::string snapshot_dev() const { return snapshot_dev_->path(); }
241 
base_fd() const242     int base_fd() const { return base_fd_; }
243 
244     static const uint64_t kBaseDeviceSize = 1024 * 1024;
245     static const uint64_t kCowDeviceSize = 1024 * 64;
246     static const uint64_t kSectorSize = 512;
247 
248   private:
249     void SetupImpl();
250     void MergeImpl();
251 
252     unique_fd base_fd_;
253     unique_fd cow_fd_;
254     unique_ptr<LoopDevice> base_loop_;
255     unique_ptr<LoopDevice> cow_loop_;
256     unique_ptr<TempDevice> origin_dev_;
257     unique_ptr<TempDevice> snapshot_dev_;
258     bool setup_ok_ = false;
259     bool merge_ok_ = false;
260 };
261 
Setup()262 bool SnapshotTestHarness::Setup() {
263     SetupImpl();
264     return setup_ok_;
265 }
266 
SetupImpl()267 void SnapshotTestHarness::SetupImpl() {
268     base_fd_ = CreateTempFile("base_device", kBaseDeviceSize);
269     ASSERT_GE(base_fd_, 0);
270     cow_fd_ = CreateTempFile("cow_device", kCowDeviceSize);
271     ASSERT_GE(cow_fd_, 0);
272 
273     base_loop_ = std::make_unique<LoopDevice>(base_fd_, 10s);
274     ASSERT_TRUE(base_loop_->valid());
275     cow_loop_ = std::make_unique<LoopDevice>(cow_fd_, 10s);
276     ASSERT_TRUE(cow_loop_->valid());
277 
278     DmTable origin_table;
279     ASSERT_TRUE(origin_table.AddTarget(make_unique<DmTargetSnapshotOrigin>(
280             0, kBaseDeviceSize / kSectorSize, base_loop_->device())));
281     ASSERT_TRUE(origin_table.valid());
282     ASSERT_EQ(kBaseDeviceSize / kSectorSize, origin_table.num_sectors());
283 
284     origin_dev_ = std::make_unique<TempDevice>("libdm-test-dm-snapshot-origin", origin_table);
285     ASSERT_TRUE(origin_dev_->valid());
286     ASSERT_FALSE(origin_dev_->path().empty());
287 
288     // chunk size = 4K blocks.
289     DmTable snap_table;
290     ASSERT_TRUE(snap_table.AddTarget(make_unique<DmTargetSnapshot>(
291             0, kBaseDeviceSize / kSectorSize, base_loop_->device(), cow_loop_->device(),
292             SnapshotStorageMode::Persistent, 8)));
293     ASSERT_TRUE(snap_table.valid());
294     ASSERT_EQ(kBaseDeviceSize / kSectorSize, snap_table.num_sectors());
295 
296     snapshot_dev_ = std::make_unique<TempDevice>("libdm-test-dm-snapshot", snap_table);
297     ASSERT_TRUE(snapshot_dev_->valid());
298     ASSERT_FALSE(snapshot_dev_->path().empty());
299 
300     setup_ok_ = true;
301 }
302 
Merge()303 bool SnapshotTestHarness::Merge() {
304     MergeImpl();
305     return merge_ok_;
306 }
307 
MergeImpl()308 void SnapshotTestHarness::MergeImpl() {
309     DmTable merge_table;
310     ASSERT_TRUE(merge_table.AddTarget(
311             make_unique<DmTargetSnapshot>(0, kBaseDeviceSize / kSectorSize, base_loop_->device(),
312                                           cow_loop_->device(), SnapshotStorageMode::Merge, 8)));
313     ASSERT_TRUE(merge_table.valid());
314     ASSERT_EQ(kBaseDeviceSize / kSectorSize, merge_table.num_sectors());
315 
316     DeviceMapper& dm = DeviceMapper::Instance();
317     ASSERT_TRUE(dm.LoadTableAndActivate("libdm-test-dm-snapshot", merge_table));
318 
319     while (true) {
320         vector<DeviceMapper::TargetInfo> status;
321         ASSERT_TRUE(dm.GetTableStatus("libdm-test-dm-snapshot", &status));
322         ASSERT_EQ(status.size(), 1);
323         ASSERT_EQ(strncmp(status[0].spec.target_type, "snapshot-merge", strlen("snapshot-merge")),
324                   0);
325 
326         DmTargetSnapshot::Status merge_status;
327         ASSERT_TRUE(DmTargetSnapshot::ParseStatusText(status[0].data, &merge_status));
328         ASSERT_TRUE(merge_status.error.empty());
329         if (merge_status.sectors_allocated == merge_status.metadata_sectors) {
330             break;
331         }
332 
333         std::this_thread::sleep_for(250ms);
334     }
335 
336     merge_ok_ = true;
337 }
338 
CheckSnapshotAvailability()339 bool CheckSnapshotAvailability() {
340     DmTargetTypeInfo info;
341 
342     DeviceMapper& dm = DeviceMapper::Instance();
343     if (!dm.GetTargetByName("snapshot", &info)) {
344         cout << "snapshot module not enabled; skipping test" << std::endl;
345         return false;
346     }
347     if (!dm.GetTargetByName("snapshot-merge", &info)) {
348         cout << "snapshot-merge module not enabled; skipping test" << std::endl;
349         return false;
350     }
351     if (!dm.GetTargetByName("snapshot-origin", &info)) {
352         cout << "snapshot-origin module not enabled; skipping test" << std::endl;
353         return false;
354     }
355     return true;
356 }
357 
TEST_F(DmTest,DmSnapshot)358 TEST_F(DmTest, DmSnapshot) {
359     if (!CheckSnapshotAvailability()) {
360         return;
361     }
362 
363     SnapshotTestHarness harness;
364     ASSERT_TRUE(harness.Setup());
365 
366     // Open the dm devices.
367     unique_fd origin_fd(open(harness.origin_dev().c_str(), O_RDONLY | O_CLOEXEC));
368     ASSERT_GE(origin_fd, 0);
369     unique_fd snapshot_fd(open(harness.snapshot_dev().c_str(), O_RDWR | O_CLOEXEC | O_SYNC));
370     ASSERT_GE(snapshot_fd, 0);
371 
372     // Write to the first block of the snapshot device.
373     std::string data("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
374     ASSERT_TRUE(android::base::WriteFully(snapshot_fd, data.data(), data.size()));
375     ASSERT_EQ(lseek(snapshot_fd, 0, SEEK_SET), 0);
376 
377     // We should get the same data back from the snapshot device.
378     std::string read(data.size(), '\0');
379     ASSERT_TRUE(android::base::ReadFully(snapshot_fd, read.data(), read.size()));
380     ASSERT_EQ(read, data);
381 
382     // We should see the original data from the origin device.
383     std::string zeroes(data.size(), '\0');
384     ASSERT_TRUE(android::base::ReadFully(origin_fd, read.data(), read.size()));
385     ASSERT_EQ(lseek(snapshot_fd, 0, SEEK_SET), 0);
386     ASSERT_EQ(read, zeroes);
387 
388     // We should also see the original data from the base device.
389     ASSERT_TRUE(android::base::ReadFully(harness.base_fd(), read.data(), read.size()));
390     ASSERT_EQ(lseek(harness.base_fd(), 0, SEEK_SET), 0);
391     ASSERT_EQ(read, zeroes);
392 
393     // Now, perform the merge and wait.
394     ASSERT_TRUE(harness.Merge());
395 
396     // Reading from the base device should give us the modified data.
397     ASSERT_TRUE(android::base::ReadFully(harness.base_fd(), read.data(), read.size()));
398     ASSERT_EQ(lseek(harness.base_fd(), 0, SEEK_SET), 0);
399     ASSERT_EQ(read, data);
400 }
401 
TEST_F(DmTest,DmSnapshotOverflow)402 TEST_F(DmTest, DmSnapshotOverflow) {
403     if (!CheckSnapshotAvailability()) {
404         return;
405     }
406 
407     SnapshotTestHarness harness;
408     ASSERT_TRUE(harness.Setup());
409 
410     // Open the dm devices.
411     unique_fd snapshot_fd(open(harness.snapshot_dev().c_str(), O_RDWR | O_CLOEXEC));
412     ASSERT_GE(snapshot_fd, 0);
413 
414     // Fill the copy-on-write device until it overflows.
415     uint64_t bytes_remaining = SnapshotTestHarness::kCowDeviceSize;
416     uint8_t byte = 1;
417     while (bytes_remaining) {
418         std::string data(4096, char(byte));
419         if (!android::base::WriteFully(snapshot_fd, data.data(), data.size())) {
420             ASSERT_EQ(errno, EIO);
421             break;
422         }
423         bytes_remaining -= data.size();
424     }
425 
426     // If writes succeed (because they are buffered), then we should expect an
427     // fsync to fail with EIO.
428     if (!bytes_remaining) {
429         ASSERT_EQ(fsync(snapshot_fd), -1);
430         ASSERT_EQ(errno, EIO);
431     }
432 
433     DeviceMapper& dm = DeviceMapper::Instance();
434 
435     vector<DeviceMapper::TargetInfo> target_status;
436     ASSERT_TRUE(dm.GetTableStatus("libdm-test-dm-snapshot", &target_status));
437     ASSERT_EQ(target_status.size(), 1);
438     ASSERT_EQ(strncmp(target_status[0].spec.target_type, "snapshot", strlen("snapshot")), 0);
439 
440     DmTargetSnapshot::Status status;
441     ASSERT_TRUE(DmTargetSnapshot::ParseStatusText(target_status[0].data, &status));
442     if (DmTargetSnapshot::ReportsOverflow("snapshot")) {
443         ASSERT_EQ(status.error, "Overflow");
444     } else {
445         ASSERT_EQ(status.error, "Invalid");
446     }
447 }
448 
TEST_F(DmTest,ParseStatusText)449 TEST_F(DmTest, ParseStatusText) {
450     DmTargetSnapshot::Status status;
451 
452     // Bad inputs
453     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("", &status));
454     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("X", &status));
455     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123", &status));
456     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123/456", &status));
457     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456", &status));
458     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456", &status));
459     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456 789", &status));
460     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 456/789", &status));
461     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123/456/789", &status));
462     EXPECT_FALSE(DmTargetSnapshot::ParseStatusText("123 / 456 789", &status));
463 
464     // Good input
465     EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("123/456 789", &status));
466     EXPECT_EQ(status.sectors_allocated, 123);
467     EXPECT_EQ(status.total_sectors, 456);
468     EXPECT_EQ(status.metadata_sectors, 789);
469 
470     // Known error codes
471     EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Invalid", &status));
472     EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Merge failed", &status));
473     EXPECT_TRUE(DmTargetSnapshot::ParseStatusText("Overflow", &status));
474 }
475 
TEST_F(DmTest,DmSnapshotMergePercent)476 TEST_F(DmTest, DmSnapshotMergePercent) {
477     DmTargetSnapshot::Status status;
478 
479     // Correct input
480     status.sectors_allocated = 1000;
481     status.total_sectors = 1000;
482     status.metadata_sectors = 0;
483     EXPECT_LE(DmTargetSnapshot::MergePercent(status), 1.0);
484 
485     status.sectors_allocated = 500;
486     status.total_sectors = 1000;
487     status.metadata_sectors = 0;
488     EXPECT_GE(DmTargetSnapshot::MergePercent(status), 49.0);
489     EXPECT_LE(DmTargetSnapshot::MergePercent(status), 51.0);
490 
491     status.sectors_allocated = 0;
492     status.total_sectors = 1000;
493     status.metadata_sectors = 0;
494     EXPECT_GE(DmTargetSnapshot::MergePercent(status), 99.0);
495 
496     status.sectors_allocated = 500;
497     status.total_sectors = 1000;
498     status.metadata_sectors = 500;
499     EXPECT_GE(DmTargetSnapshot::MergePercent(status), 99.0);
500 
501     status.sectors_allocated = 500;
502     status.total_sectors = 1000;
503     status.metadata_sectors = 0;
504     EXPECT_LE(DmTargetSnapshot::MergePercent(status, 500), 1.0);
505     EXPECT_LE(DmTargetSnapshot::MergePercent(status, 1000), 51.0);
506     EXPECT_GE(DmTargetSnapshot::MergePercent(status, 1000), 49.0);
507 
508     // Robustness
509     status.sectors_allocated = 2000;
510     status.total_sectors = 1000;
511     status.metadata_sectors = 0;
512     EXPECT_LE(DmTargetSnapshot::MergePercent(status), 0.0);
513 
514     status.sectors_allocated = 2000;
515     status.total_sectors = 1000;
516     status.metadata_sectors = 2000;
517     EXPECT_LE(DmTargetSnapshot::MergePercent(status), 0.0);
518 
519     status.sectors_allocated = 2000;
520     status.total_sectors = 0;
521     status.metadata_sectors = 2000;
522     EXPECT_LE(DmTargetSnapshot::MergePercent(status), 0.0);
523 
524     status.sectors_allocated = 1000;
525     status.total_sectors = 0;
526     status.metadata_sectors = 1000;
527     EXPECT_LE(DmTargetSnapshot::MergePercent(status, 0), 0.0);
528 }
529 
TEST_F(DmTest,CryptArgs)530 TEST_F(DmTest, CryptArgs) {
531     DmTargetCrypt target1(0, 512, "sha1", "abcdefgh", 50, "/dev/loop0", 100);
532     ASSERT_EQ(target1.name(), "crypt");
533     ASSERT_TRUE(target1.Valid());
534     ASSERT_EQ(target1.GetParameterString(), "sha1 abcdefgh 50 /dev/loop0 100");
535 
536     DmTargetCrypt target2(0, 512, "sha1", "abcdefgh", 50, "/dev/loop0", 100);
537     target2.SetSectorSize(64);
538     target2.AllowDiscards();
539     target2.SetIvLargeSectors();
540     target2.AllowEncryptOverride();
541     ASSERT_EQ(target2.GetParameterString(),
542               "sha1 abcdefgh 50 /dev/loop0 100 4 allow_discards allow_encrypt_override "
543               "iv_large_sectors sector_size:64");
544 }
545 
TEST_F(DmTest,DefaultKeyArgs)546 TEST_F(DmTest, DefaultKeyArgs) {
547     DmTargetDefaultKey target(0, 4096, "aes-xts-plain64", "abcdef0123456789", "/dev/loop0", 0);
548     target.SetSetDun();
549     ASSERT_EQ(target.name(), "default-key");
550     ASSERT_TRUE(target.Valid());
551     // TODO: Add case for wrapped key enabled
552     ASSERT_EQ(target.GetParameterString(),
553               "aes-xts-plain64 abcdef0123456789 0 /dev/loop0 0 3 allow_discards sector_size:4096 "
554               "iv_large_sectors");
555 }
556 
TEST_F(DmTest,DefaultKeyLegacyArgs)557 TEST_F(DmTest, DefaultKeyLegacyArgs) {
558     DmTargetDefaultKey target(0, 4096, "AES-256-XTS", "abcdef0123456789", "/dev/loop0", 0);
559     target.SetUseLegacyOptionsFormat();
560     ASSERT_EQ(target.name(), "default-key");
561     ASSERT_TRUE(target.Valid());
562     ASSERT_EQ(target.GetParameterString(), "AES-256-XTS abcdef0123456789 /dev/loop0 0");
563 }
564 
TEST_F(DmTest,DeleteDeviceWithTimeout)565 TEST_F(DmTest, DeleteDeviceWithTimeout) {
566     unique_fd tmp(CreateTempFile("file_1", 4096));
567     ASSERT_GE(tmp, 0);
568     LoopDevice loop(tmp, 10s);
569     ASSERT_TRUE(loop.valid());
570 
571     DmTable table;
572     ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop.device(), 0));
573     ASSERT_TRUE(table.valid());
574     TempDevice dev("libdm-test-dm-linear", table);
575     ASSERT_TRUE(dev.valid());
576 
577     DeviceMapper& dm = DeviceMapper::Instance();
578 
579     std::string path;
580     ASSERT_TRUE(dm.GetDmDevicePathByName("libdm-test-dm-linear", &path));
581     ASSERT_EQ(0, access(path.c_str(), F_OK));
582 
583     ASSERT_TRUE(dm.DeleteDevice("libdm-test-dm-linear", 5s));
584     ASSERT_EQ(DmDeviceState::INVALID, dm.GetState("libdm-test-dm-linear"));
585     ASSERT_NE(0, access(path.c_str(), F_OK));
586     ASSERT_EQ(ENOENT, errno);
587 }
588 
TEST_F(DmTest,IsDmBlockDevice)589 TEST_F(DmTest, IsDmBlockDevice) {
590     unique_fd tmp(CreateTempFile("file_1", 4096));
591     ASSERT_GE(tmp, 0);
592     LoopDevice loop(tmp, 10s);
593     ASSERT_TRUE(loop.valid());
594     ASSERT_TRUE(android::base::StartsWith(loop.device(), "/dev/block"));
595 
596     DmTable table;
597     ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop.device(), 0));
598     ASSERT_TRUE(table.valid());
599 
600     TempDevice dev("libdm-test-dm-linear", table);
601     ASSERT_TRUE(dev.valid());
602 
603     DeviceMapper& dm = DeviceMapper::Instance();
604     ASSERT_TRUE(dm.IsDmBlockDevice(dev.path()));
605     ASSERT_FALSE(dm.IsDmBlockDevice(loop.device()));
606 }
607 
TEST_F(DmTest,GetDmDeviceNameByPath)608 TEST_F(DmTest, GetDmDeviceNameByPath) {
609     unique_fd tmp(CreateTempFile("file_1", 4096));
610     ASSERT_GE(tmp, 0);
611     LoopDevice loop(tmp, 10s);
612     ASSERT_TRUE(loop.valid());
613     ASSERT_TRUE(android::base::StartsWith(loop.device(), "/dev/block"));
614 
615     DmTable table;
616     ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop.device(), 0));
617     ASSERT_TRUE(table.valid());
618 
619     TempDevice dev("libdm-test-dm-linear", table);
620     ASSERT_TRUE(dev.valid());
621 
622     DeviceMapper& dm = DeviceMapper::Instance();
623     // Not a dm device, GetDmDeviceNameByPath will return std::nullopt.
624     ASSERT_FALSE(dm.GetDmDeviceNameByPath(loop.device()));
625     auto name = dm.GetDmDeviceNameByPath(dev.path());
626     ASSERT_EQ("libdm-test-dm-linear", *name);
627 }
628 
TEST_F(DmTest,GetParentBlockDeviceByPath)629 TEST_F(DmTest, GetParentBlockDeviceByPath) {
630     unique_fd tmp(CreateTempFile("file_1", 4096));
631     ASSERT_GE(tmp, 0);
632     LoopDevice loop(tmp, 10s);
633     ASSERT_TRUE(loop.valid());
634     ASSERT_TRUE(android::base::StartsWith(loop.device(), "/dev/block"));
635 
636     DmTable table;
637     ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop.device(), 0));
638     ASSERT_TRUE(table.valid());
639 
640     TempDevice dev("libdm-test-dm-linear", table);
641     ASSERT_TRUE(dev.valid());
642 
643     DeviceMapper& dm = DeviceMapper::Instance();
644     ASSERT_FALSE(dm.GetParentBlockDeviceByPath(loop.device()));
645     auto sub_block_device = dm.GetParentBlockDeviceByPath(dev.path());
646     ASSERT_EQ(loop.device(), *sub_block_device);
647 }
648 
TEST_F(DmTest,DeleteDeviceDeferredNoReferences)649 TEST_F(DmTest, DeleteDeviceDeferredNoReferences) {
650     unique_fd tmp(CreateTempFile("file_1", 4096));
651     ASSERT_GE(tmp, 0);
652     LoopDevice loop(tmp, 10s);
653     ASSERT_TRUE(loop.valid());
654 
655     DmTable table;
656     ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop.device(), 0));
657     ASSERT_TRUE(table.valid());
658     TempDevice dev("libdm-test-dm-linear", table);
659     ASSERT_TRUE(dev.valid());
660 
661     DeviceMapper& dm = DeviceMapper::Instance();
662 
663     std::string path;
664     ASSERT_TRUE(dm.GetDmDevicePathByName("libdm-test-dm-linear", &path));
665     ASSERT_EQ(0, access(path.c_str(), F_OK));
666 
667     ASSERT_TRUE(dm.DeleteDeviceDeferred("libdm-test-dm-linear"));
668 
669     ASSERT_TRUE(WaitForFileDeleted(path, 5s));
670     ASSERT_EQ(DmDeviceState::INVALID, dm.GetState("libdm-test-dm-linear"));
671     ASSERT_NE(0, access(path.c_str(), F_OK));
672     ASSERT_EQ(ENOENT, errno);
673 }
674 
TEST_F(DmTest,DeleteDeviceDeferredWaitsForLastReference)675 TEST_F(DmTest, DeleteDeviceDeferredWaitsForLastReference) {
676     unique_fd tmp(CreateTempFile("file_1", 4096));
677     ASSERT_GE(tmp, 0);
678     LoopDevice loop(tmp, 10s);
679     ASSERT_TRUE(loop.valid());
680 
681     DmTable table;
682     ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop.device(), 0));
683     ASSERT_TRUE(table.valid());
684     TempDevice dev("libdm-test-dm-linear", table);
685     ASSERT_TRUE(dev.valid());
686 
687     DeviceMapper& dm = DeviceMapper::Instance();
688 
689     std::string path;
690     ASSERT_TRUE(dm.GetDmDevicePathByName("libdm-test-dm-linear", &path));
691     ASSERT_EQ(0, access(path.c_str(), F_OK));
692 
693     {
694         // Open a reference to block device.
695         unique_fd fd(TEMP_FAILURE_RETRY(open(dev.path().c_str(), O_RDONLY | O_CLOEXEC)));
696         ASSERT_GE(fd.get(), 0);
697 
698         ASSERT_TRUE(dm.DeleteDeviceDeferred("libdm-test-dm-linear"));
699 
700         ASSERT_EQ(0, access(path.c_str(), F_OK));
701     }
702 
703     // After release device will be removed.
704     ASSERT_TRUE(WaitForFileDeleted(path, 5s));
705     ASSERT_EQ(DmDeviceState::INVALID, dm.GetState("libdm-test-dm-linear"));
706     ASSERT_NE(0, access(path.c_str(), F_OK));
707     ASSERT_EQ(ENOENT, errno);
708 }
709 
TEST_F(DmTest,CreateEmptyDevice)710 TEST_F(DmTest, CreateEmptyDevice) {
711     DeviceMapper& dm = DeviceMapper::Instance();
712     ASSERT_TRUE(dm.CreateEmptyDevice("empty-device"));
713     auto guard =
714             android::base::make_scope_guard([&]() { dm.DeleteDeviceIfExists("empty-device", 5s); });
715 
716     // Empty device should be in suspended state.
717     ASSERT_EQ(DmDeviceState::SUSPENDED, dm.GetState("empty-device"));
718 }
719 
TEST_F(DmTest,UeventAfterLoadTable)720 TEST_F(DmTest, UeventAfterLoadTable) {
721     struct utsname u;
722     ASSERT_EQ(uname(&u), 0);
723 
724     unsigned int major, minor;
725     ASSERT_EQ(sscanf(u.release, "%u.%u", &major, &minor), 2);
726 
727     if (major < 5 || (major == 5 && minor < 15)) {
728         GTEST_SKIP() << "Skipping test on kernel < 5.15";
729     }
730 
731     DeviceMapper& dm = DeviceMapper::Instance();
732     ASSERT_TRUE(dm.CreateEmptyDevice(test_name_));
733 
734     DmTable table;
735     table.Emplace<DmTargetError>(0, 1);
736     ASSERT_TRUE(dm.LoadTable(test_name_, table));
737 
738     std::string ignore_path;
739     ASSERT_TRUE(dm.WaitForDevice(test_name_, 5s, &ignore_path));
740 
741     auto info = dm.GetDetailedInfo(test_name_);
742     ASSERT_TRUE(info.has_value());
743     ASSERT_TRUE(info->IsSuspended());
744 
745     ASSERT_TRUE(dm.DeleteDevice(test_name_));
746 }
747 
TEST_F(DmTest,GetNameAndUuid)748 TEST_F(DmTest, GetNameAndUuid) {
749     auto& dm = DeviceMapper::Instance();
750     ASSERT_TRUE(dm.CreatePlaceholderDevice(test_name_));
751 
752     dev_t dev;
753     ASSERT_TRUE(dm.GetDeviceNumber(test_name_, &dev));
754 
755     std::string name, uuid;
756     ASSERT_TRUE(dm.GetDeviceNameAndUuid(dev, &name, &uuid));
757     ASSERT_EQ(name, test_name_);
758     ASSERT_FALSE(uuid.empty());
759 }
760