xref: /freebsd/tests/sys/file/ftruncate_test.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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(void)
61 {
62 	int error, fd, fds[2], i, read_only_fd;
63 	char path[] = "ftruncate_file";
64 	struct stat sb;
65 	ssize_t size;
66 	off_t len;
67 	char ch;
68 
69 	/*
70 	 * Tests using a writable 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 	fd = open(path, O_RDWR|O_CREAT, 0600);
79 	if (fd < 0)
80 		err(1, "open(%s, O_RDWR|O_CREAT, 0600)", path);
81 	read_only_fd = open(path, O_RDONLY);
82 	if (read_only_fd < 0) {
83 		error = errno;
84 		(void)unlink(path);
85 		errno = error;
86 		err(1, "open(%s, O_RDONLY)", path);
87 	}
88 	(void)unlink(path);
89 
90 	if (ftruncate(fd, -1) == 0)
91 		errx(1, "ftruncate(fd, -1) succeeded unexpectedly");
92 	if (errno != EINVAL)
93 		err(1, "ftruncate(fd, -1) returned wrong error");
94 
95 	for (i = 0; i < lengths_count; i++) {
96 		len = lengths[i];
97 		if (ftruncate(fd, len) < 0)
98 			err(1, "ftruncate(%jd) up", (intmax_t)len);
99 		if (fstat(fd, &sb) < 0)
100 			err(1, "stat");
101 		if (sb.st_size != len)
102 			errx(-1, "fstat with len=%jd returned len %jd up",
103 			    (intmax_t)len, (intmax_t)sb.st_size);
104 		if (len != 0) {
105 			size = pread(fd, &ch, sizeof(ch), len - 1);
106 			if (size < 0)
107 				err(1, "pread on len %jd up", (intmax_t)len);
108 			if (size != sizeof(ch))
109 				errx(-1, "pread len %jd size %jd up",
110 				    (intmax_t)len, (intmax_t)size);
111 			if (ch != 0)
112 				errx(-1,
113 				    "pread length %jd size %jd ch %d up",
114 				    (intmax_t)len, (intmax_t)size, ch);
115 		}
116 	}
117 
118 	for (i = lengths_count - 1; i >= 0; i--) {
119 		len = lengths[i];
120 		if (ftruncate(fd, len) < 0)
121 			err(1, "ftruncate(%jd) down", (intmax_t)len);
122 		if (fstat(fd, &sb) < 0)
123 			err(1, "stat");
124 		if (sb.st_size != len)
125 			errx(-1, "fstat(%jd) returned %jd down", (intmax_t)len,
126 			    sb.st_size);
127 	}
128 	close(fd);
129 
130 	/*
131 	 * Make sure that a read-only descriptor can't be truncated.
132 	 */
133 	if (ftruncate(read_only_fd, 0) == 0)
134 		errx(-1, "ftruncate(read_only_fd) succeeded");
135 	if (errno != EINVAL)
136 		err(1, "ftruncate(read_only_fd) returned wrong error");
137 	close(read_only_fd);
138 
139 	/*
140 	 * Make sure that ftruncate on sockets doesn't work.
141 	 */
142 	fd = socket(PF_UNIX, SOCK_STREAM, 0);
143 	if (fd < 0)
144 		err(1, "socket(PF_UNIX, SOCK_STREAM, 0)");
145 	if (ftruncate(fd, 0) == 0)
146 		errx(-1, "ftruncate(socket) succeeded");
147 	if (errno != EINVAL)
148 		err(1, "ftruncate(socket) returned wrong error");
149 	close(fd);
150 
151 	/*
152 	 * Make sure that ftruncate on pipes doesn't work.
153 	 */
154 	if (pipe(fds) < 0)
155 		err(1, "pipe");
156 	if (ftruncate(fds[0], 0) == 0)
157 		errx(-1, "ftruncate(pipe) succeeded");
158 	if (errno != EINVAL)
159 		err(1, "ftruncate(pipe) returned wrong error");
160 	close(fds[0]);
161 	close(fds[1]);
162 
163 	/*
164 	 * Make sure that ftruncate on kqueues doesn't work.
165 	 */
166 	fd = kqueue();
167 	if (fd < 0)
168 		err(1, "kqueue");
169 	if (ftruncate(fds[0], 0) == 0)
170 		errx(-1, "ftruncate(kqueue) succeeded");
171 	if (errno != EINVAL)
172 		err(1, "ftruncate(kqueue) returned wrong error");
173 	close(fd);
174 
175 	return (0);
176 }
177