Update.
[glibc.git] / posix / wordexp-test.c
blob56bac3f3ea96bb7886ea890220c66e0633b972fd
1 /* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <pwd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <wordexp.h>
28 #define IFS " \n\t"
30 struct test_case_struct
32 int retval;
33 const char *env;
34 const char *words;
35 int flags;
36 int wordc;
37 const char *wordv[10];
38 const char *ifs;
39 } test_case[] =
41 /* Simple word- and field-splitting */
42 { 0, NULL, "one", 0, 1, { "one", }, IFS },
43 { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS },
44 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS },
45 { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS },
46 { 0, NULL, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," },
47 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" },
48 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS },
49 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" },
50 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS },
51 { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS },
52 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" },
53 { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" },
55 /* The non-whitespace IFS char at the end delimits the second field
56 * but does NOT start a new field. */
57 { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" },
59 { 0, NULL, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" },
60 { 0, NULL, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " },
61 { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " },
62 { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" },
63 { 0, NULL, ":abc:", 0, 1, { ":abc:", }, ":" },
64 { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", },
65 ":" },
66 { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" },
67 { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", },
68 ":" },
69 { 0, NULL, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", },
70 ":" },
71 { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def",
72 "ghi", }, ":" },
73 { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def",
74 "ghi", }, ":" },
75 { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def",
76 "ghi", }, ":" },
77 { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def",
78 "ghi", }, ":" },
80 /* Simple parameter expansion */
81 { 0, "foo", "${var}", 0, 1, { "foo", }, IFS },
82 { 0, "foo", "$var", 0, 1, { "foo", }, IFS },
83 { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS },
84 { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS },
85 { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS },
87 /* Simple quote removal */
88 { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS },
89 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS },
90 { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS },
91 { 0, NULL, "contin\\\nuation", 0, 1, { "continuation", }, IFS },
92 { 0, NULL, "explicit ''", 0, 2, { "explicit", "", }, IFS },
93 { 0, NULL, "explicit \"\"", 0, 2, { "explicit", "", }, IFS },
94 { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS },
96 /* Simple command substitution */
97 { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS },
98 { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS },
99 { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS },
100 { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS },
101 { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS },
102 { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS },
104 /* Simple arithmetic expansion */
105 { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS },
106 { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS },
107 { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS },
108 { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS },
109 { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS },
111 /* Advanced parameter expansion */
112 { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS },
113 { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS },
114 { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS },
115 { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS },
116 { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS },
117 { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS },
118 { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS },
119 { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS },
120 { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS },
121 { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS },
122 { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS },
123 { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS },
124 { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS },
125 { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS },
126 { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS },
127 { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS },
128 { 0, "12345", "${#var}", 0, 1, { "5", }, IFS },
129 { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS },
130 { 0, NULL, "${var-}", 0, 0, { NULL }, IFS },
132 { 0, "pizza", "${var#${var}}", 0, 0, { NULL }, IFS },
133 { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS },
134 { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS },
135 { 0, "b*witched", "${var##b*}", 0, 0, { NULL }, IFS },
136 { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS },
137 { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS },
138 { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS },
139 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS },
140 { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS },
141 { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS },
142 { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS },
144 /* Pathname expansion */
145 { 0, NULL, "???", 0, 2, { "one", "two", }, IFS },
146 { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS },
147 { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS },
148 { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS },
150 /* Nested constructs */
151 { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS },
152 { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS },
153 { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS },
154 { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
155 { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" },
156 { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS },
157 { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS },
158 { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS },
159 { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS },
160 { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS },
161 { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS },
162 { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
164 /* Different IFS values */
165 { 0, "a b\tc\nd ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
166 { 0, "a b\tc d ", "$var", 0, 1, { "a b\tc d " }, "" /* `null' */ },
167 { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
169 /* Other things that should succeed */
170 { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
171 { 0, "???", "$var", 0, 1, { "???", }, IFS },
172 { 0, NULL, "$var", 0, 0, { NULL, }, IFS },
173 { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS },
174 { 0, NULL, "", 0, 0, { NULL, }, IFS },
176 /* Things that should fail */
177 { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ },
178 { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS },
179 { WRDE_BADCHAR, NULL, "&ampersand", 0, 0, { NULL, }, IFS },
180 { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS },
181 { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS },
182 { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS },
183 { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS },
184 { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS },
185 { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS },
186 { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS },
187 { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS },
188 { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS },
189 { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS },
190 { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS },
191 { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS },
192 { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS },
193 { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
194 { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
196 { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
199 static int testit (struct test_case_struct *tc);
200 static int tests;
202 void
203 command_line_test (const char *words)
205 wordexp_t we;
206 int i;
207 int retval = wordexp (words, &we, 0);
208 printf ("wordexp returned %d\n", retval);
209 for (i = 0; i < we.we_wordc; i++)
210 printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
214 main (int argc, char *argv[])
216 const char *globfile[] = { "one", "two", "three", NULL };
217 char tmpdir[32];
218 struct passwd *pw;
219 const char *cwd;
220 int test;
221 int fail = 0;
222 int i;
224 if (argc > 1)
226 command_line_test (argv[1]);
227 return 0;
230 cwd = getcwd (NULL, 0);
232 /* Set up arena for pathname expansion */
233 tmpnam (tmpdir);
234 if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir))
235 return -1;
236 else
238 int fd;
240 for (i = 0; globfile[i]; ++i)
241 if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1
242 || close (fd))
243 return -1;
246 for (test = 0; test_case[test].retval != -1; test++)
247 if (testit (&test_case[test]))
248 ++fail;
250 /* Tilde-expansion tests. */
251 pw = getpwnam ("root");
252 if (pw != NULL)
254 struct test_case_struct ts;
256 ts.retval = 0;
257 ts.env = NULL;
258 ts.words = "~root";
259 ts.flags = 0;
260 ts.wordc = 1;
261 ts.wordv[0] = pw->pw_dir;
262 ts.ifs = IFS;
264 if (testit (&ts))
265 ++fail;
267 ts.retval = 0;
268 ts.env = pw->pw_dir;
269 ts.words = "${var#~root}x";
270 ts.flags = 0;
271 ts.wordc = 1;
272 ts.wordv[0] = "x";
273 ts.ifs = IFS;
275 if (testit (&ts))
276 ++fail;
279 puts ("tests completed, now cleaning up");
281 /* Clean up */
282 for (i = 0; globfile[i]; ++i)
283 remove (globfile[i]);
285 if (cwd == NULL)
286 cwd = "..";
288 chdir (cwd);
289 rmdir (tmpdir);
291 printf ("tests failed: %d\n", fail);
293 return fail != 0;
297 static int
298 testit (struct test_case_struct *tc)
300 int retval;
301 wordexp_t we;
302 int bzzzt = 0;
303 int i;
305 if (tc->env)
306 setenv ("var", tc->env, 1);
307 else
308 unsetenv ("var");
310 if (tc->ifs)
311 setenv ("IFS", tc->ifs, 1);
312 else
313 unsetenv ("IFS");
315 printf ("Test %d (%s): ", ++tests, tc->words);
316 retval = wordexp (tc->words, &we, tc->flags);
318 if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
319 bzzzt = 1;
320 else
321 for (i = 0; i < we.we_wordc; ++i)
322 if (strcmp (tc->wordv[i], we.we_wordv[i]) != 0)
324 bzzzt = 1;
325 break;
328 if (bzzzt)
330 printf ("FAILED\n");
331 printf ("Test words: <%s>, need retval %d, wordc %d\n",
332 tc->words, tc->retval, tc->wordc);
333 printf ("Got retval %d, wordc %d: ", retval, we.we_wordc);
334 for (i = 0; i < we.we_wordc; ++i)
335 printf ("<%s> ", we.we_wordv[i]);
336 printf ("\n");
338 else
339 printf ("OK\n");
341 if (retval == 0 || retval == WRDE_NOSPACE)
342 wordfree (&we);
344 return bzzzt;