2 * Copyright (c) 2006 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 * Very simple regression test.
32 * Future tests that might be of interest:
34 * - Make sure we get EISDIR on a directory.
37 #include <sys/types.h>
38 #include <sys/event.h>
39 #include <sys/socket.h>
51 * Select various potentially interesting lengths at and around power of 2
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,
57 static int lengths_count = sizeof(lengths) / sizeof(off_t);
60 main(int argc, char *argv[])
62 int error, fd, fds[2], i, read_only_fd;
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.
75 * Save a read-only reference to the file to use later for read-only
78 snprintf(path, PATH_MAX, "/tmp/ftruncate.XXXXXXXXXXXXX");
82 read_only_fd = open(path, O_RDONLY);
83 if (read_only_fd < 0) {
87 err(-1, "open(%s, O_RDONLY)", path);
91 if (ftruncate(fd, -1) == 0)
92 errx(-1, "ftruncate(fd, -1) succeeded");
94 err(-1, "ftruncate(fd, -1) returned wrong error");
96 for (i = 0; i < lengths_count; i++) {
98 if (ftruncate(fd, len) < 0)
99 err(-1, "ftruncate(%llu) up", len);
100 if (fstat(fd, &sb) < 0)
102 if (sb.st_size != len)
103 errx(-1, "fstat(%llu) returned len %llu up", len,
106 size = pread(fd, &ch, sizeof(ch), len - 1);
108 err(-1, "pread on len %llu up", len);
109 if (size != sizeof(ch))
110 errx(-1, "pread len %llu size %jd up",
111 len, (intmax_t)size);
114 "pread length %llu size %jd ch %d up",
115 len, (intmax_t)size, ch);
119 for (i = lengths_count - 1; i >= 0; i--) {
121 if (ftruncate(fd, len) < 0)
122 err(-1, "ftruncate(%llu) down", len);
123 if (fstat(fd, &sb) < 0)
125 if (sb.st_size != len)
126 errx(-1, "fstat(%llu) returned %llu down", len,
132 * Make sure that a read-only descriptor can't be truncated.
134 if (ftruncate(read_only_fd, 0) == 0)
135 errx(-1, "ftruncate(read_only_fd) succeeded");
137 err(-1, "ftruncate(read_only_fd) returned wrong error");
141 * Make sure that ftruncate on sockets doesn't work.
143 fd = socket(PF_UNIX, SOCK_STREAM, 0);
145 err(-1, "socket(PF_UNIX, SOCK_STREAM, 0)");
146 if (ftruncate(fd, 0) == 0)
147 errx(-1, "ftruncate(socket) succeeded");
149 err(-1, "ftruncate(socket) returned wrong error");
153 * Make sure that ftruncate on pipes doesn't work.
157 if (ftruncate(fds[0], 0) == 0)
158 errx(-1, "ftruncate(pipe) succeeded");
160 err(-1, "ftruncate(pipe) returned wrong error");
165 * Make sure that ftruncate on kqueues doesn't work.
170 if (ftruncate(fds[0], 0) == 0)
171 errx(-1, "ftruncate(kqueue) succeeded");
173 err(-1, "ftruncate(kqueue) returned wrong error");