#    rpmbuild.at: test rpmbuild
#
#    Copyright (C) 2007  Ralf Corsépius <corsepiu@fedoraproject.org>
#
#    This program 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.
#
#    This program 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, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

AT_BANNER([RPM build])

# ------------------------------
# Check if rpmbuild -ba *.spec works
AT_SETUP([rpmbuild -ba *.spec])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

run rpmbuild \
  --define "packager Iam" \
  -ba ${RPMDATA}/SPECS/hello.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([

runroot rpm -qp --qf "%{license}\n" /build/SRPMS/hello-1.0-1.src.rpm
runroot rpm -qp --qf "%{license}\n" /build/RPMS/*/hello-1.0-1.*.rpm
],
[0],
[GPL, ASL 1.0
GPL
],
[])

RPMTEST_CHECK([
runroot rpm -qp --qf "%{packager}\n" /build/SRPMS/hello-1.0-1.src.rpm
runroot rpm -qp --qf "%{packager}\n" /build/RPMS/*/hello-1.0-1.*.rpm
],
[0],
[Iam
Iam
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild -bs spec])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK([
runroot rpmbuild -bs \
	--define "packager Iam" \
	${RPMDATA}/SPECS/hello.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([
runroot rpm -q --qf "%{packager}\n" /build/SRPMS/hello-1.0-1.src.rpm
],
[0],
[Iam
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild -b buildsystem])
AT_KEYWORDS([build])
RPMDB_INIT

cp "${RPMTEST}/data/macros.buildsystem" "${RPMTEST}/${RPM_CONFIGDIR_PATH}/macros.d/"

RPMTEST_CHECK([
runroot rpmbuild -bb \
	--define "_prefix /usr" \
	--define "_sysconfdir /etc" \
	--define "_docdir_fmt %%{NAME}" \
	--quiet /data/SPECS/amhello.spec

runroot rpm -qpl --noartifact /build/RPMS/*/amhello-1.0-1.*.rpm
],
[0],
[/etc/hello.a
/etc/hello.d
/etc/hello.p
/usr/bin/hello
/usr/share/doc/amhello
/usr/share/doc/amhello/README
/usr/share/doc/amhello/README.distro
],
[ignore])

RPMTEST_CHECK([
# re-tar the source to a different name
tar xf /data/SOURCES/amhello-1.0.tar.gz
mv amhello-1.0 amhello-1.0-prerelease
tar czf "${RPMTEST}"/tmp/amhello-pre.tgz amhello-1.0-prerelease/
runroot rpmbuild -bb \
	--define "_prefix /usr" \
	--define "_sysconfdir /etc" \
	--define "_docdir_fmt %%{NAME}" \
	--define "_sourcedir /tmp" \
	--define "srcname amhello-pre.tgz" \
	--quiet /data/SPECS/amhello.spec

runroot rpm -qpl --noartifact /build/RPMS/*/amhello-1.0-1.*.rpm
],
[0],
[/etc/hello.a
/etc/hello.d
/etc/hello.p
/usr/bin/hello
/usr/share/doc/amhello
/usr/share/doc/amhello/README
/usr/share/doc/amhello/README.distro
],
[ignore])

RPMTEST_CHECK([
runroot rpmbuild -bb \
	--define "_prefix /usr" \
	--define "_sysconfdir /etc" \
	--define "_docdir_fmt %%{NAME}" \
	--with alt \
	--quiet /data/SPECS/amhello.spec

runroot rpm -qpl --noartifact /build/RPMS/*/amhello-1.0-1alt.*.rpm
],
[0],
[/alt/usr/bin/alt-hello
/alt/usr/share/doc/amhello
/alt/usr/share/doc/amhello/README
/etc/hello.a
/etc/hello.d
/etc/hello.p
/usr/share/doc/amhello
/usr/share/doc/amhello/README.distro
],
[ignore])

RPMTEST_CHECK([
runroot rpmbuild -bb \
	--define "buildsys bogons" \
	--quiet /data/SPECS/amhello.spec
],
[1],
[],
[error: line 11: Unknown buildsystem: bogons
])

RPMTEST_CHECK([
runroot rpmbuild -bb \
	--define 'buildsystem_autotools_conf %nil' \
	--define 'buildsystem_autotools_build %nil' \
	--define 'buildsystem_autotools_install %nil' \
	--define 'buildsystem_autotools_check %nil' \
	--quiet /data/SPECS/amhello.spec
],
[1],
[],
[error: line 11: Unknown buildsystem: autotools
])

RPMTEST_CHECK([
runroot rpmbuild -bb \
	--define 'buildsystem_autotools_build %nil' \
	--define 'buildsystem_autotools_check %nil' \
	--quiet /data/SPECS/amhello.spec
],
[1],
[],
[error: line 11: Required parametric macro %buildsystem_autotools_build not defined for buildsystem autotools
])

RPMTEST_CLEANUP

AT_SETUP([rpmbuild -b steps])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
runroot rpmbuild -bp /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
],
[0],
[Executing(%mkbuilddir)
Executing(%prep)
],
[])

RPMTEST_CHECK([
runroot rpmbuild -bp -D "preamble BuildRequires:foo" /data/SPECS/specstep.spec 2>&1|grep ^error:
],
[0],
[error: Failed build dependencies:
],
[])

RPMTEST_CHECK([
runroot rpmbuild -br /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
],
[0],
[Executing(%mkbuilddir)
Executing(%prep)
Executing(%generate_buildrequires)
],
[])

RPMTEST_CHECK([
runroot rpmbuild -bd /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
],
[0],
[Executing(%mkbuilddir)
Executing(%prep)
Executing(%generate_buildrequires)
],
[])

RPMTEST_CHECK([
runroot rpmbuild -bf /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
],
[0],
[Executing(%mkbuilddir)
Executing(%prep)
Executing(%generate_buildrequires)
Executing(%conf)
],
[])
RPMTEST_CHECK([
runroot rpmbuild -bc /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
],
[0],
[Executing(%mkbuilddir)
Executing(%prep)
Executing(%generate_buildrequires)
Executing(%conf)
Executing(%build)
],
[])

RPMTEST_CHECK([
runroot rpmbuild -bi /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
],
[0],
[Executing(%mkbuilddir)
Executing(%prep)
Executing(%generate_buildrequires)
Executing(%conf)
Executing(%build)
Executing(%install)
Executing(%check)
],
[])

RPMTEST_CHECK([
runroot rpmbuild -bb /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
],
[0],
[Executing(%mkbuilddir)
Executing(%prep)
Executing(%generate_buildrequires)
Executing(%conf)
Executing(%build)
Executing(%install)
Executing(%check)
Executing(rmbuild)
],
[])

RPMTEST_CHECK([
runroot rpmbuild -ba /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
],
[0],
[Executing(%mkbuilddir)
Executing(%prep)
Executing(%generate_buildrequires)
Executing(%conf)
Executing(%build)
Executing(%install)
Executing(%check)
Executing(rmbuild)
],
[])

RPMTEST_CHECK([
runroot rpmbuild -bs /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
],
[0],
[],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild script prepend/append])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
runroot rpmbuild -bb --quiet ${RPMDATA}/SPECS/append.spec
runroot_other rpm2cpio /build/RPMS/noarch/append-1.0-1.noarch.rpm | cpio -id 2> /dev/null
cat opt/out
],
[0],
[000
AAA
BBB
CCC
DDD
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild dir layout])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
run rpmbuild \
	-bi --target noarch --quiet \
	${RPMDATA}/SPECS/simple.spec
runroot_other find /build/BUILD|sort
],
[0],
[/build/BUILD
/build/BUILD/simple-1.0-build
/build/BUILD/simple-1.0-build/BUILDROOT
/build/BUILD/simple-1.0-build/BUILDROOT/opt
/build/BUILD/simple-1.0-build/BUILDROOT/opt/bin
/build/BUILD/simple-1.0-build/BUILDROOT/opt/bin/simple
/build/BUILD/simple-1.0-build/SPECPARTS
/build/BUILD/simple-1.0-build/simple
],
[ignore])

RPMTEST_CHECK([
run rpmbuild \
	-bi --target noarch --quiet \
	--with setup \
	${RPMDATA}/SPECS/simple.spec
runroot_other find /build/BUILD|sort
],
[0],
[/build/BUILD
/build/BUILD/simple-1.0-build
/build/BUILD/simple-1.0-build/BUILDROOT
/build/BUILD/simple-1.0-build/BUILDROOT/opt
/build/BUILD/simple-1.0-build/BUILDROOT/opt/bin
/build/BUILD/simple-1.0-build/BUILDROOT/opt/bin/simple
/build/BUILD/simple-1.0-build/SPECPARTS
/build/BUILD/simple-1.0-build/simple-1.0
/build/BUILD/simple-1.0-build/simple-1.0/dir1
/build/BUILD/simple-1.0-build/simple-1.0/dir1/file3
/build/BUILD/simple-1.0-build/simple-1.0/file1
/build/BUILD/simple-1.0-build/simple-1.0/file2
/build/BUILD/simple-1.0-build/simple-1.0/simple
],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild -ba autosetup])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

run rpmbuild \
  -ba ${RPMDATA}/SPECS/hello-auto.spec
],
[0],
[ignore],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild -bp autosetup without patches])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

run rpmbuild -D="nopatches 1" \
  -bp ${RPMDATA}/SPECS/hello-auto.spec
],
[0],
[ignore],
[stderr])

RPMTEST_CHECK([
grep warning: stderr
],
[1],
[],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild --build-in-place 1])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK([
runroot_other tar xf /data/SOURCES/hello-2.0.tar.gz
runroot_other mv hello-2.0/hello.spec .
runroot_other find hello-2.0 | sort
],
[0],
[hello-2.0
hello-2.0/COPYING
hello-2.0/FAQ
hello-2.0/Makefile
hello-2.0/README
hello-2.0/hello.c
],
[])

RPMTEST_CHECK([
runroot --chdir hello-2.0 rpmbuild -bb --build-in-place ../hello.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([
runroot_other sed -i -e "/%setup.*/d" hello.spec
runroot --chdir hello-2.0 rpmbuild -bb --build-in-place ../hello.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([
runroot rpm -qp --qf "%{name}-%{version}-%{release}\n" /build/RPMS/*/*.rpm
],
[0],
[hello-2.0-1
hello-debuginfo-2.0-1
],
[])

# finally, see that we left the tree intact 
RPMTEST_CHECK([
runroot_other find hello-2.0 | sort
],
[0],
[hello-2.0
hello-2.0/COPYING
hello-2.0/FAQ
hello-2.0/Makefile
hello-2.0/README
hello-2.0/debugfiles.list
hello-2.0/debuglinks.list
hello-2.0/debugsources.list
hello-2.0/elfbins.list
hello-2.0/hello
hello-2.0/hello.c
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild --build-in-place --noprep])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK([
runroot_other tar xf /data/SOURCES/hello-2.0.tar.gz
runroot_other mv hello-2.0/hello.spec .
],
[0],
[],
[])

RPMTEST_CHECK([
runroot --chdir hello-2.0 rpmbuild -bb --quiet \
	--noprep --build-in-place ../hello.spec
],
[0],
[],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild --build-in-place --short-circuit])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK([
runroot_other tar xf /data/SOURCES/hello-2.0.tar.gz
runroot --chdir hello-2.0 rpmbuild -bi --build-in-place hello.spec
runroot --chdir hello-2.0 rpmbuild -bb --build-in-place --short-circuit hello.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([
runroot rpm -qp --qf "%{name}-%{version}-%{release}\n" /build/RPMS/*/*.rpm
],
[0],
[hello-2.0-1
hello-debuginfo-2.0-1
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild with %autosetup -C])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

run rpmbuild \
  -bp ${RPMDATA}/SPECS/autosetup-noroot.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([

run rpmbuild \
  -bp ${RPMDATA}/SPECS/autosetup-singleroot.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([
run rpmbuild \
  -bp ${RPMDATA}/SPECS/autosetup-singleroot1.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([
run rpmbuild \
  -bp ${RPMDATA}/SPECS/autosetup-singleroot2.spec
],
[0],
[ignore],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild -ba autopatch])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

run rpmbuild \
  -ba --quiet ${RPMDATA}/SPECS/hello-autopatch.spec
],
[0],
[],
[stderr])

RPMTEST_CHECK([
grep warning: stderr
],
[0],
[warning: /data/SPECS/hello-autopatch.spec line 22: autopatch: no matching patches in range
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild -ba find-lang])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
runroot rpmbuild \
  -ba /data/SPECS/find-lang-test.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([[
runroot rpm -qp --qf '[%{FILELANGS} %{FILENAMES}\n]' /build/RPMS/noarch/find-lang-test-1.0-1.noarch.rpm
]],
[0],
[ /usr/share/find-lang-test/empty.txt
de /usr/share/man/de_DE/man1/find-lang-test.1.gz
fi /usr/share/man/fi/man1/find-lang-test.1.gz
 /usr/share/man/man1/find-lang-test.1.gz
pl /usr/share/man/pl/man1/find-lang-test.1.gz
],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild -ba find-lang --generate-subpackages])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
runroot rpmbuild -D "langpacks 1"\
  -ba /data/SPECS/find-lang-test.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([[
runroot rpm -qp --qf '%{NVRA}\n[%{FILELANGS} %{FILENAMES}\n]' /build/RPMS/noarch/find-lang-test*-1.0-1.noarch.rpm
]],
[0],
[find-lang-test-1.0-1.noarch
 /usr/share/find-lang-test/empty.txt
 /usr/share/man/man1/find-lang-test.1.gz
find-lang-test-langpack-de-1.0-1.noarch
de /usr/share/man/de_DE/man1/find-lang-test.1.gz
find-lang-test-langpack-fi-1.0-1.noarch
fi /usr/share/man/fi/man1/find-lang-test.1.gz
find-lang-test-langpack-pl-1.0-1.noarch
pl /usr/share/man/pl/man1/find-lang-test.1.gz
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check if rpmbuild --rebuild *.src.rpm works
AT_SETUP([rpmbuild --rebuild])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

run rpmbuild \
  --define "packager Iam" \
  --rebuild ${RPMDATA}/SRPMS/hello-1.0-1.src.rpm
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([
runroot rpm -qp --qf "%{packager}\n" /build/RPMS/*/hello-1.0-1.*.rpm
],
[0],
[Iam
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild --short-circuit -bl])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

run rpmbuild -bi ${RPMDATA}/SPECS/hello.spec &> /dev/null
run rpmbuild --quiet -bl --short-circuit ${RPMDATA}/SPECS/hello.spec
],
[0],
[],
[])
RPMTEST_CLEANUP

# ------------------------------
# Check if tar unpacking works
AT_SETUP([rpmbuild -tb <tar with bad spec>])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild \
  -tb /data/SOURCES/hello-1.0.tar.gz
],
[1],
[ignore],
[error: line 5: Unknown tag: Serial:	1
])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild -ts <spec>])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --quiet \
	--define "packager Iam" \
	-ts /data/SOURCES/hello-2.0.tar.gz
runroot rpm -qpl /build/SRPMS/hello-2.0-1.src.rpm
],
[0],
[hello-2.0.tar.gz
hello.spec
],
[])

RPMTEST_CHECK([
runroot rpm -q --qf "%{packager}\n" /build/SRPMS/hello-2.0-1.src.rpm
],
[0],
[Iam
],
[])
RPMTEST_CLEANUP

# weird filename survival test
AT_SETUP([rpmbuild package with weird filenames])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild -bb --quiet /data/SPECS/weirdnames.spec
runroot rpm -qpl /build/RPMS/noarch/weirdnames-1.0-1.noarch.rpm
],
[0],
[/opt/!absolutely
/opt/#nocomment
/opt/$talks
/opt/&ground
/opt/(maybe)
/opt/<namehere>
/opt/\.back
/opt/after{all}
/opt/bar"ed
/opt/equals=not
/opt/foo'ed
/opt/index[[this]]
/opt/just space
/opt/perhaps;not
/opt/tee|two
/opt/the * are falling
/opt/to?be
/opt/true:false
/opt/~right
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild package with illegal filenames])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
# XXX current output is not well suited for grab + compare, just ignore
runroot rpmbuild -bb --quiet --with illegal /data/SPECS/weirdnames.spec
],
[1],
[],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check if tar build works
# TODO: test that the rpms are actually created...
AT_SETUP([rpmbuild -tb])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

run rpmbuild \
  -ta "${RPMDATA}/SOURCES/hello-2.0.tar.gz"
],
[0],
[ignore],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild with deprecated patch])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
runroot rpmbuild -bp --quiet /data/SPECS/hello-patch.spec
],
[1],
[ignore],
[error: %patchN is obsolete, use %patch N (or %patch -P N): %patch0 -p1 -b .install

])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild scriptlet -f])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild \
	-bb --quiet /data/SPECS/scriptfile.spec
runroot rpm -qp --scripts --triggers --filetriggers \
	/build/RPMS/noarch/scriptfile-1-1.noarch.rpm
],
[0],
[pretrans scriptlet (using /bin/sh):
pretrans

preinstall scriptlet (using /bin/sh):
pre

postinstall scriptlet (using /bin/sh):
post

preuninstall scriptlet (using /bin/sh):
preun

postuninstall scriptlet (using /bin/sh):
postun

posttrans scriptlet (using /bin/sh):
posttrans

preuntrans scriptlet (using /bin/sh):
preuntrans

postuntrans scriptlet (using /bin/sh):
postuntrans

verify scriptlet (using /bin/sh):
verifyscript

triggerprein scriptlet (using /bin/sh) -- scriptfile
triggerprein

triggerin scriptlet (using /bin/sh) -- scriptfile
triggerin

triggerun scriptlet (using /bin/sh) -- scriptfile
triggerun

triggerpostun scriptlet (using /bin/sh) -- scriptfile
triggerpostun

filetriggerin scriptlet (using /bin/sh) -- /path
filetriggerin

filetriggerun scriptlet (using /bin/sh) -- /path
filetriggerun

transfiletriggerin scriptlet (using /bin/sh) -- /path
transfiletriggerin

transfiletriggerun scriptlet (using /bin/sh) -- /path
transfiletriggerun

],
[])
RPMTEST_CLEANUP

# ------------------------------
# %attr/%defattr tests
AT_SETUP([rpmbuild %attr and %defattr])
AT_KEYWORDS([build])
RPMDB_INIT

runroot \
  --setenv SOURCE_DATE_EPOCH 1582222800 \
  rpmbuild \
  -bb --quiet \
  --define "use_source_date_epoch_as_buildtime 1" \
  --define "build_mtime_policy clamp_to_source_date_epoch" \
  /data/SPECS/attrtest.spec

RPMTEST_CHECK([[
runroot rpm -qp --qf \
  "\n[%{filemodes:perms} %-8{fileusername} %-8{filegroupname} %{filenames}\n]"\
  /build/RPMS/noarch/attrtest-1.0-1.noarch.rpm
]],
[0],
[
drwx------ root     root     /a/dir
-r-------- root     root     /a/file
drwx------ daemon   adm      /b/dir
-r-------- daemon   adm      /b/file
drwxr-x--- root     adm      /c/dir
-rw-r----- daemon   root     /c/file
drwxr-x--x daemon   bin      /d/dir
-rw-r--r-- bin      daemon   /d/file
drwx------ foo      bar      /e/dir
-r-------- foo      bar      /e/file
drwxrwx--- bar      foo      /f/dir
-rw-rw---- bar      foo      /f/file
drwx------ adm      foo      /g/dir
-r-------- bar      adm      /g/file
drwxr-xr-x foo      bar      /h/dir
-rw-r--r-- foo      bar      /h/file
drwxr-x--- adm      root     /i/dir
-rwsr-xr-x root     adm      /i/file
drwxrwxrwx zoot     zoot     /j/dir
--w--w--w- zoot     zoot     /j/file
],
[])

RPMTEST_CHECK([[
runroot_other rpm2cpio /build/RPMS/noarch/attrtest-1.0-1.noarch.rpm | cpio -tv --quiet
]],
[0],
[drwx------   1 root     root            0 Feb 20  2020 ./a/dir
-r--------   1 root     root           15 Feb 20  2020 ./a/file
drwx------   1 daemon   adm             0 Feb 20  2020 ./b/dir
-r--------   1 daemon   adm            15 Feb 20  2020 ./b/file
drwxr-x---   1 root     adm             0 Feb 20  2020 ./c/dir
-rw-r-----   1 daemon   root           15 Feb 20  2020 ./c/file
drwxr-x--x   1 daemon   bin             0 Feb 20  2020 ./d/dir
-rw-r--r--   1 bin      daemon         15 Feb 20  2020 ./d/file
drwx------   1 root     root            0 Feb 20  2020 ./e/dir
-r--------   1 root     root           15 Feb 20  2020 ./e/file
drwxrwx---   1 root     root            0 Feb 20  2020 ./f/dir
-rw-rw----   1 root     root           15 Feb 20  2020 ./f/file
drwx------   1 adm      root            0 Feb 20  2020 ./g/dir
-r--------   1 root     adm            15 Feb 20  2020 ./g/file
drwxr-xr-x   1 root     root            0 Feb 20  2020 ./h/dir
-rw-r--r--   1 root     root           15 Feb 20  2020 ./h/file
drwxr-x---   1 adm      root            0 Feb 20  2020 ./i/dir
-rwsr-xr-x   1 root     adm            15 Feb 20  2020 ./i/file
drwxrwxrwx   1 root     root            0 Feb 20  2020 ./j/dir
--w--w--w-   1 root     root           15 Feb 20  2020 ./j/file
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild SOURCE_DATE_EPOCH])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot \
  --setenv SOURCE_DATE_EPOCH 1699460418 \
  rpmbuild \
  -bb --quiet \
  --define "use_source_date_epoch_as_buildtime 1" \
  --define "build_mtime_policy clamp_to_source_date_epoch" \
  /data/SPECS/source_date_epoch.spec

runroot rpm -q --dump /build/RPMS/noarch/test-1-1.noarch.rpm
],
[0],
[/0.txt 15 1699460418 91751cee0a1ab8414400238a761411daa29643ab4b8243e9a91649e25be53ada 0100644 root root 0 0 0 X
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild SOURCE_DATE_EPOCH=0])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot \
  --setenv SOURCE_DATE_EPOCH 0 \
  rpmbuild \
  -bb --quiet \
  --define "use_source_date_epoch_as_buildtime 1" \
  --define "build_mtime_policy clamp_to_source_date_epoch" \
  /data/SPECS/source_date_epoch.spec

runroot rpm -q --dump /build/RPMS/noarch/test-1-1.noarch.rpm
],
[0],
[/0.txt 15 0 91751cee0a1ab8414400238a761411daa29643ab4b8243e9a91649e25be53ada 0100644 root root 0 0 0 X
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild _buildtime macro])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot \
  rpmbuild \
  -bb --quiet \
  --define "_buildtime 1699460418" \
  --define "build_mtime_policy clamp_to_buildtime" \
  /data/SPECS/source_date_epoch.spec

runroot rpm -q --dump /build/RPMS/noarch/test-1-1.noarch.rpm
runroot rpm -q --qf '%{BUILDTIME}\n' /build/RPMS/noarch/test-1-1.noarch.rpm
],
[0],
[/0.txt 15 1699460418 91751cee0a1ab8414400238a761411daa29643ab4b8243e9a91649e25be53ada 0100644 root root 0 0 0 X
1699460418
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild unpackaged files])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild \
  -bb --quiet --with unpackaged_files /data/SPECS/hlinktest.spec
],
[1],
[],
[error: Installed (but unpackaged) file(s) found:
   /toot
])
RPMTEST_CLEANUP

# rpm doesn't detect unpackaged directories but should, really
AT_SETUP([rpmbuild unpackaged directories])
AT_KEYWORDS([build])
AT_XFAIL_IF([test $RPM_XFAIL -ne 0])
RPMDB_INIT
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild \
  -bb --quiet --with unpackaged_dirs /data/SPECS/hlinktest.spec
],
[1],
[],
[error: Installed (but unpackaged) file(s) found:
   /zoo
    Installed (but unpackaged) file(s) found:
   /zoo
])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild glob])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet /data/SPECS/globtest.spec
runroot rpm -qp \
	--qf "[[%{filemodes:perms} %{filenames}\n]]" \
	/build/RPMS/noarch/globtest-1.0-1.noarch.rpm
],
[0],
[-rw-r--r-- /opt/globtest/baf
drwxr-xr-x /opt/globtest/bang
-rw-r--r-- /opt/globtest/bif
drwxr-xr-x /opt/globtest/bing
drwxr-xr-x /opt/globtest/bong
-rw-r--r-- /opt/globtest/brace
drwxr-xr-x /opt/globtest/foo
-rw-r--r-- /opt/globtest/foo/one
-rw-r--r-- /opt/globtest/foo/three
-rw-r--r-- /opt/globtest/foo/two
-rw-r--r-- /opt/globtest/grace
lrwxrwxrwx /opt/globtest/linkbad
lrwxrwxrwx /opt/globtest/linkgood
-rw-r--r-- /opt/globtest/weird%name
-rw-r--r-- /opt/globtest/zab
-rw-r--r-- /opt/globtest/zeb
-rw-r--r-- /opt/globtest/zib
b--------- /test-block
],
[warning: absolute symlink: /opt/globtest/linkbad -> /opt/globtest/zub
warning: absolute symlink: /opt/globtest/linkgood -> /opt/globtest/zab
])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild glob escape])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet /data/SPECS/globesctest.spec
runroot rpm -qpl /build/RPMS/noarch/globesctest-1.0-1.noarch.rpm
],
[0],
[/opt/foo [[baz]]
/opt/foo a
/opt/foo b
/opt/foo bar
/opt/foo bar.conf
/opt/foo r
/opt/foo" bar"
/opt/foo"'baz"'
/opt/foo"bar"
/opt/foo"baz"
/opt/foo%name
/opt/foo*
/opt/foo*a
/opt/foo*b
/opt/foo*r
/opt/foo-bar1
/opt/foo-bar2
/opt/foo?bar
/opt/foo[[123]]
/opt/foo[[bar baz]]
/opt/foo[[bar]]
/opt/foo[[bax bay]]
/opt/foo\
/opt/foo\[[bar\]]
/opt/foo\[[baz\]]
/opt/foo\bar
/opt/foo\bay
/opt/foobar
/opt/foobara
/opt/foobarb
/opt/foobawa
/opt/foobawb
/opt/foobaya
/opt/foobayb
/opt/foobaz
/opt/foobaza
/opt/foobazb
/opt/fooxbarybaz
/opt/foo{bar,baz}
/opt/share/doc/globesctest-1.0
/opt/share/doc/globesctest-1.0/bar
/opt/share/doc/globesctest-1.0/baz
/opt/share/doc/globesctest-1.0/docfb[[123]]
/opt/share/doc/globesctest-1.0/foo & bar !
/opt/share/doc/globesctest-1.0/foo a
/opt/share/doc/globesctest-1.0/foo b
/opt/share/doc/globesctest-1.0/foo bar
/opt/share/doc/globesctest-1.0/foo r
/opt/share/doc/globesctest-1.0/foo%name
/opt/share/doc/globesctest-1.0/foo[[bar]]
/opt/share/licenses/globesctest-1.0
/opt/share/licenses/globesctest-1.0/licfb[[123]]
],
[],
)
RPMTEST_CLEANUP

AT_SETUP([rpmbuild bad escape])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet /data/SPECS/badescape.spec
],
[1],
[],
[error: Trailing backslash: /opt/foo\

],
)
RPMTEST_CLEANUP

AT_SETUP([rpmbuild bad quote])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet /data/SPECS/badquote.spec
],
[1],
[],
[error: Missing quote: /opt/foo

],
)
RPMTEST_CLEANUP

AT_SETUP([rpmbuild prefixpostfix])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet \
	/data/SPECS/prefixtest.spec
runroot rpm -qp \
	--qf "[[%{filemodes:perms} %{filenames}\n]]" \
	/build/RPMS/noarch/prefixtest-1.0-1.noarch.rpm
],
[0],
[-rw-r--r-- /opt/prefixtest/ba
drwxr-xr-x /opt/prefixtest/ban
-rw-r--r-- /opt/prefixtest/bi
drwxr-xr-x /opt/prefixtest/bin
drwxr-xr-x /opt/prefixtest/bon
drwxr-xr-x /opt/prefixtest/foo
-rw-r--r-- /opt/prefixtest/foo/one
-rw-r--r-- /opt/prefixtest/foo/three
-rw-r--r-- /opt/prefixtest/foo/two
lrwxrwxrwx /opt/prefixtest/linkbad
lrwxrwxrwx /opt/prefixtest/linkgood
-rw-r--r-- /opt/prefixtest/weird%name
-rw-r--r-- /opt/prefixtest/zab
-rw-r--r-- /opt/prefixtest/zeb
-rw-r--r-- /opt/prefixtest/zib
],
[warning: absolute symlink: /opt/prefixtest/linkbad -> /opt/prefixtest/zub
warning: absolute symlink: /opt/prefixtest/linkgood -> /opt/prefixtest/zab
])
RPMTEST_CLEANUP

# ------------------------------
# Check if weak and reverse requires can be built
AT_SETUP([Weak and reverse requires])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet \
	--define "pkg weakdeps" \
	--define "recs foo > 1.2.3" \
	--define "recflags post" \
	--define "sugs bar >= 0.1.2" \
	--define "sups baz" \
	--define "supflags preun" \
	--define "ens zap = 3" \
	  /data/SPECS/deptest.spec

runroot rpm -qpv --recommends /build/RPMS/noarch/deptest-weakdeps-1.0-1.noarch.rpm
runroot rpm -qpv --suggests /build/RPMS/noarch/deptest-weakdeps-1.0-1.noarch.rpm
runroot rpm -qpv --supplements /build/RPMS/noarch/deptest-weakdeps-1.0-1.noarch.rpm
runroot rpm -qpv --enhances /build/RPMS/noarch/deptest-weakdeps-1.0-1.noarch.rpm
],
[0],
[post: foo > 1.2.3
manual: bar >= 0.1.2
preun: baz
manual: zap = 3
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Test BuildRequire functionality
AT_SETUP([Build requires])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet \
		--define "pkg buildreq" \
		--define "buildreqs foo > 1.2.3" \
		/data/SPECS/deptest.spec
],
[11],
[],
[error: Failed build dependencies:
	foo > 1.2.3 is needed by deptest-buildreq-1.0-1.noarch
])
RPMTEST_CLEANUP

AT_SETUP([Dependency generation 1])
AT_KEYWORDS([build])
RPMDB_INIT

runroot rpmbuild -bb --quiet \
		--define "_binary_payload w9.gzdio" \
		/data/SPECS/filedep.spec

RPMTEST_CHECK([
runroot rpm  -qp --requires /build/RPMS/noarch/filedep-1.0-1.noarch.rpm
],
[0],
[/bin/f00f
/bin/sh
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
],
[])

RPMTEST_CHECK([
runroot rpm -qp --provides /build/RPMS/noarch/filedep-1.0-1.noarch.rpm
],
[0],
[filedep = 1.0-1
],
[])

RPMTEST_CHECK([
runroot rpm -qp --qf '[["%{FILENAMES}\t%{FILEREQUIRE}"\n]]' /build/RPMS/noarch/filedep-1.0-1.noarch.rpm
],
[0],
["/etc/foo.conf	"
"/usr/bin/bar	/bin/f00f"
"/usr/bin/foo	/bin/sh"
"/usr/share/doc/filedep	"
"/usr/share/doc/filedep/README	"
],
[])

RPMTEST_CHECK([
runroot rpm -qp --qf '[["%{FILENAMES}\t%{FILEPROVIDE}"\n]]' /build/RPMS/noarch/filedep-1.0-1.noarch.rpm
],
[0],
["/etc/foo.conf	"
"/usr/bin/bar	"
"/usr/bin/foo	"
"/usr/share/doc/filedep	"
"/usr/share/doc/filedep/README	"
],
[])
RPMTEST_CLEANUP

AT_SETUP([Dependency generation 2])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet \
		/data/SPECS/shebang.spec
runroot rpm -qp --requires /build/RPMS/noarch/shebang-0.1-1.noarch.rpm|grep -v ^rpmlib
],
[0],
[/bin/blabla
],
[])
RPMTEST_CLEANUP

AT_SETUP([Dependency generation 3])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

cat << EOF > "${RPMTEST}"/tmp/bad.req
#!/bin/sh
echo 'bad = *'
EOF
chmod a+x "${RPMTEST}"/tmp/bad.req

runroot rpmbuild -bb --quiet \
		--define "__script_requires /tmp/bad.req" \
		/data/SPECS/shebang.spec
],
[1],
[],
[error: Illegal char '*' (0x2a) in: *
])
RPMTEST_CLEANUP

AT_SETUP([Dependency generation 4])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet \
		--define '__script_requires() \
					foo(%{basename:%{1}})\
					many(%{basename:%{1}}) = 55\
					many(%{basename:%{1}}) = 99\
					many(%{basename:%{1}})\
					bar(%{basename:%{1}}) = 33\
					bar(%{basename:%{1}})\
					bar(%{basename:%{1}}) = 66\
					one(%{basename:%{1}}) = 77'\
		/data/SPECS/shebang.spec
runroot rpm -qp --requires /build/RPMS/noarch/shebang-0.1-1.noarch.rpm|grep -v ^rpmlib
],
[0],
[bar(shebang) = 33
bar(shebang) = 66
foo(shebang)
many(shebang) = 55
many(shebang) = 99
one(shebang) = 77
],
[])
RPMTEST_CLEANUP

AT_SETUP([Dependency generation 5])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet \
		--define '__script_mime text/plain' \
		--define '__script_magic %{nil}' \
		--define '__script_namespace mine' \
		/data/SPECS/shebang.spec
runroot rpm -qp --requires /build/RPMS/noarch/shebang-0.1-1.noarch.rpm|grep -v ^rpmlib
],
[0],
[mine(/bin/blabla)
],
[])
RPMTEST_CLEANUP

AT_SETUP([Local dependency generator])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet \
		--define '_local_file_attrs my_test_attr' \
		--define '__my_test_attr_provides() foo(%{basename:%{1}})' \
		--define '__my_test_attr_path .*' \
		/data/SPECS/shebang.spec
runroot rpm -qp --provides /build/RPMS/noarch/shebang-0.1-1.noarch.rpm|grep -v ^rpmlib
],
[0],
[foo(shebang)
shebang = 0.1-1
],
[])

RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet \
		--define '_local_file_attrs script' \
		--define '__script_provides() foobar(%{basename:%{1}})' \
		/data/SPECS/shebang.spec
runroot rpm -qp --provides /build/RPMS/noarch/shebang-0.1-1.noarch.rpm|grep -v ^rpmlib
],
[0],
[foobar(shebang)
shebang = 0.1-1
],
[])

RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet \
		--define '_local_file_attrs my_test_attr:script' \
		--define '__my_test_attr_provides() foo(%{basename:%{1}})' \
		--define '__my_test_attr_path .*' \
		--define '__script_provides() foobar(%{basename:%{1}})' \
		/data/SPECS/shebang.spec
runroot rpm -qp --provides /build/RPMS/noarch/shebang-0.1-1.noarch.rpm|grep -v ^rpmlib
],
[0],
[foo(shebang)
foobar(shebang)
shebang = 0.1-1
],
[])
RPMTEST_CLEANUP

AT_SETUP([elf dependencies])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK([
runroot_other chmod a-x /data/misc/libhello.so
runroot_other ${RPM_CONFIGDIR_PATH}/elfdeps -R /data/misc/libhello.so
runroot_other chmod a+x /data/misc/libhello.so
runroot_other ${RPM_CONFIGDIR_PATH}/elfdeps -R /data/misc/libhello.so
],
[0],
[libc.so.6(GLIBC_2.2.5)(64bit)
libc.so.6()(64bit)
rtld(GNU_HASH)
libc.so.6(GLIBC_2.2.5)(64bit)
libc.so.6()(64bit)
rtld(GNU_HASH)
],
[])

RPMTEST_CHECK([
runroot_other chmod a-x /data/misc/libhello.so
runroot_other ${RPM_CONFIGDIR_PATH}/elfdeps -P /data/misc/libhello.so
runroot_other chmod a+x /data/misc/libhello.so
runroot_other ${RPM_CONFIGDIR_PATH}/elfdeps -P /data/misc/libhello.so
],
[0],
[libhello.so()(64bit)
libhello.so()(64bit)
],
[])

RPMTEST_CHECK([
runroot_other chmod a-x /data/misc/helloexe
runroot_other ${RPM_CONFIGDIR_PATH}/elfdeps -R /data/misc/helloexe
runroot_other chmod a+x /data/misc/helloexe
runroot_other ${RPM_CONFIGDIR_PATH}/elfdeps -R /data/misc/helloexe
],
[0],
[libc.so.6(GLIBC_2.2.5)(64bit)
libhello.so()(64bit)
libc.so.6()(64bit)
rtld(GNU_HASH)
],
[])

RPMTEST_CHECK([
runroot_other chmod a-x /data/misc/hellopie
runroot_other ${RPM_CONFIGDIR_PATH}/elfdeps -R /data/misc/hellopie
runroot_other chmod a+x /data/misc/hellopie
runroot_other ${RPM_CONFIGDIR_PATH}/elfdeps -R /data/misc/hellopie
],
[0],
[libc.so.6(GLIBC_2.2.5)(64bit)
libhello.so()(64bit)
libc.so.6()(64bit)
rtld(GNU_HASH)
],
[])
RPMTEST_CLEANUP

# ------------------------------
# Test spec query functionality
AT_SETUP([rpmspec query 1])
AT_KEYWORDS([build])
RPMTEST_CHECK([

runroot rpmspec -q \
		--define "pkg dep" \
		--define "reqs foo > 1.2.3 bar <= 2.3" \
		--requires \
		/data/SPECS/deptest.spec
],
[0],
[bar <= 2.3
foo > 1.2.3
],
[])

RPMTEST_CLEANUP

# ------------------------------
# archive sanity check
AT_SETUP([rpmbuild archive sanity])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild \
  -bb --quiet /data/SPECS/attrtest.spec

runroot_other rpm2cpio \
  /build/RPMS/noarch/attrtest-1.0-1.noarch.rpm > "${RPMTEST}"/tmp/attrtest.cpio
],
[0],
[],
[])
RPMTEST_CLEANUP

# ------------------------------
# Check if rpmbuild creates the minisymtab section in the main hello binary
AT_SETUP([rpmbuild debuginfo minisymtab])
AT_KEYWORDS([build] [debuginfo])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

# Use macros.debug to generate a debuginfo package.
export CFLAGS="-g"
rundebug rpmbuild --quiet \
  --rebuild ${RPMDATA}/SRPMS/hello-1.0-1.src.rpm

# Extract the main package and inspect the hello binary
# It should contain .gnu_debugdata, but not the full .symtab
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm | cpio -diu
test -f ./usr/local/bin/hello || exit 1
readelf -S ./usr/local/bin/hello |\
   grep -q .gnu_debugdata; test $? == 0 || exit 1
readelf -S ./usr/local/bin/hello \
  | grep -q .symtab; test $? == 1 || exit 1

# And the opposite for the debuginfo package
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | cpio -diu
test -f ./usr/lib/debug/usr/local/bin/hello*.debug || exit 1
readelf -S ./usr/lib/debug/usr/local/bin/hello*.debug \
  | grep -q .gnu_debugdata; test $? == 1 || exit 1
readelf -S ./usr/lib/debug/usr/local/bin/hello*.debug \
  | grep -q .symtab; test $? == 0 || exit 1
],
[0],
[],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check if rpmbuild doesn't create the minisymtab section if we keep symtab
# Some package might want to use strip -g to keep the symbol table and only
# but the debug symbols/info in the debuginfo package.
AT_SETUP([rpmbuild debuginfo minisymtab strip -g])
AT_KEYWORDS([build] [debuginfo])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

# Use macros.debug to generate a debuginfo package.
export CFLAGS="-g"
rundebug rpmbuild --quiet \
  --define="_find_debuginfo_opts -g" \
  --rebuild ${RPMDATA}/SRPMS/hello-1.0-1.src.rpm

# Extract the main package and inspect the hello binary
# It should contain .symtab (because of strip -g), so doesn't .gnu_debugdata.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm | cpio -diu
test -f ./usr/local/bin/hello || exit 1
readelf -S ./usr/local/bin/hello \
  | grep -q .gnu_debugdata; test $? == 1 || exit 1
readelf -S ./usr/local/bin/hello \
  | grep -q .symtab; test $? == 0 || exit 1

# The debuginfo package should contain neither. The .symtab is NOBITS.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-debuginfo-1.0-1.*.rpm \
  | cpio -diu
test -f ./usr/lib/debug/usr/local/bin/hello*.debug || exit 1
readelf -S ./usr/lib/debug/usr/local/bin/hello*.debug \
  | grep -q .gnu_debugdata; test $? == 1 || exit 1
readelf -S ./usr/lib/debug/usr/local/bin/hello*.debug \
  | grep .symtab | grep -q NOBITS; test $? == 0 || exit 1
],
[0],
[],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check if rpmbuild runs dwz and generates a multi file that with shared
# debuginfo. This is simply the hello example with one binary build twice
# so dwz has enough slightly similar debug data.
# Test the case without unique debug file names.
AT_SETUP([rpmbuild debuginfo dwz])
AT_KEYWORDS([build] [debuginfo] [dwz])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --undefine "_unique_debug_names" \
  --undefine "_unique_debug_srcs" \
  -ba ${RPMDATA}/SPECS/hello2.spec

# The debuginfo package should contain a .debug file for each binary
# and a dwz multi file that contains the shared debuginfo between them.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu

hello_file_debug=./usr/lib/debug/usr/local/bin/hello.debug
hello2_file_debug=./usr/lib/debug/usr/local/bin/hello2.debug
hello_multi_file=./usr/lib/debug/.dwz/hello2-1.0-1.*
test -f $hello_file_debug || echo "no hello debug file: $hello_file_debug"
test -f $hello2_file_debug || echo "no hello2 debug file: $hello2_file_debug"
test -f $hello_multi_file || echo "no dwz multi file: $hello_multi_file"

# Make sure the main package binaries contain the correct build-ids
# linking them to the debug packages.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-1.0-1.*.rpm \
  | cpio -diu
hello_file=./usr/local/bin/hello
hello2_file=./usr/local/bin/hello2
test -f $hello_file || echo "no hello file: $hello_file"
test -f $hello2_file || echo "no hello2 file: $hello2_file"

id1=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')
id2=$(file $hello2_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')
id1debug=$(file $hello_file_debug | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')
id2debug=$(file $hello2_file_debug | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')
idmulti=$(file $hello_multi_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

test "$id1" = "$id1debug" || echo "id1: $id1 != id1debug: $id1debug"
test "$id2" = "$id2debug" || echo "id2: $id2 != id2debug: $id2debug"

# The build-id files should link to the .debug files.
id1file="./usr/lib/debug/.build-id/${id1:0:2}/${id1:2}"
canonid1file=$(readlink -f ${id1file})
canonfile1=$(readlink -f $hello_file)
canonid1debug=$(readlink -f ${id1file}.debug)
canondebug1=$(readlink -f $hello_file_debug)

test "$canonid1file" = "$canonfile1" \
  || echo "canonid1file: $canonid1file != $canonfile1"
test "$canonid1debug" = "$canondebug1" \
  || echo "canonid1debug: $canonid1debug != $canondebug1"

id2file="./usr/lib/debug/.build-id/${id2:0:2}/${id2:2}"
canonid2file=$(readlink -f ${id2file})
canonfile2=$(readlink -f $hello2_file)
canonid2debug=$(readlink -f ${id2file}.debug)
canondebug2=$(readlink -f $hello2_file_debug)

test "$canonid2file" = "$canonfile2" \
  || echo "canonid2: $canonid2file != $canonfile2"
test "$canonid2debug" = "$canondebug2" \
  || echo "canonid2debug: $canonid2debug" != "$canondebug2"

# Both .debug files should point to the dwz multi file.
# It would be nice to also test that they contain the correct dwz build-id
# but that is a bit hard to grep out of the section data.
multiref1=$(readelf --string-dump=.gnu_debugaltlink $hello_file_debug \
  | grep '[     0]' | cut -c13-)
multiref2=$(readelf --string-dump=.gnu_debugaltlink $hello2_file_debug \
  | grep '[     0]' | cut -c13-)

test "$multiref1" = "$multiref2" || echo "multiref: $multiref1 != $multiref2"

canonmultiref=$(readlink -f $(dirname $canondebug1)/$multiref1)
canonmultifile=$(readlink -f $hello_multi_file)

test "$canonmultiref" = "$canonmultifile" \
  || echo "canonmultiref: $canonmultiref" != "$canonmultifile"
],
[0],
[],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that rpmbuild creates no debuginfo when --nodebuginfo is passed
AT_SETUP([rpmbuild no debuginfo])
AT_KEYWORDS([build] [debuginfo])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

# Use macros.debug to generate a debuginfo package,
# but pass --nodebuginfo to skip it.
export CFLAGS="-g"
rundebug rpmbuild --quiet --nodebuginfo \
  --rebuild ${RPMDATA}/SRPMS/hello-1.0-1.src.rpm

# Extract the main package and inspect the hello binary
# It should not contain .gnu_debugdata, but the full .symtab
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-1.0-1.*.rpm | cpio -diu
test -f ./usr/local/bin/hello || exit 1
readelf -S ./usr/local/bin/hello |\
   grep -q .gnu_debugdata; test $? == 1 || exit 1
readelf -S ./usr/local/bin/hello \
  | grep -q .symtab; test $? == 0 || exit 1

# And the opposite for the debuginfo package
test ! -e ${RPMTEST}/build/RPMS/*/hello-debuginfo-1.0-1.*.rpm || exit 1
],
[0],
[],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check if rpmbuild runs dwz and generates a multi file that with shared
# debuginfo. This is simply the hello example with one binary build twice
# so dwz has enough slightly similar debug data.
# Test with unique debug file names.
AT_SETUP([rpmbuild debuginfo dwz unique debug names])
AT_KEYWORDS([build] [debuginfo] [dwz])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --define "_unique_debug_names 1" \
  -ba ${RPMDATA}/SPECS/hello2.spec

# The debuginfo package should contain a .debug file for each binary
# and a dwz multi file that contains the shared debuginfo between them.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu

hello_file_debug=./usr/lib/debug/usr/local/bin/hello-*.debug
hello2_file_debug=./usr/lib/debug/usr/local/bin/hello2-*.debug
hello_multi_file=./usr/lib/debug/.dwz/hello2-1.0-1.*
test -f $hello_file_debug || echo "no hello debug file: $hello_file_debug"
test -f $hello2_file_debug || echo "no hello2 debug file: $hello2_file_debug"
test -f $hello_multi_file || echo "no dwz multi file: $hello_multi_file"

# Make sure the main package binaries contain the correct build-ids
# linking them to the debug packages.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-1.0-1.*.rpm \
  | cpio -diu
hello_file=./usr/local/bin/hello
hello2_file=./usr/local/bin/hello2
test -f $hello_file || echo "no hello file: $hello_file"
test -f $hello2_file || echo "no hello2 file: $hello2_file"

id1=$(file $hello_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')
id2=$(file $hello2_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')
id1debug=$(file $hello_file_debug | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')
id2debug=$(file $hello2_file_debug | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')
idmulti=$(file $hello_multi_file | sed 's/.*, BuildID\[[.*\]]=\([[0-9a-f]]*\),.*/\1/')

test "$id1" = "$id1debug" || echo "id1: $id1 != id1debug: $id1debug"
test "$id2" = "$id2debug" || echo "id2: $id2 != id2debug: $id2debug"

# The build-id files should link to the .debug files.
id1file="./usr/lib/debug/.build-id/${id1:0:2}/${id1:2}"
canonid1file=$(readlink -f ${id1file})
canonfile1=$(readlink -f $hello_file)
canonid1debug=$(readlink -f ${id1file}.debug)
canondebug1=$(readlink -f $hello_file_debug)

test "$canonid1file" = "$canonfile1" \
  || echo "canonid1file: $canonid1file != $canonfile1"
test "$canonid1debug" = "$canondebug1" \
  || echo "canonid1debug: $canonid1debug != $canondebug1"

id2file="./usr/lib/debug/.build-id/${id2:0:2}/${id2:2}"
canonid2file=$(readlink -f ${id2file})
canonfile2=$(readlink -f $hello2_file)
canonid2debug=$(readlink -f ${id2file}.debug)
canondebug2=$(readlink -f $hello2_file_debug)

test "$canonid2file" = "$canonfile2" \
  || echo "canonid2: $canonid2file != $canonfile2"
test "$canonid2debug" = "$canondebug2" \
  || echo "canonid2debug: $canonid2debug" != "$canondebug2"

# Both .debug files should point to the dwz multi file.
# It would be nice to also test that they contain the correct dwz build-id
# but that is a bit hard to grep out of the section data.
multiref1=$(readelf --string-dump=.gnu_debugaltlink $hello_file_debug \
  | grep '[     0]' | cut -c13-)
multiref2=$(readelf --string-dump=.gnu_debugaltlink $hello2_file_debug \
  | grep '[     0]' | cut -c13-)

test "$multiref1" = "$multiref2" || echo "multiref: $multiref1 != $multiref2"

canonmultiref=$(readlink -f $(dirname $canondebug1)/$multiref1)
canonmultifile=$(readlink -f $hello_multi_file)

test "$canonmultiref" = "$canonmultifile" \
  || echo "canonmultiref: $canonmultiref" != "$canonmultifile"
],
[0],
[],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that an implicit suid binary get included with the suid bit set.
# We explicitly build with all debug.macros to test those helpers.
AT_SETUP([rpmbuild implicit suid binary])
AT_KEYWORDS([build] [debuginfo] [dwz] [suid])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

# Build a package that has some debuginfo
rundebug rpmbuild --quiet \
  -ba ${RPMDATA}/SPECS/hello2-suid.spec

# Unpack rpm so we can check the included binaries.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-1.0-1.*.rpm \
  | cpio -diu --quiet

# List all binaries with suid bit set (should be one, hello).
echo "suid:"
find usr -executable -type f -perm /4000
# List all binaries without suid bit set (should also be one, hello2).
echo "no-suid:"
find usr -executable -type f \! -perm /4000
],
[0],
[suid:
usr/local/bin/hello
no-suid:
usr/local/bin/hello2
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that a GDB index is included when requested.
AT_SETUP([rpmbuild debuginfo gdb index included])
AT_KEYWORDS([build] [debuginfo] [gdb])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

# Build a package that has some debuginfo
rundebug rpmbuild --quiet \
  --define "_include_gdb_index 1" \
  -ba ${RPMDATA}/SPECS/hello2.spec

# Unpack the debuginfo rpms so we can check the .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# Check that gdb-add-index has ran and a .gdb_index section has been added
readelf -S ./usr/lib/debug/usr/local/bin/hello2*.debug \
  | grep gdb_index | cut -c8-17
],
[0],
[.gdb_index
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that a GDB index is NOT included when not requested.
AT_SETUP([rpmbuild debuginfo no gdb index included])
AT_KEYWORDS([build] [debuginfo] [gdb])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

# Build a package that has some debuginfo
rundebug rpmbuild --quiet \
  --undefine "_include_gdb_index" \
  -ba ${RPMDATA}/SPECS/hello2.spec

# Unpack the debuginfo rpms so we can check the .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# Check that gdb-add-index has not ran and no .gdb_index section has been added
readelf -S ./usr/lib/debug/usr/local/bin/hello2*.debug \
  | grep gdb_index | cut -c8-17
],
[0],
[],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that a -g3 (macros) build creates a valid .debug file.
AT_SETUP([rpmbuild debuginfo -g3 .debug_macro])
AT_KEYWORDS([build] [debuginfo] [gdb])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

# Build a package that has some debuginfo generated with -g3.
# Specifically it uses -DDEBUG_DEFINE=1, which we want to see back
# in the .debug_macro section of the .debug file.
rundebug rpmbuild --quiet \
  --define "__cc gcc" \
  --define "_include_gdb_index 1" \
  -ba ${RPMDATA}/SPECS/hello-g3.spec

# Unpack the debuginfo rpms so we can check the .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello-g3-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# We are looking for a line like:
#  DW_MACRO_define_strp - lineno : 0 macro : DEBUG_DEFINE 1
readelf --debug-dump=macro ./usr/lib/debug/usr/local/bin/hello-g3*.debug \
  | grep DEBUG_DEFINE | cut -f3 -d:
],
[0],
[ DEBUG_DEFINE 1
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# ------------------------------
# Check that a debug source is in a "unique" directory when requested.
AT_SETUP([rpmbuild debuginfo unique debug src dir])
AT_KEYWORDS([build] [debuginfo])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

# Build a package that has some debuginfo
# Note that the spec defines hello2 as name, but the source is hello-1.0.
# Disable dwz to make debuginfo path rewrite checking easier.
rundebug rpmbuild --quiet \
  --undefine "_find_debuginfo_dwz_opts" \
  --define "_unique_debug_srcs 1" \
  -ba ${RPMDATA}/SPECS/hello2.spec

# Unpack the debuginfo rpms so we can check the .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# Check that the source path is "unique"
# Drop the final arch prefix to make the test arch-independent.
ls ./usr/src/debug/ | cut -f1,2 -d\.

# Check that the source path has been rewritten in the .debug file.
# Drop the final arch prefix to make the test arch-independent.
readelf --debug-dump=info ./usr/lib/debug/usr/local/bin/hello2*.debug \
  | grep comp_dir | sed -e 's/\t$//' | cut -f5- -d/ | cut -f1,2 -d\.
],
[0],
[hello2-1.0-1
hello2-1.0-1
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that a debug source is NOT in a "unique" directory when not requested.
# It will be in the "build directory" name under /usr/src/debug.
AT_SETUP([rpmbuild debuginfo no unique debug src dir])
AT_KEYWORDS([build] [debuginfo])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

# Build a package that has some debuginfo
# Note that the spec defines hello2 as name, but the source is hello-1.0.
# Disable dwz to make debuginfo path rewrite checking easier.
rundebug rpmbuild --quiet \
  --undefine "_find_debuginfo_dwz_opts" \
  --undefine "_unique_debug_srcs" \
  -ba ${RPMDATA}/SPECS/hello2.spec

# Unpack the debuginfo rpms so we can check the .debug files.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet

# Check that the source path is "unique"
ls ./usr/src/debug/

# Check that the source path has been rewritten in the .debug file.
readelf --debug-dump=info ./usr/lib/debug/usr/local/bin/hello2*.debug \
  | grep comp_dir | sed -e 's/\t$//' | cut -f5- -d/
],
[0],
[hello-1.0
hello-1.0
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that defining _debugsource_packages creates -debugsource package
AT_SETUP([rpmbuild debugsource])
AT_KEYWORDS([build] [debuginfo] [debugsource])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --define "_debugsource_packages 1" \
  -ba ${RPMDATA}/SPECS/hello2.spec

# Unpack the debugsource rpm so we can check the sources are there.
rpm2cpio ${RPMTEST}/build/RPMS/*/hello2-debugsource-1.0-1.*.rpm \
  | cpio -diu --quiet

# Check that hello.c is there.
ls ./usr/src/debug/hello2-1.0*/

# The debuginfo package should recommend the debugsource package (ignore arch).
echo -n "Recommends: "
runroot rpm -qp --recommends /build/RPMS/*/hello2-debuginfo-1.0-1.*.rpm | sed -E 's/([[-.a-z0-9]]+)\(.*\) = ([[-.0-9]]+)/\1\(ignore-arch\) = \2/'
],
[0],
[hello.c
Recommends: hello2-debugsource(ignore-arch) = 1.0-1
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that defining _debugsource_packages creates -debugsource package
# even if %install changes the working directory (debugsourcefiles.list
# should be in expected build dir).
AT_SETUP([rpmbuild debugsource debugsourcefiles.list path])
AT_KEYWORDS([build] [debuginfo] [debugsource])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --define "_debugsource_packages 1" \
  -ba ${RPMDATA}/SPECS/hello-cd.spec

# Unpack the debuginfo rpms so we can check the sources are there.
rpm2cpio ${RPMTEST}/build/RPMS/*/test-debugsource-1.0-1.*.rpm \
  | cpio -diu --quiet

# Check that hello.c is there.
ls ./usr/src/debug/test-1.0*/
],
[0],
[hello.c
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that undefining _debuginfo_subpackages creates one single -debuginfo.
AT_SETUP([rpmbuild debuginfo subpackages single])
AT_KEYWORDS([build] [debuginfo] [debugsubpackage])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --undefine "_unique_debug_names" \
  --undefine "_unique_debug_srcs" \
  --undefine "_debugsource_packages" \
  --undefine "_debuginfo_subpackages" \
  -ba ${RPMDATA}/SPECS/test-subpackages.spec

# Check that there is just one debuginfo package.
ls ${RPMTEST}/build/RPMS/*/*debuginfo*rpm | wc --lines

# Which contains hello.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello debug exists"
else
  echo "No hello: $debug_name"
fi
],
[0],
[1
hello debug exists
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that defining _debuginfo_subpackages creates multiple -debuginfos.
AT_SETUP([rpmbuild debuginfo subpackages multiple])
AT_KEYWORDS([build] [debuginfo] [debugsubpackage])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --undefine "_unique_debug_names" \
  --undefine "_unique_debug_srcs" \
  --undefine "_debugsource_packages" \
  --define "_debuginfo_subpackages 1" \
  -ba ${RPMDATA}/SPECS/test-subpackages.spec

# Check that there are 3 debuginfo packages.
ls ${RPMTEST}/build/RPMS/*/*debuginfo*rpm | wc --lines

# First contains hello.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello debug exists"
else
  echo "No hello: $debug_name"
fi

# Second contains hello2.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello2 | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello2 debug exists"
else
  echo "No hello2: $debug_name"
fi

# No debugsource package, so sources are in the main debuginfo package.
# Make sure it is recommended.
echo -n "Recommends: "
runroot rpm -qp --recommends /build/RPMS/*/test-test2-debuginfo-1.0-1.*.rpm | sed -E 's/([[-.a-z0-9]]+)\(.*\) = ([[-.0-9]]+)/\1\(ignore-arch\) = \2/'

# Third contains hello3.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-test3-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello3 | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-test3-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello3 debug exists"
else
  echo "No hello3: $debug_name"
fi

# No debugsource package, so sources are in the main debuginfo package.
# Make sure it is recommended.
echo -n "Recommends: "
runroot rpm -qp --recommends /build/RPMS/*/test-test3-debuginfo-1.0-1.*.rpm | sed -E 's/([[-.a-z0-9]]+)\(.*\) = ([[-.0-9]]+)/\1\(ignore-arch\) = \2/'
],
[0],
[3
hello debug exists
hello2 debug exists
Recommends: test-debuginfo(ignore-arch) = 1.0-1
hello3 debug exists
Recommends: test-debuginfo(ignore-arch) = 1.0-1
],
[ignore])
RPMTEST_CLEANUP
# ------------------------------
# Check that defining _debuginfo_subpackages creates multiple -debuginfos.
# With unique debug and source names
AT_SETUP([rpmbuild debuginfo subpackages multiple unique])
AT_KEYWORDS([build] [debuginfo] [debugsubpackage])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --define "_unique_debug_names 1" \
  --define "_unique_debug_srcs 1" \
  --undefine "_debugsource_packages" \
  --define "_debuginfo_subpackages 1" \
  -ba ${RPMDATA}/SPECS/test-subpackages.spec

# Check that there are 3 debuginfo packages.
ls ${RPMTEST}/build/RPMS/*/*debuginfo*rpm | wc --lines

# First contains hello.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello debug exists"
else
  echo "No hello: $debug_name"
fi

# Second contains hello2.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello2 | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello2 debug exists"
else
  echo "No hello2: $debug_name"
fi

# No debugsource package, so sources are in the main debuginfo package.
# Make sure it is recommended.
echo -n "Recommends: "
runroot rpm -qp --recommends /build/RPMS/*/test-test2-debuginfo-1.0-1.*.rpm | sed -E 's/([[-.a-z0-9]]+)\(.*\) = ([[-.0-9]]+)/\1\(ignore-arch\) = \2/'

# Third contains hello3.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-test3-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello3 | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-test3-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello3 debug exists"
else
  echo "No hello3: $debug_name"
fi

# No debugsource package, so sources are in the main debuginfo package.
# Make sure it is recommended.
echo -n "Recommends: "
runroot rpm -qp --recommends /build/RPMS/*/test-test3-debuginfo-1.0-1.*.rpm | sed -E 's/([[-.a-z0-9]]+)\(.*\) = ([[-.0-9]]+)/\1\(ignore-arch\) = \2/'
],
[0],
[3
hello debug exists
hello2 debug exists
Recommends: test-debuginfo(ignore-arch) = 1.0-1
hello3 debug exists
Recommends: test-debuginfo(ignore-arch) = 1.0-1
],
[ignore])
RPMTEST_CLEANUP
# ------------------------------
# Check that defining _debuginfo_subpackages creates multiple -debuginfos.
# Unique with debugsources.
AT_SETUP([rpmbuild debuginfo subpackages multiple unique debugsource])
AT_KEYWORDS([build] [debuginfo] [debugsubpackage] [debugsource])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --define "_unique_debug_names 1" \
  --define "_unique_debug_srcs 1" \
  --define "_debugsource_packages 1" \
  --define "_debuginfo_subpackages 1" \
  -ba ${RPMDATA}/SPECS/test-subpackages.spec

# Check that there are 3 debuginfo packages.
ls ${RPMTEST}/build/RPMS/*/*debuginfo*rpm | wc --lines

# First contains hello.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello debug exists"
else
  echo "No hello: $debug_name"
fi

# Sources are in debugsource package. Make sure it is recommended.
echo -n "Recommends: "
runroot rpm -qp --recommends /build/RPMS/*/test-debuginfo-1.0-1.*.rpm | sed -E 's/([[-.a-z0-9]]+)\(.*\) = ([[-.0-9]]+)/\1\(ignore-arch\) = \2/'

# Second contains hello2.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello2 | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello2 debug exists"
else
  echo "No hello2: $debug_name"
fi

# Sources are in debugsource package. Make sure it is recommended.
echo -n "Recommends: "
runroot rpm -qp --recommends /build/RPMS/*/test-test2-debuginfo-1.0-1.*.rpm | sed -E 's/([[-.a-z0-9]]+)\(.*\) = ([[-.0-9]]+)/\1\(ignore-arch\) = \2/'

# Third contains hello3.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-test3-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello3 | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-test3-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello3 debug exists"
else
  echo "No hello3: $debug_name"
fi

# Sources are in debugsource package. Make sure it is recommended.
echo -n "Recommends: "
runroot rpm -qp --recommends /build/RPMS/*/test-test3-debuginfo-1.0-1.*.rpm | sed -E 's/([[-.a-z0-9]]+)\(.*\) = ([[-.0-9]]+)/\1\(ignore-arch\) = \2/'
],
[0],
[3
hello debug exists
Recommends: test-debugsource(ignore-arch) = 1.0-1
hello2 debug exists
Recommends: test-debugsource(ignore-arch) = 1.0-1
hello3 debug exists
Recommends: test-debugsource(ignore-arch) = 1.0-1
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that defining _debuginfo_subpackages works with excluded files.
AT_SETUP([rpmbuild debuginfo subpackages multiple excluded])
AT_KEYWORDS([build] [debuginfo] [debugsubpackage] [debugsource])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --define "_unique_debug_names 1" \
  --define "_unique_debug_srcs 1" \
  --define "_debugsource_packages 1" \
  --define "_debuginfo_subpackages 1" \
  -ba ${RPMDATA}/SPECS/test-subpackages-exclude.spec

# Check that there are 2 debuginfo packages.
ls ${RPMTEST}/build/RPMS/*/*debuginfo*rpm | wc --lines

# First contains hello.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello debug exists"
else
  echo "No hello: $debug_name"
fi

# Second contains hello2.debug but NOT hello3.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello2 | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello2 debug exists"
else
  echo "No hello2: $debug_name"
fi

if test -f ./usr/lib/debug/bin/hello3*; then
  echo "hello3 debug exists"
else
  echo "No hello3 debug"
fi
],
[0],
[2
hello debug exists
hello2 debug exists
No hello3 debug
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that defining _debuginfo_subpackages works with RemovePathPostfixes.
AT_SETUP([rpmbuild debuginfo subpackages multiple excluded])
AT_KEYWORDS([build] [debuginfo] [debugsubpackage] [debugsource])
AT_SKIP_IF([$DEBUGINFO_DISABLED])
RPMTEST_CHECK([
RPMDB_INIT

rundebug rpmbuild --quiet \
  --define "_unique_debug_names 1" \
  --define "_unique_debug_srcs 1" \
  --define "_debugsource_packages 1" \
  --define "_debuginfo_subpackages 1" \
  -ba ${RPMDATA}/SPECS/test-subpackages-pathpostfixes.spec

# Check that there are 2 debuginfo packages.
ls ${RPMTEST}/build/RPMS/*/*debuginfo*rpm | wc --lines

# First contains hello.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello debug exists"
else
  echo "No hello: $debug_name"
fi

# remove it, we are going to check the other debuginfo package.
rm ./bin/hello
rm ./usr/lib/debug/bin/$debug_name
orig_debugname=$debugname

# Second contains hello.foobar.debug but NOT hello.debug
rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-1.0-1.*.rpm \
  | cpio -diu --quiet
# Extract the debug name from the exe (.gnu_debuglink section, first string)
debug_name=$(readelf -p .gnu_debuglink ./bin/hello | grep hello | cut -c13-)

rpm2cpio ${RPMTEST}/build/RPMS/*/test-test2-debuginfo-1.0-1.*.rpm \
  | cpio -diu --quiet
if test -f ./usr/lib/debug/bin/$debug_name; then
  echo "hello.foobar debug exists"
else
  echo "No hello2: $debug_name"
fi

if test -f ./usr/lib/debug/bin/$orig_debugname; then
  echo "$orig_debugname exists"
else
  echo "No hello.debug"
fi
],
[0],
[2
hello debug exists
hello.foobar debug exists
No hello.debug
],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([manual debuginfo package])
AT_KEYWORDS([build debuginfo])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild --quiet -bb \
		--define "_prefix /usr/local" \
		/data/SPECS/hello-debuginfo.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([

runroot rpm -qpl /build/RPMS/*/hello-1.0-1.*.rpm
runroot rpm -qpl /build/RPMS/*/hello-debuginfo-1.0-1.*.rpm
],
[0],
[/usr/local/bin/hello
/usr/local/share/doc/hello-1.0
/usr/local/share/doc/hello-1.0/FAQ
/usr/lib/debug/test.txt
],
[ignore])

RPMTEST_CHECK([
runroot rpmbuild --quiet -bb \
		--define "_prefix /usr/local" \
		--without customdebug \
		--define "_enable_debug_packages 0" \
		/data/SPECS/hello-debuginfo.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([
runroot rpmbuild --quiet -bb \
		--define "_prefix /usr/local" \
		--without customdebug \
		/data/SPECS/hello-debuginfo.spec
],
[1],
[ignore],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([explicit %_enable_debug_package on noarch])
AT_KEYWORDS([build debuginfo])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild --quiet -bb \
		--define "%_enable_debug_packages 1" \
		/data/SPECS/hlinktest.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([
# mock uses --target <hostarch> everywhere, even noarch packages
target=$(runroot rpm --eval "%{_host_cpu}")
runroot rpmbuild --quiet -bb \
		--target ${target} \
		--define "%_enable_debug_packages 1" \
		/data/SPECS/hlinktest.spec
],
[0],
[ignore],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check dynamic build requires
AT_SETUP([dynamic build requires rpmbuild -bs])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild \
  --quiet -bs /data/SPECS/buildrequires.spec
runroot rpm -qpR /build/SRPMS/buildrequires-1.0-1.src.rpm
],
[0],
[rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(DynamicBuildRequires) <= 4.15.0-1
rpmlib(FileDigests) <= 4.6.0-1
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check dynamic build requires
# Mimick what mock does: repeat build with --noprep --noclean
# until -br succeeds, and finally build the package.
AT_SETUP([rpmbuild -br])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild \
  -br  --quiet /data/SPECS/buildrequires.spec
],
[11],
[],
[error: Failed build dependencies:
	(bar = 3.4 or bar = 3.5) is needed by buildrequires-1.0-1.noarch
	foo > 1.3 is needed by buildrequires-1.0-1.noarch
	foo-bar = 2.0 is needed by buildrequires-1.0-1.noarch
],
)

RPMTEST_CHECK([
runroot rpmbuild \
    --define "provs bar foo foo-bar" \
    --define "pkg foo" \
    -bb --quiet /data/SPECS/deptest.spec

runroot rpm -U /build/RPMS/noarch/deptest-foo-1.0-1.noarch.rpm

runroot rpmbuild \
    -br --noprep --noclean\
    --quiet /data/SPECS/buildrequires.spec
],
[0],
[],
[])
RPMTEST_CLEANUP

# Test that -br creates an src.rpm on success
AT_SETUP([rpmbuild -br success])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild \
  -br  /data/SPECS/mini.spec
],
[0],
[Wrote: /build/SRPMS/mini-1-1.src.rpm
],
[],
)
RPMTEST_CLEANUP

# ------------------------------
# Check dynamic build requires
AT_SETUP([rpmbuild -bd with errors])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild \
  -bd  --quiet /data/SPECS/buildrequires.spec
],
[11],
[],
[error: Failed build dependencies:
	(bar = 3.4 or bar = 3.5) is needed by buildrequires-1.0-1.noarch
	foo > 1.3 is needed by buildrequires-1.0-1.noarch
	foo-bar = 2.0 is needed by buildrequires-1.0-1.noarch
],
)
RPMTEST_CLEANUP

# ------------------------------
# Check if rpmbuild -bd *.spec doesn't build anything
AT_SETUP([rpmbuild -bd *.spec])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

run rpmbuild \
  --quiet -bd ${RPMDATA}/SPECS/hello.spec
ls ls ${RPMTEST}/build/*/*.rpm | wc --lines
],
[0],
[0
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check dynamic build requires
AT_SETUP([rpmbuild -ba])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild \
  -ba  --quiet /data/SPECS/buildrequires.spec
],
[11],
[],
[error: Failed build dependencies:
	(bar = 3.4 or bar = 3.5) is needed by buildrequires-1.0-1.noarch
	foo > 1.3 is needed by buildrequires-1.0-1.noarch
	foo-bar = 2.0 is needed by buildrequires-1.0-1.noarch
],
)
RPMTEST_CLEANUP


# ------------------------------
# Check dynamic build requires
AT_SETUP([rpmbuild -br --nodeps])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild \
  -br  --quiet --nodeps /data/SPECS/buildrequires.spec
runroot rpm -qpvR /build/SRPMS/buildrequires-1.0-1.buildreqs.nosrc.rpm
],
[0],
[auto: (bar = 3.4 or bar = 3.5)
auto: foo > 1.3
auto: foo-bar = 2.0
rpmlib: rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib,missingok: rpmlib(DynamicBuildRequires) <= 4.15.0-1
rpmlib: rpmlib(FileDigests) <= 4.6.0-1
rpmlib: rpmlib(RichDependencies) <= 4.12.0-1
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check that rpmbuild aborts with missing Source
AT_SETUP([rpmbuild -ba missing source])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK_UNQUOTED([
runroot rpmbuild \
  --define "_sourcedir /notthere" \
  --quiet -bb /data/SPECS/hello.spec 2> cmd.err
ec=$?
grep "^error: File" cmd.err >&2
exit ${ec}
],
[1],
[],
[error: File /notthere/hello-1.0.tar.gz: No such file or directory
])

RPMTEST_CHECK_UNQUOTED([
runroot rpmbuild \
  --define "_sourcedir /notthere" \
  --quiet -bs /data/SPECS/hello.spec
],
[1],
[],
[error: Bad file: /notthere/hello-1.0-modernize.patch: No such file or directory
error: Bad file: /notthere/hello-1.0.tar.gz: No such file or directory
])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild minimal spec])
AT_KEYWORDS([build])
RPMTEST_CHECK_UNQUOTED([
RPMDB_INIT

runroot rpmbuild \
  -bb --quiet /data/SPECS/mini.spec
],
[0],
[],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild missing files])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT

runroot rpmbuild -bb --quiet /data/SPECS/filemiss.spec
],
[1],
[],
[error: File not found: /build/BUILD/filemisstest-1.0-build/BUILDROOT/opt/share/doc/filemisstest-1.0/CREDITS
error: File not found: /build/BUILD/filemisstest-1.0-build/BUILDROOT/opt/foo
error: File not found: /build/BUILD/filemisstest-1.0-build/BUILDROOT/opt/bar{a,b}
cp: cannot stat '/build/BUILD/filemisstest-1.0-build/INSTALL': No such file or directory
cp: cannot stat '/build/BUILD/filemisstest-1.0-build/README*': No such file or directory
error: File not found: /build/BUILD/filemisstest-1.0-build/BUILDROOT/opt/share/doc/filemisstest-1.0/INSTALL
error: File not found: /build/BUILD/filemisstest-1.0-build/BUILDROOT/opt/share/doc/filemisstest-1.0/README*
cp: cannot stat '/build/BUILD/filemisstest-1.0-build/LICENSE': No such file or directory
cp: cannot stat '/build/BUILD/filemisstest-1.0-build/OTHERLICENSE?': No such file or directory
error: File not found: /build/BUILD/filemisstest-1.0-build/BUILDROOT/opt/share/licenses/filemisstest-1.0/LICENSE
error: File not found: /build/BUILD/filemisstest-1.0-build/BUILDROOT/opt/share/licenses/filemisstest-1.0/OTHERLICENSE?
],
)
RPMTEST_CLEANUP

AT_SETUP([rpmbuild missing doc])
AT_KEYWORDS([build])
RPMTEST_CHECK_UNQUOTED([
RPMDB_INIT

for val in 1 0; do
    runroot rpmbuild \
        -bb --quiet \
        --define "_missing_doc_files_terminate_build ${val}" \
        /data/SPECS/docmiss.spec
    echo $?
done
],
[],
[1
0
],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([spec conditionals])
AT_KEYWORDS([if else elif build])
RPMDB_INIT

# %if, %else, %elif test basic
RPMTEST_CHECK([
runroot rpmbuild -ba --quiet \
    --define "testif 0"      \
   /data/SPECS/iftest.spec
],
[0],
[])

# %if, %else, %elif test existing script
RPMTEST_CHECK([
runroot rpmbuild -ba --quiet  \
    --define "testif 2"       \
    --define "fedora 1"       \
   /data/SPECS/iftest.spec
],
[0],
[])

RPMTEST_CHECK([
runroot rpmbuild -ba --quiet \
 --define "testif 1"         \
 --define "variable1 1"      \
 --define "variable2 1"      \
 --define "variable3 1"      \
 --define "action1 echo 0"   \
 --define "action2 %if 1"    \
 --define "action3 %if 1"    \
 --define "action4 %if 1"    \
   /data/SPECS/eliftest.spec
],
[0],
[])

RPMTEST_CHECK([
runroot rpmbuild -ba --quiet   \
 --define "testif 1"           \
 --define "variable1 0"        \
 --define "variable2 0"        \
 --define "variable3 1"        \
 --define "action1 echo 0"     \
 --define "action2 echo 0"     \
 --define "action3 %if 1"      \
 --define "action4 echo 0"     \
   /data/SPECS/eliftest.spec > /dev/null 2>&1
],
[1],
[])

RPMTEST_CHECK([
runroot rpmbuild -ba --quiet      \
 --define "testif 1"              \
 --define "variable1 0"           \
 --define "variable2 1"           \
 --define "variable3 rubbish:4-3" \
 --define "action1 %if 1"         \
 --define "action2 echo 0"        \
 --define "action3 %if 1"         \
 --define "action4 %if 1"         \
   /data/SPECS/eliftest.spec
],
[0],
[])

RPMTEST_CHECK([
runroot rpmbuild -ba --quiet      \
 --define "testif 1"              \
 --define "variable1 0"           \
 --define "variable2 rubbish:4-3" \
 --define "variable3 1"           \
 --define "action1 exit 1"        \
 --define "action2 echo 0"        \
 --define "action3 exit 1"        \
 --define "action4 exit 1"        \
   /data/SPECS/eliftest.spec
],
[1],
[],
[error: bare words are no longer supported, please use "...":  rubbish:4-3

error:                                                        ^
error: /data/SPECS/eliftest.spec:40: bad %elif condition:  rubbish:4-3

])

# multiline %if test
RPMTEST_CHECK([
runroot rpmbuild -ba --quiet      \
 /data/SPECS/ifmultiline.spec
],
[0],
[],
[])
RPMTEST_CLEANUP

AT_SETUP([bcond macro])
AT_KEYWORDS([bcond build])
RPMDB_INIT

# basic bcond behavior with --eval
RPMTEST_CHECK([
runroot rpm \
	--eval "%bcond normally_on 1" \
	--eval "%bcond normally_off 0" \
	--eval "%bcond both_features %[[%{with normally_on} && %{with normally_off}]]" \
	--eval "%{with normally_on}" \
	--eval "%{with normally_off}" \
	--eval "%{with both_features}"
],
[0],
[


1
0
0
],
[])

# bcond behavior, without CLI options
RPMTEST_CHECK([
runroot rpmbuild -bb --quiet /data/SPECS/bcondtest.spec
runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
    grep has_bcond | sort
],
[0],
[has_bcond(normally_on)
],
[])

# bcond behavior, --with
RPMTEST_CHECK([
runroot rpmbuild -bb --quiet --with normally_on --with normally_off \
    /data/SPECS/bcondtest.spec
runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
    grep has_bcond | sort
],
[0],
[has_bcond(both_features)
has_bcond(normally_off)
has_bcond(normally_on)
],
[])

# bcond behavior, --without
RPMTEST_CHECK([
runroot rpmbuild -bb --quiet --without normally_on --without normally_off \
    /data/SPECS/bcondtest.spec
runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
    grep has_bcond | sort
],
[0],
[],
[])

# bcond behavior, CLI overriding a complex default
RPMTEST_CHECK([
runroot rpmbuild -bb --quiet --with both_features /data/SPECS/bcondtest.spec
runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
    grep has_bcond | sort
],
[0],
[has_bcond(both_features)
has_bcond(normally_on)
],
[])
RPMTEST_CLEANUP

AT_SETUP([bcond_override_default macros])
AT_KEYWORDS([bcond build])
RPMDB_INIT

# check bcond_override_default by defining
RPMTEST_CHECK([
runroot rpm \
	--eval "%bcond_override_default normally_on 0" \
	--eval "%bcond_override_default normally_off 1" \
	--eval "%bcond normally_on 1" \
	--eval "%bcond normally_off 0" \
	--eval "%{with normally_on}" \
	--eval "%{with normally_off}" \
],
[0],
[



0
1
],
[])

# check bcond_override_default via helper macro
RPMTEST_CHECK([
runroot rpm \
	-D "%bcond_override_default_normally_on 0" \
	-D "%bcond_override_default_normally_off 1" \
	--eval "%bcond normally_on 1" \
	--eval "%bcond normally_off 0" \
	--eval "%{with normally_on}" \
	--eval "%{with normally_off}" \
],
[0],
[

0
1
],
[])

# check bcond_override_default via macro file

# bcond behavior, without CLI options
RPMTEST_CHECK([
echo '%bcond_override_default_normally_on 0' > ${RPMTEST}/${RPM_CONFIGDIR_PATH}/macros.d/macros.bcondtests
echo '%bcond_override_default_normally_off 1' >> ${RPMTEST}/${RPM_CONFIGDIR_PATH}/macros.d/macros.bcondtests
runroot rpmbuild -bb --quiet /data/SPECS/bcondtest.spec
runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
    grep has_bcond | sort
],
[0],
[has_bcond(normally_off)
],
[])

# bcond behavior, --with
RPMTEST_CHECK([
runroot rpmbuild -bb --quiet --with normally_on --with normally_off \
    /data/SPECS/bcondtest.spec
runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
    grep has_bcond | sort
],
[0],
[has_bcond(both_features)
has_bcond(normally_off)
has_bcond(normally_on)
],
[])

# bcond behavior, --without
RPMTEST_CHECK([
runroot rpmbuild -bb --quiet --without normally_on --without normally_off \
    /data/SPECS/bcondtest.spec
runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
    grep has_bcond | sort
],
[0],
[],
[])

# bcond behavior, CLI overriding a complex default
RPMTEST_CHECK([
runroot rpmbuild -bb --quiet --with both_features /data/SPECS/bcondtest.spec
runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
    grep has_bcond | sort
],
[0],
[has_bcond(both_features)
has_bcond(normally_off)
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild spec tag])
AT_KEYWORDS([build])
RPMTEST_CHECK([
RPMDB_INIT
RPMTEST_SETUP
runroot rpmbuild -bs --quiet /data/SPECS/foo.spec
runroot rpm -qp --qf "%{spec}" /build/SRPMS/foo-1.0-1.src.rpm
],
[0],
[

Summary: foo
Name: foo
Version: 1.0
Release: 1
Source: hello-2.0.tar.gz
Patch1: hello-1.0-modernize.patch
Group: Testing
License: GPLv2+
BuildArch: noarch

%description
Simple rpm demonstration.

%package sub
Summary: foo
Requires: foo = 1.0-1

%description sub
foo

%package bus
Summary: foo
Requires: foo = 1.0-1

%description bus
foo

%files

%files sub

],
[])
RPMTEST_CLEANUP

# ------------------------------
# Check %sources and %patches with weird file names
AT_SETUP([%sources and %patches])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild \
  -bp /data/SPECS/hello-sources.spec | grep -v Executing
],
[0],
[/build/SOURCES/hello-1.0 %tar.gz
/build/SOURCES/hello-addon ! tar\.gz
/build/SOURCES/hello-1.0-m<o>dernize&renew.patch
/build/SOURCES/hello-1.0-inst(a);;.patch
],
[ignore])
RPMTEST_CLEANUP

# ------------------------------
# Check if dynamic spec generation works
AT_SETUP([rpmbuild with dynamic spec generation])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --define "_prefix /usr/local" -ba /data/SPECS/dynamic.spec
],
[0],
[ignore],
[ignore])

RPMTEST_CHECK([

runroot rpm -qp --qf "%{Summary}\n" /build/RPMS/noarch/dynamic-docs-1.0-1.noarch.rpm
runroot rpm -ql /build/RPMS/noarch/dynamic-docs-1.0-1.noarch.rpm
],
[0],
[Documentation for dynamic spec
/usr/local/share/doc/dynamic-docs-1.0
/usr/local/share/doc/dynamic-docs-1.0/FAQ
],
[])
RPMTEST_CLEANUP

# ------------------------------
# Check if dynamic spec generation works for main package, too
# Fails now as feature is disbled
AT_SETUP([rpmbuild with dynamic spec generation for main package])
AT_KEYWORDS([build])
AT_XFAIL_IF([test $RPM_XFAIL -ne 0])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --define "_prefix /usr/local" -D "FULLDYNAMIC 1" -ba /data/SPECS/dynamic.spec
],
[0],
[ignore],
[ignore])

AT_XFAIL_IF([test $RPM_XFAIL -ne 0])
RPMTEST_CHECK([

runroot rpm -qp --qf "%{Summary}\n" /build/RPMS/noarch/dynamic-docs-1.0-1.noarch.rpm
runroot rpm -ql /build/RPMS/noarch/dynamic-docs-1.0-1.noarch.rpm
],
[0],
[Documentation for dynamic spec
/usr/local/share/doc/dynamic-docs-1.0
/usr/local/share/doc/dynamic-docs-1.0/FAQ
],
[])
RPMTEST_CLEANUP

# ------------------------------
# Check if dynamic spec generation works for main package, too
# Check for failure as feature is disabled. Remove tests when enabled
AT_SETUP([rpmbuild with dynamic spec generation for main package])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --define "_prefix /usr/local" -D "FULLDYNAMIC 1" -ba /data/SPECS/dynamic.spec
],
[1],
[],
[error: Summary field must be present in package: dynamic
error: License field must be present in package: dynamic
])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild with dynamic spec generation for main package])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --define "_prefix /usr/local" -D "FULLDYNAMIC 1" -bs /data/SPECS/dynamic.spec
],
[1],
[],
[error: Summary field must be present in package: dynamic
error: License field must be present in package: dynamic
])
RPMTEST_CLEANUP

# ------------------------------
# Check failing dynamic spec generation
AT_SETUP([rpmbuild with dynamic spec generation fail])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --quiet -D "FAIL 1" -ba /data/SPECS/dynamic.spec
],
[1],
[],
[error: Summary field must be present in package: dynamic-docs
error: parsing failed
])

RPMTEST_CLEANUP

# ------------------------------
# Check failing dynamic spec generation
AT_SETUP([rpmbuild with dynamic spec generation fail])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --quiet -D "DOUBLESUMMARY 1" -ba /data/SPECS/dynamic.spec
],
[0],
[],
[warning: line 1: second Summary
])

RPMTEST_CLEANUP

# ------------------------------
# Check failing dynamic spec generation
AT_SETUP([rpmbuild with dynamic spec generation fail])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --quiet -D "WRONGTAG 1" -ba /data/SPECS/dynamic.spec
],
[1],
[],
[error: line 1: Unknown tag: LicenseToKill: True
error: parsing failed
])

RPMTEST_CLEANUP

# ------------------------------
# Check failing dynamic spec generation
AT_SETUP([rpmbuild with dynamic spec generation fail])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --quiet -D "FORBIDDENSECTION 1" -ba /data/SPECS/dynamic.spec
],
[1],
[],
[error: Section %check is not allowed after build is done!
error: parsing failed
])

RPMTEST_CLEANUP

# ------------------------------
# Check failing dynamic spec generation
AT_SETUP([rpmbuild with dynamic spec generation fail])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild --quiet -D "FORBIDDENTAG 1" -ba /data/SPECS/dynamic.spec
],
[1],
[],
[error: line 4: Tag not allowed after build is done: BuildRequires: python3
error: parsing failed
])

RPMTEST_CLEANUP


# ------------------------------
# Check source name with space
AT_SETUP([rpmbuild source name with space])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([

runroot rpmbuild -bp --quiet /data/SPECS/source_space.spec
],
[0],
[],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild bad filetrigger condition])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK([
rpmspec --parse /data/SPECS/badftrigger.spec
],
[1],
[],
[error: file trigger conditions must begin with '/': %{nosuchmacro}
])

RPMTEST_CHECK([
rpmspec --define "nosuchmacro aaa" --parse /data/SPECS/badftrigger.spec
],
[1],
[],
[error: file trigger conditions must begin with '/': aaa
])

RPMTEST_CHECK([
rpmspec --define "nosuchmacro /bbb" --parse /data/SPECS/badftrigger.spec
],
[0],
[ignore],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild interactive])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK([
runroot rpmbuild --quiet -bb /data/SPECS/interact.spec
],
[1],
[ignore],
[ignore])

RPMTEST_CHECK([
runroot rpmbuild --quiet -bp /data/SPECS/stdin.spec
],
[0],
[ignore],
[ignore])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild bpf file])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK([
runroot rpmbuild --quiet -bb /data/SPECS/bpf.spec
],
[0],
[],
[])
RPMTEST_CLEANUP

# XXX this always succeeds when rpmbuild is run as root so the test is
# kinda meaningless until #3005 is done, but works as a manual reproducer.
AT_SETUP([rpmbuild non-removable file in builddir])
AT_KEYWORDS([build])
RPMDB_INIT

# -bi doesn't run cleanup, test that we manage that situation too
RPMTEST_CHECK([
runroot rpmbuild --quiet -bi /data/SPECS/noperms.spec
runroot rpmbuild --quiet -bb /data/SPECS/noperms.spec
],
[0],
[],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild %caps])
AT_KEYWORDS([build])
AT_SKIP_IF([$CAP_DISABLED])
RPMDB_INIT

RPMTEST_CHECK([
runroot rpmbuild --quiet -bb /data/SPECS/caps.spec
],
[0],
[],
[])

RPMTEST_CHECK([
runroot rpm -qp --qf "%{name}\n" /build/RPMS/*/*.rpm|sort
],
[0],
[caps
],
[])

RPMTEST_CHECK([
runroot rpmbuild --quiet -bb --define "buildsubdir ." /data/SPECS/caps.spec
],
[0],
[],
[])

RPMTEST_CHECK([
runroot rpm -qp --qf "%{name}\n" /build/RPMS/*/*.rpm|sort
],
[0],
[caps
caps-debuginfo
],
[])
RPMTEST_CHECK([
runroot rpm -q --filecaps /build/RPMS/*/caps-1.0-1.*.rpm | grep -v build-id
],
[0],
[/usr/bin/test	cap_net_raw=p
],
[])

RPMTEST_CHECK([
runroot rpm -U --nodeps /build/RPMS/*/caps-1.0-1.*.rpm
runroot_other /usr/sbin/getcap /usr/bin/test
],
[0],
[/usr/bin/test cap_net_raw=p]
[])

RPMTEST_CLEANUP

AT_SETUP([rpmbuild -bs with source macro])
AT_KEYWORDS([build])
RPMDB_INIT

RPMTEST_CHECK([
runroot rpmbuild --quiet -bs /data/SPECS/macrosource.spec
],
[0],
[],
[])

RPMTEST_CHECK([
runroot rpm -ql /build/SRPMS/macrosource-0-0.src.rpm
],
[0],
[hello-1.0-install.patch
hello-2.0.tar.gz
macrosource.spec
],
[])

RPMTEST_CHECK([
runroot rpm -q --qf "[%{SOURCE}\n]" --qf "[%{PATCH}\n]" /build/SRPMS/macrosource-0-0.src.rpm
],
[0],
[hello-2.0.tar.gz
hello-1.0-install.patch
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild target macro sanity])
AT_KEYWORDS([build])
RPMDB_INIT

# This is surprisingly tricky - we need to place these macros in a place
# that is after the platform stuff in macro path so we can't just drop
# into macros.d, and then we need to take cmake prefix into account,
# escape \'s and $'s and whatnot. Pff. Would be easier to place to
# a macro file, but this is so tightly coupled that seems saner to
# have it all here.
etcdir=$(runroot rpm --eval "%_sysconfdir")
mkdir -p ${RPMTEST}/${etcdir}/rpm/

cat << EOF >> ${RPMTEST}/${etcdir}/rpm/macros.template
%__spec_build_pre\\
%{___build_pre_env}\\
echo BUILD_PRE \${RPM_ARCH} %{_arch} %{_target_cpu} >> /tmp/build.out\\
%{nil}

%__spec_build_post\\
echo BUILD_POST \${RPM_ARCH} %{_arch} %{_target_cpu} >> /tmp/build.out\\
%{nil}
EOF

RPMTEST_CHECK([
runroot_other rm -f /tmp/build.out
runroot rpmbuild --quiet \
	-bc /data/SPECS/templ.spec
runroot_other cat /tmp/build.out
],
[0],
[BUILD_PRE noarch noarch noarch
BUILD noarch noarch noarch
BUILD_POST noarch noarch noarch
],
[])

RPMTEST_CHECK([
runroot_other rm -f /tmp/build.out
runroot rpmbuild --quiet \
	--target noarch \
	-bc /data/SPECS/templ.spec
runroot_other cat /tmp/build.out
],
[0],
[BUILD_PRE noarch noarch noarch
BUILD noarch noarch noarch
BUILD_POST noarch noarch noarch
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild install_pre override])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
runroot rpmbuild -bb /data/SPECS/install-hack.spec
],
[0],
[ignore],
[ignore])
RPMTEST_CLEANUP

# Mimick what cpack does. It's not how rpmbuild wants to be used, so
# please don't copy this to any new usages.
AT_SETUP([rpmbuild --buildroot])
AT_KEYWORDS([build])

RPMDB_INIT
RPMTEST_CHECK([
runroot_other mkdir -p /tmp/buildroot/out
runroot_other touch /tmp/buildroot/out/sider
runroot rpmbuild -bb --quiet \
	--buildroot /tmp/buildroot \
	/data/SPECS/brtest.spec
],
[0],
[],
[])

RPMTEST_CHECK([
runroot rpm -qpl /build/RPMS/noarch/brtest-1.0-1.noarch.rpm
],
[0],
[/out/sider
],
[])
RPMTEST_CLEANUP

AT_SETUP([builddir])
AT_KEYWORDS([build builddir])

RPMDB_INIT
# Trick for compatibly getting the builddir out of old and new rpm
RPMTEST_CHECK([
runroot rpmbuild \
	--with weirdver \
	-bc --short-circuit --define '__spec_build_pre echo builddir=%{_builddir}; exit 0' /data/SPECS/weirdnames.spec 2>&1 | awk /^builddir/
],
[0],
[builddir=/build/BUILD/weirdnames-1.0_aa_bb-build
],
[])

# Test for %{_builddir} related %global misuse workaround
RPMTEST_CHECK([
runroot rpmbuild -bc /data/SPECS/mydir.spec 2>&1 | awk /^mydir/
],
[0],
[mydir1=/build/BUILD/mydir-1.0-build/mydir-1.0
mydir2=/build/BUILD/mydir-1.0-build/mydir-1.0
],
[])
RPMTEST_CLEANUP
