Module Name:    src
Committed By:   jruoho
Date:           Wed Mar 30 09:43:22 UTC 2011

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/tests/include: Makefile
        src/tests/syscall: Makefile
Added Files:
        src/tests/include: t_paths.c
        src/tests/syscall: t_fsync.c

Log Message:
Add some basic tests for fsync(2) and <paths.h>.


To generate a diff of this commit:
cvs rdiff -u -r1.278 -r1.279 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.1 -r1.2 src/tests/include/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/include/t_paths.c
cvs rdiff -u -r1.9 -r1.10 src/tests/syscall/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/syscall/t_fsync.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.278 src/distrib/sets/lists/tests/mi:1.279
--- src/distrib/sets/lists/tests/mi:1.278	Fri Mar 25 09:34:02 2011
+++ src/distrib/sets/lists/tests/mi	Wed Mar 30 09:43:21 2011
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.278 2011/03/25 09:34:02 jruoho Exp $
+# $NetBSD: mi,v 1.279 2011/03/30 09:43:21 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -286,6 +286,7 @@
 ./usr/libdata/debug/usr/tests/include/t_bitstring.debug			tests-ipf-tests		debug,atf
 ./usr/libdata/debug/usr/tests/include/t_inttypes.debug			tests-ipf-tests		debug,atf
 ./usr/libdata/debug/usr/tests/include/t_limits.debug			tests-ipf-tests		debug,atf
+./usr/libdata/debug/usr/tests/include/t_paths.debug			tests-ipf-tests		debug,atf
 ./usr/libdata/debug/usr/tests/include/t_stdint.debug			tests-ipf-tests		debug,atf
 ./usr/libdata/debug/usr/tests/ipf					tests-ipf-tests
 ./usr/libdata/debug/usr/tests/kernel					tests-kernel-tests
@@ -564,6 +565,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_fsync.debug			tests-syscall-debug	debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_timer.debug			tests-syscall-debug	debug,atf
 ./usr/libdata/debug/usr/tests/usr.bin					tests-sbin-tests
 ./usr/libdata/debug/usr/tests/usr.sbin					tests-sbin-tests
@@ -1337,6 +1339,7 @@
 ./usr/tests/include/t_bitstring			tests-include-tests	atf
 ./usr/tests/include/t_inttypes			tests-include-tests	atf
 ./usr/tests/include/t_limits			tests-include-tests	atf
+./usr/tests/include/t_paths			tests-include-tests	atf
 ./usr/tests/include/t_stdint			tests-include-tests	atf
 ./usr/tests/ipf					tests-ipf-tests
 ./usr/tests/ipf/Atffile				tests-ipf-tests
@@ -2100,6 +2103,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_fsync			tests-syscall-tests	atf
 ./usr/tests/syscall/t_timer			tests-syscall-tests	atf
 ./usr/tests/toolchain	tests-syscall-tests	atf
 ./usr/tests/toolchain/Atffile	tests-syscall-tests	atf

Index: src/tests/include/Makefile
diff -u src/tests/include/Makefile:1.1 src/tests/include/Makefile:1.2
--- src/tests/include/Makefile:1.1	Sat Jul 17 19:26:27 2010
+++ src/tests/include/Makefile	Wed Mar 30 09:43:22 2011
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.1 2010/07/17 19:26:27 jmmv Exp $
+# $NetBSD: Makefile,v 1.2 2011/03/30 09:43:22 jruoho Exp $
 
 NOMAN=		# defined
 
@@ -10,6 +10,7 @@
 TESTS_C=	t_bitstring
 TESTS_C+=	t_inttypes
 TESTS_C+=	t_limits
+TESTS_C+=	t_paths
 TESTS_C+=	t_stdint
 
 FILESDIR=	${TESTSDIR}

Index: src/tests/syscall/Makefile
diff -u src/tests/syscall/Makefile:1.9 src/tests/syscall/Makefile:1.10
--- src/tests/syscall/Makefile:1.9	Wed May 19 19:17:08 2010
+++ src/tests/syscall/Makefile	Wed Mar 30 09:43:21 2011
@@ -1,13 +1,13 @@
-# $NetBSD: Makefile,v 1.9 2010/05/19 19:17:08 jruoho Exp $
+# $NetBSD: Makefile,v 1.10 2011/03/30 09:43:21 jruoho Exp $
 
 .include <bsd.own.mk>
 
 TESTSDIR=	${TESTSBASE}/syscall
 
-TESTS_C+=	t_cmsg t_timer
+TESTS_C+=	t_cmsg t_fsync t_timer
 
-LDADD+=	-lrumpnet_local -lrumpnet_net -lrumpnet
-LDADD+=	-lrumpvfs -lrump -lrumpuser -lpthread
+LDADD.t_cmsg+=	-lrumpnet_local -lrumpnet_net -lrumpnet
+LDADD.t_cmsg+=	-lrumpvfs -lrump -lrumpuser -lpthread
 
 WARNS=		4
 

