1 /* $NetBSD: t_unlink.c,v 1.4 2017/01/14 20:55:26 christos 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_unlink.c,v 1.4 2017/01/14 20:55:26 christos Exp $");
43 static char path[] = "unlink";
45 ATF_TC_WITH_CLEANUP(unlink_basic);
46 ATF_TC_HEAD(unlink_basic, tc)
48 atf_tc_set_md_var(tc, "descr", "A basic test of unlink(2)");
51 ATF_TC_BODY(unlink_basic, tc)
57 for (i = 0; i < n; i++) {
59 fd = open(path, O_RDWR | O_CREAT, 0666);
61 ATF_REQUIRE(fd != -1);
62 ATF_REQUIRE(close(fd) == 0);
63 ATF_REQUIRE(unlink(path) == 0);
66 ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
70 ATF_TC_CLEANUP(unlink_basic, tc)
75 ATF_TC_WITH_CLEANUP(unlink_err);
76 ATF_TC_HEAD(unlink_err, tc)
78 atf_tc_set_md_var(tc, "descr", "Test error conditions of unlink(2)");
81 ATF_TC_BODY(unlink_err, tc)
83 char buf[PATH_MAX + 1];
85 (void)memset(buf, 'x', sizeof(buf));
89 ATF_REQUIRE_ERRNO(EISDIR, unlink("/") == -1);
91 ATF_REQUIRE_ERRNO(EBUSY, unlink("/") == -1);
95 ATF_REQUIRE_ERRNO(ENAMETOOLONG, unlink(buf) == -1);
98 ATF_REQUIRE_ERRNO(ENOENT, unlink("/a/b/c/d/e/f/g/h/i/j/k/l/m") == -1);
101 ATF_TC_CLEANUP(unlink_err, tc)
106 ATF_TC_WITH_CLEANUP(unlink_fifo);
107 ATF_TC_HEAD(unlink_fifo, tc)
109 atf_tc_set_md_var(tc, "descr", "Test unlink(2) for a FIFO");
112 ATF_TC_BODY(unlink_fifo, tc)
115 ATF_REQUIRE(mkfifo(path, 0666) == 0);
116 ATF_REQUIRE(unlink(path) == 0);
119 ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
122 ATF_TC_CLEANUP(unlink_fifo, tc)
127 ATF_TC_WITH_CLEANUP(unlink_perm);
128 ATF_TC_HEAD(unlink_perm, tc)
130 atf_tc_set_md_var(tc, "descr", "Test permissions with unlink(2)");
131 atf_tc_set_md_var(tc, "require.user", "unprivileged");
134 ATF_TC_BODY(unlink_perm, tc)
140 ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM),
141 "unlinking a directory did not fail with EPERM or EACCESS; "
142 "unlink() returned %d, errno %d", rv, errno);
145 ATF_REQUIRE_ERRNO(EACCES, unlink("/root/.profile") == -1);
148 ATF_TC_CLEANUP(unlink_perm, tc)
156 ATF_TP_ADD_TC(tp, unlink_basic);
157 ATF_TP_ADD_TC(tp, unlink_err);
158 ATF_TP_ADD_TC(tp, unlink_fifo);
159 ATF_TP_ADD_TC(tp, unlink_perm);
161 return atf_no_error();