diff options
author | Rich Felker <dalias@aerifal.cx> | 2012-11-05 12:08:52 -0500 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2012-11-05 12:08:52 -0500 |
commit | b80c2f6e4e4a274bca9dd3d56a51c2fed2d0e141 (patch) | |
tree | 704cb44e4706ec45618ad030603440a05a7fa5fc | |
parent | 182deaf9d461df3a22906ed611544a2560bc0752 (diff) | |
download | libc-testsuite-b80c2f6e4e4a274bca9dd3d56a51c2fed2d0e141.tar.gz |
add tests for fcntl (so far, just locking)
this is mainly to test for incorrect macro/struct definitions
-rw-r--r-- | fcntl.c | 52 | ||||
-rw-r--r-- | testsuite.c | 1 |
2 files changed, 53 insertions, 0 deletions
@@ -0,0 +1,52 @@ +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> + +#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); |