Module Name: src Committed By: kamil Date: Sat Jun 1 19:49:03 UTC 2019
Modified Files: src/distrib/sets/lists/debug: mi src/distrib/sets/lists/tests: mi src/tests/kernel: Makefile Added Files: src/tests/kernel: t_proccwd.c Log Message: Add ATF KERN_PROC_CWD tests To generate a diff of this commit: cvs rdiff -u -r1.283 -r1.284 src/distrib/sets/lists/debug/mi cvs rdiff -u -r1.814 -r1.815 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.58 -r1.59 src/tests/kernel/Makefile cvs rdiff -u -r0 -r1.1 src/tests/kernel/t_proccwd.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/debug/mi diff -u src/distrib/sets/lists/debug/mi:1.283 src/distrib/sets/lists/debug/mi:1.284 --- src/distrib/sets/lists/debug/mi:1.283 Fri Apr 26 08:52:16 2019 +++ src/distrib/sets/lists/debug/mi Sat Jun 1 19:49:02 2019 @@ -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 Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.814 src/distrib/sets/lists/tests/mi:1.815 --- src/distrib/sets/lists/tests/mi:1.814 Sun May 26 21:04:56 2019 +++ src/distrib/sets/lists/tests/mi Sat Jun 1 19:49:02 2019 @@ -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 Index: src/tests/kernel/Makefile diff -u src/tests/kernel/Makefile:1.58 src/tests/kernel/Makefile:1.59 --- src/tests/kernel/Makefile:1.58 Mon Feb 4 05:24:18 2019 +++ src/tests/kernel/Makefile Sat Jun 1 19:49:02 2019 @@ -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_lock 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 Added files: Index: src/tests/kernel/t_proccwd.c diff -u /dev/null src/tests/kernel/t_proccwd.c:1.1 --- /dev/null Sat Jun 1 19:49:03 2019 +++ src/tests/kernel/t_proccwd.c Sat Jun 1 19:49:02 2019 @@ -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(); +}