Source-Changes-HG archive

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

[src/trunk]: src Add couple of simple tests for dup(2).



details:   https://anonhg.NetBSD.org/src/rev/bff454daf161
branches:  trunk
changeset: 763713:bff454daf161
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Thu Mar 31 15:47:56 2011 +0000

description:
Add couple of simple tests for dup(2).

diffstat:

 distrib/sets/lists/tests/mi |    4 +-
 tests/syscall/Makefile      |    4 +-
 tests/syscall/t_dup.c       |  179 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 184 insertions(+), 3 deletions(-)

diffs (224 lines):

diff -r d517bea72cec -r bff454daf161 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Thu Mar 31 15:46:30 2011 +0000
+++ b/distrib/sets/lists/tests/mi       Thu Mar 31 15:47:56 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.282 2011/03/31 13:07:20 jruoho Exp $
+# $NetBSD: mi,v 1.283 2011/03/31 15:47:56 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -562,6 +562,7 @@
 ./usr/libdata/debug/usr/tests/sbin/resize_ffs                          tests-sbin-tests
 ./usr/libdata/debug/usr/tests/syscall                                  tests-syscall-debug
 ./usr/libdata/debug/usr/tests/syscall/t_cmsg.debug                     tests-syscall-debug     debug,atf
+./usr/libdata/debug/usr/tests/syscall/t_dup.debug                      tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_fsync.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_timer.debug                    tests-syscall-debug     debug,atf
@@ -2096,6 +2097,7 @@
 ./usr/tests/syscall                            tests-syscall-tests
 ./usr/tests/syscall/Atffile                    tests-syscall-tests     atf
 ./usr/tests/syscall/t_cmsg                     tests-syscall-tests     atf
+./usr/tests/syscall/t_dup                      tests-syscall-tests     atf
 ./usr/tests/syscall/t_fsync                    tests-syscall-tests     atf
 ./usr/tests/syscall/t_mmap                     tests-syscall-tests     atf
 ./usr/tests/syscall/t_timer                    tests-syscall-tests     atf
diff -r d517bea72cec -r bff454daf161 tests/syscall/Makefile
--- a/tests/syscall/Makefile    Thu Mar 31 15:46:30 2011 +0000
+++ b/tests/syscall/Makefile    Thu Mar 31 15:47:56 2011 +0000
@@ -1,10 +1,10 @@
-# $NetBSD: Makefile,v 1.11 2011/03/31 13:07:20 jruoho Exp $
+# $NetBSD: Makefile,v 1.12 2011/03/31 15:47:57 jruoho Exp $
 
 .include <bsd.own.mk>
 
 TESTSDIR=      ${TESTSBASE}/syscall
 
-TESTS_C+=      t_cmsg t_fsync t_mmap t_timer
+TESTS_C+=      t_cmsg t_dup t_fsync t_mmap t_timer
 
 LDADD.t_cmsg+= -lrumpnet_local -lrumpnet_net -lrumpnet
 LDADD.t_cmsg+= -lrumpvfs -lrump -lrumpuser -lpthread
diff -r d517bea72cec -r bff454daf161 tests/syscall/t_dup.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/syscall/t_dup.c     Thu Mar 31 15:47:56 2011 +0000
@@ -0,0 +1,179 @@
+/* $NetBSD: t_dup.c,v 1.1 2011/03/31 15:47:56 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_dup.c,v 1.1 2011/03/31 15:47:56 jruoho Exp $");
+
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <stdio.h>
+
+#include <atf-c.h>
+
+static char     path[] = "/tmp/dup";
+
+ATF_TC(dup_err);
+ATF_TC_HEAD(dup_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test error conditions of dup(2)");
+}
+
+ATF_TC_BODY(dup_err, tc)
+{
+       ATF_REQUIRE(dup(-1) != 0);
+}
+
+ATF_TC_WITH_CLEANUP(dup_max);
+ATF_TC_HEAD(dup_max, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test dup(2) against limits");
+}
+
+ATF_TC_BODY(dup_max, tc)
+{
+       int current, fd, *buf, serrno;
+       long i, maxfd;
+
+       /*
+        * Open a temporary file until the
+        * maximum number of open files is
+        * reached. Ater that dup(2) should
+        * fail with EMFILE.
+        */
+       maxfd = sysconf(_SC_OPEN_MAX);
+       ATF_REQUIRE(maxfd >= 0);
+
+       buf = calloc(maxfd, sizeof(int));
+
+       if (buf == NULL)
+               return;
+
+       buf[0] = mkstemp(path);
+       ATF_REQUIRE(buf[0] != -1);
+
+       current = fcntl(0, F_MAXFD);
+       ATF_REQUIRE(current != -1);
+
+       fd = -1;
+       serrno = EMFILE;
+
+       for (i = current; i <= maxfd; i++) {
+
+               buf[i] = open(path, O_RDONLY);
+
+               if (buf[i] < 0)
+                       goto out;
+       }
+
+       errno = 0;
+       fd = dup(buf[0]);
+       serrno = errno;
+
+out:
+       for (i = 0; i <= maxfd; i++)
+               (void)close(buf[i]);
+
+       free(buf);
+
+       if (fd != -1 || serrno != EMFILE)
+               atf_tc_fail("dup(2) dupped more than _SC_OPEN_MAX");
+}
+
+ATF_TC_CLEANUP(dup_max, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(dup_mode);
+ATF_TC_HEAD(dup_mode, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "A basic test of dup(2)");
+}
+
+ATF_TC_BODY(dup_mode, tc)
+{
+       int mode[3] = { O_RDONLY, O_WRONLY, O_RDWR   };
+       int perm[5] = { 0700, 0400, 0600, 0444, 0666 };
+       struct stat st1, st2;
+       int fd1, fd2;
+       size_t i, j;
+
+       /*
+        * Check that a duplicated descriptor
+        * retains the mode of the original file.
+        */
+       for (i = 0; i < __arraycount(mode); i++) {
+
+               for (j = 0; j < __arraycount(perm); j++) {
+
+                       fd1 = open(path, mode[i] | O_CREAT, perm[j]);
+
+                       if (fd1 < 0)
+                               return;
+
+                       fd2 = dup(fd1);
+                       ATF_REQUIRE(fd2 >= 0);
+
+                       (void)memset(&st1, 0, sizeof(struct stat));
+                       (void)memset(&st2, 0, sizeof(struct stat));
+
+                       ATF_REQUIRE(fstat(fd1, &st1) == 0);
+                       ATF_REQUIRE(fstat(fd2, &st2) == 0);
+
+                       if (st1.st_mode != st2.st_mode)
+                               atf_tc_fail("invalid mode");
+
+                       ATF_REQUIRE(close(fd1) == 0);
+                       ATF_REQUIRE(close(fd2) == 0);
+                       ATF_REQUIRE(unlink(path) == 0);
+               }
+       }
+}
+
+ATF_TC_CLEANUP(dup_mode, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+       ATF_TP_ADD_TC(tp, dup_err);
+       ATF_TP_ADD_TC(tp, dup_max);
+       ATF_TP_ADD_TC(tp, dup_mode);
+
+       return atf_no_error();
+}



Home | Main Index | Thread Index | Old Index