| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "escape.h" |
| 5 |
#include "fd-util.h" |
| 6 |
#include "libmount-util.h" |
| 7 |
#include "tests.h" |
| 8 |
|
| 9 |
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 |
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 |
|
| 24 |
f = fmemopen((char*) string, strlen(string), "r"); |
| 25 |
assert_se(f); |
| 26 |
|
| 27 |
assert_se(libmount_parse_mountinfo(f, &table, &iter) >= 0); |
| 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 = sym_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 = sym_mnt_fs_get_source(fs)); |
| 45 |
assert_se(target = sym_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(sym_mnt_table_next_fs(table, iter, &fs) == 1); |
| 67 |
} |
| 68 |
|
| 69 |
TEST(libmount_unescaping) { |
| 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 |
|
| 110 |
DEFINE_TEST_MAIN(LOG_DEBUG); |