Module Name:    src
Committed By:   jruoho
Date:           Thu Mar 31 15:47:57 UTC 2011

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/tests/syscall: Makefile
Added Files:
        src/tests/syscall: t_dup.c

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


To generate a diff of this commit:
cvs rdiff -u -r1.282 -r1.283 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.11 -r1.12 src/tests/syscall/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/syscall/t_dup.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.282 src/distrib/sets/lists/tests/mi:1.283
--- src/distrib/sets/lists/tests/mi:1.282	Thu Mar 31 13:07:20 2011
+++ src/distrib/sets/lists/tests/mi	Thu Mar 31 15:47:56 2011
@@ -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

Index: src/tests/syscall/Makefile
diff -u src/tests/syscall/Makefile:1.11 src/tests/syscall/Makefile:1.12
--- src/tests/syscall/Makefile:1.11	Thu Mar 31 13:07:20 2011
+++ src/tests/syscall/Makefile	Thu Mar 31 15:47:57 2011
@@ -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

Added files:

Index: src/tests/syscall/t_dup.c
diff -u /dev/null src/tests/syscall/t_dup.c:1.1
--- /dev/null	Thu Mar 31 15:47:58 2011
+++ src/tests/syscall/t_dup.c	Thu Mar 31 15:47:56 2011
@@ -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();
+}

Reply via email to