Update.
[glibc.git] / posix / wordexp-test.c
blob82af1666a2e921c883e0d652dd7a23f9a2ba0062
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 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, 3, { "red", "white", "blue", }, ", \n\t" },
48 /* Simple parameter expansion */
49 { 0, "foo", "${var}", 0, 1, { "foo", }, IFS },
50 { 0, "foo", "$var", 0, 1, { "foo", }, IFS },
51 { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS },
52 { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS },
53 { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS },
55 /* Simple quote removal */
56 { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS },
57 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS },
58 { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS },
60 /* Simple command substitution */
61 { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS },
62 { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS },
63 { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS },
64 { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS },
65 { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS },
66 { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS },
68 /* Simple arithmetic expansion */
69 { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS },
70 { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS },
71 { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS },
72 { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS },
73 { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS },
75 /* Advanced parameter expansion */
76 { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS },
77 { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS },
78 { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS },
79 { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS },
80 { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS },
81 { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS },
82 { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS },
83 { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS },
84 { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS },
85 { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS },
86 { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS },
87 { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS },
88 { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS },
89 { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS },
90 { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS },
91 { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS },
92 { 0, "12345", "${#var}", 0, 1, { "5", }, IFS },
93 { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS },
94 { 0, NULL, "${var-}", 0, 0, { NULL }, IFS },
96 { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS },
97 { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS },
98 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS },
99 { 0, "borabora-island", "${var##*bora}", 0, 1, {"-island", }, IFS },
100 { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS },
102 /* Pathname expansion */
103 { 0, NULL, "???", 0, 2, { "one", "two", }, IFS },
104 { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS },
105 { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS },
106 { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS },
108 /* Nested constructs */
109 { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS },
110 { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS },
111 { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS },
112 { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
113 { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" },
114 { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS },
115 { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS },
116 { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS },
117 { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS },
118 { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS },
119 { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS },
120 { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
122 /* Other things that should succeed */
123 { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
124 { 0, "???", "$var", 0, 1, { "???", }, IFS },
125 { 0, NULL, "$var", 0, 0, { NULL, }, IFS },
126 { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS },
127 { 0, NULL, "", 0, 0, { NULL, }, IFS },
129 /* Things that should fail */
130 { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, IFS },
131 { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS },
132 { WRDE_BADCHAR, NULL, "&ampersand", 0, 0, { NULL, }, IFS },
133 { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS },
134 { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS },
135 { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS },
136 { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS },
137 { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS },
138 { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS },
139 { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS },
140 { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS },
141 { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS },
142 { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS },
143 { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS },
144 { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS },
145 { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS },
146 { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
147 { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
149 { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
152 static int testit (struct test_case_struct *tc);
153 static int tests;
155 void
156 command_line_test (const char *words)
158 wordexp_t we;
159 int i;
160 int retval = wordexp (words, &we, 0);
161 printf ("wordexp returned %d\n", retval);
162 for (i = 0; i < we.we_wordc; i++)
163 printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
167 main (int argc, char *argv[])
169 const char *globfile[] = { "one", "two", "three", NULL };
170 char tmpdir[32];
171 struct passwd *pw;
172 const char *cwd;
173 int test;
174 int fail = 0;
175 int i;
177 if (argc > 1)
179 command_line_test (argv[1]);
180 return 0;
183 cwd = getcwd (NULL, 0);
185 /* Set up arena for pathname expansion */
186 tmpnam (tmpdir);
187 if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir))
188 return -1;
189 else
191 int fd;
193 for (i = 0; globfile[i]; ++i)
194 if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1
195 || close (fd))
196 return -1;
199 for (test = 0; test_case[test].retval != -1; test++)
200 if (testit (&test_case[test]))
201 ++fail;
203 pw = getpwnam ("root");
204 if (pw != NULL)
206 struct test_case_struct ts;
208 ts.retval = 0;
209 ts.env = NULL;
210 ts.words = "~root";
211 ts.flags = 0;
212 ts.wordc = 1;
213 ts.wordv[0] = pw->pw_dir;
214 ts.ifs = IFS;
216 if (testit (&ts))
217 ++fail;
220 puts ("tests completed, now cleaning up");
222 /* Clean up */
223 for (i = 0; globfile[i]; ++i)
224 remove (globfile[i]);
226 if (cwd == NULL)
227 cwd = "..";
229 chdir (cwd);
230 rmdir (tmpdir);
232 printf ("tests failed: %d\n", fail);
234 return fail != 0;
238 static int
239 testit (struct test_case_struct *tc)
241 int retval;
242 wordexp_t we;
243 int bzzzt = 0;
244 int i;
246 if (tc->env)
247 setenv ("var", tc->env, 1);
248 else
249 unsetenv ("var");
251 if (tc->ifs)
252 setenv ("IFS", tc->ifs, 1);
253 else
254 unsetenv ("IFS");
256 printf ("Test %d (%s): ", ++tests, tc->words);
257 retval = wordexp (tc->words, &we, tc->flags);
259 if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
260 bzzzt = 1;
261 else
262 for (i = 0; i < we.we_wordc; ++i)
263 if (strcmp (tc->wordv[i], we.we_wordv[i]) != 0)
265 bzzzt = 1;
266 break;
269 if (bzzzt)
271 printf ("FAILED\n");
272 printf ("Test words: <%s>, need retval %d, wordc %d\n",
273 tc->words, tc->retval, tc->wordc);
274 printf ("Got retval %d, wordc %d: ", retval, we.we_wordc);
275 for (i = 0; i < we.we_wordc; ++i)
276 printf ("<%s> ", we.we_wordv[i]);
277 printf ("\n");
279 else
280 printf ("OK\n");
282 wordfree (&we);
284 return bzzzt;