Skip to content

Commit

Permalink
Merge pull request #773 from wangvsa/dev
Browse files Browse the repository at this point in the history
Add tests for stat(), lstat() and fstat()
  • Loading branch information
adammoody authored Jun 5, 2023
2 parents 4238eb1 + 8c67e92 commit e07093f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 3 deletions.
10 changes: 10 additions & 0 deletions client/src/unifyfs-sysio.c
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,11 @@ int UNIFYFS_WRAP(fstat)(int fd, struct stat* buf)
/* check whether we should intercept this file descriptor */
if (unifyfs_intercept_fd(&fd)) {
int fid = unifyfs_get_fid_from_fd(fd);
/* check if the file is still active (e.g., not closed)*/
if (fid == -1) {
errno = EBADF;
return -1;
}
const char* path = unifyfs_path_from_fid(posix_client, fid);
int ret = __stat(path, buf);
return ret;
Expand Down Expand Up @@ -892,6 +897,11 @@ int UNIFYFS_WRAP(__fxstat)(int vers, int fd, struct stat* buf)
}

int fid = unifyfs_get_fid_from_fd(fd);
/* check if the file is still active (e.g., not closed) */
if (fid == -1) {
errno = EBADF;
return -1;
}
const char* path = unifyfs_path_from_fid(posix_client, fid);
int ret = __stat(path, buf);
return ret;
Expand Down
3 changes: 2 additions & 1 deletion t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ test_sysio_sources = \
sys/write-read-hole.c \
sys/truncate.c \
sys/unlink.c \
sys/chdir.c
sys/chdir.c \
sys/stat.c

sys_sysio_gotcha_t_CPPFLAGS = $(test_cppflags)
sys_sysio_gotcha_t_LDADD = $(test_gotcha_ldadd)
Expand Down
2 changes: 1 addition & 1 deletion t/sharness.d/00-test-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export UNIFYFS_TEST_RUN_SCRIPT=$UNIFYFS_BUILD_DIR/t/test_run_env.sh
if test -n "$(which jsrun 2>/dev/null)"; then
JOB_RUN_COMMAND="jsrun -r1 -n1"
elif test -n "$(which srun 2>/dev/null)"; then
JOB_RUN_COMMAND="srun -n1 -N1"
JOB_RUN_COMMAND="srun -n1 -N1 --overlap"
elif test -n "$(which mpirun 2>/dev/null)"; then
JOB_RUN_COMMAND="mpirun -np 1"
if [ $UID -eq 0 ]; then
Expand Down
121 changes: 121 additions & 0 deletions t/sys/stat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2019, Lawrence Livermore National Security, LLC.
* Produced at the Lawrence Livermore National Laboratory.
*
* Copyright 2018, UT-Battelle, LLC.
*
* LLNL-CODE-741539
* All rights reserved.
*
* This is the license for UnifyFS.
* For details, see https://github.com/LLNL/UnifyFS.
* Please read https://github.com/LLNL/UnifyFS/LICENSE for full license text.
*/

/*
* Test stat, lstat, and fstat
*/
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <linux/limits.h>
#include <stdio.h>
#include <sys/stat.h>
#include "t/lib/tap.h"
#include "t/lib/testutil.h"

int stat_test(char* unifyfs_root)
{
diag("Starting UNIFYFS_WRAP(stat) tests");

char path[64];
char dir_path[64];
int err, fd, rc;
struct stat sb = {0};

testutil_rand_path(path, sizeof(path), unifyfs_root);
testutil_rand_path(dir_path, sizeof(dir_path), unifyfs_root);

errno = 0;
rc = stat(path, &sb);
err = errno;
ok(rc == -1 && err == ENOENT,
"%s:%d stat() non-existent file fails (errno=%d): %s",
__FILE__, __LINE__, err, strerror(err));

errno = 0;
rc = lstat(path, &sb);
err = errno;
ok(rc == -1 && err == ENOENT,
"%s:%d lstat() non-existent file fails (errno=%d): %s",
__FILE__, __LINE__, err, strerror(err));

errno = 0;
fd = creat(path, 0222);
err = errno;
ok(fd != -1 && err == 0, "%s:%d creat(%s) (fd=%d): %s",
__FILE__, __LINE__, path, fd, strerror(err));

errno = 0;
rc = fsync(fd);
err = errno;
ok(rc == 0 && err == 0, "%s:%d fsync(): %s",
__FILE__, __LINE__, strerror(err));

errno = 0;
rc = close(fd);
err = errno;
ok(rc == 0 && err == 0, "%s:%d close(): %s",
__FILE__, __LINE__, strerror(err));

errno = 0;
rc = stat(path, &sb);
err = errno;
ok(rc == 0 && err == 0, "%s:%d stat(): %s",
__FILE__, __LINE__, strerror(err));

errno = 0;
rc = lstat(path, &sb);
err = errno;
ok(rc == 0 && err == 0, "%s:%d lstat(): %s",
__FILE__, __LINE__, strerror(err));

errno = 0;
rc = fstat(fd, &sb);
err = errno;
ok(rc == -1 && err == EBADF,
"%s:%d fstat() after close fails (errno=%d): %s",
__FILE__, __LINE__, err, strerror(err));

errno = 0;
rc = unlink(path);
err = errno;
ok(rc == 0 && err == 0,
"%s:%d unlink() empty file: %s",
__FILE__, __LINE__, strerror(err));

errno = 0;
rc = stat(path, &sb);
err = errno;
ok(rc == -1 && err == ENOENT,
"%s:%d stat() after unlink fails (errno=%d): %s",
__FILE__, __LINE__, err, strerror(err));

errno = 0;
rc = lstat(path, &sb);
err = errno;
ok(rc == -1 && err == ENOENT,
"%s:%d lstat() after unlink fails (errno=%d): %s",
__FILE__, __LINE__, err, strerror(err));

errno = 0;
rc = fstat(fd, &sb);
err = errno;
ok(rc == -1 && err == EBADF,
"%s:%d fstat() after unlink fails (errno=%d): %s",
__FILE__, __LINE__, err, strerror(err));

diag("Finished UNIFYFS_WRAP(stat) tests");

return 0;
}
2 changes: 2 additions & 0 deletions t/sys/sysio_suite.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ int main(int argc, char* argv[])

chdir_test(unifyfs_root);

stat_test(unifyfs_root);

rc = unifyfs_unmount();
ok(rc == 0, "unifyfs_unmount(%s) (rc=%d)", unifyfs_root, rc);

Expand Down
3 changes: 3 additions & 0 deletions t/sys/sysio_suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ int unlink_test(char* unifyfs_root);

int chdir_test(char* unifyfs_root);

/* Test for UNIFYFS_WRAP(stat, lstat, fstat) */
int stat_test(char* unifyfs_root);

#endif /* SYSIO_SUITE_H */
2 changes: 1 addition & 1 deletion t/sys/unlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static int unlink_after_sync_laminate_test(char* unifyfs_root)

int unlink_test(char* unifyfs_root)
{
diag("Finished UNIFYFS_WRAP(unlink) tests");
diag("Starting UNIFYFS_WRAP(unlink) tests");

char path[64];
char dir_path[64];
Expand Down

0 comments on commit e07093f

Please sign in to comment.