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