]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-escape.c
Merge pull request #8417 from brauner/2018-03-09/add_bind_mount_fallback_to_private_d...
[thirdparty/systemd.git] / src / test / test-escape.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include "alloc-util.h"
9 #include "escape.h"
10 #include "macro.h"
11
12 static void test_cescape(void) {
13 _cleanup_free_ char *escaped;
14
15 assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
16 assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
17 }
18
19 static void test_cunescape(void) {
20 _cleanup_free_ char *unescaped;
21
22 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
23 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
24 assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
25 unescaped = mfree(unescaped);
26
27 /* incomplete sequences */
28 assert_se(cunescape("\\x0", 0, &unescaped) < 0);
29 assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
30 assert_se(streq_ptr(unescaped, "\\x0"));
31 unescaped = mfree(unescaped);
32
33 assert_se(cunescape("\\x", 0, &unescaped) < 0);
34 assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
35 assert_se(streq_ptr(unescaped, "\\x"));
36 unescaped = mfree(unescaped);
37
38 assert_se(cunescape("\\", 0, &unescaped) < 0);
39 assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
40 assert_se(streq_ptr(unescaped, "\\"));
41 unescaped = mfree(unescaped);
42
43 assert_se(cunescape("\\11", 0, &unescaped) < 0);
44 assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
45 assert_se(streq_ptr(unescaped, "\\11"));
46 unescaped = mfree(unescaped);
47
48 assert_se(cunescape("\\1", 0, &unescaped) < 0);
49 assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
50 assert_se(streq_ptr(unescaped, "\\1"));
51 unescaped = mfree(unescaped);
52
53 assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
54 assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
55 assert_se(streq_ptr(unescaped, "ßßΠA"));
56 unescaped = mfree(unescaped);
57
58 assert_se(cunescape("\\073", 0, &unescaped) >= 0);
59 assert_se(streq_ptr(unescaped, ";"));
60 unescaped = mfree(unescaped);
61
62 assert_se(cunescape("A=A\\\\x0aB", 0, &unescaped) >= 0);
63 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
64 unescaped = mfree(unescaped);
65
66 assert_se(cunescape("A=A\\\\x0aB", UNESCAPE_RELAX, &unescaped) >= 0);
67 assert_se(streq_ptr(unescaped, "A=A\\x0aB"));
68 }
69
70 static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
71 _cleanup_free_ char *r;
72
73 assert_se(r = shell_escape(s, bad));
74 assert_se(streq_ptr(r, expected));
75 }
76
77 static void test_shell_escape(void) {
78 test_shell_escape_one("", "", "");
79 test_shell_escape_one("\\", "", "\\\\");
80 test_shell_escape_one("foobar", "", "foobar");
81 test_shell_escape_one("foobar", "o", "f\\o\\obar");
82 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
83 }
84
85 static void test_shell_maybe_quote_one(const char *s,
86 EscapeStyle style,
87 const char *expected) {
88 _cleanup_free_ char *ret = NULL;
89
90 assert_se(ret = shell_maybe_quote(s, style));
91 log_debug("[%s] → [%s] (%s)", s, ret, expected);
92 assert_se(streq(ret, expected));
93 }
94
95 static void test_shell_maybe_quote(void) {
96
97 test_shell_maybe_quote_one("", ESCAPE_BACKSLASH, "");
98 test_shell_maybe_quote_one("", ESCAPE_POSIX, "");
99 test_shell_maybe_quote_one("\\", ESCAPE_BACKSLASH, "\"\\\\\"");
100 test_shell_maybe_quote_one("\\", ESCAPE_POSIX, "$'\\\\'");
101 test_shell_maybe_quote_one("\"", ESCAPE_BACKSLASH, "\"\\\"\"");
102 test_shell_maybe_quote_one("\"", ESCAPE_POSIX, "$'\"'");
103 test_shell_maybe_quote_one("foobar", ESCAPE_BACKSLASH, "foobar");
104 test_shell_maybe_quote_one("foobar", ESCAPE_POSIX, "foobar");
105 test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH, "\"foo bar\"");
106 test_shell_maybe_quote_one("foo bar", ESCAPE_POSIX, "$'foo bar'");
107 test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH, "\"foo\tbar\"");
108 test_shell_maybe_quote_one("foo\tbar", ESCAPE_POSIX, "$'foo\\tbar'");
109 test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH, "\"foo\nbar\"");
110 test_shell_maybe_quote_one("foo\nbar", ESCAPE_POSIX, "$'foo\\nbar'");
111 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH, "\"foo \\\"bar\\\" waldo\"");
112 test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_POSIX, "$'foo \"bar\" waldo'");
113 test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH, "\"foo\\$bar\"");
114 test_shell_maybe_quote_one("foo$bar", ESCAPE_POSIX, "$'foo$bar'");
115
116 /* Note that current users disallow control characters, so this "test"
117 * is here merely to establish current behaviour. If control characters
118 * were allowed, they should be quoted, i.e. \001 should become \\001. */
119 test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH, "\"a\nb\001\"");
120 test_shell_maybe_quote_one("a\nb\001", ESCAPE_POSIX, "$'a\\nb\001'");
121
122 test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH, "\"foo!bar\"");
123 test_shell_maybe_quote_one("foo!bar", ESCAPE_POSIX, "$'foo!bar'");
124 }
125
126 int main(int argc, char *argv[]) {
127 log_set_max_level(LOG_DEBUG);
128 log_parse_environment();
129 log_open();
130
131 test_cescape();
132 test_cunescape();
133 test_shell_escape();
134 test_shell_maybe_quote();
135
136 return 0;
137 }