소스 검색

ovr_write, unit tests

isundil 9 년 전
부모
커밋
2ff4ed57bb
12개의 변경된 파일354개의 추가작업 그리고 260개의 파일을 삭제
  1. 2 2
      CMakeLists.txt
  2. 14 14
      src/apply.c
  3. 6 6
      src/apply_fs.c
  4. 62 62
      src/environment.c
  5. 9 0
      src/ovr_syscall/ovr_lseek.c
  6. 44 43
      src/ovr_syscall/ovr_open.c
  7. 10 0
      src/ovr_syscall/ovr_read.c
  8. 37 11
      src/ovr_syscall/ovr_write.c
  9. 38 38
      src/pathutil.c
  10. 27 19
      src/sandbox.h
  11. 2 0
      src/sandbox_syscall.h
  12. 103 65
      test/write/main.c

+ 2 - 2
CMakeLists.txt

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8)
 
 add_library(sllist STATIC lib/sllist/src/create.c lib/sllist/src/add.c lib/sllist/src/at.c lib/sllist/src/del.c lib/sllist/src/find.c lib/sllist/src/remove.c)
 add_executable(sandbox src/main.c src/mem.c src/apply.c src/apply_fs.c src/exec.c src/param.c src/sandbox.c src/environment.c src/pathutil.c
-	src/ovr_syscall/ovr_write.c src/ovr_syscall/ovr_open.c src/ovr_syscall/ovr_close.c)
+	src/ovr_syscall/ovr_write.c src/ovr_syscall/ovr_open.c src/ovr_syscall/ovr_close.c src/ovr_syscall/ovr_lseek.c src/ovr_syscall/ovr_read.c)
 
 set_property(TARGET sandbox PROPERTY RUNTIME_OUTPUT_DIRECTORY bin)
 set_property(TARGET sllist  PROPERTY LIBRARY_OUTPUT_DIRECTORY lib)
@@ -18,7 +18,7 @@ add_executable(test_sllist test/sllist/main.c)
 set_property(TARGET test_sllist PROPERTY RUNTIME_OUTPUT_DIRECTORY test/bin)
 target_link_libraries(test_sllist sllist)
 
-add_executable(test_write test/write/main.c test/write/functions.c src/ovr_syscall/ovr_close.c src/ovr_syscall/ovr_open.c src/ovr_syscall/ovr_write.c src/pathutil.c)
+add_executable(test_write test/write/main.c test/write/functions.c src/ovr_syscall/ovr_close.c src/ovr_syscall/ovr_open.c src/ovr_syscall/ovr_write.c src/ovr_syscall/ovr_lseek.c src/ovr_syscall/ovr_read.c src/pathutil.c)
 set_property(TARGET test_write PROPERTY RUNTIME_OUTPUT_DIRECTORY test/bin)
 target_link_libraries(test_write sllist)
 

+ 14 - 14
src/apply.c

@@ -4,24 +4,24 @@
 
 void sandbox_apply(struct s_sandboxenv *env)
 {
-	apply_fs(env);
+    apply_fs(env);
 }
 
 void prompt_sandbox(struct s_sandboxenv *env)
 {
-	char *c = NULL;
-	size_t size;
+    char *c = NULL;
+    size_t size;
 
-	do
-	{
-		if (c)
-			free(c);
-		c = NULL;
-		printf("Apply (a) / Cancel (c) changes ?\n");
-		getline(&c, &size, stdin);
-	} while (strcmp("a\n", c) && strcmp("c\n", c));
-	if (*c == 'c')
-		return;
-	sandbox_apply(env);
+    do
+    {
+        if (c)
+            free(c);
+        c = NULL;
+        printf("Apply (a) / Cancel (c) changes ?\n");
+        getline(&c, &size, stdin);
+    } while (strcmp("a\n", c) && strcmp("c\n", c));
+    if (*c == 'c')
+        return;
+    sandbox_apply(env);
 }
 

+ 6 - 6
src/apply_fs.c

