Source-Changes-HG archive

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

[src/trunk]: src A simple test for mincore(2).



details:   https://anonhg.NetBSD.org/src/rev/afe06848d3a5
branches:  trunk
changeset: 765675:afe06848d3a5
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Thu Jun 02 10:48:55 2011 +0000

description:
A simple test for mincore(2).

diffstat:

 distrib/sets/lists/tests/mi |    4 +-
 tests/syscall/Makefile      |    8 +-
 tests/syscall/t_mincore.c   |  189 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 197 insertions(+), 4 deletions(-)

diffs (240 lines):

diff -r f4fedf9362bc -r afe06848d3a5 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Thu Jun 02 09:02:39 2011 +0000
+++ b/distrib/sets/lists/tests/mi       Thu Jun 02 10:48:55 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.342 2011/06/01 19:45:08 tron Exp $
+# $NetBSD: mi,v 1.343 2011/06/02 10:48:55 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -617,6 +617,7 @@
 ./usr/libdata/debug/usr/tests/syscall/t_gettimeofday.debug             tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_itimer.debug                   tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/syscall/t_kill.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_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
@@ -2323,6 +2324,7 @@
 ./usr/tests/syscall/t_gettimeofday             tests-syscall-tests     atf
 ./usr/tests/syscall/t_itimer                   tests-syscall-tests     atf
 ./usr/tests/syscall/t_kill                     tests-syscall-tests     atf
+./usr/tests/syscall/t_mincore                  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
diff -r f4fedf9362bc -r afe06848d3a5 tests/syscall/Makefile
--- a/tests/syscall/Makefile    Thu Jun 02 09:02:39 2011 +0000
+++ b/tests/syscall/Makefile    Thu Jun 02 10:48:55 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.29 2011/06/01 19:43:10 tron Exp $
+# $NetBSD: Makefile,v 1.30 2011/06/02 10:48:56 jruoho Exp $
 
 .include <bsd.own.mk>
 
@@ -6,8 +6,10 @@
 
 TESTS_C+=      t_access t_cmsg t_dup t_fsync
 TESTS_C+=      t_getgroups t_getpid t_getrusage t_getsid t_gettimeofday
-TESTS_C+=      t_itimer t_kill t_mmap t_mprotect t_msync t_nanosleep t_poll
-TESTS_C+=      t_pollts t_pselect t_setrlimit t_setuid t_timer t_umask
+TESTS_C+=      t_itimer t_kill
+TESTS_C+=      t_mincore t_mmap t_mprotect t_msync t_nanosleep
+TESTS_C+=      t_poll t_pollts t_pselect
+TESTS_C+=      t_setrlimit t_setuid t_timer t_umask
 
 LDADD.t_getpid+=       -lpthread
 LDADD.t_timer+=                -lpthread
diff -r f4fedf9362bc -r afe06848d3a5 tests/syscall/t_mincore.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/syscall/t_mincore.c Thu Jun 02 10:48:55 2011 +0000
@@ -0,0 +1,189 @@
+/* $NetBSD: t_mincore.c,v 1.1 2011/06/02 10:48:55 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_mincore.c,v 1.1 2011/06/02 10:48:55 jruoho Exp $");
+
+#include <sys/mman.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static long            page = 0;
+static const char      path[] = "mincore";
+
+ATF_TC(mincore_err);
+ATF_TC_HEAD(mincore_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test errors from mincore(2)");
+}
+
+ATF_TC_BODY(mincore_err, tc)
+{
+       char *map, *vec;
+
+       map = mmap(NULL, page, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
+       vec = malloc(page);
+
+       ATF_REQUIRE(vec != NULL);
+       ATF_REQUIRE(map != MAP_FAILED);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EINVAL, mincore(map, 0, vec) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOMEM, mincore(0, page, vec) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EFAULT, mincore(map, page, (void *)-1) == -1);
+
+       free(vec);
+       ATF_REQUIRE(munmap(map, page) == 0);
+}
+
+ATF_TC_WITH_CLEANUP(mincore_incore);
+ATF_TC_HEAD(mincore_incore, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test that mincore(2) works");
+}
+
+ATF_TC_BODY(mincore_incore, tc)
+{
+       char *buf, *vec, *map = MAP_FAILED;
+       const char *str = NULL;
+       const size_t n = 3;
+       ssize_t tot;
+       int fd, rv;
+       size_t i, j;
+
+       /*
+        * Create a temporary file, write
+        * few pages to it, and map the file.
+        */
+       buf = calloc(n, page);
+       vec = calloc(n, page);
+
+       if (buf == NULL || vec == NULL)
+               return;
+
+       for (i = 0; i < (size_t)page * n; i++)
+               buf[i] = 'x';
+
+       fd = open(path, O_RDWR | O_CREAT, 0600);
+
+       if (fd < 0) {
+               str = "failed to open";
+               goto out;
+       }
+
+       tot = 0;
+
+       while (tot < page * (long)n) {
+
+               rv = write(fd, buf, sizeof(buf));
+
+               if (rv < 0) {
+                       str = "failed to write";
+                       goto out;
+               }
+
+               tot += rv;
+       }
+
+       map = mmap(NULL, page * n, PROT_READ | PROT_WRITE,
+           MAP_FILE | MAP_PRIVATE, fd, 0);
+
+       if (map == MAP_FAILED) {
+               str = "failed to map";
+               goto out;
+       }
+
+       /*
+        * Lock the mapping such that only
+        * in-core page status is returned.
+        */
+       if (mlock(map, page * n) != 0) {
+               str = "failed to lock";
+               goto out;
+       }
+
+       if (mincore(map, page * n, vec) != 0) {
+               str = "mincore failed";
+               goto out;
+       }
+
+       /*
+        * Check that the in-core pages
+        * match the locked pages.
+        */
+       for (i = j = 0; i < (size_t)page * n; i++) {
+
+               if (vec[i] != 0)
+                       j++;
+       }
+
+       if (j != n)
+               str = "mismatch of in-core pages";
+
+out:
+       free(buf);
+       free(vec);
+
+       (void)close(fd);
+       (void)unlink(path);
+
+       if (map != MAP_FAILED) {
+               (void)munlock(map, page);
+               (void)munmap(map, page);
+       }
+
+       if (str != NULL)
+               atf_tc_fail(str);
+}
+
+ATF_TC_CLEANUP(mincore_incore, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+       page = sysconf(_SC_PAGESIZE);
+       ATF_REQUIRE(page >= 0);
+
+       ATF_TP_ADD_TC(tp, mincore_err);
+       ATF_TP_ADD_TC(tp, mincore_incore);
+
+       return atf_no_error();
+}



Home | Main Index | Thread Index | Old Index