From b80c2f6e4e4a274bca9dd3d56a51c2fed2d0e141 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Mon, 5 Nov 2012 12:08:52 -0500 Subject: add tests for fcntl (so far, just locking) this is mainly to test for incorrect macro/struct definitions --- fcntl.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ testsuite.c | 1 + 2 files changed, 53 insertions(+) create mode 100644 fcntl.c diff --git a/fcntl.c b/fcntl.c new file mode 100644 index 0000000..577d781 --- /dev/null +++ b/fcntl.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +#define TEST2(c, l, ...) ((c) ? 1 : \ +(err++,printf(__FILE__":"#l": "#c" failed: " __VA_ARGS__),putchar('\n'),0)) +#define TEST1(c, l, ...) TEST2(c, l, __VA_ARGS__) +#define TEST(c, ...) TEST1(c, __LINE__, __VA_ARGS__) + +#define TESTE(c) TEST(c, "errno = %s", strerror(errno)) + +int test_fcntl(void) +{ + int err = 0; + struct flock fl = {0}; + FILE *f; + int fd; + pid_t pid; + int status; + + if (!TESTE(f=tmpfile())) return err; + fd = fileno(f); + + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; + TESTE(fcntl(fd, F_SETLK, &fl)==0); + + pid = fork(); + if (!pid) { + fl.l_type = F_RDLCK; + _exit(fcntl(fd, F_SETLK, &fl)==0 || + (errno!=EAGAIN && errno!=EACCES)); + } + while (waitpid(pid, &status, 0)<0 && errno==EINTR); + TEST(status==0, "lock failed to work"); + + pid = fork(); + if (!pid) { + fl.l_type = F_WRLCK; + _exit(fcntl(fd, F_GETLK, &fl) || fl.l_pid != getppid()); + } + while (waitpid(pid, &status, 0)<0 && errno==EINTR); + TEST(status==0, "child failed to detect lock held by parent"); + + fclose(f); + + return err; +} diff --git a/testsuite.c b/testsuite.c index d8bd773..c353735 100644 --- a/testsuite.c +++ b/testsuite.c @@ -13,6 +13,7 @@ int main() int err=0; RUN_TEST(fdopen); + RUN_TEST(fcntl); RUN_TEST(fnmatch); RUN_TEST(fscanf); RUN_TEST(popen); -- cgit v1.2.1