# rpmbuildid.at: test rpmbuild buildid symlink support
#
# This file is part of RPM, the RPM Package Manager.
# Copyright (C) 2016 Mark J. Wielaard <mjw@redhat.com>
#
# 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 2 of the License, or
# (at your option) any later version.
#
# RPM 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/>.

AT_BANNER([RPM buildid tests])

# ------------------------------
# Check if rpmbuild "none" doesn't generates buildid symlinks for hello program
AT_SETUP([rpmbuild buildid none])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT

# Build, contains one ELF which should have a buildid.
rundebug rpmbuild \
  --define="_build_id_links none" \
  --quiet -ba "${abs_srcdir}"/data/SPECS/hello.spec

# There should be zero build-id files in both the main and debuginfo package
echo -n "hello build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

echo -n "hello debuginfo build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

],
[0],
[hello build-id files: 0
hello debuginfo build-id files: 0
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check if rpmbuild "alldebug" generates debuginfo buildid symlinks.
# Without unique debug file names.
AT_SETUP([rpmbuild buildid alldebug])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT

# Build, contains one ELF which should have a buildid.
rundebug rpmbuild \
  --define="_build_id_links alldebug" \
  --undefine "_unique_debug_names" \
  --undefine "_unique_debug_srcs" \
  --quiet -ba "${abs_srcdir}"/data/SPECS/hello.spec

# There should be zero build-id files in the main package
# Main and debug should be in the debuginfo package,
# plus the .build-id/xx subdir, 3 in total.
echo -n "hello build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

echo -n "hello debuginfo build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

# Extract the both packages to check the build-id files link to the
# main and .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | cpio -diu

# Check there is a build-id symlink for the main file.
main_file=./usr/local/bin/hello
test -f "${main_file}" || echo "No main file ${main_file}"

# Extract the build-id from the main file
id_main=$(file $main_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

id_main_file="./usr/lib/debug/.build-id/${id_main:0:2}/${id_main:2}"
test -L "$id_main_file" || echo "No build-id file $id_main_file"

canon_main_file=$(readlink -f ${main_file})

test -f "$canon_main_file" \
  || echo "Cannot resolve main file ${main_file} -> ${canon_main_file}"

canon_main_id_file=$(readlink -f ${id_main_file})

test -f "$canon_main_id_file" \
  || echo "Cannot resolve main build-id file ${id_main_file} -> ${canon_main_id_file}"

test "$canon_main_file" = "$canon_main_id_file" \
  || echo "main and build-id file not linked"

# And check the same for the debug file.
debug_file=./usr/lib/debug/usr/local/bin/hello.debug
test -f ${debug_file} || echo "No debug file ${debug_file}"

# Extract the build-id from the .debug file
id_debug=$(file $debug_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

test ${id_main} = ${id_debug} || echo "unequal main and debug id"

id_debug_file="./usr/lib/debug/.build-id/${id_debug:0:2}/${id_debug:2}.debug"
test -L "$id_debug_file" || echo "No build-id file $id_debug_file"

canon_debug_file=$(readlink -f ${debug_file})

test -f "$canon_debug_file" \
  || echo "Cannot resolve debug file ${debug_file} -> ${canon_debug_file}"

canon_debug_id_file=$(readlink -f ${id_debug_file})

test -f "$canon_debug_id_file" \
  || echo "Cannot resolve debug build-id file ${id_debug_file} -> ${canon_debug_id_file}"

test "$canon_debug_file" = "$canon_debug_id_file" \
  || echo "debug and build-id not linked"
],
[0],
[hello build-id files: 0
hello debuginfo build-id files: 3
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check if rpmbuild "alldebug" generates debuginfo buildid symlinks.
# With unique debug file names.
AT_SETUP([rpmbuild buildid alldebug unique debug names])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT

# Build, contains one ELF which should have a buildid.
rundebug rpmbuild \
  --define="_build_id_links alldebug" \
  --define="_unique_debug_names 1" \
  --quiet -ba "${abs_srcdir}"/data/SPECS/hello.spec

# There should be zero build-id files in the main package
# Main and debug should be in the debuginfo package,
# plus the .build-id/xx subdir, 3 in total.
echo -n "hello build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

echo -n "hello debuginfo build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

# Extract the both packages to check the build-id files link to the
# main and .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | cpio -diu

# Check there is a build-id symlink for the main file.
main_file=./usr/local/bin/hello
test -f "${main_file}" || echo "No main file ${main_file}"

# Extract the build-id from the main file
id_main=$(file $main_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

id_main_file="./usr/lib/debug/.build-id/${id_main:0:2}/${id_main:2}"
test -L "$id_main_file" || echo "No build-id file $id_main_file"

canon_main_file=$(readlink -f ${main_file})

test -f "$canon_main_file" \
  || echo "Cannot resolve main file ${main_file} -> ${canon_main_file}"

canon_main_id_file=$(readlink -f ${id_main_file})

test -f "$canon_main_id_file" \
  || echo "Cannot resolve main build-id file ${id_main_file} -> ${canon_main_id_file}"

test "$canon_main_file" = "$canon_main_id_file" \
  || echo "main and build-id file not linked"

# And check the same for the debug file.
debug_file=./usr/lib/debug/usr/local/bin/hello-*.debug
test -f ${debug_file} || echo "No debug file ${debug_file}"

# Extract the build-id from the .debug file
id_debug=$(file $debug_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

test ${id_main} = ${id_debug} || echo "unequal main and debug id"

id_debug_file="./usr/lib/debug/.build-id/${id_debug:0:2}/${id_debug:2}.debug"
test -L "$id_debug_file" || echo "No build-id file $id_debug_file"

canon_debug_file=$(readlink -f ${debug_file})

test -f "$canon_debug_file" \
  || echo "Cannot resolve debug file ${debug_file} -> ${canon_debug_file}"

canon_debug_id_file=$(readlink -f ${id_debug_file})

test -f "$canon_debug_id_file" \
  || echo "Cannot resolve debug build-id file ${id_debug_file} -> ${canon_debug_id_file}"

test "$canon_debug_file" = "$canon_debug_id_file" \
  || echo "debug and build-id not linked"
],
[0],
[hello build-id files: 0
hello debuginfo build-id files: 3
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check if rpmbuild "separate" generates main and debuginfo buildid symlinks
# Without unique debug file names
AT_SETUP([rpmbuild buildid separate])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT

# Build, contains one ELF which should have a buildid.
rundebug rpmbuild \
  --define="_build_id_links separate" \
  --undefine "_unique_debug_names" \
  --undefine "_unique_debug_srcs" \
  --quiet -ba "${abs_srcdir}"/data/SPECS/hello.spec

# There should be one build-id files in the main and debuginfo package
# plus the .build-id/xx subdir, 2 in total.
echo -n "hello build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

echo -n "hello debuginfo build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

# Extract the both packages to check the build-id files link to the
# main and .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | cpio -diu

# Check there is a build-id symlink for the main file.
main_file=./usr/local/bin/hello
test -f "${main_file}" || echo "No main file ${main_file}"

# Extract the build-id from the main file
id_main=$(file $main_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

id_main_file="./usr/lib/.build-id/${id_main:0:2}/${id_main:2}"
test -L "$id_main_file" || echo "No build-id file $id_main_file"

canon_main_file=$(readlink -f ${main_file})

test -f "$canon_main_file" \
  || echo "Cannot resolve main file ${main_file} -> ${canon_main_file}"

canon_main_id_file=$(readlink -f ${id_main_file})

test -f "$canon_main_id_file" \
  || echo "Cannot resolve main build-id file ${id_main_file} -> ${canon_main_id_file}"

test "$canon_main_file" = "$canon_main_id_file" \
  || echo "main and build-id file not linked"

# And check the same for the debug file.
debug_file=./usr/lib/debug/usr/local/bin/hello.debug
test -f ${debug_file} || echo "No debug file ${debug_file}"

# Extract the build-id from the .debug file
id_debug=$(file $debug_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

test ${id_main} = ${id_debug} || echo "unequal main and debug id"

id_debug_file="./usr/lib/debug/.build-id/${id_debug:0:2}/${id_debug:2}.debug"
test -L "$id_debug_file" || echo "No build-id file $id_debug_file"

canon_debug_file=$(readlink -f ${debug_file})

test -f "$canon_debug_file" \
  || echo "Cannot resolve debug file ${debug_file} -> ${canon_debug_file}"

canon_debug_id_file=$(readlink -f ${id_debug_file})

test -f "$canon_debug_id_file" \
  || echo "Cannot resolve debug build-id file ${id_debug_file} -> ${canon_debug_id_file}"

test "$canon_debug_file" = "$canon_debug_id_file" \
  || echo "debug and build-id not linked"
],
[0],
[hello build-id files: 2
hello debuginfo build-id files: 2
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check if rpmbuild "separate" generates main and debuginfo buildid symlinks
# With unique debug file names
AT_SETUP([rpmbuild buildid separate unique debug names])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT

# Build, contains one ELF which should have a buildid.
rundebug rpmbuild \
  --define="_build_id_links separate" \
  --define="_unique_debug_names 1" \
  --quiet -ba "${abs_srcdir}"/data/SPECS/hello.spec

# There should be one build-id files in the main and debuginfo package
# plus the .build-id/xx subdir, 2 in total.
echo -n "hello build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

echo -n "hello debuginfo build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

# Extract the both packages to check the build-id files link to the
# main and .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | cpio -diu

# Check there is a build-id symlink for the main file.
main_file=./usr/local/bin/hello
test -f "${main_file}" || echo "No main file ${main_file}"

# Extract the build-id from the main file
id_main=$(file $main_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

id_main_file="./usr/lib/.build-id/${id_main:0:2}/${id_main:2}"
test -L "$id_main_file" || echo "No build-id file $id_main_file"

canon_main_file=$(readlink -f ${main_file})

test -f "$canon_main_file" \
  || echo "Cannot resolve main file ${main_file} -> ${canon_main_file}"

canon_main_id_file=$(readlink -f ${id_main_file})

test -f "$canon_main_id_file" \
  || echo "Cannot resolve main build-id file ${id_main_file} -> ${canon_main_id_file}"

test "$canon_main_file" = "$canon_main_id_file" \
  || echo "main and build-id file not linked"

# And check the same for the debug file.
debug_file=./usr/lib/debug/usr/local/bin/hello-*.debug
test -f ${debug_file} || echo "No debug file ${debug_file}"

# Extract the build-id from the .debug file
id_debug=$(file $debug_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

test ${id_main} = ${id_debug} || echo "unequal main and debug id"

id_debug_file="./usr/lib/debug/.build-id/${id_debug:0:2}/${id_debug:2}.debug"
test -L "$id_debug_file" || echo "No build-id file $id_debug_file"

canon_debug_file=$(readlink -f ${debug_file})

test -f "$canon_debug_file" \
  || echo "Cannot resolve debug file ${debug_file} -> ${canon_debug_file}"

canon_debug_id_file=$(readlink -f ${id_debug_file})

test -f "$canon_debug_id_file" \
  || echo "Cannot resolve debug build-id file ${id_debug_file} -> ${canon_debug_id_file}"

test "$canon_debug_file" = "$canon_debug_id_file" \
  || echo "debug and build-id not linked"
],
[0],
[hello build-id files: 2
hello debuginfo build-id files: 2
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check if rpmbuild "compat" generates main and debuginfo buildid symlinks
# Without unique debug file names
AT_SETUP([rpmbuild buildid compat])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT

# Build, contains one ELF which should have a buildid.
rundebug rpmbuild \
  --define="_build_id_links compat" \
  --undefine "_unique_debug_names" \
  --undefine "_unique_debug_srcs" \
  --quiet -ba "${abs_srcdir}"/data/SPECS/hello.spec

# There should be one build-id files in the main and debuginfo package.
# the debuginfo package has one extra main build-id compat symlink
# plus the .build-id/xx subdir, 2 in total in main, 3 in total in debug
echo -n "hello build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

echo -n "hello debuginfo build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

# Extract the both packages to check the build-id files link to the
# main and .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | cpio -diu

# Check there is a build-id symlink for the main file.
main_file=./usr/local/bin/hello
test -f "${main_file}" || echo "No main file ${main_file}"

# Extract the build-id from the main file
id_main=$(file $main_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

id_main_file="./usr/lib/.build-id/${id_main:0:2}/${id_main:2}"
test -L "$id_main_file" || echo "No build-id file $id_main_file"

canon_main_file=$(readlink -f ${main_file})

test -f "$canon_main_file" \
  || echo "Cannot resolve main file ${main_file} -> ${canon_main_file}"

canon_main_id_file=$(readlink -f ${id_main_file})

test -f "$canon_main_id_file" \
  || echo "Cannot resolve main build-id file ${id_main_file} -> ${canon_main_id_file}"

test "$canon_main_file" = "$canon_main_id_file" \
  || echo "main and build-id file not linked"

# And check the same for the debug file.
debug_file=./usr/lib/debug/usr/local/bin/hello.debug
test -f ${debug_file} || echo "No debug file ${debug_file}"

# Extract the build-id from the .debug file
id_debug=$(file $debug_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

test ${id_main} = ${id_debug} || echo "unequal main and debug id"

id_debug_file="./usr/lib/debug/.build-id/${id_debug:0:2}/${id_debug:2}.debug"
test -L "$id_debug_file" || echo "No build-id file $id_debug_file"

canon_debug_file=$(readlink -f ${debug_file})

test -f "$canon_debug_file" \
  || echo "Cannot resolve debug file ${debug_file} -> ${canon_debug_file}"

canon_debug_id_file=$(readlink -f ${id_debug_file})

test -f "$canon_debug_id_file" \
  || echo "Cannot resolve debug build-id file ${id_debug_file} -> ${canon_debug_id_file}"

test "$canon_debug_file" = "$canon_debug_id_file" \
  || echo "debug and build-id not linked"

# The compat link should also point to the same (indirectly).
id_compat_file="./usr/lib/debug/.build-id/${id_main:0:2}/${id_main:2}"
test -L "$id_compat_file" || echo "No build-id compat file $id_compat_file"

canon_compat_file=$(readlink -f ${id_compat_file})

test -f "$canon_compat_file" \
  || echo "Cannot resolve compat file ${id_compat_file} -> ${canon_compat_file}"

test "$canon_compat_file" = "$canon_main_file" \
  || echo "compat and build-id not linked"
],
[0],
[hello build-id files: 2
hello debuginfo build-id files: 3
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check if rpmbuild "compat" generates main and debuginfo buildid symlinks
# With unique debug file names
AT_SETUP([rpmbuild buildid compat unique debug names])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT

# Build, contains one ELF which should have a buildid.
rundebug rpmbuild \
  --define="_build_id_links compat" \
  --define="_unique_debug_names 1" \
  --quiet -ba "${abs_srcdir}"/data/SPECS/hello.spec

# There should be one build-id files in the main and debuginfo package.
# the debuginfo package has one extra main build-id compat symlink
# plus the .build-id/xx subdir, 2 in total in main, 3 in total in debug
echo -n "hello build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

echo -n "hello debuginfo build-id files: "
runroot rpm -ql -p /build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | grep /.build-id/ | wc --lines

# Extract the both packages to check the build-id files link to the
# main and .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | cpio -diu

# Check there is a build-id symlink for the main file.
main_file=./usr/local/bin/hello
test -f "${main_file}" || echo "No main file ${main_file}"

# Extract the build-id from the main file
id_main=$(file $main_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

id_main_file="./usr/lib/.build-id/${id_main:0:2}/${id_main:2}"
test -L "$id_main_file" || echo "No build-id file $id_main_file"

canon_main_file=$(readlink -f ${main_file})

test -f "$canon_main_file" \
  || echo "Cannot resolve main file ${main_file} -> ${canon_main_file}"

canon_main_id_file=$(readlink -f ${id_main_file})

test -f "$canon_main_id_file" \
  || echo "Cannot resolve main build-id file ${id_main_file} -> ${canon_main_id_file}"

test "$canon_main_file" = "$canon_main_id_file" \
  || echo "main and build-id file not linked"

# And check the same for the debug file.
debug_file=./usr/lib/debug/usr/local/bin/hello-*debug
test -f ${debug_file} || echo "No debug file ${debug_file}"

# Extract the build-id from the .debug file
id_debug=$(file $debug_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

test ${id_main} = ${id_debug} || echo "unequal main and debug id"

id_debug_file="./usr/lib/debug/.build-id/${id_debug:0:2}/${id_debug:2}.debug"
test -L "$id_debug_file" || echo "No build-id file $id_debug_file"

canon_debug_file=$(readlink -f ${debug_file})

test -f "$canon_debug_file" \
  || echo "Cannot resolve debug file ${debug_file} -> ${canon_debug_file}"

canon_debug_id_file=$(readlink -f ${id_debug_file})

test -f "$canon_debug_id_file" \
  || echo "Cannot resolve debug build-id file ${id_debug_file} -> ${canon_debug_id_file}"

test "$canon_debug_file" = "$canon_debug_id_file" \
  || echo "debug and build-id not linked"

# The compat link should also point to the same (indirectly).
id_compat_file="./usr/lib/debug/.build-id/${id_main:0:2}/${id_main:2}"
test -L "$id_compat_file" || echo "No build-id compat file $id_compat_file"

canon_compat_file=$(readlink -f ${id_compat_file})

test -f "$canon_compat_file" \
  || echo "Cannot resolve compat file ${id_compat_file} -> ${canon_compat_file}"

test "$canon_compat_file" = "$canon_main_file" \
  || echo "compat and build-id not linked"
],
[0],
[hello build-id files: 2
hello debuginfo build-id files: 3
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check that (copied) files with duplicate build-ids are handled correctly.
# This should create "numbered" build-id files.
# This is simply the hello example with one binary copied.
AT_SETUP([rpmbuild buildid duplicate alldebug])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
# Should create two warnings
rundebug rpmbuild --quiet \
  --define="_build_id_links alldebug" \
  -ba "${abs_srcdir}"/data/SPECS/hello2cp.spec 2>&1 | grep "^warning: " \
  | cut -f1-3 -d' '

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
id=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# alldebug not here...
id_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}"
test -L "$id_file" && echo "main id in main package"
id_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1"
test -L "$id_dup_file" && echo "main dup id in main package"

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# alldebug, so they are all here
test -L "$id_file" && echo "main id in debug package"
test -L "$id_dup_file" && echo "main dup id in debug package"

debug_id_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.debug"
test -L "$debug_id_file" && echo "debug id in debug package"
debug_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1.debug"
test -L "$debug_dup_file" && echo "debug dup id in debug package"

# We don't know which points to which, but we do know they point
# to different files.
canon_id_file=$(readlink -f ${id_file})
canon_dup_file=$(readlink -f ${id_dup_file})
test "$canon_id_file" != "$canon_dup_file" \
  || echo "id and dup same"

canon_debug_id_file=$(readlink -f ${debug_id_file})
canon_debug_dup_file=$(readlink -f ${debug_dup_file})
test "$canon_debug_id_file" != "$canon_debug_dup_file" \
  || echo "debug id and dup same"
],
[0],
[warning: Duplicate build-ids
warning: Duplicate build-ids
main id in debug package
main dup id in debug package
debug id in debug package
debug dup id in debug package
],
[])
AT_CLEANUP

# ------------------------------
# Check that hard linked files are handled correctly.
# Since the hard linked files have duplicate build-ids,
# it should create "numbered" build-id files.
# This is simply the hello example with one binary hard linked.
AT_SETUP([rpmbuild buildid hardlink alldebug])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
# No warnings for hard links
rundebug rpmbuild --quiet \
  --define="_build_id_links alldebug" \
  -ba "${abs_srcdir}"/data/SPECS/hello2ln.spec 2>&1 | grep "^warning: " \
  | cut -f1-3 -d' '

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
id=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# alldebug not here...
id_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}"
test -L "$id_file" && echo "main id in main package"
id_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1"
test -L "$id_dup_file" && echo "main dup id in main package"

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# alldebug, so they are all here
test -L "$id_file" && echo "main id in debug package"
test -L "$id_dup_file" && echo "main dup id in debug package"

debug_id_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.debug"
test -L "$debug_id_file" && echo "debug id in debug package"
debug_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1.debug"
test -L "$debug_dup_file" && echo "debug dup id in debug package"

# We don't know which points to which, but we do know they point
# to different files.
canon_id_file=$(readlink -f ${id_file})
canon_dup_file=$(readlink -f ${id_dup_file})
test "$canon_id_file" != "$canon_dup_file" \
  || echo "id and dup same"

canon_debug_id_file=$(readlink -f ${debug_id_file})
canon_debug_dup_file=$(readlink -f ${debug_dup_file})
test "$canon_debug_id_file" != "$canon_debug_dup_file" \
  || echo "debug id and dup same"
],
[0],
[main id in debug package
main dup id in debug package
debug id in debug package
debug dup id in debug package
],
[])
AT_CLEANUP

# ------------------------------
# Check that (copied) files with duplicate build-ids are handled correctly.
# This should create "numbered" build-id files.
# This is simply the hello example with one binary copied.
AT_SETUP([rpmbuild buildid duplicate separate])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
# Should create two warnings
rundebug rpmbuild --quiet \
  --define="_build_id_links separate" \
  -ba "${abs_srcdir}"/data/SPECS/hello2cp.spec 2>&1 | grep "^warning: " \
  | cut -f1-3 -d' '

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
id=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# separate build-ids split...
id_file="./usr/lib/.build-id/${id:0:2}/${id:2}"
test -L "$id_file" && echo "main id in main package"
id_dup_file="./usr/lib/.build-id/${id:0:2}/${id:2}.1"
test -L "$id_dup_file" && echo "main dup id in main package"

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# separate, so debug ids are here
debug_id_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.debug"
test -L "$debug_id_file" && echo "debug id in debug package"
debug_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1.debug"
test -L "$debug_dup_file" && echo "debug dup id in debug package"

# We don't know which points to which, but we do know they point
# to different files.
canon_id_file=$(readlink -f ${id_file})
canon_dup_file=$(readlink -f ${id_dup_file})
test "$canon_id_file" != "$canon_dup_file" \
  || echo "id and dup same"

canon_debug_id_file=$(readlink -f ${debug_id_file})
canon_debug_dup_file=$(readlink -f ${debug_dup_file})
test "$canon_debug_id_file" != "$canon_debug_dup_file" \
  || echo "debug id and dup same"
],
[0],
[warning: Duplicate build-ids
warning: Duplicate build-ids
main id in main package
main dup id in main package
debug id in debug package
debug dup id in debug package
],
[])
AT_CLEANUP

# ------------------------------
# Check that hard linked files are handled correctly.
# Since the hard linked files have duplicate build-ids,
# it should create "numbered" build-id files.
# This is simply the hello example with one binary hard linked.
AT_SETUP([rpmbuild buildid hardlink separate])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
# No warnings for hard links
rundebug rpmbuild --quiet \
  --define="_build_id_links separate" \
  -ba "${abs_srcdir}"/data/SPECS/hello2ln.spec 2>&1 | grep "^warning: " \
  | cut -f1-3 -d' '

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
id=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# separate build-ids split...
id_file="./usr/lib/.build-id/${id:0:2}/${id:2}"
test -L "$id_file" && echo "main id in main package"
id_dup_file="./usr/lib/.build-id/${id:0:2}/${id:2}.1"
test -L "$id_dup_file" && echo "main dup id in main package"

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# separate, so debug ids are here
debug_id_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.debug"
test -L "$debug_id_file" && echo "debug id in debug package"
debug_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1.debug"
test -L "$debug_dup_file" && echo "debug dup id in debug package"

# We don't know which points to which, but we do know they point
# to different files.
canon_id_file=$(readlink -f ${id_file})
canon_dup_file=$(readlink -f ${id_dup_file})
test "$canon_id_file" != "$canon_dup_file" \
  || echo "id and dup same"

canon_debug_id_file=$(readlink -f ${debug_id_file})
canon_debug_dup_file=$(readlink -f ${debug_dup_file})
test "$canon_debug_id_file" != "$canon_debug_dup_file" \
  || echo "debug id and dup same"
],
[0],
[main id in main package
main dup id in main package
debug id in debug package
debug dup id in debug package
],
[])
AT_CLEANUP

# ------------------------------
# Check that (copied) files with duplicate build-ids are handled correctly.
# This should create "numbered" build-id files.
# This is simply the hello example with one binary copied.
AT_SETUP([rpmbuild buildid duplicate compat])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
# Should create two warnings
rundebug rpmbuild --quiet \
  --define="_build_id_links compat" \
  -ba "${abs_srcdir}"/data/SPECS/hello2cp.spec 2>&1 | grep "^warning: " \
  | cut -f1-3 -d' '

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
id=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# compat build-ids split...
id_file="./usr/lib/.build-id/${id:0:2}/${id:2}"
test -L "$id_file" && echo "main id in main package"
id_dup_file="./usr/lib/.build-id/${id:0:2}/${id:2}.1"
test -L "$id_dup_file" && echo "main dup id in main package"

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# compat, so main (and debug) ids are (also) here
compat_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}"
test -L "$compat_file" && echo "compat id in debug package"
compat_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1"
test -L "$compat_dup_file" && echo "compat dup id in debug package"

debug_id_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.debug"
test -L "$debug_id_file" && echo "debug id in debug package"
debug_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1.debug"
test -L "$debug_dup_file" && echo "debug dup id in debug package"

# We don't know which points to which, but we do know they point
# to different files.
canon_id_file=$(readlink -f ${id_file})
canon_dup_file=$(readlink -f ${id_dup_file})
test "$canon_id_file" != "$canon_dup_file" \
  || echo "id and dup same"

canon_debug_id_file=$(readlink -f ${debug_id_file})
canon_debug_dup_file=$(readlink -f ${debug_dup_file})
test "$canon_debug_id_file" != "$canon_debug_dup_file" \
  || echo "debug id and dup same"

canon_compat_file=$(readlink -f ${compat_file})
canon_compat_dup_file=$(readlink -f ${compat_dup_file})
test "$canon_compat_file" != "$canon_compat_dup_file" \
  || echo "compat id and dup same"
],
[0],
[warning: Duplicate build-ids
warning: Duplicate build-ids
main id in main package
main dup id in main package
compat id in debug package
compat dup id in debug package
debug id in debug package
debug dup id in debug package
],
[])
AT_CLEANUP

