From 95c18f41a7331175992ce99da98b2efb8f1f75d9 Mon Sep 17 00:00:00 2001 From: LiYang Date: Sat, 27 Aug 2016 22:03:58 +0800 Subject: [PATCH] add math test case and use -static flag by default --- src/rules/c_cpp/rule.c | 3 ++- tests/testcase/base.py | 7 +++++-- tests/testcase/integration/test.py | 4 ++-- tests/testcase/seccomp/math.c | 9 +++++++++ tests/testcase/seccomp/test.py | 13 +++++++++++-- 5 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 tests/testcase/seccomp/math.c diff --git a/src/rules/c_cpp/rule.c b/src/rules/c_cpp/rule.c index f77a51b..fdcc181 100644 --- a/src/rules/c_cpp/rule.c +++ b/src/rules/c_cpp/rule.c @@ -10,7 +10,8 @@ int load_seccomp(void *dl_handler, struct config *_config) { SCMP_SYS(munmap), SCMP_SYS(open), SCMP_SYS(arch_prctl), SCMP_SYS(brk), SCMP_SYS(access), SCMP_SYS(exit_group), - SCMP_SYS(close)}; + SCMP_SYS(close), SCMP_SYS(readlink), + SCMP_SYS(uname)}; int syscalls_whitelist_length = sizeof(syscalls_whitelist) / sizeof(int); scmp_filter_ctx ctx = NULL; // load seccomp rules diff --git a/tests/testcase/base.py b/tests/testcase/base.py index 2beb2a0..b1c2351 100644 --- a/tests/testcase/base.py +++ b/tests/testcase/base.py @@ -23,10 +23,13 @@ class BaseTestCase(TestCase): def rand_str(self): return ''.join(map(lambda xx:(hex(ord(xx))[2:]), os.urandom(16))) - def _compile_c(self, src_name): + def _compile_c(self, src_name, extra_flags=None): path = os.path.dirname(os.path.abspath(__file__)) exe_path = os.path.join(self.workspace, src_name.split("/")[-1].split(".")[0]) - cmd = "gcc {0} -g -O0 -o {1}".format(os.path.join(path, src_name), exe_path) + flags = " " + if extra_flags: + flags += " ".join(extra_flags) + cmd = ("gcc {0} -g -O0 -static -o {1}" + flags).format(os.path.join(path, src_name), exe_path) if os.system(cmd): raise AssertionError("compile error, cmd: {0}".format(cmd)) return exe_path diff --git a/tests/testcase/integration/test.py b/tests/testcase/integration/test.py index 01537d1..38a56ca 100644 --- a/tests/testcase/integration/test.py +++ b/tests/testcase/integration/test.py @@ -26,8 +26,8 @@ class IntegrationTest(base.BaseTestCase): "gid": 0} self.workspace = self.init_workspace("integration") - def _compile_c(self, src_name): - return super(IntegrationTest, self)._compile_c("integration/" + src_name) + def _compile_c(self, src_name, extra_flags=None): + return super(IntegrationTest, self)._compile_c("integration/" + src_name, extra_flags) def _compile_cpp(self, src_name): return super(IntegrationTest, self)._compile_cpp("integration/" + src_name) diff --git a/tests/testcase/seccomp/math.c b/tests/testcase/seccomp/math.c new file mode 100644 index 0000000..5fde585 --- /dev/null +++ b/tests/testcase/seccomp/math.c @@ -0,0 +1,9 @@ +#include +#include + +int main() +{ + sin(1.0); + printf("sin"); + return 0; +} \ No newline at end of file diff --git a/tests/testcase/seccomp/test.py b/tests/testcase/seccomp/test.py index ccdde6e..2f8391f 100644 --- a/tests/testcase/seccomp/test.py +++ b/tests/testcase/seccomp/test.py @@ -26,8 +26,8 @@ class SeccompTest(base.BaseTestCase): "gid": 0} self.workspace = self.init_workspace("seccomp") - def _compile_c(self, src_name): - return super(SeccompTest, self)._compile_c("seccomp/" + src_name) + def _compile_c(self, src_name, extra_flags=None): + return super(SeccompTest, self)._compile_c("seccomp/" + src_name, extra_flags) def test_mmap_write_file(self): config = self.config @@ -36,3 +36,12 @@ class SeccompTest(base.BaseTestCase): result = _judger.run(**config) self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR) self.assertEqual(result["signal"], 31) + + def test_math(self): + config = self.config + config["exe_path"] = self._compile_c("math.c", extra_flags=["-lm"]) + config["seccomp_rule_so_path"] = "/usr/lib/judger/librule_c_cpp.so" + config["output_path"] = self.output_path() + result = _judger.run(**config) + self.assertEqual(result["result"], _judger.RESULT_SUCCESS) + self.assertEqual("sin", self.output_content(config["output_path"]))