m68k: fix missing definition of __feraiseexcept
[glibc.git] / posix / wordexp-test.c
blob8a312e0bcd83bf98ab6ca2e7d443ed9b5051fd5f
1 /* Copyright (C) 1997-2015 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <wordexp.h>
28 #define IFS " \n\t"
30 extern void *__dso_handle __attribute__ ((__weak__, __visibility__ ("hidden")));
31 extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *);
33 static int __app_register_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void))
35 return __register_atfork (prepare, parent, child,
36 &__dso_handle == NULL ? NULL : __dso_handle);
39 /* Number of forks seen. */
40 static int registered_forks;
42 /* For each fork increment the fork count. */
43 static void
44 register_fork (void)
46 registered_forks++;
49 struct test_case_struct
51 int retval;
52 const char *env;
53 const char *words;
54 int flags;
55 size_t wordc;
56 const char *wordv[10];
57 const char *ifs;
58 } test_case[] =
60 /* Simple word- and field-splitting */
61 { 0, NULL, "one", 0, 1, { "one", }, IFS },
62 { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS },
63 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS },
64 { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS },
65 { 0, NULL, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," },
66 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" },
67 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS },
68 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" },
69 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS },
70 { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS },
71 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" },
72 { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" },
74 /* The non-whitespace IFS char at the end delimits the second field
75 * but does NOT start a new field. */
76 { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" },
78 { 0, NULL, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" },
79 { 0, NULL, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " },
80 { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " },
81 { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" },
82 { 0, NULL, ":abc:", 0, 1, { ":abc:", }, ":" },
83 { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", },
84 ":" },
85 { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" },
86 { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", },
87 ":" },
88 { 0, NULL, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", },
89 ":" },
90 { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def",
91 "ghi", }, ":" },
92 { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def",
93 "ghi", }, ":" },
94 { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def",
95 "ghi", }, ":" },
96 { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def",
97 "ghi", }, ":" },
99 /* Simple parameter expansion */
100 { 0, "foo", "${var}", 0, 1, { "foo", }, IFS },
101 { 0, "foo", "$var", 0, 1, { "foo", }, IFS },
102 { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS },
103 { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS },
104 { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS },
106 /* Simple quote removal */
107 { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS },
108 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS },
109 { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS },
110 { 0, NULL, "contin\\\nuation", 0, 1, { "continuation", }, IFS },
111 { 0, NULL, "explicit ''", 0, 2, { "explicit", "", }, IFS },
112 { 0, NULL, "explicit \"\"", 0, 2, { "explicit", "", }, IFS },
113 { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS },
115 /* Simple command substitution */
116 { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS },
117 { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS },
118 { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS },
119 { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS },
120 { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS },
121 { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS },
122 { 0, NULL, "a$(echo b)c", 0, 1, { "abc", }, IFS },
124 /* Simple arithmetic expansion */
125 { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS },
126 { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS },
127 { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS },
128 { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS },
129 { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS },
130 { 0, NULL, "$((010))", 0, 1, { "8" }, IFS },
131 { 0, NULL, "$((0x10))", 0, 1, { "16" }, IFS },
132 { 0, NULL, "$((010+0x10))", 0, 1, { "24" }, IFS },
133 { 0, NULL, "$((-010+0x10))", 0, 1, { "8" }, IFS },
134 { 0, NULL, "$((-0x10+010))", 0, 1, { "-8" }, IFS },
136 /* Advanced parameter expansion */
137 { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS },
138 { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS },
139 { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS },
140 { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS },
141 { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS },
142 { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS },
143 { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS },
144 { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS },
145 { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS },
146 { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS },
147 { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS },
148 { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS },
149 { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS },
150 { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS },
151 { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS },
152 { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS },
153 { 0, "12345", "${#var}", 0, 1, { "5", }, IFS },
154 { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS },
155 { 0, NULL, "${var-}", 0, 0, { NULL }, IFS },
157 { 0, "pizza", "${var#${var}}", 0, 0, { NULL }, IFS },
158 { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS },
159 { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS },
160 { 0, "b*witched", "${var##b*}", 0, 0, { NULL }, IFS },
161 { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS },
162 { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS },
163 { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS },
164 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS },
165 { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS },
166 { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS },
167 { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS },
169 /* Pathname expansion */
170 { 0, NULL, "???", 0, 2, { "one", "two", }, IFS },
171 { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS },
172 { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS },
173 { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS },
175 /* Nested constructs */
176 { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS },
177 { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS },
178 { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS },
179 { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
180 { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" },
181 { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS },
182 { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS },
183 { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS },
184 { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS },
185 { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS },
186 { 0, NULL, "\"a\n\n$(echo)b\"", 0, 1, { "a\n\nb", }, IFS },
187 { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS },
188 { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
190 /* Different IFS values */
191 { 0, "a b\tc\nd ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
192 { 0, "a b\tc d ", "$var", 0, 1, { "a b\tc d " }, "" /* `null' */ },
193 { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
195 /* Other things that should succeed */
196 { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
197 { 0, "???", "$var", 0, 1, { "???", }, IFS },
198 { 0, NULL, "$var", 0, 0, { NULL, }, IFS },
199 { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS },
200 { 0, NULL, "", 0, 0, { NULL, }, IFS },
202 /* Flags not already covered (testit() has special handling for these) */
203 { 0, NULL, "one two", WRDE_DOOFFS, 2, { "one", "two", }, IFS },
204 { 0, NULL, "appended", WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
205 { 0, NULL, "appended", WRDE_DOOFFS|WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
207 /* Things that should fail */
208 { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ },
209 { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS },
210 { WRDE_BADCHAR, NULL, "&ampersand", 0, 0, { NULL, }, IFS },
211 { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS },
212 { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS },
213 { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS },
214 { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS },
215 { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS },
216 { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS },
217 { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS },
218 { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS },
219 { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS },
220 { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS },
221 { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS },
222 { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS },
223 { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS },
224 { WRDE_SYNTAX, NULL, "$(for i in)", 0, 0, { NULL, }, IFS },
225 { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
226 { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
227 { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS },
228 /* Test for CVE-2014-7817. We test 3 combinations of command
229 substitution inside an arithmetic expression to make sure that
230 no commands are executed and error is returned. */
231 { WRDE_CMDSUB, NULL, "$((`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
232 { WRDE_CMDSUB, NULL, "$((1+`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
233 { WRDE_CMDSUB, NULL, "$((1+$((`echo 1`))))", WRDE_NOCMD, 0, { NULL, }, IFS },
235 { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
238 static int testit (struct test_case_struct *tc);
239 static int tests;
241 static void
242 command_line_test (const char *words)
244 wordexp_t we;
245 int i;
246 int retval = wordexp (words, &we, 0);
247 printf ("wordexp returned %d\n", retval);
248 for (i = 0; i < we.we_wordc; i++)
249 printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
253 main (int argc, char *argv[])
255 const char *globfile[] = { "one", "two", "three", NULL };
256 char tmpdir[32];
257 struct passwd *pw;
258 const char *cwd;
259 int test;
260 int fail = 0;
261 int i;
262 struct test_case_struct ts;
264 if (argc > 1)
266 command_line_test (argv[1]);
267 return 0;
270 cwd = getcwd (NULL, 0);
272 /* Set up arena for pathname expansion */
273 tmpnam (tmpdir);
274 if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir))
275 return -1;
276 else
278 int fd;
280 for (i = 0; globfile[i]; ++i)
281 if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1
282 || close (fd))
283 return -1;
286 /* If we are not allowed to do command substitution, we install
287 fork handlers to verify that no forks happened. No forks should
288 happen at all if command substitution is disabled. */
289 if (__app_register_atfork (register_fork, NULL, NULL) != 0)
291 printf ("Failed to register fork handler.\n");
292 return -1;
295 for (test = 0; test_case[test].retval != -1; test++)
296 if (testit (&test_case[test]))
297 ++fail;
299 /* Tilde-expansion tests. */
300 pw = getpwnam ("root");
301 if (pw != NULL)
303 ts.retval = 0;
304 ts.env = NULL;
305 ts.words = "~root ";
306 ts.flags = 0;
307 ts.wordc = 1;
308 ts.wordv[0] = pw->pw_dir;
309 ts.ifs = IFS;
311 if (testit (&ts))
312 ++fail;
314 ts.retval = 0;
315 ts.env = pw->pw_dir;
316 ts.words = "${var#~root}x";
317 ts.flags = 0;
318 ts.wordc = 1;
319 ts.wordv[0] = "x";
320 ts.ifs = IFS;
322 if (testit (&ts))
323 ++fail;
326 /* "~" expands to value of $HOME when HOME is set */
328 setenv ("HOME", "/dummy/home", 1);
329 ts.retval = 0;
330 ts.env = NULL;
331 ts.words = "~ ~/foo";
332 ts.flags = 0;
333 ts.wordc = 2;
334 ts.wordv[0] = "/dummy/home";
335 ts.wordv[1] = "/dummy/home/foo";
336 ts.ifs = IFS;
338 if (testit (&ts))
339 ++fail;
341 /* "~" expands to home dir from passwd file if HOME is not set */
343 pw = getpwuid (getuid ());
344 if (pw != NULL)
346 unsetenv ("HOME");
347 ts.retval = 0;
348 ts.env = NULL;
349 ts.words = "~";
350 ts.flags = 0;
351 ts.wordc = 1;
352 ts.wordv[0] = pw->pw_dir;
353 ts.ifs = IFS;
355 if (testit (&ts))
356 ++fail;
359 puts ("tests completed, now cleaning up");
361 /* Clean up */
362 for (i = 0; globfile[i]; ++i)
363 remove (globfile[i]);
365 if (cwd == NULL)
366 cwd = "..";
368 chdir (cwd);
369 rmdir (tmpdir);
371 printf ("tests failed: %d\n", fail);
373 return fail != 0;
377 static int
378 testit (struct test_case_struct *tc)
380 int retval;
381 wordexp_t we, sav_we;
382 char *dummy;
383 int bzzzt = 0;
384 int start_offs = 0;
385 int i;
387 if (tc->env)
388 setenv ("var", tc->env, 1);
389 else
390 unsetenv ("var");
392 if (tc->ifs)
393 setenv ("IFS", tc->ifs, 1);
394 else
395 unsetenv ("IFS");
397 sav_we.we_wordc = 99;
398 sav_we.we_wordv = &dummy;
399 sav_we.we_offs = 3;
400 we = sav_we;
402 printf ("Test %d (%s): ", ++tests, tc->words);
404 if (tc->flags & WRDE_NOCMD)
405 registered_forks = 0;
407 if (tc->flags & WRDE_APPEND)
409 /* initial wordexp() call, to be appended to */
410 if (wordexp ("pre1 pre2", &we, tc->flags & ~WRDE_APPEND) != 0)
412 printf ("FAILED setup\n");
413 return 1;
416 retval = wordexp (tc->words, &we, tc->flags);
418 if ((tc->flags & WRDE_NOCMD)
419 && (registered_forks > 0))
421 printf ("FAILED fork called for WRDE_NOCMD\n");
422 return 1;
425 if (tc->flags & WRDE_DOOFFS)
426 start_offs = sav_we.we_offs;
428 if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
429 bzzzt = 1;
430 else if (retval == 0)
432 for (i = 0; i < start_offs; ++i)
433 if (we.we_wordv[i] != NULL)
435 bzzzt = 1;
436 break;
439 for (i = 0; i < we.we_wordc; ++i)
440 if (we.we_wordv[i+start_offs] == NULL ||
441 strcmp (tc->wordv[i], we.we_wordv[i+start_offs]) != 0)
443 bzzzt = 1;
444 break;
448 if (bzzzt)
450 printf ("FAILED\n");
451 printf ("Test words: <%s>, need retval %d, wordc %Zd\n",
452 tc->words, tc->retval, tc->wordc);
453 if (start_offs != 0)
454 printf ("(preceded by %d NULLs)\n", start_offs);
455 printf ("Got retval %d, wordc %Zd: ", retval, we.we_wordc);
456 if (retval == 0 || retval == WRDE_NOSPACE)
458 for (i = 0; i < we.we_wordc + start_offs; ++i)
459 if (we.we_wordv[i] == NULL)
460 printf ("NULL ");
461 else
462 printf ("<%s> ", we.we_wordv[i]);
464 printf ("\n");
466 else if (retval != 0 && retval != WRDE_NOSPACE &&
467 (we.we_wordc != sav_we.we_wordc ||
468 we.we_wordv != sav_we.we_wordv ||
469 we.we_offs != sav_we.we_offs))
471 bzzzt = 1;
472 printf ("FAILED to restore wordexp_t members\n");
474 else
475 printf ("OK\n");
477 if (retval == 0 || retval == WRDE_NOSPACE)
478 wordfree (&we);
480 return bzzzt;