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