test-wordexp.c revision 174692
1108639Stjr/*- 2108639Stjr * Copyright (c) 2003 Tim J. Robbins 3108639Stjr * All rights reserved. 4108639Stjr * 5108639Stjr * Redistribution and use in source and binary forms, with or without 6108639Stjr * modification, are permitted provided that the following conditions 7108639Stjr * are met: 8108639Stjr * 1. Redistributions of source code must retain the above copyright 9108639Stjr * notice, this list of conditions and the following disclaimer. 10108639Stjr * 2. Redistributions in binary form must reproduce the above copyright 11108639Stjr * notice, this list of conditions and the following disclaimer in the 12108639Stjr * documentation and/or other materials provided with the distribution. 13108639Stjr * 14108639Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15108639Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16108639Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17108639Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18108639Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19108639Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20108639Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21108639Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22108639Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23108639Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24108639Stjr * SUCH DAMAGE. 25108639Stjr */ 26108639Stjr 27108639Stjr/* 28108639Stjr * Test program for wordexp() and wordfree() as specified by 29108639Stjr * IEEE Std. 1003.1-2001. 30108639Stjr */ 31108639Stjr 32108639Stjr#include <sys/cdefs.h> 33108639Stjr__FBSDID("$FreeBSD: head/tools/regression/lib/libc/gen/test-wordexp.c 174692 2007-12-16 23:38:55Z das $"); 34108639Stjr 35108639Stjr#include <assert.h> 36108639Stjr#include <stdio.h> 37108639Stjr#include <stdlib.h> 38108639Stjr#include <string.h> 39108639Stjr#include <wordexp.h> 40108639Stjr 41108639Stjrint 42108639Stjrmain(int argc, char *argv[]) 43108639Stjr{ 44108639Stjr wordexp_t we; 45108639Stjr int r; 46108639Stjr 47108639Stjr /* Test that the macros are there. */ 48174692Sdas (void)(WRDE_APPEND + WRDE_DOOFFS + WRDE_NOCMD + WRDE_REUSE + 49108639Stjr WRDE_SHOWERR + WRDE_UNDEF); 50108639Stjr (void)(WRDE_BADCHAR + WRDE_BADVAL + WRDE_CMDSUB + WRDE_NOSPACE + 51108639Stjr WRDE_SYNTAX); 52108639Stjr 53108639Stjr /* Simple test. */ 54108639Stjr r = wordexp("hello world", &we, 0); 55108639Stjr assert(r == 0); 56108639Stjr assert(we.we_wordc == 2); 57108639Stjr assert(strcmp(we.we_wordv[0], "hello") == 0); 58108639Stjr assert(strcmp(we.we_wordv[1], "world") == 0); 59108639Stjr assert(we.we_wordv[2] == NULL); 60108639Stjr wordfree(&we); 61108639Stjr 62174692Sdas /* WRDE_DOOFFS */ 63108639Stjr we.we_offs = 3; 64174692Sdas r = wordexp("hello world", &we, WRDE_DOOFFS); 65108639Stjr assert(r == 0); 66108639Stjr assert(we.we_wordc == 2); 67108639Stjr assert(we.we_wordv[0] == NULL); 68108639Stjr assert(we.we_wordv[1] == NULL); 69108639Stjr assert(we.we_wordv[2] == NULL); 70108639Stjr assert(strcmp(we.we_wordv[3], "hello") == 0); 71108639Stjr assert(strcmp(we.we_wordv[4], "world") == 0); 72108639Stjr assert(we.we_wordv[5] == NULL); 73108639Stjr wordfree(&we); 74108639Stjr 75108639Stjr /* WRDE_REUSE */ 76108639Stjr r = wordexp("hello world", &we, 0); 77108639Stjr r = wordexp("hello world", &we, WRDE_REUSE); 78108639Stjr assert(r == 0); 79108639Stjr assert(we.we_wordc == 2); 80108639Stjr assert(strcmp(we.we_wordv[0], "hello") == 0); 81108639Stjr assert(strcmp(we.we_wordv[1], "world") == 0); 82108639Stjr assert(we.we_wordv[2] == NULL); 83108639Stjr wordfree(&we); 84108639Stjr 85108639Stjr /* WRDE_APPEND */ 86108639Stjr r = wordexp("this is", &we, 0); 87108639Stjr assert(r == 0); 88108639Stjr r = wordexp("a test", &we, WRDE_APPEND); 89108639Stjr assert(r == 0); 90108639Stjr assert(we.we_wordc == 4); 91108639Stjr assert(strcmp(we.we_wordv[0], "this") == 0); 92108639Stjr assert(strcmp(we.we_wordv[1], "is") == 0); 93108639Stjr assert(strcmp(we.we_wordv[2], "a") == 0); 94108639Stjr assert(strcmp(we.we_wordv[3], "test") == 0); 95108639Stjr assert(we.we_wordv[4] == NULL); 96108639Stjr wordfree(&we); 97108639Stjr 98174692Sdas /* WRDE_DOOFFS + WRDE_APPEND */ 99108639Stjr we.we_offs = 2; 100174692Sdas r = wordexp("this is", &we, WRDE_DOOFFS); 101108639Stjr assert(r == 0); 102174692Sdas r = wordexp("a test", &we, WRDE_APPEND|WRDE_DOOFFS); 103108639Stjr assert(r == 0); 104174692Sdas r = wordexp("of wordexp", &we, WRDE_APPEND|WRDE_DOOFFS); 105108639Stjr assert(r == 0); 106108639Stjr assert(we.we_wordc == 6); 107108639Stjr assert(we.we_wordv[0] == NULL); 108108639Stjr assert(we.we_wordv[1] == NULL); 109108639Stjr assert(strcmp(we.we_wordv[2], "this") == 0); 110108639Stjr assert(strcmp(we.we_wordv[3], "is") == 0); 111108639Stjr assert(strcmp(we.we_wordv[4], "a") == 0); 112108639Stjr assert(strcmp(we.we_wordv[5], "test") == 0); 113108639Stjr assert(strcmp(we.we_wordv[6], "of") == 0); 114108639Stjr assert(strcmp(we.we_wordv[7], "wordexp") == 0); 115108639Stjr assert(we.we_wordv[8] == NULL); 116108639Stjr wordfree(&we); 117108639Stjr 118108639Stjr /* WRDE_UNDEF */ 119108639Stjr r = wordexp("${dont_set_me}", &we, WRDE_UNDEF); 120108639Stjr assert(r == WRDE_BADVAL); 121108639Stjr 122108639Stjr /* WRDE_NOCMD */ 123108639Stjr r = wordexp("`date`", &we, WRDE_NOCMD); 124108639Stjr assert(r == WRDE_CMDSUB); 125108639Stjr r = wordexp("\"`date`\"", &we, WRDE_NOCMD); 126108639Stjr assert(r == WRDE_CMDSUB); 127108639Stjr r = wordexp("$(date)", &we, WRDE_NOCMD); 128108639Stjr assert(r == WRDE_CMDSUB); 129108639Stjr r = wordexp("\"$(date)\"", &we, WRDE_NOCMD); 130108639Stjr assert(r == WRDE_CMDSUB); 131108639Stjr r = wordexp("$((3+5))", &we, WRDE_NOCMD); 132108639Stjr assert(r == 0); 133108639Stjr r = wordexp("\\$\\(date\\)", &we, WRDE_NOCMD|WRDE_REUSE); 134108639Stjr assert(r == 0); 135108639Stjr r = wordexp("'`date`'", &we, WRDE_NOCMD|WRDE_REUSE); 136108639Stjr assert(r == 0); 137108639Stjr r = wordexp("'$(date)'", &we, WRDE_NOCMD|WRDE_REUSE); 138108639Stjr assert(r == 0); 139108639Stjr wordfree(&we); 140108639Stjr 141108639Stjr /* WRDE_BADCHAR */ 142108639Stjr r = wordexp("'\n|&;<>(){}'", &we, 0); 143108639Stjr assert(r == 0); 144108639Stjr r = wordexp("\"\n|&;<>(){}\"", &we, WRDE_REUSE); 145108639Stjr assert(r == 0); 146108639Stjr r = wordexp("\\\n\\|\\&\\;\\<\\>\\(\\)\\{\\}", &we, WRDE_REUSE); 147108639Stjr assert(r == 0); 148108639Stjr wordfree(&we); 149108639Stjr r = wordexp("test \n test", &we, 0); 150108639Stjr assert(r == WRDE_BADCHAR); 151108639Stjr r = wordexp("test | test", &we, 0); 152108639Stjr assert(r == WRDE_BADCHAR); 153108639Stjr r = wordexp("test & test", &we, 0); 154108639Stjr assert(r == WRDE_BADCHAR); 155108639Stjr r = wordexp("test ; test", &we, 0); 156108639Stjr assert(r == WRDE_BADCHAR); 157108639Stjr r = wordexp("test > test", &we, 0); 158108639Stjr assert(r == WRDE_BADCHAR); 159108639Stjr r = wordexp("test < test", &we, 0); 160108639Stjr assert(r == WRDE_BADCHAR); 161108639Stjr r = wordexp("test ( test", &we, 0); 162108639Stjr assert(r == WRDE_BADCHAR); 163108639Stjr r = wordexp("test ) test", &we, 0); 164108639Stjr assert(r == WRDE_BADCHAR); 165108639Stjr r = wordexp("test { test", &we, 0); 166108639Stjr assert(r == WRDE_BADCHAR); 167108639Stjr r = wordexp("test } test", &we, 0); 168108639Stjr assert(r == WRDE_BADCHAR); 169108639Stjr 170108639Stjr printf("PASS wordexp()\n"); 171108639Stjr printf("PASS wordfree()\n"); 172108639Stjr 173108639Stjr return (0); 174108639Stjr} 175