]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-escape.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
24 static void test_cescape(void) {
25 _cleanup_free_
char *escaped
;
27 assert_se(escaped
= cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
28 assert_se(streq(escaped
, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
31 static void test_cunescape(void) {
32 _cleanup_free_
char *unescaped
;
34 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped
) < 0);
35 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX
, &unescaped
) >= 0);
36 assert_se(streq_ptr(unescaped
, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
37 unescaped
= mfree(unescaped
);
39 /* incomplete sequences */
40 assert_se(cunescape("\\x0", 0, &unescaped
) < 0);
41 assert_se(cunescape("\\x0", UNESCAPE_RELAX
, &unescaped
) >= 0);
42 assert_se(streq_ptr(unescaped
, "\\x0"));
43 unescaped
= mfree(unescaped
);
45 assert_se(cunescape("\\x", 0, &unescaped
) < 0);
46 assert_se(cunescape("\\x", UNESCAPE_RELAX
, &unescaped
) >= 0);
47 assert_se(streq_ptr(unescaped
, "\\x"));
48 unescaped
= mfree(unescaped
);
50 assert_se(cunescape("\\", 0, &unescaped
) < 0);
51 assert_se(cunescape("\\", UNESCAPE_RELAX
, &unescaped
) >= 0);
52 assert_se(streq_ptr(unescaped
, "\\"));
53 unescaped
= mfree(unescaped
);
55 assert_se(cunescape("\\11", 0, &unescaped
) < 0);
56 assert_se(cunescape("\\11", UNESCAPE_RELAX
, &unescaped
) >= 0);
57 assert_se(streq_ptr(unescaped
, "\\11"));
58 unescaped
= mfree(unescaped
);
60 assert_se(cunescape("\\1", 0, &unescaped
) < 0);
61 assert_se(cunescape("\\1", UNESCAPE_RELAX
, &unescaped
) >= 0);
62 assert_se(streq_ptr(unescaped
, "\\1"));
63 unescaped
= mfree(unescaped
);
65 assert_se(cunescape("\\u0000", 0, &unescaped
) < 0);
66 assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX
, &unescaped
) >= 0);
67 assert_se(streq_ptr(unescaped
, "ßßΠA"));
68 unescaped
= mfree(unescaped
);
70 assert_se(cunescape("\\073", 0, &unescaped
) >= 0);
71 assert_se(streq_ptr(unescaped
, ";"));
74 static void test_shell_escape_one(const char *s
, const char *bad
, const char *expected
) {
75 _cleanup_free_
char *r
;
77 assert_se(r
= shell_escape(s
, bad
));
78 assert_se(streq_ptr(r
, expected
));
81 static void test_shell_escape(void) {
82 test_shell_escape_one("", "", "");
83 test_shell_escape_one("\\", "", "\\\\");
84 test_shell_escape_one("foobar", "", "foobar");
85 test_shell_escape_one("foobar", "o", "f\\o\\obar");
86 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
89 static void test_shell_maybe_quote_one(const char *s
,
91 const char *expected
) {
92 _cleanup_free_
char *ret
= NULL
;
94 assert_se(ret
= shell_maybe_quote(s
, style
));
95 log_debug("[%s] → [%s] (%s)", s
, ret
, expected
);
96 assert_se(streq(ret
, expected
));
99 static void test_shell_maybe_quote(void) {
101 test_shell_maybe_quote_one("", ESCAPE_BACKSLASH
, "");
102 test_shell_maybe_quote_one("", ESCAPE_POSIX
, "");
103 test_shell_maybe_quote_one("\\", ESCAPE_BACKSLASH
, "\"\\\\\"");
104 test_shell_maybe_quote_one("\\", ESCAPE_POSIX
, "$'\\\\'");
105 test_shell_maybe_quote_one("\"", ESCAPE_BACKSLASH
, "\"\\\"\"");
106 test_shell_maybe_quote_one("\"", ESCAPE_POSIX
, "$'\"'");
107 test_shell_maybe_quote_one("foobar", ESCAPE_BACKSLASH
, "foobar");
108 test_shell_maybe_quote_one("foobar", ESCAPE_POSIX
, "foobar");
109 test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH
, "\"foo bar\"");
110 test_shell_maybe_quote_one("foo bar", ESCAPE_POSIX
, "$'foo bar'");
111 test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH
, "\"foo\tbar\"");
112 test_shell_maybe_quote_one("foo\tbar", ESCAPE_POSIX
, "$'foo\\tbar'");
113 test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH
, "\"foo\nbar\"");
114 test_shell_maybe_quote_one("foo\nbar", ESCAPE_POSIX
, "$'foo\\nbar'");
115 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH
, "\"foo \\\"bar\\\" waldo\"");
116 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_POSIX
, "$'foo \"bar\" waldo'");
117 test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH
, "\"foo\\$bar\"");
118 test_shell_maybe_quote_one("foo$bar", ESCAPE_POSIX
, "$'foo$bar'");
120 /* Note that current users disallow control characters, so this "test"
121 * is here merely to establish current behaviour. If control characters
122 * were allowed, they should be quoted, i.e. \001 should become \\001. */
123 test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH
, "\"a\nb\001\"");
124 test_shell_maybe_quote_one("a\nb\001", ESCAPE_POSIX
, "$'a\\nb\001'");
126 test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH
, "\"foo!bar\"");
127 test_shell_maybe_quote_one("foo!bar", ESCAPE_POSIX
, "$'foo!bar'");
130 int main(int argc
, char *argv
[]) {
131 log_set_max_level(LOG_DEBUG
);
132 log_parse_environment();
138 test_shell_maybe_quote();