diff options
| author | kamil <kamil@NetBSD.org> | 2019-06-01 19:49:02 +0000 |
|---|---|---|
| committer | kamil <kamil@NetBSD.org> | 2019-06-01 19:49:02 +0000 |
| commit | f9f447b6b05f4ca7f677c9e5d16cced49ada684f (patch) | |
| tree | 97aefff989c0e7e9fca3d7541030d1f09841a85d /tests/kernel | |
| parent | eb0d006abd4589b7ecd783750d10f458631a1e44 (diff) | |
Add ATF KERN_PROC_CWD tests
Diffstat (limited to 'tests/kernel')
| -rw-r--r-- | tests/kernel/Makefile | 3 | ||||
| -rw-r--r-- | tests/kernel/t_proccwd.c | 154 |
2 files changed, 156 insertions, 1 deletions
diff --git a/tests/kernel/Makefile b/tests/kernel/Makefile index 3d253d33d81..5214c7e6cb8 100644 --- a/tests/kernel/Makefile +++ b/tests/kernel/Makefile @@ -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 diff --git a/tests/kernel/t_proccwd.c b/tests/kernel/t_proccwd.c new file mode 100644 index 00000000000..abb52d9e289 --- /dev/null +++ b/tests/kernel/t_proccwd.c @@ -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(); +} |
