2 # Copyright (c) 2017 Dell EMC
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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
31 atf_set "descr" "Verify the output format for -F"
35 # TODO: socket, whiteout file
38 atf_check install -m 0777 /dev/null c
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
52 atf_set "descr" "Verify the output format for -l"
67 # - Even though stat -l claims to be equivalent to `ls -lT`, the
68 # whitespace is a bit more liberal in the `ls -lT` output.
69 # - `ls -ldT` is used to not recursively list the contents of
71 for path in $paths; do
72 atf_check -o save:$ls_out ls -ldT $path
74 atf_check -o save:$stat_out stat -l $path
76 echo "Comparing normalized whitespace"
77 atf_check sed -i '' -E -e 's/[[:space:]]+/ /g' $ls_out
78 atf_check sed -i '' -E -e 's/[[:space:]]+/ /g' $stat_out
79 atf_check cmp $ls_out $stat_out
86 atf_set "descr" "Verify that -n suppresses newline output for lines"
91 atf_check -o inline:"$(stat a | tr -d '\n')" stat -n a
92 atf_check -o inline:"$(stat a b | tr -d '\n')" stat -n a b
98 atf_set "descr" "Verify that -q suppresses error messages from l?stat(2)"
102 ln -s nonexistent broken-link
104 atf_check -s exit:1 stat -q nonexistent
105 atf_check -s exit:1 stat -q nonexistent
106 atf_check -o not-empty stat -q broken-link
107 atf_check -o not-empty stat -qL broken-link
113 atf_set "descr" "Verify that -r displays output in 'raw mode'"
118 # TODO: add more thorough checks.
119 atf_check -o not-empty stat -r a
125 atf_set "descr" "Verify the output format for -s"
136 # The order/name of each of the fields is specified by stat(1) manpage.
137 fields="st_dev st_ino st_mode st_nlink"
138 fields="$fields st_uid st_gid st_rdev st_size"
139 fields="$fields st_uid st_gid st_mode"
140 fields="$fields st_atime st_mtime st_ctime st_birthtime"
141 fields="$fields st_blksize st_blocks st_flags"
143 # NOTE: the following...
144 # - ... relies on set -eu to ensure that the fields are set, as
145 # documented, in stat(1).
146 # - ... uses a subshell to ensure that the eval'ed variables don't
147 # pollute the next iteration's behavior.
148 for path in $paths; do
151 eval $(stat -s $path)
152 for field in $fields; do
153 eval "$field=\$$field"
155 ) || atf_fail 'One or more fields not set by stat(1)'
162 atf_set "descr" "Verify the output format for -t"
168 atf_check touch -d 1970-01-01T00:00:42 foo
169 atf_check -o inline:'42\n' \
170 stat -t '%s' -f '%a' foo
171 atf_check -o inline:'1970-01-01 00:00:42\n' \
172 stat -t '%F %H:%M:%S' -f '%Sa' foo
177 local date_format='%a %b %e %H:%M:%S %Y'
179 stat -t "$date_format" "$@"
186 local atime_s=$(x_output_date -f '%Sa' $path)
187 local ctime_s=$(x_output_date -f '%Sc' $path)
188 local devid=$(stat -f '%Hd,%Ld' $path)
189 local file_type_s=$(stat -f '%HT' $path)
190 local gid=$(stat -f '%5g' $path)
191 local groupname=$(stat -f '%8Sg' $path)
192 local inode=$(stat -f '%i' $path)
193 local mode=$(stat -f '%Mp%Lp' $path)
194 local mode_s=$(stat -f '%Sp' $path)
195 local mtime_s=$(x_output_date -f '%Sm' $path)
196 local nlink=$(stat -f '%l' $path)
197 local size_a=$(stat -f '%-11z' $path)
198 local uid=$(stat -f '%5u' $path)
199 local username=$(stat -f '%8Su' $path)
203 Size: $size_a FileType: $file_type_s
204 Mode: ($mode/$mode_s) Uid: ($uid/$username) Gid: ($gid/$groupname)
205 Device: $devid Inode: $inode Links: $nlink
215 atf_set "descr" "Verify the output format for -x"
226 for path in $paths; do
227 atf_check -o "inline:$(x_output $path)\n" stat -x $path
231 atf_init_test_cases()
233 atf_add_test_case F_flag
234 #atf_add_test_case H_flag
235 #atf_add_test_case L_flag
236 #atf_add_test_case f_flag
237 atf_add_test_case l_flag
238 atf_add_test_case n_flag
239 atf_add_test_case q_flag
240 atf_add_test_case r_flag
241 atf_add_test_case s_flag
242 atf_add_test_case t_flag
243 atf_add_test_case x_flag