]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-escape.c
413e8cd860e734972a56923710789f369e895cfd
[thirdparty/systemd.git] / src / test / test-escape.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 log_info("/* %s */", __func__);
12
13 assert_se(t = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
14 assert_se(streq(t, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
15 }
16
17 static void test_xescape(void) {
18 _cleanup_free_ char *t;
19
20 log_info("/* %s */", __func__);
21
22 assert_se(t = xescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", ""));
23 assert_se(streq(t, "abc\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb"));
24 }
25
26 static void test_xescape_full(bool eight_bits) {
27 const char* escaped = !eight_bits ?
28 "a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb" :
29 "a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\177\234\313";
30 const unsigned full_fit = !eight_bits ? 55 : 46;
31 XEscapeFlags flags = eight_bits * XESCAPE_8_BIT;
32
33 log_info("/* %s */", __func__);
34
35 for (unsigned i = 0; i < 60; i++) {
36 _cleanup_free_ char *t, *q;
37
38 assert_se(t = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i, flags));
39
40 log_info("%02d: <%s>", i, t);
41
42 if (i >= full_fit)
43 assert_se(streq(t, escaped));
44 else if (i >= 3) {
45 /* We need up to four columns, so up to three three columns may be wasted */
46 assert_se(strlen(t) == i || strlen(t) == i - 1 || strlen(t) == i - 2 || strlen(t) == i - 3);
47 assert_se(strneq(t, escaped, i - 3) || strneq(t, escaped, i - 4) ||
48 strneq(t, escaped, i - 5) || strneq(t, escaped, i - 6));
49 assert_se(endswith(t, "..."));
50 } else {
51 assert_se(strlen(t) == i);
52 assert_se(strneq(t, "...", i));
53 }
54
55 assert_se(q = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i,
56 flags | XESCAPE_FORCE_ELLIPSIS));
57
58 log_info("%02d: <%s>", i, q);
59 if (i > 0)
60 assert_se(endswith(q, "."));
61 assert(strlen(q) <= i);
62 assert(strlen(q) + 3 >= strlen(t));
63 }
64 }
65
66 static void test_cunescape(void) {
67 _cleanup_free_ char *unescaped;
68
69 log_info("/* %s */", __func__);
70
71 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
72 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
73 assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
74 unescaped = mfree(unescaped);
75
76 /* incomplete sequences */
77 assert_se(cunescape("\\x0", 0, &unescaped) < 0);
78 assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
79 assert_se(streq_ptr(unescaped, "\\x0"));
80 unescaped = mfree(unescaped);
81
82 assert_se(cunescape("\\x", 0, &unescaped) < 0);
83 assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
84 assert_se(streq_ptr(unescaped, "\\x"));
85 unescaped = mfree(unescaped);
86
87 assert_se(cunescape("\\", 0, &unescaped) < 0);
88 assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
89 assert_se(streq_ptr(unescaped, "\\"));
90 unescaped = mfree(unescaped);
91
92 assert_se(cunescape("\\11", 0, &unescaped) < 0);
93 assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
94 assert_se(streq_ptr(unescaped, "\\11"));
95 unescaped = mfree(unescaped);
96
97 assert_se(cunescape("\\1", 0, &unescaped) < 0);
98 assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
99 assert_se(streq_ptr(unescaped, "\\1"));
100 unescaped = mfree(unescaped);
101
102 assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
103 assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
104 assert_se(streq_ptr(unescaped, "ßßΠA"));
105 unescaped = mfree(unescaped);
106
107 assert_se(cunescape("\\073", 0, &unescaped) >= 0);
108 assert_se(streq_ptr(unescaped, ";"));
109 unescaped = mfree(unescaped);
110
111 assert_se(cunescape("A=A\\\\x0aB", 0, &unescaped) >= 0);
112 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
113 unescaped = mfree(unescaped);
114
115 assert_se(cunescape("A=A\\\\x0aB", UNESCAPE_RELAX, &unescaped) >= 0);
116 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
117 unescaped = mfree(unescaped);
118
119 assert_se(cunescape("\\x00\\x00\\x00", UNESCAPE_ACCEPT_NUL, &unescaped) == 3);
120 assert_se(memcmp(unescaped, "\0\0\0", 3) == 0);
121 unescaped = mfree(unescaped);
122
123 assert_se(cunescape("\\u0000\\u0000\\u0000", UNESCAPE_ACCEPT_NUL, &unescaped) == 3);
124 assert_se(memcmp(unescaped, "\0\0\0", 3) == 0);
125 unescaped = mfree(unescaped);
126
127 assert_se(cunescape("\\U00000000\\U00000000\\U00000000", UNESCAPE_ACCEPT_NUL, &unescaped) == 3);
128 assert_se(memcmp(unescaped, "\0\0\0", 3) == 0);
129 unescaped = mfree(unescaped);
130
131 assert_se(cunescape("\\000\\000\\000", UNESCAPE_ACCEPT_NUL, &unescaped) == 3);
132 assert_se(memcmp(unescaped, "\0\0\0", 3) == 0);
133 }
134
135 static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
136 _cleanup_free_ char *r;
137
138 assert_se(r = shell_escape(s, bad));
139 log_debug("%s → %s (expected %s)", s, r, expected);
140 assert_se(streq_ptr(r, expected));
141 }
142
143 static void test_shell_escape(void) {
144 log_info("/* %s */", __func__);
145
146 test_shell_escape_one("", "", "");
147 test_shell_escape_one("\\", "", "\\\\");
148 test_shell_escape_one("foobar", "", "foobar");
149 test_shell_escape_one("foobar", "o", "f\\o\\obar");
150 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
151 test_shell_escape_one("foo\nbar\nbaz", ",:", "foo\\nbar\\nbaz");
152 }
153
154 static void test_shell_maybe_quote_one(const char *s, ShellEscapeFlags flags, const char *expected) {
155 _cleanup_free_ char *ret = NULL;
156
157 assert_se(ret = shell_maybe_quote(s, flags));
158 log_debug("[%s] → [%s] (%s)", s, ret, expected);
159 assert_se(streq(ret, expected));
160 }
161
162 static void test_shell_maybe_quote(void) {
163 log_info("/* %s */", __func__);
164
165 test_shell_maybe_quote_one("", 0, "");
166 test_shell_maybe_quote_one("", SHELL_ESCAPE_EMPTY, "\"\"");
167 test_shell_maybe_quote_one("", SHELL_ESCAPE_POSIX, "");
168 test_shell_maybe_quote_one("", SHELL_ESCAPE_POSIX | SHELL_ESCAPE_EMPTY, "\"\"");
169 test_shell_maybe_quote_one("\\", 0, "\"\\\\\"");
170 test_shell_maybe_quote_one("\\", SHELL_ESCAPE_POSIX, "$'\\\\'");
171 test_shell_maybe_quote_one("\"", 0, "\"\\\"\"");
172 test_shell_maybe_quote_one("\"", SHELL_ESCAPE_POSIX, "$'\"'");
173 test_shell_maybe_quote_one("foobar", 0, "foobar");
174 test_shell_maybe_quote_one("foobar", SHELL_ESCAPE_POSIX, "foobar");
175 test_shell_maybe_quote_one("foo bar", 0, "\"foo bar\"");
176 test_shell_maybe_quote_one("foo bar", SHELL_ESCAPE_POSIX, "$'foo bar'");
177 test_shell_maybe_quote_one("foo\tbar", 0, "\"foo\\tbar\"");
178 test_shell_maybe_quote_one("foo\tbar", SHELL_ESCAPE_POSIX, "$'foo\\tbar'");
179 test_shell_maybe_quote_one("foo\nbar", 0, "\"foo\\nbar\"");
180 test_shell_maybe_quote_one("foo\nbar", SHELL_ESCAPE_POSIX, "$'foo\\nbar'");
181 test_shell_maybe_quote_one("foo \"bar\" waldo", 0, "\"foo \\\"bar\\\" waldo\"");
182 test_shell_maybe_quote_one("foo \"bar\" waldo", SHELL_ESCAPE_POSIX, "$'foo \"bar\" waldo'");
183 test_shell_maybe_quote_one("foo$bar", 0, "\"foo\\$bar\"");
184 test_shell_maybe_quote_one("foo$bar", SHELL_ESCAPE_EMPTY, "\"foo\\$bar\"");
185 test_shell_maybe_quote_one("foo$bar", SHELL_ESCAPE_POSIX, "$'foo$bar'");
186 test_shell_maybe_quote_one("foo$bar", SHELL_ESCAPE_POSIX | SHELL_ESCAPE_EMPTY, "$'foo$bar'");
187
188 /* Exclamation mark is special in the interactive shell, but we don't treat it so. */
189 test_shell_maybe_quote_one("foo!bar", 0, "\"foo!bar\"");
190 test_shell_maybe_quote_one("foo!bar", SHELL_ESCAPE_POSIX, "$'foo!bar'");
191
192 /* Control characters and unicode */
193 test_shell_maybe_quote_one("a\nb\001", 0, "\"a\\nb\\001\"");
194 test_shell_maybe_quote_one("a\nb\001", SHELL_ESCAPE_POSIX, "$'a\\nb\\001'");
195
196 test_shell_maybe_quote_one("głąb", 0, "głąb");
197 test_shell_maybe_quote_one("głąb", SHELL_ESCAPE_POSIX, "głąb");
198
199 test_shell_maybe_quote_one("głąb\002\003", 0, "\"głąb\\002\\003\"");
200 test_shell_maybe_quote_one("głąb\002\003", SHELL_ESCAPE_POSIX, "$'głąb\\002\\003'");
201
202 test_shell_maybe_quote_one("głąb\002\003rząd", 0, "\"głąb\\002\\003rząd\"");
203 test_shell_maybe_quote_one("głąb\002\003rząd", SHELL_ESCAPE_POSIX, "$'głąb\\002\\003rząd'");
204 }
205
206 static void test_quote_command_line_one(char **argv, const char *expected) {
207 _cleanup_free_ char *s;
208
209 assert_se(s = quote_command_line(argv));
210 log_info("%s", s);
211 assert_se(streq(s, expected));
212 }
213
214 static void test_quote_command_line(void) {
215 log_info("/* %s */", __func__);
216
217 test_quote_command_line_one(STRV_MAKE("true", "true"),
218 "true true");
219 test_quote_command_line_one(STRV_MAKE("true", "with a space"),
220 "true \"with a space\"");
221 test_quote_command_line_one(STRV_MAKE("true", "with a 'quote'"),
222 "true \"with a 'quote'\"");
223 test_quote_command_line_one(STRV_MAKE("true", "with a \"quote\""),
224 "true \"with a \\\"quote\\\"\"");
225 test_quote_command_line_one(STRV_MAKE("true", "$dollar"),
226 "true \"\\$dollar\"");
227 }
228
229 int main(int argc, char *argv[]) {
230 test_setup_logging(LOG_DEBUG);
231
232 test_cescape();
233 test_xescape();
234 test_xescape_full(false);
235 test_xescape_full(true);
236 test_cunescape();
237 test_shell_escape();
238 test_shell_maybe_quote();
239 test_quote_command_line();
240
241 return 0;
242 }