1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "errno-util.h"
5 #include "log.h"
6 #include "path-util.h"
7 #include "string-util.h"
8 #include "tests.h"
9 #include "umount.h"
10 #include "util.h"
11
test_mount_points_list_one(const char * fname)12 static void test_mount_points_list_one(const char *fname) {
13 _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head);
14 _cleanup_free_ char *testdata_fname = NULL;
15
16 log_info("/* %s(\"%s\") */", __func__, fname ?: "/proc/self/mountinfo");
17
18 if (fname) {
19 assert_se(get_testdata_dir(fname, &testdata_fname) >= 0);
20 fname = testdata_fname;
21 }
22
23 LIST_HEAD_INIT(mp_list_head);
24 assert_se(mount_points_list_get(fname, &mp_list_head) >= 0);
25
26 LIST_FOREACH(mount_point, m, mp_list_head)
27 log_debug("path=%s o=%s f=0x%lx try-ro=%s dev=%u:%u",
28 m->path,
29 strempty(m->remount_options),
30 m->remount_flags,
31 yes_no(m->try_remount_ro),
32 major(m->devnum), minor(m->devnum));
33 }
34
TEST(mount_points_list)35 TEST(mount_points_list) {
36 test_mount_points_list_one(NULL);
37 test_mount_points_list_one("/test-umount/empty.mountinfo");
38 test_mount_points_list_one("/test-umount/garbled.mountinfo");
39 test_mount_points_list_one("/test-umount/rhbug-1554943.mountinfo");
40 }
41
test_swap_list_one(const char * fname)42 static void test_swap_list_one(const char *fname) {
43 _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head);
44 _cleanup_free_ char *testdata_fname = NULL;
45 int r;
46
47 log_info("/* %s(\"%s\") */", __func__, fname ?: "/proc/swaps");
48
49 if (fname) {
50 assert_se(get_testdata_dir(fname, &testdata_fname) >= 0);
51 fname = testdata_fname;
52 }
53
54 LIST_HEAD_INIT(mp_list_head);
55 r = swap_list_get(fname, &mp_list_head);
56 if (ERRNO_IS_PRIVILEGE(r))
57 return;
58 assert_se(r >= 0);
59
60 LIST_FOREACH(mount_point, m, mp_list_head)
61 log_debug("path=%s o=%s f=0x%lx try-ro=%s dev=%u:%u",
62 m->path,
63 strempty(m->remount_options),
64 m->remount_flags,
65 yes_no(m->try_remount_ro),
66 major(m->devnum), minor(m->devnum));
67 }
68
TEST(swap_list)69 TEST(swap_list) {
70 test_swap_list_one(NULL);
71 test_swap_list_one("/test-umount/example.swaps");
72 }
73
74 DEFINE_TEST_MAIN(LOG_DEBUG);
75