test-wordexp.c revision 108639
1/*-
2 * Copyright (c) 2003 Tim J. Robbins
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Test program for wordexp() and wordfree() as specified by
29 * IEEE Std. 1003.1-2001.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/tools/regression/lib/libc/gen/test-wordexp.c 108639 2003-01-04 05:50:35Z tjr $");
34
35#include <assert.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <wordexp.h>
40
41int
42main(int argc, char *argv[])
43{
44	wordexp_t we;
45	int r;
46
47	/* Test that the macros are there. */
48	(void)(WRDE_APPEND + WRDE_DOOFS + WRDE_NOCMD + WRDE_REUSE +
49	    WRDE_SHOWERR + WRDE_UNDEF);
50	(void)(WRDE_BADCHAR + WRDE_BADVAL + WRDE_CMDSUB + WRDE_NOSPACE +
51	    WRDE_SYNTAX);
52
53	/* Simple test. */
54	r = wordexp("hello world", &we, 0);
55	assert(r == 0);
56	assert(we.we_wordc == 2);
57	assert(strcmp(we.we_wordv[0], "hello") == 0);
58	assert(strcmp(we.we_wordv[1], "world") == 0);
59	assert(we.we_wordv[2] == NULL);
60	wordfree(&we);
61
62	/* WRDE_DOOFS */
63	we.we_offs = 3;
64	r = wordexp("hello world", &we, WRDE_DOOFS);
65	assert(r == 0);
66	assert(we.we_wordc == 2);
67	assert(we.we_wordv[0] == NULL);
68	assert(we.we_wordv[1] == NULL);
69	assert(we.we_wordv[2] == NULL);
70	assert(strcmp(we.we_wordv[3], "hello") == 0);
71	assert(strcmp(we.we_wordv[4], "world") == 0);
72	assert(we.we_wordv[5] == NULL);
73	wordfree(&we);
74
75	/* WRDE_REUSE */
76	r = wordexp("hello world", &we, 0);
77	r = wordexp("hello world", &we, WRDE_REUSE);
78	assert(r == 0);
79	assert(we.we_wordc == 2);
80	assert(strcmp(we.we_wordv[0], "hello") == 0);
81	assert(strcmp(we.we_wordv[1], "world") == 0);
82	assert(we.we_wordv[2] == NULL);
83	wordfree(&we);
84
85	/* WRDE_APPEND */
86	r = wordexp("this is", &we, 0);
87	assert(r == 0);
88	r = wordexp("a test", &we, WRDE_APPEND);
89	assert(r == 0);
90	assert(we.we_wordc == 4);
91	assert(strcmp(we.we_wordv[0], "this") == 0);
92	assert(strcmp(we.we_wordv[1], "is") == 0);
93	assert(strcmp(we.we_wordv[2], "a") == 0);
94	assert(strcmp(we.we_wordv[3], "test") == 0);
95	assert(we.we_wordv[4] == NULL);
96	wordfree(&we);
97
98	/* WRDE_DOOFS + WRDE_APPEND */
99	we.we_offs = 2;
100	r = wordexp("this is", &we, WRDE_DOOFS);
101	assert(r == 0);
102	r = wordexp("a test", &we, WRDE_APPEND|WRDE_DOOFS);
103	assert(r == 0);
104	r = wordexp("of wordexp", &we, WRDE_APPEND|WRDE_DOOFS);
105	assert(r == 0);
106	assert(we.we_wordc == 6);
107	assert(we.we_wordv[0] == NULL);
108	assert(we.we_wordv[1] == NULL);
109	assert(strcmp(we.we_wordv[2], "this") == 0);
110	assert(strcmp(we.we_wordv[3], "is") == 0);
111	assert(strcmp(we.we_wordv[4], "a") == 0);
112	assert(strcmp(we.we_wordv[5], "test") == 0);
113	assert(strcmp(we.we_wordv[6], "of") == 0);
114	assert(strcmp(we.we_wordv[7], "wordexp") == 0);
115	assert(we.we_wordv[8] == NULL);
116	wordfree(&we);
117
118	/* WRDE_UNDEF */
119	r = wordexp("${dont_set_me}", &we, WRDE_UNDEF);
120	assert(r == WRDE_BADVAL);
121
122	/* WRDE_NOCMD */
123	r = wordexp("`date`", &we, WRDE_NOCMD);
124	assert(r == WRDE_CMDSUB);
125	r = wordexp("\"`date`\"", &we, WRDE_NOCMD);
126	assert(r == WRDE_CMDSUB);
127	r = wordexp("$(date)", &we, WRDE_NOCMD);
128	assert(r == WRDE_CMDSUB);
129	r = wordexp("\"$(date)\"", &we, WRDE_NOCMD);
130	assert(r == WRDE_CMDSUB);
131	r = wordexp("$((3+5))", &we, WRDE_NOCMD);
132	assert(r == 0);
133	r = wordexp("\\$\\(date\\)", &we, WRDE_NOCMD|WRDE_REUSE);
134	assert(r == 0);
135	r = wordexp("'`date`'", &we, WRDE_NOCMD|WRDE_REUSE);
136	assert(r == 0);
137	r = wordexp("'$(date)'", &we, WRDE_NOCMD|WRDE_REUSE);
138	assert(r == 0);
139	wordfree(&we);
140
141	/* WRDE_BADCHAR */
142	r = wordexp("'\n|&;<>(){}'", &we, 0);
143	assert(r == 0);
144	r = wordexp("\"\n|&;<>(){}\"", &we, WRDE_REUSE);
145	assert(r == 0);
146	r = wordexp("\\\n\\|\\&\\;\\<\\>\\(\\)\\{\\}", &we, WRDE_REUSE);
147	assert(r == 0);
148	wordfree(&we);
149	r = wordexp("test \n test", &we, 0);
150	assert(r == WRDE_BADCHAR);
151	r = wordexp("test | test", &we, 0);
152	assert(r == WRDE_BADCHAR);
153	r = wordexp("test & test", &we, 0);
154	assert(r == WRDE_BADCHAR);
155	r = wordexp("test ; test", &we, 0);
156	assert(r == WRDE_BADCHAR);
157	r = wordexp("test > test", &we, 0);
158	assert(r == WRDE_BADCHAR);
159	r = wordexp("test < test", &we, 0);
160	assert(r == WRDE_BADCHAR);
161	r = wordexp("test ( test", &we, 0);
162	assert(r == WRDE_BADCHAR);
163	r = wordexp("test ) test", &we, 0);
164	assert(r == WRDE_BADCHAR);
165	r = wordexp("test { test", &we, 0);
166	assert(r == WRDE_BADCHAR);
167	r = wordexp("test } test", &we, 0);
168	assert(r == WRDE_BADCHAR);
169
170	printf("PASS wordexp()\n");
171	printf("PASS wordfree()\n");
172
173	return (0);
174}
175