]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: add trap to open() including tests
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 25 Jan 2012 13:36:28 +0000 (11:36 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Thu, 26 Jan 2012 18:05:04 +0000 (16:05 -0200)
testsuite/path.c
testsuite/test-testsuite.c

index 9b435b4927655a36b46bb09575041b3924a1441a..cd2a7d49c0d90b0ff6f306c6c3fe084aed25a060 100644 (file)
@@ -1,10 +1,14 @@
 #include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <dlfcn.h>
 #include <limits.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #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);
+}
index f5b6fceb573c5ebfcbb41290540684ce91eea95e..ec5415240d0b542df2ef660fa3661b6ca2506c6f 100644 (file)
@@ -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,
 };