Explorar el Código

Unit tests, begining env

isundil hace 10 años
padre
commit
71db8f9c9c
Se han modificado 12 ficheros con 105 adiciones y 12 borrados
  1. 1 1
      .gitignore
  2. 3 1
      CMakeLists.txt
  3. 2 1
      src/CMakeLists.txt
  4. 29 0
      src/environment.c
  5. 3 2
      src/main.c
  6. 5 6
      src/param.c
  7. 7 1
      src/sandbox.h
  8. 13 0
      test/common.h
  9. 2 0
      test/open/CMakeLists.txt
  10. 37 0
      test/open/main.c
  11. 1 0
      test/test/CMakeLists.txt
  12. 2 0
      test/test/test.c

+ 1 - 1
.gitignore

@@ -41,7 +41,7 @@ CMakeFiles
 cmake_install.cmake
 CMakeCache.txt
 /bin
+/test/bin
 
 # test directory
-/test/test
 

+ 3 - 1
CMakeLists.txt

@@ -8,4 +8,6 @@ add_definitions ("-Wall")
 add_definitions ("-g3")
 
 add_subdirectory(src)
- 
+
+add_subdirectory(test/open)
+add_subdirectory(test/test)

+ 2 - 1
src/CMakeLists.txt

@@ -1 +1,2 @@
-add_executable(sandbox main.c mem.c exec.c param.c sandbox.c ovr_syscall/ovr_write.c ovr_syscall/ovr_open.c ovr_syscall/ovr_close.c)
+add_executable(sandbox main.c mem.c exec.c param.c sandbox.c environment.c
+	ovr_syscall/ovr_write.c ovr_syscall/ovr_open.c ovr_syscall/ovr_close.c)

+ 29 - 0
src/environment.c

@@ -0,0 +1,29 @@
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <time.h>
+#include "sandbox.h"
+
+int init_env(t_param *params)
+{
+	char * path = params->tmppath = (char *) malloc(sizeof(*path) * (strlen(params->tmpdir) + 24));
+
+	sprintf(path, "%s/sandbox_%ld:%d", params->tmpdir, time(NULL), getpid());
+	if (mkdir(path, 0) == -1)
+	{
+		fprintf(stderr, "Cannot create directory %s: %s\n", path, strerror(errno));
+		return -1;
+	}
+	return 0;
+}
+
+void release_env(t_param *params)
+{
+	rmdir(params->tmppath);
+	/* TODO remove files */
+	free(params->tmppath);
+	free(params);
+}
+

+ 3 - 2
src/main.c

@@ -10,8 +10,9 @@ int main(int argc, char **argv)
 	params = parse_argv((const char **) argv);
 	if (params == NULL)
 		return 0;
-	launch_program(params);
-	free(params);
+	if (init_env(params) == 0)
+		launch_program(params);
+	release_env(params);
 	return 0;
 }
 

+ 5 - 6
src/param.c

@@ -3,11 +3,10 @@
 #include <string.h>
 #include "sandbox.h"
 
-// TODO man 2 getopt
-
 void print_help(const char *progname, int exit_status)
 {
 	fprintf(stderr, "Usage: %s [options] [--] command\n", progname);
+	fprintf(stderr, "Options:\t--tmp-dir directory - path to temporary dir (default: /tmp)\n");
 	exit(exit_status);
 }
 
@@ -15,10 +14,9 @@ static int parse_param(t_param *result, const char **av)
 {
 	printf("Parsing -%s-\n", *av);
 	if (!strcmp(*av, "--"))
-	{
-		av++;
-		result->cmd = av;
-	}
+		result->cmd = ++av;
+	else if (!strcmp(*av, "-tmp-dir"))
+		result->tmpdir = *(++av);
 	return 1;
 }
 
@@ -31,6 +29,7 @@ t_param *parse_argv(const char **av)
 	if (result == NULL)
 		return NULL;
 	bzero(result, sizeof(*result));
+	result->tmpdir = "/tmp";
 	for (progname = *(av++); *av && !result->cmd; ++av)
 	{
 		if (**av == '-')

+ 7 - 1
src/sandbox.h

@@ -1,8 +1,8 @@
 #ifndef   SANDBOX_H__
 # define  SANDBOX_H__
 
-# include <sys/user.h>
 # include <sys/syscall.h>
+# include <sys/user.h>
 # include <unistd.h>
 
 # ifndef NR_syscalls
@@ -17,6 +17,8 @@
 
 typedef struct {
 	const char **cmd;
+	const char *tmpdir;
+	char *tmppath;
 } t_param;
 
 struct s_sandboxenv;
@@ -50,6 +52,10 @@ void doExec(int pid_parent, const t_param *params);
 **/
 void *getMem(const struct s_sandboxenv *env, size_t ptr, int *readlen);
 
+/* environment.c */
+int init_env(t_param *);
+void release_env(t_param *);
+
 # include "sandbox_syscall.h"
 
 #endif /* SANDBOX_H__ */

+ 13 - 0
test/common.h

@@ -0,0 +1,13 @@
+#ifndef _SANDBOX__TEST_COMMON_H_
+# define _SANDBOX__TEST_COMMON_H_
+
+#include <stdlib.h>
+#include <stdio.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; }}
+
+#define _assertEqual(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 equals %lld\n", __FILE__, __LINE__, _a, _b); return -1; }}
+
+#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; }}
+
+#endif /* _SANDBOX__TEST_COMMON_H_ */

+ 2 - 0
test/open/CMakeLists.txt

@@ -0,0 +1,2 @@
+include_directories(../)
+add_executable(open main.c)

+ 37 - 0
test/open/main.c

@@ -0,0 +1,37 @@
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "common.h"
+
+int runTests()
+{
+	int fd = open("filenotfound", O_RDONLY);
+	_assertEqual(fd, -1);
+	_assertEqual(open("newFile", O_RDONLY), -1);
+	fd = open("newFile", O_CREAT, 0x644);
+	_assertDiff(fd, -1);
+	close(fd);
+	_assertDiff((fd = open("newFile", O_RDONLY)), -1);
+	close(fd);
+	return 0;
+}
+
+void cleanup()
+{
+	unlink("newFile");
+	unlink("filenotfound");
+}
+
+int main(int ac, char **av)
+{
+	cleanup();
+
+	if (runTests() == -1)
+	{
+		cleanup();
+		exit(EXIT_FAILURE);
+	}
+	cleanup();
+	exit(EXIT_SUCCESS);
+}
+

+ 1 - 0
test/CMakeLists.txt → test/test/CMakeLists.txt

@@ -1 +1,2 @@
+include_directories(../)
 add_executable(test test.c)

+ 2 - 0
test/test.c → test/test/test.c

@@ -1,3 +1,5 @@
+#include <unistd.h>
+#include "common.h"
 
 int main()
 {