# ------------------------------
# Check that hard linked files are handled correctly.
# Since the hard linked files have duplicate build-ids,
# it should create "numbered" build-id files.
# This is simply the hello example with one binary hard linked.
AT_SETUP([rpmbuild buildid hardlink compat])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
# No warnings for hard links
rundebug rpmbuild --quiet \
  --define="_build_id_links compat" \
  -ba "${abs_srcdir}"/data/SPECS/hello2ln.spec 2>&1 | grep "^warning: " \
  | cut -f1-3 -d' '

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
id=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# compat build-ids split...
id_file="./usr/lib/.build-id/${id:0:2}/${id:2}"
test -L "$id_file" && echo "main id in main package"
id_dup_file="./usr/lib/.build-id/${id:0:2}/${id:2}.1"
test -L "$id_dup_file" && echo "main dup id in main package"

rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# compat, so main (and debug) ids are (also) here
compat_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}"
test -L "$compat_file" && echo "compat id in debug package"
compat_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1"
test -L "$compat_dup_file" && echo "compat dup id in debug package"

debug_id_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.debug"
test -L "$debug_id_file" && echo "debug id in debug package"
debug_dup_file="./usr/lib/debug/.build-id/${id:0:2}/${id:2}.1.debug"
test -L "$debug_dup_file" && echo "debug dup id in debug package"

