etc/services - sync with NetBSD-8
[minix.git] / tests / lib / libc / sys / t_revoke.c
blob10fd6d64a00bd64d42a48a7dba9a6ff9622f397d
1 /* $NetBSD: t_revoke.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_revoke.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $");
34 #include <sys/resource.h>
35 #include <sys/wait.h>
37 #include <atf-c.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
46 static const char path[] = "revoke";
48 ATF_TC_WITH_CLEANUP(revoke_basic);
49 ATF_TC_HEAD(revoke_basic, tc)
51 atf_tc_set_md_var(tc, "descr", "A basic test of revoke(2)");
54 ATF_TC_BODY(revoke_basic, tc)
56 struct rlimit res;
57 char tmp[10];
58 size_t i, n;
59 int *buf;
61 (void)memset(&res, 0, sizeof(struct rlimit));
62 (void)getrlimit(RLIMIT_NOFILE, &res);
64 if ((n = res.rlim_cur / 10) == 0)
65 n = 10;
67 buf = calloc(n, sizeof(int));
68 ATF_REQUIRE(buf != NULL);
70 buf[0] = open(path, O_RDWR | O_CREAT, 0600);
71 ATF_REQUIRE(buf[0] >= 0);
73 for (i = 1; i < n; i++) {
74 buf[i] = open(path, O_RDWR);
75 ATF_REQUIRE(buf[i] >= 0);
78 ATF_REQUIRE(revoke(path) == 0);
80 for (i = 0; i < n; i++) {
82 ATF_REQUIRE(read(buf[i], tmp, sizeof(tmp)) == -1);
84 (void)close(buf[i]);
87 free(buf);
89 (void)unlink(path);
92 ATF_TC_CLEANUP(revoke_basic, tc)
94 (void)unlink(path);
97 ATF_TC(revoke_err);
98 ATF_TC_HEAD(revoke_err, tc)
100 atf_tc_set_md_var(tc, "descr", "Test errors from revoke(2)");
101 atf_tc_set_md_var(tc, "require.user", "unprivileged");
104 ATF_TC_BODY(revoke_err, tc)
106 char buf[1024 + 1]; /* XXX: From the manual page... */
108 (void)memset(buf, 'x', sizeof(buf));
110 errno = 0;
111 ATF_REQUIRE_ERRNO(EFAULT, revoke((char *)-1) == -1);
113 errno = 0;
114 ATF_REQUIRE_ERRNO(ENAMETOOLONG, revoke(buf) == -1);
116 errno = 0;
117 ATF_REQUIRE_ERRNO(EPERM, revoke("/etc/passwd") == -1);
119 errno = 0;
120 ATF_REQUIRE_ERRNO(ENOENT, revoke("/etc/xxx/yyy") == -1);
123 ATF_TC_WITH_CLEANUP(revoke_perm);
124 ATF_TC_HEAD(revoke_perm, tc)
126 atf_tc_set_md_var(tc, "descr", "Test permissions revoke(2)");
127 atf_tc_set_md_var(tc, "require.user", "root");
130 ATF_TC_BODY(revoke_perm, tc)
132 struct passwd *pw;
133 int fd, sta;
134 pid_t pid;
136 pw = getpwnam("nobody");
137 fd = open(path, O_RDWR | O_CREAT, 0600);
139 ATF_REQUIRE(fd >= 0);
140 ATF_REQUIRE(pw != NULL);
141 ATF_REQUIRE(revoke(path) == 0);
143 pid = fork();
144 ATF_REQUIRE(pid >= 0);
146 if (pid == 0) {
148 if (setuid(pw->pw_uid) != 0)
149 _exit(EXIT_FAILURE);
151 errno = 0;
153 if (revoke(path) == 0)
154 _exit(EXIT_FAILURE);
156 if (errno != EACCES)
157 _exit(EXIT_FAILURE);
159 if (close(fd) != 0)
160 _exit(EXIT_FAILURE);
162 _exit(EXIT_SUCCESS);
165 (void)wait(&sta);
167 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
168 atf_tc_fail("revoke(2) did not obey permissions");
170 ATF_REQUIRE(unlink(path) == 0);
173 ATF_TC_CLEANUP(revoke_perm, tc)
175 (void)unlink(path);
178 ATF_TP_ADD_TCS(tp)
181 ATF_TP_ADD_TC(tp, revoke_basic);
182 ATF_TP_ADD_TC(tp, revoke_err);
183 ATF_TP_ADD_TC(tp, revoke_perm);
185 return atf_no_error();