]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-device-nodes.c
b6c179868a44ec0fc0a11ddc67287986c5ede527
[thirdparty/systemd.git] / src / test / test-device-nodes.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Dave Reisner
6 ***/
7
8 #include <sys/types.h>
9
10 #include "alloc-util.h"
11 #include "device-nodes.h"
12 #include "string-util.h"
13 #include "util.h"
14
15 /* helpers for test_encode_devnode_name */
16 static char *do_encode_string(const char *in) {
17 size_t out_len = strlen(in) * 4 + 1;
18 char *out = malloc(out_len);
19
20 assert_se(out);
21 assert_se(encode_devnode_name(in, out, out_len) >= 0);
22 puts(out);
23
24 return out;
25 }
26
27 static bool expect_encoded_as(const char *in, const char *expected) {
28 _cleanup_free_ char *encoded = do_encode_string(in);
29 return streq(encoded, expected);
30 }
31
32 static void test_encode_devnode_name(void) {
33 assert_se(expect_encoded_as("systemd sucks", "systemd\\x20sucks"));
34 assert_se(expect_encoded_as("pinkiepie", "pinkiepie"));
35 assert_se(expect_encoded_as("valíd\\ųtf8", "valíd\\x5cųtf8"));
36 assert_se(expect_encoded_as("s/ash/ng", "s\\x2fash\\x2fng"));
37 assert_se(expect_encoded_as("/", "\\x2f"));
38 assert_se(expect_encoded_as("!", "\\x21"));
39 }
40
41 int main(int argc, char *argv[]) {
42 test_encode_devnode_name();
43
44 return 0;
45 }