Path: blob/main/lib/libc/tests/gen/wordexp_test.c
104874 views
/*-1* Copyright (c) 2003 Tim J. Robbins2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Test program for wordexp() and wordfree() as specified by28* IEEE Std. 1003.1-2001.29*/3031#include <sys/wait.h>32#include <errno.h>33#include <signal.h>34#include <stdio.h>35#include <stdlib.h>36#include <string.h>37#include <wordexp.h>3839#include <atf-c.h>4041static void42chld_handler(int x)43{44int status, serrno;4546(void)x;47serrno = errno;48while (waitpid(-1, &status, WNOHANG) > 0)49;50errno = serrno;51}5253ATF_TC_WITHOUT_HEAD(simple_test);54ATF_TC_BODY(simple_test, tc)55{56wordexp_t we;57int r;5859/* Test that the macros are there. */60(void)(WRDE_APPEND + WRDE_DOOFFS + WRDE_NOCMD + WRDE_REUSE +61WRDE_SHOWERR + WRDE_UNDEF);62(void)(WRDE_BADCHAR + WRDE_BADVAL + WRDE_CMDSUB + WRDE_NOSPACE +63WRDE_SYNTAX);6465/* Simple test. */66r = wordexp("hello world", &we, 0);67ATF_REQUIRE(r == 0);68ATF_REQUIRE(we.we_wordc == 2);69ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0);70ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0);71ATF_REQUIRE(we.we_wordv[2] == NULL);72wordfree(&we);73}7475ATF_TC_WITHOUT_HEAD(long_output_test);76ATF_TC_BODY(long_output_test, tc)77{78char longdata[6 * 10000 + 1];79wordexp_t we;80int i, r;8182/* Long output. */83for (i = 0; i < 10000; i++)84snprintf(longdata + 6 * i, 7, "%05d ", i);85r = wordexp(longdata, &we, 0);86ATF_REQUIRE(r == 0);87ATF_REQUIRE(we.we_wordc == 10000);88ATF_REQUIRE(we.we_wordv[10000] == NULL);89wordfree(&we);90}9192ATF_TC_WITHOUT_HEAD(WRDE_DOOFFS_test);93ATF_TC_BODY(WRDE_DOOFFS_test, tc)94{95wordexp_t we;96int r;9798we.we_offs = 3;99r = wordexp("hello world", &we, WRDE_DOOFFS);100ATF_REQUIRE(r == 0);101ATF_REQUIRE(we.we_wordc == 2);102ATF_REQUIRE(we.we_wordv[0] == NULL);103ATF_REQUIRE(we.we_wordv[1] == NULL);104ATF_REQUIRE(we.we_wordv[2] == NULL);105ATF_REQUIRE(strcmp(we.we_wordv[3], "hello") == 0);106ATF_REQUIRE(strcmp(we.we_wordv[4], "world") == 0);107ATF_REQUIRE(we.we_wordv[5] == NULL);108wordfree(&we);109}110111ATF_TC_WITHOUT_HEAD(WRDE_REUSE_test);112ATF_TC_BODY(WRDE_REUSE_test, tc)113{114wordexp_t we;115int r;116117r = wordexp("hello world", &we, 0);118r = wordexp("hello world", &we, WRDE_REUSE);119ATF_REQUIRE(r == 0);120ATF_REQUIRE(we.we_wordc == 2);121ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0);122ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0);123ATF_REQUIRE(we.we_wordv[2] == NULL);124wordfree(&we);125}126127ATF_TC_WITHOUT_HEAD(WRDE_APPEND_test);128ATF_TC_BODY(WRDE_APPEND_test, tc)129{130wordexp_t we;131int r;132133r = wordexp("this is", &we, 0);134ATF_REQUIRE(r == 0);135r = wordexp("a test", &we, WRDE_APPEND);136ATF_REQUIRE(r == 0);137ATF_REQUIRE(we.we_wordc == 4);138ATF_REQUIRE(strcmp(we.we_wordv[0], "this") == 0);139ATF_REQUIRE(strcmp(we.we_wordv[1], "is") == 0);140ATF_REQUIRE(strcmp(we.we_wordv[2], "a") == 0);141ATF_REQUIRE(strcmp(we.we_wordv[3], "test") == 0);142ATF_REQUIRE(we.we_wordv[4] == NULL);143wordfree(&we);144}145146ATF_TC_WITHOUT_HEAD(WRDE_DOOFFS__WRDE_APPEND_test);147ATF_TC_BODY(WRDE_DOOFFS__WRDE_APPEND_test, tc)148{149wordexp_t we;150int r;151152we.we_offs = 2;153r = wordexp("this is", &we, WRDE_DOOFFS);154ATF_REQUIRE(r == 0);155r = wordexp("a test", &we, WRDE_APPEND|WRDE_DOOFFS);156ATF_REQUIRE(r == 0);157r = wordexp("of wordexp", &we, WRDE_APPEND|WRDE_DOOFFS);158ATF_REQUIRE(r == 0);159ATF_REQUIRE(we.we_wordc == 6);160ATF_REQUIRE(we.we_wordv[0] == NULL);161ATF_REQUIRE(we.we_wordv[1] == NULL);162ATF_REQUIRE(strcmp(we.we_wordv[2], "this") == 0);163ATF_REQUIRE(strcmp(we.we_wordv[3], "is") == 0);164ATF_REQUIRE(strcmp(we.we_wordv[4], "a") == 0);165ATF_REQUIRE(strcmp(we.we_wordv[5], "test") == 0);166ATF_REQUIRE(strcmp(we.we_wordv[6], "of") == 0);167ATF_REQUIRE(strcmp(we.we_wordv[7], "wordexp") == 0);168ATF_REQUIRE(we.we_wordv[8] == NULL);169wordfree(&we);170}171172ATF_TC_WITHOUT_HEAD(WRDE_UNDEF_test);173ATF_TC_BODY(WRDE_UNDEF_test, tc)174{175wordexp_t we;176int r;177178r = wordexp("${dont_set_me}", &we, WRDE_UNDEF);179ATF_REQUIRE(r == WRDE_BADVAL);180}181182ATF_TC_WITHOUT_HEAD(WRDE_NOCMD_test);183ATF_TC_BODY(WRDE_NOCMD_test, tc)184{185wordexp_t we;186int r;187188r = wordexp("`date`", &we, WRDE_NOCMD);189ATF_REQUIRE(r == WRDE_CMDSUB);190r = wordexp("\"`date`\"", &we, WRDE_NOCMD);191ATF_REQUIRE(r == WRDE_CMDSUB);192r = wordexp("$(date)", &we, WRDE_NOCMD);193ATF_REQUIRE(r == WRDE_CMDSUB);194r = wordexp("\"$(date)\"", &we, WRDE_NOCMD);195ATF_REQUIRE(r == WRDE_CMDSUB);196r = wordexp("$((3+5))", &we, WRDE_NOCMD);197ATF_REQUIRE(r == 0);198r = wordexp("\\$\\(date\\)", &we, WRDE_NOCMD|WRDE_REUSE);199ATF_REQUIRE(r == 0);200r = wordexp("'`date`'", &we, WRDE_NOCMD|WRDE_REUSE);201ATF_REQUIRE(r == 0);202r = wordexp("'$(date)'", &we, WRDE_NOCMD|WRDE_REUSE);203ATF_REQUIRE(r == 0);204wordfree(&we);205}206207ATF_TC_WITHOUT_HEAD(WRDE_BADCHAR_test);208ATF_TC_BODY(WRDE_BADCHAR_test, tc)209{210wordexp_t we;211int r;212213r = wordexp("'\n|&;<>(){}'", &we, 0);214ATF_REQUIRE(r == 0);215r = wordexp("\"\n|&;<>(){}\"", &we, WRDE_REUSE);216ATF_REQUIRE(r == 0);217r = wordexp("\\\n\\|\\&\\;\\<\\>\\(\\)\\{\\}", &we, WRDE_REUSE);218ATF_REQUIRE(r == 0);219wordfree(&we);220r = wordexp("test \n test", &we, 0);221ATF_REQUIRE(r == WRDE_BADCHAR);222r = wordexp("test | test", &we, 0);223ATF_REQUIRE(r == WRDE_BADCHAR);224r = wordexp("test & test", &we, 0);225ATF_REQUIRE(r == WRDE_BADCHAR);226r = wordexp("test ; test", &we, 0);227ATF_REQUIRE(r == WRDE_BADCHAR);228r = wordexp("test > test", &we, 0);229ATF_REQUIRE(r == WRDE_BADCHAR);230r = wordexp("test < test", &we, 0);231ATF_REQUIRE(r == WRDE_BADCHAR);232r = wordexp("test ( test", &we, 0);233ATF_REQUIRE(r == WRDE_BADCHAR);234r = wordexp("test ) test", &we, 0);235ATF_REQUIRE(r == WRDE_BADCHAR);236r = wordexp("test { test", &we, 0);237ATF_REQUIRE(r == WRDE_BADCHAR);238r = wordexp("test } test", &we, 0);239ATF_REQUIRE(r == WRDE_BADCHAR);240}241242ATF_TC_WITHOUT_HEAD(WRDE_SYNTAX_test);243ATF_TC_BODY(WRDE_SYNTAX_test, tc)244{245wordexp_t we;246int r;247248r = wordexp("'", &we, 0);249ATF_REQUIRE(r == WRDE_SYNTAX);250r = wordexp("'", &we, WRDE_UNDEF);251ATF_REQUIRE(r == WRDE_SYNTAX);252r = wordexp("'\\'", &we, 0);253ATF_REQUIRE(r == 0);254ATF_REQUIRE(we.we_wordc == 1);255ATF_REQUIRE(strcmp(we.we_wordv[0], "\\") == 0);256ATF_REQUIRE(we.we_wordv[1] == NULL);257wordfree(&we);258/* Two syntax errors that are not detected by the current we_check(). */259r = wordexp("${IFS:+'}", &we, 0);260ATF_REQUIRE(r == WRDE_SYNTAX);261r = wordexp("${IFS:+'}", &we, WRDE_UNDEF);262ATF_REQUIRE(r == WRDE_SYNTAX);263r = wordexp("$(case)", &we, 0);264ATF_REQUIRE(r == WRDE_SYNTAX);265r = wordexp("$(case)", &we, WRDE_UNDEF);266ATF_REQUIRE(r == WRDE_SYNTAX);267}268269ATF_TC_WITHOUT_HEAD(with_SIGCHILD_handler_test);270ATF_TC_BODY(with_SIGCHILD_handler_test, tc)271{272struct sigaction sa;273wordexp_t we;274int r;275276/* With a SIGCHLD handler that reaps all zombies. */277sa.sa_flags = 0;278sigemptyset(&sa.sa_mask);279sa.sa_handler = chld_handler;280r = sigaction(SIGCHLD, &sa, NULL);281ATF_REQUIRE(r == 0);282r = wordexp("hello world", &we, 0);283ATF_REQUIRE(r == 0);284ATF_REQUIRE(we.we_wordc == 2);285ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0);286ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0);287ATF_REQUIRE(we.we_wordv[2] == NULL);288wordfree(&we);289sa.sa_handler = SIG_DFL;290r = sigaction(SIGCHLD, &sa, NULL);291ATF_REQUIRE(r == 0);292}293294ATF_TC_WITHOUT_HEAD(with_SIGCHILD_ignore_test);295ATF_TC_BODY(with_SIGCHILD_ignore_test, tc)296{297struct sigaction sa;298wordexp_t we;299int r;300301/* With SIGCHLD set to ignore so that the kernel auto-reaps zombies. */302sa.sa_flags = 0;303sigemptyset(&sa.sa_mask);304sa.sa_handler = SIG_IGN;305r = sigaction(SIGCHLD, &sa, NULL);306ATF_REQUIRE(r == 0);307r = wordexp("hello world", &we, 0);308ATF_REQUIRE(r == 0);309ATF_REQUIRE(we.we_wordc == 2);310ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0);311ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0);312ATF_REQUIRE(we.we_wordv[2] == NULL);313wordfree(&we);314sa.sa_handler = SIG_DFL;315r = sigaction(SIGCHLD, &sa, NULL);316ATF_REQUIRE(r == 0);317}318319ATF_TC_WITHOUT_HEAD(with_unused_non_default_IFS_test);320ATF_TC_BODY(with_unused_non_default_IFS_test, tc)321{322wordexp_t we;323int r;324325/*326* With IFS set to a non-default value (without depending on whether327* IFS is inherited or not).328*/329r = setenv("IFS", ":", 1);330ATF_REQUIRE(r == 0);331r = wordexp("hello world", &we, 0);332ATF_REQUIRE(r == 0);333ATF_REQUIRE(we.we_wordc == 2);334ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0);335ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0);336ATF_REQUIRE(we.we_wordv[2] == NULL);337wordfree(&we);338r = unsetenv("IFS");339ATF_REQUIRE(r == 0);340}341342ATF_TC_WITHOUT_HEAD(with_used_non_default_IFS_test);343ATF_TC_BODY(with_used_non_default_IFS_test, tc)344{345wordexp_t we;346int r;347348/*349* With IFS set to a non-default value, and using it.350*/351r = setenv("IFS", ":", 1);352ATF_REQUIRE(r == 0);353r = wordexp("${IFS+hello:world}", &we, 0);354ATF_REQUIRE(r == 0);355ATF_REQUIRE(we.we_wordc == 2);356ATF_REQUIRE(strcmp(we.we_wordv[0], "hello") == 0);357ATF_REQUIRE(strcmp(we.we_wordv[1], "world") == 0);358ATF_REQUIRE(we.we_wordv[2] == NULL);359wordfree(&we);360r = unsetenv("IFS");361ATF_REQUIRE(r == 0);362}363364ATF_TP_ADD_TCS(tp)365{366ATF_TP_ADD_TC(tp, simple_test);367ATF_TP_ADD_TC(tp, long_output_test);368ATF_TP_ADD_TC(tp, WRDE_DOOFFS_test);369ATF_TP_ADD_TC(tp, WRDE_REUSE_test);370ATF_TP_ADD_TC(tp, WRDE_APPEND_test);371ATF_TP_ADD_TC(tp, WRDE_DOOFFS__WRDE_APPEND_test);372ATF_TP_ADD_TC(tp, WRDE_UNDEF_test);373ATF_TP_ADD_TC(tp, WRDE_NOCMD_test);374ATF_TP_ADD_TC(tp, WRDE_BADCHAR_test);375ATF_TP_ADD_TC(tp, WRDE_SYNTAX_test);376ATF_TP_ADD_TC(tp, with_SIGCHILD_handler_test);377ATF_TP_ADD_TC(tp, with_SIGCHILD_ignore_test);378ATF_TP_ADD_TC(tp, with_unused_non_default_IFS_test);379ATF_TP_ADD_TC(tp, with_used_non_default_IFS_test);380381return (atf_no_error());382}383384385