| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #include "common.h"
- #include "sandbox.h"
- #include <sys/stat.h>
- #include <string.h>
- #include <fcntl.h>
- /*
- _assertNotNull(a);
- _assertEqual(sllist_count(a), 0);
- */
- int fakeread(int fd, char *buf, int buflen, struct s_sandboxenv *env)
- {
- #ifdef __x86_64__
- env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_read;
- env->syscall_args[0] = env->registers.rdi = (REGISTER_TYPE) fd;
- env->syscall_args[1] = env->registers.rsi = (REGISTER_TYPE) buf;
- env->syscall_args[2] = env->registers.rdx = (REGISTER_TYPE) buflen;
- #else
- # error "non-x86 unsupported"
- #endif
- ovr_read(env);
- return fd;
- }
- int fakelseek(int fd, off_t offset, int whence, struct s_sandboxenv *env)
- {
- #ifdef __x86_64__
- env->syscall_no.syscall_no = env->registers.orig_rax = (REGISTER_TYPE) __NR_lseek;
- env->syscall_args[0] = env->registers.rdi = (REGISTER_TYPE) fd;
- env->syscall_args[1] = env->registers.rsi = (REGISTER_TYPE) offset;
- env->syscall_args[2] = env->registers.rdx = (REGISTER_TYPE) whence;
- #else
- # error "non-x86 unsupported"
- #endif
- ovr_lseek(env);
- return fd;
- }
- 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)
- {
- struct stat st;
- char buf[4];
- _assertEqual(fakewrite(fd, "test", 4, env), 4);
- _assertEqual(stat("_test", &st), 0);
- _assertEqual(st.st_size, 0);
- #warning TODO
- return 0;
- _assertEqual(fakelseek(fd, 0, SEEK_SET, env), 0);
- _assertEqual(fakeread(fd, buf, 4, env), 4);
- _assertEqual(strcmp(buf, "test"), 0);
- 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);
- }
|