From owner-svn-src-head@freebsd.org Mon Jun 12 00:21:56 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 9493FBF0240; Mon, 12 Jun 2017 00:21:56 +0000 (UTC) (envelope-from ngie@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 6CE187B987; Mon, 12 Jun 2017 00:21:56 +0000 (UTC) (envelope-from ngie@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v5C0LtGK069189; Mon, 12 Jun 2017 00:21:55 GMT (envelope-from ngie@FreeBSD.org) Received: (from ngie@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v5C0LtsX069187; Mon, 12 Jun 2017 00:21:55 GMT (envelope-from ngie@FreeBSD.org) Message-Id: <201706120021.v5C0LtsX069187@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ngie set sender to ngie@FreeBSD.org using -f From: Ngie Cooper Date: Mon, 12 Jun 2017 00:21:55 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r319841 - head/usr.bin/stat/tests X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Jun 2017 00:21:56 -0000 Author: ngie Date: Mon Jun 12 00:21:55 2017 New Revision: 319841 URL: https://svnweb.freebsd.org/changeset/base/319841 Log: Add initial tests for stat(1) Testcases for -H, -L, and -f haven't been implemented yet, in part due to additional complexity needed to validate the features: * -H and -f will require an external "helper" program to display/modify the state/permissions for a given path. * -L is being covered partially via the -n testcase today. MFC after: 1 month Added: head/usr.bin/stat/tests/stat_test.sh (contents, props changed) Modified: head/usr.bin/stat/tests/Makefile Modified: head/usr.bin/stat/tests/Makefile ============================================================================== --- head/usr.bin/stat/tests/Makefile Sun Jun 11 22:22:17 2017 (r319840) +++ head/usr.bin/stat/tests/Makefile Mon Jun 12 00:21:55 2017 (r319841) @@ -1,5 +1,6 @@ # $FreeBSD$ ATF_TESTS_SH+= readlink_test +ATF_TESTS_SH+= stat_test .include Added: head/usr.bin/stat/tests/stat_test.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/usr.bin/stat/tests/stat_test.sh Mon Jun 12 00:21:55 2017 (r319841) @@ -0,0 +1,235 @@ +# +# Copyright (c) 2017 Dell EMC +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ + +atf_test_case F_flag +F_flag_head() +{ + atf_set "descr" "Verify the output format for -F" +} +F_flag_body() +{ + # TODO: socket, whiteout file + atf_check touch a + atf_check mkdir b + atf_check install -m 0777 /dev/null c + atf_check ln -s a d + atf_check mkfifo f + + atf_check -o match:'.* a' stat -Fn a + atf_check -o match:'.* b/' stat -Fn b + atf_check -o match:'.* c\*' stat -Fn c + atf_check -o match:'.* d@' stat -Fn d + atf_check -o match:'.* f\|' stat -Fn f +} + +atf_test_case l_flag +l_flag_head() +{ + atf_set "descr" "Verify the output format for -l" +} +l_flag_body() +{ + atf_check touch a + atf_check ln a b + atf_check ln -s a c + atf_check mkdir d + + paths="a b c d" + + # NOTE: + # - Even though stat -l claims to be equivalent to `ls -lT`, the + # whitespace is a bit more liberal in the `ls -lT` output. + # - `ls -ldT` is used to not recursively list the contents of + # directories. + for path in $paths; do + atf_check -o inline:"$(ls -ldT $path | sed -e 's, , ,g')\n" \ + stat -l $path + done +} + +atf_test_case n_flag +n_flag_head() +{ + atf_set "descr" "Verify that -n suppresses newline output for lines" +} +n_flag_body() +{ + atf_check touch a b + atf_check -o inline:"$(stat a | tr -d '\n')" stat -n a + atf_check -o inline:"$(stat a b | tr -d '\n')" stat -n a b +} + +atf_test_case q_flag +q_flag_head() +{ + atf_set "descr" "Verify that -q suppresses error messages from l?stat(2)" +} +q_flag_body() +{ + ln -s nonexistent broken-link + + atf_check -s exit:1 stat -q nonexistent + atf_check -s exit:1 stat -q nonexistent + atf_check -o not-empty stat -q broken-link + atf_check -o not-empty stat -qL broken-link +} + +atf_test_case r_flag +r_flag_head() +{ + atf_set "descr" "Verify that -r displays output in 'raw mode'" +} +r_flag_body() +{ + atf_check touch a + # TODO: add more thorough checks. + atf_check -o not-empty stat -r a +} + +atf_test_case s_flag +s_flag_head() +{ + atf_set "descr" "Verify the output format for -s" +} +s_flag_body() +{ + atf_check touch a + atf_check ln a b + atf_check ln -s a c + atf_check mkdir d + + paths="a b c d" + + # The order/name of each of the fields is specified by stat(1) manpage. + fields="st_dev st_ino st_mode st_nlink" + fields="$fields st_uid st_gid st_rdev st_size" + fields="$fields st_uid st_gid st_mode" + fields="$fields st_atime st_mtime st_ctime st_birthtime" + fields="$fields st_blksize st_blocks st_flags" + + # NOTE: the following... + # - ... relies on set -eu to ensure that the fields are set, as + # documented, in stat(1). + # - ... uses a subshell to ensure that the eval'ed variables don't + # pollute the next iteration's behavior. + for path in $paths; do + ( + set -eu + eval $(stat -s $path) + for field in $fields; do + eval "$field=\$$field" + done + ) || atf_fail 'One or more fields not set by stat(1)' + done +} + +atf_test_case t_flag +t_flag_head() +{ + atf_set "descr" "Verify the output format for -t" +} + +t_flag_body() +{ + atf_check touch foo + atf_check touch -d 1970-01-01T00:00:42 foo + atf_check -o inline:'42\n' \ + stat -t '%s' -f '%a' foo + atf_check -o inline:'1970-01-01 00:00:42\n' \ + stat -t '%F %H:%M:%S' -f '%Sa' foo +} + +x_output_date() +{ + local date_format='%a %b %d %H:%M:%S %Y' + + stat -t "$date_format" "$@" +} + +x_output() +{ + local path=$1; shift + + local atime_s=$(x_output_date -f '%Sa' $path) + local ctime_s=$(x_output_date -f '%Sc' $path) + local devid=$(stat -f '%Hd,%Ld' $path) + local file_type_s=$(stat -f '%HT' $path) + local gid=$(stat -f '%5g' $path) + local groupname=$(stat -f '%8Sg' $path) + local inode=$(stat -f '%i' $path) + local mode=$(stat -f '%Mp%Lp' $path) + local mode_s=$(stat -f '%Sp' $path) + local mtime_s=$(x_output_date -f '%Sm' $path) + local nlink=$(stat -f '%l' $path) + local size_a=$(stat -f '%-11z' $path) + local uid=$(stat -f '%5u' $path) + local username=$(stat -f '%8Su' $path) + + cat <