ref: aec67e43c52fcc1857d00d498d82189bbf0f680a
parent: 26cb2d03bbed1ee32a387d34a99fbd224f1719a5
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Sep 3 11:07:10 EDT 2018
[tests/libc] Add 0007-signal test
--- /dev/null
+++ b/tests/libc/execute/0007-signal.c
@@ -1,0 +1,168 @@
+#include <assert.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+
+sig_atomic_t abrt, fpe, iint, segv, term, def;
+jmp_buf recover;
+
+/*
+output:
+test 1
+test 2
+test 3
+test 4
+end:
+*/
+
+void
+handler1(int sig)
+{
+ switch (sig) {
+ case SIGABRT:
+ abrt = 1;
+ break;
+ case SIGFPE:
+ fpe = 1;
+ break;
+ case SIGINT:
+ iint = 1;
+ break;
+ case SIGSEGV:
+ segv = 1;
+ break;
+ case SIGTERM:
+ term = 1;
+ break;
+ default:
+ def = 1;
+ break;
+ }
+}
+
+void
+handler2(int sig)
+{
+ switch (sig) {
+ case SIGABRT:
+ abrt = 1;
+ break;
+ case SIGFPE:
+ fpe = 1;
+ break;
+ case SIGINT:
+ iint = 1;
+ break;
+ case SIGSEGV:
+ segv = 1;
+ break;
+ case SIGTERM:
+ term = 1;
+ break;
+ default:
+ def = 1;
+ break;
+ }
+ longjmp(recover, 1);
+}
+
+void
+test1()
+{
+ puts("test 1");
+ assert(signal(SIGABRT, handler1) == SIG_DFL);
+ assert(signal(SIGFPE, handler1) == SIG_DFL);
+ assert(signal(SIGINT, handler1) == SIG_DFL);
+ assert(signal(SIGSEGV, handler1) == SIG_DFL);
+ assert(signal(SIGTERM, handler1) == SIG_DFL);
+}
+
+void
+test2()
+{
+ puts("test 2");
+ term = abrt = fpe = iint = segv = 0;
+ assert(signal(SIGABRT, handler1) == handler1);
+ assert(signal(SIGFPE, handler1) == handler1);
+ assert(signal(SIGINT, handler1) == handler1);
+ assert(signal(SIGSEGV, handler1) == handler1);
+ assert(signal(SIGTERM, handler1) == handler1);
+
+ assert(raise(SIGABRT) != -1);
+ assert(raise(SIGFPE) != -1);
+ assert(raise(SIGINT) != -1);
+ assert(raise(SIGSEGV) != -1);
+ assert(raise(SIGTERM) != -1);
+
+ if (!abrt || !fpe || !iint || !segv || !term)
+ printf("a handled signal was missed: %d %d %d %d %d\n",
+ abrt, fpe, iint, segv, term);
+ if (def)
+ puts("a wrong signal was received");
+}
+
+void
+test3()
+{
+ puts("test 3");
+ def = abrt = term = fpe = iint = segv = 0;
+ assert(signal(SIGABRT, SIG_IGN) == handler1);
+ assert(signal(SIGFPE, SIG_IGN) == handler1);
+ assert(signal(SIGINT, SIG_IGN) == handler1);
+ assert(signal(SIGSEGV, SIG_IGN) == handler1);
+ assert(signal(SIGTERM, SIG_IGN) == handler1);
+
+ assert(raise(SIGABRT) != -1);
+ assert(raise(SIGFPE) != -1);
+ assert(raise(SIGINT) != -1);
+ assert(raise(SIGSEGV) != -1);
+ assert(raise(SIGTERM) != -1);
+
+ if (abrt || fpe || iint || segv || term)
+ printf("a handled signal was received: %d %d %d %d %d\n",
+ abrt, fpe, iint, segv, term);
+ if (def)
+ puts("a wrong signal was received");
+}
+
+void
+test4()
+{
+ puts("test 4");
+ def = abrt = term = fpe = iint = segv = 0;
+ assert(signal(SIGABRT, handler2) == SIG_IGN);
+ assert(signal(SIGFPE, handler2) == SIG_IGN);
+ assert(signal(SIGINT, handler2) == SIG_IGN);
+ assert(signal(SIGSEGV, handler2) == SIG_IGN);
+ assert(signal(SIGTERM, handler2) == SIG_IGN);
+
+ if (!setjmp(recover))
+ assert(raise(SIGABRT) != -1);
+ if (!setjmp(recover))
+ assert(raise(SIGFPE) != -1);
+ if (!setjmp(recover))
+ assert(raise(SIGINT) != -1);
+ if (!setjmp(recover))
+ assert(raise(SIGSEGV) != -1);
+ if (!setjmp(recover))
+ assert(raise(SIGTERM) != -1);
+
+ if (!abrt || !fpe || !iint || !segv || !term)
+ printf("a handled signal was missed: %d %d %d %d %d\n",
+ abrt, fpe, iint, segv, term);
+ if (def)
+ puts("a wrong signal was received");
+}
+
+int
+main()
+{
+ assert(SIG_ERR != SIG_IGN && SIG_ERR != SIG_DFL);
+
+ test1();
+ test2();
+ test3();
+ test4();
+
+ return;
+}
--- a/tests/libc/execute/Makefile
+++ b/tests/libc/execute/Makefile
@@ -1,6 +1,8 @@
.POSIX:
-ROOT=../../../rootdir
+ROOT = ../../../rootdir
+CFLAGS =
+CC = SCCPREFIX=$(ROOT) ./cc.sh
all: tests
@@ -9,4 +11,3 @@
clean:
rm -f *.o *core a.out test.log
-
--- a/tests/libc/execute/cc.sh
+++ b/tests/libc/execute/cc.sh
@@ -34,13 +34,17 @@
root=${root:-$SCCPREFIX}
inc=$root/include/scc
arch_inc=$root/include/scc/bits/$abi
+sys_inc=$root/include/scc/bits/$sys
lib=$root/lib/scc/${abi}-${sys}
obj=${1%.c}.o
+includes="-nostdinc -I$inc -I$arch_inc -I$sys_inc"
+flags="-std=c99 -g -w -fno-stack-protector --freestanding -static"
+
if ! gcc -nopie 2>&1 | grep unrecogn >/dev/null
then
pie=-nopie
fi
-gcc -std=c99 -g -w $pie -fno-stack-protector --freestanding -std=c99 -static -nostdinc -I$inc -I$arch_inc -c $1
+gcc $flags $pie $includes -c $1
ld -g $pie -z nodefaultlib -static -L$lib $lib/crt.o $obj -lc -o $out
--- a/tests/libc/execute/libc-tests.lst
+++ b/tests/libc/execute/libc-tests.lst
@@ -4,3 +4,4 @@
0004-abort
0005-ctype
0006-limits
+0007-signal