diff options
author | rwatson <rwatson@FreeBSD.org> | 2006-07-09 10:43:31 +0000 |
---|---|---|
committer | rwatson <rwatson@FreeBSD.org> | 2006-07-09 10:43:31 +0000 |
commit | 6d0cdb6a1efefd130f5e166ff34677710c7fd997 (patch) | |
tree | f78eabbb893342b41f13fac359f508cac6c9c666 /tools | |
parent | 2d926cc233d34c1fc114a0d2d40605118317bcb9 (diff) | |
download | FreeBSD-src-6d0cdb6a1efefd130f5e166ff34677710c7fd997.zip FreeBSD-src-6d0cdb6a1efefd130f5e166ff34677710c7fd997.tar.gz |
Add regression tests to make sure that attempting to call ftruncate() on
various non-file objects fails.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/regression/file/ftruncate/ftruncate.c | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/tools/regression/file/ftruncate/ftruncate.c b/tools/regression/file/ftruncate/ftruncate.c index 31d7210..73f5ec3 100644 --- a/tools/regression/file/ftruncate/ftruncate.c +++ b/tools/regression/file/ftruncate/ftruncate.c @@ -27,10 +27,7 @@ */ /* - * Very simple regression test. Open a temporary file, then proceed to - * ftruncate it to various values and check that fstat returns those values. - * First we run forwards through a set of sizes starting with zero, then back - * again. + * Very simple regression test. * * Future tests that might be of interest: * @@ -42,6 +39,9 @@ * - Make sure we get EISDIR on a directory. */ +#include <sys/types.h> +#include <sys/event.h> +#include <sys/socket.h> #include <sys/stat.h> #include <err.h> @@ -63,9 +63,14 @@ int main(int argc, char *argv[]) { char path[PATH_MAX]; + int fd, fds[2], i; struct stat sb; - int fd, i; + /* + * Tests using a writable temporary file: grow and then shrink a file + * using ftruncate and various sizes. Make sure that a negative file + * size is rejected. + */ snprintf(path, PATH_MAX, "/tmp/ftruncate.XXXXXXXXXXXXX"); fd = mkstemp(path); if (fd < 0) @@ -96,7 +101,43 @@ main(int argc, char *argv[]) errx(-1, "fstat(%llu) returned %llu down", sizes[i], sb.st_size); } + close(fd); + + /* + * Make sure that ftruncate on sockets doesn't work. + */ + fd = socket(PF_UNIX, SOCK_STREAM, 0); + if (fd < 0) + err(-1, "socket(PF_UNIX, SOCK_STREAM, 0)"); + if (ftruncate(fd, 0) == 0) + errx(-1, "ftruncate(socket) succeeded"); + if (errno != EINVAL) + err(-1, "ftruncate(socket) returned wrong error"); + close(fd); + /* + * Make sure that ftruncate on pipes doesn't work. + */ + if (pipe(fds) < 0) + err(-1, "pipe"); + if (ftruncate(fds[0], 0) == 0) + errx(-1, "ftruncate(pipe) succeeded"); + if (errno != EINVAL) + err(-1, "ftruncate(pipe) returned wrong error"); + close(fds[0]); + close(fds[1]); + + /* + * Make sure that ftruncate on kqueues doesn't work. + */ + fd = kqueue(); + if (fd < 0) + err(-1, "kqueue"); + if (ftruncate(fds[0], 0) == 0) + errx(-1, "ftruncate(kqueue) succeeded"); + if (errno != EINVAL) + err(-1, "ftruncate(kqueue) returned wrong error"); close(fd); + return (0); } |