xref: /illumos-gate/usr/src/test/util-tests/tests/find/findtest.ksh (revision e8d3efa1c56e5f2b5368600a2baeb7b1d54a07f8)
1#!/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2020 Joyent, Inc.
15# Copyright 2024 Bill Sommerfeld <sommerfeld@hamachi.org>
16# Copyright 2026 Oxide Computer Company
17#
18
19#
20# Clearly, grossly incomplete.
21#
22
23. "$(dirname $0)/find.kshlib"
24
25mkdir $find_dir
26mkdir -p $find_dir/1
27mkdir -p $find_dir/.2
28touch $find_dir/.2/1
29touch $find_dir/.2/c
30
31testfind "$find_dir/.2/1,$find_dir/1," \
32    $find_prog $find_dir -name \"1\"
33testfind "$find_dir/.2/1,$find_dir/1," \
34    $find_prog $find_dir -path \"*1\"
35
36cd $find_dir
37
38testfind "" $find_prog . -name \"*2\"
39testfind "./.2," $find_prog_xpg4 . -name \"*2\"
40testfind "./.2," $find_prog . -name \".*2\"
41testfind "./.2," $find_prog_xpg4 . -name \".*2\"
42testfind "./.2/1,./1," $find_prog . -path \"*1\"
43testfind "./.2," $find_prog . -path \"*2\"
44testfind "./.2,./.2/1,./.2/c," $find_prog . -path \"*2*\"
45
46cd -
47rm -rf $find_dir
48
49# Tests for the handling of a failure to run the command given to -exec, or
50# the cpio child process backing -cpio.
51
52find_dir=/var/tmp/findtest.$$.dir
53mkdir -p $find_dir
54touch $find_dir/f
55
56cmd="$find_prog $find_dir -exec /nonexistent/utility {} \;"
57echo "TEST: $cmd"
58out=$($find_prog $find_dir -exec /nonexistent/utility {} \; 2>&1 >/dev/null)
59rv=$?
60(( rv == 1 )) && [[ "$out" == *"cannot execute"* ]] || {
61	echo "TEST FAILED: $cmd" >&2
62	echo "expected exit 1 and a diagnostic, got $rv [$out]" >&2
63	find_exit=1
64}
65
66cmd="$find_prog $find_dir -exec /nonexistent/utility {} +"
67echo "TEST: $cmd"
68out=$($find_prog $find_dir -exec /nonexistent/utility {} + 2>&1 >/dev/null)
69rv=$?
70(( rv == 3 )) && [[ "$out" == *"cannot execute"* ]] || {
71	echo "TEST FAILED: $cmd" >&2
72	echo "expected exit 3 and a diagnostic, got $rv [$out]" >&2
73	find_exit=1
74}
75
76cmd="PATH=/nonexistent $find_prog $find_dir -cpio $find_dir/archive"
77echo "TEST: $cmd"
78out=$(PATH=/nonexistent $find_prog $find_dir -cpio $find_dir/archive \
79    2>&1 >/dev/null)
80rv=$?
81(( rv == 1 )) && [[ "$out" == *"cannot run cpio"* ]] || {
82	echo "TEST FAILED: $cmd" >&2
83	echo "expected exit 1 and a diagnostic, got $rv [$out]" >&2
84	find_exit=1
85}
86
87# A script without an interpreter line is executed by the shell, and is
88# invoked once regardless of the number of arguments.
89mkdir $find_dir/many
90i=1
91files=
92while ((i <= 300)); do
93	files="$files $find_dir/many/x$i"
94	((i++))
95done
96touch $files
97script=$find_dir/script
98count=$find_dir/count
99print "echo run >> $count" > $script
100chmod 0755 $script
101
102cmd="$find_prog $find_dir/many -type f -exec $script {} +"
103echo "TEST: $cmd"
104$find_prog $find_dir/many -type f -exec $script {} + >/dev/null 2>&1
105rv=$?
106runs=$(wc -l < $count)
107(( rv == 0 && runs == 1 )) || {
108	echo "TEST FAILED: $cmd" >&2
109	echo "expected exit 0 and one invocation, got $rv and [$runs]" >&2
110	find_exit=1
111}
112
113rm -rf $find_dir
114
115# Regression test for bug 15353:
116#
117# For the purposes of this test we need a user and group with the same
118# numeric id.
119#
120# We also check that /var/tmp has ZFS/CIFS/NFS4-equivalent acls.
121#
122# (A complete test would also exercise ufs's acls)
123#
124testuser=daemon
125testgroup=other
126
127testuid=$(getent passwd ${testuser} | cut -d: -f 3)
128testgid=$(getent group ${testgroup} | cut -d: -f 3)
129
130[[ "$testuid" == "$testgid" ]] || {
131	echo "TEST FAILED: $cmd" >&2
132	echo "expected ${testuser}'s uid $testuid" \
133	     "to be equal to ${testgroup}'s gid $testgid" >&2
134	find_exit=1
135}
136
137find_dir=/var/tmp/findtest.$$.dir
138mkdir -p $find_dir
139
140# ACL_ENABLED yields 0 for no acls, 1 for old acls, 2 for NFS acls.
141
142_ACL_ACE_ENABLED=2
143_ACL_ACLENT_ENABLED=1
144
145[[ $(getconf ACL_ENABLED $find_dir) == ${_ACL_ACE_ENABLED} ]] || {
146    echo "TEST SKIPPED: ACE acls not available in $find_dir"
147    find_exit=4			# UNSUPPORTED
148    exit $find_exit
149}
150
151mkdir -p $find_dir/a
152mkdir -p $find_dir/b
153chmod A+group:${testgroup}:read_set:allow $find_dir/a
154chmod A+user:${testuser}:read_set:allow $find_dir/b
155
156cd $find_dir
157testfind "./a", $find_prog . -groupacl ${testgroup}
158testfind "./b", $find_prog . -useracl ${testuser}
159
160cd -
161rm -rf $find_dir
162
163exit $find_exit
164