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