Test file metadata failure paths
All checks were successful
All checks were successful
Co-authored-by: Andrew Kesterson <andrew@aklabs.net>
This commit is contained in:
@@ -2,6 +2,14 @@
|
||||
#include "aksl_capture.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* A bit libc has never assigned in the AT_ space. fstatat(2) must reject it
|
||||
* with EINVAL rather than ignoring it, which is what proves flags reach libc
|
||||
* unmasked. Re-check this constant if AT_ ever grows into 0x40000000.
|
||||
*/
|
||||
#define AKSL_TEST_AT_INVALID 0x40000000
|
||||
|
||||
static int test_stat_success_and_fstat(void)
|
||||
{
|
||||
@@ -30,6 +38,7 @@ static int test_stat_success_and_fstat(void)
|
||||
|
||||
/* A descriptor that was just closed must surface libc's EBADF. */
|
||||
AKSL_CHECK_STATUS(aksl_fstat(fd, &fd_dest), EBADF);
|
||||
AKSL_CHECK_STATUS(aksl_fstatvfs(fd, &vfs_dest), EBADF);
|
||||
AKSL_CHECK(unlink(path) == 0);
|
||||
return 0;
|
||||
}
|
||||
@@ -38,15 +47,21 @@ static int test_stat_paths_and_fstatat(void)
|
||||
{
|
||||
char path[AKSL_TMP_MAX];
|
||||
char child[AKSL_TMP_MAX * 2];
|
||||
char directory[AKSL_TMP_MAX];
|
||||
char linkpath[AKSL_TMP_MAX * 2];
|
||||
char *basename = NULL;
|
||||
int dirfd = -1;
|
||||
struct stat dest;
|
||||
struct stat ldest;
|
||||
struct stat path_dest;
|
||||
struct statvfs vdest;
|
||||
/* Missing paths and a regular file used as a directory preserve errno. */
|
||||
AKSL_CHECK_STATUS(aksl_stat("/nonexistent/aksl/stat", &dest), ENOENT);
|
||||
AKSL_CHECK_STATUS(aksl_lstat("/nonexistent/aksl/stat", &ldest), ENOENT);
|
||||
/* The temporary path is a regular file, so adding a child tests ENOTDIR. */
|
||||
AKSL_CHECK(aksl_temp_file(path, sizeof(path)) == 0);
|
||||
AKSL_CHECK(snprintf(child, sizeof(child), "%s/child", path) < (int)sizeof(child));
|
||||
AKSL_CHECK_OK(aksl_stat(path, &path_dest));
|
||||
AKSL_CHECK_STATUS(aksl_stat(child, &dest), ENOTDIR);
|
||||
AKSL_CHECK(snprintf(linkpath, sizeof(linkpath), "%s.link", path) < (int)sizeof(linkpath));
|
||||
AKSL_CHECK(symlink(path, linkpath) == 0);
|
||||
@@ -57,13 +72,27 @@ static int test_stat_paths_and_fstatat(void)
|
||||
AKSL_CHECK(S_ISREG(dest.st_mode));
|
||||
AKSL_CHECK(S_ISLNK(ldest.st_mode));
|
||||
|
||||
/* AT_FDCWD makes fstatat resolve this path from the current directory. */
|
||||
AKSL_CHECK_OK(aksl_fstatat(AT_FDCWD, path, &dest, 0));
|
||||
/* A real dirfd must resolve a relative name against that directory. */
|
||||
AKSL_CHECK(snprintf(directory, sizeof(directory), "%s", path) < (int)sizeof(directory));
|
||||
basename = strrchr(directory, '/');
|
||||
AKSL_CHECK(basename != NULL);
|
||||
*basename = '\0';
|
||||
dirfd = open(directory, O_RDONLY | O_DIRECTORY);
|
||||
AKSL_CHECK(dirfd >= 0);
|
||||
AKSL_CHECK_OK(aksl_fstatat(dirfd, strrchr(path, '/') + 1, &dest, 0));
|
||||
AKSL_CHECK(dest.st_ino == path_dest.st_ino);
|
||||
AKSL_CHECK(close(dirfd) == 0);
|
||||
|
||||
/* A valid non-zero flag must reach libc, making this call an lstat. */
|
||||
AKSL_CHECK_OK(aksl_fstatat(AT_FDCWD, linkpath, &dest, AT_SYMLINK_NOFOLLOW));
|
||||
AKSL_CHECK(S_ISLNK(dest.st_mode));
|
||||
|
||||
/* Invalid flags must replace stale errno with the EINVAL libc reports. */
|
||||
errno = E2BIG;
|
||||
AKSL_CHECK_STATUS(aksl_fstatat(AT_FDCWD, path, &dest, 0x40000000), EINVAL);
|
||||
AKSL_CHECK(errno == EINVAL);
|
||||
AKSL_CHECK_STATUS(aksl_fstatat(AT_FDCWD, path, &dest, AKSL_TEST_AT_INVALID), EINVAL);
|
||||
|
||||
/* statvfs reports ENOENT before it can describe a missing filesystem path. */
|
||||
AKSL_CHECK_STATUS(aksl_statvfs("/nonexistent/aksl/stat", &vdest), ENOENT);
|
||||
|
||||
/* statvfs reports the filesystem containing the current directory. */
|
||||
AKSL_CHECK_OK(aksl_statvfs(".", &vdest));
|
||||
|
||||
Reference in New Issue
Block a user