summaryrefslogtreecommitdiffstats
path: root/tests/head
diff options
context:
space:
mode:
Diffstat (limited to 'tests/head')
-rwxr-xr-xtests/head/head-c.sh59
-rwxr-xr-xtests/head/head-elide-tail.pl110
-rwxr-xr-xtests/head/head-pos.sh44
-rwxr-xr-xtests/head/head-write-error.sh51
-rwxr-xr-xtests/head/head.pl87
5 files changed, 351 insertions, 0 deletions
diff --git a/tests/head/head-c.sh b/tests/head/head-c.sh
new file mode 100755
index 0000000..052caa7
--- /dev/null
+++ b/tests/head/head-c.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+# exercise head -c
+
+# Copyright (C) 2001-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_ head
+getlimits_
+
+vm=$(get_min_ulimit_v_ head -c1 /dev/null) \
+ || skip_ "this shell lacks ulimit support"
+
+# exercise the fix of 2001-08-18, based on test case from Ian Bruce
+echo abc > in || framework_failure_
+(head -c1; head -c1) < in > out || fail=1
+case "$(cat out)" in
+ ab) ;;
+ *) fail=1 ;;
+esac
+
+# Test for a bug in coreutils 5.0.1 through 8.22.
+printf 'abc\ndef\n' > in1 || framework_failure_
+(dd bs=1 skip=1 count=0 status=none && head -c-4) < in1 > out1 || fail=1
+case "$(cat out1)" in
+ bc) ;;
+ *) fail=1 ;;
+esac
+
+# Only allocate memory as needed.
+# Coreutils <= 8.21 would allocate memory up front
+# based on the value passed to -c
+(ulimit -v $(($vm+8000)) && head --bytes=-$SSIZE_MAX < /dev/null) || fail=1
+
+# Make sure it works on funny files in /proc and /sys.
+
+for file in /proc/version /sys/kernel/profiling; do
+ if test -r $file; then
+ cp -f $file copy &&
+ head -c -1 copy > exp1 || framework_failure_
+
+ head -c -1 $file > out1 || fail=1
+ compare exp1 out1 || fail=1
+ fi
+done
+
+Exit $fail
diff --git a/tests/head/head-elide-tail.pl b/tests/head/head-elide-tail.pl
new file mode 100755
index 0000000..221b45a
--- /dev/null
+++ b/tests/head/head-elide-tail.pl
@@ -0,0 +1,110 @@
+#!/usr/bin/perl
+# Exercise head's --bytes=-N option.
+
+# 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/>.
+
+use strict;
+
+(my $program_name = $0) =~ s|.*/||;
+
+$ENV{PROG} = 'head';
+
+# Turn off localization of executable's output.
+@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
+
+# This should match the definition in head.c.
+my $READ_BUFSIZE = 8192;
+
+my @Tests =
+ (
+ # Elide the exact size of the file.
+ ['elide-b1', "--bytes=-2", {IN=>"a\n"}, {OUT=>''}],
+ # Elide more than the size of the file.
+ ['elide-b2', "--bytes=-2", {IN=>"a"}, {OUT=>''}],
+ # Leave just one byte.
+ ['elide-b3', "--bytes=-2", {IN=>"abc"}, {OUT=>'a'}],
+ # Make it so the elided bytes straddle the end of the first
+ # $READ_BUFSIZE block.
+ ['elide-b4', "--bytes=-2",
+ {IN=> 'a' x ($READ_BUFSIZE-3) . "\nbcd"},
+ {OUT=>'a' x ($READ_BUFSIZE-3) . "\nb"}],
+ # Make it so the elided bytes straddle the end of the 2nd
+ # $READ_BUFSIZE block.
+ ['elide-b5', "--bytes=-2",
+ {IN=> 'a' x (2 * $READ_BUFSIZE - 2) . 'bcd'},
+ {OUT=>'a' x (2 * $READ_BUFSIZE - 2) . 'b'}],
+
+ ['elide-l0', "--lines=-1", {IN=>''}, {OUT=>''}],
+ ['elide-l1', "--lines=-1", {IN=>"a\n"}, {OUT=>''}],
+ ['elide-l2', "--lines=-1", {IN=>"a"}, {OUT=>''}],
+ ['elide-l3', "--lines=-1", {IN=>"a\nb"}, {OUT=>"a\n"}],
+ ['elide-l4', "--lines=-1", {IN=>"a\nb\n"}, {OUT=>"a\n"}],
+ ['elide-l5', "--lines=-0", {IN=>"a\nb\n"}, {OUT=>"a\nb\n"}],
+ ['elide-l6', "--lines=-0", {IN=>"a\nb"}, {OUT=>"a\nb"}],
+ );
+
+if ($ENV{RUN_EXPENSIVE_TESTS})
+ {
+ # Brute force: use all combinations of file sizes [0..20] and
+ # number of bytes to elide [0..20]. For better coverage, recompile
+ # head with -DHEAD_TAIL_PIPE_READ_BUFSIZE=4 and
+ # -DHEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD=8
+ my $s = "abcdefghijklmnopqrst";
+ for my $file_size (0..20)
+ {
+ for my $n_elide (0..20)
+ {
+ my $input = substr $s, 0, $file_size;
+ my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
+ my $output = substr $input, 0, $out_len;
+ my $t = ["elideb$file_size-$n_elide", "--bytes=-$n_elide",
+ {IN=>$input}, {OUT=>$output}];
+ push @Tests, $t;
+ my @u = @$t;
+ # Insert the ---presume-input-pipe option.
+ $u[0] .= 'p';
+ $u[1] .= ' ---presume-input-pipe';
+ push @Tests, \@u;
+ }
+ }
+
+ $s =~ s/(.)/$1\n/g;
+ $s .= 'u'; # test without trailing '\n'
+ for my $file_size (0..21)
+ {
+ for my $n_elide (0..21)
+ {
+ my $input = substr $s, 0, 2 * $file_size;
+ my $out_len = $n_elide < $file_size ? $file_size - $n_elide : 0;
+ my $output = substr $input, 0, 2 * $out_len;
+ my $t = ["elidel$file_size-$n_elide", "--lines=-$n_elide",
+ {IN=>$input}, {OUT=>$output}];
+ push @Tests, $t;
+ my @u = @$t;
+ # Insert the ---presume-input-pipe option.
+ $u[0] .= 'p';
+ $u[1] .= ' ---presume-input-pipe';
+ push @Tests, \@u;
+ }
+ }
+ }
+
+my $save_temps = $ENV{DEBUG};
+my $verbose = $ENV{VERBOSE};
+
+my $prog = $ENV{PROG} || die "$0: \$PROG not specified in environment\n";
+my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
+exit $fail;
diff --git a/tests/head/head-pos.sh b/tests/head/head-pos.sh
new file mode 100755
index 0000000..c24edd1
--- /dev/null
+++ b/tests/head/head-pos.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+# When reading a specified number of lines, ensure that the output
+# file pointer is positioned just after those lines.
+
+# Copyright (C) 2002-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_ head
+
+(echo a; echo b) > in || framework_failure_
+echo b > exp || framework_failure_
+
+for i in -1 1; do
+ (head -n $i >/dev/null; cat) < in > out || fail=1
+ compare exp out || fail=1
+done
+
+# Exercise the (start_pos < pos) block in elide_tail_lines_seekable.
+# So far, this is the only test to do that.
+# Do that by creating a file larger than BUFSIZ (I've seen 128K) and
+# elide a suffix of it (by line count) that is also larger than BUFSIZ.
+# 50000 lines times 6 bytes per line gives us enough leeway even on a
+# system with a BUFSIZ of 256K.
+n_lines=50000
+seq 70000 > in2 || framework_failure_
+echo $n_lines > exp-n || framework_failure_
+
+(head -n-$n_lines>/dev/null; wc -l) < in2 > n
+compare exp-n n || fail=1
+
+Exit $fail
diff --git a/tests/head/head-write-error.sh b/tests/head/head-write-error.sh
new file mode 100755
index 0000000..25cebe1
--- /dev/null
+++ b/tests/head/head-write-error.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+# Ensure we diagnose and not continue writing to
+# the output if we get a write error.
+
+# Copyright (C) 2014-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_ head
+
+if ! test -w /dev/full || ! test -c /dev/full; then
+ skip_ '/dev/full is required'
+fi
+
+# We can't use /dev/zero as that's bypassed in the --lines case
+# due to lseek() indicating it has a size of zero.
+yes | head -c10M > bigseek || framework_failure_
+
+# This is the single output diagnostic expected,
+# (without the possibly varying :strerror(ENOSPC) suffix).
+printf '%s\n' "head: error writing 'standard output'" > exp
+
+# Memory is bounded in these cases
+for item in lines bytes; do
+ for N in 0 1; do
+ # pipe case
+ yes | returns_ 1 timeout 10s head --$item=-$N > /dev/full 2> errt || fail=1
+ sed 's/\(head:.*\):.*/\1/' errt > err
+ compare exp err || fail=1
+
+ # seekable case
+ returns_ 1 timeout 10s head --$item=-$N bigseek > /dev/full 2> errt \
+ || fail=1
+ sed 's/\(head:.*\):.*/\1/' errt > err
+ compare exp err || fail=1
+ done
+done
+
+Exit $fail
diff --git a/tests/head/head.pl b/tests/head/head.pl
new file mode 100755
index 0000000..56a1b35
--- /dev/null
+++ b/tests/head/head.pl
@@ -0,0 +1,87 @@
+#!/usr/bin/perl
+# test head
+
+# Copyright (C) 2008-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 $prog = 'head';
+
+# Turn off localization of executable's output.
+@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
+
+my $in = join ('', map { "$_\n" } 0..600);
+my $in_1024 = substr $in, 0, 1024;
+
+# FIXME: set this properly
+my $x32_bit_long = 0;
+
+my @Tests =
+(
+ ['idem-0', {IN=>''}, {OUT=>''}],
+ ['idem-1', {IN=>'a'}, {OUT=>'a'}],
+ ['idem-2', {IN=>"\n"}, {OUT=>"\n"}],
+ ['idem-3', {IN=>"a\n"}, {OUT=>"a\n"}],
+
+ ['basic-10',
+ {IN=>"1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n"},
+ {OUT=>"1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n"}],
+
+ ['basic-09',
+ {IN=>"1\n2\n3\n4\n5\n6\n7\n8\n9\n"},
+ {OUT=>"1\n2\n3\n4\n5\n6\n7\n8\n9\n"}],
+
+ ['basic-11',
+ {IN=>"1\n2\n3\n4\n5\n6\n7\n8\n9\n0\nb\n"},
+ {OUT=>"1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n"}],
+
+ ['obs-0', '-1', {IN=>"1\n2\n"}, {OUT=>"1\n"}],
+ ['obs-1', '-1c', {IN=>''}, {OUT=>''}],
+ ['obs-2', '-1c', {IN=>'12'}, {OUT=>'1'}],
+ ['obs-3', '-14c', {IN=>'1234567890abcdefg'}, {OUT=>'1234567890abcd'}],
+ ['obs-4', '-2b', {IN=>$in}, {OUT=>$in_1024}],
+ ['obs-5', '-1k', {IN=>$in}, {OUT=>$in_1024}],
+
+ # This test fails for textutils-1.22, because head let 4096m overflow to 0
+ # and did not fail. Now head fails with a diagnostic.
+ # Disable this test because it fails on systems with 64-bit uintmax_t.
+ # ['fail-0', qw(-n 4096m), {IN=>"a\n"}, {EXIT=>1}],
+
+ # In spite of its name, this test passes -- just to contrast with the above.
+ ['fail-1', qw(-n 2048m), {IN=>"a\n"}, {OUT=>"a\n"}],
+
+ # Make sure we don't break like AIX 4.3.1 on files with \0 in them.
+ ['null-1', {IN=>"a\0a\n"}, {OUT=>"a\0a\n"}],
+
+ # Make sure counts are interpreted as decimal.
+ # Before 2.0f, these would have been interpreted as octal
+ ['no-oct-1', '-08', {IN=>"\n"x12}, {OUT=>"\n"x8}],
+ ['no-oct-2', '-010', {IN=>"\n"x12}, {OUT=>"\n"x10}],
+ ['no-oct-3', '-n 08', {IN=>"\n"x12}, {OUT=>"\n"x8}],
+ ['no-oct-4', '-c 08', {IN=>"\n"x12}, {OUT=>"\n"x8}],
+
+ # --zero-terminated
+ ['zero-1', '-z -n 1', {IN=>"x\0y"}, {OUT=>"x\0"}],
+ ['zero-2', '-z -n 2', {IN=>"x\0y"}, {OUT=>"x\0y"}],
+);
+
+@Tests = triple_test \@Tests;
+
+my $save_temps = $ENV{DEBUG};
+my $verbose = $ENV{VERBOSE};
+
+my $fail = run_tests ($prog, $prog, \@Tests, $save_temps, $verbose);
+exit $fail;