]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-libmount.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / test / test-libmount.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
b57adc94
ZJS
2
3#include "alloc-util.h"
4#include "fd-util.h"
5#include "escape.h"
6#include "libmount-util.h"
7#include "tests.h"
8
9static void test_libmount_unescaping_one(
10 const char *title,
11 const char *string,
12 bool may_fail,
13 const char *expected_source,
14 const char *expected_target) {
15 /* A test for libmount really */
16 int r;
17
18 log_info("/* %s %s */", __func__, title);
19
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;
23
b57adc94
ZJS
24 f = fmemopen((char*) string, strlen(string), "re");
25 assert_se(f);
26
e2857b3d 27 assert_se(libmount_parse(title, f, &table, &iter) >= 0);
b57adc94
ZJS
28
29 struct libmnt_fs *fs;
30 const char *source, *target;
31 _cleanup_free_ char *x = NULL, *cs = NULL, *s = NULL, *ct = NULL, *t = NULL;
32
33 /* We allow this call and the checks below to fail in some cases. See the case definitions below. */
34
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");
38 return;
39 }
40 assert_se(r == 0);
41
42 assert_se(x = cescape(string));
43
44 assert_se(source = mnt_fs_get_source(fs));
45 assert_se(target = mnt_fs_get_target(fs));
46
47 assert_se(cs = cescape(source));
48 assert_se(ct = cescape(target));
49
50 assert_se(cunescape(source, UNESCAPE_RELAX, &s) >= 0);
51 assert_se(cunescape(target, UNESCAPE_RELAX, &t) >= 0);
52
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));
62
63 assert_se(may_fail || streq(source, expected_source));
64 assert_se(may_fail || streq(target, expected_target));
65
66 assert_se(mnt_table_next_fs(table, iter, &fs) == 1);
67}
68
69static 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",
73 false,
74 "die Brühe",
75 "/tmp/„zupa zębowa”"
76 );
77
78 test_libmount_unescaping_one(
79 "escaped newline",
80 "729 38 0:59 / /tmp/x\\012y rw,relatime shared:395 - tmpfs newline rw,seclabel",
81 false,
82 "newline",
83 "/tmp/x\ny"
84 );
85
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.
89 */
90 test_libmount_unescaping_one(
91 "empty source",
92 "760 38 0:60 / /tmp/emptysource rw,relatime shared:410 - tmpfs rw,seclabel",
93 true,
94 "",
95 "/tmp/emptysource"
96 );
97
98 /* The kernel leaves \r as is.
99 * Also see https://github.com/karelzak/util-linux/issues/780.
100 */
101 test_libmount_unescaping_one(
102 "foo\\rbar",
103 "790 38 0:61 / /tmp/foo\rbar rw,relatime shared:425 - tmpfs tmpfs rw,seclabel",
104 true,
105 "tmpfs",
106 "/tmp/foo\rbar"
107 );
108}
109
110int main(int argc, char *argv[]) {
111 test_setup_logging(LOG_DEBUG);
112
113 test_libmount_unescaping();
114 return 0;
115}