reduce memory allocations to zero
[platform/upstream/glog.git] / src / signalhandler_unittest.sh
1 #! /bin/sh
2 #
3 # Copyright (c) 2008, Google Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9 #
10 #     * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 #     * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following disclaimer
14 # in the documentation and/or other materials provided with the
15 # distribution.
16 #     * Neither the name of Google Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #
32 # Author: Satoru Takabayashi
33 #
34 # Unit tests for signalhandler.cc.
35
36 die () {
37     echo $1
38     exit 1
39 }
40
41 BINDIR=".libs"
42 LIBGLOG="$BINDIR/libglog.so"
43
44 BINARY="$BINDIR/signalhandler_unittest"
45 LOG_INFO="./signalhandler_unittest.INFO"
46
47 # Remove temporary files.
48 rm -f signalhandler.out*
49
50 if test -e "$BINARY"; then
51   # We need shared object.
52   export LD_LIBRARY_PATH=$BINDIR
53   export DYLD_LIBRARY_PATH=$BINDIR
54 else
55   # For windows
56   BINARY="./signalhandler_unittest.exe"
57   if ! test -e "$BINARY"; then
58     echo "We coundn't find demangle_unittest binary."
59     exit 1
60   fi
61 fi
62
63 if [ x`$BINARY` != 'xOK' ]; then
64   echo "PASS (No stacktrace support. We don't run this test.)"
65   exit 0
66 fi
67
68 # The PC cannot be obtained in signal handlers on PowerPC correctly.
69 # We just skip the test for PowerPC.
70 if [ x`uname -p` = x"powerpc" ]; then
71   echo "PASS (We don't test the signal handler on PowerPC.)"
72   exit 0
73 fi
74
75 # Test for a case the program kills itself by SIGSEGV.
76 GOOGLE_LOG_DIR=. $BINARY segv 2> signalhandler.out1
77 for pattern in SIGSEGV 0xdead main "Aborted at [0-9]"; do
78   if ! grep --quiet "$pattern" signalhandler.out1; then
79     die "'$pattern' should appear in the output"
80   fi
81 done
82 if ! grep --quiet "a message before segv" $LOG_INFO; then
83   die "'a message before segv' should appear in the INFO log"
84 fi
85 rm -f $LOG_INFO
86
87 # Test for a case the program is killed by this shell script.
88 # $! = the process id of the last command run in the background.
89 # $$ = the process id of this shell.
90 $BINARY loop 2> signalhandler.out2 &
91 # Wait until "looping" is written in the file.  This indicates the program
92 # is ready to accept signals.
93 while true; do
94   if grep --quiet looping signalhandler.out2; then
95     break
96   fi
97 done
98 kill -TERM $!
99 wait $!
100
101 from_pid=''
102 # Only linux has the process ID of the signal sender.
103 if [ x`uname` = "xLinux" ]; then
104   from_pid="from PID $$"
105 fi
106 for pattern in SIGTERM "by PID $!" "$from_pid" main "Aborted at [0-9]"; do
107   if ! grep --quiet "$pattern" signalhandler.out2; then
108     die "'$pattern' should appear in the output"
109   fi
110 done
111
112 # Test for a case the program dies in a non-main thread.
113 $BINARY die_in_thread 2> signalhandler.out3
114 EXPECTED_TID="`sed 's/ .*//; q' signalhandler.out3`"
115
116 for pattern in SIGFPE DieInThread "TID $EXPECTED_TID" "Aborted at [0-9]"; do
117   if ! grep --quiet "$pattern" signalhandler.out3; then
118     die "'$pattern' should appear in the output"
119   fi
120 done
121
122 # Test for a case the program installs a custom failure writer that writes
123 # stuff to stdout instead of stderr.
124 $BINARY dump_to_stdout 1> signalhandler.out4
125 for pattern in SIGABRT main "Aborted at [0-9]"; do
126   if ! grep --quiet "$pattern" signalhandler.out4; then
127     die "'$pattern' should appear in the output"
128   fi
129 done
130
131 echo PASS