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 28export DEBUGINFOD_URLS='http://127.0.0.1:0' # Note invalid, will trigger error_count metric 29tempfiles $DB 30# Clean old dirictories 31mkdir D L F 32 33# not tempfiles F R L D Z - they are directories which we clean up manually 34ln -s ${abs_builddir}/dwfllines L/foo # any program not used elsewhere in this test 35# This variable is essential and ensures no time-race for claiming ports occurs 36# set base to a unique multiple of 100 not used in any other 'run-debuginfod-*' test 37base=9000 38get_ports 39 40# Launch server which will be unable to follow symlinks 41env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../debuginfod/debuginfod $VERBOSE -d ${DB} -F -U -t0 -g0 -p $PORT1 L D F > vlog$PORT1 2>&1 & 42PID1=$! 43tempfiles vlog$PORT1 44errfiles vlog$PORT1 45 46wait_ready $PORT1 'ready' 1 47 48# Wait till initial scan is done 49wait_ready $PORT1 'thread_work_total{role="traverse"}' 1 50wait_ready $PORT1 'thread_work_pending{role="scan"}' 0 51wait_ready $PORT1 'thread_busy{role="scan"}' 0 52######################################################################## 53# Compile a simple program, strip its debuginfo and save the build-id. 54# Also move the debuginfo into another directory so that elfutils 55# cannot find it without debuginfod. 56echo "int main() { return 0; }" > ${PWD}/prog.c 57tempfiles prog.c 58# Create a subdirectory to confound source path names 59mkdir foobar 60gcc -Wl,--build-id -g -o prog ${PWD}/foobar///./../prog.c 61testrun ${abs_top_builddir}/src/strip -g -f prog.debug ${PWD}/prog 62BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \ 63 -a prog | grep 'Build ID' | cut -d ' ' -f 7` 64 65mv prog F 66mv prog.debug F 67kill -USR1 $PID1 68# Wait till both files are in the index. 69wait_ready $PORT1 'thread_work_total{role="traverse"}' 2 70wait_ready $PORT1 'thread_work_pending{role="scan"}' 0 71wait_ready $PORT1 'thread_busy{role="scan"}' 0 72 73wait_ready $PORT1 'thread_busy{role="http-buildid"}' 0 74wait_ready $PORT1 'thread_busy{role="http-metrics"}' 1 75 76export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache2 77mkdir -p $DEBUGINFOD_CACHE_PATH 78# NB: run in -L symlink-following mode for the L subdir 79env 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 > vlog$PORT2 2>&1 & 80PID2=$! 81tempfiles vlog$PORT2 82errfiles vlog$PORT2 83tempfiles ${DB}_2 84 85wait_ready $PORT2 'ready' 1 86wait_ready $PORT2 'thread_work_total{role="traverse"}' 1 87wait_ready $PORT2 'thread_work_pending{role="scan"}' 0 88wait_ready $PORT2 'thread_busy{role="scan"}' 0 89 90wait_ready $PORT2 'thread_busy{role="http-buildid"}' 0 91wait_ready $PORT2 'thread_busy{role="http-metrics"}' 1 92 93# have clients contact the new server 94export DEBUGINFOD_URLS=http://127.0.0.1:$PORT2 95# Use fresh cache for debuginfod-find client requests 96export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache3 97mkdir -p $DEBUGINFOD_CACHE_PATH 98 99if type bsdtar 2>/dev/null; then 100 # copy in the deb files 101 cp -rvp ${abs_srcdir}/debuginfod-debs/*deb D 102 kill -USR1 $PID2 103 wait_ready $PORT2 'thread_work_total{role="traverse"}' 2 104 wait_ready $PORT2 'thread_work_pending{role="scan"}' 0 105 wait_ready $PORT2 'thread_busy{role="scan"}' 0 106 107 # All debs need to be in the index 108 debs=$(find D -name \*.deb | wc -l) 109 wait_ready $PORT2 'scanned_files_total{source=".deb archive"}' `expr $debs` 110 ddebs=$(find D -name \*.ddeb | wc -l) 111 wait_ready $PORT2 'scanned_files_total{source=".ddeb archive"}' `expr $ddebs` 112 113 # ubuntu 114 archive_test f17a29b5a25bd4960531d82aa6b07c8abe84fa66 "" "" 115fi 116 117testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID 118 119# send a request to stress XFF and User-Agent federation relay; 120# we'll grep for the two patterns in vlog$PORT1 121curl -s -H 'User-Agent: TESTCURL' -H 'X-Forwarded-For: TESTXFF' $DEBUGINFOD_URLS/buildid/deaddeadbeef00000000/debuginfo -o /dev/null || true 122 123grep UA:TESTCURL vlog$PORT1 124grep XFF:TESTXFF vlog$PORT1 125 126# confirm that first server can't resolve symlinked info in L/ but second can 127BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \ 128 -a L/foo | grep 'Build ID' | cut -d ' ' -f 7` 129file L/foo 130file -L L/foo 131export DEBUGINFOD_URLS=http://127.0.0.1:$PORT1 132rm -rf $DEBUGINFOD_CACHE_PATH 133testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID && false || true 134rm -f $DEBUGINFOD_CACHE_PATH/$BUILDID/debuginfo # drop 000-perm negative-hit file 135export DEBUGINFOD_URLS=http://127.0.0.1:$PORT2 136testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID 137 138# test again with scheme free url 139export DEBUGINFOD_URLS=127.0.0.1:$PORT1 140rm -rf $DEBUGINFOD_CACHE_PATH 141testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID && false || true 142rm -f $DEBUGINFOD_CACHE_PATH/$BUILDID/debuginfo # drop 000-perm negative-hit file 143export DEBUGINFOD_URLS=127.0.0.1:$PORT2 144testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID 145# test parallel queries in client 146export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache3 147mkdir -p $DEBUGINFOD_CACHE_PATH 148export DEBUGINFOD_URLS="BAD http://127.0.0.1:$PORT1 127.0.0.1:$PORT1 http://127.0.0.1:$PORT2 DNE" 149 150testrun ${abs_builddir}/debuginfod_build_id_find -e F/prog 1 151 152######################################################################## 153# Fetch some metrics 154curl -s http://127.0.0.1:$PORT1/badapi 155curl -s http://127.0.0.1:$PORT1/metrics 156curl -s http://127.0.0.1:$PORT2/metrics 157curl -s http://127.0.0.1:$PORT1/metrics | grep -q 'http_responses_total.*result.*error' 158curl -s http://127.0.0.1:$PORT2/metrics | grep -q 'http_responses_total.*result.*upstream' 159curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_duration_milliseconds_count' 160curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_duration_milliseconds_sum' 161curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_transfer_bytes_count' 162curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_transfer_bytes_sum' 163curl -s http://127.0.0.1:$PORT1/metrics | grep 'fdcache_' 164curl -s http://127.0.0.1:$PORT1/metrics | grep 'error_count' 165curl -s http://127.0.0.1:$PORT1/metrics | grep 'traversed_total' 166curl -s http://127.0.0.1:$PORT1/metrics | grep 'scanned_bytes_total' 167 168# And generate a few errors into the second debuginfod's logs, for analysis just below 169curl -s http://127.0.0.1:$PORT2/badapi > /dev/null || true 170curl -s http://127.0.0.1:$PORT2/buildid/deadbeef/debuginfo > /dev/null || true 171# NB: this error is used to seed the 404 failure for the survive-404 tests 172 173# Confirm bad artifact types are rejected without leaving trace 174curl -s http://127.0.0.1:$PORT2/buildid/deadbeef/badtype > /dev/null || true 175(curl -s http://127.0.0.1:$PORT2/metrics | grep 'badtype') && false 176 177# Confirm that reused curl connections survive 404 errors. 178# The rm's force an uncached fetch (in both servers and client cache) 179rm -f .client_cache*/$BUILDID/debuginfo 180testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID 181rm -f .client_cache*/$BUILDID/debuginfo 182testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID 183testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID 184testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID 185rm -f .client_cache*/$BUILDID/debuginfo 186testrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID 187 188# Confirm that some debuginfod client pools are being used 189curl -s http://127.0.0.1:$PORT2/metrics | grep 'dc_pool_op.*reuse' 190 191# Trigger a flood of requests against the same archive content file. 192# Use a file that hasn't been previously extracted in to make it 193# likely that even this test debuginfod will experience concurrency 194# and impose some "after-you" delays. 195(for i in `seq 100`; do 196 curl -s http://127.0.0.1:$PORT1/buildid/87c08d12c78174f1082b7c888b3238219b0eb265/executable >/dev/null & 197 done; 198 wait) 199curl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_after_you.*' 200# If we could guarantee some minimum number of seconds of CPU time, we 201# could assert that the after_you metrics show some nonzero amount of 202# waiting. A few hundred ms is typical on this developer's workstation. 203 204kill $PID1 205kill $PID2 206wait $PID1 207wait $PID2 208PID1=0 209PID2=0 210exit 0 211 212