summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-04-09 18:32:21 +0200
committerLennart Poettering <lennart@poettering.net>2015-04-09 18:32:21 +0200
commit019c7fba754f74909bdb8bbbbbbe529082928a95 (patch)
tree9b53a83d39a2b128e46065625fb6a298942e90f8 /src/test
parentab51b943d42eb6a5e4c6e26cd3c641d4e604bd1c (diff)
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.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-util.c19
1 files changed, 19 insertions, 0 deletions
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;
}