xref: /aosp_15_r20/external/elfutils/tests/run-srcfiles-self.sh (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker#! /usr/bin/env bash
2*7304104dSAndroid Build Coastguard Worker# Copyright (C) 2023 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker# This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker#
5*7304104dSAndroid Build Coastguard Worker# This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker# it under the terms of the GNU General Public License as published by
7*7304104dSAndroid Build Coastguard Worker# the Free Software Foundation; either version 3 of the License, or
8*7304104dSAndroid Build Coastguard Worker# (at your option) any later version.
9*7304104dSAndroid Build Coastguard Worker#
10*7304104dSAndroid Build Coastguard Worker# elfutils is distributed in the hope that it will be useful, but
11*7304104dSAndroid Build Coastguard Worker# WITHOUT ANY WARRANTY; without even the implied warranty of
12*7304104dSAndroid Build Coastguard Worker# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*7304104dSAndroid Build Coastguard Worker# GNU General Public License for more details.
14*7304104dSAndroid Build Coastguard Worker#
15*7304104dSAndroid Build Coastguard Worker# You should have received a copy of the GNU General Public License
16*7304104dSAndroid Build Coastguard Worker# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*7304104dSAndroid Build Coastguard Worker
18*7304104dSAndroid Build Coastguard Worker. $srcdir/debuginfod-subr.sh
19*7304104dSAndroid Build Coastguard Worker
20*7304104dSAndroid Build Coastguard Worker# for test case debugging, uncomment:
21*7304104dSAndroid Build Coastguard Worker# set -x
22*7304104dSAndroid Build Coastguard Workerset -e
23*7304104dSAndroid Build Coastguard Workerbase=14000
24*7304104dSAndroid Build Coastguard Workerget_ports
25*7304104dSAndroid Build Coastguard Worker
26*7304104dSAndroid Build Coastguard Worker# Test different command line combinations on the srcfiles binary itself.
27*7304104dSAndroid Build Coastguard WorkerET_EXEC="${abs_top_builddir}/src/srcfiles"
28*7304104dSAndroid Build Coastguard WorkerET_PID=$$
29*7304104dSAndroid Build Coastguard Worker
30*7304104dSAndroid Build Coastguard WorkerSRC_NAME="srcfiles.cxx"
31*7304104dSAndroid Build Coastguard Worker
32*7304104dSAndroid Build Coastguard Worker# Ensure the output contains the expected source file srcfiles.cxx
33*7304104dSAndroid Build Coastguard Workertestrun $ET_EXEC -e $ET_EXEC | grep $SRC_NAME > /dev/null
34*7304104dSAndroid Build Coastguard Worker
35*7304104dSAndroid Build Coastguard Worker# Check if zip option is available (only available if libarchive is available.
36*7304104dSAndroid Build Coastguard Worker#   Debuginfod optional to fetch source files from debuginfod federation.)
37*7304104dSAndroid Build Coastguard Worker$ET_EXEC --help | grep -q zip && command -v unzip >/dev/null 2>&1 && zip=true || zip=false
38*7304104dSAndroid Build Coastguard Worker
39*7304104dSAndroid Build Coastguard Workerfor null_arg in --null ""; do
40*7304104dSAndroid Build Coastguard Worker  for verbose_arg in --verbose ""; do
41*7304104dSAndroid Build Coastguard Worker    echo "Test with options $null_arg $verbose_arg"
42*7304104dSAndroid Build Coastguard Worker    testrun $ET_EXEC $null_arg $verbose_arg -p $ET_PID > /dev/null
43*7304104dSAndroid Build Coastguard Worker
44*7304104dSAndroid Build Coastguard Worker    # Ensure that the output contains srcfiles.cxx
45*7304104dSAndroid Build Coastguard Worker    cu_only=$(testrun $ET_EXEC $null_arg $verbose_arg -c -e $ET_EXEC)
46*7304104dSAndroid Build Coastguard Worker    default=$(testrun $ET_EXEC $null_arg $verbose_arg -e $ET_EXEC)
47*7304104dSAndroid Build Coastguard Worker    result1=$(echo "$cu_only" | grep "$SRC_NAME")
48*7304104dSAndroid Build Coastguard Worker    result2=$(echo "$default" | grep "$SRC_NAME")
49*7304104dSAndroid Build Coastguard Worker
50*7304104dSAndroid Build Coastguard Worker    if [ -z "$result1" ] || [ -z "$result2" ]; then
51*7304104dSAndroid Build Coastguard Worker      exit 1
52*7304104dSAndroid Build Coastguard Worker    fi
53*7304104dSAndroid Build Coastguard Worker
54*7304104dSAndroid Build Coastguard Worker    # Ensure that the output with the cu-only option contains fewer source files
55*7304104dSAndroid Build Coastguard Worker    if [ $(echo "$cu_only" | wc -m) -gt $(echo "$default" | wc -m) ]; then
56*7304104dSAndroid Build Coastguard Worker      exit 1
57*7304104dSAndroid Build Coastguard Worker    fi
58*7304104dSAndroid Build Coastguard Worker
59*7304104dSAndroid Build Coastguard Worker    if $zip; then
60*7304104dSAndroid Build Coastguard Worker      # Zip option tests
61*7304104dSAndroid Build Coastguard Worker        testrun $ET_EXEC $verbose_arg -z -e $ET_EXEC > test.zip
62*7304104dSAndroid Build Coastguard Worker        tempfiles test.zip
63*7304104dSAndroid Build Coastguard Worker
64*7304104dSAndroid Build Coastguard Worker        unzip -v test.zip
65*7304104dSAndroid Build Coastguard Worker        unzip -t test.zip
66*7304104dSAndroid Build Coastguard Worker
67*7304104dSAndroid Build Coastguard Worker        # Ensure unzipped srcfiles.cxx and its contents are the same as the original source file
68*7304104dSAndroid Build Coastguard Worker        unzip -j test.zip "*/$SRC_NAME"
69*7304104dSAndroid Build Coastguard Worker        diff "$SRC_NAME" $abs_srcdir/../src/$SRC_NAME
70*7304104dSAndroid Build Coastguard Worker        rm -f test.zip $SRC_NAME
71*7304104dSAndroid Build Coastguard Worker    fi
72*7304104dSAndroid Build Coastguard Worker  done
73*7304104dSAndroid Build Coastguard Workerdone
74*7304104dSAndroid Build Coastguard Worker
75*7304104dSAndroid Build Coastguard Worker# Debuginfod source file downloading test.
76*7304104dSAndroid Build Coastguard Worker# Start debuginfod server on the elfutils build directory.
77*7304104dSAndroid Build Coastguard Workerif [ -x ${abs_builddir}/../debuginfod/debuginfod ] && $zip; then
78*7304104dSAndroid Build Coastguard Worker  LD_LIBRARY_PATH=$ldpath ${abs_builddir}/../debuginfod/debuginfod -vvvv -d debuginfod.sqlite3 -F -p $PORT1 ${abs_top_builddir}/src > debuginfod.log 2>&1 &
79*7304104dSAndroid Build Coastguard Worker  PID1=$!
80*7304104dSAndroid Build Coastguard Worker  tempfiles debuginfod.sqlite3 debuginfod.log
81*7304104dSAndroid Build Coastguard Worker  wait_ready $PORT1 'ready' 1
82*7304104dSAndroid Build Coastguard Worker  wait_ready $PORT1 'thread_work_total{role="traverse"}' 1
83*7304104dSAndroid Build Coastguard Worker  wait_ready $PORT1 'thread_work_pending{role="scan"}' 0
84*7304104dSAndroid Build Coastguard Worker  wait_ready4 $PORT1 'thread_busy{role="scan"}' 0 300 # lots of source files may be slow to index with $db on NFS
85*7304104dSAndroid Build Coastguard Worker
86*7304104dSAndroid Build Coastguard Worker  export DEBUGINFOD_URLS="http://localhost:${PORT1}/"
87*7304104dSAndroid Build Coastguard Worker  export DEBUGINFOD_VERBOSE=1
88*7304104dSAndroid Build Coastguard Worker  testrun $ET_EXEC -z -b -e $ET_EXEC > test.zip
89*7304104dSAndroid Build Coastguard Worker  tempfiles test.zip
90*7304104dSAndroid Build Coastguard Worker
91*7304104dSAndroid Build Coastguard Worker  unzip -v test.zip
92*7304104dSAndroid Build Coastguard Worker  unzip -t test.zip
93*7304104dSAndroid Build Coastguard Worker
94*7304104dSAndroid Build Coastguard Worker  # Extract the zip.
95*7304104dSAndroid Build Coastguard Worker  mkdir extracted
96*7304104dSAndroid Build Coastguard Worker  unzip test.zip -d extracted
97*7304104dSAndroid Build Coastguard Worker
98*7304104dSAndroid Build Coastguard Worker  # Ensure that source files for this tool have been archived.
99*7304104dSAndroid Build Coastguard Worker  source_files="srcfiles.cxx libdwfl.h gelf.h"
100*7304104dSAndroid Build Coastguard Worker  extracted_files=$(find extracted -type f)
101*7304104dSAndroid Build Coastguard Worker  for file in $source_files; do
102*7304104dSAndroid Build Coastguard Worker      echo "$extracted_files" | grep -q "$file" > /dev/null
103*7304104dSAndroid Build Coastguard Worker  done
104*7304104dSAndroid Build Coastguard Worker
105*7304104dSAndroid Build Coastguard Worker  # Compare between the extracted file and the actual source file srcfiles.cxx.
106*7304104dSAndroid Build Coastguard Worker  extracted_file=$(find extracted -name $SRC_NAME)
107*7304104dSAndroid Build Coastguard Worker  diff "$extracted_file" $abs_srcdir/../src/$SRC_NAME
108*7304104dSAndroid Build Coastguard Worker
109*7304104dSAndroid Build Coastguard Worker  rm -rf extracted
110*7304104dSAndroid Build Coastguard Worker
111*7304104dSAndroid Build Coastguard Worker  kill $PID1
112*7304104dSAndroid Build Coastguard Worker  wait
113*7304104dSAndroid Build Coastguard Worker  PID1=0
114*7304104dSAndroid Build Coastguard Workerfi
115