1da0c48c4Sopenharmony_ci#!/usr/bin/env bash
2da0c48c4Sopenharmony_ci#
3da0c48c4Sopenharmony_ci# Copyright (C) 2019-2021 Red Hat, Inc.
4da0c48c4Sopenharmony_ci# This file is part of elfutils.
5da0c48c4Sopenharmony_ci#
6da0c48c4Sopenharmony_ci# This file is free software; you can redistribute it and/or modify
7da0c48c4Sopenharmony_ci# it under the terms of the GNU General Public License as published by
8da0c48c4Sopenharmony_ci# the Free Software Foundation; either version 3 of the License, or
9da0c48c4Sopenharmony_ci# (at your option) any later version.
10da0c48c4Sopenharmony_ci#
11da0c48c4Sopenharmony_ci# elfutils is distributed in the hope that it will be useful, but
12da0c48c4Sopenharmony_ci# WITHOUT ANY WARRANTY; without even the implied warranty of
13da0c48c4Sopenharmony_ci# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14da0c48c4Sopenharmony_ci# GNU General Public License for more details.
15da0c48c4Sopenharmony_ci#
16da0c48c4Sopenharmony_ci# You should have received a copy of the GNU General Public License
17da0c48c4Sopenharmony_ci# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18da0c48c4Sopenharmony_ci
19da0c48c4Sopenharmony_ci. $srcdir/debuginfod-subr.sh
20da0c48c4Sopenharmony_ci
21da0c48c4Sopenharmony_ci# for test case debugging, uncomment:
22da0c48c4Sopenharmony_ciset -x
23da0c48c4Sopenharmony_ciunset VALGRIND_CMD
24da0c48c4Sopenharmony_ci
25da0c48c4Sopenharmony_ciDB=${PWD}/.debuginfod_tmp.sqlite
26da0c48c4Sopenharmony_ciexport DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache
27da0c48c4Sopenharmony_citempfiles $DB
28da0c48c4Sopenharmony_ci
29da0c48c4Sopenharmony_ci# Clean old dirictories
30da0c48c4Sopenharmony_cimkdir D L F
31da0c48c4Sopenharmony_ci# not tempfiles F R L D Z - they are directories which we clean up manually
32da0c48c4Sopenharmony_ciln -s ${abs_builddir}/dwfllines L/foo   # any program not used elsewhere in this test
33da0c48c4Sopenharmony_ci
34da0c48c4Sopenharmony_ci########################################################################
35da0c48c4Sopenharmony_ci# Compile a simple program, strip its debuginfo and save the build-id.
36da0c48c4Sopenharmony_ci# Also move the debuginfo into another directory so that elfutils
37da0c48c4Sopenharmony_ci# cannot find it without debuginfod.
38da0c48c4Sopenharmony_ciecho "int main() { return 0; }" > ${PWD}/prog.c
39da0c48c4Sopenharmony_citempfiles prog.c
40da0c48c4Sopenharmony_ci# Create a subdirectory to confound source path names
41da0c48c4Sopenharmony_cimkdir foobar
42da0c48c4Sopenharmony_cigcc -Wl,--build-id -g -o prog ${PWD}/foobar///./../prog.c
43da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/src/strip -g -f prog.debug ${PWD}/prog
44da0c48c4Sopenharmony_ciBUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
45da0c48c4Sopenharmony_ci          -a prog | grep 'Build ID' | cut -d ' ' -f 7`
46da0c48c4Sopenharmony_ci
47da0c48c4Sopenharmony_cimv prog F
48da0c48c4Sopenharmony_cimv prog.debug F
49da0c48c4Sopenharmony_ci# This variable is essential and ensures no time-race for claiming ports occurs
50da0c48c4Sopenharmony_ci# set base to a unique multiple of 100 not used in any other 'run-debuginfod-*' test
51da0c48c4Sopenharmony_cibase=9100
52da0c48c4Sopenharmony_ciget_ports
53da0c48c4Sopenharmony_ci# Launch server which will be unable to follow symlinks
54da0c48c4Sopenharmony_cienv LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../debuginfod/debuginfod $VERBOSE -d ${DB} -F -U -t0 -g0 -p $PORT1 L D F > vlog$PORT1 2>&1 &
55da0c48c4Sopenharmony_ciPID1=$!
56da0c48c4Sopenharmony_citempfiles vlog$PORT1
57da0c48c4Sopenharmony_cierrfiles vlog$PORT1
58da0c48c4Sopenharmony_ci
59da0c48c4Sopenharmony_ciwait_ready $PORT1 'ready' 1
60da0c48c4Sopenharmony_ci
61da0c48c4Sopenharmony_ci# Wait till initial scan is done
62da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_total{role="traverse"}' 1
63da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_pending{role="scan"}' 0
64da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_busy{role="scan"}' 0
65da0c48c4Sopenharmony_ci
66da0c48c4Sopenharmony_ciexport DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache2
67da0c48c4Sopenharmony_cimkdir -p $DEBUGINFOD_CACHE_PATH
68da0c48c4Sopenharmony_ci# NB: run in -L symlink-following mode for the L subdir
69da0c48c4Sopenharmony_cienv 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 &
70da0c48c4Sopenharmony_ciPID2=$!
71da0c48c4Sopenharmony_citempfiles vlog$PORT2
72da0c48c4Sopenharmony_cierrfiles vlog$PORT2
73da0c48c4Sopenharmony_citempfiles ${DB}_2
74da0c48c4Sopenharmony_ciwait_ready $PORT2 'ready' 1
75da0c48c4Sopenharmony_ci# Wait till initial scan is done
76da0c48c4Sopenharmony_ciwait_ready $PORT2 'thread_work_total{role="traverse"}' 1
77da0c48c4Sopenharmony_ci# And initial groom cycle
78da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_total{role="groom"}' 1
79da0c48c4Sopenharmony_ci
80da0c48c4Sopenharmony_ciexport DEBUGINFOD_URLS='http://127.0.0.1:'$PORT2
81da0c48c4Sopenharmony_ci# Use fresh cache for debuginfod-find client requests
82da0c48c4Sopenharmony_ciexport DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache3
83da0c48c4Sopenharmony_cimkdir -p $DEBUGINFOD_CACHE_PATH
84da0c48c4Sopenharmony_ci
85da0c48c4Sopenharmony_ciif type bsdtar 2>/dev/null; then
86da0c48c4Sopenharmony_ci    # copy in the deb files
87da0c48c4Sopenharmony_ci    cp -rvp ${abs_srcdir}/debuginfod-debs/*deb D
88da0c48c4Sopenharmony_ci    kill -USR1 $PID2
89da0c48c4Sopenharmony_ci    wait_ready $PORT2 'thread_work_total{role="traverse"}' 2
90da0c48c4Sopenharmony_ci    wait_ready $PORT2 'thread_work_pending{role="scan"}' 0
91da0c48c4Sopenharmony_ci    wait_ready $PORT2 'thread_busy{role="scan"}' 0
92da0c48c4Sopenharmony_ci
93da0c48c4Sopenharmony_ci    # All debs need to be in the index
94da0c48c4Sopenharmony_ci    debs=$(find D -name \*.deb | wc -l)
95da0c48c4Sopenharmony_ci    wait_ready $PORT2 'scanned_files_total{source=".deb archive"}' `expr $debs`
96da0c48c4Sopenharmony_ci    ddebs=$(find D -name \*.ddeb | wc -l)
97da0c48c4Sopenharmony_ci    wait_ready $PORT2 'scanned_files_total{source=".ddeb archive"}' `expr $ddebs`
98da0c48c4Sopenharmony_ci
99da0c48c4Sopenharmony_ci    # ubuntu
100da0c48c4Sopenharmony_ci    archive_test f17a29b5a25bd4960531d82aa6b07c8abe84fa66 "" ""
101da0c48c4Sopenharmony_cifi
102da0c48c4Sopenharmony_ci
103da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
104da0c48c4Sopenharmony_ci
105da0c48c4Sopenharmony_ci# send a request to stress XFF and User-Agent federation relay;
106da0c48c4Sopenharmony_ci# we'll grep for the two patterns in vlog$PORT1
107da0c48c4Sopenharmony_cicurl -s -H 'User-Agent: TESTCURL' -H 'X-Forwarded-For: TESTXFF' $DEBUGINFOD_URLS/buildid/deaddeadbeef00000000/debuginfo -o /dev/null || true
108da0c48c4Sopenharmony_ci
109da0c48c4Sopenharmony_cigrep UA:TESTCURL vlog$PORT1
110da0c48c4Sopenharmony_cigrep XFF:TESTXFF vlog$PORT1
111da0c48c4Sopenharmony_ci
112da0c48c4Sopenharmony_ci# confirm that first server can't resolve symlinked info in L/ but second can
113da0c48c4Sopenharmony_ciBUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
114da0c48c4Sopenharmony_ci         -a L/foo | grep 'Build ID' | cut -d ' ' -f 7`
115da0c48c4Sopenharmony_cifile L/foo
116da0c48c4Sopenharmony_cifile -L L/foo
117da0c48c4Sopenharmony_ciexport DEBUGINFOD_URLS=http://127.0.0.1:$PORT1
118da0c48c4Sopenharmony_cirm -rf $DEBUGINFOD_CACHE_PATH
119da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID && false || true
120da0c48c4Sopenharmony_cirm -f $DEBUGINFOD_CACHE_PATH/$BUILDID/debuginfo # drop negative-hit file
121da0c48c4Sopenharmony_ciexport DEBUGINFOD_URLS=http://127.0.0.1:$PORT2
122da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
123da0c48c4Sopenharmony_ci
124da0c48c4Sopenharmony_ci# test again with scheme free url
125da0c48c4Sopenharmony_ciexport DEBUGINFOD_URLS=127.0.0.1:$PORT1
126da0c48c4Sopenharmony_cirm -rf $DEBUGINFOD_CACHE_PATH
127da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID && false || true
128da0c48c4Sopenharmony_cirm -f $DEBUGINFOD_CACHE_PATH/$BUILDID/debuginfo # drop negative-hit file
129da0c48c4Sopenharmony_ciexport DEBUGINFOD_URLS=127.0.0.1:$PORT2
130da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
131da0c48c4Sopenharmony_ci# test parallel queries in client
132da0c48c4Sopenharmony_cirm -rf $DEBUGINFOD_CACHE_PATH
133da0c48c4Sopenharmony_ciexport DEBUGINFOD_URLS="BAD http://127.0.0.1:$PORT1 127.0.0.1:$PORT1 http://127.0.0.1:$PORT2 DNE"
134da0c48c4Sopenharmony_ci
135da0c48c4Sopenharmony_citestrun ${abs_builddir}/debuginfod_build_id_find -e F/prog 1
136da0c48c4Sopenharmony_ci
137da0c48c4Sopenharmony_ci# And generate a few errors into the second debuginfod's logs, for analysis just below
138da0c48c4Sopenharmony_cicurl -s http://127.0.0.1:$PORT2/badapi > /dev/null || true
139da0c48c4Sopenharmony_cicurl -s http://127.0.0.1:$PORT2/buildid/deadbeef/debuginfo > /dev/null || true
140da0c48c4Sopenharmony_ci# NB: this error is used to seed the 404 failure for the survive-404 tests
141da0c48c4Sopenharmony_ci
142da0c48c4Sopenharmony_ci# Confirm bad artifact types are rejected without leaving trace
143da0c48c4Sopenharmony_cicurl -s http://127.0.0.1:$PORT2/buildid/deadbeef/badtype > /dev/null || true
144da0c48c4Sopenharmony_ci(curl -s http://127.0.0.1:$PORT2/metrics | grep 'badtype') && false
145da0c48c4Sopenharmony_ci
146da0c48c4Sopenharmony_ci# Confirm that reused curl connections survive 404 errors.
147da0c48c4Sopenharmony_ci# The rm's force an uncached fetch (in both servers and client cache)
148da0c48c4Sopenharmony_cirm -f .client_cache*/$BUILDID/debuginfo
149da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
150da0c48c4Sopenharmony_cirm -f .client_cache*/$BUILDID/debuginfo
151da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
152da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
153da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
154da0c48c4Sopenharmony_cirm -f .client_cache*/$BUILDID/debuginfo
155da0c48c4Sopenharmony_citestrun ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID
156da0c48c4Sopenharmony_ci# Trigger a flood of requests against the same archive content file.
157da0c48c4Sopenharmony_ci# Use a file that hasn't been previously extracted in to make it
158da0c48c4Sopenharmony_ci# likely that even this test debuginfod will experience concurrency
159da0c48c4Sopenharmony_ci# and impose some "after-you" delays.
160da0c48c4Sopenharmony_ci(for i in `seq 100`; do
161da0c48c4Sopenharmony_ci    curl -s http://127.0.0.1:$PORT1/buildid/87c08d12c78174f1082b7c888b3238219b0eb265/executable >/dev/null &
162da0c48c4Sopenharmony_ci done;
163da0c48c4Sopenharmony_ci wait)
164da0c48c4Sopenharmony_cicurl -s http://127.0.0.1:$PORT1/metrics | grep 'http_responses_after_you.*'
165da0c48c4Sopenharmony_ci# If we could guarantee some minimum number of seconds of CPU time, we
166da0c48c4Sopenharmony_ci# could assert that the after_you metrics show some nonzero amount of
167da0c48c4Sopenharmony_ci# waiting.  A few hundred ms is typical on this developer's workstation.
168da0c48c4Sopenharmony_ci
169da0c48c4Sopenharmony_ci########################################################################
170da0c48c4Sopenharmony_ci# Trigger some some random activity, then trigger a clean shutdown.
171da0c48c4Sopenharmony_ci# We used to try to corrupt the database while the debuginfod server
172da0c48c4Sopenharmony_ci# was running and check it detected errors, but that was unreliably
173da0c48c4Sopenharmony_ci# and slightly dangerous since part of the database was already mapped
174da0c48c4Sopenharmony_ci# into memory.
175da0c48c4Sopenharmony_cikill -USR1 $PID1
176da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_total{role="traverse"}' 2
177da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_pending{role="scan"}' 0
178da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_busy{role="scan"}' 0
179da0c48c4Sopenharmony_cikill -USR2 $PID1
180da0c48c4Sopenharmony_ciwait_ready $PORT1 'thread_work_total{role="groom"}' 2
181da0c48c4Sopenharmony_cicurl -s http://127.0.0.1:$PORT1/buildid/beefbeefbeefd00dd00d/debuginfo > /dev/null || true
182da0c48c4Sopenharmony_ci
183da0c48c4Sopenharmony_cikill -INT $PID1 $PID2
184da0c48c4Sopenharmony_ciwait $PID1 $PID2
185da0c48c4Sopenharmony_ciPID1=0
186da0c48c4Sopenharmony_ciPID2=0
187da0c48c4Sopenharmony_ci
188da0c48c4Sopenharmony_ci# Run the tests again without the servers running. The target file should
189da0c48c4Sopenharmony_ci# be found in the cache.
190da0c48c4Sopenharmony_ci
191da0c48c4Sopenharmony_citempfiles .debuginfod_*
192da0c48c4Sopenharmony_ci
193da0c48c4Sopenharmony_citestrun ${abs_builddir}/debuginfod_build_id_find -e F/prog 1
194da0c48c4Sopenharmony_ci
195da0c48c4Sopenharmony_ci# check out the debuginfod logs for the new style status lines
196da0c48c4Sopenharmony_cicat vlog$PORT2
197da0c48c4Sopenharmony_cigrep -q 'UA:.*XFF:.*GET /buildid/.* 200 ' vlog$PORT2
198da0c48c4Sopenharmony_cigrep -q 'UA:.*XFF:.*GET /metrics 200 ' vlog$PORT2
199da0c48c4Sopenharmony_cigrep -q 'UA:.*XFF:.*GET /badapi 503 ' vlog$PORT2
200da0c48c4Sopenharmony_cigrep -q 'UA:.*XFF:.*GET /buildid/deadbeef.* 404 ' vlog$PORT2
201da0c48c4Sopenharmony_ci
202da0c48c4Sopenharmony_ciexit 0
203