@@ -2,17 +2,17 @@
 
 int apply_file(void **_data, void *_env)
 {
-	struct s_sandboxenv *env = * (struct s_sandboxenv **) _env;
-	t_fileinfo *fi = (t_fileinfo *) _data;
+    struct s_sandboxenv *env = * (struct s_sandboxenv **) _env;
+    t_fileinfo *fi = (t_fileinfo *) _data;
 
 #warning todo
-	(void) fi;
-	(void) env;
-	return 0;
+    (void) fi;
+    (void) env;
+    return 0;
 }
 
 void apply_fs(struct s_sandboxenv *env)
 {
-	sllist_foreach(env->filetable, apply_file, env);
+    sllist_foreach(env->filetable, apply_file, env);
 }
 

+ 62 - 62
src/environment.c

@@ -7,77 +7,77 @@
 
 static char *check_file(const char *fname)
 {
-	const char *pos;
-	char *path = getenv("PATH");
-	char *dir;
-	char *fullpath = NULL;
-	int fullpath_len;
-	struct stat cmd_stat;
+    const char *pos;
+    char *path = getenv("PATH");
+    char *dir;
+    char *fullpath = NULL;
+    int fullpath_len;
+    struct stat cmd_stat;
 
-	for (pos = fname; *pos && *pos != '/'; ++pos);
-	if (*pos && (stat(fname, &cmd_stat) == -1))
-	{
-		fprintf(stderr, "Cannot stat %s: %s\n", fname, strerror(errno));
-		return NULL;
-	}
-	else if (!*pos && !path)
-	{
-		fprintf(stderr, "Cannot stat %s: No such file or directory\n", fname);
-		return NULL;
-	}
-	if (!*pos)
-		for ((dir = strtok(path, ":")); dir; dir = strtok(NULL, ":"))
-		{
-			int len = strlen(dir);
-			if (fullpath == NULL || fullpath_len < len)
-			{
-				if (fullpath)
-					fullpath = realloc(fullpath, sizeof(*fullpath) * (strlen(fname) + 2 + len));
-				else
-					fullpath = malloc(sizeof(*fullpath) * (strlen(fname) + len +2));
-				fullpath_len = len;
-			}
-			sprintf(fullpath, "%s/%s", dir, fname);
-			if (stat(fullpath, &cmd_stat) != -1 && !S_ISDIR(cmd_stat.st_mode))
-				return fullpath;
-		}
-	if (S_ISDIR(cmd_stat.st_mode))
-	{
-		fprintf(stderr, "Cannot execute %s: is a directory\n", fname);
-		return NULL;
-	}
-	if (!fullpath && *pos)
-		fullpath = strdup(fname);
-	else if (!*pos)
-	{
-		free(fullpath);
-		fprintf(stderr, "Cannot execute %s: command not found\n", fname);
-		fullpath = NULL;
-	}
-	return fullpath;
+    for (pos = fname; *pos && *pos != '/'; ++pos);
+    if (*pos && (stat(fname, &cmd_stat) == -1))
+    {
+        fprintf(stderr, "Cannot stat %s: %s\n", fname, strerror(errno));
+        return NULL;
+    }
+    else if (!*pos && !path)
+    {
+        fprintf(stderr, "Cannot stat %s: No such file or directory\n", fname);
+        return NULL;
+    }
+    if (!*pos)
+        for ((dir = strtok(path, ":")); dir; dir = strtok(NULL, ":"))
+        {
+            int len = strlen(dir);
+            if (fullpath == NULL || fullpath_len < len)
+            {
+                if (fullpath)
+                    fullpath = realloc(fullpath, sizeof(*fullpath) * (strlen(fname) + 2 + len));
+                else
+                    fullpath = malloc(sizeof(*fullpath) * (strlen(fname) + len +2));
+                fullpath_len = len;
+            }
+            sprintf(fullpath, "%s/%s", dir, fname);
+            if (stat(fullpath, &cmd_stat) != -1 && !S_ISDIR(cmd_stat.st_mode))
+                return fullpath;
+        }
+    if (S_ISDIR(cmd_stat.st_mode))
+    {
+        fprintf(stderr, "Cannot execute %s: is a directory\n", fname);
+        return NULL;
+    }
+    if (!fullpath && *pos)
+        fullpath = strdup(fname);
+    else if (!*pos)
+    {
+        free(fullpath);
+        fprintf(stderr, "Cannot execute %s: command not found\n", fname);
+        fullpath = NULL;
+    }
+    return fullpath;
 }
 
 int init_env(t_param *params)
 {
-	if ((params->cmdpath = check_file(params->cmd[0])) == NULL)
-		return -1;
-	asprintf(&(params->tmppath), "%s/sandbox_%ld:%d", params->tmpdir, time(NULL), getpid());
-	if (mkdir(params->tmppath, 0) == -1)
-	{
-		fprintf(stderr, "Cannot create directory %s: %s\n", params->tmppath, strerror(errno));
-		return -1;
-	}
-	if (params->verbose)
-		fprintf(params->verbose, "Created env at %s\n", params->tmppath);
-	return 0;
+    if ((params->cmdpath = check_file(params->cmd[0])) == NULL)
+        return -1;
+    asprintf(&(params->tmppath), "%s/sandbox_%ld:%d", params->tmpdir, time(NULL), getpid());
+    if (mkdir(params->tmppath, 0) == -1)
+    {
+        fprintf(stderr, "Cannot create directory %s: %s\n", params->tmppath, strerror(errno));
+        return -1;
+    }
+    if (params->verbose)
+        fprintf(params->verbose, "Created env at %s\n", params->tmppath);
+    return 0;
 }
 
 void release_env(t_param *params)
 {
-	rmdir(params->tmppath);
+    rmdir(params->tmppath);
 
-	free(params->tmppath);
-	free(params->cmdpath);
-	free(params);
+    free(params->tmppath);
+    free(params->cmdpath);
+    free(params);
 }
 

