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