Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Few tests for mkfifo(2).



details:   https://anonhg.NetBSD.org/src/rev/b0d1b982ff7a
branches:  trunk
changeset: 766943:b0d1b982ff7a
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Mon Jul 04 15:46:09 2011 +0000

description:
Few tests for mkfifo(2).

diffstat:

 distrib/sets/lists/tests/mi |    4 +-
 tests/syscall/Makefile      |    5 +-
 tests/syscall/t_mkfifo.c    |  280 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 286 insertions(+), 3 deletions(-)

diffs (truncated from 328 to 300 lines):

diff -r 3d55991db61d -r b0d1b982ff7a distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Mon Jul 04 13:00:19 2011 +0000
+++ b/distrib/sets/lists/tests/mi       Mon Jul 04 15:46:09 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.362 2011/07/04 08:09:11 jruoho Exp $
+# $NetBSD: mi,v 1.363 2011/07/04 15:46:09 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -636,6 +636,7 @@
 ./usr/libdata/debug/usr/tests/syscall/t_kill.debug                     tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_link.debug                     tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_mincore.debug                  tests-syscall-debug     debug,atf
+./usr/libdata/debug/usr/tests/syscall/t_mkfifo.debug                   tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_mknod.debug                    tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_mmap.debug                     tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_mprotect.debug                 tests-syscall-debug     debug,atf
@@ -2388,6 +2389,7 @@
 ./usr/tests/syscall/t_kill                     tests-syscall-tests     atf
 ./usr/tests/syscall/t_link                     tests-syscall-tests     atf
 ./usr/tests/syscall/t_mincore                  tests-syscall-tests     atf
+./usr/tests/syscall/t_mkfifo                   tests-syscall-tests     atf
 ./usr/tests/syscall/t_mknod                    tests-syscall-tests     atf
 ./usr/tests/syscall/t_mmap                     tests-syscall-tests     atf
 ./usr/tests/syscall/t_mprotect                 tests-syscall-tests     atf
diff -r 3d55991db61d -r b0d1b982ff7a tests/syscall/Makefile
--- a/tests/syscall/Makefile    Mon Jul 04 13:00:19 2011 +0000
+++ b/tests/syscall/Makefile    Mon Jul 04 15:46:09 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.39 2011/07/04 08:09:11 jruoho Exp $
+# $NetBSD: Makefile,v 1.40 2011/07/04 15:46:10 jruoho Exp $
 
 .include <bsd.own.mk>
 
@@ -8,7 +8,8 @@
 TESTS_C+=      t_getgroups t_getlogin t_getpid
 TESTS_C+=      t_getrusage t_getsid t_gettimeofday
 TESTS_C+=      t_issetugid t_itimer t_kill t_link
-TESTS_C+=      t_mincore t_mknod t_mmap t_mprotect t_msync t_nanosleep
+TESTS_C+=      t_mincore t_mkfifo t_mknod t_mmap
+TESTS_C+=      t_mprotect t_msync t_nanosleep
 TESTS_C+=      t_poll t_pollts t_pselect t_revoke
 TESTS_C+=      t_setrlimit t_setuid t_stat t_timer t_truncate
 TESTS_C+=      t_umask t_unlink
