From 019c7fba754f74909bdb8bbbbbbe529082928a95 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 9 Apr 2015 18:32:21 +0200 Subject: util: add shell_maybe_quote() call for preparing a string for shell cmdline inclusion If necessary the passed string is enclosed in "", and all special characters escapes. This also ports over usage in bus-util.c and job.c to use this, instead of a incorrect local implementation that forgets to properly escape. --- src/test/test-util.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/test') diff --git a/src/test/test-util.c b/src/test/test-util.c index 052e292a6c..f1403fa393 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -1542,6 +1542,24 @@ static void test_sparse_write(void) { test_sparse_write_one(fd, test_e, sizeof(test_e)); } +static void test_shell_maybe_quote_one(const char *s, const char *expected) { + _cleanup_free_ char *r; + + assert_se(r = shell_maybe_quote(s)); + assert_se(streq(r, expected)); +} + +static void test_shell_maybe_quote(void) { + + test_shell_maybe_quote_one("", ""); + test_shell_maybe_quote_one("\\", "\"\\\\\""); + test_shell_maybe_quote_one("\"", "\"\\\"\""); + test_shell_maybe_quote_one("foobar", "foobar"); + test_shell_maybe_quote_one("foo bar", "\"foo bar\""); + test_shell_maybe_quote_one("foo \"bar\" waldo", "\"foo \\\"bar\\\" waldo\""); + test_shell_maybe_quote_one("foo$bar", "\"foo\\$bar\""); +} + int main(int argc, char *argv[]) { log_parse_environment(); log_open(); @@ -1619,6 +1637,7 @@ int main(int argc, char *argv[]) { test_same_fd(); test_uid_ptr(); test_sparse_write(); + test_shell_maybe_quote(); return 0; } -- cgit v1.2.3-54-g00ecf