# We don't know which points to which, but we do know they point
# to different files.
canon_id_file=$(readlink -f ${id_file})
canon_dup_file=$(readlink -f ${id_dup_file})
test "$canon_id_file" != "$canon_dup_file" \
  || echo "id and dup same"

canon_debug_id_file=$(readlink -f ${debug_id_file})
canon_debug_dup_file=$(readlink -f ${debug_dup_file})
test "$canon_debug_id_file" != "$canon_debug_dup_file" \
  || echo "debug id and dup same"

canon_compat_file=$(readlink -f ${compat_file})
canon_compat_dup_file=$(readlink -f ${compat_dup_file})
test "$canon_compat_file" != "$canon_compat_dup_file" \
  || echo "compat id and dup same"
],
[0],
[main id in main package
main dup id in main package
compat id in debug package
compat dup id in debug package
debug id in debug package
debug dup id in debug package
],
[])
AT_CLEANUP

# ------------------------------
# Check build-ids are recomputed with unique_build_ids,
# but not with _no_recompute_build_ids
AT_SETUP([rpmbuild buildid recompute])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
# Make sure we get debuginfo
export CFLAGS="-g"

# Unique 1
rundebug rpmbuild --quiet \
  --define="_unique_build_ids 1" \
  --undefine="_no_recompute_build_ids" \
  -ba "${abs_srcdir}"/data/SPECS/hello.spec

rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
test -f $hello_file || echo "No $hello_file"
id1=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# Make sure we generate a new one
rm $hello_file

# Unique 2
# Build the "next" release, which has no changes except for the release update.
rundebug rpmbuild --quiet \
  --define="_unique_build_ids 1" \
  --undefine="_no_recompute_build_ids" \
  -ba "${abs_srcdir}"/data/SPECS/hello-r2.spec

rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-2.*.rpm \
  | cpio -diu --quiet

# Extract the build-id from the main file
test -f $hello_file || echo "No $hello_file"
id2=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# Two unique builds should not be equal
if test "$id1" == "$id2"; then
  echo "uniques equal";
else
  echo "uniques unequal";
fi

# Make sure we generate a new one
rm $hello_file

# no-recompute 1
rundebug rpmbuild --quiet \
  --undefine="_unique_build_ids" \
  --undefine="_unique_debug_names" \
  --undefine="_unique_debug_srcs" \
  --define="_no_recompute_build_ids 1" \
  -ba "${abs_srcdir}"/data/SPECS/hello.spec

rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
test -f $hello_file || echo "No $hello_file"
id3=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# An unique and no-recompute build should be unequal
if test "$id2" == "$id3"; then
  echo "no-recompute unique equal";
else
  echo "no-recompute unique unequal";
