testsuite: add trap to open() including tests

This commit is contained in:
Lucas De Marchi 2012-01-25 11:36:28 -02:00
parent 6afc9cd616
commit 7fa8c2d2df
2 changed files with 68 additions and 0 deletions

View 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);
}

View 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,
};