mirror of
https://github.com/flatpak/flatpak.git
synced 2026-03-10 11:12:46 -04:00
tests: Test flatpak_quote_argv and flatpak_argument_needs_quoting
Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
committed by
Alexander Larsson
parent
db7d9cfeb5
commit
d8a8ac2edb
@@ -1621,6 +1621,128 @@ test_envp_cmp (void)
|
||||
g_free (sort_this);
|
||||
}
|
||||
|
||||
static void
|
||||
test_needs_quoting (void)
|
||||
{
|
||||
static const char * const needs_quoting[] =
|
||||
{
|
||||
"",
|
||||
"$var",
|
||||
"{}",
|
||||
"()",
|
||||
"[]",
|
||||
"*",
|
||||
"?",
|
||||
"`exec`",
|
||||
"has space",
|
||||
"quoted-\"",
|
||||
"quoted-'",
|
||||
"back\\slash",
|
||||
"control\001char",
|
||||
};
|
||||
static const char * const does_not_need_quoting[] =
|
||||
{
|
||||
"foo",
|
||||
"--foo=bar",
|
||||
"-x",
|
||||
"foo@bar:/srv/big_files",
|
||||
"~smcv",
|
||||
"7-zip.org",
|
||||
};
|
||||
gsize i;
|
||||
|
||||
for (i = 0; i < G_N_ELEMENTS (needs_quoting); i++)
|
||||
{
|
||||
const char *orig = needs_quoting[i];
|
||||
g_autoptr(GError) error = NULL;
|
||||
g_autofree char *quoted = NULL;
|
||||
int argc = -1;
|
||||
g_auto(GStrv) argv = NULL;
|
||||
gboolean ok;
|
||||
|
||||
g_assert_true (flatpak_argument_needs_quoting (orig));
|
||||
quoted = flatpak_quote_argv (&orig, 1);
|
||||
g_test_message ("Unquoted: \"%s\"", orig);
|
||||
g_test_message (" Quoted: \"%s\"", quoted);
|
||||
g_assert_cmpstr (quoted, !=, orig);
|
||||
|
||||
ok = g_shell_parse_argv (quoted, &argc, &argv, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert_true (ok);
|
||||
g_assert_cmpint (argc, ==, 1);
|
||||
g_assert_nonnull (argv);
|
||||
g_assert_cmpstr (argv[0], ==, orig);
|
||||
g_assert_cmpstr (argv[1], ==, NULL);
|
||||
}
|
||||
|
||||
for (i = 0; i < G_N_ELEMENTS (does_not_need_quoting); i++)
|
||||
{
|
||||
const char *orig = does_not_need_quoting[i];
|
||||
g_autoptr(GError) error = NULL;
|
||||
g_autofree char *quoted = NULL;
|
||||
int argc = -1;
|
||||
g_auto(GStrv) argv = NULL;
|
||||
gboolean ok;
|
||||
|
||||
g_assert_false (flatpak_argument_needs_quoting (orig));
|
||||
quoted = flatpak_quote_argv (&orig, 1);
|
||||
g_assert_cmpstr (quoted, ==, orig);
|
||||
|
||||
ok = g_shell_parse_argv (quoted, &argc, &argv, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert_true (ok);
|
||||
g_assert_cmpint (argc, ==, 1);
|
||||
g_assert_nonnull (argv);
|
||||
g_assert_cmpstr (argv[0], ==, orig);
|
||||
g_assert_cmpstr (argv[1], ==, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_quote_argv (void)
|
||||
{
|
||||
static const char * const orig[] =
|
||||
{
|
||||
"foo",
|
||||
"--bar",
|
||||
"",
|
||||
"baz",
|
||||
NULL
|
||||
};
|
||||
gsize i;
|
||||
g_autofree char *quoted = NULL;
|
||||
g_autoptr(GError) error = NULL;
|
||||
int argc = -1;
|
||||
g_auto(GStrv) argv = NULL;
|
||||
gboolean ok;
|
||||
|
||||
quoted = flatpak_quote_argv ((const char **) orig, -1);
|
||||
ok = g_shell_parse_argv (quoted, &argc, &argv, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert_true (ok);
|
||||
g_assert_cmpint (argc, >, 0);
|
||||
g_assert_cmpuint ((gsize) argc, ==, G_N_ELEMENTS (orig) - 1);
|
||||
g_assert_nonnull (argv);
|
||||
|
||||
for (i = 0; i < G_N_ELEMENTS (orig); i++)
|
||||
g_assert_cmpstr (argv[i], ==, orig[i]);
|
||||
|
||||
g_clear_pointer ("ed, g_free);
|
||||
g_clear_pointer (&argv, g_strfreev);
|
||||
|
||||
quoted = flatpak_quote_argv ((const char **) orig, 3);
|
||||
ok = g_shell_parse_argv (quoted, &argc, &argv, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert_true (ok);
|
||||
g_assert_cmpint (argc, ==, 3);
|
||||
g_assert_nonnull (argv);
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
g_assert_cmpstr (argv[i], ==, orig[i]);
|
||||
|
||||
g_assert_cmpstr (argv[i], ==, NULL);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
@@ -1650,6 +1772,8 @@ main (int argc, char *argv[])
|
||||
g_test_add_func ("/common/dconf-paths", test_dconf_paths);
|
||||
g_test_add_func ("/common/decompose-ref", test_decompose);
|
||||
g_test_add_func ("/common/envp-cmp", test_envp_cmp);
|
||||
g_test_add_func ("/common/needs-quoting", test_needs_quoting);
|
||||
g_test_add_func ("/common/quote-argv", test_quote_argv);
|
||||
|
||||
g_test_add_func ("/app/looks-like-branch", test_looks_like_branch);
|
||||
g_test_add_func ("/app/columns", test_columns);
|
||||
|
||||
Reference in New Issue
Block a user