summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2008-03-28 22:55:31 +0000
committerJim Meyering <meyering@redhat.com>2008-06-06 14:16:45 +0200
commit75e0047c4b916393b6be3cb985c4c4d4a2d1a836 (patch)
treeac520fe47c6ba9fa6a725d47ba6f20eb4be5ddc5 /tests
parent74c0bfbc4cf0b38c9c8af7b018f8bbff60da6767 (diff)
downloadcoreutils-75e0047c4b916393b6be3cb985c4c4d4a2d1a836.tar.xz
Add new program: truncate
* AUTHORS: Register as the author * NEWS: Mention this change * README: Add truncate command to list * src/truncate.c: New command * src/Makefile.am: Add truncate command to list to build * src/.gitignore: Add truncate binary to list to ignore * doc/coreutils.texi (truncate invocation): Add truncate info * man/Makefile.am: Add truncate man page to list to build * man/truncate.x: Add truncate man page template * po/POTFILES.in: Add truncate to list to translate * tests/Makefile.am: Add truncate tests * tests/misc/help-version: Add support for new truncate command * tests/misc/truncate-dangling-symlink: check dangling link ok * tests/misc/truncate-dir-fail: ensure dirs fail * tests/misc/truncate-fail-diag: validate messages for missing paths * tests/misc/truncate-fifo: ensure fifos ignored * tests/misc/truncate-no-create-missing: ensure -c option honoured * tests/misc/truncate-overflow: check signed integer overflows * tests/misc/truncate-owned-by-other: root permissions check * tests/misc/truncate-parameters: check invalid parameter combinations * tests/misc/truncate-relative: check invalid relative sizes
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am9
-rwxr-xr-xtests/misc/help-version1
-rwxr-xr-xtests/misc/truncate-dangling-symlink33
-rwxr-xr-xtests/misc/truncate-dir-fail14
-rwxr-xr-xtests/misc/truncate-fail-diag50
-rwxr-xr-xtests/misc/truncate-fifo32
-rwxr-xr-xtests/misc/truncate-no-create-missing31
-rwxr-xr-xtests/misc/truncate-overflow55
-rwxr-xr-xtests/misc/truncate-owned-by-other40
-rwxr-xr-xtests/misc/truncate-parameters43
-rwxr-xr-xtests/misc/truncate-relative40
11 files changed, 348 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e5042b96b..bc1729945 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -28,6 +28,7 @@ root_tests = \
ls/nameless-uid \
misc/chcon \
misc/selinux \
+ misc/truncate-owned-by-other \
mkdir/writable-under-readonly \
mv/sticky-to-xpart \
rm/fail-2eperm \
@@ -212,6 +213,14 @@ TESTS = \
misc/timeout \
misc/timeout-parameters \
misc/tr \
+ misc/truncate-dangling-symlink \
+ misc/truncate-dir-fail \
+ misc/truncate-fail-diag \
+ misc/truncate-fifo \
+ misc/truncate-no-create-missing \
+ misc/truncate-overflow \
+ misc/truncate-parameters \
+ misc/truncate-relative \
misc/tsort \
misc/tty-eof \
misc/unexpand \
diff --git a/tests/misc/help-version b/tests/misc/help-version
index ff0133a22..77d37bb3d 100755
--- a/tests/misc/help-version
+++ b/tests/misc/help-version
@@ -131,6 +131,7 @@ rmdir_args=$tmp_dir
rm_args=$tmp_in
shred_args=$tmp_in
touch_args=$tmp_in2
+truncate_args="--reference=$tmp_in $tmp_in2"
basename_args=$tmp_in
dirname_args=$tmp_in
diff --git a/tests/misc/truncate-dangling-symlink b/tests/misc/truncate-dangling-symlink
new file mode 100755
index 000000000..e16da95de
--- /dev/null
+++ b/tests/misc/truncate-dangling-symlink
@@ -0,0 +1,33 @@
+#!/bin/sh
+# Make sure truncate can create a file through a dangling symlink.
+
+# Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ truncate --version
+fi
+
+. $srcdir/test-lib.sh
+
+ln -s truncate-target t-symlink
+fail=0
+
+truncate -s0 t-symlink || fail=1
+
+test -f truncate-target || fail=1
+
+(exit $fail); exit $fail
diff --git a/tests/misc/truncate-dir-fail b/tests/misc/truncate-dir-fail
new file mode 100755
index 000000000..3ae918e76
--- /dev/null
+++ b/tests/misc/truncate-dir-fail
@@ -0,0 +1,14 @@
+#!/bin/sh
+# Make sure truncate fails for a directory.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ truncate --version
+fi
+
+. $srcdir/test-lib.sh
+
+# truncate on dir not allowed
+truncate -s+0 . && fail=1 || fail=0
+
+(exit $fail); exit $fail
diff --git a/tests/misc/truncate-fail-diag b/tests/misc/truncate-fail-diag
new file mode 100755
index 000000000..32896258d
--- /dev/null
+++ b/tests/misc/truncate-fail-diag
@@ -0,0 +1,50 @@
+#!/bin/sh
+# make sure truncate gives reasonable diagnostics
+# Note open() checks for trailing '/' before checking for existance
+# open (".", O_CREAT & (O_WRONLY | O_RDWR), ...) -> EISDIR
+# open ("missing/", O_CREAT & (O_WRONLY | O_RDWR), ...) -> EISDIR
+# open ("missing/file", O_CREAT & (O_WRONLY | O_RDWR), ...) -> ENOENT
+
+# Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ truncate --version
+fi
+
+. $srcdir/lang-default
+. $srcdir/test-lib.sh
+skip_if_root_
+
+fail=0
+
+d1=no
+
+dir=$d1/such-dir
+truncate -s0 $dir > out 2>&1 && fail=1
+cat <<EOF > exp
+truncate: cannot open \`$dir' for writing: No such file or directory
+EOF
+compare out exp || fail=1
+
+dir=$d1/
+truncate -s0 $dir > out 2>&1 && fail=1
+cat <<EOF > exp
+truncate: cannot open \`$dir' for writing: Is a directory
+EOF
+compare out exp || fail=1
+
+(exit $fail); exit $fail
diff --git a/tests/misc/truncate-fifo b/tests/misc/truncate-fifo
new file mode 100755
index 000000000..f2decc6aa
--- /dev/null
+++ b/tests/misc/truncate-fifo
@@ -0,0 +1,32 @@
+#!/bin/sh
+# Make sure truncate works on fifos without hanging or errors
+
+# Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ truncate --version
+fi
+
+. $srcdir/test-lib.sh
+
+mkfifo_or_skip_ "fifo"
+
+fail=0
+
+truncate -s0 "fifo" || fail=1
+
+(exit $fail); exit $fail
diff --git a/tests/misc/truncate-no-create-missing b/tests/misc/truncate-no-create-missing
new file mode 100755
index 000000000..bbe8e8559
--- /dev/null
+++ b/tests/misc/truncate-no-create-missing
@@ -0,0 +1,31 @@
+#!/bin/sh
+# Ensure that truncate -c no-such-file doesn't fail.
+
+# Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ truncate --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+# truncate -c no-such-file should not fail.
+truncate -s0 -c no-such-file || fail=1
+
+(exit $fail); exit $fail
diff --git a/tests/misc/truncate-overflow b/tests/misc/truncate-overflow
new file mode 100755
index 000000000..5dbbf38cd
--- /dev/null
+++ b/tests/misc/truncate-overflow
@@ -0,0 +1,55 @@
+#!/bin/sh
+# Validate truncate integer overflow
+
+# Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ truncate --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+# -= overflow
+truncate -s-1 create-zero-len-file || fail=1
+
+echo > non-empty-file
+
+truncate -s2147483648 -c no-such-file && _FILE_OFFSET_BITS=64
+
+if [ $_FILE_OFFSET_BITS -eq 64 ]; then
+ # signed overflow
+ truncate -s9223372036854775808 file && fail=1
+
+ # *= signed overflow
+ truncate --io-blocks --size="1E" file && fail=1
+
+ # += signed overflow
+ truncate -s+9223372036854775807 non-empty-file && fail=1
+else
+ # signed overflow
+ truncate -s2147483648 file && fail=1
+
+ # *= signed overflow
+ truncate --io-blocks --size="1G" file && fail=1
+
+ # += signed overflow
+ truncate -s+2147483647 non-empty-file && fail=1
+fi
+
+(exit $fail); exit $fail
diff --git a/tests/misc/truncate-owned-by-other b/tests/misc/truncate-owned-by-other
new file mode 100755
index 000000000..a7ad27864
--- /dev/null
+++ b/tests/misc/truncate-owned-by-other
@@ -0,0 +1,40 @@
+#!/bin/sh
+# Demonstrate that "truncate -s0 writable-but-owned-by-other" works.
+
+# Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ truncate --version
+fi
+
+. $srcdir/envvar-check
+. $srcdir/test-lib.sh
+require_root_
+
+group_num=$(id -g $NON_ROOT_USERNAME)
+
+# Create a file owned by root, and writable by $NON_ROOT_USERNAME.
+echo > root-owned || framework_failure
+chgrp +$group_num . root-owned || framework_failure
+chmod g+w root-owned
+
+# Ensure that the current directory is searchable by $NON_ROOT_USERNAME.
+chmod g+x .
+
+setuidgid $NON_ROOT_USERNAME env PATH="$PATH" truncate -s0 root-owned || fail=1
+
+(exit $fail); exit $fail
diff --git a/tests/misc/truncate-parameters b/tests/misc/truncate-parameters
new file mode 100755
index 000000000..e41683190
--- /dev/null
+++ b/tests/misc/truncate-parameters
@@ -0,0 +1,43 @@
+#!/bin/sh
+# Validate truncate parameter combinations
+
+# Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ truncate --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+# must specify at least 1 file
+truncate --size=0 && fail=1
+
+# must specify size. don't default to 0
+truncate file && fail=1
+
+# mixture of size & reference not allowed
+truncate --size=0 --reference=file file && fail=1
+
+# blocks without size is not valid
+truncate --io-blocks --reference=file file && fail=1
+
+# must specify valid numbers
+truncate --size="invalid" file && fail=1
+
+(exit $fail); exit $fail
diff --git a/tests/misc/truncate-relative b/tests/misc/truncate-relative
new file mode 100755
index 000000000..dedb76fd8
--- /dev/null
+++ b/tests/misc/truncate-relative
@@ -0,0 +1,40 @@
+#!/bin/sh
+# Validate truncate relative sizes
+
+# Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ truncate --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+# mixture of relative modifiers not allowed
+truncate --size="+>0" file && fail=1
+
+# mixture of relative modifiers not allowed
+truncate --size=">+0" file && fail=1
+
+# division by zero
+truncate --size="/0" file && fail=1
+
+# division by zero
+truncate --size="%0" file && fail=1
+
+(exit $fail); exit $fail