summaryrefslogtreecommitdiff
path: root/tests/run-debuginfod-find.sh
blob: 37385a0bd924288ec76250b3fa56f805ce91dd51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright (C) 2019 Red Hat, Inc.
# This file is part of elfutils.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# elfutils is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. $srcdir/test-subr.sh
testfiles testfile-debuginfod-0.rpm testfile-debuginfod-1.rpm testfile-debuginfod-2.rpm

EXPECT_FAIL=0
EXPECT_PASS=1
DB=${PWD}/.debuginfod_tmp.sqlite
export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache

# find an unused port number
while true; do
    PORT1=`expr '(' $RANDOM % 1000 ')' + 9000`
    ss -atn | fgrep ":$PORT1" || break
done    

# We want to run debuginfod in the background.  We also want to start
# it with the same check/installcheck-sensitive LD_LIBRARY_PATH stuff
# that the testrun alias sets.  But: we if we just use
#    testrun .../debuginfod
# it runs in a subshell, with different pid, so not helpful.
#
# So we gather the LD_LIBRARY_PATH with this cunning trick:

ldpath=`testrun sh -c 'echo $LD_LIBRARY_PATH'`

# Compile a simple program, strip its debuginfo and save the build-id.
# Also move the debuginfo into another directory so that elfutils
# cannot find it without debuginfod.
echo "int main() { return 0; }" > ${PWD}/prog.c
gcc -g -o prog ${PWD}/prog.c
 ${abs_top_builddir}/src/strip -g -f prog.debug ${PWD}/prog
