test-umount: add a simple test for swap_list_get()

The implementation seems buggy:
/* test_swap_list("/home/zbyszek/src/systemd/test/test-umount/example.swaps") */
path=0 o= f=0x0 try-ro=no dev=0:0
path=/some/swapfile2 o= f=0x0 try-ro=no dev=0:0
path=/some/swapfile o= f=0x0 try-ro=no dev=0:0
path=/dev/dm-2 o= f=0x0 try-ro=no dev=0:0
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-03-14 12:06:34 +01:00
parent a6dcd22976
commit 1fd8edb53a
5 changed files with 30 additions and 3 deletions

View File

@ -179,14 +179,14 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
return 0;
}
static int swap_list_get(MountPoint **head) {
int swap_list_get(const char *swaps, MountPoint **head) {
_cleanup_fclose_ FILE *proc_swaps = NULL;
unsigned int i;
int r;
assert(head);
proc_swaps = fopen("/proc/swaps", "re");
proc_swaps = fopen(swaps ?: "/proc/swaps", "re");
if (!proc_swaps)
return (errno == ENOENT) ? 0 : -errno;
@ -680,7 +680,7 @@ int swapoff_all(bool *changed) {
LIST_HEAD_INIT(swap_list_head);
r = swap_list_get(&swap_list_head);
r = swap_list_get(NULL, &swap_list_head);
if (r < 0)
return r;

View File

@ -42,3 +42,4 @@ typedef struct MountPoint {
int mount_points_list_get(const char *mountinfo, MountPoint **head);
void mount_points_list_free(MountPoint **head);
int swap_list_get(const char *swaps, MountPoint **head);

View File

@ -24,6 +24,24 @@ static void test_mount_points_list(const char *fname) {
major(m->devnum), minor(m->devnum));
}
static void test_swap_list(const char *fname) {
_cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head);
MountPoint *m;
log_info("/* %s(\"%s\") */", __func__, fname ?: "/proc/self/mountinfo");
LIST_HEAD_INIT(mp_list_head);
assert_se(swap_list_get(fname, &mp_list_head) >= 0);
LIST_FOREACH(mount_point, m, mp_list_head)
log_debug("path=%s o=%s f=0x%lx try-ro=%s dev=%u:%u",
m->path,
strempty(m->remount_options),
m->remount_flags,
yes_no(m->try_remount_ro),
major(m->devnum), minor(m->devnum));
}
int main(int argc, char **argv) {
log_set_max_level(LOG_DEBUG);
log_parse_environment();
@ -33,4 +51,7 @@ int main(int argc, char **argv) {
test_mount_points_list(get_testdata_dir("/test-umount/empty.mountinfo"));
test_mount_points_list(get_testdata_dir("/test-umount/garbled.mountinfo"));
test_mount_points_list(get_testdata_dir("/test-umount/rhbug-1554943.mountinfo"));
test_swap_list(NULL);
test_swap_list(get_testdata_dir("/test-umount/example.swaps"));
}

View File

@ -177,6 +177,7 @@ test_data_files = '''
test-umount/empty.mountinfo
test-umount/garbled.mountinfo
test-umount/rhbug-1554943.mountinfo
test-umount/example.swaps
'''.split()
if conf.get('ENABLE_RESOLVE') == 1

View File

@ -0,0 +1,4 @@
Filename Type Size Used Priority
/dev/dm-2 partition 8151036 2283436 -2
/some/swapfile file 111 111 0
/some/swapfile2 (deleted) file 111 111 0