Skip to content

Commit

Permalink
test: major refactor and cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
  • Loading branch information
troglobit committed Apr 8, 2023
1 parent 63cf886 commit 311288e
Showing 1 changed file with 90 additions and 67 deletions.
157 changes: 90 additions & 67 deletions test/copyfile.c
Expand Up @@ -2,8 +2,8 @@
#include <errno.h>
#include "check.h"

#define ERR(fmt, args...) { printf(fmt, ##args); rc = 1; }
#define ERRX(fmt, args...) { printf(fmt, ##args); rc = 1; }
#define ERR(fmt, args...) { printf(fmt ": %s\n", ##args, strerror(errno)); rc = 1; }
#define ERRX(fmt, args...) { printf(fmt "\n", ##args); rc = 1; }

static char *files[] = {
"/etc/passwd", "/tmp/mypwd", "/tmp/mypwd",
Expand All @@ -12,97 +12,120 @@ static char *files[] = {
NULL
};

static int sz()
static int one_sz(const char *src, const char *dst, int sz)
{
struct stat st;
ssize_t len;
int rc = 0;

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");
len = copyfile(src, dst, sz, 0);
if (!len) {
ERR("failed copying any bytes from %s", src);
} else {
if (stat(dst, &st))
ERR("copied but cannot find destination %s", dst);

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 (st.st_size != sz)
ERR("file only %ld bytes, expected %d", st.st_size, sz);

if (len != st.st_size)
errx(1, "copied correct num bytes, 32768, but copyfile() returns %ld", len);

printf("OK => %ld\n", len);
if (len != st.st_size)
ERRX("copied expected data %d, but copyfile() says %ld", sz, len);
}

return 0;
return rc;
}

static int check_fcopyfile()
static int _sz()
{
FILE *src, *dst;
int rc, i;
const int sz = 32768;

printf("=>Start testing fcopyfile()\n");
for (i = 0; files[i]; i += 3) {
printf("fcopyfile(%s, %s)\t", files[i], files[i + 1]);
src = fopen(files[i], "r");
dst = fopen(files[i + 1], "w");

rc = fcopyfile(src, dst);

if (src)
fclose(src);
if (dst)
fclose(dst);

if (rc) {
if (!fisdir(files[i + 1]))
err(1, "Failed fcopyfile(%s, %s)", files[i], files[i + 1]);

/* Expected to fail, user tried to copy to fp=NULL due to dir */
printf("OK\n");
continue;
}

if (fexist(files[i + 2])) {
if (systemf("diff -q %s %s", files[i], files[i + 2])) {
ERRX("src and dst differ");
} else
printf("OK => %s", files[i + 2]);
return test(one_sz("/dev/zero", "/tmp/zeroes", sz), "copyfile(/dev/zero, %d)", sz);
}

static int fcopyfile_one(const char *src, const char *dst, const char *expected)
{
FILE *from, *to;
int rc;

from = fopen(src, "r");
to = fopen(dst, "w");

rc = fcopyfile(from, to);

if (from)
fclose(from);
if (to)
fclose(to);

if (rc) {
if (!src || !dst)
rc = 0; /* expected */
else if (fisdir(dst))
rc = 0; /* expected */
} else {
if (fexist(expected)) {
if (systemf("diff -q %s %s", src, expected))
rc = 1;
} else
ERR("cannot find %s", files[i + 2]);
printf("\n");
ERR("cannot find %s", expected);
}

erase(expected);

erase(files[i + 2]);
return rc;
}

static int _fcopyfile()
{
int i, rc = 0;

if (rc)
return rc;
for (i = 0; files[i]; i += 3) {
char *src = files[i];
char *dst = files[i + 1];
char *res = files[i + 2];

rc += test(fcopyfile_one(src, dst, res), "fcopyfile(%s, %s)", src, dst);
}

return 0;
return rc;
}

static int check_copyfile()
/* only checks success, correct file size verified by _sz() */
static int copyfile_one(const char *src, const char *dst, const char *expected)
{
int i;
int rc;

printf("\n=>Start testing copyfile()\n");
for (i = 0; files[i]; i += 3) {
printf("copyfile(%s, %s)\t", files[i], files[i + 1]);
if (!copyfile(files[i], files[i + 1], 0, 0))
err(1, "Failed copyfile(%s, %s)", files[i], files[i + 1]);
rc = copyfile(src, dst, 0, 0);
if (!rc)
rc = 1;
else if (!fexist(expected))
rc = 1;
else
rc = 0;

if (fexist(files[i + 2]))
printf("OK => %s", files[i + 2]);
printf("\n");
erase(expected);

return rc;
}

static int _copyfile()
{
int i, rc = 0;

for (i = 0; files[i]; i += 3) {
char *src = files[i];
char *dst = files[i + 1];
char *res = files[i + 2];

erase(files[i + 2]);
rc += test(copyfile_one(src, dst, res), "copyfile(%s, %s)", src, dst);
}

return 0;
return rc;
}

int main(void)
{
return check_fcopyfile() ||
check_copyfile() ||
sz();
return _fcopyfile() ||
_copyfile() ||
_sz();
}

0 comments on commit 311288e

Please sign in to comment.