ftruncate_test.c (160200) | ftruncate_test.c (160201) |
---|---|
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 unchanged lines hidden (view full) --- 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 * | 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 unchanged lines hidden (view full) --- 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: head/tools/regression/file/ftruncate/ftruncate.c 160200 2006-07-09 10:43:31Z rwatson $ | 26 * $FreeBSD: head/tools/regression/file/ftruncate/ftruncate.c 160201 2006-07-09 10:56:36Z rwatson $ |
27 */ 28 29/* 30 * Very simple regression test. 31 * 32 * Future tests that might be of interest: 33 * | 27 */ 28 29/* 30 * Very simple regression test. 31 * 32 * Future tests that might be of interest: 33 * |
34 * - Make sure that files grown via ftruncate() return 0 bytes for data 35 * reads. 36 * 37 * - Make sure that we can't ftruncate on a read-only descriptor. 38 * | |
39 * - Make sure we get EISDIR on a directory. 40 */ 41 42#include <sys/types.h> 43#include <sys/event.h> 44#include <sys/socket.h> 45#include <sys/stat.h> 46 47#include <err.h> 48#include <errno.h> | 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> |
|
49#include <limits.h> 50#include <stdio.h> 51#include <unistd.h> 52 53/* | 46#include <limits.h> 47#include <stdio.h> 48#include <unistd.h> 49 50/* |
54 * Select various potentially interesting sizes at and around power of 2 | 51 * Select various potentially interesting lengths at and around power of 2 |
55 * edges. 56 */ | 52 * edges. 53 */ |
57static off_t sizes[] = {0, 1, 2, 3, 4, 127, 128, 129, 511, 512, 513, 1023, | 54static off_t lengths[] = {0, 1, 2, 3, 4, 127, 128, 129, 511, 512, 513, 1023, |
58 1024, 1025, 2047, 2048, 2049, 4095, 4096, 4097, 8191, 8192, 8193, 16383, 59 16384, 16385}; | 55 1024, 1025, 2047, 2048, 2049, 4095, 4096, 4097, 8191, 8192, 8193, 16383, 56 16384, 16385}; |
60static int size_count = sizeof(sizes) / sizeof(off_t); | 57static int lengths_count = sizeof(lengths) / sizeof(off_t); |
61 62int 63main(int argc, char *argv[]) 64{ | 58 59int 60main(int argc, char *argv[]) 61{ |
62 int error, fd, fds[2], i, read_only_fd; |
|
65 char path[PATH_MAX]; | 63 char path[PATH_MAX]; |
66 int fd, fds[2], i; | |
67 struct stat sb; | 64 struct stat sb; |
65 size_t size; 66 off_t len; 67 char ch; |
|
68 69 /* 70 * Tests using a writable temporary file: grow and then shrink a file | 68 69 /* 70 * Tests using a writable temporary file: grow and then shrink a file |
71 * using ftruncate and various sizes. Make sure that a negative file 72 * size is rejected. | 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. |
73 */ 74 snprintf(path, PATH_MAX, "/tmp/ftruncate.XXXXXXXXXXXXX"); 75 fd = mkstemp(path); 76 if (fd < 0) 77 err(-1, "makestemp"); | 77 */ 78 snprintf(path, PATH_MAX, "/tmp/ftruncate.XXXXXXXXXXXXX"); 79 fd = mkstemp(path); 80 if (fd < 0) 81 err(-1, "makestemp"); |
82 read_only_fd = open(path, O_RDONLY); 83 if (read_only_fd < 0) { 84 error = errno; 85 (void)unlink(path); 86 errno = error; 87 err(-1, "open(%s, O_RDONLY)", path); 88 } |
|
78 (void)unlink(path); 79 80 if (ftruncate(fd, -1) == 0) 81 errx(-1, "ftruncate(fd, -1) succeeded"); 82 if (errno != EINVAL) 83 err(-1, "ftruncate(fd, -1) returned wrong error"); 84 | 89 (void)unlink(path); 90 91 if (ftruncate(fd, -1) == 0) 92 errx(-1, "ftruncate(fd, -1) succeeded"); 93 if (errno != EINVAL) 94 err(-1, "ftruncate(fd, -1) returned wrong error"); 95 |
85 for (i = 0; i < size_count; i++) { 86 if (ftruncate(fd, sizes[i]) < 0) 87 err(-1, "ftruncate(%llu) up", sizes[i]); | 96 for (i = 0; i < lengths_count; i++) { 97 len = lengths[i]; 98 if (ftruncate(fd, len) < 0) 99 err(-1, "ftruncate(%llu) up", len); |
88 if (fstat(fd, &sb) < 0) 89 err(-1, "stat"); | 100 if (fstat(fd, &sb) < 0) 101 err(-1, "stat"); |
90 if (sb.st_size != sizes[i]) 91 errx(-1, "fstat(%llu) returned %llu up", sizes[i], | 102 if (sb.st_size != len) 103 errx(-1, "fstat(%llu) returned len %llu up", len, |
92 sb.st_size); | 104 sb.st_size); |
105 if (len != 0) { 106 size = pread(fd, &ch, sizeof(ch), len - 1); 107 if (size < 0) 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); 112 if (ch != 0) 113 errx(-1, 114 "pread length %llu size %jd ch %d up", 115 len, (intmax_t)size, ch); 116 } |
|
93 } 94 | 117 } 118 |
95 for (i = size_count - 1; i >= 0; i--) { 96 if (ftruncate(fd, sizes[i]) < 0) 97 err(-1, "ftruncate(%llu) down", sizes[i]); | 119 for (i = lengths_count - 1; i >= 0; i--) { 120 len = lengths[i]; 121 if (ftruncate(fd, len) < 0) 122 err(-1, "ftruncate(%llu) down", len); |
98 if (fstat(fd, &sb) < 0) 99 err(-1, "stat"); | 123 if (fstat(fd, &sb) < 0) 124 err(-1, "stat"); |
100 if (sb.st_size != sizes[i]) 101 errx(-1, "fstat(%llu) returned %llu down", sizes[i], | 125 if (sb.st_size != len) 126 errx(-1, "fstat(%llu) returned %llu down", len, |
102 sb.st_size); 103 } 104 close(fd); 105 106 /* | 127 sb.st_size); 128 } 129 close(fd); 130 131 /* |
132 * Make sure that a read-only descriptor can't be truncated. 133 */ 134 if (ftruncate(read_only_fd, 0) == 0) 135 errx(-1, "ftruncate(read_only_fd) succeeded"); 136 if (errno != EINVAL) 137 err(-1, "ftruncate(read_only_fd) returned wrong error"); 138 close(read_only_fd); 139 140 /* |
|
107 * Make sure that ftruncate on sockets doesn't work. 108 */ 109 fd = socket(PF_UNIX, SOCK_STREAM, 0); 110 if (fd < 0) 111 err(-1, "socket(PF_UNIX, SOCK_STREAM, 0)"); 112 if (ftruncate(fd, 0) == 0) 113 errx(-1, "ftruncate(socket) succeeded"); 114 if (errno != EINVAL) --- 29 unchanged lines hidden --- | 141 * Make sure that ftruncate on sockets doesn't work. 142 */ 143 fd = socket(PF_UNIX, SOCK_STREAM, 0); 144 if (fd < 0) 145 err(-1, "socket(PF_UNIX, SOCK_STREAM, 0)"); 146 if (ftruncate(fd, 0) == 0) 147 errx(-1, "ftruncate(socket) succeeded"); 148 if (errno != EINVAL) --- 29 unchanged lines hidden --- |