Source-Changes-HG archive

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

[src/trunk]: src Add some tests for stat(2) (or tests that use stat(2)).



details:   https://anonhg.NetBSD.org/src/rev/a764302df4fe
branches:  trunk
changeset: 765733:a764302df4fe
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Sat Jun 04 09:29:43 2011 +0000

description:
Add some tests for stat(2) (or tests that use stat(2)).

diffstat:

 distrib/sets/lists/tests/mi |    4 +-
 tests/syscall/Makefile      |    4 +-
 tests/syscall/t_stat.c      |  384 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 389 insertions(+), 3 deletions(-)

diffs (truncated from 431 to 300 lines):

diff -r a26ce518ab69 -r a764302df4fe distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sat Jun 04 05:30:45 2011 +0000
+++ b/distrib/sets/lists/tests/mi       Sat Jun 04 09:29:43 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.346 2011/06/03 06:39:52 jruoho Exp $
+# $NetBSD: mi,v 1.347 2011/06/04 09:29:43 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -630,6 +630,7 @@
 ./usr/libdata/debug/usr/tests/syscall/t_pselect.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_stat.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/syscall/t_umask.debug                    tests-syscall-debug     debug,atf
 ./usr/libdata/debug/usr/tests/usr.bin                                  tests-sbin-tests
@@ -2340,6 +2341,7 @@
 ./usr/tests/syscall/t_pselect                  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_stat                     tests-syscall-tests     atf
 ./usr/tests/syscall/t_timer                    tests-syscall-tests     atf
 ./usr/tests/syscall/t_umask                    tests-syscall-tests     atf
 ./usr/tests/toolchain                          tests-syscall-tests     atf
diff -r a26ce518ab69 -r a764302df4fe tests/syscall/Makefile
--- a/tests/syscall/Makefile    Sat Jun 04 05:30:45 2011 +0000
+++ b/tests/syscall/Makefile    Sat Jun 04 09:29:43 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.30 2011/06/02 10:48:56 jruoho Exp $
+# $NetBSD: Makefile,v 1.31 2011/06/04 09:29:44 jruoho Exp $
 
 .include <bsd.own.mk>
 
@@ -9,7 +9,7 @@
 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
+TESTS_C+=      t_setrlimit t_setuid t_stat t_timer t_umask
 
 LDADD.t_getpid+=       -lpthread
 LDADD.t_timer+=                -lpthread