diff -r 3d55991db61d -r b0d1b982ff7a tests/syscall/t_mkfifo.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/syscall/t_mkfifo.c  Mon Jul 04 15:46:09 2011 +0000
@@ -0,0 +1,280 @@
+/* $NetBSD: t_mkfifo.c,v 1.1 2011/07/04 15:46:10 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_mkfifo.c,v 1.1 2011/07/04 15:46:10 jruoho Exp $");
+
+#include <sys/stat.h>
+#include <sys/wait.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <string.h>
+#include <unistd.h>
+
+static const char      path[] = "fifo";
+static void            support(void);
+
+static void
+support(void)
+{
+
+       errno = 0;
+
+       if (mkfifo(path, 0600) == 0) {
+               ATF_REQUIRE(unlink(path) == 0);
+               return;
+       }
+
+       if (errno == EOPNOTSUPP)
+               atf_tc_skip("the kernel does not support FIFOs");
+       else {
+               atf_tc_fail("mkfifo(2) failed");
+       }
+}
+
+ATF_TC_WITH_CLEANUP(mkfifo_block);
+ATF_TC_HEAD(mkfifo_block, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test that FIFOs block");
+}
+
+ATF_TC_BODY(mkfifo_block, tc)
+{
+       int fd[2], sta;
+       pid_t pid;
+
+       support();
+
+       fd[0] = fd[1] = -1;
+       ATF_REQUIRE(mkfifo(path, 0600) == 0);
+
+       pid = fork();
+       ATF_REQUIRE(pid >= 0);
+
+       if (pid == 0) {
+
+               /*
+                * If we open the FIFO as read-only (write-only),
+                * the call should block until another process
+                * opens the FIFO for writing (reading).
+                */
+               fd[0] = open(path, O_RDONLY);
+
+               (void)pause();
+
+               _exit(EXIT_SUCCESS); /* NOTREACHED */
+       }
+
+       (void)sleep(1);
+
+       fd[1] = open(path, O_WRONLY);
+       ATF_REQUIRE(kill(pid, SIGKILL) == 0);
+
+       (void)wait(&sta);
+
+       if (WIFSIGNALED(sta) == 0 || WTERMSIG(sta) != SIGKILL)
+               atf_tc_fail("FIFO did not block");
+
+       (void)close(fd[0]);
+       (void)unlink(path);
+}
+
+ATF_TC_CLEANUP(mkfifo_block, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(mkfifo_err);
+ATF_TC_HEAD(mkfifo_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test erros from mkfifo(2)");
+}
+
+ATF_TC_BODY(mkfifo_err, tc)
+{
+       char buf[PATH_MAX + 1];
+
+       support();
+
+       (void)memset(buf, 'x', sizeof(buf));
+       ATF_REQUIRE(mkfifo(path, 0600) == 0);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EFAULT, mkfifo((char *)-1, 0600) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EEXIST, mkfifo("/etc/passwd", 0600) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EEXIST, mkfifo(path, 0600) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENAMETOOLONG, mkfifo(buf, 0600) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOENT, mkfifo("/a/b/c/d/e/f/g", 0600) == -1);
+
+       ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(mkfifo_err, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(mkfifo_nonblock);
+ATF_TC_HEAD(mkfifo_nonblock, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test O_NONBLOCK with FIFOs");
+}
+
+ATF_TC_BODY(mkfifo_nonblock, tc)
+{
+       int fd, sta;
+       pid_t pid;
+
+       support();
+
+       fd = -1;
+       ATF_REQUIRE(mkfifo(path, 0600) == 0);
+
+       pid = fork();
+       ATF_REQUIRE(pid >= 0);
+
+       if (pid == 0) {
+
+               /*
+                * If we open the FIFO as O_NONBLOCK, the O_RDONLY
+                * call should return immediately, whereas the call
+                * for write-only should fail with ENXIO.
+                */
+               fd = open(path, O_RDONLY | O_NONBLOCK);
+
+               if (fd >= 0)
+                       _exit(EXIT_SUCCESS);
+
+               (void)pause();  /* NOTREACHED */
+       }
+
+       (void)sleep(1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENXIO, open(path, O_WRONLY | O_NONBLOCK) == -1);
+
+       (void)kill(pid, SIGKILL);
+       (void)wait(&sta);
+
+       if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL)
+               atf_tc_fail("FIFO blocked for O_NONBLOCK open(2)");
+
+       (void)close(fd);
+       (void)unlink(path);
+}
+
+ATF_TC_CLEANUP(mkfifo_nonblock, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(mkfifo_perm);
+ATF_TC_HEAD(mkfifo_perm, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test permissions with mkfifo(2)");
+       atf_tc_set_md_var(tc, "require.user", "unprivileged");
+}
+
+ATF_TC_BODY(mkfifo_perm, tc)
+{
+
+       support();
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EACCES, mkfifo("/root/fifo", 0600) == -1);
+
+       ATF_REQUIRE(mkfifo(path, 0600) == 0);
+
+       /*
+        * For some reason this fails with EFTYPE...
+        */
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EFTYPE, chmod(path, 1777) == -1);
+
+       ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(mkfifo_perm, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(mkfifo_stat);
+ATF_TC_HEAD(mkfifo_stat, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test mkfifo(2) with stat");
+}
+
+ATF_TC_BODY(mkfifo_stat, tc)
+{
+       struct stat st;
+
+       support();



Home | Main Index | Thread Index | Old Index