| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "device-nodes.h" |
| 7 |
#include "tests.h" |
| 8 |
|
| 9 |
/* helpers for test_encode_devnode_name */ |
| 10 |
static char *do_encode_string(const char *in) { |
| 11 |
size_t out_len = strlen(in) * 4 + 1; |
| 12 |
char *out = malloc(out_len); |
| 13 |
|
| 14 |
assert_se(out); |
| 15 |
assert_se(encode_devnode_name(in, out, out_len) >= 0); |
| 16 |
puts(out); |
| 17 |
|
| 18 |
return out; |
| 19 |
} |
| 20 |
|
| 21 |
static bool expect_encoded_as(const char *in, const char *expected) { |
| 22 |
_cleanup_free_ char *encoded = do_encode_string(in); |
| 23 |
return streq(encoded, expected); |
| 24 |
} |
| 25 |
|
| 26 |
TEST(encode_devnode_name) { |
| 27 |
assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks")); |
| 28 |
assert_se(expect_encoded_as("pinkiepie", "pinkiepie")); |
| 29 |
assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8")); |
| 30 |
assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng")); |
| 31 |
assert_se(expect_encoded_as("/", "\\x2f")); |
| 32 |
assert_se(expect_encoded_as("!", "\\x21")); |
| 33 |
assert_se(expect_encoded_as("QEMU ", "QEMU\\x20\\x20\\x20\\x20")); |
| 34 |
} |
| 35 |
|
| 36 |
DEFINE_TEST_MAIN(LOG_DEBUG); |