| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-daemon.h" |
| 4 |
|
| 5 |
#include "blockdev-util.h" |
| 6 |
#include "device-util.h" |
| 7 |
#include "errno-util.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "tests.h" |
| 10 |
|
| 11 |
static void test_path_is_encrypted_one(const char *p, int expect) { |
| 12 |
int r; |
| 13 |
|
| 14 |
r = path_is_encrypted(p); |
| 15 |
if (IN_SET(r, -ENOENT, -ELOOP) || ERRNO_IS_NEG_PRIVILEGE(r)) |
| 16 |
/* This might fail, if btrfs is used and we run in a container. In that case we cannot |
| 17 |
* resolve the device node paths that BTRFS_IOC_DEV_INFO returns, because the device nodes |
| 18 |
* are unlikely to exist in the container. But if we can't stat() them we cannot determine |
| 19 |
* the dev_t of them, and thus cannot figure out if they are encrypted. Hence let's just |
| 20 |
* ignore ENOENT here. Also skip the test if we lack privileges. |
| 21 |
* ELOOP might happen if the mount point is a symlink, as seen with under |
| 22 |
* some rpm-ostree distros */ |
| 23 |
return; |
| 24 |
assert_se(r >= 0); |
| 25 |
|
| 26 |
log_info("%s encrypted: %s", p, yes_no(r)); |
| 27 |
|
| 28 |
assert_se(expect < 0 || ((r > 0) == (expect > 0))); |
| 29 |
} |
| 30 |
|
| 31 |
TEST(path_is_encrypted) { |
| 32 |
int booted = sd_booted(); /* If this is run in build environments such as koji, /dev/ might be a |
| 33 |
* regular fs. Don't assume too much if not running under systemd. */ |
| 34 |
|
| 35 |
log_info("/* %s (sd_booted=%d) */", __func__, booted); |
| 36 |
|
| 37 |
test_path_is_encrypted_one("/home", -1); |
| 38 |
test_path_is_encrypted_one("/var", -1); |
| 39 |
test_path_is_encrypted_one("/", -1); |
| 40 |
test_path_is_encrypted_one("/proc", false); |
| 41 |
test_path_is_encrypted_one("/sys", false); |
| 42 |
test_path_is_encrypted_one("/dev", booted > 0 ? false : -1); |
| 43 |
} |
| 44 |
|
| 45 |
TEST(partscan_enabled) { |
| 46 |
|
| 47 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 48 |
int r; |
| 49 |
|
| 50 |
assert_se(sd_device_enumerator_new(&e) >= 0); |
| 51 |
assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0); |
| 52 |
assert_se(sd_device_enumerator_add_match_subsystem(e, "block", /* match= */ true) >= 0); |
| 53 |
|
| 54 |
FOREACH_DEVICE(e, dev) { |
| 55 |
_cleanup_close_ int fd = -EBADF; |
| 56 |
const char *name; |
| 57 |
|
| 58 |
r = sd_device_get_devname(dev, &name); |
| 59 |
if (r < 0) { |
| 60 |
log_warning_errno(r, "Found block device without a name, skipping."); |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 65 |
if (fd < 0) { |
| 66 |
log_warning_errno(fd, "Found block device '%s' which we cannot open, skipping: %m", name); |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
r = blockdev_partscan_enabled_fd(fd); |
| 71 |
if (r < 0) { |
| 72 |
log_warning_errno(r, "Failed to determine if block device '%s' has partition scanning enabled, skipping: %m", name); |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
log_info("%s has partition scanning enabled: %s", name, yes_no(r)); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
static void test_partition_node_of_one(const char *main, unsigned partition, const char *result, int retval) { |
| 81 |
_cleanup_free_ char *s = NULL; |
| 82 |
int r; |
| 83 |
|
| 84 |
r = partition_node_of(main, partition, &s); |
| 85 |
ASSERT_EQ(r, retval); |
| 86 |
if (r < 0) |
| 87 |
return; |
| 88 |
ASSERT_STREQ(s, result); |
| 89 |
|
| 90 |
log_info("%s with %u → %s", main, partition, result); |
| 91 |
} |
| 92 |
|
| 93 |
TEST(partition_node_of) { |
| 94 |
test_partition_node_of_one("/dev/sda", 2, "/dev/sda2", 0); |
| 95 |
test_partition_node_of_one("sda", 3, "sda3", 0); |
| 96 |
test_partition_node_of_one("/dev/nvme0n1", 7, "/dev/nvme0n1p7", 0); |
| 97 |
test_partition_node_of_one("nvme0n1", 8, "nvme0n1p8", 0); |
| 98 |
test_partition_node_of_one("/dev/loop1", 3, "/dev/loop1p3", 0); |
| 99 |
test_partition_node_of_one("", 1, NULL, -EINVAL); |
| 100 |
test_partition_node_of_one("/", 1, NULL, -EADDRNOTAVAIL); |
| 101 |
test_partition_node_of_one("/dev/", 1, NULL, -EISDIR); |
| 102 |
test_partition_node_of_one("/sda", 1, "/sda1", 0); |
| 103 |
test_partition_node_of_one(".", 1, NULL, -EADDRNOTAVAIL); |
| 104 |
} |
| 105 |
|
| 106 |
DEFINE_TEST_MAIN(LOG_INFO); |