[BACK]Return to t_paths.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / tests / include

Annotation of src/tests/include/t_paths.c, Revision 1.17

1.17    ! mrg         1: /*     $NetBSD: t_paths.c,v 1.16 2015/05/07 06:23:23 pgoyette Exp $ */
1.1       jruoho      2:
                      3: /*-
                      4:  * Copyright (c) 2011 The NetBSD Foundation, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Jukka Ruohonen.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31: #include <sys/cdefs.h>
1.17    ! mrg        32: __RCSID("$NetBSD: t_paths.c,v 1.16 2015/05/07 06:23:23 pgoyette Exp $");
1.1       jruoho     33:
                     34: #include <sys/param.h>
                     35: #include <sys/stat.h>
                     36:
                     37: #include <errno.h>
                     38: #include <fcntl.h>
                     39: #include <paths.h>
1.10      jruoho     40: #include <stdio.h>
1.1       jruoho     41: #include <string.h>
                     42: #include <unistd.h>
                     43:
                     44: #include <atf-c.h>
                     45:
                     46: #define PATH_DEV       __BIT(0)        /* A device node        */
                     47: #define PATH_DIR       __BIT(1)        /* A directory          */
                     48: #define PATH_FILE      __BIT(2)        /* A file               */
                     49: #define PATH_ROOT      __BIT(3)        /* Access for root only */
