• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2#
3# Copyright (C) 2022 Red Hat, Inc.
4# This file is part of elfutils.
5#
6# This file is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
10#
11# elfutils is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19type socat 2>/dev/null || exit 77
20
21. $srcdir/debuginfod-subr.sh  # includes set -e
22
23# for test case debugging, uncomment:
24set -x
25
26DB=${PWD}/.debuginfod_tmp.sqlite
27tempfiles $DB
28export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache
29
30# This variable is essential and ensures no time-race for claiming ports occurs
31# set base to a unique multiple of 100 not used in any other 'run-debuginfod-*' test
32base=9500
33get_ports
34mkdir F R
35env LD_LIBRARY_PATH=$ldpath DEBUGINFOD_URLS= ${abs_builddir}/../debuginfod/debuginfod $VERBOSE -F -R -d $DB -p $PORT1 -t0 -g0 -v R F > vlog$PORT1 2>&1 &
36PID1=$!
37tempfiles vlog$PORT1
38errfiles vlog$PORT1
39# Server must become ready
40wait_ready $PORT1 'ready' 1
41export DEBUGINFOD_URLS=http://127.0.0.1:$PORT1/   # or without trailing /
42########################################################################
43
44# Compile a simple program, strip its debuginfo and save the build-id.
45# Also move the debuginfo into another directory so that elfutils
46# cannot find it without debuginfod.
47echo "int main() { return 0; }" > ${PWD}/prog.c
48tempfiles prog.c
49# Create a subdirectory to confound source path names
50mkdir foobar
51gcc -Wl,--build-id -g -o prog ${PWD}/foobar///./../prog.c
52
53mv prog F
54
55cp -rvp ${abs_srcdir}/debuginfod-rpms R
56if [ "$zstd" = "false" ]; then  # nuke the zstd fedora 31 ones
57    rm -vrf R/debuginfod-rpms/fedora31
58fi
59
60kill -USR1 $PID1
61# Wait till both files are in the index and scan/index fully finished
62wait_ready $PORT1 'thread_work_total{role="traverse"}' 1
63# All rpms need to be in the index, except the dummy permission-000 one
64rpms=$(find R -name \*rpm | grep -v nothing | wc -l)
65wait_ready $PORT1 'scanned_files_total{source=".rpm archive"}' $rpms
66kill -USR1 $PID1  # two hits of SIGUSR1 may be needed to resolve .debug->dwz->srefs
67# Wait till both files are in the index and scan/index fully finished
68wait_ready $PORT1 'thread_work_total{role="traverse"}' 2
69
70########################################################################
71## PR27277
72# Make a simple request to the debuginfod server and check debuginfod-find's vlog to see if
73# the custom HTTP headers are received.
74rm -rf $DEBUGINFOD_CACHE_PATH
75env DEBUGINFOD_URLS="http://127.0.0.1:"$PORT1 LD_LIBRARY_PATH=$ldpath ${abs_top_builddir}/debuginfod/debuginfod-find\
76    -vvv executable F/prog > vlog-find$PORT1.1 2>&1
77tempfiles vlog-find$PORT1.1
78errfiles vlog-find$PORT1.1
79cat vlog-find$PORT1.1
80grep 'Headers:' vlog-find$PORT1.1
81grep -i 'X-DEBUGINFOD-FILE: prog' vlog-find$PORT1.1
82grep -i 'X-DEBUGINFOD-SIZE: '     vlog-find$PORT1.1
83
84# Check to see if an executable file located in an archive prints the file's description and archive
85env DEBUGINFOD_URLS="http://127.0.0.1:"$PORT1 LD_LIBRARY_PATH=$ldpath ${abs_top_builddir}/debuginfod/debuginfod-find\
86    -vvv executable c36708a78618d597dee15d0dc989f093ca5f9120 > vlog-find$PORT1.2 2>&1
87tempfiles vlog-find$PORT1.2
88errfiles vlog-find$PORT1.2
89cat vlog-find$PORT1.2
90grep 'Headers:'               vlog-find$PORT1.2
91grep -i 'X-DEBUGINFOD-FILE: '    vlog-find$PORT1.2
92grep -i 'X-DEBUGINFOD-SIZE: '    vlog-find$PORT1.2
93grep -i 'X-DEBUGINFOD-ARCHIVE: ' vlog-find$PORT1.2
94
95# Check that X-DEBUGINFOD-SIZE matches the size of each file
96for file in vlog-find$PORT1.1 vlog-find$PORT1.2
97do
98    st_size=$(stat -c%s $(tail -n 1 $file))
99    x_debuginfod_size=$(grep -i 'X-DEBUGINFOD-SIZE' $file | head -1 | grep -E -o '[0-9]+')
100    test $st_size -eq $x_debuginfod_size
101done
102
103rm -rf $DEBUGINFOD_CACHE_PATH
104BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
105          -a F/prog | grep 'Build ID' | cut -d ' ' -f 7`
106netcat_dir="buildid/$BUILDID/"
107mkdir -p ${PWD}/$netcat_dir
108cp F/prog ${PWD}/$netcat_dir/executable
109tempfiles F/prog
110
111# socat should after answering one request
112(echo -e "HTTP/1.1 200 OK\r\nX-DEBUGINFOD-SIZE: ba:d_size\nX-DEBUGINFOD-\rFILE:\=\+ \r213\n\n $(date)" | socat -u - tcp-listen:$PORT2) &
113PID2=$!
114# Wait a bit until the netcat port is in use. Otherwise debuginfod-find can query
115# before netcat is ready.
116sleep 5
117
118touch vlog-find$PORT2
119errfiles vlog-find$PORT2
120tempfiles vlog-find$PORT2
121
122# calling out to valgrind deliberately, because this process will be forced to parse broken http headers
123${VALGRIND_CMD} env DEBUGINFOD_URLS="http://127.0.0.1:"$PORT2 LD_LIBRARY_PATH=$ldpath ${abs_top_builddir}/debuginfod/debuginfod-find\
124    -vvv executable $BUILDID > vlog-find$PORT2 2>&1 || true # permit curl rejection of the bad headers
125cat vlog-find$PORT2 # won't have any valid x-debuginfod* headers
126rm -f "$netcat_dir"executable
127rmdir -p $netcat_dir
128
129kill $PID2 || true
130wait $PID2 || true
131PID2=0
132
133kill $PID1
134wait $PID1
135PID1=0
136
137exit 0
138