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