1 /* $NetBSD: t_truncate.c,v 1.2 2011/08/18 19:48:03 dholland Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_truncate.c,v 1.2 2011/08/18 19:48:03 dholland Exp $");
33 
34 #include <sys/stat.h>
35 
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #ifdef __FreeBSD__
44 #include <limits.h>
45 #endif
46 
47 static const char path[] = "truncate";
48 static const size_t sizes[] = { 8, 16, 512, 1024, 2048, 4094, 3000, 30 };
49 
50 ATF_TC_WITH_CLEANUP(ftruncate_basic);
51 ATF_TC_HEAD(ftruncate_basic, tc)
52 {
53 	atf_tc_set_md_var(tc, "descr", "A basic test of ftruncate(2)");
54 }
55 
56 ATF_TC_BODY(ftruncate_basic, tc)
57 {
58 	struct stat st;
59 	size_t i;
60 	int fd;
61 
62 	fd = open(path, O_RDWR | O_CREAT, 0600);
63 	ATF_REQUIRE(fd >= 0);
64 
65 	for (i = 0; i < __arraycount(sizes); i++) {
66 
67 		(void)memset(&st, 0, sizeof(struct stat));
68 
69 		ATF_REQUIRE(ftruncate(fd, sizes[i]) == 0);
70 		ATF_REQUIRE(fstat(fd, &st) == 0);
71 
72 		(void)fprintf(stderr, "truncating to %zu bytes\n", sizes[i]);
73 
74 		if (sizes[i] != (size_t)st.st_size)
75 			atf_tc_fail("ftruncate(2) did not truncate");
76 	}
77 
78 	(void)close(fd);
79 	(void)unlink(path);
80 }
81 
82 ATF_TC_CLEANUP(ftruncate_basic, tc)
83 {
84 	(void)unlink(path);
85 }
86 
87 ATF_TC(ftruncate_err);
88 ATF_TC_HEAD(ftruncate_err, tc)
89 {
90 	atf_tc_set_md_var(tc, "descr", "Test errors from ftruncate(2)");
91 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
92 }
93 
94 ATF_TC_BODY(ftruncate_err, tc)
95 {
96 	int fd;
97 
98 	fd = open("/etc/passwd", O_RDONLY, 0400);
99 	ATF_REQUIRE(fd >= 0);
100 
101 	errno = 0;
102 	ATF_REQUIRE_ERRNO(EBADF, ftruncate(-1, 999) == -1);
103 
104 	errno = 0;
105 	ATF_REQUIRE_ERRNO(EINVAL, ftruncate(fd, 999) == -1);
106 
107 	(void)close(fd);
108 }
109 
110 ATF_TC_WITH_CLEANUP(truncate_basic);
111 ATF_TC_HEAD(truncate_basic, tc)
112 {
113 	atf_tc_set_md_var(tc, "descr", "A basic test of truncate(2)");
114 }
115 
116 ATF_TC_BODY(truncate_basic, tc)
117 {
118 	struct stat st;
119 	size_t i;
120 	int fd;
121 
122 	fd = open(path, O_RDWR | O_CREAT, 0600);
123 	ATF_REQUIRE(fd >= 0);
124 
125 	for (i = 0; i < __arraycount(sizes); i++) {
126 
127 		(void)memset(&st, 0, sizeof(struct stat));
128 
129 		ATF_REQUIRE(truncate(path, sizes[i]) == 0);
130 		ATF_REQUIRE(fstat(fd, &st) == 0);
131 
132 		(void)fprintf(stderr, "truncating to %zu bytes\n", sizes[i]);
133 
134 		if (sizes[i] != (size_t)st.st_size)
135 			atf_tc_fail("truncate(2) did not truncate");
136 	}
137 
138 	(void)close(fd);
139 	(void)unlink(path);
140 }
141 
142 ATF_TC_CLEANUP(truncate_basic, tc)
143 {
144 	(void)unlink(path);
145 }
146 
147 ATF_TC(truncate_err);
148 ATF_TC_HEAD(truncate_err, tc)
149 {
150 	atf_tc_set_md_var(tc, "descr", "Test errors from truncate(2)");
151 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
152 }
153 
154 ATF_TC_BODY(truncate_err, tc)
155 {
156 #ifndef __NetBSD__
157 	char buf[PATH_MAX];
158 #endif
159 
160 	errno = 0;
161 	ATF_REQUIRE_ERRNO(EFAULT, truncate((void *)-1, 999) == -1);
162 
163 	errno = 0;
164 	ATF_REQUIRE_ERRNO(EISDIR, truncate("/etc", 999) == -1);
165 
166 	errno = 0;
167 	ATF_REQUIRE_ERRNO(ENOENT, truncate("/a/b/c/d/e/f/g", 999) == -1);
168 
169 	errno = 0;
170 #ifdef __NetBSD__
171 	ATF_REQUIRE_ERRNO(EACCES, truncate("/usr/bin/fpr", 999) == -1);
172 #else
173 	snprintf(buf, sizeof(buf), "%s/truncate_test.root_owned",
174 	    atf_tc_get_config_var(tc, "srcdir"));
175 	ATF_REQUIRE_ERRNO(EACCES, truncate(buf, 999) == -1);
176 #endif
177 }
178 
179 ATF_TP_ADD_TCS(tp)
180 {
181 
182 	ATF_TP_ADD_TC(tp, ftruncate_basic);
183 	ATF_TP_ADD_TC(tp, ftruncate_err);
184 	ATF_TP_ADD_TC(tp, truncate_basic);
185 	ATF_TP_ADD_TC(tp, truncate_err);
186 
187 	return atf_no_error();
188 }
189