]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-escape.c
Merge the "boot loader specification" wiki page
[thirdparty/systemd.git] / src / test / test-escape.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "escape.h"
5 #include "macro.h"
6
7 static void test_cescape(void) {
8 _cleanup_free_ char *escaped;
9
10 assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
11 assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
12 }
13
14 static void test_cunescape(void) {
15 _cleanup_free_ char *unescaped;
16
17 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
18 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
19 assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
20 unescaped = mfree(unescaped);
21
22 /* incomplete sequences */
23 assert_se(cunescape("\\x0", 0, &unescaped) < 0);
24 assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
25 assert_se(streq_ptr(unescaped, "\\x0"));
26 unescaped = mfree(unescaped);
27
28 assert_se(cunescape("\\x", 0, &unescaped) < 0);
29 assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
30 assert_se(streq_ptr(unescaped, "\\x"));
31 unescaped = mfree(unescaped);
32
33 assert_se(cunescape("\\", 0, &unescaped) < 0);
34 assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
35 assert_se(streq_ptr(unescaped, "\\"));
36 unescaped = mfree(unescaped);
37
38 assert_se(cunescape("\\11", 0, &unescaped) < 0);
39 assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
40 assert_se(streq_ptr(unescaped, "\\11"));
41 unescaped = mfree(unescaped);
42
43 assert_se(cunescape("\\1", 0, &unescaped) < 0);
44 assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
45 assert_se(streq_ptr(unescaped, "\\1"));
46 unescaped = mfree(unescaped);
47
48 assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
49 assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
50 assert_se(streq_ptr(unescaped, "ßßΠA"));
51 unescaped = mfree(unescaped);
52
53 assert_se(cunescape("\\073", 0, &unescaped) >= 0);
54 assert_se(streq_ptr(unescaped, ";"));
55 unescaped = mfree(unescaped);
56
57 assert_se(cunescape("A=A\\\\x0aB", 0, &unescaped) >= 0);
58 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
59 unescaped = mfree(unescaped);
60
61 assert_se(cunescape("A=A\\\\x0aB", UNESCAPE_RELAX, &unescaped) >= 0);
62 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
63 }
64
65 static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
66 _cleanup_free_ char *r;
67
68 assert_se(r = shell_escape(s, bad));
69 assert_se(streq_ptr(r, expected));
70 }
71
72 static void test_shell_escape(void) {
73 test_shell_escape_one("", "", "");
74 test_shell_escape_one("\\", "", "\\\\");
75 test_shell_escape_one("foobar", "", "foobar");
76 test_shell_escape_one("foobar", "o", "f\\o\\obar");
77 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
78 }
79
80 static void test_shell_maybe_quote_one(const char *s,
81 EscapeStyle style,
82 const char *expected) {
83 _cleanup_free_ char *ret = NULL;
84
85 assert_se(ret = shell_maybe_quote(s, style));
86 log_debug("[%s] → [%s] (%s)", s, ret, expected);
87 assert_se(streq(ret, expected));
88 }
89
90 static void test_shell_maybe_quote(void) {
91
92 test_shell_maybe_quote_one("", ESCAPE_BACKSLASH, "");
93 test_shell_maybe_quote_one("", ESCAPE_POSIX, "");
94 test_shell_maybe_quote_one("\\", ESCAPE_BACKSLASH, "\"\\\\\"");
95 test_shell_maybe_quote_one("\\", ESCAPE_POSIX, "$'\\\\'");
96 test_shell_maybe_quote_one("\"", ESCAPE_BACKSLASH, "\"\\\"\"");
97 test_shell_maybe_quote_one("\"", ESCAPE_POSIX, "$'\"'");
98 test_shell_maybe_quote_one("foobar", ESCAPE_BACKSLASH, "foobar");
99 test_shell_maybe_quote_one("foobar", ESCAPE_POSIX, "foobar");
100 test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH, "\"foo bar\"");
101 test_shell_maybe_quote_one("foo bar", ESCAPE_POSIX, "$'foo bar'");
102 test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH, "\"foo\tbar\"");
103 test_shell_maybe_quote_one("foo\tbar", ESCAPE_POSIX, "$'foo\\tbar'");
104 test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH, "\"foo\nbar\"");
105 test_shell_maybe_quote_one("foo\nbar", ESCAPE_POSIX, "$'foo\\nbar'");
106 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH, "\"foo \\\"bar\\\" waldo\"");
107 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_POSIX, "$'foo \"bar\" waldo'");
108 test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH, "\"foo\\$bar\"");
109 test_shell_maybe_quote_one("foo$bar", ESCAPE_POSIX, "$'foo$bar'");
110
111 /* Note that current users disallow control characters, so this "test"
112 * is here merely to establish current behaviour. If control characters
113 * were allowed, they should be quoted, i.e. \001 should become \\001. */
114 test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH, "\"a\nb\001\"");
115 test_shell_maybe_quote_one("a\nb\001", ESCAPE_POSIX, "$'a\\nb\001'");
116
117 test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH, "\"foo!bar\"");
118 test_shell_maybe_quote_one("foo!bar", ESCAPE_POSIX, "$'foo!bar'");
119 }
120
121 int main(int argc, char *argv[]) {
122 log_set_max_level(LOG_DEBUG);
123 log_parse_environment();
124 log_open();
125
126 test_cescape();
127 test_cunescape();
128 test_shell_escape();
129 test_shell_maybe_quote();
130
131 return 0;
132 }