/*-1* Copyright (c) 2006 Robert N. M. Watson2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Very simple regression test.28*29* Future tests that might be of interest:30*31* - Make sure we get EISDIR on a directory.32*/3334#include <sys/types.h>35#include <sys/event.h>36#include <sys/socket.h>37#include <sys/stat.h>3839#include <err.h>40#include <errno.h>41#include <fcntl.h>42#include <inttypes.h>43#include <limits.h>44#include <stdio.h>45#include <unistd.h>4647/*48* Select various potentially interesting lengths at and around power of 249* edges.50*/51static off_t lengths[] = {0, 1, 2, 3, 4, 127, 128, 129, 511, 512, 513, 1023,521024, 1025, 2047, 2048, 2049, 4095, 4096, 4097, 8191, 8192, 8193, 16383,5316384, 16385};54static int lengths_count = sizeof(lengths) / sizeof(off_t);5556int57main(void)58{59int error, fd, fds[2], i, read_only_fd;60char path[] = "ftruncate_file";61struct stat sb;62ssize_t size;63off_t len;64char ch;6566/*67* Tests using a writable file: grow and then shrink a file68* using ftruncate and various lengths. Make sure that a negative69* file length is rejected. Make sure that when we grow the file,70* bytes now in the range of the file size return 0.71*72* Save a read-only reference to the file to use later for read-only73* descriptor tests.74*/75fd = open(path, O_RDWR|O_CREAT, 0600);76if (fd < 0)77err(1, "open(%s, O_RDWR|O_CREAT, 0600)", path);78read_only_fd = open(path, O_RDONLY);79if (read_only_fd < 0) {80error = errno;81(void)unlink(path);82errno = error;83err(1, "open(%s, O_RDONLY)", path);84}85(void)unlink(path);8687if (ftruncate(fd, -1) == 0)88errx(1, "ftruncate(fd, -1) succeeded unexpectedly");89if (errno != EINVAL)90err(1, "ftruncate(fd, -1) returned wrong error");9192for (i = 0; i < lengths_count; i++) {93len = lengths[i];94if (ftruncate(fd, len) < 0)95err(1, "ftruncate(%jd) up", (intmax_t)len);96if (fstat(fd, &sb) < 0)97err(1, "stat");98if (sb.st_size != len)99errx(-1, "fstat with len=%jd returned len %jd up",100(intmax_t)len, (intmax_t)sb.st_size);101if (len != 0) {102size = pread(fd, &ch, sizeof(ch), len - 1);103if (size < 0)104err(1, "pread on len %jd up", (intmax_t)len);105if (size != sizeof(ch))106errx(-1, "pread len %jd size %jd up",107(intmax_t)len, (intmax_t)size);108if (ch != 0)109errx(-1,110"pread length %jd size %jd ch %d up",111(intmax_t)len, (intmax_t)size, ch);112}113}114115for (i = lengths_count - 1; i >= 0; i--) {116len = lengths[i];117if (ftruncate(fd, len) < 0)118err(1, "ftruncate(%jd) down", (intmax_t)len);119if (fstat(fd, &sb) < 0)120err(1, "stat");121if (sb.st_size != len)122errx(-1, "fstat(%jd) returned %jd down", (intmax_t)len,123sb.st_size);124}125close(fd);126127/*128* Make sure that a read-only descriptor can't be truncated.129*/130if (ftruncate(read_only_fd, 0) == 0)131errx(-1, "ftruncate(read_only_fd) succeeded");132if (errno != EINVAL)133err(1, "ftruncate(read_only_fd) returned wrong error");134close(read_only_fd);135136/*137* Make sure that ftruncate on sockets doesn't work.138*/139fd = socket(PF_UNIX, SOCK_STREAM, 0);140if (fd < 0)141err(1, "socket(PF_UNIX, SOCK_STREAM, 0)");142if (ftruncate(fd, 0) == 0)143errx(-1, "ftruncate(socket) succeeded");144if (errno != EINVAL)145err(1, "ftruncate(socket) returned wrong error");146close(fd);147148/*149* Make sure that ftruncate on pipes doesn't work.150*/151if (pipe(fds) < 0)152err(1, "pipe");153if (ftruncate(fds[0], 0) == 0)154errx(-1, "ftruncate(pipe) succeeded");155if (errno != EINVAL)156err(1, "ftruncate(pipe) returned wrong error");157close(fds[0]);158close(fds[1]);159160/*161* Make sure that ftruncate on kqueues doesn't work.162*/163fd = kqueue();164if (fd < 0)165err(1, "kqueue");166if (ftruncate(fds[0], 0) == 0)167errx(-1, "ftruncate(kqueue) succeeded");168if (errno != EINVAL)169err(1, "ftruncate(kqueue) returned wrong error");170close(fd);171172return (0);173}174175176