summaryrefslogtreecommitdiffstats
path: root/tools/regression/file
diff options
context:
space:
mode:
Diffstat (limited to 'tools/regression/file')
-rw-r--r--tools/regression/file/dup/Makefile7
-rw-r--r--tools/regression/file/dup/dup.c160
-rw-r--r--tools/regression/file/dup/dup.t10
-rw-r--r--tools/regression/file/ftruncate/Makefile7
-rw-r--r--tools/regression/file/ftruncate/ftruncate.c177
5 files changed, 361 insertions, 0 deletions
diff --git a/tools/regression/file/dup/Makefile b/tools/regression/file/dup/Makefile
new file mode 100644
index 0000000..4de0815
--- /dev/null
+++ b/tools/regression/file/dup/Makefile
@@ -0,0 +1,7 @@
+# $FreeBSD$
+
+PROG= dup
+NO_MAN=
+WARNS?= 6
+
+.include <bsd.prog.mk>
diff --git a/tools/regression/file/dup/dup.c b/tools/regression/file/dup/dup.c
new file mode 100644
index 0000000..ad1f6df
--- /dev/null
+++ b/tools/regression/file/dup/dup.c
@@ -0,0 +1,160 @@
+/*
+ * $OpenBSD: dup2test.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $
+ * $OpenBSD: dup2_self.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $
+ * $OpenBSD: fcntl_dup.c,v 1.2 2003/07/31 21:48:08 deraadt Exp $
+ *
+ * Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * Test #1: check if dup(2) works.
+ * Test #2: check if dup2(2) works.
+ * Test #3: check if dup2(2) returned a fd we asked for.
+ * Test #4: check if dup2(2) cleared close-on-exec flag for duped fd.
+ * Test #5: check if dup2(2) allows to dup fd to itself.
+ * Test #6: check if dup2(2) returned a fd we asked for.
+ * Test #7: check if dup2(2) did not clear close-on-exec flag for duped fd.
+ * Test #8: check if fcntl(F_DUPFD) works.
+ * Test #9: check if fcntl(F_DUPFD) cleared close-on-exec flag for duped fd.
+ * Test #10: check if dup2() to a fd > current maximum number of open files
+ * limit work.
+ */
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static int getafile(void);
+
+static int
+getafile(void)
+{
+ int fd;
+
+ char temp[] = "/tmp/dup2XXXXXXXXX";
+ if ((fd = mkstemp(temp)) < 0)
+ err(1, "mkstemp");
+ remove(temp);
+ if (ftruncate(fd, 1024) != 0)
+ err(1, "ftruncate");
+ return (fd);
+}
+
+int
+main(int __unused argc, char __unused *argv[])
+{
+ struct rlimit rlp;
+ int orgfd, fd1, fd2, test = 0;
+
+ orgfd = getafile();
+
+ printf("1..10\n");
+
+ /* If dup(2) ever work? */
+ if ((fd1 = dup(orgfd)) < 0)
+ err(1, "dup");
+ printf("ok %d - dup(2) works\n", ++test);
+
+ /* Set close-on-exec */
+ if (fcntl(fd1, F_SETFD, 1) != 0)
+ err(1, "fcntl(F_SETFD)");
+
+ /* If dup2(2) ever work? */
+ if ((fd2 = dup2(fd1, fd1 + 1)) < 0)
+ err(1, "dup2");
+ printf("ok %d - dup2(2) works\n", ++test);
+
+ /* Do we get the right fd? */
+ ++test;
+ if (fd2 != fd1 + 1)
+ printf("no ok %d - dup2(2) didn't give us the right fd\n",
+ test);
+ else
+ printf("ok %d - dup2(2) returned a correct fd\n", test);
+
+ /* Was close-on-exec cleared? */
+ ++test;
+ if (fcntl(fd2, F_GETFD) != 0)
+ printf("not ok %d - dup2(2) didn't clear close-on-exec\n",
+ test);
+ else
+ printf("ok %d - dup2(2) cleared close-on-exec\n", test);
+
+ /*
+ * Dup to itself.
+ *
+ * We're testing a small tweak in dup2 semantics.
+ * Normally dup and dup2 will clear the close-on-exec
+ * flag on the new fd (which appears to be an implementation
+ * mistake from start and not some planned behavior).
+ * In todays implementations of dup and dup2 we have to make
+ * an effort to really clear that flag. But all tested
+ * implementations of dup2 have another tweak. If we
+ * dup2(old, new) when old == new, the syscall short-circuits
+ * and returns early (because there is no need to do all the
+ * work (and there is a risk for serious mistakes)).
+ * So although the docs say that dup2 should "take 'old',
+ * close 'new' perform a dup(2) of 'old' into 'new'"
+ * the docs are not really followed because close-on-exec
+ * is not cleared on 'new'.
+ *
+ * Since everyone has this bug, we pretend that this is
+ * the way it is supposed to be and test here that it really
+ * works that way.
+ *
+ * This is a fine example on where two separate implementation
+ * fuckups take out each other and make the end-result the way
+ * it was meant to be.
+ */
+ if ((fd2 = dup2(fd1, fd1)) < 0)
+ err(1, "dup2");
+ printf("ok %d - dup2(2) to itself works\n", ++test);
+
+ /* Do we get the right fd? */
+ ++test;
+ if (fd2 != fd1)
+ printf("not ok %d - dup2(2) didn't give us the right fd\n",
+ test);
+ else
+ printf("ok %d - dup2(2) to itself returned a correct fd\n",
+ test);
+
+ /* Was close-on-exec cleared? */
+ ++test;
+ if (fcntl(fd2, F_GETFD) == 0)
+ printf("not ok %d - dup2(2) cleared close-on-exec\n", test);
+ else
+ printf("ok %d - dup2(2) didn't clear close-on-exec\n", test);
+
+ /* Does fcntl(F_DUPFD) work? */
+ if ((fd2 = fcntl(fd1, F_DUPFD)) < 0)
+ err(1, "fcntl(F_DUPFD)");
+ printf("ok %d - fcntl(F_DUPFD) works\n", ++test);
+
+ /* Was close-on-exec cleared? */
+ ++test;
+ if (fcntl(fd2, F_GETFD) != 0)
+ printf(
+ "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n",
+ test);
+ else
+ printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test);
+
+ ++test;
+ if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
+ err(1, "getrlimit");
+ if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) == 0)
+ printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test);
+ else
+ printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test);
+
+ return (0);
+}
diff --git a/tools/regression/file/dup/dup.t b/tools/regression/file/dup/dup.t
new file mode 100644
index 0000000..8bdfd03
--- /dev/null
+++ b/tools/regression/file/dup/dup.t
@@ -0,0 +1,10 @@
+#!/bin/sh
+# $FreeBSD$
+
+cd `dirname $0`
+
+executable=`basename $0 .t`
+
+make $executable 2>&1 > /dev/null
+
+exec ./$executable
diff --git a/tools/regression/file/ftruncate/Makefile b/tools/regression/file/ftruncate/Makefile
new file mode 100644
index 0000000..c3285ef
--- /dev/null
+++ b/tools/regression/file/ftruncate/Makefile
@@ -0,0 +1,7 @@
+# $FreeBSD$
+
+PROG= ftruncate
+NO_MAN=
+WARNS?= 2
+
+.include <bsd.prog.mk>
diff --git a/tools/regression/file/ftruncate/ftruncate.c b/tools/regression/file/ftruncate/ftruncate.c
new file mode 100644
index 0000000..aebcdcd
--- /dev/null
+++ b/tools/regression/file/ftruncate/ftruncate.c
@@ -0,0 +1,177 @@
+/*-
+ * Copyright (c) 2006 Robert N. M. Watson
+ * 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$
+ */
+
+/*
+ * Very simple regression test.
+ *
+ * Future tests that might be of interest:
+ *
+ * - Make sure we get EISDIR on a directory.
+ */
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdio.h>
+#include <unistd.h>
+
+/*
+ * Select various potentially interesting lengths at and around power of 2
+ * edges.
+ */
+static off_t lengths[] = {0, 1, 2, 3, 4, 127, 128, 129, 511, 512, 513, 1023,
+ 1024, 1025, 2047, 2048, 2049, 4095, 4096, 4097, 8191, 8192, 8193, 16383,
+ 16384, 16385};
+static int lengths_count = sizeof(lengths) / sizeof(off_t);
+
+int
+main(int argc, char *argv[])
+{
+ int error, fd, fds[2], i, read_only_fd;
+ char path[PATH_MAX];
+ struct stat sb;
+ size_t size;
+ off_t len;
+ char ch;
+
+ /*
+ * Tests using a writable temporary file: grow and then shrink a file
+ * using ftruncate and various lengths. Make sure that a negative
+ * file length is rejected. Make sure that when we grow the file,
+ * bytes now in the range of the file size return 0.
+ *
+ * Save a read-only reference to the file to use later for read-only
+ * descriptor tests.
+ */
+ snprintf(path, PATH_MAX, "/tmp/ftruncate.XXXXXXXXXXXXX");
+ fd = mkstemp(path);
+ if (fd < 0)
+ err(-1, "makestemp");
+ read_only_fd = open(path, O_RDONLY);
+ if (read_only_fd < 0) {
+ error = errno;
+ (void)unlink(path);
+ errno = error;
+ err(-1, "open(%s, O_RDONLY)", path);
+ }
+ (void)unlink(path);
+
+ if (ftruncate(fd, -1) == 0)
+ errx(-1, "ftruncate(fd, -1) succeeded");
+ if (errno != EINVAL)
+ err(-1, "ftruncate(fd, -1) returned wrong error");
+
+ for (i = 0; i < lengths_count; i++) {
+ len = lengths[i];
+ if (ftruncate(fd, len) < 0)
+ err(-1, "ftruncate(%llu) up", len);
+ if (fstat(fd, &sb) < 0)
+ err(-1, "stat");
+ if (sb.st_size != len)
+ errx(-1, "fstat(%llu) returned len %llu up", len,
+ sb.st_size);
+ if (len != 0) {
+ size = pread(fd, &ch, sizeof(ch), len - 1);
+ if (size < 0)
+ err(-1, "pread on len %llu up", len);
+ if (size != sizeof(ch))
+ errx(-1, "pread len %llu size %jd up",
+ len, (intmax_t)size);
+ if (ch != 0)
+ errx(-1,
+ "pread length %llu size %jd ch %d up",
+ len, (intmax_t)size, ch);
+ }
+ }
+
+ for (i = lengths_count - 1; i >= 0; i--) {
+ len = lengths[i];
+ if (ftruncate(fd, len) < 0)
+ err(-1, "ftruncate(%llu) down", len);
+ if (fstat(fd, &sb) < 0)
+ err(-1, "stat");
+ if (sb.st_size != len)
+ errx(-1, "fstat(%llu) returned %llu down", len,
+ sb.st_size);
+ }
+ close(fd);
+
+ /*
+ * Make sure that a read-only descriptor can't be truncated.
+ */
+ if (ftruncate(read_only_fd, 0) == 0)
+ errx(-1, "ftruncate(read_only_fd) succeeded");
+ if (errno != EINVAL)
+ err(-1, "ftruncate(read_only_fd) returned wrong error");
+ close(read_only_fd);
+
+ /*
+ * Make sure that ftruncate on sockets doesn't work.
+ */
+ fd = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (fd < 0)
+ err(-1, "socket(PF_UNIX, SOCK_STREAM, 0)");
+ if (ftruncate(fd, 0) == 0)
+ errx(-1, "ftruncate(socket) succeeded");
+ if (errno != EINVAL)
+ err(-1, "ftruncate(socket) returned wrong error");
+ close(fd);
+
+ /*
+ * Make sure that ftruncate on pipes doesn't work.
+ */
+ if (pipe(fds) < 0)
+ err(-1, "pipe");
+ if (ftruncate(fds[0], 0) == 0)
+ errx(-1, "ftruncate(pipe) succeeded");
+ if (errno != EINVAL)
+ err(-1, "ftruncate(pipe) returned wrong error");
+ close(fds[0]);
+ close(fds[1]);
+
+ /*
+ * Make sure that ftruncate on kqueues doesn't work.
+ */
+ fd = kqueue();
+ if (fd < 0)
+ err(-1, "kqueue");
+ if (ftruncate(fds[0], 0) == 0)
+ errx(-1, "ftruncate(kqueue) succeeded");
+ if (errno != EINVAL)
+ err(-1, "ftruncate(kqueue) returned wrong error");
+ close(fd);
+
+ return (0);
+}
OpenPOWER on IntegriCloud