xref: /aosp_15_r20/external/elfutils/tests/debuginfod-subr.sh (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1# Copyright (C) 2021 Red Hat, Inc.
2# This file is part of elfutils.
3#
4# This file is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# elfutils is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17# sourced from run-debuginfod-*.sh tests (must be bash scripts)
18
19# We trap ERR and like commands that fail in function to also trap
20set -o functrace
21set -o errtrace
22
23. $srcdir/test-subr.sh  # includes set -e
24
25type curl 2>/dev/null || (echo "need curl"; exit 77)
26type rpm2cpio 2>/dev/null || (echo "need rpm2cpio"; exit 77)
27type cpio 2>/dev/null || (echo "need cpio"; exit 77)
28type bzcat 2>/dev/null || (echo "need bzcat"; exit 77)
29bsdtar --version | grep -q zstd && zstd=true || zstd=false
30echo "zstd=$zstd bsdtar=`bsdtar --version`"
31
32cleanup()
33{
34  # No more cleanups after this cleanup
35  trap - 0
36
37  if [ $PID1 -ne 0 ]; then kill $PID1 || : ; wait $PID1 || :; fi
38  if [ $PID2 -ne 0 ]; then kill $PID2 || : ; wait $PID2 || :; fi
39  rm -rf F R D L Z ${PWD}/foobar ${PWD}/mocktree ${PWD}/.client_cache* ${PWD}/tmp*
40  exit_cleanup
41}
42
43# clean up trash if we exit
44trap cleanup 0
45
46errfiles_list=
47err() {
48    # Don't trap any new errors from now on
49    trap - ERR
50
51    echo ERROR REPORTS
52    for port in $PORT1 $PORT2
53    do
54        echo ERROR REPORT $port metrics
55        curl -s http://127.0.0.1:$port/metrics || :
56        echo
57    done
58    for x in $errfiles_list
59    do
60        echo ERROR REPORT "$x"
61        cat $x
62        echo
63    done
64    cleanup
65    false # trigger set -e
66}
67trap err ERR
68
69errfiles() {
70    errfiles_list="$errfiles_list $*"
71}
72
73# We want to run debuginfod in the background.  We also want to start
74# it with the same check/installcheck-sensitive LD_LIBRARY_PATH stuff
75# that the testrun alias sets.  But: we if we just use
76#    testrun .../debuginfod
77# it runs in a subshell, with different pid, so not helpful.
78#
79# So we gather the LD_LIBRARY_PATH with this cunning trick:
80ldpath=`testrun sh -c 'echo $LD_LIBRARY_PATH'`
81
82wait_ready4()
83{
84  port=$1;
85  what=$2;
86  value=$3;
87  timeout=$4;
88
89  echo "Wait $timeout seconds on $port for metric $what to change to $value"
90  while [ $timeout -gt 0 ]; do
91    mvalue="$(curl -s http://127.0.0.1:$port/metrics \
92              | grep "$what" | awk '{print $NF}')"
93    if [ -z "$mvalue" ]; then mvalue=0; fi
94      echo "metric $what: $mvalue"
95      if [ "$mvalue" -eq "$value" ]; then
96        break;
97    fi
98    sleep 0.5;
99    ((timeout--));
100  done;
101
102  if [ $timeout -eq 0 ]; then
103    echo "metric $what never changed to $value on port $port"
104    err
105  fi
106}
107
108wait_ready()
109{
110  port=$1;
111  what=$2;
112  value=$3;
113  timeout=20;
114  wait_ready4 "$port" "$what" "$value" "$timeout"
115}
116
117
118archive_test() {
119    __BUILDID=$1
120    __SOURCEPATH=$2
121    __SOURCESHA1=$3
122
123    filename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find executable $__BUILDID`
124    buildid=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
125             -a $filename | grep 'Build ID' | cut -d ' ' -f 7`
126    test $__BUILDID = $buildid
127    # check that timestamps are plausible - older than the near-present (tmpdir mtime)
128    test $filename -ot `pwd`
129
130    # run again to assure that fdcache is being enjoyed
131    filename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find executable $__BUILDID`
132    buildid=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
133             -a $filename | grep 'Build ID' | cut -d ' ' -f 7`
134    test $__BUILDID = $buildid
135    test $filename -ot `pwd`
136
137    filename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $__BUILDID`
138    buildid=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
139             -a $filename | grep 'Build ID' | cut -d ' ' -f 7`
140    test $__BUILDID = $buildid
141    test $filename -ot `pwd`
142
143    if test "x$__SOURCEPATH" != "x"; then
144        filename=`testrun ${abs_top_builddir}/debuginfod/debuginfod-find source $__BUILDID $__SOURCEPATH`
145        hash=`cat $filename | sha1sum | awk '{print $1}'`
146        test $__SOURCESHA1 = $hash
147        test $filename -ot `pwd`
148    fi
149}
150
151get_ports() {
152  while true; do
153    PORT1=`expr '(' $RANDOM % 50 ')' + $base`
154    ss -atn | grep -F ":$PORT1" || break
155  done
156# Some tests will use two servers, so assign the second var
157  while true; do
158    PORT2=`expr '(' $RANDOM % 50 ')' + $base + 50`
159    ss -atn | grep -F ":$PORT2" || break
160  done
161
162}
163
164VERBOSE=-vvv
165# We gather the LD_LIBRARY_PATH with this cunning trick:
166ldpath=`testrun sh -c 'echo $LD_LIBRARY_PATH'`
167PORT1=0
168PORT2=0
169PID1=0
170PID2=0
171
172
173# run $1 as a sh -c command, invert result code
174xfail() {
175    if sh -c "$1"; then
176        false
177    else
178        true
179    fi
180}
181