Added files:

Index: src/tests/include/t_paths.c
diff -u /dev/null src/tests/include/t_paths.c:1.1
--- /dev/null	Wed Mar 30 09:43:22 2011
+++ src/tests/include/t_paths.c	Wed Mar 30 09:43:22 2011
@@ -0,0 +1,195 @@
+/*	$NetBSD: t_paths.c,v 1.1 2011/03/30 09:43:22 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_paths.c,v 1.1 2011/03/30 09:43:22 jruoho Exp $");
+
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <paths.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#define PATH_DEV	__BIT(0)	/* A device node	*/
+#define PATH_DIR	__BIT(1)	/* A directory		*/
+#define PATH_FILE	__BIT(2)	/* A file		*/
+#define PATH_ROOT	__BIT(3)	/* Access for root only */
+
+static const struct {
+	const char	*path;
+	int		 flags;
+} paths[] = {
+	{ _PATH_AUDIO,		PATH_DEV		},
+	{ _PATH_AUDIO0,		PATH_DEV		},
+	{ _PATH_AUDIOCTL,	PATH_DEV		},
+	{ _PATH_AUDIOCTL0,	PATH_DEV		},
+	{ _PATH_BPF,		PATH_DEV | PATH_ROOT	},
+	{ _PATH_CLOCKCTL,	PATH_DEV | PATH_ROOT	},
+	{ _PATH_CONSOLE,	PATH_DEV		},
+	{ _PATH_CONSTTY,	PATH_DEV | PATH_ROOT	},
+	{ _PATH_CSMAPPER,	PATH_DIR		},
+	{ _PATH_DEFTAPE,	PATH_DEV | PATH_ROOT	},
+	{ _PATH_DEVDB,		PATH_FILE		},
+	{ _PATH_DEVNULL,	PATH_DEV		},
+	{ _PATH_DRUM,		PATH_DEV | PATH_ROOT	},
+	{ _PATH_ESDB,		PATH_DIR		},
+	{ _PATH_FTPUSERS,	PATH_FILE		},
+	{ _PATH_GETTYTAB,	PATH_FILE		},
+	{ _PATH_I18NMODULE,	PATH_DIR		},
+	{ _PATH_ICONV,		PATH_DIR		},
+	{ _PATH_KMEM,		PATH_DEV | PATH_ROOT	},
+	{ _PATH_KSYMS,		PATH_DEV		},
+	{ _PATH_KVMDB,		PATH_FILE		},
+	{ _PATH_LOCALE,		PATH_DIR		},
+	{ _PATH_MAILDIR,	PATH_DIR		},
+	{ _PATH_MAN,		PATH_DIR		},
+	{ _PATH_MEM,		PATH_DEV | PATH_ROOT	},
+	{ _PATH_MIXER,		PATH_DEV		},
+	{ _PATH_MIXER0,		PATH_DEV		},
+	{ _PATH_NOLOGIN,	PATH_FILE		},
+	{ _PATH_POWER,		PATH_DEV		},
+	{ _PATH_PRINTCAP,	PATH_FILE		},
+	{ _PATH_PUD,		PATH_DEV | PATH_ROOT	},
+	{ _PATH_PUFFS,		PATH_DEV | PATH_ROOT	},
+	{ _PATH_RANDOM,		PATH_DEV		},
+	{ _PATH_SENDMAIL,	PATH_FILE		},
+	{ _PATH_SHELLS,		PATH_FILE		},
+	{ _PATH_SKEYKEYS,	PATH_FILE | PATH_ROOT	},
+	{ _PATH_SOUND,		PATH_DEV		},
+	{ _PATH_SOUND0,		PATH_DEV		},
+	{ _PATH_SYSMON,		PATH_DEV		},
+	{ _PATH_TTY,		PATH_DEV		},
+	{ _PATH_UNIX,		PATH_FILE		},
+	{ _PATH_URANDOM,	PATH_DEV		},
+	{ _PATH_VIDEO,		PATH_DEV		},
+	{ _PATH_VIDEO0,		PATH_DEV		},
+
+	{ _PATH_DEV,		PATH_DIR		},
+	{ _PATH_DEV_PTS,	PATH_DIR		},
+	{ _PATH_EMUL_AOUT,	PATH_DIR		},
+	{ _PATH_TMP,		PATH_DIR		},
+	{ _PATH_VARDB,		PATH_DIR		},
+	{ _PATH_VARRUN,		PATH_DIR		},
+	{ _PATH_VARTMP,		PATH_DIR		},
+
+	{ _PATH_BSHELL,		PATH_FILE		},
+	{ _PATH_CSHELL,		PATH_FILE		},
+	{ _PATH_VI,		PATH_FILE		},
+};
+
+ATF_TC(paths);
+ATF_TC_HEAD(paths, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A basic test for <paths.h>");
+}
+
+ATF_TC_BODY(paths, tc)
+{
+	struct stat st;
+	uid_t uid;
+	mode_t m;
+	size_t i;
+	int fd;
+
+	uid = getuid();
+
+	for (i = 0; i < __arraycount(paths); i++) {
+
+		errno = 0;
+		fd = open(paths[i].path, O_RDONLY);
+
+		if (fd < 0) {
+
+			switch (errno) {
+
+			case EPERM:	/* FALLTHROUGH */
+			case EACCES:	/* FALLTHROUGH */
+
+				if ((paths[i].flags & PATH_ROOT) == 0) {
+
+					atf_tc_fail("UID %u failed to open %s",
+					    (uint32_t)uid, paths[i].path);
+				}
+
+			case EBUSY:	/* FALLTHROUGH */
+			case ENXIO:	/* FALLTHROUGH */
+			case ENOENT:	/* FALLTHROUGH */
+
+			default:
+				continue;
+			}
+		}
+
+		(void)memset(&st, 0, sizeof(struct stat));
+
+		ATF_REQUIRE(fstat(fd, &st) == 0);
+
+		m = st.st_mode;
+
+		if ((paths[i].flags & PATH_DEV) != 0) {
+
+			ATF_REQUIRE(S_ISBLK(m) != 0 || S_ISCHR(m) != 0);
+
+			ATF_REQUIRE((paths[i].flags & PATH_DIR) == 0);
+			ATF_REQUIRE((paths[i].flags & PATH_FILE) == 0);
+		}
+
+		if ((paths[i].flags & PATH_DIR) != 0) {
+
+			ATF_REQUIRE(S_ISDIR(m) != 0);
+
+			ATF_REQUIRE((paths[i].flags & PATH_DEV) == 0);
+			ATF_REQUIRE((paths[i].flags & PATH_FILE) == 0);
+		}
+
+		if ((paths[i].flags & PATH_FILE) != 0) {
+
+			ATF_REQUIRE(S_ISREG(m) != 0);
+
+			ATF_REQUIRE((paths[i].flags & PATH_DEV) == 0);
+			ATF_REQUIRE((paths[i].flags & PATH_DIR) == 0);
+		}
+
+		ATF_REQUIRE(close(fd) == 0);
+	}
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, paths);
+
+	return atf_no_error();
+}

