• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2#
3# Copyright (C) 2019-2021 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
19. $srcdir/debuginfod-subr.sh
20
21# for test case debugging, uncomment:
22set -x
23unset VALGRIND_CMD
24
25DB=${PWD}/.debuginfod_tmp.sqlite
26export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache
27export DEBUGINFOD_TIMEOUT=10
28tempfiles $DB
29
30# Clean old dirictories
31mkdir D L F
32# not tempfiles F R L D Z - they are directories which we clean up manually
33ln -s ${abs_builddir}/dwfllines L/foo   # any program not used elsewhere in this test
34
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
44testrun ${abs_top_builddir}/src/strip -g -f prog.debug ${PWD}/prog
45BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
46          -a prog | grep 'Build ID' | cut -d ' ' -f 7`
47
48mv prog F
49mv prog.debug F
50# This variable is essential and ensures no time-race for claiming ports occurs
51# set base to a unique multiple of 100 not used in any other 'run-debuginfod-*' test
52base=9100
53get_ports
54# Launch server which will be unable to follow symlinks
55env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../debuginfod/debuginfod $VERBOSE -d ${DB} -F -U -t0 -g0 -p $PORT1 L D F > vlog$PORT1 2>&1 &
56PID1=$!
57tempfiles vlog$PORT1
58errfiles vlog$PORT1
59
60wait_ready $PORT1 'ready' 1
61
62# Wait till initial scan is done
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
67export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache2
68mkdir -p $DEBUGINFOD_CACHE_PATH
69# NB: run in -L symlink-following mode for the L subdir
70env LD_LIBRARY_PATH=$ldpath DEBUGINFOD_URLS=http://127.0.0.1:$PORT1 ${abs_builddir}/../debuginfod/debuginfod $VERBOSE -d ${DB}_2 -F -U -p $PORT2 -L L D F > vlog$PORT2 2>&1 &
71PID2=$!
72tempfiles vlog$PORT2
73errfiles vlog$PORT2
74tempfiles ${DB}_2
75wait_ready $PORT2 'ready' 1
76# Wait till initial scan is done
77wait_ready $PORT2 'thread_work_total{role="traverse"}' 1
78# And initial groom cycle
79wait_ready $PORT1 'thread_work_total{role="groom"}' 1
80
81export DEBUGINFOD_URLS='http://127.0.0.1:'$PORT2
82# Use fresh cache for debuginfod-find client requests
83export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache3
84mkdir -p $DEBUGINFOD_CACHE_PATH
85
86if type bsdtar 2>/dev/null; then
87    # copy in the deb files
88    cp -rvp ${abs_srcdir}/debuginfod-debs/*deb D
89    kill -USR1 $PID2
90    wait_ready $PORT2 'thread_work_total{role="traverse"}' 2
91    wait_ready $PORT2 'thread_work_pending{role="scan"}' 0
92    wait_ready $PORT2 'thread_busy{role="scan"}' 0
93
94    # All debs need to be in the index
95    debs=$(find D -name \*.deb | wc -l)
96    wait_ready $PORT2 'scanned_files_total{source=".deb archive"}' `expr $debs`
97    ddebs=$(find D -name \*.ddeb | wc -l)
98    wait_ready $PORT2 'scanned_files_total{source=".ddeb archive"}' `expr $ddebs`
99
100    # ubuntu
101    archive_test f17a29b5a25bd4960531d82aa6b07c8abe84fa66 "" ""
102fi
103
104testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
105
106# send a request to stress XFF and User-Agent federation relay;
107# we'll grep for the two patterns in vlog$PORT1
108curl -s -H 'User-Agent: TESTCURL' -H 'X-Forwarded-For: TESTXFF' $DEBUGINFOD_URLS/buildid/deaddeadbeef00000000/debuginfo -o /dev/null || true
109
110grep UA:TESTCURL vlog$PORT1
111grep XFF:TESTXFF vlog$PORT1
112
113# confirm that first server can't resolve symlinked info in L/ but second can
114BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
115         -a L/foo | grep 'Build ID' | cut -d ' ' -f 7`
116file L/foo
117file -L L/foo
118export DEBUGINFOD_URLS=http://127.0.0.1:$PORT1
119rm -rf $DEBUGINFOD_CACHE_PATH
120testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID && false || true
121rm -f $DEBUGINFOD_CACHE_PATH/$BUILDID/debuginfo # drop 000-perm negative-hit file
122export DEBUGINFOD_URLS=http://127.0.0.1:$PORT2
123testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
124
125# test again with scheme free url
126export DEBUGINFOD_URLS=127.0.0.1:$PORT1
127rm -rf $DEBUGINFOD_CACHE_PATH
128testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID && false || true
129rm -f $DEBUGINFOD_CACHE_PATH/$BUILDID/debuginfo # drop 000-perm negative-hit file
130export DEBUGINFOD_URLS=127.0.0.1:$PORT2
131testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
132# test parallel queries in client
133rm -rf $DEBUGINFOD_CACHE_PATH
134export DEBUGINFOD_URLS="BAD http://127.0.0.1:$PORT1 127.0.0.1:$PORT1 http://127.0.0.1:$PORT2 DNE"
135
136testrun ${abs_builddir}/debuginfod_build_id_find -e F/prog 1
137
138# And generate a few errors into the second debuginfod's logs, for analysis just below
139curl -s http://127.0.0.1:$PORT2/badapi > /dev/null || true
140curl -s http://127.0.0.1:$PORT2/buildid/deadbeef/debuginfo > /dev/null || true
141# NB: this error is used to seed the 404 failure for the survive-404 tests
142
143# Confirm bad artifact types are rejected without leaving trace
144curl -s http://127.0.0.1:$PORT2/buildid/deadbeef/badtype > /dev/null || true
145(curl -s http://127.0.0.1:$PORT2/metrics | grep 'badtype') && false
146
147# Confirm that reused curl connections survive 404 errors.
148# The rm's force an uncached fetch (in both servers and client cache)
149rm -f .client_cache*/$BUILDID/debuginfo
150testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
151rm -f .client_cache*/$BUILDID/debuginfo
152testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
153testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
154testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
155rm -f .client_cache*/$BUILDID/debuginfo
156testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
157# Trigger a flood of requests against the same archive content file.
158# Use a file that hasn't been previously extracted in to make it
159# likely that even this test debuginfod will experience concurrency
160# and impose some "after-you" delays.
161(for i in `seq 100`; do
162    curl -s http://127.0.0.1:$PORT1/buildid/87c08d12c78174f1082b7c888b3238219b0eb265/executable >/dev/null &
163 done;
164 wait)
165curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_after_you.*'
166# If we could guarantee some minimum number of seconds of CPU time, we
167# could assert that the after_you metrics show some nonzero amount of
168# waiting.  A few hundred ms is typical on this developer's workstation.
169
170########################################################################
171# Corrupt the sqlite database and get debuginfod to trip across its errors
172curl -s http://127.0.0.1:$PORT1/metrics | grep 'sqlite3.*reset'
173dd if=/dev/zero of=$DB bs=1 count=1
174
175# trigger some random activity that's Sure to get sqlite3 upset
176kill -USR1 $PID1
177wait_ready $PORT1 'thread_work_total{role="traverse"}' 2
178wait_ready $PORT1 'thread_work_pending{role="scan"}' 0
179wait_ready $PORT1 'thread_busy{role="scan"}' 0
180kill -USR2 $PID1
181wait_ready $PORT1 'thread_work_total{role="groom"}' 2
182curl -s http://127.0.0.1:$PORT1/buildid/beefbeefbeefd00dd00d/debuginfo > /dev/null || true
183curl -s http://127.0.0.1:$PORT1/metrics | grep 'error_count.*sqlite'
184# Run the tests again without the servers running. The target file should
185# be found in the cache.
186
187kill -INT $PID1 $PID2
188wait $PID1 $PID2
189PID1=0
190PID2=0
191tempfiles .debuginfod_*
192
193testrun ${abs_builddir}/debuginfod_build_id_find -e F/prog 1
194
195# check out the debuginfod logs for the new style status lines
196cat vlog$PORT2
197grep -q 'UA:.*XFF:.*GET /buildid/.* 200 ' vlog$PORT2
198grep -q 'UA:.*XFF:.*GET /metrics 200 ' vlog$PORT2
199grep -q 'UA:.*XFF:.*GET /badapi 503 ' vlog$PORT2
200grep -q 'UA:.*XFF:.*GET /buildid/deadbeef.* 404 ' vlog$PORT2
201
202exit 0
203