xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_unlink.c (revision 20a4ce8bf215487ce53d30ccae3bb6c86babb5a2)
1 /* $NetBSD: t_unlink.c,v 1.2 2014/04/21 18:05:17 martin 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_unlink.c,v 1.2 2014/04/21 18:05:17 martin Exp $");
33 
34 #include <sys/stat.h>
35 
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 static char	 path[] = "unlink";
44 
45 ATF_TC_WITH_CLEANUP(unlink_basic);
46 ATF_TC_HEAD(unlink_basic, tc)
47 {
48 	atf_tc_set_md_var(tc, "descr", "A basic test of unlink(2)");
49 }
50 
51 ATF_TC_BODY(unlink_basic, tc)
52 {
53 	const size_t n = 512;
54 	size_t i;
55 	int fd;
56 
57 	for (i = 0; i < n; i++) {
58 
59 		fd = open(path, O_RDWR | O_CREAT, 0666);
60 
61 		ATF_REQUIRE(fd != -1);
62 		ATF_REQUIRE(close(fd) == 0);
63 		ATF_REQUIRE(unlink(path) == 0);
64 
65 		errno = 0;
66 #ifdef	__FreeBSD__
67 		ATF_REQUIRE_ERRNO(ENOENT, (fd = open(path, O_RDONLY)) == -1);
68 		(void)close(fd);
69 #else
70 		ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
71 #endif
72 	}
73 }
74 
75 ATF_TC_CLEANUP(unlink_basic, tc)
76 {
77 	(void)unlink(path);
78 }
79 
80 ATF_TC_WITH_CLEANUP(unlink_err);
81 ATF_TC_HEAD(unlink_err, tc)
82 {
83 	atf_tc_set_md_var(tc, "descr", "Test error conditions of unlink(2)");
84 }
85 
86 ATF_TC_BODY(unlink_err, tc)
87 {
88 	char buf[PATH_MAX + 1];
89 
90 	(void)memset(buf, 'x', sizeof(buf));
91 
92 	errno = 0;
93 #ifdef __FreeBSD__
94 	ATF_REQUIRE_ERRNO(EISDIR, unlink("/") == -1);
95 #else
96 	ATF_REQUIRE_ERRNO(EBUSY, unlink("/") == -1);
97 #endif
98 
99 	errno = 0;
100 	ATF_REQUIRE_ERRNO(ENAMETOOLONG, unlink(buf) == -1);
101 
102 	errno = 0;
103 	ATF_REQUIRE_ERRNO(ENOENT, unlink("/a/b/c/d/e/f/g/h/i/j/k/l/m") == -1);
104 }
105 
106 ATF_TC_CLEANUP(unlink_err, tc)
107 {
108 	(void)unlink(path);
109 }
110 
111 ATF_TC_WITH_CLEANUP(unlink_fifo);
112 ATF_TC_HEAD(unlink_fifo, tc)
113 {
114 	atf_tc_set_md_var(tc, "descr", "Test unlink(2) for a FIFO");
115 }
116 
117 ATF_TC_BODY(unlink_fifo, tc)
118 {
119 #ifdef	__FreeBSD__
120 	int fd;
121 
122 	ATF_REQUIRE_MSG((fd = mkfifo(path, 0666)) == 0,
123 	    "mkfifo failed: %s", strerror(errno));
124 	(void)close(fd);
125 #else
126 	ATF_REQUIRE(mkfifo(path, 0666) == 0);
127 #endif
128 	ATF_REQUIRE(unlink(path) == 0);
129 
130 	errno = 0;
131 #ifdef	__FreeBSD__
132 	ATF_REQUIRE_ERRNO(ENOENT, (fd = open(path, O_RDONLY)) == -1);
133 	(void)close(fd);
134 #else
135 	ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
136 #endif
137 }
138 
139 ATF_TC_CLEANUP(unlink_fifo, tc)
140 {
141 	(void)unlink(path);
142 }
143 
144 ATF_TC_WITH_CLEANUP(unlink_perm);
145 ATF_TC_HEAD(unlink_perm, tc)
146 {
147 	atf_tc_set_md_var(tc, "descr", "Test permissions with unlink(2)");
148 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
149 }
150 
151 ATF_TC_BODY(unlink_perm, tc)
152 {
153 	int rv;
154 
155 	errno = 0;
156 	rv = unlink("/etc");
157 	ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM),
158 	    "unlinking a directory did not fail with EPERM or EACCESS; "
159 	    "unlink() returned %d, errno %d", rv, errno);
160 
161 	errno = 0;
162 	ATF_REQUIRE_ERRNO(EACCES, unlink("/root/.profile") == -1);
163 }
164 
165 ATF_TC_CLEANUP(unlink_perm, tc)
166 {
167 	(void)unlink(path);
168 }
169 
170 ATF_TP_ADD_TCS(tp)
171 {
172 
173 	ATF_TP_ADD_TC(tp, unlink_basic);
174 	ATF_TP_ADD_TC(tp, unlink_err);
175 	ATF_TP_ADD_TC(tp, unlink_fifo);
176 	ATF_TP_ADD_TC(tp, unlink_perm);
177 
178 	return atf_no_error();
179 }
180