1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_unlink.c,v 1.2 2014/04/21 18:05:17 martin Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc * by Jukka Ruohonen.
911be35a1SLionel Sambuc *
1011be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc * are met:
1311be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc *
1911be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_unlink.c,v 1.2 2014/04/21 18:05:17 martin Exp $");
3311be35a1SLionel Sambuc
3411be35a1SLionel Sambuc #include <sys/stat.h>
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc #include <atf-c.h>
3711be35a1SLionel Sambuc #include <errno.h>
3811be35a1SLionel Sambuc #include <fcntl.h>
3911be35a1SLionel Sambuc #include <limits.h>
4011be35a1SLionel Sambuc #include <string.h>
4111be35a1SLionel Sambuc #include <unistd.h>
4211be35a1SLionel Sambuc
4311be35a1SLionel Sambuc static char path[] = "unlink";
4411be35a1SLionel Sambuc
4511be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(unlink_basic);
ATF_TC_HEAD(unlink_basic,tc)4611be35a1SLionel Sambuc ATF_TC_HEAD(unlink_basic, tc)
4711be35a1SLionel Sambuc {
4811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "A basic test of unlink(2)");
4911be35a1SLionel Sambuc }
5011be35a1SLionel Sambuc
ATF_TC_BODY(unlink_basic,tc)5111be35a1SLionel Sambuc ATF_TC_BODY(unlink_basic, tc)
5211be35a1SLionel Sambuc {
5311be35a1SLionel Sambuc const size_t n = 512;
5411be35a1SLionel Sambuc size_t i;
5511be35a1SLionel Sambuc int fd;
5611be35a1SLionel Sambuc
5711be35a1SLionel Sambuc for (i = 0; i < n; i++) {
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc fd = open(path, O_RDWR | O_CREAT, 0666);
6011be35a1SLionel Sambuc
6111be35a1SLionel Sambuc ATF_REQUIRE(fd != -1);
6211be35a1SLionel Sambuc ATF_REQUIRE(close(fd) == 0);
6311be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
6411be35a1SLionel Sambuc
6511be35a1SLionel Sambuc errno = 0;
6611be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
6711be35a1SLionel Sambuc }
6811be35a1SLionel Sambuc }
6911be35a1SLionel Sambuc
ATF_TC_CLEANUP(unlink_basic,tc)7011be35a1SLionel Sambuc ATF_TC_CLEANUP(unlink_basic, tc)
7111be35a1SLionel Sambuc {
7211be35a1SLionel Sambuc (void)unlink(path);
7311be35a1SLionel Sambuc }
7411be35a1SLionel Sambuc
7511be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(unlink_err);
ATF_TC_HEAD(unlink_err,tc)7611be35a1SLionel Sambuc ATF_TC_HEAD(unlink_err, tc)
7711be35a1SLionel Sambuc {
7811be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test error conditions of unlink(2)");
7911be35a1SLionel Sambuc }
8011be35a1SLionel Sambuc
ATF_TC_BODY(unlink_err,tc)8111be35a1SLionel Sambuc ATF_TC_BODY(unlink_err, tc)
8211be35a1SLionel Sambuc {
8311be35a1SLionel Sambuc char buf[PATH_MAX + 1];
8411be35a1SLionel Sambuc
8511be35a1SLionel Sambuc (void)memset(buf, 'x', sizeof(buf));
8611be35a1SLionel Sambuc
8711be35a1SLionel Sambuc errno = 0;
8811be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EBUSY, unlink("/") == -1);
8911be35a1SLionel Sambuc
9011be35a1SLionel Sambuc errno = 0;
9111be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENAMETOOLONG, unlink(buf) == -1);
9211be35a1SLionel Sambuc
9311be35a1SLionel Sambuc errno = 0;
9411be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENOENT, unlink("/a/b/c/d/e/f/g/h/i/j/k/l/m") == -1);
9511be35a1SLionel Sambuc }
9611be35a1SLionel Sambuc
ATF_TC_CLEANUP(unlink_err,tc)9711be35a1SLionel Sambuc ATF_TC_CLEANUP(unlink_err, tc)
9811be35a1SLionel Sambuc {
9911be35a1SLionel Sambuc (void)unlink(path);
10011be35a1SLionel Sambuc }
10111be35a1SLionel Sambuc
10211be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(unlink_fifo);
ATF_TC_HEAD(unlink_fifo,tc)10311be35a1SLionel Sambuc ATF_TC_HEAD(unlink_fifo, tc)
10411be35a1SLionel Sambuc {
10511be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test unlink(2) for a FIFO");
10611be35a1SLionel Sambuc }
10711be35a1SLionel Sambuc
ATF_TC_BODY(unlink_fifo,tc)10811be35a1SLionel Sambuc ATF_TC_BODY(unlink_fifo, tc)
10911be35a1SLionel Sambuc {
11011be35a1SLionel Sambuc
11111be35a1SLionel Sambuc ATF_REQUIRE(mkfifo(path, 0666) == 0);
11211be35a1SLionel Sambuc ATF_REQUIRE(unlink(path) == 0);
11311be35a1SLionel Sambuc
11411be35a1SLionel Sambuc errno = 0;
11511be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1);
11611be35a1SLionel Sambuc }
11711be35a1SLionel Sambuc
ATF_TC_CLEANUP(unlink_fifo,tc)11811be35a1SLionel Sambuc ATF_TC_CLEANUP(unlink_fifo, tc)
11911be35a1SLionel Sambuc {
12011be35a1SLionel Sambuc (void)unlink(path);
12111be35a1SLionel Sambuc }
12211be35a1SLionel Sambuc
12311be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(unlink_perm);
ATF_TC_HEAD(unlink_perm,tc)12411be35a1SLionel Sambuc ATF_TC_HEAD(unlink_perm, tc)
12511be35a1SLionel Sambuc {
12611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test permissions with unlink(2)");
12711be35a1SLionel Sambuc atf_tc_set_md_var(tc, "require.user", "unprivileged");
12811be35a1SLionel Sambuc }
12911be35a1SLionel Sambuc
ATF_TC_BODY(unlink_perm,tc)13011be35a1SLionel Sambuc ATF_TC_BODY(unlink_perm, tc)
13111be35a1SLionel Sambuc {
132*0a6a1f1dSLionel Sambuc int rv;
13311be35a1SLionel Sambuc
13411be35a1SLionel Sambuc errno = 0;
135*0a6a1f1dSLionel Sambuc rv = unlink("/etc");
136*0a6a1f1dSLionel Sambuc ATF_REQUIRE_MSG(rv == -1 && (errno == EACCES || errno == EPERM),
137*0a6a1f1dSLionel Sambuc "unlinking a directory did not fail with EPERM or EACCESS; "
138*0a6a1f1dSLionel Sambuc "unlink() returned %d, errno %d", rv, errno);
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc errno = 0;
14111be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EACCES, unlink("/root/.profile") == -1);
14211be35a1SLionel Sambuc }
14311be35a1SLionel Sambuc
ATF_TC_CLEANUP(unlink_perm,tc)14411be35a1SLionel Sambuc ATF_TC_CLEANUP(unlink_perm, tc)
14511be35a1SLionel Sambuc {
14611be35a1SLionel Sambuc (void)unlink(path);
14711be35a1SLionel Sambuc }
14811be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)14911be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
15011be35a1SLionel Sambuc {
15111be35a1SLionel Sambuc
15211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, unlink_basic);
15311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, unlink_err);
15411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, unlink_fifo);
15511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, unlink_perm);
15611be35a1SLionel Sambuc
15711be35a1SLionel Sambuc return atf_no_error();
15811be35a1SLionel Sambuc }
159