Update.
[glibc.git] / posix / wordexp-test.c
blob1815241fca874fd1823376dd1c5f4501cb1182bd
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", }, "" },
54 { 0, ":abc:", "$var", 0, 3, { "", "abc", "", }, ":" },
55 { 0, NULL, "$(echo :abc:)", 0, 3, { "", "abc", "", }, ":" },
56 { 0, NULL, "$(echo :abc:\\ )", 0, 3, { "", "abc", "", }, ": " },
57 { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " },
58 { 0, ":abc:", "$(echo $var)", 0, 3, { "", "abc", "", }, ":" },
59 { 0, NULL, ":abc:", 0, 1, { " abc ", }, ":" },
60 { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", }, ":" },
61 { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" },
62 { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def ghi", }, ":" },
63 { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def", "ghi", }, ":" },
64 { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def", "ghi", }, ":" },
65 { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def", "ghi", }, ":" },
66 { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def", "ghi", }, ":" },
68 /* Simple parameter expansion */
69 { 0, "foo", "${var}", 0, 1, { "foo", }, IFS },
70 { 0, "foo", "$var", 0, 1, { "foo", }, IFS },
71 { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS },
72 { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS },
73 { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS },
75 /* Simple quote removal */
76 { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS },
77 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS },
78 { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS },
80 /* Simple command substitution */
81 { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS },
82 { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS },
83 { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS },
84 { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS },
85 { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS },
86 { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS },
88 /* Simple arithmetic expansion */
89 { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS },
90 { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS },
91 { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS },
92 { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS },
93 { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS },
95 /* Advanced parameter expansion */
96 { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS },
97 { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS },
98 { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS },
99 { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS },
100 { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS },
101 { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS },
102 { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS },
103 { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS },
104 { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS },
105 { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS },
106 { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS },
107 { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS },
108 { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS },
109 { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS },
110 { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS },
111 { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS },
112 { 0, "12345", "${#var}", 0, 1, { "5", }, IFS },
113 { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS },
114 { 0, NULL, "${var-}", 0, 0, { NULL }, IFS },
116 { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS },
117 { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS },
118 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS },
119 { 0, "borabora-island", "${var##*bora}", 0, 1, {"-island", }, IFS },
120 { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS },
122 /* Pathname expansion */
123 { 0, NULL, "???", 0, 2, { "one", "two", }, IFS },
124 { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS },
125 { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS },
126 { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS },
128 /* Nested constructs */
129 { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS },
130 { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS },
131 { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS },
132 { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
133 { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" },
134 { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS },
135 { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS },
136 { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS },
137 { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS },
138 { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS },
139 { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS },
140 { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
142 /* Different IFS values */
143 { 0, "a b\tc\nd ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
144 { 0, "a b\tc d ", "$var", 0, 1, { "a b\tc d " }, "" /* `null' */ },
145 { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
147 /* Other things that should succeed */
148 { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
149 { 0, "???", "$var", 0, 1, { "???", }, IFS },
150 { 0, NULL, "$var", 0, 0, { NULL, }, IFS },
151 { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS },
152 { 0, NULL, "", 0, 0, { NULL, }, IFS },
154 /* Things that should fail */
155 { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ },
156 { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS },
157 { WRDE_BADCHAR, NULL, "&ampersand", 0, 0, { NULL, }, IFS },
158 { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS },
159 { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS },
160 { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS },
161 { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS },
162 { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS },
163 { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS },
164 { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS },
165 { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS },
166 { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS },
167 { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS },
168 { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS },
169 { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS },
170 { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS },
171 { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
172 { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
174 { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
177 static int testit (struct test_case_struct *tc);
178 static int tests;
180 void
181 command_line_test (const char *words)
183 wordexp_t we;
184 int i;
185 int retval = wordexp (words, &we, 0);
186 printf ("wordexp returned %d\n", retval);
187 for (i = 0; i < we.we_wordc; i++)
188 printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
192 main (int argc, char *argv[])
194 const char *globfile[] = { "one", "two", "three", NULL };
195 char tmpdir[32];
196 struct passwd *pw;
197 const char *cwd;
198 int test;
199 int fail = 0;
200 int i;
202 if (argc > 1)
204 command_line_test (argv[1]);
205 return 0;
208 cwd = getcwd (NULL, 0);
210 /* Set up arena for pathname expansion */
211 tmpnam (tmpdir);
212 if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir))
213 return -1;
214 else
216 int fd;
218 for (i = 0; globfile[i]; ++i)
219 if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1
220 || close (fd))
221 return -1;
224 for (test = 0; test_case[test].retval != -1; test++)
225 if (testit (&test_case[test]))
226 ++fail;
228 pw = getpwnam ("root");
229 if (pw != NULL)
231 struct test_case_struct ts;
233 ts.retval = 0;
234 ts.env = NULL;
235 ts.words = "~root";
236 ts.flags = 0;
237 ts.wordc = 1;
238 ts.wordv[0] = pw->pw_dir;
239 ts.ifs = IFS;
241 if (testit (&ts))
242 ++fail;
245 puts ("tests completed, now cleaning up");
247 /* Clean up */
248 for (i = 0; globfile[i]; ++i)
249 remove (globfile[i]);
251 if (cwd == NULL)
252 cwd = "..";
254 chdir (cwd);
255 rmdir (tmpdir);
257 printf ("tests failed: %d\n", fail);
259 return fail != 0;
263 static int
264 testit (struct test_case_struct *tc)
266 int retval;
267 wordexp_t we;
268 int bzzzt = 0;
269 int i;
271 if (tc->env)
272 setenv ("var", tc->env, 1);
273 else
274 unsetenv ("var");
276 if (tc->ifs)
277 setenv ("IFS", tc->ifs, 1);
278 else
279 unsetenv ("IFS");
281 printf ("Test %d (%s): ", ++tests, tc->words);
282 retval = wordexp (tc->words, &we, tc->flags);
284 if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
285 bzzzt = 1;
286 else
287 for (i = 0; i < we.we_wordc; ++i)
288 if (strcmp (tc->wordv[i], we.we_wordv[i]) != 0)
290 bzzzt = 1;
291 break;
294 if (bzzzt)
296 printf ("FAILED\n");
297 printf ("Test words: <%s>, need retval %d, wordc %d\n",
298 tc->words, tc->retval, tc->wordc);
299 printf ("Got retval %d, wordc %d: ", retval, we.we_wordc);
300 for (i = 0; i < we.we_wordc; ++i)
301 printf ("<%s> ", we.we_wordv[i]);
302 printf ("\n");
304 else
305 printf ("OK\n");
307 wordfree (&we);
309 return bzzzt;