xref: /aosp_15_r20/external/elfutils/tests/run-debuginfod-response-headers.sh (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
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
35
36# Compile a simple program, strip its debuginfo and save the build-id.
37# Also move the debuginfo into another directory so that elfutils
38# cannot find it without debuginfod.
39echo "int main() { return 0; }" > ${PWD}/prog.c
40tempfiles prog.c
41# Create a subdirectory to confound source path names
42mkdir foobar
43gcc -Wl,--build-id -g -o prog ${PWD}/foobar///./../prog.c
44
45mv prog F
46
47cp -rvp ${abs_srcdir}/debuginfod-rpms R
48if [ "$zstd" = "false" ]; then  # nuke the zstd fedora 31 ones
49    rm -vrf R/debuginfod-rpms/fedora31
50fi
51
52env 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 &
53PID1=$!
54tempfiles vlog$PORT1
55errfiles vlog$PORT1
56# Server must become ready
57wait_ready $PORT1 'ready' 1
58export DEBUGINFOD_URLS=http://127.0.0.1:$PORT1/   # or without trailing /
59########################################################################
60
61# Wait till both files are in the index and scan/index fully finished
62wait_ready $PORT1 'ready' 1
63wait_ready $PORT1 'thread_work_total{role="traverse"}' 1
64wait_ready $PORT1 'thread_work_pending{role="scan"}' 0
65wait_ready $PORT1 'thread_busy{role="scan"}' 0
66
67# All rpms need to be in the index, except the dummy permission-000 one
68rpms=$(find R -name \*rpm | grep -v nothing | wc -l)
69wait_ready $PORT1 'scanned_files_total{source=".rpm archive"}' $rpms
70
71kill -USR1 $PID1  # two hits of SIGUSR1 may be needed to resolve .debug->dwz->srefs
72# Wait till both files are in the index and scan/index fully finished
73wait_ready $PORT1 'thread_work_total{role="traverse"}' 2
74
75########################################################################
76## PR27277
77# Make a simple request to the debuginfod server and check debuginfod-find's vlog to see if
78# the custom HTTP headers are received.
79rm -rf $DEBUGINFOD_CACHE_PATH
80env DEBUGINFOD_URLS="http://127.0.0.1:"$PORT1 LD_LIBRARY_PATH=$ldpath ${abs_top_builddir}/debuginfod/debuginfod-find\
81    -vvv executable F/prog > vlog-find$PORT1.1 2>&1
82tempfiles vlog-find$PORT1.1
83errfiles vlog-find$PORT1.1
84cat vlog-find$PORT1.1
85grep 'Headers:' vlog-find$PORT1.1
86grep -i 'X-DEBUGINFOD-FILE: .*/prog' vlog-find$PORT1.1
87grep -i 'X-DEBUGINFOD-SIZE: '        vlog-find$PORT1.1
88
89# Check to see if an executable file located in an archive prints the file's description and archive
90env DEBUGINFOD_URLS="http://127.0.0.1:"$PORT1 LD_LIBRARY_PATH=$ldpath ${abs_top_builddir}/debuginfod/debuginfod-find\
91    -vvv executable c36708a78618d597dee15d0dc989f093ca5f9120 > vlog-find$PORT1.2 2>&1
92tempfiles vlog-find$PORT1.2
93errfiles vlog-find$PORT1.2
94cat vlog-find$PORT1.2
95grep 'Headers:'               vlog-find$PORT1.2
96grep -i 'X-DEBUGINFOD-FILE: .*/.*'    vlog-find$PORT1.2
97grep -i 'X-DEBUGINFOD-SIZE: '    vlog-find$PORT1.2
98grep -i 'X-DEBUGINFOD-ARCHIVE: .*/.*' vlog-find$PORT1.2
99
100# Check that X-DEBUGINFOD-SIZE matches the size of each file
101for file in vlog-find$PORT1.1 vlog-find$PORT1.2
102do
103    st_size=$(stat -c%s $(tail -n 1 $file))
104    x_debuginfod_size=$(grep -i 'X-DEBUGINFOD-SIZE' $file | head -1 | grep -E -o '[0-9]+')
105    test $st_size -eq $x_debuginfod_size
106done
107
108rm -rf $DEBUGINFOD_CACHE_PATH
109BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
110          -a F/prog | grep 'Build ID' | cut -d ' ' -f 7`
111netcat_dir="buildid/$BUILDID/"
112mkdir -p ${PWD}/$netcat_dir
113cp F/prog ${PWD}/$netcat_dir/executable
114tempfiles F/prog
115
116# socat should after answering one request
117(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) &
118PID2=$!
119# Wait a bit until the netcat port is in use. Otherwise debuginfod-find can query
120# before netcat is ready.
121sleep 5
122
123touch vlog-find$PORT2
124errfiles vlog-find$PORT2
125tempfiles vlog-find$PORT2
126
127# calling out to valgrind deliberately, because this process will be forced to parse broken http headers
128${VALGRIND_CMD} env DEBUGINFOD_URLS="http://127.0.0.1:"$PORT2 LD_LIBRARY_PATH=$ldpath ${abs_top_builddir}/debuginfod/debuginfod-find\
129    -vvv executable $BUILDID > vlog-find$PORT2 2>&1 || true # permit curl rejection of the bad headers
130cat vlog-find$PORT2 # won't have any valid x-debuginfod* headers
131rm -f "$netcat_dir"executable
132rmdir -p $netcat_dir
133
134kill $PID2 || true
135wait $PID2 || true
136PID2=0
137
138kill $PID1
139wait $PID1
140PID1=0
141
142exit 0
143