fi

# Make sure we generate a new one
rm $hello_file

# no-recompute 2
# Build the "next" release, which has no changes except for the release update.
rundebug rpmbuild --quiet \
  --undefine="_unique_build_ids" \
  --undefine="_unique_debug_names" \
  --undefine="_unique_debug_srcs" \
  --define="_no_recompute_build_ids 1" \
  -ba "${abs_srcdir}"/data/SPECS/hello-r2.spec

rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-2.*.rpm \
  | cpio -diu --quiet

# Extract the build-id from the main file
test -f $hello_file || echo "No $hello_file"
id4=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# Two no-recompute builds should be equal. Even for different "releases".
if test "$id3" == "$id4"; then
  echo "no-recomputes equal";
else
  echo "no-recomputes unequal";
fi
],
[0],
[uniques unequal
no-recompute unique unequal
no-recomputes equal
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check build-ids are unique between versions/releases
# with _unique_build_ids defined.
AT_SETUP([rpmbuild buildid unique r1 r2])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
# No warnings for hard links
rundebug rpmbuild --quiet \
  --define="_unique_build_ids 1" \
  -ba "${abs_srcdir}"/data/SPECS/hello.spec

rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
test -f $hello_file || echo "No $hello_file"
id1=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# Make sure we generate a new one
rm $hello_file

# Build the "next" release, which has no changes except for the release update.
rundebug rpmbuild --quiet \
  --define="_unique_build_ids 1" \
  -ba "${abs_srcdir}"/data/SPECS/hello-r2.spec

rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-2.*.rpm \
  | cpio -diu --quiet

# Extract the build-id from the main file
test -f $hello_file || echo "No $hello_file"
id2=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

if test "$id1" == "$id2"; then echo "equal $id1"; else echo "unequal"; fi
],
[0],
[unequal
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check build-ids are non-unique between versions/releases
# with _unique_build_ids undefined (and exact same sources).
AT_SETUP([rpmbuild buildid non-unique r1 r2])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
# No warnings for hard links
rundebug rpmbuild --quiet \
  --undefine="_unique_build_ids" \
  --undefine="_unique_debug_names" \
  --undefine="_unique_debug_srcs" \
  -ba "${abs_srcdir}"/data/SPECS/hello.spec

rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm \
  | cpio -diu --quiet

hello_file=./usr/local/bin/hello

# Extract the build-id from the main file
test -f $hello_file || echo "No $hello_file"
id1=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

# Make sure we generate a new one
rm $hello_file

# Build the "next" release, which has no changes except for the release update.
rundebug rpmbuild --quiet \
  --undefine="_unique_build_ids" \
  --undefine="_unique_debug_names" \
  --undefine="_unique_debug_srcs" \
  -ba "${abs_srcdir}"/data/SPECS/hello-r2.spec

rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-2.*.rpm \
  | cpio -diu --quiet

# Extract the build-id from the main file
test -f $hello_file || echo "No $hello_file"
id2=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

if test "$id1" == "$id2"; then echo "equal"; else echo "unequal $id1 $id2"; fi
],
[0],
[equal
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check that build-id directories are created with the right permissions
# even if the spec file sets attrs explicitly.
AT_SETUP([rpmbuild buildid attrs])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
AT_SKIP_IF([$LUA_DISABLED])

# Build, contains one ELF which should have a buildid.
rundebug rpmbuild \
  --define="_build_id_links compat" \
  --define "_unique_debug_names 1" \
  --define "_unique_debug_srcs 1" \
  --quiet -ba "${abs_srcdir}"/data/SPECS/hello-attr-buildid.spec

runroot rpm -qp --qf "[[%{filenames} %{filemodes:perms}\n]]" \
        /build/RPMS/*/test-1.0-1*rpm \
	| grep "build-id d"

runroot rpm -qp --qf "[[%{filenames} %{filemodes:perms}\n]]" \
        /build/RPMS/*/test-debuginfo-1.0-1*rpm \
	| grep "build-id d"
],
[0],
[/usr/lib/.build-id drwxr-xr-x
/usr/lib/debug/.build-id drwxr-xr-x
],
[ignore])
AT_CLEANUP

# ------------------------------
# Check that build-id directories are created with the right attributes
# even if the spec file sets config explicitly.
AT_SETUP([rpmbuild buildid config attrs])
AT_KEYWORDS([build] [debuginfo] [buildid])
AT_CHECK([
RPMDB_INIT
AT_SKIP_IF([$LUA_DISABLED])

# Build, contains one ELF which should have a buildid.
rundebug rpmbuild \
  --define="_build_id_links compat" \
  --define "_unique_debug_names 1" \
  --define "_unique_debug_srcs 1" \
  --quiet -ba "${abs_srcdir}"/data/SPECS/hello-config-buildid.spec

# Should contain one config file.
runroot rpm -c -qp /build/RPMS/*/test-1.0-1*rpm
# Should not contain config files.
runroot rpm -c -qp /build/RPMS/*/test-debuginfo-1.0-1*rpm
],
[0],
[/etc/config.file
],
[ignore])
AT_CLEANUP
