1 /* $NetBSD: t_truncate.c,v 1.2 2011/08/18 19:48:03 dholland Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_truncate.c,v 1.2 2011/08/18 19:48:03 dholland Exp $");
43 static const char path
[] = "truncate";
44 static const size_t sizes
[] = { 8, 16, 512, 1024, 2048, 4094, 3000, 30 };
46 ATF_TC_WITH_CLEANUP(ftruncate_basic
);
47 ATF_TC_HEAD(ftruncate_basic
, tc
)
49 atf_tc_set_md_var(tc
, "descr", "A basic test of ftruncate(2)");
52 ATF_TC_BODY(ftruncate_basic
, tc
)
58 fd
= open(path
, O_RDWR
| O_CREAT
, 0600);
61 for (i
= 0; i
< __arraycount(sizes
); i
++) {
63 (void)memset(&st
, 0, sizeof(struct stat
));
65 ATF_REQUIRE(ftruncate(fd
, sizes
[i
]) == 0);
66 ATF_REQUIRE(fstat(fd
, &st
) == 0);
68 (void)fprintf(stderr
, "truncating to %zu bytes\n", sizes
[i
]);
70 if (sizes
[i
] != (size_t)st
.st_size
)
71 atf_tc_fail("ftruncate(2) did not truncate");
78 ATF_TC_CLEANUP(ftruncate_basic
, tc
)
83 ATF_TC(ftruncate_err
);
84 ATF_TC_HEAD(ftruncate_err
, tc
)
86 atf_tc_set_md_var(tc
, "descr", "Test errors from ftruncate(2)");
87 atf_tc_set_md_var(tc
, "require.user", "unprivileged");
90 ATF_TC_BODY(ftruncate_err
, tc
)
94 fd
= open("/etc/passwd", O_RDONLY
, 0400);
98 ATF_REQUIRE_ERRNO(EBADF
, ftruncate(-1, 999) == -1);
101 ATF_REQUIRE_ERRNO(EINVAL
, ftruncate(fd
, 999) == -1);
106 ATF_TC_WITH_CLEANUP(truncate_basic
);
107 ATF_TC_HEAD(truncate_basic
, tc
)
109 atf_tc_set_md_var(tc
, "descr", "A basic test of truncate(2)");
112 ATF_TC_BODY(truncate_basic
, tc
)
118 fd
= open(path
, O_RDWR
| O_CREAT
, 0600);
119 ATF_REQUIRE(fd
>= 0);
121 for (i
= 0; i
< __arraycount(sizes
); i
++) {
123 (void)memset(&st
, 0, sizeof(struct stat
));
125 ATF_REQUIRE(truncate(path
, sizes
[i
]) == 0);
126 ATF_REQUIRE(fstat(fd
, &st
) == 0);
128 (void)fprintf(stderr
, "truncating to %zu bytes\n", sizes
[i
]);
130 if (sizes
[i
] != (size_t)st
.st_size
)
131 atf_tc_fail("truncate(2) did not truncate");
138 ATF_TC_CLEANUP(truncate_basic
, tc
)
143 ATF_TC(truncate_err
);
144 ATF_TC_HEAD(truncate_err
, tc
)
146 atf_tc_set_md_var(tc
, "descr", "Test errors from truncate(2)");
147 atf_tc_set_md_var(tc
, "require.user", "unprivileged");
150 ATF_TC_BODY(truncate_err
, tc
)
154 ATF_REQUIRE_ERRNO(EFAULT
, truncate((void *)-1, 999) == -1);
157 ATF_REQUIRE_ERRNO(EISDIR
, truncate("/etc", 999) == -1);
160 ATF_REQUIRE_ERRNO(ENOENT
, truncate("/a/b/c/d/e/f/g", 999) == -1);
163 ATF_REQUIRE_ERRNO(EACCES
, truncate("/usr/bin/fpr", 999) == -1);
169 ATF_TP_ADD_TC(tp
, ftruncate_basic
);
170 ATF_TP_ADD_TC(tp
, ftruncate_err
);
171 ATF_TP_ADD_TC(tp
, truncate_basic
);
172 ATF_TP_ADD_TC(tp
, truncate_err
);
174 return atf_no_error();