Skip to content

Commit

Permalink
Add test-root.c to avoid test timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Moe-hacker committed Dec 20, 2024
1 parent 0e7746a commit 147cece
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ debian/source
debian/.debhelper
debian/debhelper-build-stamp
debian/files
tmpdir*
tmpdir*
test-root
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
@cc -o test-root test-root.c
@sudo ./test-root
12 changes: 6 additions & 6 deletions test/root/3-capability.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export SUBTEST_DESCRIPTION="Default capability"
show_subtest_description
cd ${TMPDIR}
cat <<EOF >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
Expand Down Expand Up @@ -55,7 +55,7 @@ export SUBTEST_DESCRIPTION="Add capability CAP_SYS_ADMIN"
show_subtest_description
cd ${TMPDIR}
cat <<EOF >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
Expand All @@ -72,7 +72,7 @@ export SUBTEST_DESCRIPTION="Drop capability CAP_CHOWN"
show_subtest_description
cd ${TMPDIR}
cat <<EOF >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
Expand All @@ -89,7 +89,7 @@ export SUBTEST_DESCRIPTION="Drop all capabilities"
show_subtest_description
cd ${TMPDIR}
cat <<EOF >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
Expand All @@ -109,7 +109,7 @@ export SUBTEST_DESCRIPTION="Keep all capabilities(privileged)"
show_subtest_description
cd ${TMPDIR}
cat <<EOF >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
Expand All @@ -126,7 +126,7 @@ export SUBTEST_DESCRIPTION="Drop all capabilities but keep CAP_SYS_ADMIN"
show_subtest_description
cd ${TMPDIR}
cat <<EOF >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
Expand Down
25 changes: 25 additions & 0 deletions test/test-root.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 147cece

Please sign in to comment.