1276478Sngie/* $NetBSD: t_paths.c,v 1.14 2014/11/04 00:20:19 justin Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Jukka Ruohonen. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie#include <sys/cdefs.h> 32276478Sngie__RCSID("$NetBSD: t_paths.c,v 1.14 2014/11/04 00:20:19 justin Exp $"); 33272343Sngie 34272343Sngie#include <sys/param.h> 35272343Sngie#include <sys/stat.h> 36272343Sngie 37272343Sngie#include <errno.h> 38272343Sngie#include <fcntl.h> 39272343Sngie#include <paths.h> 40272343Sngie#include <stdio.h> 41272343Sngie#include <string.h> 42272343Sngie#include <unistd.h> 43272343Sngie 44272343Sngie#include <atf-c.h> 45272343Sngie 46272343Sngie#define PATH_DEV __BIT(0) /* A device node */ 47272343Sngie#define PATH_DIR __BIT(1) /* A directory */ 48272343Sngie#define PATH_FILE __BIT(2) /* A file */ 49272343Sngie#define PATH_ROOT __BIT(3) /* Access for root only */ 50272343Sngie 51272343Sngiestatic const struct { 52272343Sngie const char *path; 53272343Sngie int flags; 54272343Sngie} paths[] = { 55272343Sngie { _PATH_AUDIO, PATH_DEV }, 56272343Sngie { _PATH_AUDIO0, PATH_DEV }, 57272343Sngie { _PATH_AUDIOCTL, PATH_DEV }, 58272343Sngie { _PATH_AUDIOCTL0, PATH_DEV }, 59272343Sngie { _PATH_BPF, PATH_DEV | PATH_ROOT }, 60272343Sngie { _PATH_CLOCKCTL, PATH_DEV | PATH_ROOT }, 61272343Sngie { _PATH_CONSOLE, PATH_DEV | PATH_ROOT }, 62272343Sngie { _PATH_CONSTTY, PATH_DEV | PATH_ROOT }, 63272343Sngie { _PATH_CPUCTL, PATH_DEV }, 64272343Sngie { _PATH_CSMAPPER, PATH_DIR }, 65272343Sngie { _PATH_DEFTAPE, PATH_DEV | PATH_ROOT }, 66272343Sngie { _PATH_DEVCDB, PATH_FILE }, 67272343Sngie { _PATH_DEVDB, PATH_FILE }, 68272343Sngie { _PATH_DEVNULL, PATH_DEV }, 69272343Sngie { _PATH_DRUM, PATH_DEV | PATH_ROOT }, 70272343Sngie { _PATH_ESDB, PATH_DIR }, 71272343Sngie { _PATH_FTPUSERS, PATH_FILE }, 72272343Sngie { _PATH_GETTYTAB, PATH_FILE }, 73272343Sngie { _PATH_I18NMODULE, PATH_DIR }, 74272343Sngie { _PATH_ICONV, PATH_DIR }, 75272343Sngie { _PATH_KMEM, PATH_DEV | PATH_ROOT }, 76272343Sngie { _PATH_KSYMS, PATH_DEV }, 77272343Sngie { _PATH_KVMDB, PATH_FILE }, 78272343Sngie { _PATH_LOCALE, PATH_DIR }, 79272343Sngie { _PATH_MAILDIR, PATH_DIR }, 80272343Sngie { _PATH_MAN, PATH_DIR }, 81272343Sngie { _PATH_MEM, PATH_DEV | PATH_ROOT }, 82272343Sngie { _PATH_MIXER, PATH_DEV }, 83272343Sngie { _PATH_MIXER0, PATH_DEV }, 84272343Sngie { _PATH_NOLOGIN, PATH_FILE }, 85272343Sngie { _PATH_POWER, PATH_DEV | PATH_ROOT }, 86272343Sngie { _PATH_PRINTCAP, PATH_FILE }, 87272343Sngie { _PATH_PUD, PATH_DEV | PATH_ROOT }, 88272343Sngie { _PATH_PUFFS, PATH_DEV | PATH_ROOT }, 89272343Sngie { _PATH_RANDOM, PATH_DEV }, 90272343Sngie { _PATH_SENDMAIL, PATH_FILE }, 91272343Sngie { _PATH_SHELLS, PATH_FILE }, 92272343Sngie { _PATH_SKEYKEYS, PATH_FILE | PATH_ROOT }, 93272343Sngie { _PATH_SOUND, PATH_DEV }, 94272343Sngie { _PATH_SOUND0, PATH_DEV }, 95272343Sngie { _PATH_SYSMON, PATH_DEV }, 96272343Sngie { _PATH_TTY, PATH_DEV }, 97272343Sngie { _PATH_UNIX, PATH_FILE | PATH_ROOT }, 98272343Sngie { _PATH_URANDOM, PATH_DEV }, 99272343Sngie { _PATH_VIDEO, PATH_DEV }, 100272343Sngie { _PATH_VIDEO0, PATH_DEV }, 101272343Sngie 102272343Sngie { _PATH_DEV, PATH_DIR }, 103272343Sngie { _PATH_DEV_PTS, PATH_DIR }, 104272343Sngie { _PATH_EMUL_AOUT, PATH_DIR }, 105272343Sngie { _PATH_TMP, PATH_DIR }, 106272343Sngie { _PATH_VARDB, PATH_DIR }, 107272343Sngie { _PATH_VARRUN, PATH_DIR }, 108272343Sngie { _PATH_VARTMP, PATH_DIR }, 109272343Sngie 110272343Sngie { _PATH_BSHELL, PATH_FILE }, 111272343Sngie { _PATH_CSHELL, PATH_FILE }, 112272343Sngie { _PATH_VI, PATH_FILE }, 113272343Sngie}; 114272343Sngie 115272343SngieATF_TC(paths); 116272343SngieATF_TC_HEAD(paths, tc) 117272343Sngie{ 118272343Sngie atf_tc_set_md_var(tc, "descr", "A basic test for <paths.h>"); 119272343Sngie} 120272343Sngie 121272343SngieATF_TC_BODY(paths, tc) 122272343Sngie{ 123272343Sngie struct stat st; 124272343Sngie uid_t uid; 125272343Sngie mode_t m; 126272343Sngie size_t i; 127272343Sngie int fd; 128272343Sngie 129272343Sngie#if defined(__sparc__) 130272343Sngie atf_tc_skip("PR port-sparc/45580"); 131272343Sngie#endif 132272343Sngie 133272343Sngie uid = getuid(); 134272343Sngie 135272343Sngie for (i = 0; i < __arraycount(paths); i++) { 136272343Sngie 137272343Sngie (void)fprintf(stderr, "testing '%s'\n", paths[i].path); 138272343Sngie 139272343Sngie errno = 0; 140272343Sngie fd = open(paths[i].path, O_RDONLY); 141272343Sngie 142272343Sngie if (fd < 0) { 143272343Sngie 144272343Sngie switch (errno) { 145272343Sngie 146272343Sngie case EPERM: /* FALLTHROUGH */ 147272343Sngie case EACCES: /* FALLTHROUGH */ 148272343Sngie 149272343Sngie if ((paths[i].flags & PATH_ROOT) == 0) { 150272343Sngie 151272343Sngie atf_tc_fail("UID %u failed to open %s", 152272343Sngie (uint32_t)uid, paths[i].path); 153272343Sngie } 154272343Sngie 155272343Sngie case EBUSY: /* FALLTHROUGH */ 156272343Sngie case ENXIO: /* FALLTHROUGH */ 157272343Sngie case ENOENT: /* FALLTHROUGH */ 158272343Sngie 159272343Sngie default: 160272343Sngie continue; 161272343Sngie } 162272343Sngie } 163272343Sngie 164272343Sngie (void)memset(&st, 0, sizeof(struct stat)); 165272343Sngie 166272343Sngie ATF_REQUIRE(fstat(fd, &st) == 0); 167272343Sngie 168272343Sngie m = st.st_mode; 169272343Sngie 170272343Sngie if ((paths[i].flags & PATH_DEV) != 0) { 171272343Sngie 172272343Sngie ATF_CHECK(S_ISBLK(m) != 0 || S_ISCHR(m) != 0); 173272343Sngie 174272343Sngie ATF_CHECK((paths[i].flags & PATH_DIR) == 0); 175272343Sngie ATF_CHECK((paths[i].flags & PATH_FILE) == 0); 176272343Sngie } 177272343Sngie 178272343Sngie if ((paths[i].flags & PATH_DIR) != 0) { 179272343Sngie 180272343Sngie ATF_CHECK(S_ISDIR(m) != 0); 181272343Sngie 182272343Sngie ATF_CHECK((paths[i].flags & PATH_DEV) == 0); 183272343Sngie ATF_CHECK((paths[i].flags & PATH_FILE) == 0); 184272343Sngie } 185272343Sngie 186272343Sngie if ((paths[i].flags & PATH_FILE) != 0) { 187272343Sngie 188272343Sngie ATF_CHECK(S_ISREG(m) != 0); 189272343Sngie 190272343Sngie ATF_CHECK((paths[i].flags & PATH_DEV) == 0); 191272343Sngie ATF_CHECK((paths[i].flags & PATH_DIR) == 0); 192272343Sngie } 193272343Sngie 194272343Sngie ATF_REQUIRE(close(fd) == 0); 195272343Sngie } 196272343Sngie} 197272343Sngie 198272343SngieATF_TP_ADD_TCS(tp) 199272343Sngie{ 200272343Sngie 201272343Sngie ATF_TP_ADD_TC(tp, paths); 202272343Sngie 203272343Sngie return atf_no_error(); 204272343Sngie} 205