blob: 91deee2198da6fd793185ead11f0c825b22ef26f [file] [log] [blame]
Austin Schuh906616c2019-01-21 20:25:11 -08001#! /bin/sh
2#
3# Copyright (c) 2006, 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 demangle.c with a real binary.
35
36set -e
37
38die () {
39 echo $1
40 exit 1
41}
42
43BINDIR=".libs"
44LIBGLOG="$BINDIR/libglog.so"
45
46DEMANGLER="$BINDIR/demangle_unittest"
47
48if test -e "$DEMANGLER"; then
49 # We need shared object.
50 export LD_LIBRARY_PATH=$BINDIR
51 export DYLD_LIBRARY_PATH=$BINDIR
52else
53 # For windows
54 DEMANGLER="./demangle_unittest.exe"
55 if ! test -e "$DEMANGLER"; then
56 echo "We coundn't find demangle_unittest binary."
57 exit 1
58 fi
59fi
60
61# Extract C++ mangled symbols from libbase.so.
62NM_OUTPUT="demangle.nm"
63nm "$LIBGLOG" | perl -nle 'print $1 if /\s(_Z\S+$)/' > "$NM_OUTPUT"
64
65# Check if mangled symbols exist. If there are none, we quit.
66# The binary is more likely compiled with GCC 2.95 or something old.
67if ! grep --quiet '^_Z' "$NM_OUTPUT"; then
68 echo "PASS"
69 exit 0
70fi
71
72# Demangle the symbols using our demangler.
73DM_OUTPUT="demangle.dm"
74GLOG_demangle_filter=1 "$DEMANGLER" --demangle_filter < "$NM_OUTPUT" > "$DM_OUTPUT"
75
76# Calculate the numbers of lines.
77NM_LINES=`wc -l "$NM_OUTPUT" | awk '{ print $1 }'`
78DM_LINES=`wc -l "$DM_OUTPUT" | awk '{ print $1 }'`
79
80# Compare the numbers of lines. They must be the same.
81if test "$NM_LINES" != "$DM_LINES"; then
82 die "$NM_OUTPUT and $DM_OUTPUT don't have the same numbers of lines"
83fi
84
85# Check if mangled symbols exist. They must not exist.
86if grep --quiet '^_Z' "$DM_OUTPUT"; then
87 MANGLED=`grep '^_Z' "$DM_OUTPUT" | wc -l | awk '{ print \$1 }'`
88 echo "Mangled symbols ($MANGLED out of $NM_LINES) found in $DM_OUTPUT:"
89 grep '^_Z' "$DM_OUTPUT"
90 die "Mangled symbols ($MANGLED out of $NM_LINES) found in $DM_OUTPUT"
91fi
92
93# All C++ symbols are demangled successfully.
94echo "PASS"
95exit 0