1*f40f3adcSEnji Cooper /* $NetBSD: t_unlink.c,v 1.4 2017/01/14 20:55:26 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper * by Jukka Ruohonen.
957718be8SEnji Cooper *
1057718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper * are met:
1357718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper *
1957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper */
3157718be8SEnji Cooper #include <sys/cdefs.h>
32*f40f3adcSEnji Cooper __RCSID("$NetBSD: t_unlink.c,v 1.4 2017/01/14 20:55:26 christos Exp $");
3357718be8SEnji Cooper
3457718be8SEnji Cooper #include <sys/stat.h>
3557718be8SEnji Cooper
3657718be8SEnji Cooper #include <atf-c.h>
3757718be8SEnji Cooper #include <errno.h>
3857718be8SEnji Cooper #include <fcntl.h>
3957718be8SEnji Cooper #include <limits.h>
4057718be8SEnji Cooper #include <string.h>
4157718be8SEnji Cooper #include <unistd.h>
4257718be8SEnji Cooper
4357718be8SEnji Cooper static char path[] = "unlink";
4457718be8SEnji Cooper
4557718be8SEnji Cooper ATF_TC_WITH_CLEANUP(unlink_basic);
ATF_TC_HEAD(unlink_basic,tc)4657718be8SEnji Cooper ATF_TC_HEAD(unlink_basic, tc)
4757718be8SEnji Cooper {
4857718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "A basic test of unlink(2)");
4957718be8SEnji Cooper }
5057718be8SEnji Cooper
ATF_TC_BODY(unlink_basic,tc)5157718be8SEnji Cooper ATF_TC_BODY(unlink_basic, tc)
5257718be8SEnji Cooper {
5357718be8SEnji Cooper const size_t n = 512;
5457718be8SEnji Cooper size_t i;
5557718be8SEnji Cooper int fd;
5657718be8SEnji Cooper
5757718be8SEnji Cooper for (i = 0; i < n; i++) {
5857718be8SEnji Cooper
5957718be8SEnji Cooper fd = open(path, O_RDWR | O_CREAT, 0666);
6057718be8SEnji Cooper
6157718be8SEnji Cooper ATF_REQUIRE(fd != -1);
6257718be8SEnji Cooper ATF_REQUIRE(close(fd) == 0);
6357718be8SEnji Cooper ATF_REQUIRE(unlink(path) == 0);
6457718be8SEnji Cooper
6557718be8SEnji Cooper errno = 0;
6657718be8SEnji Cooper ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
6757718be8SEnji Cooper }
6857718be8SEnji Cooper }
6957718be8SEnji Cooper
ATF_TC_CLEANUP(unlink_basic,tc)7057718be8SEnji Cooper ATF_TC_CLEANUP(unlink_basic, tc)
7157718be8SEnji Cooper {
7257718be8SEnji Cooper (void)unlink(path);
7357718be8SEnji Cooper }
7457718be8SEnji Cooper
7557718be8SEnji Cooper ATF_TC_WITH_CLEANUP(unlink_err);
ATF_TC_HEAD(unlink_err,tc)7657718be8SEnji Cooper ATF_TC_HEAD(unlink_err, tc)
7757718be8SEnji Cooper {
7857718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test error conditions of unlink(2)");
7957718be8SEnji Cooper }
8057718be8SEnji Cooper
ATF_TC_BODY(unlink_err,tc)8157718be8SEnji Cooper ATF_TC_BODY(unlink_err, tc)
8257718be8SEnji Cooper {
8357718be8SEnji Cooper char buf[PATH_MAX + 1];
8457718be8SEnji Cooper
8557718be8SEnji Cooper (void)memset(buf, 'x', sizeof(buf));
8657718be8SEnji Cooper
8757718be8SEnji Cooper errno = 0;
88ff0ba872SEnji Cooper #ifdef __FreeBSD__
8967654a1aSEnji Cooper ATF_REQUIRE_ERRNO(EISDIR, unlink("/") == -1);
9067654a1aSEnji Cooper #else
9157718be8SEnji Cooper ATF_REQUIRE_ERRNO(EBUSY, unlink("/") == -1);
9267654a1aSEnji Cooper #endif
9357718be8SEnji Cooper
9457718be8SEnji Cooper errno = 0;
9557718be8SEnji Cooper ATF_REQUIRE_ERRNO(ENAMETOOLONG, unlink(buf) == -1);
9657718be8SEnji Cooper
9757718be8SEnji Cooper errno = 0;
9857718be8SEnji Cooper ATF_REQUIRE_ERRNO(ENOENT, unlink("/a/b/c/d/e/f/g/h/i/j/k/l/m") == -1);
9957718be8SEnji Cooper }
10057718be8SEnji Cooper
ATF_TC_CLEANUP(unlink_err,tc)10157718be8SEnji Cooper ATF_TC_CLEANUP(unlink_err, tc)
10257718be8SEnji Cooper {
10357718be8SEnji Cooper (void)unlink(path);
10457718be8SEnji Cooper }
10557718be8SEnji Cooper
10657718be8SEnji Cooper ATF_TC_WITH_CLEANUP(unlink_fifo);
ATF_TC_HEAD(unlink_fifo,tc)10757718be8SEnji Cooper ATF_TC_HEAD(unlink_fifo, tc)
10857718be8SEnji Cooper {
10957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test unlink(2) for a FIFO");
11057718be8SEnji Cooper }
11157718be8SEnji Cooper
ATF_TC_BODY(unlink_fifo,tc)11257718be8SEnji Cooper ATF_TC_BODY(unlink_fifo, tc)
11357718be8SEnji Cooper {
11457718be8SEnji Cooper
115*f40f3adcSEnji Cooper ATF_REQUIRE(mkfifo(path, 0666) == 0);
11657718be8SEnji Cooper ATF_REQUIRE(unlink(path) == 0);
11757718be8SEnji Cooper
11857718be8SEnji Cooper errno = 0;
11957718be8SEnji Cooper ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
12057718be8SEnji Cooper }
12157718be8SEnji Cooper
ATF_TC_CLEANUP(unlink_fifo,tc)12257718be8SEnji Cooper ATF_TC_CLEANUP(unlink_fifo, tc)
12357718be8SEnji Cooper {
12457718be8SEnji Cooper (void)unlink(path);
12557718be8SEnji Cooper }
12657718be8SEnji Cooper
12757718be8SEnji Cooper ATF_TC_WITH_CLEANUP(unlink_perm);
ATF_TC_HEAD(unlink_perm,tc)12857718be8SEnji Cooper ATF_TC_HEAD(unlink_perm, tc)
12957718be8SEnji Cooper {
13057718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test permissions with unlink(2)");
13157718be8SEnji Cooper atf_tc_set_md_var(tc, "require.user", "unprivileged");
13257718be8SEnji Cooper }
13357718be8SEnji Cooper
ATF_TC_BODY(unlink_perm,tc)13457718be8SEnji Cooper ATF_TC_BODY(unlink_perm, tc)
13557718be8SEnji Cooper {
13657718be8SEnji Cooper int rv;
13757718be8SEnji Cooper
13857718be8SEnji Cooper errno = 0;
13957718be8SEnji Cooper rv = unlink("/etc");
14057718be8SEnji Cooper ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM),
14157718be8SEnji Cooper "unlinking a directory did not fail with EPERM or EACCESS; "
14257718be8SEnji Cooper "unlink() returned %d, errno %d", rv, errno);
14357718be8SEnji Cooper
14457718be8SEnji Cooper errno = 0;
14557718be8SEnji Cooper ATF_REQUIRE_ERRNO(EACCES, unlink("/root/.profile") == -1);
14657718be8SEnji Cooper }
14757718be8SEnji Cooper
ATF_TC_CLEANUP(unlink_perm,tc)14857718be8SEnji Cooper ATF_TC_CLEANUP(unlink_perm, tc)
14957718be8SEnji Cooper {
15057718be8SEnji Cooper (void)unlink(path);
15157718be8SEnji Cooper }
15257718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)15357718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
15457718be8SEnji Cooper {
15557718be8SEnji Cooper
15657718be8SEnji Cooper ATF_TP_ADD_TC(tp, unlink_basic);
15757718be8SEnji Cooper ATF_TP_ADD_TC(tp, unlink_err);
15857718be8SEnji Cooper ATF_TP_ADD_TC(tp, unlink_fifo);
15957718be8SEnji Cooper ATF_TP_ADD_TC(tp, unlink_perm);
16057718be8SEnji Cooper
16157718be8SEnji Cooper return atf_no_error();
16257718be8SEnji Cooper }
163