BUILDID=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
          -a prog | grep 'Build ID' | cut -d ' ' -f 7`

mkdir debug exec src
mv prog exec/
mv prog.debug debug/
mv testfile-debuginfod-0.rpm debug/

# hardcode the buildid of an executable from testfile-debuginfod-0.rpm
BUILDID_RPM0=43a3c5fa7e4a7469b2cf3c25d316ea898d9857fd

env LD_LIBRARY_PATH=$ldpath DEBUGINFOD_URLS= ${abs_builddir}/../debuginfod/debuginfod -vvv -d $DB \
-p $PORT1 -t 2 ./debug ./exec ./src &
PID1=$!
sleep 3

export DEBUGINFOD_URLS=http://localhost:$PORT1/   # or without trailing /

# Test whether the elfutils client is able to fetch the file from the local debuginfod.
testrun ${abs_builddir}/debuginfod_build_id_find -e exec/prog $EXPECT_PASS

# Test whether debuginfod-find is able to fetch files from the local debuginfod's rpm and
# directory search paths.
rm -rf $DEBUGINFOD_CACHE_PATH
testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID}/debuginfo
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID}/executable
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID ${PWD}/prog.c <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID}/$(echo source${PWD}/prog.c | sed -e 's\[/.]\#\g')
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID_RPM0 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM0}/executable
EOF


# Add artifacts to the search paths and test whether debuginfod finds them while already running.
mv testfile-debuginfod-1.rpm exec/
mv testfile-debuginfod-2.rpm src/
BUILDID_RPM1=d2db118e196b604976f95277ea64eb710c1486d0
SRC_PATH_RPM2=/usr/src/debug/elfutils-0.177-1.fc30.x86_64/src/readelf.c

echo "int main() { return 0; }" > ${PWD}/prog2.c
gcc -g -o prog2 ${PWD}/prog2.c
 ${abs_top_builddir}/src/strip -g -f prog2.debug prog2
BUILDID2=`env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../src/readelf \
          -a prog2 | grep 'Build ID' | cut -d ' ' -f 7`

mv prog2 exec/
mv prog2.debug debug/

tempfiles prog*
sleep 3

testrun ${abs_builddir}/debuginfod_build_id_find -e exec/prog2 $EXPECT_PASS

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID2 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID2}/executable
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID2 ${PWD}/prog2.c <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID2}/$(echo source${PWD}/prog2.c | sed -e 's\[/.]\#\g')
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID_RPM1 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM1}/debuginfo
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID_RPM1 $SRC_PATH_RPM2  <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM1}/$(echo source${SRC_PATH_RPM2} | sed -e 's\[/.]\#\g')
EOF

# find another unused port
while true; do
    PORT2=`expr '(' $RANDOM % 1000 ')' + 9000`
    ss -atn | fgrep ":$PORT2" || break
done

# start another server and test federation
export DEBUGINFOD_CACHE_PATH=${PWD}/.client_cache2
env LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../debuginfod/debuginfod -vvv -d ${DB}_2 -p $PORT2 &
PID2=$!
sleep 3

# have clients contact the new server
export DEBUGINFOD_URLS=http://localhost:$PORT2

# test whether the new server fetches files from the original.
testrun ${abs_builddir}/debuginfod_build_id_find -e exec/prog $EXPECT_PASS

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID2 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID2}/debuginfo
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID_RPM1 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM1}/debuginfo
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID}/executable
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID_RPM0 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM0}/executable
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID2 ${PWD}/prog2.c <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID2}/$(echo source${PWD}/prog2.c | sed -e 's\[/.]\#\g')
EOF

# test parallel queries.
rm -rf ${PWD}/.client_cache*
export DEBUGINFOD_URLS="http://localhost:$PORT1 localhost:$PORT1 http://localhost:$PORT2 DNE"

testrun ${abs_builddir}/debuginfod_build_id_find -e exec/prog $EXPECT_PASS

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID2 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID2}/debuginfo
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID_RPM1 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM1}/debuginfo
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID}/executable
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID_RPM0 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM0}/executable
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID2 ${PWD}/prog2.c <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID2}/$(echo source${PWD}/prog2.c | sed -e 's\[/.]\#\g')
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID_RPM1 $SRC_PATH_RPM2 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM1}/$(echo source${SRC_PATH_RPM2} | sed -e 's\[/.]\#\g')
EOF

# Run the tests again without the servers running. The target file should
# be found in the cache.
kill -INT $PID1 $PID2
sleep 5
tempfiles .debuginfod_*

testrun ${abs_builddir}/debuginfod_build_id_find -e exec/prog $EXPECT_PASS

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID2 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID2}/debuginfo
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID_RPM1 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM1}/debuginfo
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID}/executable
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID_RPM0 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM0}/executable
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID2 ${PWD}/prog2.c <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID2}/$(echo source${PWD}/prog2.c | sed -e 's\[/.]\#\g')
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID_RPM1 $SRC_PATH_RPM2 <<EOF
${DEBUGINFOD_CACHE_PATH}/${BUILDID_RPM1}/$(echo source${SRC_PATH_RPM2} | sed -e 's\[/.]\#\g')
EOF

# Trigger a cache clean and run the tests again. The clients should be unable to
# find the target.
echo 0 > $DEBUGINFOD_CACHE_PATH/cache_clean_interval_s
echo 0 > $DEBUGINFOD_CACHE_PATH/max_unused_age_s

testrun ${abs_builddir}/debuginfod_build_id_find -e exec/prog $EXPECT_FAIL

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID2 <<EOF
Server query failed: No route to host
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find debuginfo $BUILDID_RPM1 <<EOF
Server query failed: No route to host
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID <<EOF
Server query failed: No route to host
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find executable $BUILDID_RPM0 <<EOF
Server query failed: No route to host
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID2 ${PWD}/prog.c <<EOF
Server query failed: No route to host
EOF

testrun_compare ${abs_top_builddir}/debuginfod/debuginfod-find source $BUILDID_RPM1 $SRC_PATH_RPM2  <<EOF
Server query failed: No route to host
EOF

# Ensure debuginfod-find can be safely called with no arguments.
# Use a relative path to prevent automatic line breaks in the output
# due to excessive characters.
testrun_compare ../../debuginfod/debuginfod-find <<EOF
Usage: ../../debuginfod/debuginfod-find debuginfo BUILDID
  or:  ../../debuginfod/debuginfod-find executable BUILDID
  or:  ../../debuginfod/debuginfod-find source BUILDID /FILENAME
EOF

rm -rf debug exec src ${PWD}/.client_cache*
exit 0