| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include "common.h"
- #include "sandbox.h"
- #include <sys/stat.h>
- #include <fcntl.h>
- /*
- _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, 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);
- return 0;
- }
- int test_write(struct s_sandboxenv *env)
- {
- 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);
- tests_release_env(&env, ¶ms);
- fakeclose(fd, &env);
- unlink("_test");
- exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
- }
|