xref: /aosp_15_r20/external/toybox/tests/find.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1*cf5a6c84SAndroid Build Coastguard Worker#!/bin/bash
2*cf5a6c84SAndroid Build Coastguard Worker
3*cf5a6c84SAndroid Build Coastguard Worker[ -f testing.sh ] && . testing.sh
4*cf5a6c84SAndroid Build Coastguard Worker
5*cf5a6c84SAndroid Build Coastguard Workermkdir dir
6*cf5a6c84SAndroid Build Coastguard Workercd dir
7*cf5a6c84SAndroid Build Coastguard Workertouch file
8*cf5a6c84SAndroid Build Coastguard Workermkfifo fifo
9*cf5a6c84SAndroid Build Coastguard Worker# fs timestamp granularity isn't always enough for -newer to tell, so wait
10*cf5a6c84SAndroid Build Coastguard Workersleep .1
11*cf5a6c84SAndroid Build Coastguard Workerln -s fifo link
12*cf5a6c84SAndroid Build Coastguard Workercd ..
13*cf5a6c84SAndroid Build Coastguard Workertouch irrelevant
14*cf5a6c84SAndroid Build Coastguard Worker
15*cf5a6c84SAndroid Build Coastguard Workermkdir perm
16*cf5a6c84SAndroid Build Coastguard Workertouch perm/all-read-only
17*cf5a6c84SAndroid Build Coastguard Workerchmod a=r perm/all-read-only
18*cf5a6c84SAndroid Build Coastguard Worker
19*cf5a6c84SAndroid Build Coastguard Worker#testing "name" "command" "result" "infile" "stdin"
20*cf5a6c84SAndroid Build Coastguard Worker
21*cf5a6c84SAndroid Build Coastguard Worker# Testing operators
22*cf5a6c84SAndroid Build Coastguard Worker
23*cf5a6c84SAndroid Build Coastguard Workertesting "-type l -a -type d -o -type p" \
24*cf5a6c84SAndroid Build Coastguard Worker	"find dir -type l -a -type d -o -type p" "dir/fifo\n" "" ""
25*cf5a6c84SAndroid Build Coastguard Workertesting "-type l -type d -o -type p" "find dir -type l -type d -o -type p" \
26*cf5a6c84SAndroid Build Coastguard Worker	"dir/fifo\n" "" ""
27*cf5a6c84SAndroid Build Coastguard Workertesting "-type l -o -type d -a -type p" \
28*cf5a6c84SAndroid Build Coastguard Worker	"find dir -type l -o -type d -a -type p" "dir/link\n" "" ""
29*cf5a6c84SAndroid Build Coastguard Workertesting "-type l -o -type d -type p" "find dir -type l -o -type d -type p" \
30*cf5a6c84SAndroid Build Coastguard Worker	"dir/link\n" "" ""
31*cf5a6c84SAndroid Build Coastguard Workertesting "-type l ( -type d -o -type l )" \
32*cf5a6c84SAndroid Build Coastguard Worker	"find dir -type l \( -type d -o -type l \)" "dir/link\n" "" ""
33*cf5a6c84SAndroid Build Coastguard Workertesting "extra parentheses" \
34*cf5a6c84SAndroid Build Coastguard Worker	"find dir \( \( -type l \) \( -type d -o \( \( -type l \) \) \) \)" \
35*cf5a6c84SAndroid Build Coastguard Worker	"dir/link\n" "" ""
36*cf5a6c84SAndroid Build Coastguard Workertesting "( -type p -o -type d ) -type p" \
37*cf5a6c84SAndroid Build Coastguard Worker	"find dir \( -type p -o -type d \) -type p" "dir/fifo\n" "" ""
38*cf5a6c84SAndroid Build Coastguard Workertesting "-type l -o -type d -type p -o -type f" \
39*cf5a6c84SAndroid Build Coastguard Worker	"find dir -type l -o -type d -type p -o -type f | sort" \
40*cf5a6c84SAndroid Build Coastguard Worker	"dir/file\ndir/link\n" "" ""
41*cf5a6c84SAndroid Build Coastguard Workertesting "-type l,f" \
42*cf5a6c84SAndroid Build Coastguard Worker	"find dir -type l,f | sort" "dir/file\ndir/link\n" "" ""
43*cf5a6c84SAndroid Build Coastguard Worker
44*cf5a6c84SAndroid Build Coastguard Worker# Testing short-circuit evaluations
45*cf5a6c84SAndroid Build Coastguard Worker
46*cf5a6c84SAndroid Build Coastguard Workertesting "-type f -a -print" \
47*cf5a6c84SAndroid Build Coastguard Worker	"find dir -type f -a -print" "dir/file\n" "" ""
48*cf5a6c84SAndroid Build Coastguard Workertesting "-print -o -print" \
49*cf5a6c84SAndroid Build Coastguard Worker	"find dir -type f -a \( -print -o -print \)" "dir/file\n" "" ""
50*cf5a6c84SAndroid Build Coastguard Worker
51*cf5a6c84SAndroid Build Coastguard Worker# these were erroring or segfaulting:
52*cf5a6c84SAndroid Build Coastguard Worker# find -type f -user nobody -exec : \;
53*cf5a6c84SAndroid Build Coastguard Worker# find -type f -user nobody -exec : -exec : \;
54*cf5a6c84SAndroid Build Coastguard Worker
55*cf5a6c84SAndroid Build Coastguard Worker# Testing previous failures
56*cf5a6c84SAndroid Build Coastguard Worker
57*cf5a6c84SAndroid Build Coastguard Workertesting " " "cd perm; find" ".\n./all-read-only\n" "" ""
58*cf5a6c84SAndroid Build Coastguard Workertesting "-type f -user -exec" \
59*cf5a6c84SAndroid Build Coastguard Worker  "find dir -type f -user $USER -exec ls {} \\;" "dir/file\n" "" ""
60*cf5a6c84SAndroid Build Coastguard Workertesting "-type l -newer -exec" \
61*cf5a6c84SAndroid Build Coastguard Worker  "find dir -type l -newer dir/file -exec ls {} \\;" "dir/link\n" "" ""
62*cf5a6c84SAndroid Build Coastguard Workertesting "-exec true \\; -print" \
63*cf5a6c84SAndroid Build Coastguard Worker  "find dir/file -exec true \\; -print" "dir/file\n" "" ""
64*cf5a6c84SAndroid Build Coastguard Workertesting "-exec false \\; -print" \
65*cf5a6c84SAndroid Build Coastguard Worker  "find dir/file -exec false \\; -print" "" "" ""
66*cf5a6c84SAndroid Build Coastguard Workertesting "-perm (exact success)" \
67*cf5a6c84SAndroid Build Coastguard Worker  "find perm -type f -perm 0444" "perm/all-read-only\n" "" ""
68*cf5a6c84SAndroid Build Coastguard Workertesting "-perm (exact failure)" \
69*cf5a6c84SAndroid Build Coastguard Worker  "find perm -type f -perm 0400" "" "" ""
70*cf5a6c84SAndroid Build Coastguard Workertesting "-perm (min success)" \
71*cf5a6c84SAndroid Build Coastguard Worker  "find perm -type f -perm -0400" "perm/all-read-only\n" "" ""
72*cf5a6c84SAndroid Build Coastguard Workertesting "-perm (min failure)" \
73*cf5a6c84SAndroid Build Coastguard Worker  "find perm -type f -perm -0600" "" "" ""
74*cf5a6c84SAndroid Build Coastguard Workertesting "-perm (any success)" \
75*cf5a6c84SAndroid Build Coastguard Worker  "find perm -type f -perm -0444" "perm/all-read-only\n" "" ""
76*cf5a6c84SAndroid Build Coastguard Workertesting "-perm (any failure)" \
77*cf5a6c84SAndroid Build Coastguard Worker  "find perm -type f -perm -0222" "" "" ""
78*cf5a6c84SAndroid Build Coastguard Worker
79*cf5a6c84SAndroid Build Coastguard Workertesting "unterminated -exec {}" \
80*cf5a6c84SAndroid Build Coastguard Worker  "find dir -type f -exec ls {} 2>/dev/null || echo bad" "bad\n" "" ""
81*cf5a6c84SAndroid Build Coastguard Workertesting "-exec {} +" \
82*cf5a6c84SAndroid Build Coastguard Worker  "find dir -type f -exec ls {} +" "dir/file\n" "" ""
83*cf5a6c84SAndroid Build Coastguard Worker
84*cf5a6c84SAndroid Build Coastguard Worker# `find . -iname` was segfaulting
85*cf5a6c84SAndroid Build Coastguard Workertesting "-name file" "find dir -name file" "dir/file\n" "" ""
86*cf5a6c84SAndroid Build Coastguard Workertesting "-name FILE" "find dir -name FILE" "" "" ""
87*cf5a6c84SAndroid Build Coastguard Worker
88*cf5a6c84SAndroid Build Coastguard Workerln -s ../broken dir/link2
89*cf5a6c84SAndroid Build Coastguard Workertesting "-iname file" "find dir -iname FILE" "dir/file\n" "" ""
90*cf5a6c84SAndroid Build Coastguard Workertesting "-iname FILE" "find dir -iname FILE" "dir/file\n" "" ""
91*cf5a6c84SAndroid Build Coastguard Worker
92*cf5a6c84SAndroid Build Coastguard Workertesting "-name (no arguments)" \
93*cf5a6c84SAndroid Build Coastguard Worker  "find dir -name 2>&1 | grep -o '[-]name'" "-name\n" "" ""
94*cf5a6c84SAndroid Build Coastguard Workertesting "-iname (no arguments)" \
95*cf5a6c84SAndroid Build Coastguard Worker  "find dir -iname 2>&1 | grep -o '[-]iname'" "-iname\n" "" ""
96*cf5a6c84SAndroid Build Coastguard Workertesting "-lname" "find dir -lname '?./brok*'" "dir/link2\n" "" ""
97*cf5a6c84SAndroid Build Coastguard Workertesting "-ilname" "find dir -ilname '*ROK*'" "dir/link2\n" "" ""
98*cf5a6c84SAndroid Build Coastguard Worker
99*cf5a6c84SAndroid Build Coastguard Workertesting "" "find dir \( -iname file -o -iname missing \) -exec echo {} \;" \
100*cf5a6c84SAndroid Build Coastguard Worker  "dir/file\n" "" ""
101*cf5a6c84SAndroid Build Coastguard Worker
102*cf5a6c84SAndroid Build Coastguard Workertesting "-path glob" "find dir -path 'dir*e'" "dir/file\n" "" ""
103*cf5a6c84SAndroid Build Coastguard Workertesting "-wholename glob" "find dir -wholename 'dir*e'" "dir/file\n" "" ""
104*cf5a6c84SAndroid Build Coastguard Workertesting "-ipath glob" "find dir -ipath 'dIr*E'" "dir/file\n" "" ""
105*cf5a6c84SAndroid Build Coastguard Workertesting "-iwholename glob" "find dir -iwholename 'dIr*E'" "dir/file\n" "" ""
106*cf5a6c84SAndroid Build Coastguard Workertesting "-printf" "find dir -name file -printf '%f %p %P %s'" \
107*cf5a6c84SAndroid Build Coastguard Worker  "file dir/file file 0" "" ""
108*cf5a6c84SAndroid Build Coastguard Workertesting "-printf .N" "find dir -name file -printf %.2f" "fi" "" ""
109*cf5a6c84SAndroid Build Coastguard Worker# findutils find supports C letter escapes and \0 octal, but not \x or \u.
110*cf5a6c84SAndroid Build Coastguard Workertesting "-printf escapes" \
111*cf5a6c84SAndroid Build Coastguard Worker  "find dir -name file -printf '\0 \007 \t \079' | xxd -p" \
112*cf5a6c84SAndroid Build Coastguard Worker  "0020072009200739\n" "" ""
113*cf5a6c84SAndroid Build Coastguard Worker# findutils find treats \c as "no more output from this -printf", not "no more
114*cf5a6c84SAndroid Build Coastguard Worker# output from find".
115*cf5a6c84SAndroid Build Coastguard Workertesting "-printf \\c escape" "find dir -name f* -printf 'x\cy'" "xx" "" ""
116*cf5a6c84SAndroid Build Coastguard Worker
117*cf5a6c84SAndroid Build Coastguard Worker# No error message for a dangling link.
118*cf5a6c84SAndroid Build Coastguard Workerln -s does-not-exist dir/dangler
119*cf5a6c84SAndroid Build Coastguard Workertesting "-L dangling symlink silent" \
120*cf5a6c84SAndroid Build Coastguard Worker  "LANG=C find -L dir -name file 2>&1" "dir/file\n" "" ""
121*cf5a6c84SAndroid Build Coastguard Workerrm -f dir/dangler
122*cf5a6c84SAndroid Build Coastguard Worker
123*cf5a6c84SAndroid Build Coastguard Worker# An error for a symlink loop.
124*cf5a6c84SAndroid Build Coastguard Workerln -s looper dir/looper
125*cf5a6c84SAndroid Build Coastguard Workertesting "-L symlink loop noisy" \
126*cf5a6c84SAndroid Build Coastguard Worker  "LANG=C find -L dir -name file 2>err ; grep -q dir/looper err || echo missing error" \
127*cf5a6c84SAndroid Build Coastguard Worker  "dir/file\n" "" ""
128*cf5a6c84SAndroid Build Coastguard Worker
129*cf5a6c84SAndroid Build Coastguard Workertesting "-false" "find dir -false" "" "" ""
130*cf5a6c84SAndroid Build Coastguard Workertesting "-true" "find dir/file -true" "dir/file\n" "" ""
131*cf5a6c84SAndroid Build Coastguard Worker
132*cf5a6c84SAndroid Build Coastguard Workertesting "missing root error" \
133*cf5a6c84SAndroid Build Coastguard Worker  "LANG=C find -L dir/missing-root 2>err ; grep -q dir/missing-root err || echo missing error" \
134*cf5a6c84SAndroid Build Coastguard Worker  "" "" ""
135*cf5a6c84SAndroid Build Coastguard Workerrm -f dir/looper err
136*cf5a6c84SAndroid Build Coastguard Worker
137*cf5a6c84SAndroid Build Coastguard Workertesting "-path match root" "find dir/f* -path dir/file" "dir/file\n" "" ""
138*cf5a6c84SAndroid Build Coastguard Workertesting "-name match root" "find dir/f* -name file" "dir/file\n" "" ""
139*cf5a6c84SAndroid Build Coastguard Worker
140*cf5a6c84SAndroid Build Coastguard Worker# https://github.com/landley/toybox/issues/69
141*cf5a6c84SAndroid Build Coastguard Workerln -s nowhere broken
142*cf5a6c84SAndroid Build Coastguard Workertesting "-H broken" "find -H broken" "broken\n" "" ""
143*cf5a6c84SAndroid Build Coastguard Workertesting "-L broken" "find -L broken" "broken\n" "" ""
144*cf5a6c84SAndroid Build Coastguard Worker
145*cf5a6c84SAndroid Build Coastguard Workertesting "one slash" 'find /etc/ -maxdepth 1 | grep /passwd\$' '/etc/passwd\n' \
146*cf5a6c84SAndroid Build Coastguard Worker  '' ''
147*cf5a6c84SAndroid Build Coastguard Workertesting 'empty arg' 'find "" dir -name file 2>/dev/null' 'dir/file\n' '' ''
148*cf5a6c84SAndroid Build Coastguard Workertesting 'quit' 'find dir perm -print -quit' 'dir\n' '' ''
149*cf5a6c84SAndroid Build Coastguard Workerln dir/file perm/hardlink
150*cf5a6c84SAndroid Build Coastguard Workertesting 'samefile' 'find . -samefile dir/file | sort' \
151*cf5a6c84SAndroid Build Coastguard Worker  './dir/file\n./perm/hardlink\n' '' ''
152*cf5a6c84SAndroid Build Coastguard Workerrm -rf dir broken perm irrelevant
153*cf5a6c84SAndroid Build Coastguard Worker
154*cf5a6c84SAndroid Build Coastguard Workermkdir dir
155*cf5a6c84SAndroid Build Coastguard Workertouch -d @12345 dir/one
156*cf5a6c84SAndroid Build Coastguard Workertouch -d @12346 dir/two
157*cf5a6c84SAndroid Build Coastguard Workertesting 'newerat' 'find dir -type f -newerat @12345' 'dir/two\n' '' ''
158*cf5a6c84SAndroid Build Coastguard Workertesting 'newer nano' 'find dir -type f -newerat @12345.67890' 'dir/two\n' '' ''
159*cf5a6c84SAndroid Build Coastguard Workerln -s one dir/three
160*cf5a6c84SAndroid Build Coastguard Workertesting '-size implies -type f' 'find dir -size -1M | sort' \
161*cf5a6c84SAndroid Build Coastguard Worker  'dir/one\ndir/two\n' '' ''
162*cf5a6c84SAndroid Build Coastguard Workerrm -rf dir
163*cf5a6c84SAndroid Build Coastguard Worker
164*cf5a6c84SAndroid Build Coastguard Workerutf8locale
165*cf5a6c84SAndroid Build Coastguard Workerskipnot [ "$(uname)" != "Darwin" ]  # Darwin's towlower() is broken.
166*cf5a6c84SAndroid Build Coastguard Workertesting 'strlower edge case' \
167*cf5a6c84SAndroid Build Coastguard Worker  'touch aaaaaⱥⱥⱥⱥⱥⱥⱥⱥⱥ; find . -iname aaaaaȺȺȺȺȺȺȺȺȺ' './aaaaaⱥⱥⱥⱥⱥⱥⱥⱥⱥ\n' \
168*cf5a6c84SAndroid Build Coastguard Worker  '' ''
169