#include "common.h" #include "sandbox.h" #include #include /* _assertNotNull(a); _assertEqual(sllist_count(a), 0); */ int fakeopen(const char *path, int flags, int mod, struct s_sandboxenv *env) { int fd; #ifdef __x86_64__ env->syscall_no.syscall_no = env->registers.orig_rax = (unsigned long long) __NR_open; env->syscall_no.syscall_return = fd = open(path, flags, mod); env->syscall_args[0] = env->registers.rdi = (unsigned long long) path; env->syscall_args[1] = env->registers.rsi = flags; env->syscall_args[2] = env->registers.rdx = mod; #else # error "non-x86 unsupported" #endif ovr_open(env); return fd; } int fakeclose(int fd, struct s_sandboxenv *env) { #ifdef __x86_64__ env->syscall_no.syscall_no = env->registers.orig_rax = (unsigned long long) __NR_close; env->syscall_args[0] = env->registers.rdi = fd; #else # error "non-x86 unsupported" #endif ovr_close(env); return fd; } int test_open(struct s_sandboxenv *env) { t_fileinfo *fi; close(open("_test", O_CREAT, 0644)); _assertEqual(sllist_count(env->filetable), 0); int fd = fakeopen("_test", O_RDONLY, 0, env); _assertDiff(fd, -1); _assertEqual(env->syscall_no.syscall_return, fd); _assertEqual(sllist_count(env->filetable), 0); close(fd); fd = fakeopen("_test", O_WRONLY, 0, env); _assertDiff(fd, -1); _assertEqual(env->syscall_no.syscall_return, fd); _assertEqual(sllist_count(env->filetable), 1); fi = (t_fileinfo *)sllist_at(env->filetable, 0); _assertEqual(sllist_count(fi->fds), 1); _assertTrue(fi->local_fd > 0); int fd2 = fakeopen("_test", O_RDONLY, 0, env); _assertTrue(fd2 > 0); _assertEqual(sllist_count(env->filetable), 1); _assertEqual(sllist_count(fi->fds), 2); fakeclose(fd2, env); _assertEqual(sllist_count(fi->fds), 1); fakeclose(fd, env); unlink("_test"); return 0; } int test_write(struct s_sandboxenv *env) { return 0; } int main() { int success = 1; struct s_sandboxenv env; t_param params; tests_init_env(&env, ¶ms); success &= !test_open(&env); success &= !test_write(&env); tests_release_env(&env, ¶ms); exit(success ? EXIT_SUCCESS: EXIT_FAILURE); }