hurd: Make libc able to call pthread stubs
[glibc.git] / posix / wordexp-test.c
blob137044e95f30591fd1fd0274e5555f626f3e5027
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 <sys/mman.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <pwd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <wordexp.h>
29 #define IFS " \n\t"
31 extern void *__dso_handle __attribute__ ((__weak__, __visibility__ ("hidden")));
32 extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *);
34 static int __app_register_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void))
36 return __register_atfork (prepare, parent, child,
37 &__dso_handle == NULL ? NULL : __dso_handle);
40 /* Number of forks seen. */
41 static int registered_forks;
43 /* For each fork increment the fork count. */
44 static void
45 register_fork (void)
47 registered_forks++;
50 struct test_case_struct
52 int retval;
53 const char *env;
54 const char *words;
55 int flags;
56 size_t wordc;
57 const char *wordv[10];
58 const char *ifs;
59 } test_case[] =
61 /* Simple word- and field-splitting */
62 { 0, NULL, "one", 0, 1, { "one", }, IFS },
63 { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS },
64 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS },
65 { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS },
66 { 0, NULL, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," },
67 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" },
68 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS },
69 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" },
70 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS },
71 { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS },
72 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" },
73 { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" },
75 /* The non-whitespace IFS char at the end delimits the second field
76 * but does NOT start a new field. */
77 { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" },
79 { 0, NULL, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" },
80 { 0, NULL, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " },
81 { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " },
82 { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" },
83 { 0, NULL, ":abc:", 0, 1, { ":abc:", }, ":" },
84 { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", },
85 ":" },
86 { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" },
87 { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", },
88 ":" },
89 { 0, NULL, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", },
90 ":" },
91 { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def",
92 "ghi", }, ":" },
93 { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def",
94 "ghi", }, ":" },
95 { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def",
96 "ghi", }, ":" },
97 { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def",
98 "ghi", }, ":" },
100 /* Simple parameter expansion */
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 },
105 { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS },
107 /* Simple quote removal */
108 { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS },
109 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS },
110 { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS },
111 { 0, NULL, "contin\\\nuation", 0, 1, { "continuation", }, IFS },
112 { 0, NULL, "explicit ''", 0, 2, { "explicit", "", }, IFS },
113 { 0, NULL, "explicit \"\"", 0, 2, { "explicit", "", }, IFS },
114 { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS },
116 /* Simple command substitution */
117 { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS },
118 { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS },
119 { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS },
120 { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS },
121 { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS },
122 { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS },
123 { 0, NULL, "a$(echo b)c", 0, 1, { "abc", }, IFS },
125 /* Simple arithmetic expansion */
126 { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS },
127 { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS },
128 { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS },
129 { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS },
130 { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS },
131 { 0, NULL, "$((010))", 0, 1, { "8" }, IFS },
132 { 0, NULL, "$((0x10))", 0, 1, { "16" }, IFS },
133 { 0, NULL, "$((010+0x10))", 0, 1, { "24" }, IFS },
134 { 0, NULL, "$((-010+0x10))", 0, 1, { "8" }, IFS },
135 { 0, NULL, "$((-0x10+010))", 0, 1, { "-8" }, IFS },
137 /* Advanced parameter expansion */
138 { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS },
139 { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS },
140 { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS },
141 { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS },
142 { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS },
143 { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS },
144 { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS },
145 { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS },
146 { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS },
147 { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS },
148 { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS },
149 { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS },
150 { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS },
151 { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS },
152 { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS },
153 { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS },
154 { 0, "12345", "${#var}", 0, 1, { "5", }, IFS },
155 { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS },
156 { 0, NULL, "${var-}", 0, 0, { NULL }, IFS },
158 { 0, "pizza", "${var#${var}}", 0, 0, { NULL }, IFS },
159 { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS },
160 { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS },
161 { 0, "b*witched", "${var##b*}", 0, 0, { NULL }, IFS },
162 { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS },
163 { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS },
164 { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS },
165 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS },
166 { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS },
167 { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS },
168 { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS },
170 /* Pathname expansion */
171 { 0, NULL, "???", 0, 2, { "one", "two", }, IFS },
172 { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS },
173 { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS },
174 { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS },
176 /* Nested constructs */
177 { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS },
178 { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS },
179 { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS },
180 { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
181 { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" },
182 { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS },
183 { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS },
184 { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS },
185 { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS },
186 { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS },
187 { 0, NULL, "\"a\n\n$(echo)b\"", 0, 1, { "a\n\nb", }, IFS },
188 { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS },
189 { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
191 /* Different IFS values */
192 { 0, "a b\tc\nd ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
193 { 0, "a b\tc d ", "$var", 0, 1, { "a b\tc d " }, "" /* `null' */ },
194 { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
196 /* Other things that should succeed */
197 { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
198 { 0, "???", "$var", 0, 1, { "???", }, IFS },
199 { 0, NULL, "$var", 0, 0, { NULL, }, IFS },
200 { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS },
201 { 0, NULL, "", 0, 0, { NULL, }, IFS },
203 /* Flags not already covered (testit() has special handling for these) */
204 { 0, NULL, "one two", WRDE_DOOFFS, 2, { "one", "two", }, IFS },
205 { 0, NULL, "appended", WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
206 { 0, NULL, "appended", WRDE_DOOFFS|WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
208 /* Things that should fail */
209 { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ },
210 { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS },
211 { WRDE_BADCHAR, NULL, "&ampersand", 0, 0, { NULL, }, IFS },
212 { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS },
213 { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS },
214 { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS },
215 { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS },
216 { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS },
217 { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS },
218 { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS },
219 { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS },
220 { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS },
221 { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS },
222 { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS },
223 { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS },
224 { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS },
225 { WRDE_SYNTAX, NULL, "$(for i in)", 0, 0, { NULL, }, IFS },
226 { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
227 { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
228 { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS },
229 /* Test for CVE-2014-7817. We test 3 combinations of command
230 substitution inside an arithmetic expression to make sure that
231 no commands are executed and error is returned. */
232 { WRDE_CMDSUB, NULL, "$((`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
233 { WRDE_CMDSUB, NULL, "$((1+`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
234 { WRDE_CMDSUB, NULL, "$((1+$((`echo 1`))))", WRDE_NOCMD, 0, { NULL, }, IFS },
236 { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
239 static int testit (struct test_case_struct *tc);
240 static int tests;
242 static void
243 command_line_test (const char *words)
245 wordexp_t we;
246 int i;
247 int retval = wordexp (words, &we, 0);
248 printf ("wordexp returned %d\n", retval);
249 for (i = 0; i < we.we_wordc; i++)
250 printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
253 static int
254 do_bz18043 (void)
256 const int pagesize = getpagesize ();
257 char *start = mmap (0, 2 * pagesize, PROT_READ|PROT_WRITE,
258 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
260 if (start == MAP_FAILED)
261 return 1;
263 if (mprotect (start + pagesize, pagesize, PROT_NONE))
264 return 2;
266 const char word[] = "${";
267 char *word_start = start + pagesize - sizeof (word);
268 memcpy (word_start, word, sizeof (word));
270 wordexp_t w;
271 if (wordexp (word_start, &w, 0) != WRDE_SYNTAX)
272 return 3;
274 if (munmap (start, 2 * pagesize) != 0)
275 return 4;
277 return 0;
281 main (int argc, char *argv[])
283 const char *globfile[] = { "one", "two", "three", NULL };
284 char tmpdir[32];
285 struct passwd *pw;
286 const char *cwd;
287 int test;
288 int fail = 0;
289 int i;
290 struct test_case_struct ts;
292 if (argc > 1)
294 command_line_test (argv[1]);
295 return 0;
298 cwd = getcwd (NULL, 0);
300 /* Set up arena for pathname expansion */
301 tmpnam (tmpdir);
302 if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir))
303 return -1;
304 else
306 int fd;
308 for (i = 0; globfile[i]; ++i)
309 if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1
310 || close (fd))
311 return -1;
314 /* If we are not allowed to do command substitution, we install
315 fork handlers to verify that no forks happened. No forks should
316 happen at all if command substitution is disabled. */
317 if (__app_register_atfork (register_fork, NULL, NULL) != 0)
319 printf ("Failed to register fork handler.\n");
320 return -1;
323 for (test = 0; test_case[test].retval != -1; test++)
324 if (testit (&test_case[test]))
325 ++fail;
327 /* Tilde-expansion tests. */
328 pw = getpwnam ("root");
329 if (pw != NULL)
331 ts.retval = 0;
332 ts.env = NULL;
333 ts.words = "~root ";
334 ts.flags = 0;
335 ts.wordc = 1;
336 ts.wordv[0] = pw->pw_dir;
337 ts.ifs = IFS;
339 if (testit (&ts))
340 ++fail;
342 ts.retval = 0;
343 ts.env = pw->pw_dir;
344 ts.words = "${var#~root}x";
345 ts.flags = 0;
346 ts.wordc = 1;
347 ts.wordv[0] = "x";
348 ts.ifs = IFS;
350 if (testit (&ts))
351 ++fail;
354 /* "~" expands to value of $HOME when HOME is set */
356 setenv ("HOME", "/dummy/home", 1);
357 ts.retval = 0;
358 ts.env = NULL;
359 ts.words = "~ ~/foo";
360 ts.flags = 0;
361 ts.wordc = 2;
362 ts.wordv[0] = "/dummy/home";
363 ts.wordv[1] = "/dummy/home/foo";
364 ts.ifs = IFS;
366 if (testit (&ts))
367 ++fail;
369 /* "~" expands to home dir from passwd file if HOME is not set */
371 pw = getpwuid (getuid ());
372 if (pw != NULL)
374 unsetenv ("HOME");
375 ts.retval = 0;
376 ts.env = NULL;
377 ts.words = "~";
378 ts.flags = 0;
379 ts.wordc = 1;
380 ts.wordv[0] = pw->pw_dir;
381 ts.ifs = IFS;
383 if (testit (&ts))
384 ++fail;
387 puts ("tests completed, now cleaning up");
389 /* Clean up */
390 for (i = 0; globfile[i]; ++i)
391 remove (globfile[i]);
393 if (cwd == NULL)
394 cwd = "..";
396 chdir (cwd);
397 rmdir (tmpdir);
399 printf ("tests failed: %d\n", fail);
401 if (do_bz18043 ())
402 ++fail;
404 return fail != 0;
408 static int
409 testit (struct test_case_struct *tc)
411 int retval;
412 wordexp_t we, sav_we;
413 char *dummy;
414 int bzzzt = 0;
415 int start_offs = 0;
416 int i;
418 if (tc->env)
419 setenv ("var", tc->env, 1);
420 else
421 unsetenv ("var");
423 if (tc->ifs)
424 setenv ("IFS", tc->ifs, 1);
425 else
426 unsetenv ("IFS");
428 sav_we.we_wordc = 99;
429 sav_we.we_wordv = &dummy;
430 sav_we.we_offs = 3;
431 we = sav_we;
433 printf ("Test %d (%s): ", ++tests, tc->words);
435 if (tc->flags & WRDE_NOCMD)
436 registered_forks = 0;
438 if (tc->flags & WRDE_APPEND)
440 /* initial wordexp() call, to be appended to */
441 if (wordexp ("pre1 pre2", &we, tc->flags & ~WRDE_APPEND) != 0)
443 printf ("FAILED setup\n");
444 return 1;
447 retval = wordexp (tc->words, &we, tc->flags);
449 if ((tc->flags & WRDE_NOCMD)
450 && (registered_forks > 0))
452 printf ("FAILED fork called for WRDE_NOCMD\n");
453 return 1;
456 if (tc->flags & WRDE_DOOFFS)
457 start_offs = sav_we.we_offs;
459 if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
460 bzzzt = 1;
461 else if (retval == 0)
463 for (i = 0; i < start_offs; ++i)
464 if (we.we_wordv[i] != NULL)
466 bzzzt = 1;
467 break;
470 for (i = 0; i < we.we_wordc; ++i)
471 if (we.we_wordv[i+start_offs] == NULL ||
472 strcmp (tc->wordv[i], we.we_wordv[i+start_offs]) != 0)
474 bzzzt = 1;
475 break;
479 if (bzzzt)
481 printf ("FAILED\n");
482 printf ("Test words: <%s>, need retval %d, wordc %Zd\n",
483 tc->words, tc->retval, tc->wordc);
484 if (start_offs != 0)
485 printf ("(preceded by %d NULLs)\n", start_offs);
486 printf ("Got retval %d, wordc %Zd: ", retval, we.we_wordc);
487 if (retval == 0 || retval == WRDE_NOSPACE)
489 for (i = 0; i < we.we_wordc + start_offs; ++i)
490 if (we.we_wordv[i] == NULL)
491 printf ("NULL ");
492 else
493 printf ("<%s> ", we.we_wordv[i]);
495 printf ("\n");
497 else if (retval != 0 && retval != WRDE_NOSPACE &&
498 (we.we_wordc != sav_we.we_wordc ||
499 we.we_wordv != sav_we.we_wordv ||
500 we.we_offs != sav_we.we_offs))
502 bzzzt = 1;
503 printf ("FAILED to restore wordexp_t members\n");
505 else
506 printf ("OK\n");
508 if (retval == 0 || retval == WRDE_NOSPACE)
509 wordfree (&we);
511 return bzzzt;