]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/file/ftruncate_test.c
zfs: merge openzfs/zfs@229b9f4ed
[FreeBSD/FreeBSD.git] / tests / sys / file / ftruncate_test.c
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
27 /*
28  * Very simple regression test.
29  *
30  * Future tests that might be of interest:
31  *
32  * - Make sure we get EISDIR on a directory.
33  */
34
35 #include <sys/types.h>
36 #include <sys/event.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <inttypes.h>
44 #include <limits.h>
45 #include <stdio.h>
46 #include <unistd.h>
47
48 /*
49  * Select various potentially interesting lengths at and around power of 2
50  * edges.
51  */
52 static off_t lengths[] = {0, 1, 2, 3, 4, 127, 128, 129, 511, 512, 513, 1023,
53     1024, 1025, 2047, 2048, 2049, 4095, 4096, 4097, 8191, 8192, 8193, 16383,
54     16384, 16385};
55 static int lengths_count = sizeof(lengths) / sizeof(off_t);
56
57 int
58 main(void)
59 {
60         int error, fd, fds[2], i, read_only_fd;
61         char path[] = "ftruncate_file";
62         struct stat sb;
63         ssize_t size;
64         off_t len;
65         char ch;
66
67         /*
68          * Tests using a writable file: grow and then shrink a file
69          * using ftruncate and various lengths.  Make sure that a negative
70          * file length is rejected.  Make sure that when we grow the file,
71          * bytes now in the range of the file size return 0.
72          *
73          * Save a read-only reference to the file to use later for read-only
74          * descriptor tests.
75          */
76         fd = open(path, O_RDWR|O_CREAT, 0600);
77         if (fd < 0)
78                 err(1, "open(%s, O_RDWR|O_CREAT, 0600)", path);
79         read_only_fd = open(path, O_RDONLY);
80         if (read_only_fd < 0) {
81                 error = errno;
82                 (void)unlink(path);
83                 errno = error;
84                 err(1, "open(%s, O_RDONLY)", path);
85         }
86         (void)unlink(path);
87
88         if (ftruncate(fd, -1) == 0)
89                 errx(1, "ftruncate(fd, -1) succeeded unexpectedly");
90         if (errno != EINVAL)
91                 err(1, "ftruncate(fd, -1) returned wrong error");
92
93         for (i = 0; i < lengths_count; i++) {
94                 len = lengths[i];
95                 if (ftruncate(fd, len) < 0)
96                         err(1, "ftruncate(%jd) up", (intmax_t)len);
97                 if (fstat(fd, &sb) < 0)
98                         err(1, "stat");
99                 if (sb.st_size != len)
100                         errx(-1, "fstat with len=%jd returned len %jd up",
101                             (intmax_t)len, (intmax_t)sb.st_size);
102                 if (len != 0) {
103                         size = pread(fd, &ch, sizeof(ch), len - 1);
104                         if (size < 0)
105                                 err(1, "pread on len %jd up", (intmax_t)len);
106                         if (size != sizeof(ch))
107                                 errx(-1, "pread len %jd size %jd up",
108                                     (intmax_t)len, (intmax_t)size);
109                         if (ch != 0)
110                                 errx(-1,
111                                     "pread length %jd size %jd ch %d up",
112                                     (intmax_t)len, (intmax_t)size, ch);
113                 }
114         }
115
116         for (i = lengths_count - 1; i >= 0; i--) {
117                 len = lengths[i];
118                 if (ftruncate(fd, len) < 0)
119                         err(1, "ftruncate(%jd) down", (intmax_t)len);
120                 if (fstat(fd, &sb) < 0)
121                         err(1, "stat");
122                 if (sb.st_size != len)
123                         errx(-1, "fstat(%jd) returned %jd down", (intmax_t)len,
124                             sb.st_size);
125         }
126         close(fd);
127
128         /*
129          * Make sure that a read-only descriptor can't be truncated.
130          */
131         if (ftruncate(read_only_fd, 0) == 0)
132                 errx(-1, "ftruncate(read_only_fd) succeeded");
133         if (errno != EINVAL)
134                 err(1, "ftruncate(read_only_fd) returned wrong error");
135         close(read_only_fd);
136
137         /*
138          * Make sure that ftruncate on sockets doesn't work.
139          */
140         fd = socket(PF_UNIX, SOCK_STREAM, 0);
141         if (fd < 0)
142                 err(1, "socket(PF_UNIX, SOCK_STREAM, 0)");
143         if (ftruncate(fd, 0) == 0)
144                 errx(-1, "ftruncate(socket) succeeded");
145         if (errno != EINVAL)
146                 err(1, "ftruncate(socket) returned wrong error");
147         close(fd);
148
149         /*
150          * Make sure that ftruncate on pipes doesn't work.
151          */
152         if (pipe(fds) < 0)
153                 err(1, "pipe");
154         if (ftruncate(fds[0], 0) == 0)
155                 errx(-1, "ftruncate(pipe) succeeded");
156         if (errno != EINVAL)
157                 err(1, "ftruncate(pipe) returned wrong error");
158         close(fds[0]);
159         close(fds[1]);
160
161         /*
162          * Make sure that ftruncate on kqueues doesn't work.
163          */
164         fd = kqueue();
165         if (fd < 0)
166                 err(1, "kqueue");
167         if (ftruncate(fds[0], 0) == 0)
168                 errx(-1, "ftruncate(kqueue) succeeded");
169         if (errno != EINVAL)
170                 err(1, "ftruncate(kqueue) returned wrong error");
171         close(fd);
172
173         return (0);
174 }