1160199Srwatson/*-
2160199Srwatson * Copyright (c) 2006 Robert N. M. Watson
3160199Srwatson * All rights reserved.
4160199Srwatson *
5160199Srwatson * Redistribution and use in source and binary forms, with or without
6160199Srwatson * modification, are permitted provided that the following conditions
7160199Srwatson * are met:
8160199Srwatson * 1. Redistributions of source code must retain the above copyright
9160199Srwatson *    notice, this list of conditions and the following disclaimer.
10160199Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11160199Srwatson *    notice, this list of conditions and the following disclaimer in the
12160199Srwatson *    documentation and/or other materials provided with the distribution.
13160199Srwatson *
14160199Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15160199Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16160199Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17160199Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18160199Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19160199Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20160199Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21160199Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22160199Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23160199Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24160199Srwatson * SUCH DAMAGE.
25160199Srwatson *
26160199Srwatson * $FreeBSD$
27160199Srwatson */
28160199Srwatson
29160199Srwatson/*
30160200Srwatson * Very simple regression test.
31160199Srwatson *
32160199Srwatson * Future tests that might be of interest:
33160199Srwatson *
34160199Srwatson * - Make sure we get EISDIR on a directory.
35160199Srwatson */
36160199Srwatson
37160200Srwatson#include <sys/types.h>
38160200Srwatson#include <sys/event.h>
39160200Srwatson#include <sys/socket.h>
40160199Srwatson#include <sys/stat.h>
41160199Srwatson
42160199Srwatson#include <err.h>
43160199Srwatson#include <errno.h>
44160201Srwatson#include <fcntl.h>
45160201Srwatson#include <inttypes.h>
46160199Srwatson#include <limits.h>
47160199Srwatson#include <stdio.h>
48160199Srwatson#include <unistd.h>
49160199Srwatson
50160199Srwatson/*
51160201Srwatson * Select various potentially interesting lengths at and around power of 2
52160199Srwatson * edges.
53160199Srwatson */
54160201Srwatsonstatic off_t lengths[] = {0, 1, 2, 3, 4, 127, 128, 129, 511, 512, 513, 1023,
55160199Srwatson    1024, 1025, 2047, 2048, 2049, 4095, 4096, 4097, 8191, 8192, 8193, 16383,
56160199Srwatson    16384, 16385};
57160201Srwatsonstatic int lengths_count = sizeof(lengths) / sizeof(off_t);
58160199Srwatson
59160199Srwatsonint
60160199Srwatsonmain(int argc, char *argv[])
61160199Srwatson{
62160201Srwatson	int error, fd, fds[2], i, read_only_fd;
63160199Srwatson	char path[PATH_MAX];
64160199Srwatson	struct stat sb;
65160201Srwatson	size_t size;
66160201Srwatson	off_t len;
67160201Srwatson	char ch;
68160199Srwatson
69160200Srwatson	/*
70160200Srwatson	 * Tests using a writable temporary file: grow and then shrink a file
71160201Srwatson	 * using ftruncate and various lengths.  Make sure that a negative
72160201Srwatson	 * file length is rejected.  Make sure that when we grow the file,
73160201Srwatson	 * bytes now in the range of the file size return 0.
74160201Srwatson	 *
75160201Srwatson	 * Save a read-only reference to the file to use later for read-only
76160201Srwatson	 * descriptor tests.
77160200Srwatson	 */
78160199Srwatson	snprintf(path, PATH_MAX, "/tmp/ftruncate.XXXXXXXXXXXXX");
79160199Srwatson	fd = mkstemp(path);
80160199Srwatson	if (fd < 0)
81160199Srwatson		err(-1, "makestemp");
82160201Srwatson	read_only_fd = open(path, O_RDONLY);
83160201Srwatson	if (read_only_fd < 0) {
84160201Srwatson		error = errno;
85160201Srwatson		(void)unlink(path);
86160201Srwatson		errno = error;
87160201Srwatson		err(-1, "open(%s, O_RDONLY)", path);
88160201Srwatson	}
89160199Srwatson	(void)unlink(path);
90160199Srwatson
91160199Srwatson	if (ftruncate(fd, -1) == 0)
92160199Srwatson		errx(-1, "ftruncate(fd, -1) succeeded");
93160199Srwatson	if (errno != EINVAL)
94160199Srwatson		err(-1, "ftruncate(fd, -1) returned wrong error");
95160199Srwatson
96160201Srwatson	for (i = 0; i < lengths_count; i++) {
97160201Srwatson		len = lengths[i];
98160201Srwatson		if (ftruncate(fd, len) < 0)
99160201Srwatson			err(-1, "ftruncate(%llu) up", len);
100160199Srwatson		if (fstat(fd, &sb) < 0)
101160199Srwatson			err(-1, "stat");
102160201Srwatson		if (sb.st_size != len)
103160201Srwatson			errx(-1, "fstat(%llu) returned len %llu up", len,
104160199Srwatson			    sb.st_size);
105160201Srwatson		if (len != 0) {
106160201Srwatson			size = pread(fd, &ch, sizeof(ch), len - 1);
107160201Srwatson			if (size < 0)
108160201Srwatson				err(-1, "pread on len %llu up", len);
109160201Srwatson			if (size != sizeof(ch))
110160201Srwatson				errx(-1, "pread len %llu size %jd up",
111160201Srwatson				    len, (intmax_t)size);
112160201Srwatson			if (ch != 0)
113160201Srwatson				errx(-1,
114160201Srwatson				    "pread length %llu size %jd ch %d up",
115160201Srwatson				    len, (intmax_t)size, ch);
116160201Srwatson		}
117160199Srwatson	}
118160199Srwatson
119160201Srwatson	for (i = lengths_count - 1; i >= 0; i--) {
120160201Srwatson		len = lengths[i];
121160201Srwatson		if (ftruncate(fd, len) < 0)
122160201Srwatson			err(-1, "ftruncate(%llu) down", len);
123160199Srwatson		if (fstat(fd, &sb) < 0)
124160199Srwatson			err(-1, "stat");
125160201Srwatson		if (sb.st_size != len)
126160201Srwatson			errx(-1, "fstat(%llu) returned %llu down", len,
127160199Srwatson			    sb.st_size);
128160199Srwatson	}
129160200Srwatson	close(fd);
130160199Srwatson
131160200Srwatson	/*
132160201Srwatson	 * Make sure that a read-only descriptor can't be truncated.
133160201Srwatson	 */
134160201Srwatson	if (ftruncate(read_only_fd, 0) == 0)
135160201Srwatson		errx(-1, "ftruncate(read_only_fd) succeeded");
136160201Srwatson	if (errno != EINVAL)
137160201Srwatson		err(-1, "ftruncate(read_only_fd) returned wrong error");
138160201Srwatson	close(read_only_fd);
139160201Srwatson
140160201Srwatson	/*
141160200Srwatson	 * Make sure that ftruncate on sockets doesn't work.
142160200Srwatson	 */
143160200Srwatson	fd = socket(PF_UNIX, SOCK_STREAM, 0);
144160200Srwatson	if (fd < 0)
145160200Srwatson		err(-1, "socket(PF_UNIX, SOCK_STREAM, 0)");
146160200Srwatson	if (ftruncate(fd, 0) == 0)
147160200Srwatson		errx(-1, "ftruncate(socket) succeeded");
148160200Srwatson	if (errno != EINVAL)
149160200Srwatson		err(-1, "ftruncate(socket) returned wrong error");
150160199Srwatson	close(fd);
151160200Srwatson
152160200Srwatson	/*
153160200Srwatson	 * Make sure that ftruncate on pipes doesn't work.
154160200Srwatson	 */
155160200Srwatson	if (pipe(fds) < 0)
156160200Srwatson		err(-1, "pipe");
157160200Srwatson	if (ftruncate(fds[0], 0) == 0)
158160200Srwatson		errx(-1, "ftruncate(pipe) succeeded");
159160200Srwatson	if (errno != EINVAL)
160160200Srwatson		err(-1, "ftruncate(pipe) returned wrong error");
161160200Srwatson	close(fds[0]);
162160200Srwatson	close(fds[1]);
163160200Srwatson
164160200Srwatson	/*
165160200Srwatson	 * Make sure that ftruncate on kqueues doesn't work.
166160200Srwatson	 */
167160200Srwatson	fd = kqueue();
168160200Srwatson	if (fd < 0)
169160200Srwatson		err(-1, "kqueue");
170160200Srwatson	if (ftruncate(fds[0], 0) == 0)
171160200Srwatson		errx(-1, "ftruncate(kqueue) succeeded");
172160200Srwatson	if (errno != EINVAL)
173160200Srwatson		err(-1, "ftruncate(kqueue) returned wrong error");
174160200Srwatson	close(fd);
175160200Srwatson
176160199Srwatson	return (0);
177160199Srwatson}
178