xref: /freebsd/usr.bin/find/tests/find_test.sh (revision 02f394281fd67dc15afb26c51d52f2b4cbf28065)
1#-
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright 2017, Conrad Meyer <cem@FreeBSD.org>.
5# Copyright (c) 2025 Dag-Erling Smørgrav <des@FreeBSD.org>
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
10#
11# * Redistributions of source code must retain the above copyright
12#   notice, this list of conditions and the following disclaimer.
13# * Redistributions in binary form must reproduce the above copyright
14#   notice, this list of conditions and the following disclaimer in the
15#   documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29#
30
31atf_test_case find_newer_link
32find_newer_link_head()
33{
34	atf_set "descr" "Verifies that -newer correctly uses a symlink, " \
35	    "rather than its target, for comparison"
36}
37find_newer_link_body()
38{
39	atf_check -s exit:0 mkdir test
40	atf_check -s exit:0 ln -s file1 test/link
41	atf_check -s exit:0 touch -d 2017-12-31T10:00:00Z -h test/link
42	atf_check -s exit:0 touch -d 2017-12-31T11:00:00Z test/file2
43	atf_check -s exit:0 touch -d 2017-12-31T12:00:00Z test/file1
44
45	# find(1) should evaluate 'link' as a symlink rather than its target
46	# (with -P / without -L flags).  Since link was created first, the
47	# other two files should be newer.
48	echo -e "test\ntest/file1\ntest/file2" > expout
49	atf_check -s exit:0 -o save:output find test -newer test/link
50	atf_check -s exit:0 -o file:expout sort < output
51}
52
53atf_test_case find_samefile_link
54find_samefile_link_head()
55{
56	atf_set "descr" "Verifies that -samefile correctly uses a symlink, " \
57	    "rather than its target, for comparison"
58}
59find_samefile_link_body()
60{
61	atf_check -s exit:0 mkdir test
62	atf_check -s exit:0 touch test/file3
63	atf_check -s exit:0 ln -s file3 test/link2
64
65	# find(1) should evaluate 'link' as a symlink rather than its target
66	# (with -P / without -L flags).
67	atf_check -s exit:0 -o "inline:test/link2\n" find test -samefile test/link2
68}
69
70atf_test_case find_printf
71find_printf_head()
72{
73	atf_set "descr" "Test the -printf primary"
74}
75find_printf_body()
76{
77	mkdir dir
78	chmod 0755 dir
79	jot -b hello 1024 >dir/file
80	chmod 0644 dir/file
81	ln -s file dir/link
82	chmod -h 0444 dir/link
83	local db=$(stat -f %b dir)
84	local fb=$(stat -f %b dir/file)
85	local lb=$(stat -f %b dir/link)
86
87	# paths
88	atf_check -o inline:"dir\ndir/file\ndir/link\n" \
89	    find -s dir -printf '%p\n'
90	atf_check -o inline:"dir\nfile\nlink\n" \
91	    find -s dir -printf '%f\n'
92	atf_check -o inline:".\ndir\ndir\n" \
93	    find -s dir -printf '%h\n'
94	atf_check -s exit:1 -e match:"unimplemented" -o ignore \
95	    find -s dir -printf '%P\n'
96	atf_check -s exit:1 -e match:"unimplemented" -o ignore \
97	    find -s dir -printf '%H\n'
98
99	# group
100	atf_check -o inline:"$(stat -f %Sg dir dir/file dir/link)\n" \
101	    find -s dir -printf '%g\n'
102	atf_check -o inline:"$(stat -f %g dir dir/file dir/link)\n" \
103	    find -s dir -printf '%G\n'
104
105	# owner
106	atf_check -o inline:"$(stat -f %Su dir dir/file dir/link)\n" \
107	    find -s dir -printf '%u\n'
108	atf_check -o inline:"$(stat -f %u dir dir/file dir/link)\n" \
109	    find -s dir -printf '%U\n'
110
111	# mode
112	atf_check -o inline:"$(stat -f %Lp dir dir/file dir/link)\n" \
113	    find -s dir -printf '%m\n'
114	atf_check -o inline:"$(stat -f %Sp dir dir/file dir/link)\n" \
115	    find -s dir -printf '%M\n'
116
117	# size
118	atf_check -o inline:"$((db/2))\n$((fb/2))\n$((lb/2))\n" \
119	    find -s dir -printf '%k\n'
120	atf_check -o inline:"$db\n$fb\n$lb\n" \
121	    find -s dir -printf '%b\n'
122	atf_check -o inline:"$(stat -f %z dir dir/file dir/link)\n" \
123	    find -s dir -printf '%s\n'
124	# XXX test %S properly
125	atf_check -o ignore \
126	    find -s dir -printf '%S\n'
127	atf_check -o inline:"0\n1\n1\n" \
128	    find -s dir -printf '%d\n'
129
130	# device
131	atf_check -o inline:"$(stat -f %d dir dir/file dir/link)\n" \
132	    find -s dir -printf '%D\n'
133	atf_check -s exit:1 -e match:"unimplemented" -o ignore \
134	    find -s dir -printf '%F\n'
135
136	# link target
137	atf_check -o inline:"\n\nfile\n" \
138	    find -s dir -printf '%l\n'
139
140	# inode
141	atf_check -o inline:"$(stat -f %i dir dir/file dir/link)\n" \
142	    find -s dir -printf '%i\n'
143
144	# nlinks
145	atf_check -o inline:"$(stat -f %l dir dir/file dir/link)\n" \
146	    find -s dir -printf '%n\n'
147
148	# type
149	atf_check -o inline:"d\nf\nl\n" \
150	    find -s dir -printf '%y\n'
151	atf_check -o inline:"d\nf\nf\n" \
152	    find -s dir -printf '%Y\n'
153
154	# access time
155	atf_check -o inline:"$(stat -f %Sa -t '%a %b %e %T %Y' dir dir/file dir/link)\n" \
156	    find -s dir -printf '%a\n'
157	atf_check -o inline:"$(stat -f %Sa -t '%e' dir dir/file dir/link)\n" \
158	    find -s dir -printf '%Ae\n'
159
160	# birth time
161	atf_check -o inline:"$(stat -f %SB -t '%e' dir dir/file dir/link)\n" \
162	    find -s dir -printf '%Be\n'
163
164	# inode change time
165	atf_check -o inline:"$(stat -f %Sc -t '%a %b %e %T %Y' dir dir/file dir/link)\n" \
166	    find -s dir -printf '%c\n'
167	atf_check -o inline:"$(stat -f %Sc -t '%e' dir dir/file dir/link)\n" \
168	    find -s dir -printf '%Ce\n'
169
170	# modification time
171	atf_check -o inline:"$(stat -f %Sm -t '%a %b %e %T %Y' dir dir/file dir/link)\n" \
172	    find -s dir -printf '%t\n'
173	atf_check -o inline:"$(stat -f %Sm -t '%e' dir dir/file dir/link)\n" \
174	    find -s dir -printf '%Te\n'
175}
176
177atf_init_test_cases()
178{
179	atf_add_test_case find_newer_link
180	atf_add_test_case find_samefile_link
181	atf_add_test_case find_printf
182}
183