Debian Codegraph @

src/shutdown/test-umount.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "detach-swap.h"
5 #include "errno-util.h"
6 #include "fd-util.h"
7 #include "log.h"
8 #include "tests.h"
9 #include "umount.h"
10
11 static void test_mount_points_list_one(const char *fname) {
12 _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head);
13 _cleanup_fclose_ FILE *f = NULL;
14
15 log_info("/* %s(\"%s\") */", __func__, fname ?: "/proc/self/mountinfo");
16
17 if (fname) {
18 _cleanup_free_ char *testdata_fname = NULL;
19 assert_se(get_testdata_dir(fname, &testdata_fname) >= 0);
20 ASSERT_NOT_NULL((f = fopen(testdata_fname, "re")));
21 }
22
23 LIST_HEAD_INIT(mp_list_head);
24 assert_se(mount_points_list_get(f, &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",
28 m->path,
29 strempty(m->remount_options),
30 m->remount_flags,
31 yes_no(m->try_remount_ro));
32 }
33
34 TEST(mount_points_list) {
35 test_mount_points_list_one(NULL);
36 test_mount_points_list_one("/test-umount/empty.mountinfo");
37 test_mount_points_list_one("/test-umount/garbled.mountinfo");
38 test_mount_points_list_one("/test-umount/rhbug-1554943.mountinfo");
39 }
40
41 static void test_swap_list_one(const char *fname) {
42 _cleanup_(swap_devices_list_free) LIST_HEAD(SwapDevice, sd_list_head);
43 _cleanup_free_ char *testdata_fname = NULL;
44 int r;
45
46 log_info("/* %s(\"%s\") */", __func__, fname ?: "/proc/swaps");
47
48 if (fname) {
49 assert_se(get_testdata_dir(fname, &testdata_fname) >= 0);
50 fname = testdata_fname;
51 }
52
53 LIST_HEAD_INIT(sd_list_head);
54 r = swap_list_get(fname, &sd_list_head);
55 if (ERRNO_IS_PRIVILEGE(r))
56 return;
57 assert_se(r >= 0);
58
59 LIST_FOREACH(swap_device, m, sd_list_head)
60 log_debug("path=%s", m->path);
61 }
62
63 TEST(swap_list) {
64 test_swap_list_one(NULL);
65 test_swap_list_one("/test-umount/example.swaps");
66 }
67
68 DEFINE_TEST_MAIN(LOG_DEBUG);