]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-escape.c
Merge pull request #12756 from cdown/uninit
[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 #include "tests.h"
7
8 static void test_cescape(void) {
9 _cleanup_free_ char *t;
10
11 assert_se(t = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
12 assert_se(streq(t, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
13 }
14
15 static void test_xescape(void) {
16 _cleanup_free_ char *t;
17
18 assert_se(t = xescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", ""));
19 assert_se(streq(t, "abc\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb"));
20 }
21
22 static void test_xescape_full(bool eight_bits) {
23 const char* escaped = !eight_bits ?
24 "a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb" :
25 "a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\177\234\313";
26 const unsigned full_fit = !eight_bits ? 55 : 46;
27
28 for (unsigned i = 0; i < 60; i++) {
29 _cleanup_free_ char *t;
30
31 assert_se(t = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i, eight_bits));
32
33 log_info("%02d: %s", i, t);
34
35 if (i >= full_fit)
36 assert_se(streq(t, escaped));
37 else if (i >= 3) {
38 /* We need up to four columns, so up to three three columns may be wasted */
39 assert_se(strlen(t) == i || strlen(t) == i - 1 || strlen(t) == i - 2 || strlen(t) == i - 3);
40 assert_se(strneq(t, escaped, i - 3) || strneq(t, escaped, i - 4) ||
41 strneq(t, escaped, i - 5) || strneq(t, escaped, i - 6));
42 assert_se(endswith(t, "..."));
43 } else {
44 assert_se(strlen(t) == i);
45 assert_se(strneq(t, "...", i));
46 }
47 }
48 }
49
50 static void test_cunescape(void) {
51 _cleanup_free_ char *unescaped;
52
53 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
54 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
55 assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
56 unescaped = mfree(unescaped);
57
58 /* incomplete sequences */
59 assert_se(cunescape("\\x0", 0, &unescaped) < 0);
60 assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
61 assert_se(streq_ptr(unescaped, "\\x0"));
62 unescaped = mfree(unescaped);
63
64 assert_se(cunescape("\\x", 0, &unescaped) < 0);
65 assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
66 assert_se(streq_ptr(unescaped, "\\x"));
67 unescaped = mfree(unescaped);
68
69 assert_se(cunescape("\\", 0, &unescaped) < 0);
70 assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
71 assert_se(streq_ptr(unescaped, "\\"));
72 unescaped = mfree(unescaped);
73
74 assert_se(cunescape("\\11", 0, &unescaped) < 0);
75 assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
76 assert_se(streq_ptr(unescaped, "\\11"));
77 unescaped = mfree(unescaped);
78
79 assert_se(cunescape("\\1", 0, &unescaped) < 0);
80 assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
81 assert_se(streq_ptr(unescaped, "\\1"));
82 unescaped = mfree(unescaped);
83
84 assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
85 assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
86 assert_se(streq_ptr(unescaped, "ßßΠA"));
87 unescaped = mfree(unescaped);
88
89 assert_se(cunescape("\\073", 0, &unescaped) >= 0);
90 assert_se(streq_ptr(unescaped, ";"));
91 unescaped = mfree(unescaped);
92
93 assert_se(cunescape("A=A\\\\x0aB", 0, &unescaped) >= 0);
94 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
95 unescaped = mfree(unescaped);
96
97 assert_se(cunescape("A=A\\\\x0aB", UNESCAPE_RELAX, &unescaped) >= 0);
98 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
99 }
100
101 static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
102 _cleanup_free_ char *r;
103
104 assert_se(r = shell_escape(s, bad));
105 assert_se(streq_ptr(r, expected));
106 }
107
108 static void test_shell_escape(void) {
109 test_shell_escape_one("", "", "");
110 test_shell_escape_one("\\", "", "\\\\");
111 test_shell_escape_one("foobar", "", "foobar");
112 test_shell_escape_one("foobar", "o", "f\\o\\obar");
113 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
114 }
115
116 static void test_shell_maybe_quote_one(const char *s,
117 EscapeStyle style,
118 const char *expected) {
119 _cleanup_free_ char *ret = NULL;
120
121 assert_se(ret = shell_maybe_quote(s, style));
122 log_debug("[%s] → [%s] (%s)", s, ret, expected);
123 assert_se(streq(ret, expected));
124 }
125
126 static void test_shell_maybe_quote(void) {
127
128 test_shell_maybe_quote_one("", ESCAPE_BACKSLASH, "");
129 test_shell_maybe_quote_one("", ESCAPE_POSIX, "");
130 test_shell_maybe_quote_one("\\", ESCAPE_BACKSLASH, "\"\\\\\"");
131 test_shell_maybe_quote_one("\\", ESCAPE_POSIX, "$'\\\\'");
132 test_shell_maybe_quote_one("\"", ESCAPE_BACKSLASH, "\"\\\"\"");
133 test_shell_maybe_quote_one("\"", ESCAPE_POSIX, "$'\"'");
134 test_shell_maybe_quote_one("foobar", ESCAPE_BACKSLASH, "foobar");
135 test_shell_maybe_quote_one("foobar", ESCAPE_POSIX, "foobar");
136 test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH, "\"foo bar\"");
137 test_shell_maybe_quote_one("foo bar", ESCAPE_POSIX, "$'foo bar'");
138 test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH, "\"foo\tbar\"");
139 test_shell_maybe_quote_one("foo\tbar", ESCAPE_POSIX, "$'foo\\tbar'");
140 test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH, "\"foo\nbar\"");
141 test_shell_maybe_quote_one("foo\nbar", ESCAPE_POSIX, "$'foo\\nbar'");
142 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH, "\"foo \\\"bar\\\" waldo\"");
143 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_POSIX, "$'foo \"bar\" waldo'");
144 test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH, "\"foo\\$bar\"");
145 test_shell_maybe_quote_one("foo$bar", ESCAPE_POSIX, "$'foo$bar'");
146
147 /* Note that current users disallow control characters, so this "test"
148 * is here merely to establish current behaviour. If control characters
149 * were allowed, they should be quoted, i.e. \001 should become \\001. */
150 test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH, "\"a\nb\001\"");
151 test_shell_maybe_quote_one("a\nb\001", ESCAPE_POSIX, "$'a\\nb\001'");
152
153 test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH, "\"foo!bar\"");
154 test_shell_maybe_quote_one("foo!bar", ESCAPE_POSIX, "$'foo!bar'");
155 }
156
157 int main(int argc, char *argv[]) {
158 test_setup_logging(LOG_DEBUG);
159
160 test_cescape();
161 test_xescape();
162 test_xescape_full(false);
163 test_xescape_full(true);
164 test_cunescape();
165 test_shell_escape();
166 test_shell_maybe_quote();
167
168 return 0;
169 }