xref: /freebsd/usr.bin/stat/tests/stat_test.sh (revision f56f82e0)
1#
2# Copyright (c) 2017 Dell EMC
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27
28atf_test_case F_flag
29F_flag_head()
30{
31	atf_set	"descr" "Verify the output format for -F"
32}
33F_flag_body()
34{
35	# TODO: socket, whiteout file
36	atf_check touch a
37	atf_check mkdir b
38	atf_check install -m 0777 /dev/null c
39	atf_check ln -s a d
40	atf_check mkfifo f
41
42	atf_check -o match:'.* a' stat -Fn a
43	atf_check -o match:'.* b/' stat -Fn b
44	atf_check -o match:'.* c\*' stat -Fn c
45	atf_check -o match:'.* d@' stat -Fn d
46	atf_check -o match:'.* f\|' stat -Fn f
47}
48
49atf_test_case l_flag
50l_flag_head()
51{
52	atf_set	"descr" "Verify the output format for -l"
53}
54l_flag_body()
55{
56	atf_check touch a
57	atf_check ln a b
58	atf_check ln -s a c
59	atf_check mkdir d
60
61	paths="a b c d"
62
63	# NOTE:
64	# - Even though stat -l claims to be equivalent to `ls -lT`, the
65	#   whitespace is a bit more liberal in the `ls -lT` output.
66	# - `ls -ldT` is used to not recursively list the contents of
67	#   directories.
68	for path in $paths; do
69		atf_check -o inline:"$(ls -ldT $path | sed -e 's,  , ,g')\n" \
70		    stat -l $path
71	done
72}
73
74atf_test_case n_flag
75n_flag_head()
76{
77	atf_set	"descr" "Verify that -n suppresses newline output for lines"
78}
79n_flag_body()
80{
81	atf_check touch a b
82	atf_check -o inline:"$(stat a | tr -d '\n')" stat -n a
83	atf_check -o inline:"$(stat a b | tr -d '\n')" stat -n a b
84}
85
86atf_test_case q_flag
87q_flag_head()
88{
89	atf_set	"descr" "Verify that -q suppresses error messages from l?stat(2)"
90}
91q_flag_body()
92{
93	ln -s nonexistent broken-link
94
95	atf_check -s exit:1 stat -q nonexistent
96	atf_check -s exit:1 stat -q nonexistent
97	atf_check -o not-empty stat -q broken-link
98	atf_check -o not-empty stat -qL broken-link
99}
100
101atf_test_case r_flag
102r_flag_head()
103{
104	atf_set	"descr" "Verify that -r displays output in 'raw mode'"
105}
106r_flag_body()
107{
108	atf_check touch a
109	# TODO: add more thorough checks.
110	atf_check -o not-empty stat -r a
111}
112
113atf_test_case s_flag
114s_flag_head()
115{
116	atf_set	"descr" "Verify the output format for -s"
117}
118s_flag_body()
119{
120	atf_check touch a
121	atf_check ln a b
122	atf_check ln -s a c
123	atf_check mkdir d
124
125	paths="a b c d"
126
127	# The order/name of each of the fields is specified by stat(1) manpage.
128	fields="st_dev st_ino st_mode st_nlink"
129	fields="$fields st_uid st_gid st_rdev st_size"
130	fields="$fields st_uid st_gid st_mode"
131	fields="$fields st_atime st_mtime st_ctime st_birthtime"
132	fields="$fields st_blksize st_blocks st_flags"
133
134	# NOTE: the following...
135	# - ... relies on set -eu to ensure that the fields are set, as
136	#       documented, in stat(1).
137	# - ... uses a subshell to ensure that the eval'ed variables don't
138	#	pollute the next iteration's behavior.
139	for path in $paths; do
140		(
141		set -eu
142		eval $(stat -s $path)
143		for field in $fields; do
144			eval "$field=\$$field"
145		done
146		) || atf_fail 'One or more fields not set by stat(1)'
147	done
148}
149
150atf_test_case t_flag
151t_flag_head()
152{
153	atf_set	"descr" "Verify the output format for -t"
154}
155
156t_flag_body()
157{
158	atf_check touch foo
159	atf_check touch -d 1970-01-01T00:00:42 foo
160	atf_check -o inline:'42\n' \
161	    stat -t '%s' -f '%a' foo
162	atf_check -o inline:'1970-01-01 00:00:42\n' \
163	    stat -t '%F %H:%M:%S' -f '%Sa' foo
164}
165
166x_output_date()
167{
168	local date_format='%a %b %d %H:%M:%S %Y'
169
170	stat -t "$date_format" "$@"
171}
172
173x_output()
174{
175	local path=$1; shift
176
177	local atime_s=$(x_output_date -f '%Sa' $path)
178	local ctime_s=$(x_output_date -f '%Sc' $path)
179	local devid=$(stat -f '%Hd,%Ld' $path)
180	local file_type_s=$(stat -f '%HT' $path)
181	local gid=$(stat -f '%5g' $path)
182	local groupname=$(stat -f '%8Sg' $path)
183	local inode=$(stat -f '%i' $path)
184	local mode=$(stat -f '%Mp%Lp' $path)
185	local mode_s=$(stat -f '%Sp' $path)
186	local mtime_s=$(x_output_date -f '%Sm' $path)
187	local nlink=$(stat -f '%l' $path)
188	local size_a=$(stat -f '%-11z' $path)
189	local uid=$(stat -f '%5u' $path)
190	local username=$(stat -f '%8Su' $path)
191
192	cat <<EOF
193  File: "$path"
194  Size: $size_a  FileType: $file_type_s
195  Mode: ($mode/$mode_s)         Uid: ($uid/$username)  Gid: ($gid/$groupname)
196Device: $devid   Inode: $inode    Links: $nlink
197Access: $atime_s
198Modify: $mtime_s
199Change: $ctime_s
200EOF
201}
202
203atf_test_case x_flag
204x_flag_head()
205{
206	atf_set	"descr" "Verify the output format for -x"
207}
208x_flag_body()
209{
210	atf_check touch a
211	atf_check ln a b
212	atf_check ln -s a c
213	atf_check mkdir d
214
215	paths="a b c d"
216
217	for path in $paths; do
218		atf_check -o "inline:$(x_output $path)\n" stat -x $path
219	done
220}
221
222atf_init_test_cases()
223{
224	atf_add_test_case F_flag
225	#atf_add_test_case H_flag
226	#atf_add_test_case L_flag
227	#atf_add_test_case f_flag
228	atf_add_test_case l_flag
229	atf_add_test_case n_flag
230	atf_add_test_case q_flag
231	atf_add_test_case r_flag
232	atf_add_test_case s_flag
233	atf_add_test_case t_flag
234	atf_add_test_case x_flag
235}
236