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: stable/10/tests/sys/file/ftruncate_test.c 319301 2017-05-31 08:36:47Z ngie $
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
60312323Sngiemain(void)
61160199Srwatson{
62160201Srwatson	int error, fd, fds[2], i, read_only_fd;
63319301Sngie	char path[] = "ftruncate_file";
64160199Srwatson	struct stat sb;
65281432Sngie	ssize_t size;
66160201Srwatson	off_t len;
67160201Srwatson	char ch;
68160199Srwatson
69160200Srwatson	/*
70319301Sngie	 * Tests using a writable 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	 */
78319301Sngie	fd = open(path, O_RDWR|O_CREAT, 0600);
79160199Srwatson	if (fd < 0)
80319301Sngie		err(1, "open(%s, O_RDWR|O_CREAT, 0600)", path);
81160201Srwatson	read_only_fd = open(path, O_RDONLY);
82160201Srwatson	if (read_only_fd < 0) {
83160201Srwatson		error = errno;
84160201Srwatson		(void)unlink(path);
85160201Srwatson		errno = error;
86319301Sngie		err(1, "open(%s, O_RDONLY)", path);
87160201Srwatson	}
88160199Srwatson	(void)unlink(path);
89160199Srwatson
90160199Srwatson	if (ftruncate(fd, -1) == 0)
91319301Sngie		errx(1, "ftruncate(fd, -1) succeeded unexpectedly");
92160199Srwatson	if (errno != EINVAL)
93319301Sngie		err(1, "ftruncate(fd, -1) returned wrong error");
94160199Srwatson
95160201Srwatson	for (i = 0; i < lengths_count; i++) {
96160201Srwatson		len = lengths[i];
97160201Srwatson		if (ftruncate(fd, len) < 0)
98319301Sngie			err(1, "ftruncate(%jd) up", (intmax_t)len);
99160199Srwatson		if (fstat(fd, &sb) < 0)
100319301Sngie			err(1, "stat");
101160201Srwatson		if (sb.st_size != len)
102281432Sngie			errx(-1, "fstat with len=%jd returned len %jd up",
103281432Sngie			    (intmax_t)len, (intmax_t)sb.st_size);
104160201Srwatson		if (len != 0) {
105160201Srwatson			size = pread(fd, &ch, sizeof(ch), len - 1);
106160201Srwatson			if (size < 0)
107319301Sngie				err(1, "pread on len %jd up", (intmax_t)len);
108160201Srwatson			if (size != sizeof(ch))
109281432Sngie				errx(-1, "pread len %jd size %jd up",
110281432Sngie				    (intmax_t)len, (intmax_t)size);
111160201Srwatson			if (ch != 0)
112160201Srwatson				errx(-1,
113281432Sngie				    "pread length %jd size %jd ch %d up",
114281432Sngie				    (intmax_t)len, (intmax_t)size, ch);
115160201Srwatson		}
116160199Srwatson	}
117160199Srwatson
118160201Srwatson	for (i = lengths_count - 1; i >= 0; i--) {
119160201Srwatson		len = lengths[i];
120160201Srwatson		if (ftruncate(fd, len) < 0)
121319301Sngie			err(1, "ftruncate(%jd) down", (intmax_t)len);
122160199Srwatson		if (fstat(fd, &sb) < 0)
123319301Sngie			err(1, "stat");
124160201Srwatson		if (sb.st_size != len)
125281432Sngie			errx(-1, "fstat(%jd) returned %jd down", (intmax_t)len,
126160199Srwatson			    sb.st_size);
127160199Srwatson	}
128160200Srwatson	close(fd);
129160199Srwatson
130160200Srwatson	/*
131160201Srwatson	 * Make sure that a read-only descriptor can't be truncated.
132160201Srwatson	 */
133160201Srwatson	if (ftruncate(read_only_fd, 0) == 0)
134160201Srwatson		errx(-1, "ftruncate(read_only_fd) succeeded");
135160201Srwatson	if (errno != EINVAL)
136319301Sngie		err(1, "ftruncate(read_only_fd) returned wrong error");
137160201Srwatson	close(read_only_fd);
138160201Srwatson
139160201Srwatson	/*
140160200Srwatson	 * Make sure that ftruncate on sockets doesn't work.
141160200Srwatson	 */
142160200Srwatson	fd = socket(PF_UNIX, SOCK_STREAM, 0);
143160200Srwatson	if (fd < 0)
144319301Sngie		err(1, "socket(PF_UNIX, SOCK_STREAM, 0)");
145160200Srwatson	if (ftruncate(fd, 0) == 0)
146160200Srwatson		errx(-1, "ftruncate(socket) succeeded");
147160200Srwatson	if (errno != EINVAL)
148319301Sngie		err(1, "ftruncate(socket) returned wrong error");
149160199Srwatson	close(fd);
150160200Srwatson
151160200Srwatson	/*
152160200Srwatson	 * Make sure that ftruncate on pipes doesn't work.
153160200Srwatson	 */
154160200Srwatson	if (pipe(fds) < 0)
155319301Sngie		err(1, "pipe");
156160200Srwatson	if (ftruncate(fds[0], 0) == 0)
157160200Srwatson		errx(-1, "ftruncate(pipe) succeeded");
158160200Srwatson	if (errno != EINVAL)
159319301Sngie		err(1, "ftruncate(pipe) returned wrong error");
160160200Srwatson	close(fds[0]);
161160200Srwatson	close(fds[1]);
162160200Srwatson
163160200Srwatson	/*
164160200Srwatson	 * Make sure that ftruncate on kqueues doesn't work.
165160200Srwatson	 */
166160200Srwatson	fd = kqueue();
167160200Srwatson	if (fd < 0)
168319301Sngie		err(1, "kqueue");
169160200Srwatson	if (ftruncate(fds[0], 0) == 0)
170160200Srwatson		errx(-1, "ftruncate(kqueue) succeeded");
171160200Srwatson	if (errno != EINVAL)
172319301Sngie		err(1, "ftruncate(kqueue) returned wrong error");
173160200Srwatson	close(fd);
174160200Srwatson
175160199Srwatson	return (0);
176160199Srwatson}
177