xref: /titanic_50/usr/src/lib/libshell/common/tests/bracket.sh (revision 44743693dce3212f5edba623e0cb0327bd4337a3)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                      by AT&T Knowledge Ventures                      #
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
30null=''
31if	[[ ! -z $null ]]
32then	err_exit "-z: null string should be of zero length"
33fi
34file=/tmp/regress$$
35if	[[ -z $file ]]
36then	err_exit "-z: $file string should not be of zero length"
37fi
38trap "rm -f $file" EXIT
39rm -f $file
40if	[[ -a $file ]]
41then	err_exit "-a: $file shouldn't exist"
42fi
43> $file
44if	[[ ! -a $file ]]
45then	err_exit "-a: $file should exist"
46fi
47chmod 777 $file
48if	[[ ! -r $file ]]
49then	err_exit "-r: $file should be readable"
50fi
51if	[[ ! -w $file ]]
52then	err_exit "-w: $file should be writable"
53fi
54if	[[ ! -w $file ]]
55then	err_exit "-x: $file should be executable"
56fi
57if	[[ ! -w $file || ! -r $file ]]
58then	err_exit "-rw: $file should be readable/writable"
59fi
60if	[[ -s $file ]]
61then	err_exit "-s: $file should be of zero size"
62fi
63if	[[ ! -f $file ]]
64then	err_exit "-f: $file should be an ordinary file"
65fi
66if	[[  -d $file ]]
67then	err_exit "-f: $file should not be a directory file"
68fi
69if	[[  ! -d . ]]
70then	err_exit "-d: . should not be a directory file"
71fi
72if	[[  -f /dev/null ]]
73then	err_exit "-f: /dev/null  should not be an ordinary file"
74fi
75chmod 000 $file
76if	[[ -r $file ]]
77then	err_exit "-r: $file should not be readable"
78fi
79if	[[ ! -O $file ]]
80then	err_exit "-r: $file should be owned by me"
81fi
82if	[[ -w $file ]]
83then	err_exit "-w: $file should not be writable"
84fi
85if	[[ -w $file ]]
86then	err_exit "-x: $file should not be executable"
87fi
88if	[[ -w $file || -r $file ]]
89then	err_exit "-rw: $file should not be readable/writable"
90fi
91if	[[   -z x &&  -z x || ! -z x ]]
92then	:
93else	err_exit " wrong precedence"
94fi
95if	[[   -z x &&  (-z x || ! -z x) ]]
96then	err_exit " () grouping not working"
97fi
98if	[[ foo < bar ]]
99then	err_exit "foo comes before bar"
100fi
101[[ . -ef $(pwd) ]] || err_exit ". is not $PWD"
102set -o allexport
103[[ -o allexport ]] || err_exit '-o: did not set allexport option'
104if	[[ -n  $null ]]
105then	err_exit "'$null' has non-zero length"
106fi
107if	[[ ! -r /dev/fd/0 ]]
108then	err_exit "/dev/fd/0 not open for reading"
109fi
110if	[[ ! -w /dev/fd/2 ]]
111then	err_exit "/dev/fd/2 not open for writing"
112fi
113sleep 1
114if	[[ ! . -ot $file ]]
115then	err_exit ". should be older than $file"
116fi
117if	[[ /bin -nt $file ]]
118then	err_exit "$file should be newer than /bin"
119fi
120if	[[ $file != /tmp/* ]]
121then	err_exit "$file should match /tmp/*"
122fi
123if	[[ $file = '/tmp/*' ]]
124then	err_exit "$file should not equal /tmp/*"
125fi
126[[ ! ( ! -z $null && ! -z x) ]]	|| err_exit "negation and grouping"
127[[ -z '' || -z '' || -z '' ]]	|| err_exit "three ors not working"
128[[ -z '' &&  -z '' && -z '' ]]	|| err_exit "three ors not working"
129(exit 8)
130if	[[ $? -ne 8 || $? -ne 8 ]]
131then	err_exit 'value $? within [[...]]'
132fi
133x='(x'
134if	[[ '(x' != '('* ]]
135then	err_exit " '(x' does not match '('* within [[...]]"
136fi
137if	[[ '(x' != "("* ]]
138then	err_exit ' "(x" does not match "("* within [[...]]'
139fi
140if	[[ '(x' != \(* ]]
141then	err_exit ' "(x" does not match \(* within [[...]]'
142fi
143if	[[ 'x(' != *'(' ]]
144then	err_exit " 'x(' does not match '('* within [[...]]"
145fi
146if	[[ 'x&' != *'&' ]]
147then	err_exit " 'x&' does not match '&'* within [[...]]"
148fi
149if	[[ 'xy' = *'*' ]]
150then	err_exit " 'xy' matches *'*' within [[...]]"
151fi
152if	[[ 3 > 4 ]]
153then	err_exit '3 < 4'
154fi
155if	[[ 4 < 3 ]]
156then	err_exit '3 > 4'
157fi
158if	[[ 3x > 4x ]]
159then	err_exit '3x < 4x'
160fi
161x='bin|dev|?'
162cd /
163if	[[ $(print $x) != "$x" ]]
164then	err_exit 'extended pattern matching on command arguments'
165fi
166if	[[ dev != $x ]]
167then	err_exit 'extended pattern matching not working on variables'
168fi
169if	[[ -u $SHELL ]]
170then	err_exit "setuid on $SHELL"
171fi
172if	[[ -g $SHELL ]]
173then	err_exit "setgid on $SHELL"
174fi
175test -d .  -a '(' ! -f . ')' || err_exit 'test not working'
176if	[[ '!' != ! ]]
177then	err_exit 'quoting unary operator not working'
178fi
179chmod 600 $file
180exec 4> $file
181print -u4 foobar
182if	[[ ! -s $file ]]
183then	err_exit "-s: $file should be non-zero"
184fi
185exec 4>&-
186if	[[ 011 -ne 11 ]]
187then	err_exit "leading zeros in arithmetic compares not ignored"
188fi
189{
190	set -x
191	[[ foo > bar ]]
192} 2> /dev/null || { set +x; err_exit "foo<bar with -x enabled" ;}
193set +x
194(
195	eval "[[ (a) ]]"
196) 2> /dev/null || err_exit "[[ (a) ]] not working"
197> $file
198chmod 4755 "$file"
199if	test -u $file && test ! -u $file
200then	err_exit "test ! -u suidfile not working"
201fi
202for i in '(' ')' '[' ']'
203do	[[ $i == $i ]] || err_exit "[[ $i != $i ]]"
204done
205(
206	[[ aaaa == {4}(a) ]] || err_exit 'aaaa != {4}(a)'
207	[[ aaaa == {2,5}(a) ]] || err_exit 'aaaa != {2,4}(a)'
208	[[ abcdcdabcd == {3,6}(ab|cd) ]] || err_exit 'abcdcdabcd == {3,4}(ab|cd)'
209	[[ abcdcdabcde == {5}(ab|cd)e ]] || err_exit 'abcdcdabcd == {5}(ab|cd)e'
210) || err_exit 'Errors with {..}(...) patterns'
211[[ D290.2003.02.16.temp == D290.+(2003.02.16).temp* ]] || err_exit 'pattern match bug with +(...)'
212rm -rf $file
213{
214[[ -N $file ]] && err_exit 'test -N /tmp/*: st_mtime>st_atime after creat'
215sleep 2
216print 'hello world'
217[[ -N $file ]] || err_exit 'test -N /tmp/*: st_mtime<=st_atime after write'
218sleep 2
219read
220[[ -N $file ]] && err_exit 'test -N /tmp/*: st_mtime>st_atime after read'
221} > $file < $file
222if	rm -rf "$file" && ln -s / "$file"
223then	[[ -L "$file" ]] || err_exit '-L not working'
224	[[ -L "$file"/ ]] && err_exit '-L with file/ not working'
225fi
226$SHELL -c 't=1234567890; [[ $t == @({10}(\d)) ]]' 2> /dev/null || err_exit ' @({10}(\d)) pattern not working'
227$SHELL -c '[[ att_ == ~(E)(att|cus)_.* ]]' 2> /dev/null || err_exit ' ~(E)(att|cus)_* pattern not working'
228$SHELL -c '[[ att_ =~ (att|cus)_.* ]]' 2> /dev/null || err_exit ' =~ ere not working'
229$SHELL -c '[[ abc =~ a(b)c ]]' 2> /dev/null || err_exit '[[ abc =~ a(b)c ]] fails'
230$SHELL -xc '[[ abc =~  \babc\b ]]' 2> /dev/null || err_exit '[[ abc =~ \babc\b ]] fails'
231[[ abc == ~(E)\babc\b ]] || err_exit '\b not preserved for ere when not in ()'
232[[ abc == ~(iEi)\babc\b ]] || err_exit '\b not preserved for ~(iEi) when not in ()'
233exit $((Errors))
234