t_paths.c revision 1.5 1 /* $NetBSD: t_paths.c,v 1.5 2011/04/04 19:59:08 jruoho Exp $ */
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>
32 __RCSID("$NetBSD: t_paths.c,v 1.5 2011/04/04 19:59:08 jruoho Exp $");
33
34 #include <sys/param.h>
35 #include <sys/stat.h>
36
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <paths.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <atf-c.h>
44
45 #define PATH_DEV __BIT(0) /* A device node */
46 #define PATH_DIR __BIT(1) /* A directory */
47 #define PATH_FILE __BIT(2) /* A file */
48 #define PATH_ROOT __BIT(3) /* Access for root only */
49
50 static const struct {
51 const char *path;
52 int flags;
53 } paths[] = {
54 { _PATH_AUDIO, PATH_DEV },
55 { _PATH_AUDIO0, PATH_DEV },
56 { _PATH_AUDIOCTL, PATH_DEV },
57 { _PATH_AUDIOCTL0, PATH_DEV },
58 { _PATH_BPF, PATH_DEV | PATH_ROOT },
59 { _PATH_CLOCKCTL, PATH_DEV | PATH_ROOT },
60 { _PATH_CONSOLE, PATH_DEV | PATH_ROOT },
61 { _PATH_CONSTTY, PATH_DEV | PATH_ROOT },
62 { _PATH_CSMAPPER, PATH_DIR },
63 { _PATH_DEFTAPE, PATH_DEV | PATH_ROOT },
64 { _PATH_DEVDB, PATH_FILE },
65 { _PATH_DEVNULL, PATH_DEV },
66 { _PATH_DRUM, PATH_DEV | PATH_ROOT },
67 { _PATH_ESDB, PATH_DIR },
68 { _PATH_FTPUSERS, PATH_FILE },
69 { _PATH_GETTYTAB, PATH_FILE },
70 { _PATH_I18NMODULE, PATH_DIR },
71 { _PATH_ICONV, PATH_DIR },
72 { _PATH_KMEM, PATH_DEV | PATH_ROOT },
73 { _PATH_KSYMS, PATH_DEV },
74 { _PATH_KVMDB, PATH_FILE },
75 { _PATH_LOCALE, PATH_DIR },
76 { _PATH_MAILDIR, PATH_DIR },
77 { _PATH_MAN, PATH_DIR },
78 { _PATH_MEM, PATH_DEV | PATH_ROOT },
79 { _PATH_MIXER, PATH_DEV },
80 { _PATH_MIXER0, PATH_DEV },
81 { _PATH_NOLOGIN, PATH_FILE },
82 { _PATH_POWER, PATH_DEV },
83 { _PATH_PRINTCAP, PATH_FILE },
84 { _PATH_PUD, PATH_DEV | PATH_ROOT },
85 { _PATH_PUFFS, PATH_DEV | PATH_ROOT },
86 { _PATH_RANDOM, PATH_DEV },
87 { _PATH_SENDMAIL, PATH_FILE },
88 { _PATH_SHELLS, PATH_FILE },
89 { _PATH_SKEYKEYS, PATH_FILE | PATH_ROOT },
90 { _PATH_SOUND, PATH_DEV },
91 { _PATH_SOUND0, PATH_DEV },
92 { _PATH_SYSMON, PATH_DEV },
93 { _PATH_TTY, PATH_DEV },
94 { _PATH_UNIX, PATH_FILE },
95 { _PATH_URANDOM, PATH_DEV },
96 { _PATH_VIDEO, PATH_DEV },
97 { _PATH_VIDEO0, PATH_DEV },
98
99 { _PATH_DEV, PATH_DIR },
100 { _PATH_DEV_PTS, PATH_DIR },
101 { _PATH_EMUL_AOUT, PATH_DIR },
102 { _PATH_TMP, PATH_DIR },
103 { _PATH_VARDB, PATH_DIR },
104 { _PATH_VARRUN, PATH_DIR },
105 { _PATH_VARTMP, PATH_DIR },
106
107 { _PATH_BSHELL, PATH_FILE },
108 { _PATH_CSHELL, PATH_FILE },
109 { _PATH_VI, PATH_FILE },
110 };
111
112 ATF_TC(paths);
113 ATF_TC_HEAD(paths, tc)
114 {
115 atf_tc_set_md_var(tc, "descr", "A basic test for <paths.h>");
116 }
117
118 ATF_TC_BODY(paths, tc)
119 {
120 struct stat st;
121 uid_t uid;
122 mode_t m;
123 size_t i;
124 int fd;
125
126 uid = getuid();
127
128 for (i = 0; i < __arraycount(paths); i++) {
129
130 errno = 0;
131 fd = open(paths[i].path, O_RDONLY);
132
133 if (fd < 0) {
134
135 switch (errno) {
136
137 case EPERM: /* FALLTHROUGH */
138 case EACCES: /* FALLTHROUGH */
139
140 if ((paths[i].flags & PATH_ROOT) == 0) {
141
142 atf_tc_fail("UID %u failed to open %s",
143 (uint32_t)uid, paths[i].path);
144 }
145
146 case EBUSY: /* FALLTHROUGH */
147 case ENXIO: /* FALLTHROUGH */
148 case ENOENT: /* FALLTHROUGH */
149
150 default:
151 continue;
152 }
153 }
154
155 (void)memset(&st, 0, sizeof(struct stat));
156
157 ATF_REQUIRE(fstat(fd, &st) == 0);
158
159 m = st.st_mode;
160
161 if ((paths[i].flags & PATH_DEV) != 0) {
162
163 if (strcmp(paths[i].path, _PATH_BPF) == 0)
164 atf_tc_expect_fail("PR lib/44807");
165
166 ATF_REQUIRE(S_ISBLK(m) != 0 || S_ISCHR(m) != 0);
167
168 ATF_REQUIRE((paths[i].flags & PATH_DIR) == 0);
169 ATF_REQUIRE((paths[i].flags & PATH_FILE) == 0);
170 }
171
172 if ((paths[i].flags & PATH_DIR) != 0) {
173
174 ATF_REQUIRE(S_ISDIR(m) != 0);
175
176 ATF_REQUIRE((paths[i].flags & PATH_DEV) == 0);
177 ATF_REQUIRE((paths[i].flags & PATH_FILE) == 0);
178 }
179
180 if ((paths[i].flags & PATH_FILE) != 0) {
181
182 ATF_REQUIRE(S_ISREG(m) != 0);
183
184 ATF_REQUIRE((paths[i].flags & PATH_DEV) == 0);
185 ATF_REQUIRE((paths[i].flags & PATH_DIR) == 0);
186 }
187
188 ATF_REQUIRE(close(fd) == 0);
189 }
190 }
191
192 ATF_TP_ADD_TCS(tp)
193 {
194
195 ATF_TP_ADD_TC(tp, paths);
196
197 return atf_no_error();
198 }
199