1.15      pgoyette   50: #define PATH_OPT       __BIT(3)        /* Optional, ENODEV if not supported */
1.1       jruoho     51:
                     52: static const struct {
                     53:        const char      *path;
                     54:        int              flags;
                     55: } paths[] = {
                     56:        { _PATH_AUDIO,          PATH_DEV                },
                     57:        { _PATH_AUDIO0,         PATH_DEV                },
                     58:        { _PATH_AUDIOCTL,       PATH_DEV                },
                     59:        { _PATH_AUDIOCTL0,      PATH_DEV                },
                     60:        { _PATH_BPF,            PATH_DEV | PATH_ROOT    },
                     61:        { _PATH_CLOCKCTL,       PATH_DEV | PATH_ROOT    },
1.3       jruoho     62:        { _PATH_CONSOLE,        PATH_DEV | PATH_ROOT    },
1.1       jruoho     63:        { _PATH_CONSTTY,        PATH_DEV | PATH_ROOT    },
1.9       jruoho     64:        { _PATH_CPUCTL,         PATH_DEV                },
1.1       jruoho     65:        { _PATH_CSMAPPER,       PATH_DIR                },
                     66:        { _PATH_DEFTAPE,        PATH_DEV | PATH_ROOT    },
1.12      joerg      67:        { _PATH_DEVCDB,         PATH_FILE               },
1.1       jruoho     68:        { _PATH_DEVDB,          PATH_FILE               },
                     69:        { _PATH_DEVNULL,        PATH_DEV                },
                     70:        { _PATH_DRUM,           PATH_DEV | PATH_ROOT    },
                     71:        { _PATH_ESDB,           PATH_DIR                },
                     72:        { _PATH_FTPUSERS,       PATH_FILE               },
                     73:        { _PATH_GETTYTAB,       PATH_FILE               },
                     74:        { _PATH_I18NMODULE,     PATH_DIR                },
                     75:        { _PATH_ICONV,          PATH_DIR                },
                     76:        { _PATH_KMEM,           PATH_DEV | PATH_ROOT    },
                     77:        { _PATH_KSYMS,          PATH_DEV                },
                     78:        { _PATH_KVMDB,          PATH_FILE               },
                     79:        { _PATH_LOCALE,         PATH_DIR                },
                     80:        { _PATH_MAILDIR,        PATH_DIR                },
                     81:        { _PATH_MAN,            PATH_DIR                },
                     82:        { _PATH_MEM,            PATH_DEV | PATH_ROOT    },
                     83:        { _PATH_MIXER,          PATH_DEV                },
                     84:        { _PATH_MIXER0,         PATH_DEV                },
                     85:        { _PATH_NOLOGIN,        PATH_FILE               },
1.15      pgoyette   86:        { _PATH_POWER,          PATH_DEV | PATH_ROOT | PATH_OPT },
1.1       jruoho     87:        { _PATH_PRINTCAP,       PATH_FILE               },
                     88:        { _PATH_PUD,            PATH_DEV | PATH_ROOT    },
                     89:        { _PATH_PUFFS,          PATH_DEV | PATH_ROOT    },
                     90:        { _PATH_RANDOM,         PATH_DEV                },
                     91:        { _PATH_SENDMAIL,       PATH_FILE               },
                     92:        { _PATH_SHELLS,         PATH_FILE               },
                     93:        { _PATH_SKEYKEYS,       PATH_FILE | PATH_ROOT   },
                     94:        { _PATH_SOUND,          PATH_DEV                },
                     95:        { _PATH_SOUND0,         PATH_DEV                },
1.15      pgoyette   96:        { _PATH_SYSMON,         PATH_DEV | PATH_OPT     },
1.1       jruoho     97:        { _PATH_TTY,            PATH_DEV                },
1.9       jruoho     98:        { _PATH_UNIX,           PATH_FILE | PATH_ROOT   },
1.1       jruoho     99:        { _PATH_URANDOM,        PATH_DEV                },
                    100:        { _PATH_VIDEO,          PATH_DEV                },
                    101:        { _PATH_VIDEO0,         PATH_DEV                },
1.15      pgoyette  102:        { _PATH_WATCHDOG,       PATH_DEV | PATH_OPT     },
1.1       jruoho    103:
                    104:        { _PATH_DEV,            PATH_DIR                },
                    105:        { _PATH_DEV_PTS,        PATH_DIR                },
                    106:        { _PATH_EMUL_AOUT,      PATH_DIR                },
                    107:        { _PATH_TMP,            PATH_DIR                },
                    108:        { _PATH_VARDB,          PATH_DIR                },
                    109:        { _PATH_VARRUN,         PATH_DIR                },
                    110:        { _PATH_VARTMP,         PATH_DIR                },
                    111:
                    112:        { _PATH_BSHELL,         PATH_FILE               },
                    113:        { _PATH_CSHELL,         PATH_FILE               },
                    114:        { _PATH_VI,             PATH_FILE               },
                    115: };
                    116:
                    117: ATF_TC(paths);
                    118: ATF_TC_HEAD(paths, tc)
                    119: {
                    120:        atf_tc_set_md_var(tc, "descr", "A basic test for <paths.h>");
                    121: }
                    122:
                    123: ATF_TC_BODY(paths, tc)
                    124: {
                    125:        struct stat st;
                    126:        uid_t uid;
                    127:        mode_t m;
                    128:        size_t i;
                    129:        int fd;
                    130:
1.13      jmmv      131: #if defined(__sparc__)
                    132:        atf_tc_skip("PR port-sparc/45580");
                    133: #endif
1.11      jruoho    134:
1.1       jruoho    135:        uid = getuid();
                    136:
                    137:        for (i = 0; i < __arraycount(paths); i++) {
                    138:
1.10      jruoho    139:                (void)fprintf(stderr, "testing '%s'\n", paths[i].path);
                    140:
1.1       jruoho    141:                errno = 0;
                    142:                fd = open(paths[i].path, O_RDONLY);
                    143:
                    144:                if (fd < 0) {
                    145:
                    146:                        switch (errno) {
                    147:
1.15      pgoyette  148:                        case ENODEV:
                    149:                                if ((paths[i].flags & PATH_OPT) == 0) {
                    150:
                    151:                                        atf_tc_fail("Required path %s does "
                    152:                                            "not exist", paths[i].path);
                    153:                                }
                    154:                                break;
                    155:
1.1       jruoho    156:                        case EPERM:     /* FALLTHROUGH */
                    157:                        case EACCES:    /* FALLTHROUGH */
                    158:
                    159:                                if ((paths[i].flags & PATH_ROOT) == 0) {
                    160:
1.15      pgoyette  161:                                        atf_tc_fail("UID %u failed to open %s, "
                    162:                                            "error %d", (uint32_t)uid,
1.16      pgoyette  163:                                             paths[i].path, errno);
1.1       jruoho    164:                                }
                    165:
1.17    ! mrg       166:                                /* FALLTHROUGH */
1.1       jruoho    167:                        case EBUSY:     /* FALLTHROUGH */
                    168:                        case ENXIO:     /* FALLTHROUGH */
                    169:                        case ENOENT:    /* FALLTHROUGH */
                    170:
                    171:                        default:
                    172:                                continue;
                    173:                        }
                    174:                }
                    175:
                    176:                (void)memset(&st, 0, sizeof(struct stat));
                    177:
                    178:                ATF_REQUIRE(fstat(fd, &st) == 0);
                    179:
                    180:                m = st.st_mode;
                    181:
                    182:                if ((paths[i].flags & PATH_DEV) != 0) {
                    183:
1.6       njoly     184:                        ATF_CHECK(S_ISBLK(m) != 0 || S_ISCHR(m) != 0);
1.1       jruoho    185:
1.6       njoly     186:                        ATF_CHECK((paths[i].flags & PATH_DIR) == 0);
                    187:                        ATF_CHECK((paths[i].flags & PATH_FILE) == 0);
1.1       jruoho    188:                }
                    189:
                    190:                if ((paths[i].flags & PATH_DIR) != 0) {
                    191:
1.6       njoly     192:                        ATF_CHECK(S_ISDIR(m) != 0);
1.1       jruoho    193:
1.6       njoly     194:                        ATF_CHECK((paths[i].flags & PATH_DEV) == 0);
                    195:                        ATF_CHECK((paths[i].flags & PATH_FILE) == 0);
1.1       jruoho    196:                }
                    197:
                    198:                if ((paths[i].flags & PATH_FILE) != 0) {
                    199:
1.6       njoly     200:                        ATF_CHECK(S_ISREG(m) != 0);
1.1       jruoho    201:
1.6       njoly     202:                        ATF_CHECK((paths[i].flags & PATH_DEV) == 0);
                    203:                        ATF_CHECK((paths[i].flags & PATH_DIR) == 0);
1.1       jruoho    204:                }
                    205:
                    206:                ATF_REQUIRE(close(fd) == 0);
                    207:        }
                    208: }
                    209:
                    210: ATF_TP_ADD_TCS(tp)
                    211: {
                    212:
                    213:        ATF_TP_ADD_TC(tp, paths);
                    214:
                    215:        return atf_no_error();
                    216: }

CVSweb <webmaster@jp.NetBSD.org>