summaryrefslogtreecommitdiffstats
path: root/tests/stat
diff options
context:
space:
mode:
Diffstat (limited to 'tests/stat')
-rwxr-xr-xtests/stat/stat-birthtime.sh51
-rwxr-xr-xtests/stat/stat-fmt.sh47
-rwxr-xr-xtests/stat/stat-hyphen.sh30
-rwxr-xr-xtests/stat/stat-mount.sh28
-rwxr-xr-xtests/stat/stat-nanoseconds.sh46
-rwxr-xr-xtests/stat/stat-printf.pl64
-rwxr-xr-xtests/stat/stat-slash.sh44
7 files changed, 310 insertions, 0 deletions
diff --git a/tests/stat/stat-birthtime.sh b/tests/stat/stat-birthtime.sh
new file mode 100755
index 0000000..8d2e661
--- /dev/null
+++ b/tests/stat/stat-birthtime.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+# ensure that stat attempts birthtime access
+
+# Copyright (C) 2010-2023 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ stat
+
+# Whether birthtime is supported or not, it better not change even when
+# [acm]time are modified. :)
+touch a || fail=1
+btime=$(stat --format %W a) || fail=1
+atime=$(stat --format %X a) || fail=1
+mtime=$(stat --format %Y a) || fail=1
+ctime=$(stat --format %Z a) || fail=1
+
+# Wait up to 2.17s for timestamps to change.
+# ----------------------------------------
+# iterations file system resolution e.g.
+# ----------------------------------------
+# 1 nano or micro second ext4
+# 4 1 second ext3
+# 5 2 second FAT
+# ----------------------------------------
+check_timestamps_updated()
+{
+ local delay="$1"
+ sleep $delay
+ touch a || fail=1
+
+ test "x$btime" = x$(stat --format %W a) &&
+ test "x$atime" != x$(stat --format %X a) &&
+ test "x$mtime" != x$(stat --format %Y a) &&
+ test "x$ctime" != x$(stat --format %Z a)
+}
+retry_delay_ check_timestamps_updated .07 5 || fail=1
+
+Exit $fail
diff --git a/tests/stat/stat-fmt.sh b/tests/stat/stat-fmt.sh
new file mode 100755
index 0000000..df50973
--- /dev/null
+++ b/tests/stat/stat-fmt.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+# stat --format tests
+
+# Copyright (C) 2003-2023 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ stat
+
+
+# ensure that stat properly handles a format string ending with %
+for i in $(seq 50); do
+ fmt=$(printf "%${i}s" %)
+ out=$(stat --form="$fmt" .)
+ test "$out" = "$fmt" || fail=1
+done
+
+
+# ensure QUOTING_STYLE is honored by %N
+touch "'" || framework_failure_
+# Default since v8.25
+stat -c%N \' >> out || fail=1
+# Default before v8.25
+QUOTING_STYLE=locale stat -c%N \' >> out || fail=1
+cat <<\EOF >exp
+"'"
+'\''
+EOF
+compare exp out || fail=1
+
+# ensure %H and %L modifiers are handled
+stat -c '%r %R %Hd,%Ld %Hr,%Lr' . > out || fail=1
+grep -F '?' out && fail=1
+
+Exit $fail
diff --git a/tests/stat/stat-hyphen.sh b/tests/stat/stat-hyphen.sh
new file mode 100755
index 0000000..ea65c20
--- /dev/null
+++ b/tests/stat/stat-hyphen.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+# demonstrate that stat - works and stat -f - does not.
+
+# Copyright (C) 2009-2023 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ stat
+
+printf -- '-\n' > exp || framework_failure_
+touch f || framework_failure_
+
+stat --format=%n - < f > out || fail=1
+returns_ 1 stat -f - < f || fail=1
+
+compare exp out || fail=1
+
+Exit $fail
diff --git a/tests/stat/stat-mount.sh b/tests/stat/stat-mount.sh
new file mode 100755
index 0000000..e07e89a
--- /dev/null
+++ b/tests/stat/stat-mount.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+# Test stat -c%m
+
+# Copyright (C) 2010-2023 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ stat
+
+stat_mnt=$(stat -c%m .) || fail=1
+case "$stat_mnt" in
+ /*) ;;
+ *) fail=1;;
+esac
+
+Exit $fail
diff --git a/tests/stat/stat-nanoseconds.sh b/tests/stat/stat-nanoseconds.sh
new file mode 100755
index 0000000..d42626e
--- /dev/null
+++ b/tests/stat/stat-nanoseconds.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+# Exercise format strings involving %:X, %:Y, etc.
+
+# Copyright (C) 2010-2023 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+print_ver_ stat
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+
+# Set this to avoid problems with weird time zones.
+TZ=UTC0
+export TZ
+
+# Use a timestamp near the Epoch to avoid trouble with leap seconds.
+touch -d '1970-01-01 18:43:33.023456789' k || framework_failure_
+
+ls --full-time | grep 18:43:33.023456789 \
+ || skip_ this file system does not support sub-second timestamps
+
+test "$(stat -c %X k)" = 67413 || fail=1
+test "$(stat -c %.X k)" = 67413.023456789 || fail=1
+test "$(stat -c %.1X k)" = 67413.0 || fail=1
+test "$(stat -c %.3X k)" = 67413.023 || fail=1
+test "$(stat -c %.6X k)" = 67413.023456 || fail=1
+test "$(stat -c %.9X k)" = 67413.023456789 || fail=1
+test "$(stat -c %13.6X k)" = ' 67413.023456' || fail=1
+test "$(stat -c %013.6X k)" = 067413.023456 || fail=1
+test "$(stat -c %-13.6X k)" = '67413.023456 ' || fail=1
+test "$(stat -c %18.10X k)" = ' 67413.0234567890' || fail=1
+test "$(stat -c %I18.10X k)" = ' 67413.0234567890' || fail=1
+test "$(stat -c %018.10X k)" = 0067413.0234567890 || fail=1
+test "$(stat -c %-18.10X k)" = '67413.0234567890 ' || fail=1
+
+Exit $fail
diff --git a/tests/stat/stat-printf.pl b/tests/stat/stat-printf.pl
new file mode 100755
index 0000000..491fb36
--- /dev/null
+++ b/tests/stat/stat-printf.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+# Test "stat --printf".
+
+# Copyright (C) 2005-2023 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+use strict;
+
+(my $ME = $0) =~ s|.*/||;
+my $prog = 'stat';
+
+# Turn off localization of executable's output.
+@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
+
+my @Tests =
+ (
+ # test-name, [option, option, ...] {OUT=>"expected-output"}
+ #
+ ['nl', q!--printf='\n' .!, {OUT=>"\n"}],
+ ['no-nl', "--printf=%n .", {OUT=>"."}],
+ ['pct-and-esc', q!--printf='\0%n\0' .!, {OUT=>"\0.\0"}],
+ ['backslash', q!--printf='\\\\' .!, {OUT=>"\\"}],
+ ['nul', q!--printf='\0' .!, {OUT=>"\0"}],
+ # Don't bother testing \v, since Perl doesn't handle it.
+ ['bel-etc', q!--printf='\a\b\f\n\r\t' .!, {OUT=>"\a\b\f\n\r\t"}],
+ ['octal-1', q!--printf='\012\377' .!, {OUT=>"\012\377"}],
+ ['octal-2', q!--printf='.\012a\377b' .!, {OUT=>".\012a\377b"}],
+ ['hex-1', q!--printf='\x34\xf' .!, {OUT=>"\x34\xf"}],
+ ['hex-2', q!--printf='.\x18p\xfq' .!, {OUT=>".\x18p\x0fq"}],
+ ['hex-3', q!--printf='\x' .!, {OUT=>'x'},
+ {ERR=>"$prog: warning: unrecognized escape '\\x'\n"}],
+
+ # With --format, there *is* a trailing newline.
+ ['f-nl', "--format=%n .", {OUT=>".\n"}],
+ ['f-nl2', "--format=%n . .", {OUT=>".\n.\n"}],
+
+ ['end-pct', "--printf=% .", {OUT=>"%"}],
+ ['pct-pct', "--printf=%% .", {OUT=>"%"}],
+ ['end-bs', "--printf='\\' .", {OUT=>'\\'},
+ {ERR=>"$prog: warning: backslash at end of format\n"}],
+
+ ['err-1', "--printf=%9% .", {EXIT => 1},
+ {ERR=>"$prog: '%9%': invalid directive\n"}],
+ ['err-2', "--printf=%9 .", {EXIT => 1},
+ {ERR=>"$prog: '%9': invalid directive\n"}],
+ );
+
+my $save_temps = $ENV{DEBUG};
+my $verbose = $ENV{VERBOSE};
+
+my $fail = run_tests ($ME, $prog, \@Tests, $save_temps, $verbose);
+exit $fail;
diff --git a/tests/stat/stat-slash.sh b/tests/stat/stat-slash.sh
new file mode 100755
index 0000000..16ecce8
--- /dev/null
+++ b/tests/stat/stat-slash.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+# demonstrate that stat handles trailing slashes correctly
+
+# Copyright (C) 2009-2023 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ stat
+
+touch file || framework_failure_
+mkdir dir || framework_failure_
+ln -s file link1 || framework_failure_
+ln -s dir link2 || framework_failure_
+
+cat <<EOF > exp || framework_failure_
+link1
+symbolic link
+directory
+directory
+EOF
+
+# This failed on Solaris 9 for coreutils 8.0.
+stat --format=%n link1 > out || fail=1
+returns_ 1 stat --format=%n link1/ >> out || fail=1
+
+stat --format=%F link2 >> out || fail=1
+stat -L --format=%F link2 >> out || fail=1
+stat --format=%F link2/ >> out || fail=1
+
+compare exp out || fail=1
+
+Exit $fail