Fix test macros if `#cond` contains `%s`

This commit is contained in:
Ruihan Li 2025-11-18 10:30:13 +08:00 committed by Tate, Hongliang Tian
parent 7ebee6d8a1
commit bdbea8e8c6
5 changed files with 50 additions and 39 deletions

View File

@ -16,7 +16,8 @@ FN_SETUP(check_child_stat)
int pipefd = -1;
FILE *file = CHECK_WITH(fopen(FILENAME, "r"), _ret != NULL);
fscanf(file, "%d %d %d", &pid, &exit_code, &pipefd);
CHECK_WITH(fscanf(file, "%d %d %d", &pid, &exit_code, &pipefd),
_ret == 3);
CHECK(fclose(file));
CHECK(unlink(FILENAME));
@ -36,13 +37,15 @@ FN_SETUP(check_child_stat)
id = flag = -1;
CHECK_WITH(stat = fopen("/proc/self/stat", "r"), stat != NULL);
fscanf(stat, "%d (execve_mt_child) %n", &id, &flag);
CHECK_WITH(fscanf(stat, "%d (execve_mt_child) %n", &id, &flag),
_ret == 1);
CHECK(fclose(stat));
CHECK_WITH(getpid(), _ret == id && flag != -1);
id = flag = -1;
CHECK_WITH(stat = fopen("/proc/thread-self/stat", "r"), stat != NULL);
fscanf(stat, "%d (execve_mt_child) %n", &id, &flag);
CHECK_WITH(fscanf(stat, "%d (execve_mt_child) %n", &id, &flag),
_ret == 1);
CHECK(fclose(stat));
CHECK_WITH(syscall(SYS_gettid), _ret == id && flag != -1);

View File

@ -47,13 +47,15 @@ int exec_child()
id = flag = -1;
CHECK_WITH(stat = fopen("/proc/self/stat", "r"), stat != NULL);
fscanf(stat, "%d (execve_mt_paren) %n", &id, &flag);
CHECK_WITH(fscanf(stat, "%d (execve_mt_paren) %n", &id, &flag),
_ret == 1);
CHECK(fclose(stat));
CHECK_WITH(getpid(), _ret == id && flag != -1);
id = flag = -1;
CHECK_WITH(stat = fopen("/proc/thread-self/stat", "r"), stat != NULL);
fscanf(stat, "%d (execve_mt_paren) %n", &id, &flag);
CHECK_WITH(fscanf(stat, "%d (execve_mt_paren) %n", &id, &flag),
_ret == 1);
CHECK(fclose(stat));
CHECK_WITH(syscall(SYS_gettid), _ret == id && flag != -1);

View File

@ -9,8 +9,6 @@
#include <sys/mman.h>
#include <sys/types.h>
#include <linux/memfd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
@ -21,8 +19,10 @@ char memfd_path[64];
FN_SETUP(create)
{
int fd = CHECK(memfd_create("test_memfd", MFD_ALLOW_SEALING));
ftruncate(fd, 4096);
CHECK(snprintf(memfd_path, sizeof(memfd_path), "/proc/self/fd/%d", fd));
CHECK(ftruncate(fd, 4096));
CHECK_WITH(snprintf(memfd_path, sizeof(memfd_path), "/proc/self/fd/%d",
fd),
_ret > 0 && _ret < sizeof(memfd_path));
}
END_SETUP()
@ -42,6 +42,8 @@ FN_TEST(path)
TEST_ERRNO(fallocate(fd, 0, 0, 100), EBADF);
TEST_ERRNO(ioctl(fd, TCGETS), EBADF);
TEST_ERRNO(mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0), EBADF);
TEST_SUCC(close(fd));
}
END_TEST()
@ -59,6 +61,8 @@ FN_TEST(readonly)
TEST_ERRNO(fallocate(fd, 0, 0, 100), EBADF);
TEST_ERRNO(ioctl(fd, TCGETS), ENOTTY);
TEST_SUCC(mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0));
TEST_SUCC(close(fd));
}
END_TEST()
@ -76,5 +80,7 @@ FN_TEST(writeonly)
TEST_SUCC(fallocate(fd, 0, 0, 100));
TEST_ERRNO(ioctl(fd, TCGETS), ENOTTY);
TEST_ERRNO(mmap(NULL, 4096, PROT_READ, MAP_PRIVATE, fd, 0), EACCES);
TEST_SUCC(close(fd));
}
END_TEST()
END_TEST()

View File