+ 9 - 0
src/ovr_syscall/ovr_lseek.c

@@ -0,0 +1,9 @@
+#include "sandbox.h"
+#include "sllist.h"
+
+int ovr_lseek(struct s_sandboxenv *env)
+{
+#warning TODO
+    return 0;
+}
+

+ 44 - 43
src/ovr_syscall/ovr_open.c

@@ -8,56 +8,57 @@
 
 static t_fileinfo *open_local(struct s_sandboxenv *env, char *filename)
 {
-	t_fileinfo *result;
-	unsigned long long int filename_hash;
-	char *localpath;
+    t_fileinfo *result;
+    unsigned long long int filename_hash;
+    char *localpath;
 
-	filename_hash = hash_path(filename);
-	asprintf(&localpath, "%s/%llu", env->params->tmppath, filename_hash);
-	result = malloc(sizeof(*result));
-	result->filename = filename;
-	result->local_fd = open(localpath, O_CREAT, 0600);
-	result->fds = sllist_create();
-	sllist_pushback(env->filetable, result);
-	free(localpath);
-	return result;
+    filename_hash = hash_path(filename);
+    asprintf(&localpath, "%s/%llu", env->params->tmppath, filename_hash);
+    result = malloc(sizeof(*result));
+    result->filename = filename;
+    result->local_fd = open(localpath, O_CREAT | O_RDWR, 0600);
+    result->fds = sllist_create();
+    result->modif_write = sllist_create();
+    sllist_pushback(env->filetable, result);
+    free(localpath);
+    return result;
 }
 
 int ovr_open(struct s_sandboxenv *env)
 {
-	char *pathname = getMem(env, (size_t) env->syscall_args[0], NULL);
-	int flags = (int) env->syscall_args[1];
-	mode_t mode = (mode_t) env->syscall_args[2];
-	int ro = !(flags & (O_RDWR | O_WRONLY | O_TRUNC));
-	t_fileinfo *fileentry;
-	t_fd *fd;
+    char *pathname = getMem(env, (size_t) env->syscall_args[0], NULL);
+    int flags = (int) env->syscall_args[1];
+    mode_t mode = (mode_t) env->syscall_args[2];
+    int ro = !(flags & (O_RDWR | O_WRONLY | O_TRUNC));
+    t_fileinfo *fileentry;
+    t_fd *fd;
 
-	if (*pathname != '/')
-		find_fullpath(&pathname, 1);
-	fileentry = get_fileinfo(env, pathname);
-	if (!fileentry && ro && (flags & O_CREAT) && !file_exists(pathname))
-		ro = 0;
-	if (!fileentry && ro)
-	{
-		printf("DO open %s (unmanaged)\n", pathname);
-		fflush(stdout);
-		free(pathname);
-		return 0;
-	}
-	waitForSyscall(env->child_pid, SANDBOX_SYS_EXIT);
-	read_registers(env);
-	if (!fileentry)
-		fileentry = open_local(env, strdup(pathname));
+    if (*pathname != '/')
+        find_fullpath(&pathname, 1);
+    fileentry = get_fileinfo(env, pathname);
+    if (!fileentry && ro && (flags & O_CREAT) && !file_exists(pathname))
+        ro = 0;
+    if (!fileentry && ro)
+    {
+        printf("DO open %s (unmanaged)\n", pathname);
+        fflush(stdout);
+        free(pathname);
+        return 0;
+    }
+    waitForSyscall(env->child_pid, SANDBOX_SYS_EXIT);
+    read_registers(env);
+    if (!fileentry)
+        fileentry = open_local(env, strdup(pathname));
 
-	fd = malloc(sizeof(*fd));
-	fd->fd = (int) env->syscall_no.syscall_return;
-	fd->flags = flags;
-	fd->mod = mode;
-	sllist_pushback(fileentry->fds, fd);
+    fd = malloc(sizeof(*fd));
+    fd->fd = (int) env->syscall_no.syscall_return;
+    fd->flags = flags;
+    fd->mod = mode;
+    sllist_pushback(fileentry->fds, fd);
 
-	printf("DO open ! ([%s], [%d], [%d])%s = %d\n", pathname, flags, mode, ro ? "- RO" : "- RW", (int) env->syscall_no.syscall_return);
-	fflush(stdout);
-	free(pathname);
-	return 0;
+    printf("DO open ! ([%s], [%d], [%d])%s = %d\n", pathname, flags, mode, ro ? "- RO" : "- RW", (int) env->syscall_no.syscall_return);
+    fflush(stdout);
+    free(pathname);
+    return 0;
 }
 

