xref: /titanic_50/usr/src/lib/libshell/common/tests/attributes.sh (revision 694c35faa87b858ecdadfe4fc592615f4eefbb07)
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
34r=readonly u=Uppercase l=Lowercase i=22 i8=10 L=abc L5=def uL5=abcdef xi=20
35x=export t=tagged H=hostname LZ5=026 RZ5=026 Z5=123 lR5=ABcdef R5=def n=l
36for option in u l i i8 L L5 LZ5 RZ5 Z5 r x H t R5 uL5 lR5 xi n
37do	typeset -$option $option
38done
39(r=newval) 2> /dev/null && err_exit readonly attribute fails
40i=i+5
41if	((i != 27))
42then	err_exit integer attributes fails
43fi
44if	[[ $i8 != 8#12 ]]
45then	err_exit integer base 8 fails
46fi
47if	[[ $u != UPPERCASE ]]
48then	err_exit uppercase fails
49fi
50if	[[ $l != lowercase ]]
51then	err_exit lowercase fails
52fi
53if	[[ $n != lowercase ]]
54then	err_exit reference variables fail
55fi
56if	[[ t=tagged != $(typeset -t) ]]
57then	err_exit tagged fails
58fi
59if	[[ t != $(typeset +t) ]]
60then	err_exit tagged fails
61fi
62if	[[ $Z5 != 00123 ]]
63then	err_exit zerofill fails
64fi
65if	[[ $RZ5 != 00026 ]]
66then	err_exit right zerofill fails
67fi
68L=12345
69if	[[ $L != 123 ]]
70then	err_exit leftjust fails
71fi
72if	[[ $L5 != "def  " ]]
73then	err_exit leftjust fails
74fi
75if	[[ $uL5 != ABCDE ]]
76then	err_exit leftjust uppercase fails
77fi
78if	[[ $lR5 != bcdef ]]
79then	err_exit rightjust fails
80fi
81if	[[ $R5 != "  def" ]]
82then	err_exit rightjust fails
83fi
84if	[[ $($SHELL -c 'echo $x') != export ]]
85then	err_exit export fails
86fi
87if	[[ $($SHELL -c 'xi=xi+4;echo $xi') != 24 ]]
88then	err_exit export attributes fails
89fi
90x=$(foo=abc $SHELL <<!
91	foo=bar
92	$SHELL -c  'print \$foo'
93!
94)
95if	[[ $x != bar ]]
96then	err_exit 'environment variables require re-export'
97fi
98(typeset + ) > /dev/null 2>&1 || err_exit 'typeset + not working'
99(typeset -L-5 buf="A" 2>/dev/null)
100if [[ $? == 0 ]]
101then	err_exit 'typeset allows negative field for left/right adjust'
102fi
103a=b
104readonly $a=foo
105if	[[ $b != foo ]]
106then	err_exit 'readonly $a=b not working'
107fi
108if	[[ $(export | grep '^PATH=') != PATH=* ]]
109then	err_exit 'export not working'
110fi
111picture=(
112	bitmap=/fruit
113	size=(typeset -E x=2.5)
114)
115string="$(print $picture)"
116if [[ "${string}" != *'size=( typeset -E'* ]]
117then	err_exit 'print of compound exponential variable not working'
118fi
119sz=(typeset -E y=2.2)
120string="$(print $sz)"
121if [[ "${sz}" == *'typeset -E -F'* ]]
122then 	err_exit 'print of exponential shows both -E and -F attributes'
123fi
124print 'typeset -i m=48/4+1;print -- $m' > $tmp/script
125chmod +x $tmp/script
126typeset -Z2 m
127if	[[ $($tmp/script) != 13 ]]
128then	err_exit 'attributes not cleared for script execution'
129fi
130print 'print VAR=$VAR' > $tmp/script
131typeset -L70 VAR=var
132$tmp/script > $tmp/script.1
133[[ $(< $tmp/script.1) == VAR= ]] || err_exit 'typeset -L should not be inherited'
134typeset -Z  LAST=00
135unset -f foo
136function foo
137{
138        if [[ $1 ]]
139        then    LAST=$1
140        else    ((LAST++))
141        fi
142}
143foo 1
144if	(( ${#LAST} != 2 ))
145then	err_exit 'LAST!=2'
146fi
147foo
148if	(( ${#LAST} != 2 ))
149then	err_exit 'LAST!=2'
150fi
151[[ $(set | grep LAST) == LAST=02 ]] || err_exit "LAST not correct in set list"
152set -a
153unset foo
154foo=bar
155if	[[ $(export | grep ^foo=) != 'foo=bar' ]]
156then	err_exit 'all export not working'
157fi
158unset foo
159read foo <<!
160bar
161!
162if	[[ $(export | grep ^foo=) != 'foo=bar' ]]
163then	err_exit 'all export not working with read'
164fi
165if	[[ $(typeset | grep PS2) == PS2 ]]
166then	err_exit 'typeset without arguments outputs names without attributes'
167fi
168unset a z q x
169w1=hello
170w2=world
171t1="$w1 $w2"
172if	(( 'a' == 97 ))
173then	b1=aGVsbG8gd29ybGQ=
174	b2=aGVsbG8gd29ybGRoZWxsbyB3b3JsZA==
175else	b1=iIWTk5ZAppaZk4Q=
176	b2=iIWTk5ZAppaZk4SIhZOTlkCmlpmThA==
177fi
178z=$b1
179typeset -b x=$b1
180[[ $x == "$z" ]] || print -u2 'binary variable not expanding correctly'
181[[  $(printf "%B" x) == $t1 ]] || err_exit 'typeset -b not working'
182typeset -b -Z5 a=$b1
183[[  $(printf "%B" a) == $w1 ]] || err_exit 'typeset -b -Z5 not working'
184typeset -b q=$x$x
185[[ $q == $b2 ]] || err_exit 'typeset -b not working with concatination'
186[[  $(printf "%B" q) == $t1$t1 ]] || err_exit 'typeset -b concatination not working'
187x+=$b1
188[[ $x == $b2 ]] || err_exit 'typeset -b not working with append'
189[[  $(printf "%B" x) == $t1$t1 ]] || err_exit 'typeset -b append not working'
190typeset -b -Z20 z=$b1
191(( $(printf "%B" z | wc -c) == 20 )) || err_exit 'typeset -b -Z20 not storing 20 bytes'
192{
193	typeset -b v1 v2
194	read -N11 v1
195	read -N22 v2
196} << !
197hello worldhello worldhello world
198!
199[[ $v1 == "$b1" ]] || err_exit "v1=$v1 should be $b1"
200[[ $v2 == "$x" ]] || err_exit "v1=$v2 should be $x"
201if	env '!=1' >/dev/null 2>&1
202then	[[ $(env '!=1' $SHELL -c 'echo ok' 2>/dev/null) == ok ]] || err_exit 'malformed environment terminates shell'
203fi
204unset var
205typeset -b var
206printf '12%Z34' | read -r -N 5 var
207[[ $var == MTIAMzQ= ]] || err_exit 'binary files with zeros not working'
208unset var
209if	command typeset -usi var=0xfffff 2> /dev/null
210then	(( $var == 0xffff )) || err_exit 'unsigned short integers not working'
211else	err_exit 'typeset -usi cannot be used for unsigned short'
212fi
213[[ $($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:-3}' 2> /dev/null) == 3 ]]  || err_exit  '${foo:-3} not 3 when typeset -Z2 field undefined'
214[[ $($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:=3}' 2> /dev/null) == 03 ]]  || err_exit  '${foo:=-3} not 3 when typeset -Z2 foo undefined'
215unset foo bar
216unset -f fun
217function fun
218{
219	export foo=hello
220	typeset -x  bar=world
221	[[ $foo == hello ]] || err_exit 'export scoping problem in function'
222}
223fun
224[[ $(export | grep foo) == 'foo=hello' ]] || err_exit 'export not working in functions'
225[[ $(export | grep bar) ]] && err_exit 'typeset -x not local'
226[[ $($SHELL -c 'typeset -r IFS=;print -r $(pwd)' 2> /dev/null) == "$(pwd)" ]] || err_exit 'readonly IFS causes command substitution to fail'
227fred[66]=88
228[[ $(typeset -pa) == *fred* ]] || err_exit 'typeset -pa not working'
229unset x y z
230typeset -LZ3 x=abcd y z=00abcd
231y=03
232[[ $y == "3  " ]] || err_exit '-LZ3 not working for value 03'
233[[ $x == "abc" ]] || err_exit '-LZ3 not working for value abcd'
234[[ $x == "abc" ]] || err_exit '-LZ3 not working for value 00abcd'
235unset x z
236set +a
237[[ $(typeset -p z) ]] && err_exit "typeset -p for z undefined failed"
238unset z
239x='typeset -i z=45'
240eval "$x"
241[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
242[[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed"
243unset z
244x='typeset -a z=(a b c)'
245eval "$x"
246[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
247[[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed"
248unset z
249x='typeset -C z=(
250	foo=bar
251	xxx=bam
252)'
253eval "$x"
254x=${x//$'\t'}
255x=${x//$'(\n'/'('}
256x=${x//$'\n'/';'}
257[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
258[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
259unset z
260x='typeset -A z=([bar]=bam [xyz]=bar)'
261eval "$x"
262[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
263[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
264unset z
265foo=abc
266x='typeset -n z=foo'
267eval "$x"
268[[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
269[[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
270typeset +n z
271unset foo z
272typeset -T Pt_t=(
273	float x=1 y=2
274)
275Pt_t z
276x=${z//$'\t'}
277x=${x//$'(\n'/'('}
278x=${x//$'\n'/';'}
279[[ $(typeset -p z) == "Pt_t z=$x" ]] || err_exit "typeset -p for type failed"
280[[ $(typeset +p z) == "Pt_t z" ]] || err_exit "typeset +p for type failed"
281unset z
282function foo
283{
284	typeset -p bar
285}
286bar=xxx
287[[ $(foo) == bar=xxx ]] || err_exit 'typeset -p not working inside a function'
288unset foo
289typeset -L5 foo
290[[ $(typeset -p foo) == 'typeset -L 5 foo' ]] || err_exit 'typeset -p not working for variables with attributes but without a value'
291{ $SHELL  <<- EOF
292	typeset -L3 foo=aaa
293	typeset -L6 foo=bbbbbb
294	[[ \$foo == bbbbbb ]]
295EOF
296}  || err_exit 'typeset -L should not preserve old attributes'
297{ $SHELL <<- EOF
298	typeset -R3 foo=aaa
299	typeset -R6 foo=bbbbbb
300	[[ \$foo == bbbbbb ]]
301EOF
302} 2> /dev/null || err_exit 'typeset -R should not preserve old attributes'
303
304expected='YWJjZGVmZ2hpag=='
305unset foo
306typeset -b -Z10 foo
307read foo <<< 'abcdefghijklmnop'
308[[ $foo == "$expected" ]] || err_exit 'read foo, where foo is "typeset -b -Z10" not working'
309unset foo
310typeset -b -Z10 foo
311read -N10 foo <<< 'abcdefghijklmnop'
312[[ $foo == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -Z10" not working'
313unset foo
314typeset  -b -A foo
315read -N10 foo[4] <<< 'abcdefghijklmnop'
316[[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset  -b -A" foo not working'
317unset foo
318typeset  -b -a foo
319read -N10 foo[4] <<< 'abcdefghijklmnop'
320[[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset  -b -a" foo not working'
321[[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary associative array element not working'
322[[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary indexed array element not working'
323unset foo
324
325$SHELL 2> /dev/null -c 'export foo=(bar=3)' && err_exit 'compound variables cannot be exported'
326
327$SHELL -c 'builtin date' >/dev/null 2>&1 &&
328{
329
330# check env var changes against a builtin that uses the env var
331
332SEC=1234252800
333ETZ=EST5EDT
334EDT=03
335PTZ=PST8PDT
336PDT=00
337
338CMD="date -f%H \\#$SEC"
339
340export TZ=$ETZ
341
342set -- \
343	"$EDT $PDT $EDT"	""		"TZ=$PTZ"	"" \
344	"$EDT $PDT $EDT"	""		"TZ=$PTZ"	"TZ=$ETZ" \
345	"$EDT $PDT $EDT"	"TZ=$ETZ"	"TZ=$PTZ"	"TZ=$ETZ" \
346	"$PDT $EDT $PDT"	"TZ=$PTZ"	""		"TZ=$PTZ" \
347	"$PDT $EDT $PDT"	"TZ=$PTZ"	"TZ=$ETZ"	"TZ=$PTZ" \
348	"$EDT $PDT $EDT"	"foo=bar"	"TZ=$PTZ"	"TZ=$ETZ" \
349
350while	(( $# >= 4 ))
351do	exp=$1
352	got=$(print $($SHELL -c "builtin date; $2 $CMD; $3 $CMD; $4 $CMD"))
353	[[ $got == $exp ]] || err_exit "[ '$2'  '$3'  '$4' ] env sequence failed -- expected '$exp', got '$got'"
354	shift 4
355done
356
357}
358
359unset v
360typeset -H v=/dev/null
361[[ $v == *nul* ]] || err_exit 'typeset -H for /dev/null not working'
362
363exit	$((Errors))
364