]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
45e0b1f6 | 2 | /*** |
45e0b1f6 | 3 | Copyright 2010 Lennart Poettering |
45e0b1f6 RC |
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, ";")); | |
a096d8c8 ZJS |
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")); | |
45e0b1f6 RC |
66 | } |
67 | ||
68 | static void test_shell_escape_one(const char *s, const char *bad, const char *expected) { | |
69 | _cleanup_free_ char *r; | |
70 | ||
71 | assert_se(r = shell_escape(s, bad)); | |
72 | assert_se(streq_ptr(r, expected)); | |
73 | } | |
74 | ||
75 | static void test_shell_escape(void) { | |
76 | test_shell_escape_one("", "", ""); | |
77 | test_shell_escape_one("\\", "", "\\\\"); | |
78 | test_shell_escape_one("foobar", "", "foobar"); | |
79 | test_shell_escape_one("foobar", "o", "f\\o\\obar"); | |
80 | test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz"); | |
81 | } | |
82 | ||
804ee07c ZJS |
83 | static void test_shell_maybe_quote_one(const char *s, |
84 | EscapeStyle style, | |
85 | const char *expected) { | |
86 | _cleanup_free_ char *ret = NULL; | |
87 | ||
88 | assert_se(ret = shell_maybe_quote(s, style)); | |
89 | log_debug("[%s] → [%s] (%s)", s, ret, expected); | |
90 | assert_se(streq(ret, expected)); | |
45e0b1f6 RC |
91 | } |
92 | ||
93 | static void test_shell_maybe_quote(void) { | |
94 | ||
804ee07c ZJS |
95 | test_shell_maybe_quote_one("", ESCAPE_BACKSLASH, ""); |
96 | test_shell_maybe_quote_one("", ESCAPE_POSIX, ""); | |
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("foobar", ESCAPE_BACKSLASH, "foobar"); | |
102 | test_shell_maybe_quote_one("foobar", ESCAPE_POSIX, "foobar"); | |
103 | test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH, "\"foo bar\""); | |
104 | test_shell_maybe_quote_one("foo bar", ESCAPE_POSIX, "$'foo bar'"); | |
105 | test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH, "\"foo\tbar\""); | |
106 | test_shell_maybe_quote_one("foo\tbar", ESCAPE_POSIX, "$'foo\\tbar'"); | |
107 | test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH, "\"foo\nbar\""); | |
108 | test_shell_maybe_quote_one("foo\nbar", ESCAPE_POSIX, "$'foo\\nbar'"); | |
109 | test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH, "\"foo \\\"bar\\\" waldo\""); | |
110 | test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_POSIX, "$'foo \"bar\" waldo'"); | |
111 | test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH, "\"foo\\$bar\""); | |
112 | test_shell_maybe_quote_one("foo$bar", ESCAPE_POSIX, "$'foo$bar'"); | |
113 | ||
114 | /* Note that current users disallow control characters, so this "test" | |
115 | * is here merely to establish current behaviour. If control characters | |
116 | * were allowed, they should be quoted, i.e. \001 should become \\001. */ | |
117 | test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH, "\"a\nb\001\""); | |
118 | test_shell_maybe_quote_one("a\nb\001", ESCAPE_POSIX, "$'a\\nb\001'"); | |
119 | ||
120 | test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH, "\"foo!bar\""); | |
121 | test_shell_maybe_quote_one("foo!bar", ESCAPE_POSIX, "$'foo!bar'"); | |
45e0b1f6 RC |
122 | } |
123 | ||
124 | int main(int argc, char *argv[]) { | |
804ee07c ZJS |
125 | log_set_max_level(LOG_DEBUG); |
126 | log_parse_environment(); | |
127 | log_open(); | |
128 | ||
45e0b1f6 RC |
129 | test_cescape(); |
130 | test_cunescape(); | |
131 | test_shell_escape(); | |
132 | test_shell_maybe_quote(); | |
133 | ||
134 | return 0; | |
135 | } |