Tizen 2.1 base
[platform/upstream/glib2.0.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 bar \\", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
63   { "foo 'bar baz", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
64   { "foo '\"bar\" baz", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
65   { "", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
66   { "  ", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
67   { "# foo bar", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING }
68 };
69
70 static gboolean
71 strv_equal (gchar **a, gchar **b)
72 {
73   gint i;
74
75   if (g_strv_length (a) != g_strv_length (b))
76     return FALSE;
77
78   for (i = 0; a[i]; i++)
79     if (g_strcmp0 (a[i], b[i]) != 0)
80       return FALSE;
81
82   return TRUE;
83 }
84
85 static void
86 do_cmdline_test (gconstpointer d)
87 {
88   const CmdlineTest *test = d;
89   gint argc;
90   gchar **argv;
91   GError *err;
92   gboolean res;
93
94   err = NULL;
95   res = g_shell_parse_argv (test->cmdline, &argc, &argv, &err);
96   if (test->error_code == -1)
97     {
98       g_assert (res);
99       g_assert_cmpint (argc, ==, test->argc);
100       g_assert (strv_equal (argv, (gchar **)test->argv));
101       g_assert_no_error (err);
102     }
103   else
104     {
105       g_assert (!res);
106       g_assert_error (err, G_SHELL_ERROR, test->error_code);
107     }
108
109   if (err)
110     g_error_free (err);
111   if (res)
112     g_strfreev (argv);
113 }
114
115 typedef struct _QuoteTest QuoteTest;
116
117 struct _QuoteTest
118 {
119   const gchar *in;
120   const gchar *out;
121 };
122
123 static QuoteTest quote_tests[] =
124 {
125   { "", "''" },
126   { "a", "'a'" },
127   { "(", "'('" },
128   { "'", "''\\'''" },
129   { "'a", "''\\''a'" },
130   { "a'", "'a'\\'''" },
131   { "a'a", "'a'\\''a'" }
132 };
133
134 static void
135 do_quote_test (gconstpointer d)
136 {
137   const QuoteTest *test = d;
138   gchar *out;
139
140   out = g_shell_quote (test->in);
141   g_assert_cmpstr (out, ==, test->out);
142   g_free (out);
143 }
144
145 typedef struct _UnquoteTest UnquoteTest;
146
147 struct _UnquoteTest
148 {
149   const gchar *in;
150   const gchar *out;
151   gint error_code;
152 };
153
154 static UnquoteTest unquote_tests[] =
155 {
156   { "", "", -1 },
157   { "a", "a", -1 },
158   { "'a'", "a", -1 },
159   { "'('", "(", -1 },
160   { "''\\'''", "'", -1 },
161   { "''\\''a'", "'a", -1 },
162   { "'a'\\'''", "a'", -1 },
163   { "'a'\\''a'", "a'a", -1 },
164   { "\\\\", "\\", -1 },
165   { "\\\n", "", -1 },
166   { "'\\''", NULL, G_SHELL_ERROR_BAD_QUOTING },
167   { "\"\\\"\"", "\"", -1 },
168   { "\"", NULL, G_SHELL_ERROR_BAD_QUOTING },
169   { "'", NULL, G_SHELL_ERROR_BAD_QUOTING },
170   { "\x22\\\\\"", "\\", -1 },
171   { "\x22\\`\"", "`", -1 },
172   { "\x22\\$\"", "$", -1 },
173   { "\x22\\\n\"", "\n", -1 },
174   { "\"\\'\"", "\\'", -1 },
175   { "\x22\\\r\"", "\\\r", -1 },
176   { "\x22\\n\"", "\\n", -1 }
177 };
178
179 static void
180 do_unquote_test (gconstpointer d)
181 {
182   const UnquoteTest *test = d;
183   gchar *out;
184   GError *error;
185
186   error = NULL;
187   out = g_shell_unquote (test->in, &error);
188   g_assert_cmpstr (out, ==, test->out);
189   if (test->error_code == -1)
190     g_assert_no_error (error);
191   else
192     g_assert_error (error, G_SHELL_ERROR, test->error_code);
193
194   g_free (out);
195   if (error)
196     g_error_free (error);
197 }
198
199 int
200 main (int   argc, char *argv[])
201 {
202   gint i;
203   gchar *path;
204
205   g_test_init (&argc, &argv, NULL);
206
207   for (i = 0; i < G_N_ELEMENTS (cmdline_tests); i++)
208     {
209       path = g_strdup_printf ("/shell/cmdline/%d", i);
210       g_test_add_data_func (path, &cmdline_tests[i], do_cmdline_test);
211       g_free (path);
212     }
213
214   for (i = 0; i < G_N_ELEMENTS (quote_tests); i++)
215     {
216       path = g_strdup_printf ("/shell/quote/%d", i);
217       g_test_add_data_func (path, &quote_tests[i], do_quote_test);
218       g_free (path);
219     }
220
221   for (i = 0; i < G_N_ELEMENTS (unquote_tests); i++)
222     {
223       path = g_strdup_printf ("/shell/unquote/%d", i);
224       g_test_add_data_func (path, &unquote_tests[i], do_unquote_test);
225       g_free (path);
226     }
227
228   return g_test_run ();
229 }