Back to index

glibc  2.9
wordexp-test.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <sys/stat.h>
00020 #include <sys/types.h>
00021 #include <fcntl.h>
00022 #include <unistd.h>
00023 #include <pwd.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <wordexp.h>
00028 
00029 #define IFS " \n\t"
00030 
00031 struct test_case_struct
00032 {
00033   int retval;
00034   const char *env;
00035   const char *words;
00036   int flags;
00037   size_t wordc;
00038   const char *wordv[10];
00039   const char *ifs;
00040 } test_case[] =
00041   {
00042     /* Simple word- and field-splitting */
00043     { 0, NULL, "one", 0, 1, { "one", }, IFS },
00044     { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS },
00045     { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS },
00046     { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS },
00047     { 0, NULL, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," },
00048     { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" },
00049     { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS },
00050     { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" },
00051     { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS },
00052     { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS },
00053     { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" },
00054     { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" },
00055 
00056     /* The non-whitespace IFS char at the end delimits the second field
00057      * but does NOT start a new field. */
00058     { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" },
00059 
00060     { 0, NULL, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" },
00061     { 0, NULL, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " },
00062     { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " },
00063     { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" },
00064     { 0, NULL, ":abc:", 0, 1, { ":abc:", }, ":" },
00065     { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", },
00066       ":" },
00067     { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" },
00068     { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", },
00069       ":" },
00070     { 0, NULL, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", },
00071       ":" },
00072     { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def",
00073                                                    "ghi", }, ":" },
00074     { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def",
00075                                                    "ghi", }, ":" },
00076     { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def",
00077                                                    "ghi", }, ":" },
00078     { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def",
00079                                                    "ghi", }, ":" },
00080 
00081     /* Simple parameter expansion */
00082     { 0, "foo", "${var}", 0, 1, { "foo", }, IFS },
00083     { 0, "foo", "$var", 0, 1, { "foo", }, IFS },
00084     { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS },
00085     { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS },
00086     { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS },
00087 
00088     /* Simple quote removal */
00089     { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS },
00090     { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS },
00091     { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS },
00092     { 0, NULL, "contin\\\nuation", 0, 1, { "continuation", }, IFS },
00093     { 0, NULL, "explicit ''", 0, 2, { "explicit", "", }, IFS },
00094     { 0, NULL, "explicit \"\"", 0, 2, { "explicit", "", }, IFS },
00095     { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS },
00096 
00097     /* Simple command substitution */
00098     { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS },
00099     { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS },
00100     { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS },
00101     { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS },
00102     { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS },
00103     { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS },
00104     { 0, NULL, "a$(echo b)c", 0, 1, { "abc", }, IFS },
00105 
00106     /* Simple arithmetic expansion */
00107     { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS },
00108     { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS },
00109     { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS },
00110     { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS },
00111     { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS },
00112     { 0, NULL, "$((010))", 0, 1, { "8" }, IFS },
00113     { 0, NULL, "$((0x10))", 0, 1, { "16" }, IFS },
00114     { 0, NULL, "$((010+0x10))", 0, 1, { "24" }, IFS },
00115     { 0, NULL, "$((-010+0x10))", 0, 1, { "8" }, IFS },
00116     { 0, NULL, "$((-0x10+010))", 0, 1, { "-8" }, IFS },
00117 
00118     /* Advanced parameter expansion */
00119     { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS },
00120     { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS },
00121     { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS },
00122     { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS },
00123     { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS },
00124     { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS },
00125     { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS },
00126     { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS },
00127     { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS },
00128     { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS },
00129     { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS },
00130     { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS },
00131     { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS },
00132     { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS },
00133     { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS },
00134     { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS },
00135     { 0, "12345", "${#var}", 0, 1, { "5", }, IFS },
00136     { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS },
00137     { 0, NULL, "${var-}", 0, 0, { NULL }, IFS },
00138 
00139     { 0, "pizza", "${var#${var}}", 0, 0, { NULL }, IFS },
00140     { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS },
00141     { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS },
00142     { 0, "b*witched", "${var##b*}", 0, 0, { NULL }, IFS },
00143     { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS },
00144     { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS },
00145     { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS },
00146     { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS },
00147     { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS },
00148     { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS },
00149     { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS },
00150 
00151     /* Pathname expansion */
00152     { 0, NULL, "???", 0, 2, { "one", "two", }, IFS },
00153     { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS },
00154     { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS },
00155     { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS },
00156 
00157     /* Nested constructs */
00158     { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS },
00159     { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS },
00160     { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS },
00161     { 0, "  red  , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
00162     { 0, "  red  , white blue", "\"$var\"", 0, 1, { "  red  , white blue", }, ", \n\t" },
00163     { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS },
00164     { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS },
00165     { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS },
00166     { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS },
00167     { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS },
00168     { 0, NULL, "\"a\n\n$(echo)b\"", 0, 1, { "a\n\nb", }, IFS },
00169     { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS },
00170     { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
00171 
00172     /* Different IFS values */
00173     { 0, "a b\tc\nd  ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
00174     { 0, "a b\tc d  ", "$var", 0, 1, { "a b\tc d  " }, "" /* `null' */ },
00175     { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
00176 
00177     /* Other things that should succeed */
00178     { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
00179     { 0, "???", "$var", 0, 1, { "???", }, IFS },
00180     { 0, NULL, "$var", 0, 0, { NULL, }, IFS },
00181     { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS },
00182     { 0, NULL, "", 0, 0, { NULL, }, IFS },
00183 
00184     /* Flags not already covered (testit() has special handling for these) */
00185     { 0, NULL, "one two", WRDE_DOOFFS, 2, { "one", "two", }, IFS },
00186     { 0, NULL, "appended", WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
00187     { 0, NULL, "appended", WRDE_DOOFFS|WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
00188 
00189     /* Things that should fail */
00190     { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ },
00191     { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS },
00192     { WRDE_BADCHAR, NULL, "&ampersand", 0, 0, { NULL, }, IFS },
00193     { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS },
00194     { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS },
00195     { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS },
00196     { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS },
00197     { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS },
00198     { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS },
00199     { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS },
00200     { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS },
00201     { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS },
00202     { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS },
00203     { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS },
00204     { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS },
00205     { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS },
00206     { WRDE_SYNTAX, NULL, "$(for i in)", 0, 0, { NULL, }, IFS },
00207     { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
00208     { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
00209     { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS },
00210 
00211     { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
00212   };
00213 
00214 static int testit (struct test_case_struct *tc);
00215 static int tests;
00216 
00217 static void
00218 command_line_test (const char *words)
00219 {
00220   wordexp_t we;
00221   int i;
00222   int retval = wordexp (words, &we, 0);
00223   printf ("wordexp returned %d\n", retval);
00224   for (i = 0; i < we.we_wordc; i++)
00225     printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
00226 }
00227 
00228 int
00229 main (int argc, char *argv[])
00230 {
00231   const char *globfile[] = { "one", "two", "three", NULL };
00232   char tmpdir[32];
00233   struct passwd *pw;
00234   const char *cwd;
00235   int test;
00236   int fail = 0;
00237   int i;
00238   struct test_case_struct ts;
00239 
00240   if (argc > 1)
00241     {
00242       command_line_test (argv[1]);
00243       return 0;
00244     }
00245 
00246   cwd = getcwd (NULL, 0);
00247 
00248   /* Set up arena for pathname expansion */
00249   tmpnam (tmpdir);
00250   if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir))
00251     return -1;
00252   else
00253     {
00254       int fd;
00255 
00256       for (i = 0; globfile[i]; ++i)
00257        if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1
00258            || close (fd))
00259          return -1;
00260     }
00261 
00262   for (test = 0; test_case[test].retval != -1; test++)
00263     if (testit (&test_case[test]))
00264       ++fail;
00265 
00266   /* Tilde-expansion tests. */
00267   pw = getpwnam ("root");
00268   if (pw != NULL)
00269     {
00270       ts.retval = 0;
00271       ts.env = NULL;
00272       ts.words = "~root ";
00273       ts.flags = 0;
00274       ts.wordc = 1;
00275       ts.wordv[0] = pw->pw_dir;
00276       ts.ifs = IFS;
00277 
00278       if (testit (&ts))
00279        ++fail;
00280 
00281       ts.retval = 0;
00282       ts.env = pw->pw_dir;
00283       ts.words = "${var#~root}x";
00284       ts.flags = 0;
00285       ts.wordc = 1;
00286       ts.wordv[0] = "x";
00287       ts.ifs = IFS;
00288 
00289       if (testit (&ts))
00290        ++fail;
00291     }
00292 
00293   /* "~" expands to value of $HOME when HOME is set */
00294 
00295   setenv ("HOME", "/dummy/home", 1);
00296   ts.retval = 0;
00297   ts.env = NULL;
00298   ts.words = "~ ~/foo";
00299   ts.flags = 0;
00300   ts.wordc = 2;
00301   ts.wordv[0] = "/dummy/home";
00302   ts.wordv[1] = "/dummy/home/foo";
00303   ts.ifs = IFS;
00304 
00305   if (testit (&ts))
00306     ++fail;
00307 
00308   /* "~" expands to home dir from passwd file if HOME is not set */
00309 
00310   pw = getpwuid (getuid ());
00311   if (pw != NULL)
00312     {
00313       unsetenv ("HOME");
00314       ts.retval = 0;
00315       ts.env = NULL;
00316       ts.words = "~";
00317       ts.flags = 0;
00318       ts.wordc = 1;
00319       ts.wordv[0] = pw->pw_dir;
00320       ts.ifs = IFS;
00321 
00322       if (testit (&ts))
00323        ++fail;
00324     }
00325 
00326   puts ("tests completed, now cleaning up");
00327 
00328   /* Clean up */
00329   for (i = 0; globfile[i]; ++i)
00330     remove (globfile[i]);
00331 
00332   if (cwd == NULL)
00333     cwd = "..";
00334 
00335   chdir (cwd);
00336   rmdir (tmpdir);
00337 
00338   printf ("tests failed: %d\n", fail);
00339 
00340   return fail != 0;
00341 }
00342 
00343 
00344 static int
00345 testit (struct test_case_struct *tc)
00346 {
00347   int retval;
00348   wordexp_t we, sav_we;
00349   char *dummy;
00350   int bzzzt = 0;
00351   int start_offs = 0;
00352   int i;
00353 
00354   if (tc->env)
00355     setenv ("var", tc->env, 1);
00356   else
00357     unsetenv ("var");
00358 
00359   if (tc->ifs)
00360     setenv ("IFS", tc->ifs, 1);
00361   else
00362     unsetenv ("IFS");
00363 
00364   sav_we.we_wordc = 99;
00365   sav_we.we_wordv = &dummy;
00366   sav_we.we_offs = 3;
00367   we = sav_we;
00368 
00369   printf ("Test %d (%s): ", ++tests, tc->words);
00370 
00371   if (tc->flags & WRDE_APPEND)
00372     {
00373       /* initial wordexp() call, to be appended to */
00374       if (wordexp ("pre1 pre2", &we, tc->flags & ~WRDE_APPEND) != 0)
00375         {
00376          printf ("FAILED setup\n");
00377          return 1;
00378        }
00379     }
00380   retval = wordexp (tc->words, &we, tc->flags);
00381 
00382   if (tc->flags & WRDE_DOOFFS)
00383       start_offs = sav_we.we_offs;
00384 
00385   if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
00386     bzzzt = 1;
00387   else if (retval == 0)
00388     {
00389       for (i = 0; i < start_offs; ++i)
00390        if (we.we_wordv[i] != NULL)
00391          {
00392            bzzzt = 1;
00393            break;
00394          }
00395 
00396       for (i = 0; i < we.we_wordc; ++i)
00397        if (we.we_wordv[i+start_offs] == NULL ||
00398            strcmp (tc->wordv[i], we.we_wordv[i+start_offs]) != 0)
00399          {
00400            bzzzt = 1;
00401            break;
00402          }
00403     }
00404 
00405   if (bzzzt)
00406     {
00407       printf ("FAILED\n");
00408       printf ("Test words: <%s>, need retval %d, wordc %Zd\n",
00409              tc->words, tc->retval, tc->wordc);
00410       if (start_offs != 0)
00411        printf ("(preceded by %d NULLs)\n", start_offs);
00412       printf ("Got retval %d, wordc %Zd: ", retval, we.we_wordc);
00413       if (retval == 0 || retval == WRDE_NOSPACE)
00414        {
00415          for (i = 0; i < we.we_wordc + start_offs; ++i)
00416            if (we.we_wordv[i] == NULL)
00417              printf ("NULL ");
00418            else
00419              printf ("<%s> ", we.we_wordv[i]);
00420        }
00421       printf ("\n");
00422     }
00423   else if (retval != 0 && retval != WRDE_NOSPACE &&
00424           (we.we_wordc != sav_we.we_wordc ||
00425             we.we_wordv != sav_we.we_wordv ||
00426             we.we_offs != sav_we.we_offs))
00427     {
00428       bzzzt = 1;
00429       printf ("FAILED to restore wordexp_t members\n");
00430     }
00431   else
00432     printf ("OK\n");
00433 
00434   if (retval == 0 || retval == WRDE_NOSPACE)
00435     wordfree (&we);
00436 
00437   return bzzzt;
00438 }