1 /* $OpenBSD: t_truncate.c,v 1.4 2022/05/24 05:14:30 anton Exp $ */ 2 /* $NetBSD: t_truncate.c,v 1.3 2017/01/13 20:03:51 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 <stdio.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 static const char path[] = "truncate"; 46 static const size_t sizes[] = { 8, 16, 512, 1024, 2048, 4094, 3000, 30 }; 47 48 ATF_TC_WITH_CLEANUP(ftruncate_basic); 49 ATF_TC_HEAD(ftruncate_basic, tc) 50 { 51 atf_tc_set_md_var(tc, "descr", "A basic test of ftruncate(2)"); 52 } 53 54 ATF_TC_BODY(ftruncate_basic, tc) 55 { 56 struct stat st; 57 size_t i; 58 int fd; 59 60 fd = open(path, O_RDWR | O_CREAT, 0600); 61 ATF_REQUIRE(fd >= 0); 62 63 for (i = 0; i < __arraycount(sizes); i++) { 64 65 (void)memset(&st, 0, sizeof(struct stat)); 66 67 ATF_REQUIRE(ftruncate(fd, sizes[i]) == 0); 68 ATF_REQUIRE(fstat(fd, &st) == 0); 69 70 (void)fprintf(stderr, "truncating to %zu bytes\n", sizes[i]); 71 72 if (sizes[i] != (size_t)st.st_size) 73 atf_tc_fail("ftruncate(2) did not truncate"); 74 } 75 76 (void)close(fd); 77 (void)unlink(path); 78 } 79 80 ATF_TC_CLEANUP(ftruncate_basic, tc) 81 { 82 (void)unlink(path); 83 } 84 85 ATF_TC(ftruncate_err); 86 ATF_TC_HEAD(ftruncate_err, tc) 87 { 88 atf_tc_set_md_var(tc, "descr", "Test errors from ftruncate(2)"); 89 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 90 } 91 92 ATF_TC_BODY(ftruncate_err, tc) 93 { 94 int fd; 95 96 fd = open("/etc/passwd", O_RDONLY); 97 ATF_REQUIRE(fd >= 0); 98 99 errno = 0; 100 ATF_REQUIRE_ERRNO(EBADF, ftruncate(-1, 999) == -1); 101 102 errno = 0; 103 ATF_REQUIRE_ERRNO(EINVAL, ftruncate(fd, 999) == -1); 104 105 (void)close(fd); 106 } 107 108 ATF_TC_WITH_CLEANUP(truncate_basic); 109 ATF_TC_HEAD(truncate_basic, tc) 110 { 111 atf_tc_set_md_var(tc, "descr", "A basic test of truncate(2)"); 112 } 113 114 ATF_TC_BODY(truncate_basic, tc) 115 { 116 struct stat st; 117 size_t i; 118 int fd; 119 120 fd = open(path, O_RDWR | O_CREAT, 0600); 121 ATF_REQUIRE(fd >= 0); 122 123 for (i = 0; i < __arraycount(sizes); i++) { 124 125 (void)memset(&st, 0, sizeof(struct stat)); 126 127 ATF_REQUIRE(truncate(path, sizes[i]) == 0); 128 ATF_REQUIRE(fstat(fd, &st) == 0); 129 130 (void)fprintf(stderr, "truncating to %zu bytes\n", sizes[i]); 131 132 if (sizes[i] != (size_t)st.st_size) 133 atf_tc_fail("truncate(2) did not truncate"); 134 } 135 136 (void)close(fd); 137 (void)unlink(path); 138 } 139 140 ATF_TC_CLEANUP(truncate_basic, tc) 141 { 142 (void)unlink(path); 143 } 144 145 ATF_TC(truncate_err); 146 ATF_TC_HEAD(truncate_err, tc) 147 { 148 atf_tc_set_md_var(tc, "descr", "Test errors from truncate(2)"); 149 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 150 } 151 152 ATF_TC_BODY(truncate_err, tc) 153 { 154 char buf[PATH_MAX]; 155 156 errno = 0; 157 ATF_REQUIRE_ERRNO(EFAULT, truncate((void *)-1, 999) == -1); 158 159 errno = 0; 160 ATF_REQUIRE_ERRNO(EISDIR, truncate("/tmp", 999) == -1); 161 162 errno = 0; 163 ATF_REQUIRE_ERRNO(ENOENT, truncate("/a/b/c/d/e/f/g", 999) == -1); 164 165 errno = 0; 166 snprintf(buf, sizeof(buf), "%s/truncate_test.root_owned", 167 atf_tc_get_config_var(tc, "srcdir")); 168 ATF_REQUIRE_ERRNO(EACCES, truncate(buf, 999) == -1); 169 } 170 171 ATF_TP_ADD_TCS(tp) 172 { 173 174 ATF_TP_ADD_TC(tp, ftruncate_basic); 175 ATF_TP_ADD_TC(tp, ftruncate_err); 176 ATF_TP_ADD_TC(tp, truncate_basic); 177 ATF_TP_ADD_TC(tp, truncate_err); 178 179 return atf_no_error(); 180 } 181