diff options
Diffstat (limited to 'src/regress/lib/libc/sys/t_unlink.c')
| -rw-r--r-- | src/regress/lib/libc/sys/t_unlink.c | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/src/regress/lib/libc/sys/t_unlink.c b/src/regress/lib/libc/sys/t_unlink.c new file mode 100644 index 0000000000..9736d77593 --- /dev/null +++ b/src/regress/lib/libc/sys/t_unlink.c | |||
| @@ -0,0 +1,162 @@ | |||
| 1 | /* $OpenBSD: t_unlink.c,v 1.1.1.1 2019/11/19 19:57:04 bluhm Exp $ */ | ||
| 2 | /* $NetBSD: t_unlink.c,v 1.4 2017/01/14 20:55:26 christos Exp $ */ | ||
| 3 | |||
| 4 | /*- | ||
| 5 | * Copyright (c) 2011 The NetBSD Foundation, Inc. | ||
| 6 | * All rights reserved. | ||
| 7 | * | ||
| 8 | * This code is derived from software contributed to The NetBSD Foundation | ||
| 9 | * by Jukka Ruohonen. | ||
| 10 | * | ||
| 11 | * Redistribution and use in source and binary forms, with or without | ||
| 12 | * modification, are permitted provided that the following conditions | ||
| 13 | * are met: | ||
| 14 | * 1. Redistributions of source code must retain the above copyright | ||
| 15 | * notice, this list of conditions and the following disclaimer. | ||
| 16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 17 | * notice, this list of conditions and the following disclaimer in the | ||
| 18 | * documentation and/or other materials provided with the distribution. | ||
| 19 | * | ||
| 20 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| 22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
| 24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | */ | ||
| 32 | |||
| 33 | #include "macros.h" | ||
| 34 | |||
| 35 | #include <sys/cdefs.h> | ||
| 36 | __RCSID("$NetBSD: t_unlink.c,v 1.4 2017/01/14 20:55:26 christos Exp $"); | ||
| 37 | |||
| 38 | #include <sys/stat.h> | ||
| 39 | |||
| 40 | #include "atf-c.h" | ||
| 41 | #include <errno.h> | ||
| 42 | #include <fcntl.h> | ||
| 43 | #include <limits.h> | ||
| 44 | #include <string.h> | ||
| 45 | #include <unistd.h> | ||
| 46 | |||
| 47 | static char path[] = "unlink"; | ||
| 48 | |||
| 49 | ATF_TC_WITH_CLEANUP(unlink_basic); | ||
| 50 | ATF_TC_HEAD(unlink_basic, tc) | ||
| 51 | { | ||
| 52 | atf_tc_set_md_var(tc, "descr", "A basic test of unlink(2)"); | ||
| 53 | } | ||
| 54 | |||
| 55 | ATF_TC_BODY(unlink_basic, tc) | ||
| 56 | { | ||
| 57 | const size_t n = 512; | ||
| 58 | size_t i; | ||
| 59 | int fd; | ||
| 60 | |||
| 61 | for (i = 0; i < n; i++) { | ||
| 62 | |||
| 63 | fd = open(path, O_RDWR | O_CREAT, 0666); | ||
| 64 | |||
| 65 | ATF_REQUIRE(fd != -1); | ||
| 66 | ATF_REQUIRE(close(fd) == 0); | ||
| 67 | ATF_REQUIRE(unlink(path) == 0); | ||
| 68 | |||
| 69 | errno = 0; | ||
| 70 | ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | ATF_TC_CLEANUP(unlink_basic, tc) | ||
| 75 | { | ||
| 76 | (void)unlink(path); | ||
| 77 | } | ||
| 78 | |||
| 79 | ATF_TC_WITH_CLEANUP(unlink_err); | ||
| 80 | ATF_TC_HEAD(unlink_err, tc) | ||
| 81 | { | ||
| 82 | atf_tc_set_md_var(tc, "descr", "Test error conditions of unlink(2)"); | ||
| 83 | } | ||
| 84 | |||
| 85 | ATF_TC_BODY(unlink_err, tc) | ||
| 86 | { | ||
| 87 | char buf[PATH_MAX + 1]; | ||
| 88 | |||
| 89 | (void)memset(buf, 'x', sizeof(buf)); | ||
| 90 | |||
| 91 | errno = 0; | ||
| 92 | ATF_REQUIRE_ERRNO(EBUSY, unlink("/") == -1); | ||
| 93 | |||
| 94 | errno = 0; | ||
| 95 | ATF_REQUIRE_ERRNO(ENAMETOOLONG, unlink(buf) == -1); | ||
| 96 | |||
| 97 | errno = 0; | ||
| 98 | ATF_REQUIRE_ERRNO(ENOENT, unlink("/a/b/c/d/e/f/g/h/i/j/k/l/m") == -1); | ||
| 99 | } | ||
| 100 | |||
| 101 | ATF_TC_CLEANUP(unlink_err, tc) | ||
| 102 | { | ||
| 103 | (void)unlink(path); | ||
| 104 | } | ||
| 105 | |||
| 106 | ATF_TC_WITH_CLEANUP(unlink_fifo); | ||
| 107 | ATF_TC_HEAD(unlink_fifo, tc) | ||
| 108 | { | ||
| 109 | atf_tc_set_md_var(tc, "descr", "Test unlink(2) for a FIFO"); | ||
| 110 | } | ||
| 111 | |||
| 112 | ATF_TC_BODY(unlink_fifo, tc) | ||
| 113 | { | ||
| 114 | |||
| 115 | ATF_REQUIRE(mkfifo(path, 0666) == 0); | ||
| 116 | ATF_REQUIRE(unlink(path) == 0); | ||
| 117 | |||
| 118 | errno = 0; | ||
| 119 | ATF_REQUIRE_ERRNO(ENOENT, open(path, O_RDONLY) == -1); | ||
| 120 | } | ||
| 121 | |||
| 122 | ATF_TC_CLEANUP(unlink_fifo, tc) | ||
| 123 | { | ||
| 124 | (void)unlink(path); | ||
| 125 | } | ||
| 126 | |||
| 127 | ATF_TC_WITH_CLEANUP(unlink_perm); | ||
| 128 | ATF_TC_HEAD(unlink_perm, tc) | ||
| 129 | { | ||
| 130 | atf_tc_set_md_var(tc, "descr", "Test permissions with unlink(2)"); | ||
| 131 | atf_tc_set_md_var(tc, "require.user", "unprivileged"); | ||
| 132 | } | ||
| 133 | |||
| 134 | ATF_TC_BODY(unlink_perm, tc) | ||
| 135 | { | ||
| 136 | int rv; | ||
| 137 | |||
| 138 | errno = 0; | ||
| 139 | rv = unlink("/etc"); | ||
| 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); | ||
| 143 | |||
| 144 | errno = 0; | ||
| 145 | ATF_REQUIRE_ERRNO(EACCES, unlink("/root/.profile") == -1); | ||
| 146 | } | ||
| 147 | |||
| 148 | ATF_TC_CLEANUP(unlink_perm, tc) | ||
| 149 | { | ||
| 150 | (void)unlink(path); | ||
| 151 | } | ||
| 152 | |||
| 153 | ATF_TP_ADD_TCS(tp) | ||
| 154 | { | ||
| 155 | |||
| 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); | ||
| 160 | |||
| 161 | return atf_no_error(); | ||
| 162 | } | ||
