xref: /freebsd/tests/sys/file/ftruncate_test.c (revision a0ee8cc636cd5c2374ec44ca71226564ea0bca95)
1 /*-
2  * Copyright (c) 2006 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Very simple regression test.
31  *
32  * Future tests that might be of interest:
33  *
34  * - Make sure we get EISDIR on a directory.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/event.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <inttypes.h>
46 #include <limits.h>
47 #include <stdio.h>
48 #include <unistd.h>
49 
50 /*
51  * Select various potentially interesting lengths at and around power of 2
52  * edges.
53  */
54 static off_t lengths[] = {0, 1, 2, 3, 4, 127, 128, 129, 511, 512, 513, 1023,
55     1024, 1025, 2047, 2048, 2049, 4095, 4096, 4097, 8191, 8192, 8193, 16383,
56     16384, 16385};
57 static int lengths_count = sizeof(lengths) / sizeof(off_t);
58 
59 int
60 main(int argc, char *argv[])
61 {
62 	int error, fd, fds[2], i, read_only_fd;
63 	char path[PATH_MAX];
64 	struct stat sb;
65 	ssize_t size;
66 	off_t len;
67 	char ch;
68 
69 	/*
70 	 * Tests using a writable temporary file: grow and then shrink a file
71 	 * using ftruncate and various lengths.  Make sure that a negative
72 	 * file length is rejected.  Make sure that when we grow the file,
73 	 * bytes now in the range of the file size return 0.
74 	 *
75 	 * Save a read-only reference to the file to use later for read-only
76 	 * descriptor tests.
77 	 */
78 	snprintf(path, PATH_MAX, "/tmp/ftruncate.XXXXXXXXXXXXX");
79 	fd = mkstemp(path);
80 	if (fd < 0)
81 		err(-1, "mkstemp");
82 	read_only_fd = open(path, O_RDONLY);
83 	if (read_only_fd < 0) {
84 		error = errno;
85 		(void)unlink(path);
86 		errno = error;
87 		err(-1, "open(%s, O_RDONLY)", path);
88 	}
89 	(void)unlink(path);
90 
91 	if (ftruncate(fd, -1) == 0)
92 		errx(-1, "ftruncate(fd, -1) succeeded");
93 	if (errno != EINVAL)
94 		err(-1, "ftruncate(fd, -1) returned wrong error");
95 
96 	for (i = 0; i < lengths_count; i++) {
97 		len = lengths[i];
98 		if (ftruncate(fd, len) < 0)
99 			err(-1, "ftruncate(%jd) up", (intmax_t)len);
100 		if (fstat(fd, &sb) < 0)
101 			err(-1, "stat");
102 		if (sb.st_size != len)
103 			errx(-1, "fstat with len=%jd returned len %jd up",
104 			    (intmax_t)len, (intmax_t)sb.st_size);
105 		if (len != 0) {
106 			size = pread(fd, &ch, sizeof(ch), len - 1);
107 			if (size < 0)
108 				err(-1, "pread on len %jd up", (intmax_t)len);
109 			if (size != sizeof(ch))
110 				errx(-1, "pread len %jd size %jd up",
111 				    (intmax_t)len, (intmax_t)size);
112 			if (ch != 0)
113 				errx(-1,
114 				    "pread length %jd size %jd ch %d up",
115 				    (intmax_t)len, (intmax_t)size, ch);
116 		}
117 	}
118 
119 	for (i = lengths_count - 1; i >= 0; i--) {
120 		len = lengths[i];
121 		if (ftruncate(fd, len) < 0)
122 			err(-1, "ftruncate(%jd) down", (intmax_t)len);
123 		if (fstat(fd, &sb) < 0)
124 			err(-1, "stat");
125 		if (sb.st_size != len)
126 			errx(-1, "fstat(%jd) returned %jd down", (intmax_t)len,
127 			    sb.st_size);
128 	}
129 	close(fd);
130 
131 	/*
132 	 * Make sure that a read-only descriptor can't be truncated.
133 	 */
134 	if (ftruncate(read_only_fd, 0) == 0)
135 		errx(-1, "ftruncate(read_only_fd) succeeded");
136 	if (errno != EINVAL)
137 		err(-1, "ftruncate(read_only_fd) returned wrong error");
138 	close(read_only_fd);
139 
140 	/*
141 	 * Make sure that ftruncate on sockets doesn't work.
142 	 */
143 	fd = socket(PF_UNIX, SOCK_STREAM, 0);
144 	if (fd < 0)
145 		err(-1, "socket(PF_UNIX, SOCK_STREAM, 0)");
146 	if (ftruncate(fd, 0) == 0)
147 		errx(-1, "ftruncate(socket) succeeded");
148 	if (errno != EINVAL)
149 		err(-1, "ftruncate(socket) returned wrong error");
150 	close(fd);
151 
152 	/*
153 	 * Make sure that ftruncate on pipes doesn't work.
154 	 */
155 	if (pipe(fds) < 0)
156 		err(-1, "pipe");
157 	if (ftruncate(fds[0], 0) == 0)
158 		errx(-1, "ftruncate(pipe) succeeded");
159 	if (errno != EINVAL)
160 		err(-1, "ftruncate(pipe) returned wrong error");
161 	close(fds[0]);
162 	close(fds[1]);
163 
164 	/*
165 	 * Make sure that ftruncate on kqueues doesn't work.
166 	 */
167 	fd = kqueue();
168 	if (fd < 0)
169 		err(-1, "kqueue");
170 	if (ftruncate(fds[0], 0) == 0)
171 		errx(-1, "ftruncate(kqueue) succeeded");
172 	if (errno != EINVAL)
173 		err(-1, "ftruncate(kqueue) returned wrong error");
174 	close(fd);
175 
176 	return (0);
177 }
178