]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-libmount.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
6 #include "libmount-util.h"
9 static void test_libmount_unescaping_one(
13 const char *expected_source
,
14 const char *expected_target
) {
15 /* A test for libmount really */
18 log_info("/* %s %s */", __func__
, title
);
20 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
21 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
22 _cleanup_fclose_
FILE *f
= NULL
;
24 f
= fmemopen((char*) string
, strlen(string
), "re");
27 assert_se(libmount_parse(title
, f
, &table
, &iter
) >= 0);
30 const char *source
, *target
;
31 _cleanup_free_
char *x
= NULL
, *cs
= NULL
, *s
= NULL
, *ct
= NULL
, *t
= NULL
;
33 /* We allow this call and the checks below to fail in some cases. See the case definitions below. */
35 r
= mnt_table_next_fs(table
, iter
, &fs
);
36 if (r
!= 0 && may_fail
) {
37 log_error_errno(r
, "mnt_table_next_fs failed: %m");
42 assert_se(x
= cescape(string
));
44 assert_se(source
= mnt_fs_get_source(fs
));
45 assert_se(target
= mnt_fs_get_target(fs
));
47 assert_se(cs
= cescape(source
));
48 assert_se(ct
= cescape(target
));
50 assert_se(cunescape(source
, UNESCAPE_RELAX
, &s
) >= 0);
51 assert_se(cunescape(target
, UNESCAPE_RELAX
, &t
) >= 0);
53 log_info("from '%s'", x
);
54 log_info("source: '%s'", source
);
55 log_info("source: '%s'", cs
);
56 log_info("source: '%s'", s
);
57 log_info("expected: '%s'", strna(expected_source
));
58 log_info("target: '%s'", target
);
59 log_info("target: '%s'", ct
);
60 log_info("target: '%s'", t
);
61 log_info("expected: '%s'", strna(expected_target
));
63 assert_se(may_fail
|| streq(source
, expected_source
));
64 assert_se(may_fail
|| streq(target
, expected_target
));
66 assert_se(mnt_table_next_fs(table
, iter
, &fs
) == 1);
69 static void test_libmount_unescaping(void) {
70 test_libmount_unescaping_one(
71 "escaped space + utf8",
72 "729 38 0:59 / /tmp/„zupa\\040zębowa” rw,relatime shared:395 - tmpfs die\\040Brühe rw,seclabel",
78 test_libmount_unescaping_one(
80 "729 38 0:59 / /tmp/x\\012y rw,relatime shared:395 - tmpfs newline rw,seclabel",
86 /* The result of "mount -t tmpfs '' /tmp/emptysource".
87 * This will fail with libmount <= v2.33.
88 * See https://github.com/karelzak/util-linux/commit/18a52a5094.
90 test_libmount_unescaping_one(
92 "760 38 0:60 / /tmp/emptysource rw,relatime shared:410 - tmpfs rw,seclabel",
98 /* The kernel leaves \r as is.
99 * Also see https://github.com/karelzak/util-linux/issues/780.
101 test_libmount_unescaping_one(
103 "790 38 0:61 / /tmp/foo\rbar rw,relatime shared:425 - tmpfs tmpfs rw,seclabel",
110 int main(int argc
, char *argv
[]) {
111 test_setup_logging(LOG_DEBUG
);
113 test_libmount_unescaping();