diff --git a/.gitignore b/.gitignore index 3c00d09..68169da 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ build/ a.out limit -in -out runner CMakeLists.txt +main diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..723560c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: python +python: + - "2.7" +sudo: required +before_install: + - sudo apt-get -qq update + - sudo apt-get install libseccomp-dev +install: + - sudo python setup.py install +script: sudo python tests/test.py \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee45727 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ + +[![Build Status](https://travis-ci.org/QingdaoU/Judger.svg?branch=master)](https://travis-ci.org/QingdaoU/Judger) + +``` +sudo python setup.py install +touch in out +gcc test.c -o test +sudo python demo.py +``` diff --git a/readme b/readme deleted file mode 100644 index f4e9d26..0000000 --- a/readme +++ /dev/null @@ -1,4 +0,0 @@ -sudo python setup.py install -touch in out -gcc test.c -o test -python demo.py diff --git a/tests/1/Main.c b/tests/1/Main.c new file mode 100644 index 0000000..9d9d7d5 --- /dev/null +++ b/tests/1/Main.c @@ -0,0 +1,15 @@ +/* + * 正常运行的程序 + */ + +#include +#include + +int main() +{ + char input[100]; + scanf("%s", input); + printf("%s\n", input); + printf("Hello world"); + return 0; +} \ No newline at end of file diff --git a/tests/1/in b/tests/1/in new file mode 100644 index 0000000..6a537b5 --- /dev/null +++ b/tests/1/in @@ -0,0 +1 @@ +1234567890 \ No newline at end of file diff --git a/tests/1/out b/tests/1/out new file mode 100644 index 0000000..a9299e1 --- /dev/null +++ b/tests/1/out @@ -0,0 +1,2 @@ +1234567890 +Hello world \ No newline at end of file diff --git a/tests/1/result b/tests/1/result new file mode 100644 index 0000000..ebd96f5 --- /dev/null +++ b/tests/1/result @@ -0,0 +1 @@ +{"flag": 0, "signal": 0} \ No newline at end of file diff --git a/tests/2/Main.c b/tests/2/Main.c new file mode 100644 index 0000000..f1a524d --- /dev/null +++ b/tests/2/Main.c @@ -0,0 +1,36 @@ +/* + * 正常运行的程序 + */ + +#include +#include +#include + +int main() +{ + // 1k + int small_size = 1024; + // 150m + int big_size = 150 * 1024 * 1024; + + int *s = NULL, *b = NULL; + + s = (int *)malloc(small_size); + if(s){ + printf("malloc small size succedeed\n"); + } + else{ + printf("malloc small size failed\n"); + return -1; + } + + b = (int *)malloc(big_size); + if(b){ + printf("malloc big size succedeed\n"); + } + else{ + printf("malloc big size failed\n"); + return -2; + } + return 0; +} \ No newline at end of file diff --git a/tests/2/in b/tests/2/in new file mode 100644 index 0000000..e69de29 diff --git a/tests/2/out b/tests/2/out new file mode 100644 index 0000000..9929a09 --- /dev/null +++ b/tests/2/out @@ -0,0 +1,2 @@ +malloc small size succedeed +malloc big size succedeed diff --git a/tests/2/result b/tests/2/result new file mode 100644 index 0000000..ebd96f5 --- /dev/null +++ b/tests/2/result @@ -0,0 +1 @@ +{"flag": 0, "signal": 0} \ No newline at end of file diff --git a/tests/3/Main.c b/tests/3/Main.c new file mode 100644 index 0000000..0c1d95b --- /dev/null +++ b/tests/3/Main.c @@ -0,0 +1,21 @@ +/* + * 不允许的系统调用 clone + */ + +#include +#include +#include + +int main() +{ + pid_t pid; + printf("fork test\n"); + pid = fork(); + if(pid < 0){ + printf("fork failed\n"); + } + else { + printf("fork succeeded\n"); + } + return 0; +} \ No newline at end of file diff --git a/tests/3/in b/tests/3/in new file mode 100644 index 0000000..e69de29 diff --git a/tests/3/out b/tests/3/out new file mode 100644 index 0000000..e69de29 diff --git a/tests/3/result b/tests/3/result new file mode 100644 index 0000000..44a7510 --- /dev/null +++ b/tests/3/result @@ -0,0 +1 @@ +{"flag": 4, "signal": 31} \ No newline at end of file diff --git a/tests/4/Main.c b/tests/4/Main.c new file mode 100644 index 0000000..7270bb5 --- /dev/null +++ b/tests/4/Main.c @@ -0,0 +1,14 @@ +/* + * 不允许的系统调用 exec + */ + +#include +#include +#include + +int main(int arg, char **args) { + char *argv[] = {"/bin/echo", "1234567890", NULL}; + char *envp[] = {NULL}; + printf("execve test\n"); + execve("/bin/echo", argv, envp); +} diff --git a/tests/4/in b/tests/4/in new file mode 100644 index 0000000..e69de29 diff --git a/tests/4/out b/tests/4/out new file mode 100644 index 0000000..e69de29 diff --git a/tests/4/result b/tests/4/result new file mode 100644 index 0000000..44a7510 --- /dev/null +++ b/tests/4/result @@ -0,0 +1 @@ +{"flag": 4, "signal": 31} \ No newline at end of file diff --git a/tests/5/Main.c b/tests/5/Main.c new file mode 100644 index 0000000..58180fe --- /dev/null +++ b/tests/5/Main.c @@ -0,0 +1,26 @@ +/* + * 自定义构造函数执行fork + */ + +#include +#include +#include +#include +#include +#include + +__attribute((constructor)) +void myinit(void) +{ + if(fork() < 0) { + printf("fork failed\n"); + } + else { + printf("fork succeeded\n"); + } +} + +int main(void) +{ + return 0; +} \ No newline at end of file diff --git a/tests/5/in b/tests/5/in new file mode 100644 index 0000000..e69de29 diff --git a/tests/5/out b/tests/5/out new file mode 100644 index 0000000..e69de29 diff --git a/tests/5/result b/tests/5/result new file mode 100644 index 0000000..44a7510 --- /dev/null +++ b/tests/5/result @@ -0,0 +1 @@ +{"flag": 4, "signal": 31} \ No newline at end of file diff --git a/tests/6/Main.c b/tests/6/Main.c new file mode 100644 index 0000000..dace805 --- /dev/null +++ b/tests/6/Main.c @@ -0,0 +1,17 @@ +/* + * 不允许的系统调用 write file + */ + +#include +#include + +int main() +{ + FILE * fp; + + fp = fopen ("/dev/null", "w"); + fprintf(fp, "test"); + fclose(fp); + printf("write file succeesed"); + return 0; +} \ No newline at end of file diff --git a/tests/6/in b/tests/6/in new file mode 100644 index 0000000..e69de29 diff --git a/tests/6/out b/tests/6/out new file mode 100644 index 0000000..e69de29 diff --git a/tests/6/result b/tests/6/result new file mode 100644 index 0000000..44a7510 --- /dev/null +++ b/tests/6/result @@ -0,0 +1 @@ +{"flag": 4, "signal": 31} \ No newline at end of file diff --git a/tests/7/Main.c b/tests/7/Main.c new file mode 100644 index 0000000..2d2c524 --- /dev/null +++ b/tests/7/Main.c @@ -0,0 +1,13 @@ +/* + * 执行系统命令 + */ + +#include +#include +#include + +int main() +{ + system("/bin/echo test"); + return 0; +} \ No newline at end of file diff --git a/tests/7/in b/tests/7/in new file mode 100644 index 0000000..e69de29 diff --git a/tests/7/out b/tests/7/out new file mode 100644 index 0000000..e69de29 diff --git a/tests/7/result b/tests/7/result new file mode 100644 index 0000000..44a7510 --- /dev/null +++ b/tests/7/result @@ -0,0 +1 @@ +{"flag": 4, "signal": 31} \ No newline at end of file diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 0000000..dc90112 --- /dev/null +++ b/tests/test.py @@ -0,0 +1,41 @@ +# coding=utf-8 +import os +import json +import judger +import shutil +from unittest import TestCase, main + + +class JudgerTest(TestCase): + def setUp(self): + self.tmp_path = "/tmp/judger_test" + + def compile_src(self, src_path, language, exe_path): + if language == "c": + return os.system("gcc %s -o %s" % (src_path, exe_path)) + elif language == "cpp": + return os.system("g++ %s -o %s" % (src_path, exe_path)) + elif language == "java": + pass + else: + raise ValueError("Invalid language") + + def test_run(self): + shutil.rmtree(self.tmp_path, ignore_errors=True) + os.mkdir(self.tmp_path) + for i in range(1, 8): + test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), str(i)) + exe_path = os.path.join("/tmp/judger_test", str(i)) + self.assertEqual(self.compile_src(os.path.join(test_dir, "Main.c"), "c", exe_path), 0) + run_result = judger.run(path=exe_path, + in_file=os.path.join(test_dir, "in"), + out_file=os.path.join(self.tmp_path, str(i) + ".out"), + max_cpu_time=2000, max_memory=200000000) + result = json.loads(open(os.path.join(test_dir, "result")).read()) + self.assertEqual(result["flag"], run_result["flag"]) + self.assertEqual(result["signal"], run_result["signal"]) + self.assertEqual(open(os.path.join(test_dir, "out")).read(), open(os.path.join(self.tmp_path, str(i) + ".out")).read()) + + +if __name__ == "__main__": + main()