summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2015-06-23 10:32:02 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2015-08-07 15:50:43 +0000
commit61ee6939819963b7845c101485e188ca2a8119c6 (patch)
treead6314638c8818c8eff49201eeb74e699f5977a7 /src/test
parent8ef24e7a4f4b4d464b66fa7d3f0acaa88800d6cb (diff)
util: Add shell_escape
This is for shell-style \ escaping rather than quoting, which while it has the same effect in produced shell commands, is not exclusively useful for shell commands. shell_escape would be useful for producing sed commands, as you would be able to \ escape the normal special characters, plus whichever argument separator was chosen; or it could be used to escape arguments passed to the overlayfs mount command.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-util.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/test/test-util.c b/src/test/test-util.c
index fc7a3de106..3c1bac4606 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -2100,6 +2100,21 @@ static void test_sparse_write(void) {
test_sparse_write_one(fd, test_e, sizeof(test_e));
}
+static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
+ _cleanup_free_ char *r;
+
+ assert_se(r = shell_escape(s, bad));
+ assert_se(streq_ptr(r, expected));
+}
+
+static void test_shell_escape(void) {
+ test_shell_escape_one("", "", "");
+ test_shell_escape_one("\\", "", "\\\\");
+ 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");
+}
+
static void test_shell_maybe_quote_one(const char *s, const char *expected) {
_cleanup_free_ char *r;
@@ -2264,6 +2279,7 @@ int main(int argc, char *argv[]) {
test_same_fd();
test_uid_ptr();
test_sparse_write();
+ test_shell_escape();
test_shell_maybe_quote();
test_parse_mode();
test_tempfn();