]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-escape.c
Merge pull request #16271 from yuwata/network-cleanups-around-link-get
[thirdparty/systemd.git] / src / test / test-escape.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
45e0b1f6
RC
2
3#include "alloc-util.h"
4#include "escape.h"
5#include "macro.h"
6d7c4033 6#include "tests.h"
45e0b1f6
RC
7
8static void test_cescape(void) {
70d55819 9 _cleanup_free_ char *t;
45e0b1f6 10
70d55819
ZJS
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
15static 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
22static 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 }
45e0b1f6
RC
48}
49
50static 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, ";"));
a096d8c8
ZJS
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"));
a6a36dea
YW
99 unescaped = mfree(unescaped);
100
101 assert_se(cunescape("\\x00\\x00\\x00", UNESCAPE_ACCEPT_NUL, &unescaped) == 3);
102 assert_se(memcmp(unescaped, "\0\0\0", 3) == 0);
103 unescaped = mfree(unescaped);
104
105 assert_se(cunescape("\\u0000\\u0000\\u0000", UNESCAPE_ACCEPT_NUL, &unescaped) == 3);
106 assert_se(memcmp(unescaped, "\0\0\0", 3) == 0);
107 unescaped = mfree(unescaped);
108
109 assert_se(cunescape("\\U00000000\\U00000000\\U00000000", UNESCAPE_ACCEPT_NUL, &unescaped) == 3);
110 assert_se(memcmp(unescaped, "\0\0\0", 3) == 0);
111 unescaped = mfree(unescaped);
112
113 assert_se(cunescape("\\000\\000\\000", UNESCAPE_ACCEPT_NUL, &unescaped) == 3);
114 assert_se(memcmp(unescaped, "\0\0\0", 3) == 0);
45e0b1f6
RC
115}
116
117static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
118 _cleanup_free_ char *r;
119
120 assert_se(r = shell_escape(s, bad));
121 assert_se(streq_ptr(r, expected));
122}
123
124static void test_shell_escape(void) {
125 test_shell_escape_one("", "", "");
126 test_shell_escape_one("\\", "", "\\\\");
127 test_shell_escape_one("foobar", "", "foobar");
128 test_shell_escape_one("foobar", "o", "f\\o\\obar");
129 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
130}
131
804ee07c
ZJS
132static void test_shell_maybe_quote_one(const char *s,
133 EscapeStyle style,
134 const char *expected) {
135 _cleanup_free_ char *ret = NULL;
136
137 assert_se(ret = shell_maybe_quote(s, style));
138 log_debug("[%s] → [%s] (%s)", s, ret, expected);
139 assert_se(streq(ret, expected));
45e0b1f6
RC
140}
141
142static void test_shell_maybe_quote(void) {
143
804ee07c 144 test_shell_maybe_quote_one("", ESCAPE_BACKSLASH, "");
2b99f645 145 test_shell_maybe_quote_one("", ESCAPE_BACKSLASH_ONELINE, "");
804ee07c
ZJS
146 test_shell_maybe_quote_one("", ESCAPE_POSIX, "");
147 test_shell_maybe_quote_one("\\", ESCAPE_BACKSLASH, "\"\\\\\"");
2b99f645 148 test_shell_maybe_quote_one("\\", ESCAPE_BACKSLASH_ONELINE, "\"\\\\\"");
804ee07c
ZJS
149 test_shell_maybe_quote_one("\\", ESCAPE_POSIX, "$'\\\\'");
150 test_shell_maybe_quote_one("\"", ESCAPE_BACKSLASH, "\"\\\"\"");
2b99f645 151 test_shell_maybe_quote_one("\"", ESCAPE_BACKSLASH_ONELINE, "\"\\\"\"");
804ee07c
ZJS
152 test_shell_maybe_quote_one("\"", ESCAPE_POSIX, "$'\"'");
153 test_shell_maybe_quote_one("foobar", ESCAPE_BACKSLASH, "foobar");
2b99f645 154 test_shell_maybe_quote_one("foobar", ESCAPE_BACKSLASH_ONELINE, "foobar");
804ee07c
ZJS
155 test_shell_maybe_quote_one("foobar", ESCAPE_POSIX, "foobar");
156 test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH, "\"foo bar\"");
2b99f645 157 test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH_ONELINE, "\"foo bar\"");
804ee07c
ZJS
158 test_shell_maybe_quote_one("foo bar", ESCAPE_POSIX, "$'foo bar'");
159 test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH, "\"foo\tbar\"");
2b99f645 160 test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH_ONELINE, "\"foo\\tbar\"");
804ee07c
ZJS
161 test_shell_maybe_quote_one("foo\tbar", ESCAPE_POSIX, "$'foo\\tbar'");
162 test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH, "\"foo\nbar\"");
2b99f645 163 test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH_ONELINE, "\"foo\\nbar\"");
804ee07c
ZJS
164 test_shell_maybe_quote_one("foo\nbar", ESCAPE_POSIX, "$'foo\\nbar'");
165 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH, "\"foo \\\"bar\\\" waldo\"");
2b99f645 166 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH_ONELINE, "\"foo \\\"bar\\\" waldo\"");
804ee07c
ZJS
167 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_POSIX, "$'foo \"bar\" waldo'");
168 test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH, "\"foo\\$bar\"");
2b99f645 169 test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH_ONELINE, "\"foo\\$bar\"");
804ee07c
ZJS
170 test_shell_maybe_quote_one("foo$bar", ESCAPE_POSIX, "$'foo$bar'");
171
172 /* Note that current users disallow control characters, so this "test"
173 * is here merely to establish current behaviour. If control characters
174 * were allowed, they should be quoted, i.e. \001 should become \\001. */
175 test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH, "\"a\nb\001\"");
2b99f645 176 test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH_ONELINE, "\"a\\nb\001\"");
804ee07c
ZJS
177 test_shell_maybe_quote_one("a\nb\001", ESCAPE_POSIX, "$'a\\nb\001'");
178
179 test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH, "\"foo!bar\"");
2b99f645 180 test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH_ONELINE, "\"foo!bar\"");
804ee07c 181 test_shell_maybe_quote_one("foo!bar", ESCAPE_POSIX, "$'foo!bar'");
45e0b1f6
RC
182}
183
184int main(int argc, char *argv[]) {
6d7c4033 185 test_setup_logging(LOG_DEBUG);
804ee07c 186
45e0b1f6 187 test_cescape();
70d55819
ZJS
188 test_xescape();
189 test_xescape_full(false);
190 test_xescape_full(true);
45e0b1f6
RC
191 test_cunescape();
192 test_shell_escape();
193 test_shell_maybe_quote();
194
195 return 0;
196}