Initialize gflags in signalhandler_unittest.
[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 LOG_INFO="./signalhandler_unittest.INFO"
17
18 # Remove temporary files.
19 rm -f signalhandler.out*
20
21 if test -e "$BINARY"; then
22   # We need shared object.
23   export LD_LIBRARY_PATH=$BINDIR
24   export DYLD_LIBRARY_PATH=$BINDIR
25 else
26   # For windows
27   BINARY="./signalhandler_unittest.exe"
28   if ! test -e "$BINARY"; then
29     echo "We coundn't find demangle_unittest binary."
30     exit 1
31   fi
32 fi
33
34 if [ x`$BINARY` != 'xOK' ]; then
35   echo "PASS (No stacktrace support. We don't run this test.)"
36   exit 0
37 fi
38
39 # The PC cannot be obtained in signal handlers on PowerPC correctly.
40 # We just skip the test for PowerPC.
41 if [ x`uname -p` = x"powerpc" ]; then
42   echo "PASS (We don't test the signal handler on PowerPC.)"
43   exit 0
44 fi
45
46 # Test for a case the program kills itself by SIGSEGV.
47 GOOGLE_LOG_DIR=. $BINARY segv 2> signalhandler.out1
48 for pattern in SIGSEGV 0xdead main "Aborted at [0-9]"; do
49   if ! grep --quiet "$pattern" signalhandler.out1; then
50     die "'$pattern' should appear in the output"
51   fi
52 done
53 if ! grep --quiet "a message before segv" $LOG_INFO; then
54   die "'a message before segv' should appear in the INFO log"
55 fi
56 rm -f $LOG_INFO
57
58 # Test for a case the program is killed by this shell script.
59 # $! = the process id of the last command run in the background.
60 # $$ = the process id of this shell.
61 $BINARY loop 2> signalhandler.out2 &
62 # Wait until "looping" is written in the file.  This indicates the program
63 # is ready to accept signals.
64 while true; do
65   if grep --quiet looping signalhandler.out2; then
66     break
67   fi
68 done
69 kill -TERM $!
70 wait $!
71
72 from_pid=''
73 # Only linux has the process ID of the signal sender.
74 if [ x`uname` = "xLinux" ]; then
75   from_pid="from PID $$"
76 fi
77 for pattern in SIGTERM "by PID $!" "$from_pid" main "Aborted at [0-9]"; do
78   if ! grep --quiet "$pattern" signalhandler.out2; then
79     die "'$pattern' should appear in the output"
80   fi
81 done
82
83 # Test for a case the program dies in a non-main thread.
84 $BINARY die_in_thread 2> signalhandler.out3
85 EXPECTED_TID="`sed 's/ .*//' signalhandler.out3`"
86
87 for pattern in SIGFPE DieInThread "TID $EXPECTED_TID" "Aborted at [0-9]"; do
88   if ! grep --quiet "$pattern" signalhandler.out3; then
89     die "'$pattern' should appear in the output"
90   fi
91 done
92
93 # Test for a case the program installs a custom failure writer that writes
94 # stuff to stdout instead of stderr.
95 $BINARY dump_to_stdout 1> signalhandler.out4
96 for pattern in SIGABRT main "Aborted at [0-9]"; do
97   if ! grep --quiet "$pattern" signalhandler.out4; then
98     die "'$pattern' should appear in the output"
99   fi
100 done
101
102 echo PASS