Source-Changes-HG archive

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

[src/trunk]: src Add few naive test cases for msync(2).



details:   https://anonhg.NetBSD.org/src/rev/d022959c2679
branches:  trunk
changeset: 763933:d022959c2679
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Thu Apr 07 17:38:02 2011 +0000

description:
Add few naive test cases for msync(2).

diffstat:

 distrib/sets/lists/tests/mi |    4 +-
 tests/syscall/Makefile      |    8 +-
 tests/syscall/t_msync.c     |  225 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 233 insertions(+), 4 deletions(-)

diffs (274 lines):

diff -r 2572595c2c28 -r d022959c2679 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Thu Apr 07 17:33:11 2011 +0000
+++ b/distrib/sets/lists/tests/mi       Thu Apr 07 17:38:02 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.298 2011/04/07 06:24:07 plunky Exp $
+# $NetBSD: mi,v 1.299 2011/04/07 17:38:02 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -583,6 +583,7 @@
 ./usr/libdata/debug/usr/tests/syscall/t_getsid.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
+./usr/libdata/debug/usr/tests/syscall/t_msync.debug                    tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_setrlimit.debug                        tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_setuid.debug                   tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_timer.debug                    tests-syscall-debug     debug,atf
@@ -2140,6 +2141,7 @@
 ./usr/tests/syscall/t_getsid                   tests-syscall-tests     atf
 ./usr/tests/syscall/t_mmap                     tests-syscall-tests     atf
 ./usr/tests/syscall/t_mprotect                 tests-syscall-tests     atf
+./usr/tests/syscall/t_msync                    tests-syscall-tests     atf
 ./usr/tests/syscall/t_setrlimit                        tests-syscall-tests     atf
 ./usr/tests/syscall/t_setuid                   tests-syscall-tests     atf
 ./usr/tests/syscall/t_timer                    tests-syscall-tests     atf
diff -r 2572595c2c28 -r d022959c2679 tests/syscall/Makefile
--- a/tests/syscall/Makefile    Thu Apr 07 17:33:11 2011 +0000
+++ b/tests/syscall/Makefile    Thu Apr 07 17:38:02 2011 +0000
@@ -1,11 +1,13 @@
-# $NetBSD: Makefile,v 1.20 2011/04/06 05:53:17 jruoho Exp $
+# $NetBSD: Makefile,v 1.21 2011/04/07 17:38:02 jruoho Exp $
 
 .include <bsd.own.mk>
 
 TESTSDIR=      ${TESTSBASE}/syscall
 
-TESTS_C+=      t_access t_cmsg t_dup t_fsync t_getpid t_getrusage t_getsid
-TESTS_C+=      t_mmap t_mprotect t_setrlimit t_setuid t_timer t_umask
+TESTS_C+=      t_access t_cmsg t_dup t_fsync
+TESTS_C+=      t_getpid t_getrusage t_getsid
+TESTS_C+=      t_mmap t_mprotect t_msync
+TESTS_C+=      t_setrlimit t_setuid t_timer t_umask
 
 LDADD.t_getpid+=       -lpthread
 LDADD.t_timer+=                -lpthread
