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