+ 10 - 0
src/ovr_syscall/ovr_read.c

@@ -0,0 +1,10 @@
+
+#include <stdlib.h>
+#include "sandbox.h"
+
+int ovr_read(struct s_sandboxenv *env)
+{
+#warning TODO
+    return 0;
+}
+

+ 37 - 11
src/ovr_syscall/ovr_write.c

@@ -1,19 +1,45 @@
 #include <stdlib.h>
 #include "sandbox.h"
 
+#include <string.h>
+#include <errno.h>
+
 int ovr_write(struct s_sandboxenv *env)
 {
-	int fd = (int) env->syscall_args[0];
-	char *buf = getMem(env, (size_t) env->syscall_args[1], NULL);
-	int buflen = (int) env->syscall_args[2];
-	t_fileinfo *file = get_fileinfo_fd(env, fd);
+    int fd = (int) env->syscall_args[0];
+    char *buf = getMem(env, (size_t) env->syscall_args[1], NULL);
+    int buflen = (int) env->syscall_args[2];
+    int result;
+    t_fileinfo *file = get_fileinfo_fd(env, fd);
+    t_fd *_fd;
+    t_filepage *page;
 
-	if (!file)
-		return 0;
-	printf("DO WRITE ! ([%s], [%s], [%d])\n", file->filename, buf, buflen);
-	fflush(stdout);
-	free(buf);
-	//env->registers.orig_rax = -1;
-	return 0;
+    if (!file)
+        return 0;
+    result = sllist_find(file->fds, fileinfo_compare_tfd, (void *)env->syscall_args[0]) != -1;
+    if (!result)
+        return 0;
+    _fd = sllist_at(file->fds, result);
+    printf("DO WRITE ! ([%s], [%s], [%d])\n", file->filename, buf, buflen);
+    fflush(stdout);
+    result = write(file->local_fd, buf, buflen);
+#ifdef __x86_64__
+    env->registers.orig_rax = -1;
+    env->syscall_no.syscall_return = env->registers.rax = (REGISTER_TYPE) result;
+#else
+# error 32B
+#endif
+    if (result == -1)
+    {
+        fprintf(stderr, "Cannot write: %s\n", strerror(errno));
+        return;
+    }
+    page = malloc(sizeof(*page));
+    page->len = result;
+    page->offset_start = _fd->offset;
+    _fd->offset += result;
+    sllist_pushback(file->modif_write, page);
+    free(buf);
+    return 0;
 }
 

