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