xref: /titanic_41/usr/src/lib/libshell/common/tests/bracket.sh (revision 9ea11b96f9f71eef4f1bd2062ea054451a32a216)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34null=''
35if	[[ ! -z $null ]]
36then	err_exit "-z: null string should be of zero length"
37fi
38file=$tmp/original
39newer_file=$tmp/newer
40if	[[ -z $file ]]
41then	err_exit "-z: $file string should not be of zero length"
42fi
43if	[[ -a $file ]]
44then	err_exit "-a: $file shouldn't exist"
45fi
46> $file
47if	[[ ! -a $file ]]
48then	err_exit "-a: $file should exist"
49fi
50chmod 777 $file
51if	[[ ! -r $file ]]
52then	err_exit "-r: $file should be readable"
53fi
54if	[[ ! -w $file ]]
55then	err_exit "-w: $file should be writable"
56fi
57if	[[ ! -w $file ]]
58then	err_exit "-x: $file should be executable"
59fi
60if	[[ ! -w $file || ! -r $file ]]
61then	err_exit "-rw: $file should be readable/writable"
62fi
63if	[[ -s $file ]]
64then	err_exit "-s: $file should be of zero size"
65fi
66if	[[ ! -f $file ]]
67then	err_exit "-f: $file should be an ordinary file"
68fi
69if	[[  -d $file ]]
70then	err_exit "-f: $file should not be a directory file"
71fi
72if	[[  ! -d . ]]
73then	err_exit "-d: . should not be a directory file"
74fi
75if	[[  -f /dev/null ]]
76then	err_exit "-f: /dev/null  should not be an ordinary file"
77fi
78chmod 000 $file
79if	[[ -r $file ]]
80then	err_exit "-r: $file should not be readable"
81fi
82if	[[ ! -O $file ]]
83then	err_exit "-r: $file should be owned by me"
84fi
85if	[[ -w $file ]]
86then	err_exit "-w: $file should not be writable"
87fi
88if	[[ -w $file ]]
89then	err_exit "-x: $file should not be executable"
90fi
91if	[[ -w $file || -r $file ]]
92then	err_exit "-rw: $file should not be readable/writable"
93fi
94if	[[   -z x &&  -z x || ! -z x ]]
95then	:
96else	err_exit " wrong precedence"
97fi
98if	[[   -z x &&  (-z x || ! -z x) ]]
99then	err_exit " () grouping not working"
100fi
101if	[[ foo < bar ]]
102then	err_exit "foo comes before bar"
103fi
104[[ . -ef $(pwd) ]] || err_exit ". is not $PWD"
105set -o allexport
106[[ -o allexport ]] || err_exit '-o: did not set allexport option'
107if	[[ -n  $null ]]
108then	err_exit "'$null' has non-zero length"
109fi
110if	[[ ! -r /dev/fd/0 ]]
111then	err_exit "/dev/fd/0 not open for reading"
112fi
113if	[[ ! -w /dev/fd/2 ]]
114then	err_exit "/dev/fd/2 not open for writing"
115fi
116sleep 1
117> $newer_file
118if	[[ ! $file -ot $newer_file ]]
119then	err_exit "$file should be older than $newer_file"
120fi
121if	[[ $file -nt $newer_file ]]
122then	err_exit "$newer_file should be newer than $file"
123fi
124if	[[ $file != $tmp/* ]]
125then	err_exit "$file should match $tmp/*"
126fi
127if	[[ $file = $tmp'/*' ]]
128then	err_exit "$file should not equal $tmp'/*'"
129fi
130[[ ! ( ! -z $null && ! -z x) ]]	|| err_exit "negation and grouping"
131[[ -z '' || -z '' || -z '' ]]	|| err_exit "three ors not working"
132[[ -z '' &&  -z '' && -z '' ]]	|| err_exit "three ors not working"
133(exit 8)
134if	[[ $? -ne 8 || $? -ne 8 ]]
135then	err_exit 'value $? within [[...]]'
136fi
137x='(x'
138if	[[ '(x' != '('* ]]
139then	err_exit " '(x' does not match '('* within [[...]]"
140fi
141if	[[ '(x' != "("* ]]
142then	err_exit ' "(x" does not match "("* within [[...]]'
143fi
144if	[[ '(x' != \(* ]]
145then	err_exit ' "(x" does not match \(* within [[...]]'
146fi
147if	[[ 'x(' != *'(' ]]
148then	err_exit " 'x(' does not match '('* within [[...]]"
149fi
150if	[[ 'x&' != *'&' ]]
151then	err_exit " 'x&' does not match '&'* within [[...]]"
152fi
153if	[[ 'xy' = *'*' ]]
154then	err_exit " 'xy' matches *'*' within [[...]]"
155fi
156if	[[ 3 > 4 ]]
157then	err_exit '3 < 4'
158fi
159if	[[ 4 < 3 ]]
160then	err_exit '3 > 4'
161fi
162if	[[ 3x > 4x ]]
163then	err_exit '3x < 4x'
164fi
165x='bin|dev|?'
166cd /
167if	[[ $(print $x) != "$x" ]]
168then	err_exit 'extended pattern matching on command arguments'
169fi
170if	[[ dev != $x ]]
171then	err_exit 'extended pattern matching not working on variables'
172fi
173if	[[ -u $SHELL ]]
174then	err_exit "setuid on $SHELL"
175fi
176if	[[ -g $SHELL ]]
177then	err_exit "setgid on $SHELL"
178fi
179test -d .  -a '(' ! -f . ')' || err_exit 'test not working'
180if	[[ '!' != ! ]]
181then	err_exit 'quoting unary operator not working'
182fi
183test \( -n x \) -o \( -n y \) 2> /dev/null || err_exit 'test ( -n x ) -o ( -n y) not working'
184test \( -n x \) -o -n y 2> /dev/null || err_exit 'test ( -n x ) -o -n y not working'
185chmod 600 $file
186exec 4> $file
187print -u4 foobar
188if	[[ ! -s $file ]]
189then	err_exit "-s: $file should be non-zero"
190fi
191exec 4>&-
192if	[[ 011 -ne 11 ]]
193then	err_exit "leading zeros in arithmetic compares not ignored"
194fi
195{
196	set -x
197	[[ foo > bar ]]
198} 2> /dev/null || { set +x; err_exit "foo<bar with -x enabled" ;}
199set +x
200(
201	eval "[[ (a) ]]"
202) 2> /dev/null || err_exit "[[ (a) ]] not working"
203> $file
204chmod 4755 "$file"
205if	test -u $file && test ! -u $file
206then	err_exit "test ! -u suidfile not working"
207fi
208for i in '(' ')' '[' ']'
209do	[[ $i == $i ]] || err_exit "[[ $i != $i ]]"
210done
211(
212	[[ aaaa == {4}(a) ]] || err_exit 'aaaa != {4}(a)'
213	[[ aaaa == {2,5}(a) ]] || err_exit 'aaaa != {2,4}(a)'
214	[[ abcdcdabcd == {3,6}(ab|cd) ]] || err_exit 'abcdcdabcd == {3,4}(ab|cd)'
215	[[ abcdcdabcde == {5}(ab|cd)e ]] || err_exit 'abcdcdabcd == {5}(ab|cd)e'
216) || err_exit 'errors with {..}(...) patterns'
217[[ D290.2003.02.16.temp == D290.+(2003.02.16).temp* ]] || err_exit 'pattern match bug with +(...)'
218rm -rf $file
219{
220[[ -N $file ]] && err_exit 'test -N $tmp/*: st_mtime>st_atime after creat'
221sleep 2
222print 'hello world'
223[[ -N $file ]] || err_exit 'test -N $tmp/*: st_mtime<=st_atime after write'
224sleep 2
225read
226[[ -N $file ]] && err_exit 'test -N $tmp/*: st_mtime>st_atime after read'
227} > $file < $file
228if	rm -rf "$file" && ln -s / "$file"
229then	[[ -L "$file" ]] || err_exit '-L not working'
230	[[ -L "$file"/ ]] && err_exit '-L with file/ not working'
231fi
232$SHELL -c 't=1234567890; [[ $t == @({10}(\d)) ]]' 2> /dev/null || err_exit ' @({10}(\d)) pattern not working'
233$SHELL -c '[[ att_ == ~(E)(att|cus)_.* ]]' 2> /dev/null || err_exit ' ~(E)(att|cus)_* pattern not working'
234$SHELL -c '[[ att_ =~ (att|cus)_.* ]]' 2> /dev/null || err_exit ' =~ ere not working'
235$SHELL -c '[[ abc =~ a(b)c ]]' 2> /dev/null || err_exit '[[ abc =~ a(b)c ]] fails'
236$SHELL -xc '[[ abc =~  \babc\b ]]' 2> /dev/null || err_exit '[[ abc =~ \babc\b ]] fails'
237[[ abc == ~(E)\babc\b ]] || err_exit '\b not preserved for ere when not in ()'
238[[ abc == ~(iEi)\babc\b ]] || err_exit '\b not preserved for ~(iEi) when not in ()'
239
240e=$($SHELL -c '[ -z "" -a -z "" ]' 2>&1)
241[[ $e ]] && err_exit "[ ... ] compatibility check failed -- $e"
242i=hell
243[[ hell0 == $i[0] ]]  ||  err_exit 'pattern $i[0] interpreded as array ref'
244test '(' = ')' && err_exit '"test ( = )" should not be true'
245[[ $($SHELL -c 'case  F in ~(Eilr)[a-z0-9#]) print ok;;esac' 2> /dev/null) == ok ]] || err_exit '~(Eilr) not working in case command'
246[[ $($SHELL -c "case  Q in ~(Fi)q |  \$'\E') print ok;;esac" 2> /dev/null) == ok ]] || err_exit '~(Fi)q | \E  not working in case command'
247
248for l in C en_US.ISO8859-15
249do	[[ $($SHELL -c "LC_COLLATE=$l" 2>&1) ]] && continue
250	export LC_COLLATE=$l
251	set -- \
252		'A'   0 1 1   0 1 1      1 0 0   1 0 0   \
253		'Z'   0 1 1   0 1 1      1 0 0   1 0 0   \
254		'/'   0 0 0   0 0 0      1 1 1   1 1 1   \
255		'.'   0 0 0   0 0 0      1 1 1   1 1 1   \
256		'_'   0 0 0   0 0 0      1 1 1   1 1 1   \
257		'-'   1 1 1   1 1 1      0 0 0   0 0 0   \
258		'%'   0 0 0   0 0 0      1 1 1   1 1 1   \
259		'@'   0 0 0   0 0 0      1 1 1   1 1 1   \
260		'!'   0 0 0   0 0 0      1 1 1   1 1 1   \
261		'^'   0 0 0   0 0 0      1 1 1   1 1 1   \
262		# retain this line #
263	while	(( $# >= 13 ))
264	do	c=$1
265		shift
266		for p in \
267			'[![.-.]]' \
268			'[![.-.][:upper:]]' \
269			'[![.-.]A-Z]' \
270			'[!-]' \
271			'[!-[:upper:]]' \
272			'[!-A-Z]' \
273			'[[.-.]]' \
274			'[[.-.][:upper:]]' \
275			'[[.-.]A-Z]' \
276			'[-]' \
277			'[-[:upper:]]' \
278			'[-A-Z]' \
279			# retain this line #
280		do	e=$1
281			shift
282			[[ $c == $p ]]
283			g=$?
284			[[ $g == $e ]] || err_exit "[[ '$c' == $p ]] for LC_COLLATE=$l failed -- expected $e, got $g"
285		done
286	done
287done
288integer n
289if	( : < /dev/tty ) 2>/dev/null && exec {n}< /dev/tty
290then	[[ -t  $n ]] || err_exit "[[ -t  n ]] fails when n > 9"
291fi
292foo=([1]=a [2]=b [3]=c)
293[[ -v foo[1] ]] ||  err_exit 'foo[1] should be set'
294[[ ${foo[1]+x} ]] ||  err_exit '${foo[1]+x} should be x'
295[[ ${foo[@]+x} ]] ||  err_exit '${foo[@]+x} should be x'
296unset foo[1]
297[[ -v foo[1] ]] && err_exit 'foo[1] should not be set'
298[[ ${foo[1]+x} ]] &&  err_exit '${foo[1]+x} should be empty'
299bar=(a b c)
300[[ -v bar[1] ]]  || err_exit 'bar[1] should be set'
301[[ ${bar[1]+x} ]] ||  err_exit '${foo[1]+x} should be x'
302unset bar[1]
303[[ ${bar[1]+x} ]] &&  err_exit '${foo[1]+x} should be empty'
304[[ -v bar ]] || err_exit 'bar should be set'
305[[ -v bar[1] ]] && err_exit 'bar[1] should not be set'
306integer z=( 1 2 4)
307[[ -v z[1] ]] || err_exit 'z[1] should be set'
308unset z[1]
309[[ -v z[1] ]] && err_exit 'z[1] should not be set'
310typeset -si y=( 1 2 4)
311[[ -v y[6] ]] && err_exit 'y[6] should not be set'
312[[ -v y[1] ]] ||  err_exit  'y[1] should be set'
313unset y[1]
314[[ -v y[1] ]] && err_exit 'y[1] should not be set'
315x=abc
316[[ -v x[0] ]] || err_exit  'x[0] should be set'
317[[ ${x[0]+x} ]] || err_exit print  '${x[0]+x} should be x'
318[[ -v x[3] ]] && err_exit 'x[3] should not be set'
319[[ ${x[3]+x} ]] && err_exit  '${x[0]+x} should be Empty'
320unset x
321[[ ${x[@]+x} ]] && err_exit  '${x[@]+x} should be Empty'
322unset x y z foo bar
323exit $((Errors))
324