+ 38 - 38
src/pathutil.c

@@ -7,83 +7,83 @@
 
 void find_fullpath(char **path, int _free)
 {
-	char *result;
-	char *cwd;
-
-	cwd = get_current_dir_name();
-	asprintf(&result, "%s/%s", cwd, *path);
-	if (_free)
-		free(*path);
-	free(cwd);
-	*path = result;
+    char *result;
+    char *cwd;
+
+    cwd = get_current_dir_name();
+    asprintf(&result, "%s/%s", cwd, *path);
+    if (_free)
+        free(*path);
+    free(cwd);
+    *path = result;
 }
 
 int fileinfo_compare(const void *a, const void *b)
 {
-	return strcmp(((t_fileinfo *)a)->filename, (char *)b) == 0;
+    return strcmp(((t_fileinfo *)a)->filename, (char *)b) == 0;
 }
 
 int fileinfo_compare_tfd(const void *a, const void *b)
 {
-	return ((t_fd*)a)->fd == (REGISTER_TYPE) b;
+    return ((t_fd*)a)->fd == (REGISTER_TYPE) b;
 }
 
 int fileinfo_compare_fd(const void *a, const void *fd)
 {
-	return sllist_find(((t_fileinfo *)a)->fds, fileinfo_compare_tfd, fd) != -1;
+    return sllist_find(((t_fileinfo *)a)->fds, fileinfo_compare_tfd, fd) != -1;
 }
 
 t_fileinfo *get_fileinfo(const struct s_sandboxenv *env, const char * filename)
 {
-	int i;
+    int i;
 
-	i = sllist_find(env->filetable, fileinfo_compare, filename);
-	if (i == -1)
-		return NULL;
-	return sllist_at(env->filetable, i);
+    i = sllist_find(env->filetable, fileinfo_compare, filename);
+    if (i == -1)
+        return NULL;
+    return sllist_at(env->filetable, i);
 }
 
 t_fileinfo *get_fileinfo_fd(const struct s_sandboxenv *env, const int fd)
 {
-	int i;
-	REGISTER_TYPE _fd = (REGISTER_TYPE) fd;
+    int i;
+    REGISTER_TYPE _fd = (REGISTER_TYPE) fd;
 
-	i = sllist_find(env->filetable, fileinfo_compare_fd, (void *)_fd);
-	if (i == -1)
-		return NULL;
-	return sllist_at(env->filetable, i);
+    i = sllist_find(env->filetable, fileinfo_compare_fd, (void *)_fd);
+    if (i == -1)
+        return NULL;
+    return sllist_at(env->filetable, i);
 }
 
 int file_exists(const char*path)
 {
-	struct stat buf;
+    struct stat buf;
 
-	if (stat(path, &buf) == -1 && errno == ENOENT)
-		return 0;
-	return 1;
+    if (stat(path, &buf) == -1 && errno == ENOENT)
+        return 0;
+    return 1;
 }
 
 unsigned long long int hash_path(const char *filename)
 {
-	unsigned long long int result;
+    unsigned long long int result;
 
-	for (result =0; *filename; ++filename)
-		result = (result << 5) - result + *filename;
-	return result;
+    for (result =0; *filename; ++filename)
+        result = (result << 5) - result + *filename;
+    return result;
 }
 
 static inline int release_fds_struct(void **t_fds, void *nullptr)
 {
-	free(*(t_fd**)t_fds);
-	return 0;
+    free(*(t_fd**)t_fds);
+    return 0;
 }
 
 int release_file(void **t_fi, void *env)
 {
-	sllist_foreach((*(t_fileinfo**)t_fi)->fds, release_fds_struct, NULL);
-	sllist_destroy((*(t_fileinfo**)t_fi)->fds);
-	free((*(t_fileinfo**)t_fi)->filename);
-	free(*(t_fileinfo**)t_fi);
-	return 0;
+    sllist_foreach((*(t_fileinfo**)t_fi)->fds, release_fds_struct, NULL);
+    sllist_destroy((*(t_fileinfo**)t_fi)->fds);
+    free((*(t_fileinfo**)t_fi)->filename);
+    free(*(t_fileinfo**)t_fi);
+    return 0;
 }
 

