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