]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-escape.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-escape.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
45e0b1f6
RC
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
21#include "alloc-util.h"
22#include "escape.h"
23#include "macro.h"
24
25static void test_cescape(void) {
26 _cleanup_free_ char *escaped;
27
28 assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
29 assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
30}
31
32static void test_cunescape(void) {
33 _cleanup_free_ char *unescaped;
34
35 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
36 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
37 assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
38 unescaped = mfree(unescaped);
39
40 /* incomplete sequences */
41 assert_se(cunescape("\\x0", 0, &unescaped) < 0);
42 assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
43 assert_se(streq_ptr(unescaped, "\\x0"));
44 unescaped = mfree(unescaped);
45
46 assert_se(cunescape("\\x", 0, &unescaped) < 0);
47 assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
48 assert_se(streq_ptr(unescaped, "\\x"));
49 unescaped = mfree(unescaped);
50
51 assert_se(cunescape("\\", 0, &unescaped) < 0);
52 assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
53 assert_se(streq_ptr(unescaped, "\\"));
54 unescaped = mfree(unescaped);
55
56 assert_se(cunescape("\\11", 0, &unescaped) < 0);
57 assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
58 assert_se(streq_ptr(unescaped, "\\11"));
59 unescaped = mfree(unescaped);
60
61 assert_se(cunescape("\\1", 0, &unescaped) < 0);
62 assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
63 assert_se(streq_ptr(unescaped, "\\1"));
64 unescaped = mfree(unescaped);
65
66 assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
67 assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
68 assert_se(streq_ptr(unescaped, "ßßΠA"));
69 unescaped = mfree(unescaped);
70
71 assert_se(cunescape("\\073", 0, &unescaped) >= 0);
72 assert_se(streq_ptr(unescaped, ";"));
a096d8c8
ZJS
73 unescaped = mfree(unescaped);
74
75 assert_se(cunescape("A=A\\\\x0aB", 0, &unescaped) >= 0);
76 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
77 unescaped = mfree(unescaped);
78
79 assert_se(cunescape("A=A\\\\x0aB", UNESCAPE_RELAX, &unescaped) >= 0);
80 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
45e0b1f6
RC
81}
82
83static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
84 _cleanup_free_ char *r;
85
86 assert_se(r = shell_escape(s, bad));
87 assert_se(streq_ptr(r, expected));
88}
89
90static void test_shell_escape(void) {
91 test_shell_escape_one("", "", "");
92 test_shell_escape_one("\\", "", "\\\\");
93 test_shell_escape_one("foobar", "", "foobar");
94 test_shell_escape_one("foobar", "o", "f\\o\\obar");
95 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
96}
97
804ee07c
ZJS
98static void test_shell_maybe_quote_one(const char *s,
99 EscapeStyle style,
100 const char *expected) {
101 _cleanup_free_ char *ret = NULL;
102
103 assert_se(ret = shell_maybe_quote(s, style));
104 log_debug("[%s] → [%s] (%s)", s, ret, expected);
105 assert_se(streq(ret, expected));
45e0b1f6
RC
106}
107
108static void test_shell_maybe_quote(void) {
109
804ee07c
ZJS
110 test_shell_maybe_quote_one("", ESCAPE_BACKSLASH, "");
111 test_shell_maybe_quote_one("", ESCAPE_POSIX, "");
112 test_shell_maybe_quote_one("\\", ESCAPE_BACKSLASH, "\"\\\\\"");
113 test_shell_maybe_quote_one("\\", ESCAPE_POSIX, "$'\\\\'");
114 test_shell_maybe_quote_one("\"", ESCAPE_BACKSLASH, "\"\\\"\"");
115 test_shell_maybe_quote_one("\"", ESCAPE_POSIX, "$'\"'");
116 test_shell_maybe_quote_one("foobar", ESCAPE_BACKSLASH, "foobar");
117 test_shell_maybe_quote_one("foobar", ESCAPE_POSIX, "foobar");
118 test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH, "\"foo bar\"");
119 test_shell_maybe_quote_one("foo bar", ESCAPE_POSIX, "$'foo bar'");
120 test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH, "\"foo\tbar\"");
121 test_shell_maybe_quote_one("foo\tbar", ESCAPE_POSIX, "$'foo\\tbar'");
122 test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH, "\"foo\nbar\"");
123 test_shell_maybe_quote_one("foo\nbar", ESCAPE_POSIX, "$'foo\\nbar'");
124 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH, "\"foo \\\"bar\\\" waldo\"");
125 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_POSIX, "$'foo \"bar\" waldo'");
126 test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH, "\"foo\\$bar\"");
127 test_shell_maybe_quote_one("foo$bar", ESCAPE_POSIX, "$'foo$bar'");
128
129 /* Note that current users disallow control characters, so this "test"
130 * is here merely to establish current behaviour. If control characters
131 * were allowed, they should be quoted, i.e. \001 should become \\001. */
132 test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH, "\"a\nb\001\"");
133 test_shell_maybe_quote_one("a\nb\001", ESCAPE_POSIX, "$'a\\nb\001'");
134
135 test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH, "\"foo!bar\"");
136 test_shell_maybe_quote_one("foo!bar", ESCAPE_POSIX, "$'foo!bar'");
45e0b1f6
RC
137}
138
139int main(int argc, char *argv[]) {
804ee07c
ZJS
140 log_set_max_level(LOG_DEBUG);
141 log_parse_environment();
142 log_open();
143
45e0b1f6
RC
144 test_cescape();
145 test_cunescape();
146 test_shell_escape();
147 test_shell_maybe_quote();
148
149 return 0;
150}