diff -r a26ce518ab69 -r a764302df4fe tests/syscall/t_stat.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/syscall/t_stat.c    Sat Jun 04 09:29:43 2011 +0000
@@ -0,0 +1,384 @@
+/* $NetBSD: t_stat.c,v 1.1 2011/06/04 09:29:44 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_stat.c,v 1.1 2011/06/04 09:29:44 jruoho Exp $");
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <atf-c.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <limits.h>
+#include <string.h>
+#include <unistd.h>
+
+static const char *path = "stat";
+
+ATF_TC_WITH_CLEANUP(stat_chflags);
+ATF_TC_HEAD(stat_chflags, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test chflags(2) with stat(2)");
+}
+
+ATF_TC_BODY(stat_chflags, tc)
+{
+       struct stat sa, sb;
+       int fd;
+
+       (void)memset(&sa, 0, sizeof(struct stat));
+       (void)memset(&sb, 0, sizeof(struct stat));
+
+       fd = open(path, O_RDONLY | O_CREAT);
+
+       ATF_REQUIRE(fd != -1);
+       ATF_REQUIRE(stat(path, &sa) == 0);
+       ATF_REQUIRE(chflags(path, UF_NODUMP) == 0);
+       ATF_REQUIRE(stat(path, &sb) == 0);
+
+       if (sa.st_flags == sb.st_flags)
+               atf_tc_fail("stat(2) did not detect chflags(2)");
+
+       ATF_REQUIRE(close(fd) == 0);
+       ATF_REQUIRE(unlink(path) == 0);
+}
+
+ATF_TC_CLEANUP(stat_chflags, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC(stat_dir);
+ATF_TC_HEAD(stat_dir, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test stat(2) with directories");
+}
+
+ATF_TC_BODY(stat_dir, tc)
+{
+       const short depth = 3;
+       struct stat sa, sb;
+       struct dirent *dr;
+       char *argv[2];
+       FTSENT *ftse;
+       FTS *fts;
+       DIR *dir;
+       int ops;
+
+       argv[1] = NULL;
+       argv[0] = __UNCONST("/");
+
+       ops = FTS_NOCHDIR;
+       ops |= FTS_PHYSICAL;
+
+       fts = fts_open(argv, ops, NULL);
+       ATF_REQUIRE(fts != NULL);
+
+       while ((ftse = fts_read(fts)) != NULL) {
+
+               if (ftse->fts_level < 1)
+                       continue;
+
+               if (ftse->fts_level > depth) {
+                       (void)fts_set(fts, ftse, FTS_SKIP);
+                       continue;
+               }
+
+               switch(ftse->fts_info) {
+
+               case FTS_DP:
+
+                       (void)memset(&sa, 0, sizeof(struct stat));
+                       (void)memset(&sb, 0, sizeof(struct stat));
+
+                       ATF_REQUIRE(stat(ftse->fts_parent->fts_path,&sa) == 0);
+                       ATF_REQUIRE(chdir(ftse->fts_path) == 0);
+                       ATF_REQUIRE(stat(".", &sb) == 0);
+
+                       /*
+                        * The previous two stat(2) calls
+                        * should be for the same directory.
+                        */
+                       if (sa.st_dev != sb.st_dev || sa.st_ino != sb.st_ino)
+                               atf_tc_fail("inconsistent stat(2)");
+
+                       /*
+                        * Check that fts(3)'s stat(2)
+                        * call equals the manual one.
+                        */
+                       if (sb.st_ino != ftse->fts_statp->st_ino)
+                               atf_tc_fail("stat(2) and fts(3) differ");
+
+                       /*
+                        * Verify that the last stat(2) call
+                        * matches the corresponding dirent(3).
+                        */
+                       dir = opendir(ftse->fts_path);
+                       ATF_REQUIRE(dir != NULL);
+
+                       dr = readdir(dir);
+                       ATF_REQUIRE(dr != NULL);
+
+                       if (sb.st_ino != dr->d_fileno)
+                               atf_tc_fail("stat(2) and readdir(3) differ");
+
+                       ATF_REQUIRE(closedir(dir) == 0);
+                       break;
+
+               default:
+                       break;
+               }
+       }
+
+       (void)fts_close(fts);
+}
+
+ATF_TC(stat_err);
+ATF_TC_HEAD(stat_err, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test errors from the stat(2) family");
+}
+
+ATF_TC_BODY(stat_err, tc)
+{
+       char buf[NAME_MAX + 1];
+       struct stat st;
+
+       (void)memset(buf, 'x', sizeof(buf));
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EBADF, fstat(-1, &st) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENAMETOOLONG, stat(buf, &st) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENAMETOOLONG, lstat(buf, &st) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EFAULT, stat((void *)-1, &st) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EFAULT, lstat((void *)-1, &st) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EFAULT, stat("/etc/passwd", (void *)-1) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(EFAULT, lstat("/etc/passwd", (void *)-1) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOENT, stat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
+
+       errno = 0;
+       ATF_REQUIRE_ERRNO(ENOENT, lstat("/a/b/c/d/e/f/g/h/i/j/k", &st) == -1);
+}
+
+ATF_TC_WITH_CLEANUP(stat_mtime);
+ATF_TC_HEAD(stat_mtime, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test modification times with stat(2)");
+}
+
+ATF_TC_BODY(stat_mtime, tc)
+{
+       struct stat sa, sb;
+       int fd[3];
+       size_t i;
+
+       for (i = 0; i < __arraycount(fd); i++) {
+
+               (void)memset(&sa, 0, sizeof(struct stat));
+               (void)memset(&sb, 0, sizeof(struct stat));
+
+               fd[i] = open(path, O_WRONLY | O_CREAT);
+
+               ATF_REQUIRE(fd[i] != -1);
+               ATF_REQUIRE(write(fd[i], "X", 1) == 1);
+               ATF_REQUIRE(stat(path, &sa) == 0);
+
+               (void)sleep(1);
+
+               ATF_REQUIRE(write(fd[i], "X", 1) == 1);
+               ATF_REQUIRE(stat(path, &sb) == 0);
+
+               ATF_REQUIRE(close(fd[i]) == 0);
+               ATF_REQUIRE(unlink(path) == 0);
+
+               if (sa.st_mtime == sb.st_mtime)
+                       atf_tc_fail("mtimes did not change");
+       }
+}
+
+ATF_TC_CLEANUP(stat_mtime, tc)
+{
+       (void)unlink(path);
+}
+
+ATF_TC_WITH_CLEANUP(stat_perm);
+ATF_TC_HEAD(stat_perm, tc)
+{
+       atf_tc_set_md_var(tc, "descr", "Test permissions with stat(2)");
+       atf_tc_set_md_var(tc, "require.user", "root");
+}



Home | Main Index | Thread Index | Old Index