1319841Sngie#
2319841Sngie# Copyright (c) 2017 Dell EMC
3319841Sngie# All rights reserved.
4319841Sngie#
5319841Sngie# Redistribution and use in source and binary forms, with or without
6319841Sngie# modification, are permitted provided that the following conditions
7319841Sngie# are met:
8319841Sngie# 1. Redistributions of source code must retain the above copyright
9319841Sngie#    notice, this list of conditions and the following disclaimer.
10319841Sngie# 2. Redistributions in binary form must reproduce the above copyright
11319841Sngie#    notice, this list of conditions and the following disclaimer in the
12319841Sngie#    documentation and/or other materials provided with the distribution.
13319841Sngie#
14319841Sngie# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15319841Sngie# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16319841Sngie# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17319841Sngie# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18319841Sngie# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19319841Sngie# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20319841Sngie# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21319841Sngie# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22319841Sngie# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23319841Sngie# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24319841Sngie# SUCH DAMAGE.
25319841Sngie#
26319841Sngie# $FreeBSD: stable/10/usr.bin/stat/tests/stat_test.sh 321085 2017-07-17 21:01:07Z ngie $
27319841Sngie
28319841Sngieatf_test_case F_flag
29319841SngieF_flag_head()
30319841Sngie{
31319841Sngie	atf_set	"descr" "Verify the output format for -F"
32319841Sngie}
33319841SngieF_flag_body()
34319841Sngie{
35319841Sngie	# TODO: socket, whiteout file
36319841Sngie	atf_check touch a
37319841Sngie	atf_check mkdir b
38319841Sngie	atf_check install -m 0777 /dev/null c
39319841Sngie	atf_check ln -s a d
40319841Sngie	atf_check mkfifo f
41319841Sngie
42319841Sngie	atf_check -o match:'.* a' stat -Fn a
43319841Sngie	atf_check -o match:'.* b/' stat -Fn b
44319841Sngie	atf_check -o match:'.* c\*' stat -Fn c
45319841Sngie	atf_check -o match:'.* d@' stat -Fn d
46319841Sngie	atf_check -o match:'.* f\|' stat -Fn f
47319841Sngie}
48319841Sngie
49319841Sngieatf_test_case l_flag
50319841Sngiel_flag_head()
51319841Sngie{
52319841Sngie	atf_set	"descr" "Verify the output format for -l"
53319841Sngie}
54319841Sngiel_flag_body()
55319841Sngie{
56319841Sngie	atf_check touch a
57319841Sngie	atf_check ln a b
58319841Sngie	atf_check ln -s a c
59319841Sngie	atf_check mkdir d
60319841Sngie
61319841Sngie	paths="a b c d"
62319841Sngie
63321085Sngie	ls_out=ls.output
64321085Sngie	stat_out=stat.output
65321085Sngie
66319841Sngie	# NOTE:
67319841Sngie	# - Even though stat -l claims to be equivalent to `ls -lT`, the
68319841Sngie	#   whitespace is a bit more liberal in the `ls -lT` output.
69319841Sngie	# - `ls -ldT` is used to not recursively list the contents of
70319841Sngie	#   directories.
71319841Sngie	for path in $paths; do
72321085Sngie		atf_check -o save:$ls_out ls -ldT $path
73321085Sngie		cat $ls_out
74321085Sngie		atf_check -o save:$stat_out stat -l $path
75321085Sngie		cat $stat_out
76321085Sngie		echo "Comparing normalized whitespace"
77321085Sngie		atf_check sed -i '' -E -e 's/[[:space:]]+/ /g' $ls_out
78321085Sngie		atf_check sed -i '' -E -e 's/[[:space:]]+/ /g' $stat_out
79321085Sngie		atf_check cmp $ls_out $stat_out
80319841Sngie	done
81319841Sngie}
82319841Sngie
83319841Sngieatf_test_case n_flag
84319841Sngien_flag_head()
85319841Sngie{
86319841Sngie	atf_set	"descr" "Verify that -n suppresses newline output for lines"
87319841Sngie}
88319841Sngien_flag_body()
89319841Sngie{
90319841Sngie	atf_check touch a b
91319841Sngie	atf_check -o inline:"$(stat a | tr -d '\n')" stat -n a
92319841Sngie	atf_check -o inline:"$(stat a b | tr -d '\n')" stat -n a b
93319841Sngie}
94319841Sngie
95319841Sngieatf_test_case q_flag
96319841Sngieq_flag_head()
97319841Sngie{
98319841Sngie	atf_set	"descr" "Verify that -q suppresses error messages from l?stat(2)"
99319841Sngie}
100319841Sngieq_flag_body()
101319841Sngie{
102319841Sngie	ln -s nonexistent broken-link
103319841Sngie
104319841Sngie	atf_check -s exit:1 stat -q nonexistent
105319841Sngie	atf_check -s exit:1 stat -q nonexistent
106319841Sngie	atf_check -o not-empty stat -q broken-link
107319841Sngie	atf_check -o not-empty stat -qL broken-link
108319841Sngie}
109319841Sngie
110319841Sngieatf_test_case r_flag
111319841Sngier_flag_head()
112319841Sngie{
113319841Sngie	atf_set	"descr" "Verify that -r displays output in 'raw mode'"
114319841Sngie}
115319841Sngier_flag_body()
116319841Sngie{
117319841Sngie	atf_check touch a
118319841Sngie	# TODO: add more thorough checks.
119319841Sngie	atf_check -o not-empty stat -r a
120319841Sngie}
121319841Sngie
122319841Sngieatf_test_case s_flag
123319841Sngies_flag_head()
124319841Sngie{
125319841Sngie	atf_set	"descr" "Verify the output format for -s"
126319841Sngie}
127319841Sngies_flag_body()
128319841Sngie{
129319841Sngie	atf_check touch a
130319841Sngie	atf_check ln a b
131319841Sngie	atf_check ln -s a c
132319841Sngie	atf_check mkdir d
133319841Sngie
134319841Sngie	paths="a b c d"
135319841Sngie
136319841Sngie	# The order/name of each of the fields is specified by stat(1) manpage.
137319841Sngie	fields="st_dev st_ino st_mode st_nlink"
138319841Sngie	fields="$fields st_uid st_gid st_rdev st_size"
139319841Sngie	fields="$fields st_uid st_gid st_mode"
140319841Sngie	fields="$fields st_atime st_mtime st_ctime st_birthtime"
141319841Sngie	fields="$fields st_blksize st_blocks st_flags"
142319841Sngie
143319841Sngie	# NOTE: the following...
144319841Sngie	# - ... relies on set -eu to ensure that the fields are set, as
145319841Sngie	#       documented, in stat(1).
146319841Sngie	# - ... uses a subshell to ensure that the eval'ed variables don't
147319841Sngie	#	pollute the next iteration's behavior.
148319841Sngie	for path in $paths; do
149319841Sngie		(
150319841Sngie		set -eu
151319841Sngie		eval $(stat -s $path)
152319841Sngie		for field in $fields; do
153319841Sngie			eval "$field=\$$field"
154319841Sngie		done
155319841Sngie		) || atf_fail 'One or more fields not set by stat(1)'
156319841Sngie	done
157319841Sngie}
158319841Sngie
159319841Sngieatf_test_case t_flag
160319841Sngiet_flag_head()
161319841Sngie{
162319841Sngie	atf_set	"descr" "Verify the output format for -t"
163319841Sngie}
164319841Sngie
165319841Sngiet_flag_body()
166319841Sngie{
167319841Sngie	atf_check touch foo
168319841Sngie	atf_check touch -d 1970-01-01T00:00:42 foo
169319841Sngie	atf_check -o inline:'42\n' \
170319841Sngie	    stat -t '%s' -f '%a' foo
171319841Sngie	atf_check -o inline:'1970-01-01 00:00:42\n' \
172319841Sngie	    stat -t '%F %H:%M:%S' -f '%Sa' foo
173319841Sngie}
174319841Sngie
175319841Sngiex_output_date()
176319841Sngie{
177321085Sngie	local date_format='%a %b %e %H:%M:%S %Y'
178319841Sngie
179319841Sngie	stat -t "$date_format" "$@"
180319841Sngie}
181319841Sngie
182319841Sngiex_output()
183319841Sngie{
184319841Sngie	local path=$1; shift
185319841Sngie
186319841Sngie	local atime_s=$(x_output_date -f '%Sa' $path)
187319841Sngie	local ctime_s=$(x_output_date -f '%Sc' $path)
188319841Sngie	local devid=$(stat -f '%Hd,%Ld' $path)
189319841Sngie	local file_type_s=$(stat -f '%HT' $path)
190319841Sngie	local gid=$(stat -f '%5g' $path)
191319841Sngie	local groupname=$(stat -f '%8Sg' $path)
192319841Sngie	local inode=$(stat -f '%i' $path)
193319841Sngie	local mode=$(stat -f '%Mp%Lp' $path)
194319841Sngie	local mode_s=$(stat -f '%Sp' $path)
195319841Sngie	local mtime_s=$(x_output_date -f '%Sm' $path)
196319841Sngie	local nlink=$(stat -f '%l' $path)
197319841Sngie	local size_a=$(stat -f '%-11z' $path)
198319841Sngie	local uid=$(stat -f '%5u' $path)
199319841Sngie	local username=$(stat -f '%8Su' $path)
200319841Sngie
201319841Sngie	cat <<EOF
202319841Sngie  File: "$path"
203319841Sngie  Size: $size_a  FileType: $file_type_s
204319841Sngie  Mode: ($mode/$mode_s)         Uid: ($uid/$username)  Gid: ($gid/$groupname)
205319841SngieDevice: $devid   Inode: $inode    Links: $nlink
206319841SngieAccess: $atime_s
207319841SngieModify: $mtime_s
208319841SngieChange: $ctime_s
209319841SngieEOF
210319841Sngie}
211319841Sngie
212319841Sngieatf_test_case x_flag
213319841Sngiex_flag_head()
214319841Sngie{
215319841Sngie	atf_set	"descr" "Verify the output format for -x"
216319841Sngie}
217319841Sngiex_flag_body()
218319841Sngie{
219319841Sngie	atf_check touch a
220319841Sngie	atf_check ln a b
221319841Sngie	atf_check ln -s a c
222319841Sngie	atf_check mkdir d
223319841Sngie
224319841Sngie	paths="a b c d"
225319841Sngie
226319841Sngie	for path in $paths; do
227319841Sngie		atf_check -o "inline:$(x_output $path)\n" stat -x $path
228319841Sngie	done
229319841Sngie}
230319841Sngie
231319841Sngieatf_init_test_cases()
232319841Sngie{
233319841Sngie	atf_add_test_case F_flag
234319841Sngie	#atf_add_test_case H_flag
235319841Sngie	#atf_add_test_case L_flag
236319841Sngie	#atf_add_test_case f_flag
237319841Sngie	atf_add_test_case l_flag
238319841Sngie	atf_add_test_case n_flag
239319841Sngie	atf_add_test_case q_flag
240319841Sngie	atf_add_test_case r_flag
241319841Sngie	atf_add_test_case s_flag
242319841Sngie	atf_add_test_case t_flag
243319841Sngie	atf_add_test_case x_flag
244319841Sngie}
245