systemd-run: properly escape arguments

Spaces, quotes, and such, were not properly escaped. We should
write them like we read them.

https://bugs.freedesktop.org/show_bug.cgi?id=67971
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2013-09-09 13:49:29 -04:00
parent a012ab5293
commit a6fde35332
4 changed files with 125 additions and 48 deletions
+79 -35
View File
@@ -42,50 +42,68 @@ static void test_specifier_printf(void) {
assert_se(streq(w, "xxx a=AAAA b=BBBB yyy"));
}
static void test_strv_find(void) {
const char * const input_table[] = {
"one",
"two",
"three",
NULL
};
static const char* const input_table_multiple[] = {
"one",
"two",
"three",
NULL,
};
assert_se(strv_find((char **)input_table, "three"));
assert_se(!strv_find((char **)input_table, "four"));
static const char* const input_table_one[] = {
"one",
NULL,
};
static const char* const input_table_none[] = {
NULL,
};
static const char* const input_table_quotes[] = {
"\"",
"'",
"\"\"",
"\\",
"\\\\",
NULL,
};
#define QUOTES_STRING \
"\"\\\"\" " \
"\"\\\'\" " \
"\"\\\"\\\"\" " \
"\"\\\\\" " \
"\"\\\\\\\\\""
static const char * const input_table_spaces[] = {
" ",
"' '",
"\" ",
" \"",
" \\\\ ",
NULL,
};
#define SPACES_STRING \
"\" \" " \
"\"\\' \\'\" " \
"\"\\\" \" " \
"\" \\\"\" " \
"\" \\\\\\\\ \""
static void test_strv_find(void) {
assert_se(strv_find((char **)input_table_multiple, "three"));
assert_se(!strv_find((char **)input_table_multiple, "four"));
}
static void test_strv_find_prefix(void) {
const char * const input_table[] = {
"one",
"two",
"three",
NULL
};
assert_se(strv_find_prefix((char **)input_table, "o"));
assert_se(strv_find_prefix((char **)input_table, "one"));
assert_se(strv_find_prefix((char **)input_table, ""));
assert_se(!strv_find_prefix((char **)input_table, "xxx"));
assert_se(!strv_find_prefix((char **)input_table, "onee"));
assert_se(strv_find_prefix((char **)input_table_multiple, "o"));
assert_se(strv_find_prefix((char **)input_table_multiple, "one"));
assert_se(strv_find_prefix((char **)input_table_multiple, ""));
assert_se(!strv_find_prefix((char **)input_table_multiple, "xxx"));
assert_se(!strv_find_prefix((char **)input_table_multiple, "onee"));
}
static void test_strv_join(void) {
_cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL;
const char * const input_table_multiple[] = {
"one",
"two",
"three",
NULL
};
const char * const input_table_one[] = {
"one",
NULL
};
const char * const input_table_none[] = {
NULL
};
p = strv_join((char **)input_table_multiple, ", ");
assert_se(p);
assert_se(streq(p, "one, two, three"));
@@ -107,6 +125,25 @@ static void test_strv_join(void) {
assert_se(streq(t, ""));
}
static void test_strv_quote_unquote(const char* const *split, const char *quoted) {
_cleanup_free_ char *p;
_cleanup_strv_free_ char **s;
char **t;
p = strv_join_quoted((char **)split);
printf("-%s- --- -%s-\n", p, quoted); /* fprintf deals with NULL, puts does not */
assert_se(p);
assert_se(streq(p, quoted));
s = strv_split_quoted(quoted);
assert_se(s);
STRV_FOREACH(t, s) {
assert_se(*t);
assert_se(streq(*t, *split));
split++;
}
}
static void test_strv_split_nulstr(void) {
_cleanup_strv_free_ char **l = NULL;
const char nulstr[] = "str0\0str1\0str2\0str3\0";
@@ -253,6 +290,13 @@ int main(int argc, char *argv[]) {
test_strv_find();
test_strv_find_prefix();
test_strv_join();
test_strv_quote_unquote(input_table_multiple, "\"one\" \"two\" \"three\"");
test_strv_quote_unquote(input_table_one, "\"one\"");
test_strv_quote_unquote(input_table_none, "");
test_strv_quote_unquote(input_table_quotes, QUOTES_STRING);
test_strv_quote_unquote(input_table_spaces, SPACES_STRING);
test_strv_split_nulstr();
test_strv_parse_nulstr();
test_strv_overlap();