+ 27 - 19
src/sandbox.h

@@ -23,37 +23,45 @@
 #  define REGISTER_TYPE long int
 # endif
 
+struct s_sandboxenv;
+
 typedef struct fd {
-	int fd;
-	int flags;
-	int mod;
+    int fd;
+    int flags;
+    int mod;
+    size_t offset;
 } t_fd;
 
 typedef struct {
-	char *filename;
-	sl_list *fds;
-	int local_fd;
+    char *filename;
+    sl_list *fds;
+    sl_list *modif_write;
+    int local_fd;
 } t_fileinfo;
 
 typedef struct {
-	const char **cmd;
-	const char *tmpdir;
-	char *cmdpath;
-	char *tmppath;
-	FILE *verbose;
+    size_t offset_start;
+    size_t len;
+} t_filepage;
+
+typedef struct {
+    const char **cmd;
+    const char *tmpdir;
+    char *cmdpath;
+    char *tmppath;
+    FILE *verbose;
 } t_param;
 
-struct s_sandboxenv;
 typedef int(* t_syscall_fnc)(struct s_sandboxenv *);
 
 struct s_sandboxenv {
-	const t_param *params;
-	t_syscall_fnc functions[NR_syscalls];
-	int child_pid;
-	struct user_regs_struct registers;
-	struct { REGISTER_TYPE syscall_no; REGISTER_TYPE syscall_return; } syscall_no; 
-	sl_list *filetable;
-	REGISTER_TYPE syscall_args[6];
+    const t_param *params;
+    t_syscall_fnc functions[NR_syscalls];
+    int child_pid;
+    struct user_regs_struct registers;
+    struct { REGISTER_TYPE syscall_no; REGISTER_TYPE syscall_return; } syscall_no; 
+    sl_list *filetable;
+    REGISTER_TYPE syscall_args[6];
 };
 
 /* params.c */

+ 2 - 0
src/sandbox_syscall.h

@@ -8,5 +8,7 @@
 int ovr_open(struct s_sandboxenv *);
 int ovr_close(struct s_sandboxenv *);
 int ovr_write(struct s_sandboxenv *);
+int ovr_lseek(struct s_sandboxenv *);
+int ovr_read(struct s_sandboxenv *);
 
 #endif /* SANDBOX_SYSCALL_H__ */

+ 103 - 65
test/write/main.c

@@ -1,114 +1,152 @@
 #include "common.h"
 #include "sandbox.h"
 #include <sys/stat.h>
+#include <string.h>
 #include <fcntl.h>
 
 /*
-	_assertNotNull(a);
-	_assertEqual(sllist_count(a), 0);
+    _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;
+    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;
+    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;
+    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;
+    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;
+    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;
+    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;
+    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;
+    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);
+    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;
+    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;
+    int success = 1;
+    struct s_sandboxenv env;
+    int fd;
+    t_param params;
 
-	tests_init_env(&env, &params);
+    tests_init_env(&env, &params);
 
-	success &= !test_open(&env, &fd);
-	success &= !test_write(&env, fd);
+    success &= !test_open(&env, &fd);
+    success &= !test_write(&env, fd);
 
-	fakeclose(fd, &env);
-	tests_release_env(&env, &params);
-	unlink("_test");
-	exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
+    fakeclose(fd, &env);
+    tests_release_env(&env, &params);
+    unlink("_test");
+    exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
 }