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