Forgot to add file for the previous change...
[platform/upstream/glog.git] / src / signalhandler_unittest.sh
1 #! /bin/sh
2 # Copyright 2008 Google, Inc.  All Rights Reserved.
3 # Author: Satoru Takabayashi
4 #
5 # Unit tests for signalhandler.cc.
6
7 die () {
8     echo $1
9     exit 1
10 }
11
12 BINDIR=".libs"
13 LIBGLOG="$BINDIR/libglog.so"
14
15 BINARY="$BINDIR/signalhandler_unittest"
16
17 # Remove temporary files.
18 rm -f signalhandler.out*
19
20 if test -e "$BINARY"; then
21   # We need shared object.
22   export LD_LIBRARY_PATH=$BINDIR
23   export DYLD_LIBRARY_PATH=$BINDIR
24 else
25   # For windows
26   BINARY="./signalhandler_unittest.exe"
27   if ! test -e "$BINARY"; then
28     echo "We coundn't find demangle_unittest binary."
29     exit 1
30   fi
31 fi
32
33 if [ x`$BINARY` != 'xOK' ]; then
34   echo "PASS (No stacktrace support. We don't run this test.)"
35   exit 0
36 fi
37
38 # Test for a case the program kills itself by SIGSEGV.
39 $BINARY segv 2> signalhandler.out1
40 for pattern in SIGSEGV 0xdead main "Aborted at [0-9]"; do
41   if ! grep --quiet "$pattern" signalhandler.out1; then
42     die "'$pattern' should appear in the output"
43   fi
44 done
45
46 # Test for a case the program is killed by this shell script.
47 # $! = the process id of the last command run in the background.
48 # $$ = the process id of this shell.
49 $BINARY loop 2> signalhandler.out2 &
50 # Wait until "looping" is written in the file.  This indicates the program
51 # is ready to accept signals.
52 while true; do
53   if grep --quiet looping signalhandler.out2; then
54     break
55   fi
56 done
57 kill -TERM $!
58 wait $!
59
60 from_pid=''
61 # Only linux has the process ID of the signal sender.
62 if [ x`uname` = "xLinux" ]; then
63   from_pid="from PID $$"
64 fi
65 for pattern in SIGTERM "by PID $!" "$from_pid" main "Aborted at [0-9]"; do
66   if ! grep --quiet "$pattern" signalhandler.out2; then
67     die "'$pattern' should appear in the output"
68   fi
69 done
70
71 # Test for a case the program dies in a non-main thread.
72 $BINARY die_in_thread 2> signalhandler.out3
73 EXPECTED_TID="`sed 's/ .*//' signalhandler.out3`"
74
75 for pattern in SIGFPE DieInThread "TID $EXPECTED_TID" "Aborted at [0-9]"; do
76   if ! grep --quiet "$pattern" signalhandler.out3; then
77     die "'$pattern' should appear in the output"
78   fi
79 done
80
81 # Test for a case the program installs a custom failure writer that writes
82 # stuff to stdout instead of stderr.
83 $BINARY dump_to_stdout 1> signalhandler.out4
84 for pattern in SIGABRT main "Aborted at [0-9]"; do
85   if ! grep --quiet "$pattern" signalhandler.out4; then
86     die "'$pattern' should appear in the output"
87   fi
88 done
89
90 echo PASS