1 /* $NetBSD: t_revoke.c,v 1.1 2011/07/07 06:57:54 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
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
)
61 (void)memset(&res
, 0, sizeof(struct rlimit
));
62 (void)getrlimit(RLIMIT_NOFILE
, &res
);
64 if ((n
= res
.rlim_cur
/ 10) == 0)
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);
92 ATF_TC_CLEANUP(revoke_basic
, tc
)
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
));
111 ATF_REQUIRE_ERRNO(EFAULT
, revoke((char *)-1) == -1);
114 ATF_REQUIRE_ERRNO(ENAMETOOLONG
, revoke(buf
) == -1);
117 ATF_REQUIRE_ERRNO(EPERM
, revoke("/etc/passwd") == -1);
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
)
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);
144 ATF_REQUIRE(pid
>= 0);
148 if (setuid(pw
->pw_uid
) != 0)
153 if (revoke(path
) == 0)
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
)
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();