1#-
2# Copyright (c) 2005  - Garance Alistair Drosehn <gad@FreeBSD.org>.
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
28testpgm=/usr/bin/env
29gblenv=PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
30gblenv=TESTVAR=Global-TV-Value
31gblenv=OUTSIDEVAR=OutsideValue
32
33#  These first two tests are testing how well the regression-script itself is
34#  handling environment-variables, as much as testing the `env' program.
35[test]
36   sb_args:/bin/sh
37   setenv:TESTVAR=a1a
38   script:/bin/echo A-${TESTVAR}-Z
39   stdout:A-a1a-Z
40[run]
41[test]
42   sb_args:-S /bin/sh
43   script:/bin/echo A-${TESTVAR}-Z
44   stdout:A-Global-TV-Value-Z
45[run]
46
47[test]
48   sb_args:-S TESTVAR=bb22bb /bin/sh
49   script:/bin/echo A-${TESTVAR}-Z
50   stdout:A-bb22bb-Z
51[run]
52[test]
53   sb_args:-S\_TESTVAR=ab22ab\_/bin/sh
54   script:/bin/echo A-${TESTVAR}-Z
55   stdout:A-ab22ab-Z
56[run]
57[test]
58   sb_args:-S\_TESTVAR="abc\_33\_abc"\_/bin/sh
59   script:/bin/echo A-${TESTVAR}-Z
60   stdout:A-abc 33 abc-Z
61[run]
62
63#   First we see that 'sh' can not be found in /usr/sbin, and then
64#   we show that it can be found without changing PATH by using -P
65#   And then show that it can be NOT found by using -P...
66[test]
67   sb_args:-S sh
68   setenv:PATH=/usr/sbin
69   script:/bin/echo A-${PATH}-Z
70   $?:127
71   stderr:[%-testpgm.basename-%]: sh: No such file or directory
72[run]
73[test]
74   sb_args:-S -P/bin sh
75   setenv:PATH=/usr/sbin
76   script:/bin/echo A-${PATH}-Z
77   stdout:A-/usr/sbin-Z
78[run]
79[test]
80   sb_args:-S -P/sbin:/usr/sbin sh
81   script:/bin/echo A-${PATH}-Z
82   $?:127
83   stderr:[%-testpgm.basename-%]: sh: No such file or directory
84[run]
85
86#  Hmm.  I wonder if -P should always set an 'ENV_PATH' variable?
87[test]
88   sb_args:-S -P/bin:/usr/bin:${PATH} ENV_PATH=/bin:/usr/bin:${PATH} sh
89   setenv:PATH=/usr/sbin
90   script:/bin/echo A-${PATH}-Z
91   script:/bin/echo B-${ENV_PATH}-Y
92   stdout:A-/usr/sbin-Z
93   stdout:B-/bin:/usr/bin:/usr/sbin-Y
94[run]
95
96#   Show that the comment-characters are working, both for where they are
97#   recognized and where they are ignored.
98[test]
99   sb_args:-STESTVAR="abc44abc" /bin/sh # This is some arbitrary text
100   user_args:us11er us22er
101   script:/bin/echo A-${TESTVAR}-Z B-$1-Y
102   stdout:A-abc44abc-Z B-us11er-Y
103[run]
104[test]
105   sb_args:-STESTVAR="abc55abc" /bin/sh \c This is some arbitrary text
106   user_args:us11er us22er
107   script:/bin/echo A-${TESTVAR}-Z B-$1-Y
108   stdout:A-abc55abc-Z B-us11er-Y
109[run]
110[test]
111   sb_args:-STESTVAR=abc#44#abc /bin/sh
112   user_args:us11er us22er
113   script:/bin/echo A-${TESTVAR}-Z B-$1-Y
114   stdout:A-abc#44#abc-Z B-us11er-Y
115[run]
116[test]
117   sb_args:-STESTVAR='abc\c55\cabc' /bin/sh
118   user_args:us11er us22er
119   script:/bin/echo A-${TESTVAR}-Z B-$1-Y
120   stdout:A-abc\c55\cabc-Z B-us11er-Y
121[run]
122
123#   Test various aspects of quoted strings
124[test]
125   sb_args:-STESTVAR="abc'def" /bin/sh
126   script:/bin/echo A-${TESTVAR}-Z
127   stdout:A-abc'def-Z
128[run]
129[test]
130   sb_args:-STESTVAR='abc"def' /bin/sh
131   script:/bin/echo A-${TESTVAR}-Z
132   stdout:A-abc"def-Z
133[run]
134[test]
135   sb_args:-STESTVAR='ab\'cd\'ef' /bin/sh
136   script:/bin/echo A-${TESTVAR}-Z
137   stdout:A-ab'cd'ef-Z
138[run]
139[test]
140   sb_args:-STESTVAR='abc\"def\'ghi' /bin/sh
141   script:/bin/echo A-${TESTVAR}-Z
142   stdout:A-abc\"def'ghi-Z
143[run]
144[test]
145   sb_args:-STESTVAR='abc''def''ghi' /bin/sh
146   script:/bin/echo A-${TESTVAR}-Z
147   stdout:A-abcdefghi-Z
148[run]
149[test]
150   sb_args:-STESTVAR='abc\ndef\nghi' /bin/sh
151   script:/bin/echo "A-${TESTVAR}-Z"
152   stdout:A-abc\ndef\nghi-Z
153[run]
154[test]
155   sb_args:-STESTVAR="abc\ndef\nghi" /bin/sh
156   script:/bin/echo "A-${TESTVAR}-Z"
157   stdout:A-abc
158   stdout:def
159   stdout:ghi-Z
160[run]
161[test]
162   sb_args:-STESTVAR=""\_OTHERVAR=""\_/bin/sh
163   script:/bin/echo A-${TESTVAR}-M-${OTHERVAR}-Z
164   stdout:A--M--Z
165[run]
166[test]
167   sb_args:-STESTVAR=no-term-"-dq... /bin/sh
168   script:/bin/echo "A-${TESTVAR}-Z"
169   $?:1
170   stderr:[%-testpgm.basename-%]: No terminating quote for string: TESTVAR=no-term-"-dq... /bin/sh
171[run]
172[test]
173   sb_args:-STESTVAR=no-term-'-sq... /bin/sh
174   script:/bin/echo "A-${TESTVAR}-Z"
175   $?:1
176   stderr:[%-testpgm.basename-%]: No terminating quote for string: TESTVAR=no-term-'-sq... /bin/sh
177[run]
178
179# Some tests of variable-substitution.
180[test]
181   sb_args:-S TESTVAR=${TEST7} /bin/sh
182   setenv:TEST7=a23456a
183   script:/bin/echo "A-${TESTVAR}-Z"
184   stdout:A-a23456a-Z
185[run]
186[test]
187   sb_args:-S TESTVAR=${TEST8} /bin/sh
188   setenv:TEST8=b234567b
189   script:/bin/echo "A-${TESTVAR}-Z"
190   stdout:A-b234567b-Z
191[run]
192[test]
193   sb_args:-S TESTVAR=${TEST9} /bin/sh
194   setenv:TEST9=c2345678c
195   script:/bin/echo "A-${TESTVAR}-Z"
196   stdout:A-c2345678c-Z
197[run]
198[test]
199   sb_args:-S TESTVAR=${TEST8}+${TEST9}+${TEST10} /bin/sh
200   setenv:TEST8=a234567z
201   setenv:TEST9=a2345678z
202   setenv:TEST10=a23456789z
203   script:/bin/echo "A-${TESTVAR}-Z"
204   stdout:A-a234567z+a2345678z+a23456789z-Z
205[run]
206[test]
207   sb_args:-S TESTVAR=$* /bin/sh
208   script:/bin/echo "A-${TESTVAR}-Z"
209   $?:1
210   stderr:[%-testpgm.basename-%]: Only ${VARNAME} expansion is supported, error at: $* /bin/sh
211[run]
212[test]
213   sb_args:-S TESTVAR=/usr/bin:$PATH /bin/sh
214   script:/bin/echo "A-${TESTVAR}-Z"
215   $?:1
216   stderr:[%-testpgm.basename-%]: Only ${VARNAME} expansion is supported, error at: $PATH /bin/sh
217[run]
218
219#  For a short time `env' was changed to recognize 'something=value' as a
220#  valid utility name if 'something' begins with a '/'.  However, that was
221#  a bad idea, since variable-names with a '/' -- while rare -- are still
222#  more blessed by standards than a filename with an '=' in it.  So, this
223#  test goes back to expecting an error...
224[test]
225   symlink:/bin/echo /tmp/envtest=echo
226   sb_args:-S/tmp/envtest=echo false
227   $?:1
228[run]
229
230# Show interactions between -i (clear environment), and ${VAR} substitution,
231# and that -i will clear the environment at the right point in processing...
232[test]
233   sb_args:-iS PATH=/bin:/usr/bin:/Not WASPATH=${PATH} WASOUT=${OUTSIDEVAR} TESTVAR=SbValue WASTEST=${TESTVAR} /bin/sh
234   script:/bin/echo "=== set ==="
235   script:# drop some environment variables that 'sh' itself sets, and
236   script:# then have 'set' print out all remaining environment variables.
237   script:# (can't unset OPTIND/PWD, so we use grep to get rid of those)
238   script:unset -v IFS PS1 PS2 PS4 PPID
239   script:set | grep -Ev '^(OPTIND|PWD)=' | sort
240   stdout:=== set ===
241   stdout:PATH=/bin:/usr/bin:/Not
242   stdout:TESTVAR=SbValue
243   stdout:WASOUT=OutsideValue
244   stdout:WASPATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
245   stdout:WASTEST=Global-TV-Value
246[run]
247
248# Had a bug with ${VAR} expansion if the character before the $ was
249# one of the argument-separator characters.  So the first of the
250# following worked, but the second one failed:
251[test]
252   sb_args:-Secho Testv:${TESTV} Scriptname:
253   setenv:TESTV=ab/ba
254   stdout:Testv:ab/ba Scriptname: [%-script.pathname-%]
255[run]
256[test]
257   sb_args:-Secho testV: ${TESTV} scriptname:
258   setenv:TESTV=cd/dc
259   stdout:testV: cd/dc scriptname: [%-script.pathname-%]
260[run]
261
262#  A "nothing variable" inside a quoted string should produce a zero-length
263#  argument, but if it's outside of quotes then it should result in, well,
264#  nothing.  Note the tricks we play with [%-script.pathname-%] so that we
265#  can supply parameters *to* the script, even though the kernel is always
266#  going to stick the script name on as ARG[2] when invoking `env'.
267[test]
268   sb_args:-S/bin/sh [%-script.pathname-%] userDQ: "" SQ: '' scriptname:
269   setenv:TNADA=
270   script:printf "list_args.sh with \$# = $#\n"
271   script:# Process all parameters.
272   script:N=0
273   script:while test $# != 0 ; do
274   script:    N=$(($N+1))
275   script:    printf "....\$$N = [%3d] '$1'\n" ${#1}
276   script:    shift
277   script:done
278   stdout:list_args.sh with $# = 6
279   stdout:....$1 = [  7] 'userDQ:'
280   stdout:....$2 = [  0] ''
281   stdout:....$3 = [  3] 'SQ:'
282   stdout:....$4 = [  0] ''
283   stdout:....$5 = [ 11] 'scriptname:'
284   stdout:....$6 = [ 16] '/tmp/env-regress'
285[run]
286[test]
287   sb_args:-S/bin/sh [%-script.pathname-%] userB "${TNADA}" scriptname:
288   setenv:TNADA=
289   script:printf "list_args.sh with \$# = $#\n"
290   script:# Process all parameters.
291   script:N=0
292   script:while test $# != 0 ; do
293   script:    N=$(($N+1))
294   script:    printf "....\$$N = [%3d] '$1'\n" ${#1}
295   script:    shift
296   script:done
297   stdout:list_args.sh with $# = 4
298   stdout:....$1 = [  5] 'userB'
299   stdout:....$2 = [  0] ''
300   stdout:....$3 = [ 11] 'scriptname:'
301   stdout:....$4 = [ 16] '/tmp/env-regress'
302[run]
303[test]
304   sb_args:-S/bin/sh [%-script.pathname-%] userA ${TNADA} scriptname:
305   setenv:TNADA=
306   script:printf "list_args.sh with \$# = $#\n"
307   script:# Process all parameters.
308   script:N=0
309   script:while test $# != 0 ; do
310   script:    N=$(($N+1))
311   script:    printf "....\$$N = [%3d] '$1'\n" ${#1}
312   script:    shift
313   script:done
314   stdout:list_args.sh with $# = 3
315   stdout:....$1 = [  5] 'userA'
316   stdout:....$2 = [ 11] 'scriptname:'
317   stdout:....$3 = [ 16] '[%-script.pathname-%]'
318[run]
319[test]
320   sb_args:-S/bin/sh [%-script.pathname-%] ${A} ${NB} ${C} ${ND} ${NE} ${F} S:
321   setenv:A=A_ThisisAlongstring_A1
322   setenv:NB=
323   setenv:C=C_ThisisAlongstring_C1
324   setenv:ND=
325   setenv:NE=
326   setenv:F=F_ThisisAlongstring_F1
327   script:printf "list_args.sh with \$# = $#\n"
328   script:# Process all parameters.
329   script:N=0
330   script:while test $# != 0 ; do
331   script:    N=$(($N+1))
332   script:    printf "....\$$N = [%3d] '$1'\n" ${#1}
333   script:    shift
334   script:done
335   stdout:list_args.sh with $# = 5
336   stdout:....$1 = [ 22] 'A_ThisisAlongstring_A1'
337   stdout:....$2 = [ 22] 'C_ThisisAlongstring_C1'
338   stdout:....$3 = [ 22] 'F_ThisisAlongstring_F1'
339   stdout:....$4 = [  2] 'S:'
340   stdout:....$5 = [ 16] '/tmp/env-regress'
341[run]
342[test]
343   sb_args:-S/bin/sh [%-script.pathname-%] ${A} ${NB} "${NB}" ${NB} ${C} "${ND}" ${NE} ${F} S:
344   setenv:A=A_ThisisAlongstring_A1
345   setenv:NB=
346   setenv:C=C_ThisisAlongstring_C1
347   setenv:ND=
348   setenv:NE=
349   setenv:F=F_ThisisAlongstring_F1
350   script:printf "list_args.sh with \$# = $#\n"
351   script:# Process all parameters.
352   script:N=0
353   script:while test $# != 0 ; do
354   script:    N=$(($N+1))
355   script:    printf "....\$$N = [%3d] '$1'\n" ${#1}
356   script:    shift
357   script:done
358   stdout:list_args.sh with $# = 7
359   stdout:....$1 = [ 22] 'A_ThisisAlongstring_A1'
360   stdout:....$2 = [  0] ''
361   stdout:....$3 = [ 22] 'C_ThisisAlongstring_C1'
362   stdout:....$4 = [  0] ''
363   stdout:....$5 = [ 22] 'F_ThisisAlongstring_F1'
364   stdout:....$6 = [  2] 'S:'
365   stdout:....$7 = [ 16] '/tmp/env-regress'
366[run]
367
368[test]
369   sb_args:-S/bin/echo ${A} ${B} ${C} ${D} ScriptName:
370   setenv:A=A_ThisisAlongstring_A1
371   setenv:B=B_ThisisAlongstring_B1
372   setenv:C=C_ThisisAlongstring_C1
373   setenv:D=D_ThisisAlongstring_D1
374   stdout:A_ThisisAlongstring_A1 B_ThisisAlongstring_B1 C_ThisisAlongstring_C1 D_ThisisAlongstring_D1 ScriptName: [%-script.pathname-%]
375[run]
376[test]
377   sb_args:-S/bin/echo ${A} "${B}" ${C} "${D}" ScriptName:
378   setenv:A=A_ThisisAlongstring_A1
379   setenv:B=B_ThisisAlongstring_B1
380   setenv:C=C_ThisisAlongstring_C1
381   setenv:D=D_ThisisAlongstring_D1
382   stdout:A_ThisisAlongstring_A1 B_ThisisAlongstring_B1 C_ThisisAlongstring_C1 D_ThisisAlongstring_D1 ScriptName: [%-script.pathname-%]
383[run]
384
385[test]
386   sb_args:sh
387   script:[%-testpgm.pathname-%] -S '\c' >/dev/null
388[run]
389[test]
390   sb_args:sh
391   script:[%-testpgm.pathname-%] -S'\c' >/dev/null
392[run]
393[test]
394   sb_args:sh
395   script:[%-testpgm.pathname-%] -u foo -S '\c' >/dev/null
396[run]
397[test]
398   sb_args:sh
399   script:[%-testpgm.pathname-%] -u foo -S'\c' >/dev/null
400[run]
401[test]
402   sb_args:sh
403   script:[%-testpgm.pathname-%] -S '-u bar \c' >/dev/null
404[run]
405[test]
406   sb_args:sh
407   script:[%-testpgm.pathname-%] -S'-u bar \c' >/dev/null
408[run]
409[test]
410   sb_args:sh
411   script:[%-testpgm.pathname-%] -u foo -S '-u bar \c' >/dev/null
412[run]
413[test]
414   sb_args:sh
415   script:[%-testpgm.pathname-%] -u foo -S'-u bar \c' >/dev/null
416[run]
417