Index: src/tests/syscall/t_fsync.c
diff -u /dev/null src/tests/syscall/t_fsync.c:1.1
--- /dev/null	Wed Mar 30 09:43:22 2011
+++ src/tests/syscall/t_fsync.c	Wed Mar 30 09:43:21 2011
@@ -0,0 +1,119 @@
+/* $NetBSD: t_fsync.c,v 1.1 2011/03/30 09:43:21 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_fsync.c,v 1.1 2011/03/30 09:43:21 jruoho Exp $");
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+ATF_TC(fsync_1);
+ATF_TC_HEAD(fsync_1, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test error conditions of fsync(2)");
+}
+
+ATF_TC_BODY(fsync_1, tc)
+{
+	int i, fd[2];
+
+	/*
+	 * The fsync(2) call should fail with EBADF
+	 * when the 'fd' is not a valid descriptor.
+	 */
+	for (i = 1; i < 1024; i = i + 128) {
+
+		errno = 0;
+
+		ATF_REQUIRE(fsync(-i) == -1);
+		ATF_REQUIRE(errno == EBADF);
+	}
+
+	/*
+	 * On the other hand, EINVAL should follow
+	 * if the operation is not possible with
+	 * the file descriptor (cf. PR kern/30).
+	 */
+	ATF_REQUIRE(pipe(fd) == 0);
+
+	errno = 0;
+
+	ATF_REQUIRE(fsync(fd[0]) == -1);
+	ATF_REQUIRE(errno == EINVAL);
+
+	errno = 0;
+
+	ATF_REQUIRE(fsync(fd[1]) == -1);
+	ATF_REQUIRE(errno == EINVAL);
+
+	ATF_REQUIRE(close(fd[0]) == 0);
+	ATF_REQUIRE(close(fd[1]) == 0);
+}
+
+ATF_TC(fsync_2);
+ATF_TC_HEAD(fsync_2, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "A basic test of fsync(2)");
+}
+
+ATF_TC_BODY(fsync_2, tc)
+{
+	char buf[128];
+	int fd, i;
+
+	for (i = 0; i < 10; i++) {
+
+		(void)snprintf(buf, sizeof(buf), "t_fsync-%d", i);
+
+		fd = mkstemp(buf);
+
+		ATF_REQUIRE(fd != -1);
+		ATF_REQUIRE(write(fd, "0", 1) == 1);
+		ATF_REQUIRE(fsync(fd) == 0);
+
+		ATF_REQUIRE(unlink(buf) == 0);
+		ATF_REQUIRE(close(fd) == 0);
+	}
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+	ATF_TP_ADD_TC(tp, fsync_1);
+	ATF_TP_ADD_TC(tp, fsync_2);
+
+	return atf_no_error();
+}

Reply via email to