diff --git a/tests/api-test-2.c b/tests/api-test-2.c index 7acb82f6..d1f4b417 100644 --- a/tests/api-test-2.c +++ b/tests/api-test-2.c @@ -44,8 +44,6 @@ typedef int32_t key_serial_t; #include "luks1/luks.h" #include "libcryptsetup.h" -#define DMDIR "/dev/mapper/" - #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b" #define DEVICE_EMPTY_name "crypt_zero" #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name @@ -3844,6 +3842,7 @@ static void Luks2Reencryption(void) .hash = "sha1", .luks2 = ¶ms2, }; + dev_t devno; const char *mk_hex = "bb21babe733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a"; size_t key_size = strlen(mk_hex) / 2; @@ -4354,6 +4353,8 @@ static void Luks2Reencryption(void) EQ_(crypt_activate_by_passphrase(cd, CDEVICE_2, 6, PASSPHRASE, strlen(PASSPHRASE), 0), 6); OK_(t_device_size(DMDIR CDEVICE_2, &r_size_1)); EQ_(r_size_1, 512); + // store devno for later size check + OK_(t_get_devno(CDEVICE_2, &devno)); // create placeholder device to block automatic deactivation after decryption OK_(_system("dmsetup create " CDEVICE_1 " --table \"0 1 linear " DMDIR CDEVICE_2 " 0\"", 1)); remove(BACKUP_FILE); @@ -4373,7 +4374,7 @@ static void Luks2Reencryption(void) EQ_(crypt_get_data_offset(cd), 0); OK_(crypt_reencrypt_run(cd, NULL, NULL)); remove(BACKUP_FILE); - OK_(t_device_size(DMDIR CDEVICE_2, &r_size_1)); + OK_(t_device_size_by_devno(devno, &r_size_1)); EQ_(r_size_1, 512); OK_(_system("dmsetup remove " DM_RETRY CDEVICE_1 DM_NOSTDERR, 0)); CRYPT_FREE(cd); diff --git a/tests/api-test.c b/tests/api-test.c index 258c24be..93ac8e08 100644 --- a/tests/api-test.c +++ b/tests/api-test.c @@ -34,8 +34,6 @@ #include "luks1/luks.h" #include "libcryptsetup.h" -#define DMDIR "/dev/mapper/" - #define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b" #define DEVICE_EMPTY_name "crypt_zero" #define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name diff --git a/tests/api_test.h b/tests/api_test.h index fef03664..f9109e5c 100644 --- a/tests/api_test.h +++ b/tests/api_test.h @@ -96,6 +96,8 @@ void xlog(const char *msg, const char *tst, const char *func, int line, const ch #define CRYPT_FREE(x) do { crypt_free(x); x = NULL; } while (0) +#define DMDIR "/dev/mapper/" + #define TST_SECTOR_SHIFT 9L #define TST_SECTOR_SIZE 512 #define TST_LOOP_FILE_SIZE (((1 << 20) * 100) >> TST_SECTOR_SHIFT) @@ -124,4 +126,7 @@ int loop_attach(char **loop, const char *file, int offset, int autoclear, int *readonly); int loop_detach(const char *loop); +int t_device_size_by_devno(dev_t devno, uint64_t *retval); +int t_get_devno(const char *dev, dev_t *devno); + #endif diff --git a/tests/test_utils.c b/tests/test_utils.c index 213581e3..9f4a6334 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -684,3 +684,60 @@ int loop_detach(const char *loop) close(loop_fd); return r; } + +int t_get_devno(const char *name, dev_t *devno) +{ + char path[PATH_MAX]; + int r; + struct stat st; + + r = snprintf(path, sizeof(path), DMDIR "%s", name); + if (r < 0 || (size_t)r >= sizeof(path)) + return 1; + + if (stat(path, &st) || !S_ISBLK(st.st_mode)) + return 1; + + *devno = st.st_rdev; + + return 0; +} + +static int _read_uint64(const char *sysfs_path, uint64_t *value) +{ + char tmp[64] = {0}; + int fd, r; + + if ((fd = open(sysfs_path, O_RDONLY)) < 0) + return 0; + r = read(fd, tmp, sizeof(tmp)); + close(fd); + + if (r <= 0) + return 0; + + if (sscanf(tmp, "%" PRIu64, value) != 1) + return 0; + + return 1; +} + +static int _sysfs_get_uint64(int major, int minor, uint64_t *value, const char *attr) +{ + char path[PATH_MAX]; + + if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/%s", + major, minor, attr) < 0) + return 0; + + return _read_uint64(path, value); +} + +int t_device_size_by_devno(dev_t devno, uint64_t *retval) +{ + if (!_sysfs_get_uint64(major(devno), minor(devno), retval, "size")) + return 1; + + *retval *= 512; + return 0; +}