Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
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 :
9 4 : static 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 4 : log_info("/* %s %s */", __func__, title);
19 :
20 4 : _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
21 4 : _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
22 4 : _cleanup_fclose_ FILE *f = NULL;
23 :
24 4 : f = fmemopen((char*) string, strlen(string), "re");
25 4 : assert_se(f);
26 :
27 4 : assert_se(libmount_parse(title, f, &table, &iter) >= 0);
28 :
29 : struct libmnt_fs *fs;
30 : const char *source, *target;
31 8 : _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 4 : r = mnt_table_next_fs(table, iter, &fs);
36 4 : if (r != 0 && may_fail) {
37 1 : log_error_errno(r, "mnt_table_next_fs failed: %m");
38 1 : return;
39 : }
40 3 : assert_se(r == 0);
41 :
42 3 : assert_se(x = cescape(string));
43 :
44 3 : assert_se(source = mnt_fs_get_source(fs));
45 3 : assert_se(target = mnt_fs_get_target(fs));
46 :
47 3 : assert_se(cs = cescape(source));
48 3 : assert_se(ct = cescape(target));
49 :
50 3 : assert_se(cunescape(source, UNESCAPE_RELAX, &s) >= 0);
51 3 : assert_se(cunescape(target, UNESCAPE_RELAX, &t) >= 0);
52 :
53 3 : log_info("from '%s'", x);
54 3 : log_info("source: '%s'", source);
55 3 : log_info("source: '%s'", cs);
56 3 : log_info("source: '%s'", s);
57 3 : log_info("expected: '%s'", strna(expected_source));
58 3 : log_info("target: '%s'", target);
59 3 : log_info("target: '%s'", ct);
60 3 : log_info("target: '%s'", t);
61 3 : log_info("expected: '%s'", strna(expected_target));
62 :
63 3 : assert_se(may_fail || streq(source, expected_source));
64 3 : assert_se(may_fail || streq(target, expected_target));
65 :
66 3 : assert_se(mnt_table_next_fs(table, iter, &fs) == 1);
67 : }
68 :
69 1 : static void test_libmount_unescaping(void) {
70 1 : 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 1 : 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 1 : 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 1 : 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 1 : }
109 :
110 1 : int main(int argc, char *argv[]) {
111 1 : test_setup_logging(LOG_DEBUG);
112 :
113 1 : test_libmount_unescaping();
114 1 : return 0;
115 : }
|