Skip to content
Snippets Groups Projects
Commit 12d2efdf authored by Raphael S. Carvalho's avatar Raphael S. Carvalho Committed by Pekka Enberg
Browse files

tst-zfs-mount: Rename and change test to use report approach

parent 2afd6f60
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,15 @@
int sys_mount(char *dev, char *dir, char *fsname, int flags, void *data);
static int tests = 0, fails = 0;
static void report(bool ok, const char* msg)
{
++tests;
fails += !ok;
printf("%s: %s\n", (ok ? "PASS" : "FAIL"), msg);
}
int main(int argc, char **argv)
{
#define TESTDIR "/usr"
......@@ -31,41 +40,39 @@ int main(int argc, char **argv)
char path[PATH_MAX];
struct dirent *d;
struct stat s;
char foo[PATH_MAX] = { 0,};
int fd;
if (statfs("/usr", &st) < 0)
perror("statfs");
printf("f_type: %ld\n", st.f_type);
printf("f_bsize: %ld\n", st.f_bsize);
printf("f_blocks: %ld\n", st.f_blocks);
printf("f_bfree: %ld\n", st.f_bfree);
printf("f_bavail: %ld\n", st.f_bavail);
printf("f_files: %ld\n", st.f_files);
printf("f_ffree: %ld\n", st.f_ffree);
printf("f_namelen: %ld\n", st.f_namelen);
dir = opendir(TESTDIR);
if (!dir) {
perror("failed to open testdir");
return EXIT_FAILURE;
}
char foo[PATH_MAX] = { 0 };
int fd, ret;
report(statfs("/usr", &st) == 0, "stat /usr");
printf("f_type: %ld\n", st.f_type);
printf("f_bsize: %ld\n", st.f_bsize);
printf("f_blocks: %ld\n", st.f_blocks);
printf("f_bfree: %ld\n", st.f_bfree);
printf("f_bavail: %ld\n", st.f_bavail);
printf("f_files: %ld\n", st.f_files);
printf("f_ffree: %ld\n", st.f_ffree);
printf("f_namelen: %ld\n", st.f_namelen);
report((dir = opendir(TESTDIR)), "open testdir");
while ((d = readdir(dir))) {
if (strcmp(d->d_name, ".") == 0 ||
strcmp(d->d_name, "..") == 0) {
printf("found hidden entry %s\n", d->d_name);
printf("found hidden entry %s\n", d->d_name);
continue;
}
snprintf(path, PATH_MAX, "%s/%s", TESTDIR, d->d_name);
if (stat(path, &s) < 0) {
report((ret = stat(path, &s)) == 0, "stat file");
if (ret < 0) {
printf("failed to stat %s\n", path);
continue;
}
if (!S_ISREG(s.st_mode) && !S_ISDIR(s.st_mode)) {
report((ret = (S_ISREG(s.st_mode) || S_ISDIR(s.st_mode))),
"entry must be a regular file");
if (!ret) {
printf("ignoring %s, not a regular file\n", path);
continue;
}
......@@ -73,81 +80,40 @@ int main(int argc, char **argv)
printf("found %s\tsize: %ld\n", d->d_name, s.st_size);
}
if (closedir(dir) < 0) {
perror("failed to close testdir");
return EXIT_FAILURE;
}
if (mkdir("/usr/testdir", 0777) < 0) {
perror("mkdir");
return EXIT_FAILURE;
}
if (stat("/usr/testdir", &s) < 0) {
perror("stat dir");
return EXIT_FAILURE;
}
report(closedir(dir) == 0, "close testdir");
report(mkdir("/usr/testdir", 0777) == 0, "mkdir /usr/testdir (0777)");
report(stat("/usr/testdir", &s) == 0, "stat /usr/testdir");
fd = open("/usr/foo", O_CREAT|O_TRUNC|O_WRONLY|O_SYNC, 0666);
if (fd < 0) {
perror("creat");
return EXIT_FAILURE;
}
report(fd > 0, "create /usr/foo");
if (write(fd, &foo, sizeof(foo)) != sizeof(foo)) {
perror("write");
return EXIT_FAILURE;
}
report(write(fd, &foo, sizeof(foo)) == sizeof(foo), "write sizeof(foo) bytes to fd");
report(fsync(fd) == 0, "fsync fd");
report(fstat(fd, &s) == 0, "fstat fd");
if (fsync(fd) < 0) {
perror("fsync");
return EXIT_FAILURE;
}
if (fstat(fd, &s) < 0) {
perror("fstat");
return EXIT_FAILURE;
}
printf("file size = %lld\n", s.st_size);
close(fd);
report(close(fd) == 0, "close fd");
fd = creat("/usr/foo", 0666);
if (fd < 0) {
perror("creat");
return EXIT_FAILURE;
}
report(fd > 0, "possibly create /usr/foo again");
if (fstat(fd, &s) < 0) {
perror("fstat");
return EXIT_FAILURE;
}
report(fstat(fd, &s) == 0, "fstat fd");
printf("file size = %lld (after O_TRUNC)\n", s.st_size);
close(fd);
report(close(fd) == 0, "close fd again");
if (rename("/usr/foo", "/usr/foo2")) {
perror("rename simple");
return EXIT_FAILURE;
}
report(rename("/usr/foo", "/usr/foo2") == 0,
"rename /usr/foo to /usr/foo2");
if (rename("/usr/foo2", "/usr/testdir/foo")) {
perror("rename cross dir");
return EXIT_FAILURE;
}
report(rename("/usr/foo2", "/usr/testdir/foo") == 0,
"rename /usr/foo2 to /usr/testdir/foo");
if (unlink("/usr/testdir/foo") < 0) {
perror("unlink");
return EXIT_FAILURE;
}
report(unlink("/usr/testdir/foo") == 0, "unlink /usr/testdir/foo");
if (rename("/usr/testdir", "/usr/testdir2")) {
perror("rename dir");
return EXIT_FAILURE;
}
report(rename("/usr/testdir", "/usr/testdir2") == 0,
"rename /usr/testdir to /usr/testdir2");
if (rmdir("/usr/testdir2") < 0) {
perror("rmdir");
return EXIT_FAILURE;
}
report(rmdir("/usr/testdir2") == 0, "rmdir /usr/testdir2");
#if 0
fd = open("/mnt/tests/tst-zfs-simple.c", O_RDONLY);
......@@ -172,5 +138,8 @@ int main(int argc, char **argv)
// rbuf[BUF_SIZE] = '\0';
// printf("%s\n", rbuf);
#endif
// Report results.
printf("SUMMARY: %d tests, %d failures\n", tests, fails);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment