1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct _CmdlineTest CmdlineTest;
struct _CmdlineTest
{
const gchar *cmdline;
gint argc;
const gchar *argv[10];
gint error_code;
};
static CmdlineTest cmdline_tests[] =
{
{ "foo bar", 2, { "foo", "bar", NULL }, -1 },
{ "foo 'bar'", 2, { "foo", "bar", NULL }, -1 },
{ "foo \"bar\"", 2, { "foo", "bar", NULL }, -1 },
{ "foo '' 'bar'", 3, { "foo", "", "bar", NULL }, -1 },
{ "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"", 2, { "foo", "barbazblahfoo'blahboo", NULL }, -1 },
{ "foo \t \tblah\tfoo\t\tbar baz", 5, { "foo", "blah", "foo", "bar", "baz", NULL }, -1 },
{ "foo ' spaces more spaces lots of spaces in this ' \t", 2, { "foo", " spaces more spaces lots of spaces in this ", NULL }, -1 },
{ "foo \\\nbar", 2, { "foo", "bar", NULL }, -1 },
{ "foo '' ''", 3, { "foo", "", "", NULL }, -1 },
{ "foo \\\" la la la", 5, { "foo", "\"", "la", "la", "la", NULL }, -1 },
{ "foo \\ foo woo woo\\ ", 4, { "foo", " foo", "woo", "woo ", NULL }, -1 },
{ "foo \"yada yada \\$\\\"\"", 2, { "foo", "yada yada $\"", NULL }, -1 },
{ "foo \"c:\\\\\"", 2, { "foo", "c:\\", NULL }, -1 },
{ "foo # bla bla bla\n bar", 2, { "foo", "bar", NULL }, -1 },
{ "foo a#b", 2, { "foo", "a#b", NULL }, -1 },
{ "#foo", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
{ "foo bar \\", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
{ "foo 'bar baz", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
{ "foo '\"bar\" baz", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
{ "", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
{ " ", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
{ "# foo bar", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
{"foo '/bar/summer'\\''09 tours.pdf'", 2, {"foo", "/bar/summer'09 tours.pdf", NULL}, -1}
};
static void
do_cmdline_test (gconstpointer d)
{
const CmdlineTest *test = d;
gint argc;
gchar **argv;
GError *err;
gboolean res;
err = NULL;
g_printerr ("test cmdline: %s\n", test->cmdline);
res = g_shell_parse_argv (test->cmdline, &argc, &argv, &err);
if (test->error_code == -1)
{
g_assert (res);
g_assert_cmpint (argc, ==, test->argc);
g_assert (g_strv_equal ((const gchar * const *) argv, (const gchar * const *) test->argv));
g_assert_no_error (err);
}
else
{
g_assert (!res);
g_assert_error (err, G_SHELL_ERROR, test->error_code);
}
if (err)
g_error_free (err);
if (res)
g_strfreev (argv);
}
typedef struct _QuoteTest QuoteTest;
struct _QuoteTest
{
const gchar *in;
const gchar *out;
};
static QuoteTest quote_tests[] =
{
{ "", "''" },
{ "a", "'a'" },
{ "(", "'('" },
{ "'", "''\\'''" },
{ "'a", "''\\''a'" },
{ "a'", "'a'\\'''" },
{ "a'a", "'a'\\''a'" }
};
static void
do_quote_test (gconstpointer d)
{
const QuoteTest *test = d;
gchar *out;
out = g_shell_quote (test->in);
g_assert_cmpstr (out, ==, test->out);
g_free (out);
}
typedef struct _UnquoteTest UnquoteTest;
struct _UnquoteTest
{
const gchar *in;
const gchar *out;
gint error_code;
};
static UnquoteTest unquote_tests[] =
{
{ "", "", -1 },
{ "a", "a", -1 },
{ "'a'", "a", -1 },
{ "'('", "(", -1 },
{ "''\\'''", "'", -1 },
{ "''\\''a'", "'a", -1 },
{ "'a'\\'''", "a'", -1 },
{ "'a'\\''a'", "a'a", -1 },
{ "\\\\", "\\", -1 },
{ "\\\n", "", -1 },
{ "'\\''", NULL, G_SHELL_ERROR_BAD_QUOTING },
{ "\"\\\"\"", "\"", -1 },
{ "\"", NULL, G_SHELL_ERROR_BAD_QUOTING },
{ "'", NULL, G_SHELL_ERROR_BAD_QUOTING },
{ "\x22\\\\\"", "\\", -1 },
{ "\x22\\`\"", "`", -1 },
{ "\x22\\$\"", "$", -1 },
{ "\x22\\\n\"", "\n", -1 },
{ "\"\\'\"", "\\'", -1 },
{ "\x22\\\r\"", "\\\r", -1 },
{ "\x22\\n\"", "\\n", -1 }
};
static void
do_unquote_test (gconstpointer d)
{
const UnquoteTest *test = d;
gchar *out;
GError *error;
error = NULL;
out = g_shell_unquote (test->in, &error);
g_assert_cmpstr (out, ==, test->out);
if (test->error_code == -1)
g_assert_no_error (error);
else
g_assert_error (error, G_SHELL_ERROR, test->error_code);
g_free (out);
if (error)
g_error_free (error);
}
int
main (int argc, char *argv[])
{
gsize i;
gchar *path;
g_test_init (&argc, &argv, NULL);
for (i = 0; i < G_N_ELEMENTS (cmdline_tests); i++)
{
path = g_strdup_printf ("/shell/cmdline/%" G_GSIZE_FORMAT, i);
g_test_add_data_func (path, &cmdline_tests[i], do_cmdline_test);
g_free (path);
}
for (i = 0; i < G_N_ELEMENTS (quote_tests); i++)
{
path = g_strdup_printf ("/shell/quote/%" G_GSIZE_FORMAT, i);
g_test_add_data_func (path, "e_tests[i], do_quote_test);
g_free (path);
}
for (i = 0; i < G_N_ELEMENTS (unquote_tests); i++)
{
path = g_strdup_printf ("/shell/unquote/%" G_GSIZE_FORMAT, i);
g_test_add_data_func (path, &unquote_tests[i], do_unquote_test);
g_free (path);
}
return g_test_run ();
}
|