From 147cecee4671c286dbecd724e681631a3b1e360d Mon Sep 17 00:00:00 2001 From: Moe-hacker Date: Fri, 20 Dec 2024 06:38:33 +0000 Subject: [PATCH] Add test-root.c to avoid test timeout --- .gitignore | 3 ++- Makefile | 1 + test/Makefile | 3 ++- test/root/3-capability.sh | 12 ++++++------ test/test-root.c | 25 +++++++++++++++++++++++++ 5 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 test/test-root.c diff --git a/.gitignore b/.gitignore index eb78f543..f8b9a598 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ debian/source debian/.debhelper debian/debhelper-build-stamp debian/files -tmpdir* \ No newline at end of file +tmpdir* +test-root \ No newline at end of file diff --git a/Makefile b/Makefile index bec5d2b9..ad81e1b6 100644 --- a/Makefile +++ b/Makefile @@ -92,6 +92,7 @@ format : shfmt -i 4 -w get-ruri.sh chmod 777 test/*.sh chmod 777 test/root/* + clang-format -i test/*.c clean : $(CLEAN_LOG) $(BIN_TARGET) @rm -f $(BIN_TARGET)||true diff --git a/test/Makefile b/test/Makefile index 6d52958e..4b2f88b2 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,4 +2,5 @@ all : @echo "\033[33mWarning: This test is under sudo, do not run it on your device.\033[0m" @echo "\033[33mYou have 5 seconds to press Ctrl+C to cancel.\033[0m" @sleep 5 - @sudo bash test-root.sh \ No newline at end of file + @cc -o test-root test-root.c + @sudo ./test-root \ No newline at end of file diff --git a/test/root/3-capability.sh b/test/root/3-capability.sh index 655cfc6a..a422b2f4 100755 --- a/test/root/3-capability.sh +++ b/test/root/3-capability.sh @@ -10,7 +10,7 @@ export SUBTEST_DESCRIPTION="Default capability" show_subtest_description cd ${TMPDIR} cat <test/test.sh -cat /proc/self/status | grep CapEff| awk '{print \$2}' > /cap +cat /proc/self/status | grep CapBnd| awk '{print \$2}' > /cap EOF chmod 777 test/test.sh ./ruri ./test /bin/sh /test.sh @@ -55,7 +55,7 @@ export SUBTEST_DESCRIPTION="Add capability CAP_SYS_ADMIN" show_subtest_description cd ${TMPDIR} cat <test/test.sh -cat /proc/self/status | grep CapEff| awk '{print \$2}' > /cap +cat /proc/self/status | grep CapBnd| awk '{print \$2}' > /cap EOF chmod 777 test/test.sh ./ruri -k cap_sys_admin ./test /bin/sh /test.sh @@ -72,7 +72,7 @@ export SUBTEST_DESCRIPTION="Drop capability CAP_CHOWN" show_subtest_description cd ${TMPDIR} cat <test/test.sh -cat /proc/self/status | grep CapEff| awk '{print \$2}' > /cap +cat /proc/self/status | grep CapBnd| awk '{print \$2}' > /cap EOF chmod 777 test/test.sh ./ruri -d cap_chown ./test /bin/sh /test.sh @@ -89,7 +89,7 @@ export SUBTEST_DESCRIPTION="Drop all capabilities" show_subtest_description cd ${TMPDIR} cat <test/test.sh -cat /proc/self/status | grep CapEff| awk '{print \$2}' > /cap +cat /proc/self/status | grep CapBnd| awk '{print \$2}' > /cap EOF chmod 777 test/test.sh for i in $(seq 0 40); do @@ -109,7 +109,7 @@ export SUBTEST_DESCRIPTION="Keep all capabilities(privileged)" show_subtest_description cd ${TMPDIR} cat <test/test.sh -cat /proc/self/status | grep CapEff| awk '{print \$2}' > /cap +cat /proc/self/status | grep CapBnd| awk '{print \$2}' > /cap EOF chmod 777 test/test.sh ./ruri -p ./test /bin/sh /test.sh @@ -126,7 +126,7 @@ export SUBTEST_DESCRIPTION="Drop all capabilities but keep CAP_SYS_ADMIN" show_subtest_description cd ${TMPDIR} cat <test/test.sh -cat /proc/self/status | grep CapEff| awk '{print \$2}' > /cap +cat /proc/self/status | grep CapBnd| awk '{print \$2}' > /cap EOF chmod 777 test/test.sh for i in $(seq 0 40); do diff --git a/test/test-root.c b/test/test-root.c new file mode 100644 index 00000000..97627b59 --- /dev/null +++ b/test/test-root.c @@ -0,0 +1,25 @@ +#include "../src/include/ruri.h" +int main() +{ + // Timeout: 400s + // This file will ensure that the test will not stuck. + pid_t pid = fork(); + if (pid > 0) { + int status; + for (int i = 0; i < 400; i++) { + sleep(1); + if (waitpid(pid, &status, WNOHANG) == pid) { + exit(status); + } + } + if (waitpid(pid, &status, WNOHANG) == 0) { + kill(pid, SIGKILL); + printf("Timeout\n"); + exit(114); + } + exit(0); + } + char *command[] = { "bash", "test-root.sh", NULL }; + execvp(command[0], command); + return 0; +} \ No newline at end of file