소스 검색

tests ovr_close, lib remove

isundil 9 년 전
부모
커밋
e6952f3dce
11개의 변경된 파일175개의 추가작업 그리고 10개의 파일을 삭제
  1. 3 0
      .gitignore
  2. 1 1
      CMakeLists.txt
  3. 12 0
      lib/sllist/include/sllist.h
  4. 1 0
      lib/sllist/src/del.c
  5. 47 0
      lib/sllist/src/remove.c
  6. 6 4
      src/ovr_syscall/ovr_close.c
  7. 2 1
      src/pathutil.c
  8. 4 0
      test/common.h
  9. 2 0
      test/sllist/main.c
  10. 23 0
      test/write/functions.c
  11. 74 4
      test/write/main.c

+ 3 - 0
.gitignore

@@ -47,3 +47,6 @@
 
 # test directory
 
+# other
+.fuse_hidden*
+

+ 1 - 1
CMakeLists.txt

@@ -1,6 +1,6 @@
 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)
+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)
 

+ 12 - 0
lib/sllist/include/sllist.h

@@ -70,6 +70,18 @@ int sllist_find(sl_list *list, int(*fnc)(const void *cmp, const void *param), co
 **/
 int sllist_foreach(sl_list *list, int(*fnc)(void **item, void *custom), void *custom);
 
+/**
+ * Find and remove item(s)
+ * return nb of items successfully removed
+**/
+int sllist_remove(sl_list *list, int(*fnc)(const void *a, const void *b), void *item);
+
+/**
+ * Find and remove item(s), maximum n items will be removed
+ * return nb of items successfully removed
+**/
+int sllist_n_remove(sl_list *list, int(*fnc)(const void *a, const void *b), void *item, unsigned int n);
+
 /**
  * Comp functions
  * usable with sllist_find

+ 1 - 0
lib/sllist/src/del.c

@@ -30,3 +30,4 @@ void *sllist_popback(sl_list *list)
 	list->count--;
 	return data;
 }
+

+ 47 - 0
lib/sllist/src/remove.c

@@ -0,0 +1,47 @@
+#include <stdlib.h>
+#include "sllist.h"
+
+static inline void swap_items(sl_list *list, struct sl_list_item *pos, int dataindex)
+{
+	struct sl_list_item *i = list->first;
+	int _i, size;
+	void *tmp;
+
+	for (_i =0, size = sllist_count(list); _i + SL_LIST_BUFLEN < size && i; _i += SL_LIST_BUFLEN)
+		i = i->next;
+	tmp = pos->data[dataindex];
+	pos->data[dataindex] = i->data[size - _i];
+	i->data[size - _i] = tmp;
+}
+
+int sllist_remove(sl_list *list, int (*fnc)(const void *a, const void *b), void *item)
+{
+	return sllist_n_remove(list, fnc, item, sllist_count(list));
+}
+
+int sllist_n_remove(sl_list *list, int (*fnc)(const void *, const void *), void *item, unsigned int n)
+{
+	int i, j, size, result;
+	struct sl_list_item *item_ptr;
+
+	for (i =result =0, size = sllist_count(list), item_ptr = list->first; i < size && item_ptr; i += SL_LIST_BUFLEN)
+	{
+		for (j =0; i+j < size && j < SL_LIST_BUFLEN; ++j)
+		{
+			if (!fnc(item_ptr->data[j], item))
+				continue;
+			if (i+j < size -1)
+			{
+				swap_items(list, item_ptr, j);
+				--j;
+			}
+			--list->count;
+			++result;
+			if (!--n)
+				return result;
+		}
+		item_ptr = item_ptr->next;
+	}
+	return result;
+}
+

+ 6 - 4
src/ovr_syscall/ovr_close.c

@@ -5,11 +5,13 @@
 
 int ovr_close(struct s_sandboxenv *env)
 {
-	int fd = (int) env->syscall_args[0];
+	REGISTER_TYPE fd = env->syscall_args[0];
+	t_fileinfo *fi;
 
-
-	printf("DO close ! ([%d])\n", fd);
-	fflush(stdout);
+	fi = get_fileinfo_fd(env, fd);
+	if (!fi)
+		return 0;
+	sllist_remove(fi->fds, sllist_cmp_int, (void *)fd);
 	return 0;
 }
 

+ 2 - 1
src/pathutil.c

@@ -41,8 +41,9 @@ t_fileinfo *get_fileinfo(const struct s_sandboxenv *env, const char * filename)
 t_fileinfo *get_fileinfo_fd(const struct s_sandboxenv *env, const int fd)
 {
 	int i;
+	REGISTER_TYPE _fd = (REGISTER_TYPE) fd;
 
-	i = sllist_find(env->filetable, fileinfo_compare_fd, &fd);
+	i = sllist_find(env->filetable, fileinfo_compare_fd, (void *)_fd);
 	if (i == -1)
 		return NULL;
 	return sllist_at(env->filetable, i);

+ 4 - 0
test/common.h

@@ -3,6 +3,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include "sandbox.h"
 
 #define _assertTrue(a) { int _a = (int) a; if(!_a) { fprintf(stderr, "File %s, line %d: fail asserting %d as true\n", __FILE__, __LINE__, _a); return -1; }}
 
@@ -14,4 +15,7 @@
 
 #define _assertDiff(a, b) { long long int _a = (long long int) a; long long int _b = (long long int) b; if(_a == _b) { fprintf(stderr, "File %s, line %d: fail asserting %lld is different than %lld\n", __FILE__, __LINE__, _a, _b); return -1; }}
 
+void tests_init_env(struct s_sandboxenv *env, t_param *params);
+void tests_release_env(struct s_sandboxenv *env, t_param *params);
+
 #endif /* _SANDBOX__TEST_COMMON_H_ */

+ 2 - 0
test/sllist/main.c

@@ -63,6 +63,8 @@ static inline int test_add()
 	_assertEqual(sllist_find(a, sllist_cmp_int, (void*)i), 1);
 	i = 2047;
 	_assertEqual(sllist_find(a, sllist_cmp_int, (void*)i), 1023);
+	_assertEqual(sllist_remove(a, sllist_cmp_int, (void*)i), 1);
+	_assertEqual(sllist_find(a, sllist_cmp_int, (void*)i), -1);
 	while (sllist_count(a))
 		sllist_popback(a);
 	for (i=0; i < 1024; ++i)

+ 23 - 0
test/write/functions.c

@@ -1,5 +1,8 @@
 #include "sandbox.h"
+#include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 
 void *getMem(const struct s_sandboxenv *env, size_t ptr, int *readlen)
 {
@@ -25,3 +28,23 @@ void read_registers(struct s_sandboxenv *env)
 	(void) env;
 }
 
+void tests_init_env(struct s_sandboxenv *env, t_param *params)
+{
+	params->verbose = NULL;
+	params->tmpdir = "./";
+	asprintf(&(params->tmppath), "%s/tmp", params->tmpdir);
+	mkdir(params->tmppath, 0755);
+	params->cmdpath = "";
+	params->cmd = NULL;
+
+	env->params = params;
+	env->child_pid = getpid();
+	env->filetable = sllist_create();
+}
+
+void tests_release_env(struct s_sandboxenv *env, t_param *params)
+{
+	free(params->tmppath);
+	sllist_destroy(env->filetable);
+}
+

+ 74 - 4
test/write/main.c

@@ -1,18 +1,88 @@
 #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)
+{
+	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;
-	/*
-	success &= !test_create();
-	success &= !test_add();
-	*/
+	struct s_sandboxenv env;
+	t_param params;
+
+	tests_init_env(&env, &params);
+
+	success &= !test_open(&env);
+	success &= !test_write(&env);
+
+	tests_release_env(&env, &params);
 	exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
 }