diff -r 2572595c2c28 -r d022959c2679 tests/syscall/t_msync.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/syscall/t_msync.c   Thu Apr 07 17:38:02 2011 +0000
@@ -0,0 +1,225 @@
+/* $NetBSD: t_msync.c,v 1.1 2011/04/07 17:38:02 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_msync.c,v 1.1 2011/04/07 17:38:02 jruoho Exp $");
+
+#include <sys/mman.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static long            page = 0;
+static const off_t     off = 512;
+static const char      path[] = "/tmp/msync";
+
+static const char      *msync_sync(const char *, int);
+
+static const char *
+msync_sync(const char *garbage, int flags)
+{
+       char *buf, *map = MAP_FAILED;
+       const char *str = NULL;
+       size_t i, len;
+       ssize_t tot;
+       int fd, rv;
+
+       /*
+        * Create a temporary file, write
+        * one page to it, and map the file.
+        */
+       buf = malloc(page);
+
+       if (buf == NULL)
+               return NULL;
+
+       for (i = 0; i < (size_t)page; i++)
+               buf[i] = 'x';
+
+       fd = open(path, O_RDWR | O_CREAT, 0700);
+
+       if (fd < 0) {
+               str = "failed to open";
+               goto out;
+       }
+
+       tot = 0;
+
+       while (tot < page) {
+
+               rv = write(fd, buf, sizeof(buf));
+
+               if (rv < 0) {
+                       str = "failed to write";
+                       goto out;
+               }
+
+               tot += rv;
+       }
+
+       map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE, fd, 0);
+
+       if (map == MAP_FAILED) {
+               str = "failed to map";
+               goto out;
+       }
+
+       /*
+        * Seek to an arbitrary offset and
+        * write garbage to this position.
+        */
+       if (lseek(fd, off, SEEK_SET) != off) {
+               str = "failed to seek";
+               goto out;
+       }
+
+       len = strlen(garbage);
+       rv = write(fd, garbage, len);
+
+       if (rv != (ssize_t)len) {
+               str = "failed to write garbage";
+               goto out;
+       }
+
+       /*
+        * Synchronize the mapping and verify
+        * that garbage is at the given offset.
+        */
+       if (msync(map, page, flags) != 0) {
+               str = "failed to msync";
+               goto out;
+       }
+
+       if (memcmp(map + off, garbage, len) != 0) {
+               str = "msync did not synchronize";
+               goto out;
+       }
+
+out:
+       free(buf);
+
+       (void)close(fd);
+       (void)unlink(path);
+
+       if (map != MAP_FAILED)
+               (void)munmap(map, page);
+
+       return str;
+}
+
+ATF_TC(msync_async);
+ATF_TC_HEAD(msync_async, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_ASYNC");
+}
+
+ATF_TC_BODY(msync_async, tc)
+{
+       const char *str;
+
+       str = msync_sync("garbage", MS_ASYNC);
+
+       if (str != NULL)
+               atf_tc_fail(str);
+}
+
+ATF_TC(msync_err);
+ATF_TC_HEAD(msync_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test error conditions in msync(2)");
+}
+
+ATF_TC_BODY(msync_err, tc)
+{
+
+       /*
+        * Test that invalid flags error out.
+        */
+       ATF_REQUIRE(msync_sync("error", -1) != NULL);
+       ATF_REQUIRE(msync_sync("error", INT_MAX) != NULL);
+
+       errno = 0;
+
+       ATF_REQUIRE(msync((void *)INT_MAX, page, MS_SYNC) != 0);
+       ATF_REQUIRE(errno == EFAULT);
+}
+
+ATF_TC(msync_invalidate);
+ATF_TC_HEAD(msync_invalidate, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_INVALIDATE");
+}
+
+ATF_TC_BODY(msync_invalidate, tc)
+{
+       const char *str;
+
+       str = msync_sync("garbage", MS_INVALIDATE);
+
+       if (str != NULL)
+               atf_tc_fail(str);
+}
+
+ATF_TC(msync_sync);
+ATF_TC_HEAD(msync_sync, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test of msync(2), MS_SYNC");
+}
+
+ATF_TC_BODY(msync_sync, tc)
+{
+       const char *str;
+
+       str = msync_sync("garbage", MS_SYNC);
+
+       if (str != NULL)
+               atf_tc_fail(str);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+       page = sysconf(_SC_PAGESIZE);
+
+       ATF_REQUIRE(page >= 0);
+       ATF_REQUIRE(page > off);
+
+       ATF_TP_ADD_TC(tp, msync_async);
+       ATF_TP_ADD_TC(tp, msync_err);
+       ATF_TP_ADD_TC(tp, msync_invalidate);
+       ATF_TP_ADD_TC(tp, msync_sync);
+
+       return atf_no_error();
+}



Home | Main Index | Thread Index | Old Index