1#!/bin/sh
2# Test for wordexp(3).
3# Copyright (C) 1998-2022 Free Software Foundation, Inc.
4# This file is part of the GNU C Library.
5
6# The GNU C Library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10
11# The GNU C Library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# Lesser General Public License for more details.
15
16# You should have received a copy of the GNU Lesser General Public
17# License along with the GNU C Library; if not, see
18# <https://www.gnu.org/licenses/>.
19
20set -e
21
22# Some of these tests may look a little weird.
23# The first parameter to wordexp-test is what it gives to wordexp.
24# The others are just there to be parameters.
25
26common_objpfx=$1; shift
27test_program_prefix_before_env=$1; shift
28run_program_env=$1; shift
29test_program_prefix_after_env=$1; shift
30logfile=${common_objpfx}posix/wordexp-tst.out
31testout=${common_objpfx}posix/wordexp-test-result
32
33result=0
34rm -f $logfile
35# This is written in this funny way so that there is no trailing whitespace.
36# The first line contains a space followed by a tab.
37IFS=" 	\
38
39"
40export IFS
41
42failed=0
43${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
44${test_program_prefix_after_env} \
45${common_objpfx}posix/wordexp-test '$*' > ${testout}1
46cat <<"EOF" | cmp - ${testout}1 >> $logfile || failed=1
47wordexp returned 0
48we_wordv[0] = "$*"
49EOF
50if test $failed -ne 0; then
51  echo '$* test failed'
52  status=1
53fi
54
55failed=0
56${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
57${test_program_prefix_after_env} \
58${common_objpfx}posix/wordexp-test '${*}' unquoted > ${testout}2
59cat <<"EOF" | cmp - ${testout}2 >> $logfile || failed=1
60wordexp returned 0
61we_wordv[0] = "${*}"
62we_wordv[1] = "unquoted"
63EOF
64if test $failed -ne 0; then
65  echo '${*} test failed'
66  status=1
67fi
68
69failed=0
70${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
71${test_program_prefix_after_env} \
72${common_objpfx}posix/wordexp-test '$@' unquoted > ${testout}3
73cat <<"EOF" | cmp - ${testout}3 >> $logfile || failed=1
74wordexp returned 0
75we_wordv[0] = "$@"
76we_wordv[1] = "unquoted"
77EOF
78if test $failed -ne 0; then
79  echo '$@ test failed'
80  status=1
81fi
82
83failed=0
84${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
85${test_program_prefix_after_env} \
86${common_objpfx}posix/wordexp-test '"$* quoted"' param > ${testout}4
87cat <<"EOF" | cmp - ${testout}4 >> $logfile || failed=1
88wordexp returned 0
89we_wordv[0] = ""$* quoted" param quoted"
90EOF
91if test $failed -ne 0; then
92  echo '$* quoted test failed'
93  status=1
94fi
95
96failed=0
97${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
98${test_program_prefix_after_env} \
99${common_objpfx}posix/wordexp-test '"$@ quoted"' param > ${testout}5
100cat <<"EOF" | cmp - ${testout}5 >> $logfile || failed=1
101wordexp returned 0
102we_wordv[0] = ""$@ quoted""
103we_wordv[1] = "param quoted"
104EOF
105if test $failed -ne 0; then
106  echo '$@ quoted test failed'
107  status=1
108fi
109# Why?  Because bash does it that way..
110
111failed=0
112${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
113${test_program_prefix_after_env} \
114${common_objpfx}posix/wordexp-test '$#' 2 3 4 5 > ${testout}6
115cat <<"EOF" | cmp - ${testout}6 >> $logfile || failed=1
116wordexp returned 0
117we_wordv[0] = "5"
118EOF
119if test $failed -ne 0; then
120  echo '$# test failed'
121  status=1
122fi
123
124failed=0
125${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
126${test_program_prefix_after_env} \
127${common_objpfx}posix/wordexp-test '$2 ${3} $4' 2nd 3rd "4 th" > ${testout}7
128cat <<"EOF" | cmp - ${testout}7 >> $logfile || failed=1
129wordexp returned 0
130we_wordv[0] = "2nd"
131we_wordv[1] = "3rd"
132we_wordv[2] = "4"
133we_wordv[3] = "th"
134EOF
135if test $failed -ne 0; then
136  echo '$2 ${3} $4 test failed'
137  status=1
138fi
139
140failed=0
141${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
142${test_program_prefix_after_env} \
143${common_objpfx}posix/wordexp-test '${11}' 2 3 4 5 6 7 8 9 10 11 > ${testout}8
144cat <<"EOF" | cmp - ${testout}8 >> $logfile || failed=1
145wordexp returned 0
146we_wordv[0] = "11"
147EOF
148if test $failed -ne 0; then
149  echo '${11} test failed'
150  status=1
151fi
152
153failed=0
154${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
155${test_program_prefix_after_env} \
156${common_objpfx}posix/wordexp-test '"a $@ b"' c d > ${testout}9
157cat <<"EOF" | cmp - ${testout}9 >> $logfile || failed=1
158wordexp returned 0
159we_wordv[0] = "a "a $@ b""
160we_wordv[1] = "c"
161we_wordv[2] = "d b"
162EOF
163if test $failed -ne 0; then
164  echo '"a $@ b" test failed'
165  status=1
166fi
167
168${test_program_prefix_before_env} ${run_program_env} IFS="$IFS" \
169${test_program_prefix_after_env} \
170${common_objpfx}posix/wordexp-test '${#@} ${#2} *$**' two 3 4 > ${testout}10
171cat <<"EOF" | cmp - ${testout}10 || failed=1
172wordexp returned 0
173we_wordv[0] = "4"
174we_wordv[1] = "3"
175we_wordv[2] = "*${#@}"
176we_wordv[3] = "${#2}"
177we_wordv[4] = "*$**"
178we_wordv[5] = "two"
179we_wordv[6] = "3"
180we_wordv[7] = "4*"
181EOF
182
183exit $result
184