Fix shell tests
[platform/upstream/glib.git] / glib / tests / shell.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
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 };
71
72 static gboolean
73 strv_equal (gchar **a, gchar **b)
74 {
75   gint i;
76
77   if (g_strv_length (a) != g_strv_length (b))
78     return FALSE;
79
80   for (i = 0; a[i]; i++)
81     if (g_strcmp0 (a[i], b[i]) != 0)
82       return FALSE;
83
84   return TRUE;
85 }
86
87 static void
88 do_cmdline_test (gconstpointer d)
89 {
90   const CmdlineTest *test = d;
91   gint argc;
92   gchar **argv;
93   GError *err;
94   gboolean res;
95
96   err = NULL;
97 g_print ("test cmdline: %s\n", test->cmdline);
98   res = g_shell_parse_argv (test->cmdline, &argc, &argv, &err);
99   if (test->error_code == -1)
100     {
101       g_assert (res);
102       g_assert_cmpint (argc, ==, test->argc);
103       g_assert (strv_equal (argv, (gchar **)test->argv));
104       g_assert_no_error (err);
105     }
106   else
107     {
108       g_assert (!res);
109       g_assert_error (err, G_SHELL_ERROR, test->error_code);
110     }
111
112   if (err)
113     g_error_free (err);
114   if (res)
115     g_strfreev (argv);
116 }
117
118 typedef struct _QuoteTest QuoteTest;
119
120 struct _QuoteTest
121 {
122   const gchar *in;
123   const gchar *out;
124 };
125
126 static QuoteTest quote_tests[] =
127 {
128   { "", "''" },
129   { "a", "'a'" },
130   { "(", "'('" },
131   { "'", "''\\'''" },
132   { "'a", "''\\''a'" },
133   { "a'", "'a'\\'''" },
134   { "a'a", "'a'\\''a'" }
135 };
136
137 static void
138 do_quote_test (gconstpointer d)
139 {
140   const QuoteTest *test = d;
141   gchar *out;
142
143   out = g_shell_quote (test->in);
144   g_assert_cmpstr (out, ==, test->out);
145   g_free (out);
146 }
147
148 typedef struct _UnquoteTest UnquoteTest;
149
150 struct _UnquoteTest
151 {
152   const gchar *in;
153   const gchar *out;
154   gint error_code;
155 };
156
157 static UnquoteTest unquote_tests[] =
158 {
159   { "", "", -1 },
160   { "a", "a", -1 },
161   { "'a'", "a", -1 },
162   { "'('", "(", -1 },
163   { "''\\'''", "'", -1 },
164   { "''\\''a'", "'a", -1 },
165   { "'a'\\'''", "a'", -1 },
166   { "'a'\\''a'", "a'a", -1 },
167   { "\\\\", "\\", -1 },
168   { "\\\n", "", -1 },
169   { "'\\''", NULL, G_SHELL_ERROR_BAD_QUOTING },
170   { "\"\\\"\"", "\"", -1 },
171   { "\"", NULL, G_SHELL_ERROR_BAD_QUOTING },
172   { "'", NULL, G_SHELL_ERROR_BAD_QUOTING },
173   { "\x22\\\\\"", "\\", -1 },
174   { "\x22\\`\"", "`", -1 },
175   { "\x22\\$\"", "$", -1 },
176   { "\x22\\\n\"", "\n", -1 },
177   { "\"\\'\"", "\\'", -1 },
178   { "\x22\\\r\"", "\\\r", -1 },
179   { "\x22\\n\"", "\\n", -1 }
180 };
181
182 static void
183 do_unquote_test (gconstpointer d)
184 {
185   const UnquoteTest *test = d;
186   gchar *out;
187   GError *error;
188
189   error = NULL;
190   out = g_shell_unquote (test->in, &error);
191   g_assert_cmpstr (out, ==, test->out);
192   if (test->error_code == -1)
193     g_assert_no_error (error);
194   else
195     g_assert_error (error, G_SHELL_ERROR, test->error_code);
196
197   g_free (out);
198   if (error)
199     g_error_free (error);
200 }
201
202 int
203 main (int   argc, char *argv[])
204 {
205   gint i;
206   gchar *path;
207
208   g_test_init (&argc, &argv, NULL);
209
210   for (i = 0; i < G_N_ELEMENTS (cmdline_tests); i++)
211     {
212       path = g_strdup_printf ("/shell/cmdline/%d", i);
213       g_test_add_data_func (path, &cmdline_tests[i], do_cmdline_test);
214       g_free (path);
215     }
216
217   for (i = 0; i < G_N_ELEMENTS (quote_tests); i++)
218     {
219       path = g_strdup_printf ("/shell/quote/%d", i);
220       g_test_add_data_func (path, &quote_tests[i], do_quote_test);
221       g_free (path);
222     }
223
224   for (i = 0; i < G_N_ELEMENTS (unquote_tests); i++)
225     {
226       path = g_strdup_printf ("/shell/unquote/%d", i);
227       g_test_add_data_func (path, &unquote_tests[i], do_unquote_test);
228       g_free (path);
229     }
230
231   return g_test_run ();
232 }