#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 = (REGISTER_TYPE) __NR_open; env->syscall_no.syscall_return = fd = open(path, flags, mod); env->syscall_args[0] = env->registers.rdi = (REGISTER_TYPE) 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 = (REGISTER_TYPE) __NR_close; env->syscall_args[0] = env->registers.rdi = fd; #else # error "non-x86 unsupported" #endif ovr_close(env); return fd; } int fakewrite(int fd, char *buf, int len, struct s_sandboxenv *env) { #ifdef __x86_64__ env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_write; env->syscall_args[0] = env->registers.rdi = fd; env->syscall_args[1] = env->registers.rsi = (REGISTER_TYPE) buf; env->syscall_args[2] = env->registers.rdx = len; #else # error "non-x86 unsupported" #endif ovr_write(env); if (env->registers.orig_rax == __NR_write) env->registers.rax = write(fd, buf, len); return env->registers.rax; } int test_open(struct s_sandboxenv *env, int *final_fd) { t_fileinfo *fi; t_fd *fd_struct; 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, 0646, 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); fd_struct = sllist_at(fi->fds, 0); _assertNotNull(fd_struct); _assertEqual(fd_struct->fd, fd); _assertEqual(fd_struct->flags, O_WRONLY); _assertEqual(fd_struct->mod, 0646); *final_fd = fd; return 0; } int test_write(struct s_sandboxenv *env, int fd) { _assertEqual(fakewrite(fd, "test", 5, env), 5); #warning TODO return 0; } int main() { int success = 1; struct s_sandboxenv env; int fd; t_param params; tests_init_env(&env, ¶ms); success &= !test_open(&env, &fd); success &= !test_write(&env, fd); fakeclose(fd, &env); tests_release_env(&env, ¶ms); unlink("_test"); exit(success ? EXIT_SUCCESS: EXIT_FAILURE); }