xref: /aosp_15_r20/external/toybox/tests/test.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5#testing "name" "command" "result" "infile" "stdin"
6
7testcmd "-- isn't parsed" "-- == -- && echo yes" "yes\n" "" ""
8
9# Number and position of args is important
10testcmd 'no args is false' '; echo $?'  '1\n' '' ''
11testcmd 'empty string is false' '""; echo $?' '1\n' '' ''
12testcmd '1 arg is true if not empty string' '== ; echo $?' '0\n' '' ''
13testcmd "1 arg isn't an operand" '-t 2>&1; echo $?' '0\n' '' ''
14testcmd '2 args' '-e == ; echo $?' '1\n' '' ''
15testcmd '3 args' '-e == -e ; echo $?' '0\n' '' ''
16
17# parse as operator before parsing as parentheses around one argument
18testcmd '' '\( == \) ; echo $?' '1\n' '' ''
19testcmd '' '\( == \( ; echo $?' '0\n' '' ''
20testcmd '' '\( "" \) ; echo $?' '1\n' '' ''
21testcmd '' '\( x \) ; echo $?' '0\n' '' ''
22
23# TODO: Should also have device and socket files, but requires root
24
25mkdir d
26touch f
27ln -s /dev/null L
28echo nonempty > s
29mkfifo p
30
31type_test()
32{
33  for i in d f L s p n
34  do
35    "$C" $* $i && echo -n $i
36  done
37}
38
39testing "-b" "type_test -b" "" "" ""
40testing "-c" "type_test -c" "L" "" ""
41testing "-d" "type_test -d" "d" "" ""
42testing "-e" "type_test -e" "dfLsp" "" ""
43testing "-f" "type_test -f" "fs" "" ""
44testing "-h" "type_test -h" "L" "" ""
45testing "-L" "type_test -L" "L" "" ""
46testing "-p" "type_test -p" "p" "" ""
47testing "-S" "type_test -S" "" "" ""
48testing "-s" "type_test -s" "ds" "" ""
49testing "! -e" 'type_test ! -e' "n" "" ""
50
51rm f L s p
52rmdir d
53
54# Alas can't expand to a redirect, so just test one success/fail
55testcmd "-t" '-t 0 < /dev/null; echo $?' '1\n' '' ''
56testcmd "-t2" '-t 0 < /dev/ptmx; echo $?' '0\n' '' ''
57
58# test -rwx each bit position and failure
59touch walrus
60MASK=111
61for i in x w r k g u; do
62  [ $i == k ] && MASK=1000
63  XX=no
64  [ $(id -u) -eq 0 ] && [ $i == r -o $i == w ] && XX=yes  # Root always has access
65  # test everything off produces "off"
66  chmod 000 walrus
67  testcmd "-$i 0" "-$i walrus && echo yes || echo no" "$XX\n" "" ""
68  chmod $((7777-$MASK)) walrus
69  testcmd "-$i inverted" "-$i walrus && echo yes || echo no" "$XX\n" "" ""
70  MASK=$(($MASK<<1))
71done
72unset MASK
73# Test setuid setgid sticky enabled
74for i in uu+s gg+s k+t; do
75  chmod 000 walrus
76  chmod ${i:1}+s walrus
77  testcmd "-${i:0:1}" "-${i:0:1} walrus && echo yes" "yes\n" "" ""
78done
79# test each ugo+rwx bit position individually
80XX=no
81# Note: chmod 007 means everybody EXCEPT owner/group can access it. (Unix!)
82[ $(id -u) -eq 0 ] && XX=yes  # Root always has access
83for i in 1 10 100; do for j in x w r; do
84  chmod $i walrus
85
86  [ $i == 100 ] && XX=yes
87  testcmd "-$j $i" "-$j walrus && echo yes || echo no" "$XX\n" "" ""
88  i=$((i<<1))
89done; done
90rm -f walrus
91
92# Not zero length, zero length, equals, not equals
93testcmd "-n" "-n '' || echo yes" "yes\n" "" ""
94testcmd "-n2" "-n a && echo yes" "yes\n" "" ""
95testcmd "-z" "-z '' && echo yes" "yes\n" "" ""
96testcmd "-z2" "-z a || echo yes" "yes\n" "" ""
97testcmd "" "a = b || echo yes" "yes\n" "" ""
98testcmd "" "'' = '' && echo yes" "yes\n" "" ""
99testcmd "a != b" "a != b && echo yes" "yes\n" "" ""
100testcmd "a != b" "a != a || echo yes" "yes\n" "" ""
101
102arith_test()
103{
104  $C -1 $1 1 && echo -n l
105  $C 0 $1 0 && echo -n e
106  $C -3 $1 -5 && echo -n g
107}
108
109testing "-eq" "arith_test -eq" "e" "" ""
110testing "-ne" "arith_test -ne" "lg" "" ""
111testing "-gt" "arith_test -gt" "g" "" ""
112testing "-ge" "arith_test -ge" "eg" "" ""
113testing "-lt" "arith_test -lt" "l" "" ""
114testing "-le" "arith_test -le" "le" "" ""
115
116touch oldfile -d 1970-01-01
117touch newfile -d 2031-01-01
118
119testcmd "-ef" "newfile -ef newfile && echo yes" "yes\n" "" ""
120testcmd "-ef2" "newfile -ef oldfile || echo no" "no\n" "" ""
121testcmd "-ot" "oldfile -ot newfile && echo yes" "yes\n" "" ""
122testcmd "-ot2" "oldfile -ot oldfile || echo no" "no\n" "" ""
123testcmd "-nt" "newfile -nt oldfile && echo yes" "yes\n" "" ""
124testcmd "-nt2" "oldfile -nt newfile || echo no" "no\n" "" ""
125
126testing "positional" "test -a == -a && echo yes" "yes\n" "" ""
127testing "! stacks" 'test \! \! \! \! 2 -eq 2 && echo yes' "yes\n" "" ""
128
129# bash builtin "test" has these, but /usr/bin/test does not.
130testing "<1" 'test abc \< def && echo yes' "yes\n" "" ""
131testing "<2" 'test def \< abc || echo yes' "yes\n" "" ""
132testing ">1" 'test abc \> def || echo yes' "yes\n" "" ""
133testing ">2" 'test def \> abc && echo yes' "yes\n" "" ""
134
135# toyonly doesn't work with TOYFLAG_NOHELP
136# bash only has this for [[ ]] but extra tests to _exclude_ silly...
137#toyonly testcmd "=~" 'abc =~ a.c && echo yes' "yes\n" "" ""
138#toyonly testcmd "=~ fail" 'abc =~ d.c; echo $?' '1\n' "" ""
139#toyonly testcmd "=~ zero length match" 'abc =~ "1*" && echo yes' 'yes\n' '' ''
140
141# test ! = -o a
142# test ! \( = -o a \)
143# test \( ! = \) -o a
144# test \( \)
145
146
147# -e == -a
148# -e == -a -o -d != -o
149# \( "x" \) -a \) == \)
150# \( ! ! ! -e \) \)
151
152#  // () -a (() -a () -o ()) -o ()
153#  // x -a ( x -o x ) -a x
154#  // x -o ( x -a x ) -a x -o x
155
156# trailing ! and (
157# test \( ! ! ! -e \) \)
158# test \( \( "" \) -a "" \) -a ""
159# test !
160# test \( \) == \) \) -a x
161
162# test \( "" \) -a \) == \)
163# test \( "x" \) -a \) == \)
164# test -e == -a
165# test \( "" \)
166