Source-Changes-HG archive

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

[src/trunk]: src Add ATF KERN_PROC_CWD tests



details:   https://anonhg.NetBSD.org/src/rev/cadb30e66a9e
branches:  trunk
changeset: 841821:cadb30e66a9e
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sat Jun 01 19:49:02 2019 +0000

description:
Add ATF KERN_PROC_CWD tests

diffstat:

 distrib/sets/lists/debug/mi |    3 +-
 distrib/sets/lists/tests/mi |    3 +-
 tests/kernel/Makefile       |    3 +-
 tests/kernel/t_proccwd.c    |  154 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 160 insertions(+), 3 deletions(-)

diffs (209 lines):

diff -r ab9a4e646d48 -r cadb30e66a9e distrib/sets/lists/debug/mi
--- a/distrib/sets/lists/debug/mi       Sat Jun 01 19:48:29 2019 +0000
+++ b/distrib/sets/lists/debug/mi       Sat Jun 01 19:49:02 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.283 2019/04/26 08:52:16 maya Exp $
+# $NetBSD: mi,v 1.284 2019/06/01 19:49:02 kamil Exp $
 ./etc/mtree/set.debug                           comp-sys-root
 ./usr/lib                                      comp-sys-usr            compatdir
 ./usr/lib/i18n/libBIG5_g.a                     comp-c-debuglib         debuglib,compatfile
@@ -1742,6 +1742,7 @@
 ./usr/libdata/debug/usr/tests/kernel/t_poll3w.debug                    tests-obsolete          obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_pollts.debug                    tests-obsolete          obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_posix_fadvise.debug             tests-obsolete          obsolete,compattestfile
+./usr/libdata/debug/usr/tests/kernel/t_proccwd.debug                   tests-kernel-tests      debug,atf,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_ptrace.debug                    tests-obsolete          obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_ptrace_wait.debug               tests-obsolete          obsolete,compattestfile
 ./usr/libdata/debug/usr/tests/kernel/t_ptrace_wait3.debug              tests-obsolete          obsolete,compattestfile
diff -r ab9a4e646d48 -r cadb30e66a9e distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sat Jun 01 19:48:29 2019 +0000
+++ b/distrib/sets/lists/tests/mi       Sat Jun 01 19:49:02 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.814 2019/05/26 21:04:56 christos Exp $
+# $NetBSD: mi,v 1.815 2019/06/01 19:49:02 kamil Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -2191,6 +2191,7 @@
 ./usr/tests/kernel/t_pollts                    tests-obsolete          obsolete
 ./usr/tests/kernel/t_posix_fadvise             tests-obsolete          obsolete
 ./usr/tests/kernel/t_posix_fallocate           tests-obsolete          obsolete
+./usr/tests/kernel/t_proccwd                   tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/t_procpath                  tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/t_ps_strings                        tests-kernel-tests      compattestfile,atf
 ./usr/tests/kernel/t_ptrace                    tests-obsolete          obsolete
diff -r ab9a4e646d48 -r cadb30e66a9e tests/kernel/Makefile
--- a/tests/kernel/Makefile     Sat Jun 01 19:48:29 2019 +0000
+++ b/tests/kernel/Makefile     Sat Jun 01 19:49:02 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.58 2019/02/04 05:24:18 mrg Exp $
+# $NetBSD: Makefile,v 1.59 2019/06/01 19:49:02 kamil Exp $
 
 NOMAN=         # defined
 
@@ -11,6 +11,7 @@
 TESTS_C+=      t_lockf
 TESTS_C+=      t_pty
 TESTS_C+=      t_mqueue
+TESTS_C+=      t_proccwd
 TESTS_C+=      t_sysv
 TESTS_C+=      t_subr_prf
 TESTS_C+=      t_kauth_pr_47598