@ -9,7 +9,6 @@
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>
@ -21,24 +20,28 @@
static void fd_path(int fd, char *buf, size_t buflen)
{
CHECK(snprintf(buf, buflen, "/proc/self/fd/%d", fd));
CHECK_WITH(snprintf(buf, buflen, "/proc/self/fd/%d", fd),
_ret > 0 && _ret < buflen);
}
int get_mode(int fd)
static int get_mode(int fd)
{
char path[64];
struct stat st;
fd_path(fd, path, sizeof(path));
CHECK(stat(path, &st));
if (stat(path, &st) < 0)
return -1;
return st.st_mode & 0777;
}
void set_mode(int fd, int mode)
static int set_mode(int fd, int mode)
{
mode &= 0777;
char path[64];
fd_path(fd, path, sizeof(path));
CHECK(chmod(path, mode));
return chmod(path, mode & 0777);
}
FN_TEST(pipe_ends_share_inode)
@ -52,7 +55,7 @@ FN_TEST(pipe_ends_share_inode)
TEST_RES(get_mode(pipe2[0]), _ret == 0600);
TEST_RES(get_mode(pipe2[1]), _ret == 0600);
set_mode(pipe1[0], 0000);
TEST_SUCC(set_mode(pipe1[0], 0000));
TEST_RES(get_mode(pipe1[0]), _ret == 0000);
TEST_RES(get_mode(pipe1[1]), _ret == 0000);
@ -69,7 +72,7 @@ FN_TEST(sockets_do_not_share_inode)
TEST_RES(get_mode(sock[0]), _ret == 0777);
TEST_RES(get_mode(sock[1]), _ret == 0777);
set_mode(sock[0], 0000);
TEST_SUCC(set_mode(sock[0], 0000));
TEST_RES(get_mode(sock[0]), _ret == 0000);
TEST_RES(get_mode(sock[1]), _ret == 0777);
@ -83,14 +86,14 @@ FN_TEST(anon_inodefs_share_inode)
// eventfd
fd = TEST_SUCC(eventfd(0, EFD_CLOEXEC));
TEST_RES(get_mode(fd), _ret == 0600);
set_mode(fd, 0000);
TEST_SUCC(set_mode(fd, 0000));
TEST_RES(get_mode(fd), _ret == 0000);
TEST_SUCC(close(fd));
// timerfd
fd = TEST_SUCC(timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC));
TEST_RES(get_mode(fd), _ret == 0000);
set_mode(fd, 0111);
TEST_SUCC(set_mode(fd, 0111));
TEST_RES(get_mode(fd), _ret == 0111);
TEST_SUCC(close(fd));
@ -100,14 +103,14 @@ FN_TEST(anon_inodefs_share_inode)
TEST_SUCC(sigaddset(&mask, SIGUSR1));
fd = TEST_SUCC(signalfd(-1, &mask, SFD_CLOEXEC));
TEST_RES(get_mode(fd), _ret == 0111);
set_mode(fd, 0222);
TEST_SUCC(set_mode(fd, 0222));
TEST_RES(get_mode(fd), _ret == 0222);
TEST_SUCC(close(fd));
// epollfd
fd = TEST_SUCC(epoll_create1(EPOLL_CLOEXEC));
TEST_RES(get_mode(fd), _ret == 0222);
set_mode(fd, 0600);
TEST_SUCC(set_mode(fd, 0600));
TEST_RES(get_mode(fd), _ret == 0600);
TEST_SUCC(close(fd));
}

View File

@ -46,15 +46,14 @@
/** Ends the definition of a setup function. */
#define END_SETUP() }
#define __CHECK(func, cond) \
errno = 0; \
__auto_type _ret = (func); \
if (!(cond)) { \
fprintf(stderr, \
"fatal error: %s: `" #cond "` is false after `" #func \
"` [got %s]\n", \
__func__, strerror(errno)); \
exit(EXIT_FAILURE); \
#define __CHECK(func, cond) \
errno = 0; \
__auto_type _ret = (func); \
if (!(cond)) { \
fprintf(stderr, \
"fatal error: %s: `%s` is false after `%s` [got %s]\n", \
__func__, #cond, #func, strerror(errno)); \
exit(EXIT_FAILURE); \
}
/**
@ -105,18 +104,16 @@ static int __total_failures;
__auto_type _ret = (func); \
if (errno != (err)) { \
__tests_failed++; \
fprintf(stderr, \
"%s: `" #func "` failed [got %s, but expected %s]\n", \
__func__, strerror(errno), strerror(err)); \
fprintf(stderr, "%s: `%s` failed [got %s, but expected %s]\n", \
__func__, #func, strerror(errno), strerror(err)); \
} else if (!(cond)) { \
__tests_failed++; \
fprintf(stderr, \
"%s: `" #func "` failed [got %s, but `" #cond \
"` is false]\n", \
__func__, strerror(errno)); \
"%s: `%s` failed [got %s, but `%s` is false]\n", \
__func__, #func, strerror(errno), #cond); \
} else { \
__tests_passed++; \
fprintf(stderr, "%s: `" #func "` passed [got %s]\n", __func__, \
fprintf(stderr, "%s: `%s` passed [got %s]\n", __func__, #func, \
strerror(errno)); \
}