"a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\\x7f\\x9c\\xcb" :
"a\\x62c\\x5c\"\\x08\\x0c\\x0a\\x0d\\x09\\x0b\\x07\\x03\177\234\313";
const unsigned full_fit = !eight_bits ? 55 : 46;
+ XEscapeFlags flags = eight_bits * XESCAPE_8_BIT;
for (unsigned i = 0; i < 60; i++) {
- _cleanup_free_ char *t;
+ _cleanup_free_ char *t, *q;
- assert_se(t = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i, eight_bits));
+ assert_se(t = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i, flags));
- log_info("%02d: %s", i, t);
+ log_info("%02d: <%s>", i, t);
if (i >= full_fit)
assert_se(streq(t, escaped));
assert_se(strlen(t) == i);
assert_se(strneq(t, "...", i));
}
+
+ assert_se(q = xescape_full("abc\\\"\b\f\n\r\t\v\a\003\177\234\313", "b", i,
+ flags | XESCAPE_FORCE_ELLIPSIS));
+
+ log_info("%02d: <%s>", i, q);
+ if (i > 0)
+ assert_se(endswith(q, "."));
+ assert(strlen(q) <= i);
+ assert(strlen(q) + 3 >= strlen(t));
}
}
_cleanup_free_ char *r;
assert_se(r = shell_escape(s, bad));
+ log_debug("%s → %s (expected %s)", s, r, expected);
assert_se(streq_ptr(r, expected));
}
test_shell_escape_one("foobar", "", "foobar");
test_shell_escape_one("foobar", "o", "f\\o\\obar");
test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
+ test_shell_escape_one("foo\nbar\nbaz", ",:", "foo\\nbar\\nbaz");
}
-static void test_shell_maybe_quote_one(const char *s,
- EscapeStyle style,
- const char *expected) {
+static void test_shell_maybe_quote_one(const char *s, ShellEscapeFlags flags, const char *expected) {
_cleanup_free_ char *ret = NULL;
- assert_se(ret = shell_maybe_quote(s, style));
+ assert_se(ret = shell_maybe_quote(s, flags));
log_debug("[%s] → [%s] (%s)", s, ret, expected);
assert_se(streq(ret, expected));
}
static void test_shell_maybe_quote(void) {
- test_shell_maybe_quote_one("", ESCAPE_BACKSLASH, "");
- test_shell_maybe_quote_one("", ESCAPE_BACKSLASH_ONELINE, "");
- test_shell_maybe_quote_one("", ESCAPE_POSIX, "");
- test_shell_maybe_quote_one("\\", ESCAPE_BACKSLASH, "\"\\\\\"");
- test_shell_maybe_quote_one("\\", ESCAPE_BACKSLASH_ONELINE, "\"\\\\\"");
- test_shell_maybe_quote_one("\\", ESCAPE_POSIX, "$'\\\\'");
- test_shell_maybe_quote_one("\"", ESCAPE_BACKSLASH, "\"\\\"\"");
- test_shell_maybe_quote_one("\"", ESCAPE_BACKSLASH_ONELINE, "\"\\\"\"");
- test_shell_maybe_quote_one("\"", ESCAPE_POSIX, "$'\"'");
- test_shell_maybe_quote_one("foobar", ESCAPE_BACKSLASH, "foobar");
- test_shell_maybe_quote_one("foobar", ESCAPE_BACKSLASH_ONELINE, "foobar");
- test_shell_maybe_quote_one("foobar", ESCAPE_POSIX, "foobar");
- test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH, "\"foo bar\"");
- test_shell_maybe_quote_one("foo bar", ESCAPE_BACKSLASH_ONELINE, "\"foo bar\"");
- test_shell_maybe_quote_one("foo bar", ESCAPE_POSIX, "$'foo bar'");
- test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH, "\"foo\tbar\"");
- test_shell_maybe_quote_one("foo\tbar", ESCAPE_BACKSLASH_ONELINE, "\"foo\\tbar\"");
- test_shell_maybe_quote_one("foo\tbar", ESCAPE_POSIX, "$'foo\\tbar'");
- test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH, "\"foo\nbar\"");
- test_shell_maybe_quote_one("foo\nbar", ESCAPE_BACKSLASH_ONELINE, "\"foo\\nbar\"");
- test_shell_maybe_quote_one("foo\nbar", ESCAPE_POSIX, "$'foo\\nbar'");
- test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH, "\"foo \\\"bar\\\" waldo\"");
- test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_BACKSLASH_ONELINE, "\"foo \\\"bar\\\" waldo\"");
- test_shell_maybe_quote_one("foo \"bar\" waldo", ESCAPE_POSIX, "$'foo \"bar\" waldo'");
- test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH, "\"foo\\$bar\"");
- test_shell_maybe_quote_one("foo$bar", ESCAPE_BACKSLASH_ONELINE, "\"foo\\$bar\"");
- test_shell_maybe_quote_one("foo$bar", ESCAPE_POSIX, "$'foo$bar'");
-
- /* Note that current users disallow control characters, so this "test"
- * is here merely to establish current behaviour. If control characters
- * were allowed, they should be quoted, i.e. \001 should become \\001. */
- test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH, "\"a\nb\001\"");
- test_shell_maybe_quote_one("a\nb\001", ESCAPE_BACKSLASH_ONELINE, "\"a\\nb\001\"");
- test_shell_maybe_quote_one("a\nb\001", ESCAPE_POSIX, "$'a\\nb\001'");
-
- test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH, "\"foo!bar\"");
- test_shell_maybe_quote_one("foo!bar", ESCAPE_BACKSLASH_ONELINE, "\"foo!bar\"");
- test_shell_maybe_quote_one("foo!bar", ESCAPE_POSIX, "$'foo!bar'");
+ test_shell_maybe_quote_one("", 0, "");
+ test_shell_maybe_quote_one("", SHELL_ESCAPE_EMPTY, "\"\"");
+ test_shell_maybe_quote_one("", SHELL_ESCAPE_POSIX, "");
+ test_shell_maybe_quote_one("", SHELL_ESCAPE_POSIX | SHELL_ESCAPE_EMPTY, "\"\"");
+ test_shell_maybe_quote_one("\\", 0, "\"\\\\\"");
+ test_shell_maybe_quote_one("\\", SHELL_ESCAPE_POSIX, "$'\\\\'");
+ test_shell_maybe_quote_one("\"", 0, "\"\\\"\"");
+ test_shell_maybe_quote_one("\"", SHELL_ESCAPE_POSIX, "$'\"'");
+ test_shell_maybe_quote_one("foobar", 0, "foobar");
+ test_shell_maybe_quote_one("foobar", SHELL_ESCAPE_POSIX, "foobar");
+ test_shell_maybe_quote_one("foo bar", 0, "\"foo bar\"");
+ test_shell_maybe_quote_one("foo bar", SHELL_ESCAPE_POSIX, "$'foo bar'");
+ test_shell_maybe_quote_one("foo\tbar", 0, "\"foo\\tbar\"");
+ test_shell_maybe_quote_one("foo\tbar", SHELL_ESCAPE_POSIX, "$'foo\\tbar'");
+ test_shell_maybe_quote_one("foo\nbar", 0, "\"foo\\nbar\"");
+ test_shell_maybe_quote_one("foo\nbar", SHELL_ESCAPE_POSIX, "$'foo\\nbar'");
+ test_shell_maybe_quote_one("foo \"bar\" waldo", 0, "\"foo \\\"bar\\\" waldo\"");
+ test_shell_maybe_quote_one("foo \"bar\" waldo", SHELL_ESCAPE_POSIX, "$'foo \"bar\" waldo'");
+ test_shell_maybe_quote_one("foo$bar", 0, "\"foo\\$bar\"");
+ test_shell_maybe_quote_one("foo$bar", SHELL_ESCAPE_EMPTY, "\"foo\\$bar\"");
+ test_shell_maybe_quote_one("foo$bar", SHELL_ESCAPE_POSIX, "$'foo$bar'");
+ test_shell_maybe_quote_one("foo$bar", SHELL_ESCAPE_POSIX | SHELL_ESCAPE_EMPTY, "$'foo$bar'");
+
+ /* Exclamation mark is special in the interactive shell, but we don't treat it so. */
+ test_shell_maybe_quote_one("foo!bar", 0, "\"foo!bar\"");
+ test_shell_maybe_quote_one("foo!bar", SHELL_ESCAPE_POSIX, "$'foo!bar'");
+
+ /* Control characters and unicode */
+ test_shell_maybe_quote_one("a\nb\001", 0, "\"a\\nb\\001\"");
+ test_shell_maybe_quote_one("a\nb\001", SHELL_ESCAPE_POSIX, "$'a\\nb\\001'");
+
+ test_shell_maybe_quote_one("głąb", 0, "głąb");
+ test_shell_maybe_quote_one("głąb", SHELL_ESCAPE_POSIX, "głąb");
+
+ test_shell_maybe_quote_one("głąb\002\003", 0, "\"głąb\\002\\003\"");
+ test_shell_maybe_quote_one("głąb\002\003", SHELL_ESCAPE_POSIX, "$'głąb\\002\\003'");
+
+ test_shell_maybe_quote_one("głąb\002\003rząd", 0, "\"głąb\\002\\003rząd\"");
+ test_shell_maybe_quote_one("głąb\002\003rząd", SHELL_ESCAPE_POSIX, "$'głąb\\002\\003rząd'");
}
int main(int argc, char *argv[]) {