From 97cb2cb87cc104285df6b7dfed312b0c328c8dd3 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 7 Apr 2023 14:55:00 +0200 Subject: [PATCH] test: reproduce copyfile() off by one regression Signed-off-by: Joachim Wiberg --- test/copyfile.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/test/copyfile.c b/test/copyfile.c index 47aa2cf..3a00c76 100644 --- a/test/copyfile.c +++ b/test/copyfile.c @@ -2,16 +2,39 @@ #include #include "check.h" +static int sz() +{ + struct stat st; + ssize_t len; + + printf("=>Start copyfile(/dev/zero, 32768)\n"); + len = copyfile("/dev/zero", "/tmp/zeroes", 32768, 0); + if (!len) + err(1, "failed copying any bytes from /dev/zero"); + + if (stat("/tmp/zeroes", &st)) + err(1, "copied but cannot find destination"); + if (st.st_size != 32768) + err(1, "file only %ld bytes, expected 32768", st.st_size); + + if (len != st.st_size) + errx(1, "copied correct num bytes, 32768, but copyfile() returns %ld", len); + + printf("OK => %ld\n", len); + + return 0; +} + int main(void) { - int i = 0; char *files[] = { "/etc/passwd", "/tmp/mypwd", "/tmp/mypwd", - "/etc/passwd", "/tmp/", "/tmp/passwd", - "/etc/passwd", "/tmp", "/tmp/passwd", + "/etc/passwd", "/tmp/", "/tmp/passwd", + "/etc/passwd", "/tmp", "/tmp/passwd", NULL }; FILE *src, *dst; + int i = 0; printf("=>Start testing fcopyfile()\n"); while (files[i]) { @@ -51,5 +74,5 @@ int main(void) i += 3; } - return 0; + return sz(); }