testsuite: add trap to stat() and friends including tests
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 25 Jan 2012 14:22:50 +0000 (12:22 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Thu, 26 Jan 2012 18:05:04 +0000 (16:05 -0200)
Add trap to stat(): we need to trap other functions too, depending on
stat.h, the function from glibc that is actually called may be stat64 or
__xstat() too.

testsuite/path.c
testsuite/test-testsuite.c

index cd2a7d4..ad750f9 100644 (file)
@@ -9,6 +9,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <unistd.h>
 
 #include "testsuite.h"
 
@@ -119,3 +120,76 @@ TS_EXPORT int open(const char *path, int flags, ...)
 
        return _open(p, flags);
 }
+
+TS_EXPORT int stat(const char *path, struct stat *st)
+{
+       const char *p;
+       char buf[PATH_MAX * 2];
+       static int (*_stat)(const char *path, struct stat *buf);
+
+       if (!get_rootpath(__func__))
+               return -1;
+
+       _stat = get_libc_func("stat");
+
+       p = trap_path(path, buf);
+       if (p == NULL)
+               return -1;
+
+       return _stat(p, st);
+}
+
+TS_EXPORT int stat64(const char *path, struct stat64 *st)
+{
+       const char *p;
+       char buf[PATH_MAX * 2];
+       static int (*_stat64)(const char *path, struct stat64 *buf);
+
+       if (!get_rootpath(__func__))
+               return -1;
+
+       _stat64 = get_libc_func("stat64");
+
+       p = trap_path(path, buf);
+       if (p == NULL)
+               return -1;
+
+       return _stat64(p, st);
+}
+
+TS_EXPORT int __xstat(int ver, const char *path, struct stat *st)
+{
+       const char *p;
+       char buf[PATH_MAX * 2];
+       static int (*_xstat)(int __ver, const char *__filename,
+                                               struct stat *__stat_buf);
+
+       if (!get_rootpath(__func__))
+               return -1;
+
+       _xstat = get_libc_func("__xstat");
+
+       p = trap_path(path, buf);
+       if (p == NULL)
+               return -1;
+
+       return _xstat(ver, p, st);
+}
+
+TS_EXPORT int access(const char *path, int mode)
+{
+       const char *p;
+       char buf[PATH_MAX * 2];
+       static int (*_access)(const char *path, int mode);
+
+       if (!get_rootpath(__func__))
+               return -1;
+
+       _access = get_libc_func("access");
+
+       p = trap_path(path, buf);
+       if (p == NULL)
+               return -1;
+
+       return _access(p, mode);
+}
index ec54152..84f0e07 100644 (file)
@@ -4,6 +4,8 @@
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <sys/utsname.h>
 #include <libkmod.h>
 
@@ -103,10 +105,37 @@ static const struct test stestsuite_rootfs_open = {
        .need_spawn = true,
 };
 
+static int testsuite_rootfs_stat_access(const struct test *t)
+{
+       struct stat st;
+
+       if (access("/lib/modules/a", F_OK) < 0) {
+               ERR("access failed: %m\n");
+               return EXIT_FAILURE;
+       }
+
+       if (stat("/lib/modules/a", &st) < 0) {
+               ERR("stat failed: %m\n");
+               return EXIT_FAILURE;
+       }
+
+       return EXIT_SUCCESS;
+}
+static const struct test stestsuite_rootfs_stat_access = {
+       .name = "testsuite_rootfs_stat_access",
+       .description = "test if rootfs works - stat() and access()",
+       .func = testsuite_rootfs_stat_access,
+       .config = {
+               [TC_ROOTFS] = TESTSUITE_ROOTFS "test-rootfs/",
+       },
+       .need_spawn = true,
+};
+
 static const struct test *tests[] = {
        &stestsuite_uname,
        &stestsuite_rootfs_fopen,
        &stestsuite_rootfs_open,
+       &stestsuite_rootfs_stat_access,
        NULL,
 };