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