From: Lucas De Marchi Date: Wed, 25 Jan 2012 13:36:28 +0000 (-0200) Subject: testsuite: add trap to open() including tests X-Git-Tag: v5~67 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7fa8c2d2df4d4c881bfe53309e5e5772cf507d9b;p=thirdparty%2Fkmod.git testsuite: add trap to open() including tests --- diff --git a/testsuite/path.c b/testsuite/path.c index 9b435b49..cd2a7d49 100644 --- a/testsuite/path.c +++ b/testsuite/path.c @@ -1,10 +1,14 @@ #include #include +#include #include #include #include +#include #include #include +#include +#include #include "testsuite.h" @@ -88,3 +92,30 @@ TS_EXPORT FILE *fopen(const char *path, const char *mode) return (void *) (long) (*_fopen)(p, mode); } + +TS_EXPORT int open(const char *path, int flags, ...) +{ + const char *p; + char buf[PATH_MAX * 2]; + static int (*_open)(const char *path, int flags, ...); + + if (!get_rootpath(__func__)) + return -1; + + _open = get_libc_func("open"); + p = trap_path(path, buf); + if (p == NULL) + return -1; + + if (flags & O_CREAT) { + mode_t mode; + va_list ap; + + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + _open(p, flags, mode); + } + + return _open(p, flags); +} diff --git a/testsuite/test-testsuite.c b/testsuite/test-testsuite.c index f5b6fceb..ec541524 100644 --- a/testsuite/test-testsuite.c +++ b/testsuite/test-testsuite.c @@ -67,9 +67,46 @@ static const struct test stestsuite_rootfs_fopen = { .need_spawn = true, }; +static int testsuite_rootfs_open(const struct test *t) +{ + char buf[100]; + int fd, done; + + fd = open("/lib/modules/a", O_RDONLY); + if (fd < 0) + return EXIT_FAILURE; + + for (done = 0;;) { + int r = read(fd, buf + done, sizeof(buf) - 1 - done); + if (r == 0) + break; + if (r == -EWOULDBLOCK || r == -EAGAIN) + continue; + + done += r; + } + + buf[done] = '\0'; + + if (strcmp(buf, "kmod-test-chroot-works\n") != 0) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} +static const struct test stestsuite_rootfs_open = { + .name = "testsuite_rootfs_open", + .description = "test if rootfs works - open()", + .func = testsuite_rootfs_open, + .config = { + [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/", + }, + .need_spawn = true, +}; + static const struct test *tests[] = { &stestsuite_uname, &stestsuite_rootfs_fopen, + &stestsuite_rootfs_open, NULL, };