Debian Codegraph @

src/test/test-escape.c

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