diff -r ab9a4e646d48 -r cadb30e66a9e tests/kernel/t_proccwd.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/kernel/t_proccwd.c  Sat Jun 01 19:49:02 2019 +0000
@@ -0,0 +1,154 @@
+/*     $NetBSD: t_proccwd.c,v 1.1 2019/06/01 19:49:02 kamil Exp $      */
+/*-
+ * Copyright (c) 2019 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * 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>
+__COPYRIGHT("@(#) Copyright (c) 2019\
+ The NetBSD Foundation, inc. All rights reserved.");
+__RCSID("$NetBSD: t_proccwd.c,v 1.1 2019/06/01 19:49:02 kamil Exp $");
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <err.h>
+
+#include <atf-c.h>
+
+static int
+getproccwd(char *path, size_t *len, pid_t pid)
+{
+       const int name[] = {
+           CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_CWD,
+       };
+
+       return sysctl(name, __arraycount(name), path, len, NULL, 0);
+}
+
+ATF_TC(prompt_pid);
+ATF_TC_HEAD(prompt_pid, tc)
+{
+
+        atf_tc_set_md_var(tc, "descr",
+            "Prompt length of the current dir and assert that it is sane");
+}
+
+ATF_TC_BODY(prompt_pid, tc)
+{
+       char buf[MAXPATHLEN];
+       char cwdbuf[MAXPATHLEN];
+       size_t len, prompted_len;
+       size_t i;
+
+       pid_t t[] = {
+               -1,
+               getpid(),
+               1 /* /sbin/init */
+       };
+
+       for (i = 0; i < __arraycount(t); i++) {
+               len = 0;
+               ATF_REQUIRE_EQ(getproccwd(NULL, &len, t[i]), 0);
+
+               prompted_len = len;
+               ATF_REQUIRE_EQ(getproccwd(buf, &len, t[i]), 0);
+
+               ATF_REQUIRE_EQ(strlen(buf) + 1, prompted_len);
+               ATF_REQUIRE(strlen(buf) > 0);
+
+               if (t[i] == -1 || t[i] == getpid() || t[i] == getppid()) {
+                       getcwd(cwdbuf, MAXPATHLEN);
+                       ATF_REQUIRE_EQ(strcmp(buf, cwdbuf), 0);
+                       ATF_REQUIRE(strlen(buf) > strlen("/"));
+               } else if (t[i] == 1) {
+                       ATF_REQUIRE_EQ(strcmp(buf, "/"), 0);
+               }
+       }
+}
+
+ATF_TC(chroot);
+ATF_TC_HEAD(chroot, tc)
+{
+
+        atf_tc_set_md_var(tc, "descr",
+            "prompt length of the current dir and assert that it is right");
+
+       atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+#define ASSERT(x) do { if (!(x)) _exit(EXIT_FAILURE); } while (0)
+
+ATF_TC_BODY(chroot, tc)
+{
+       char buf[MAXPATHLEN];
+       struct stat root_dir;
+       struct stat cur_dir;
+       size_t len;
+       pid_t child, wpid;
+       int status;
+       int rv;
+
+       const pid_t pid_one = 1; /* /sbin/init */
+
+       ATF_REQUIRE(getcwd(buf, sizeof(buf)) != NULL);
+
+       ATF_REQUIRE_EQ(stat(buf, &cur_dir), 0);
+       ATF_REQUIRE_EQ(stat("/", &root_dir), 0);
+
+       if (cur_dir.st_ino == root_dir.st_ino)
+               atf_tc_skip("This test does not work in /");
+
+       child = atf_utils_fork();
+       if (child == 0) {
+               len = 0;
+
+               ASSERT(chroot(buf) == 0);
+
+               errno = 0;
+               rv = getproccwd(NULL, &len, pid_one);
+               ASSERT(rv == -1);
+               ASSERT(errno == ENOENT);
+
+               _exit(EXIT_SUCCESS);
+       }
+       wpid = waitpid(child, &status, WEXITED);
+       ATF_REQUIRE_EQ(wpid, child);
+       ATF_REQUIRE_EQ(WEXITSTATUS(status), EXIT_SUCCESS);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+       ATF_TP_ADD_TC(tp, prompt_pid);
+       ATF_TP_ADD_TC(tp, chroot);
+
+       return atf_no_error();
+}



Home | Main Index | Thread Index | Old Index