Skip to content

Commit

Permalink
Generalize and extend unit tests working with binary files
Browse files Browse the repository at this point in the history
* Fix shlib_list_from_elf_hints to work with FreeBSD files on non-FreeBSD systems.
* Introduce unit test helper function to describe golden values of bundled test binaries.
* Simplify abi unit test to use helper
* Introduce create-parsebin unit test that packages binaries and validates their manifest.
  • Loading branch information
Keve committed Dec 1, 2024
1 parent 2cbdc3b commit cf7a4ed
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 170 deletions.
2 changes: 1 addition & 1 deletion libpkg/elfhints.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ int
shlib_list_from_elf_hints(const char *hintsfile)
{
if (ctx.oi->ostype == OS_FREEBSD || ctx.oi->ostype == OS_DRAGONFLY)
read_elf_hints(hintsfile, 1);
read_elf_hints(hintsfile, false);

return (scan_dirs_for_shlibs(&shlibs, ndirs, dirs, true));
}
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile.autosetup
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TESTS_SH= \
frontend/conflicts.sh \
frontend/conflicts-multirepo.sh \
frontend/create.sh \
frontend/create-parsebin.sh \
frontend/delete.sh \
frontend/extract.sh \
frontend/fetch.sh \
Expand Down
202 changes: 33 additions & 169 deletions tests/frontend/abi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,180 +61,44 @@ override_body() {
elfparse_body() {
# ELF parsing now works across platforms

_expected="FreeBSD:14:aarch64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-aarch64.bin config abi

_expected="freebsd:14:aarch64:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-aarch64.bin config altabi

_expected="FreeBSD:14:amd64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-amd64.bin config abi

_expected="freebsd:14:x86:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-amd64.bin config altabi

_expected="FreeBSD:13:armv6\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-armv6.bin config abi

_expected="freebsd:13:armv6:32:el:eabi:hardfp\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-armv6.bin config altabi

_expected="FreeBSD:14:armv7\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-armv7.bin config abi

_expected="freebsd:14:armv7:32:el:eabi:hardfp\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-armv7.bin config altabi

_expected="FreeBSD:14:i386\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-i386.bin config abi

_expected="freebsd:14:x86:32\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-i386.bin config altabi

_expected="FreeBSD:14:powerpc\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-powerpc.bin config abi

_expected="freebsd:14:powerpc:32:eb\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-powerpc.bin config altabi

_expected="FreeBSD:14:powerpc64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-powerpc64.bin config abi

_expected="freebsd:14:powerpc:64:eb\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-powerpc64.bin config altabi

_expected="FreeBSD:14:powerpc64le\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-powerpc64le.bin config abi

_expected="freebsd:14:powerpc:64:el\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-powerpc64le.bin config altabi

_expected="FreeBSD:14:riscv64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-riscv64.bin config abi

_expected="freebsd:14:riscv:64:hf\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/freebsd-riscv64.bin config altabi

_expected="dragonfly:5.10:x86:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/dfly.bin config abi

_expected="dragonfly:5.10:x86:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/dfly.bin config altabi

_expected="Linux:3.2:x86_64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/linux.bin config abi

_expected="linux:3.2:x86_64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/linux.bin config altabi
for bin in \
freebsd-aarch64.bin freebsd-amd64.bin freebsd-armv6.bin freebsd-armv7.bin \
freebsd-i386.bin freebsd-powerpc.bin freebsd-powerpc64.bin freebsd-powerpc64le.bin \
freebsd-riscv64.bin dfly.bin linux.bin
do
bin_meta ${bin}

_expected="${XABI}\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/${bin} config abi

_expected="${XALTABI}\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/${bin} config altabi
done
}

machoparse_body() {
# Macho-O parsing now works across platforms
_expected="Darwin:24:aarch64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macos.bin config abi

_expected="darwin:24:aarch64:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macos.bin config altabi

_expected="Darwin:10:amd64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macos106.bin config abi

_expected="darwin:10:x86:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macos106.bin config altabi

_expected="Darwin:24:amd64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macos150.bin config abi

_expected="darwin:24:x86:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macos150.bin config altabi

# macosfat.bin has amd64 as its first entry
_expected="Darwin:24:amd64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macosfat.bin config abi

_expected="darwin:24:x86:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macosfat.bin config altabi

# explicitely select an existing fat entry
_expected="Darwin:24:amd64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macosfat.bin#amd64 config abi

_expected="darwin:24:x86:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macosfat.bin#amd64 config altabi

_expected="Darwin:24:aarch64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macosfat.bin#aarch64 config abi

_expected="darwin:24:aarch64:64\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/macosfat.bin#aarch64 config altabi
for bin in \
macos.bin macos106.bin macos150.bin macosfat.bin \
"macosfat.bin#amd64" "macosfat.bin#aarch64"
do
bin_meta ${bin}

_expected="${XABI}\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/${bin} config abi

_expected="${XALTABI}\n"
atf_check \
-o inline:"${_expected}" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=$(atf_get_srcdir)/${bin} config altabi
done

# explicitely select a fat entry that is not in the ABI_FILE
_expected="Scanned 2 entries, found none matching selector i386\n"
Expand Down
157 changes: 157 additions & 0 deletions tests/frontend/create-parsebin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#! /usr/bin/env atf-sh

. $(atf_get_srcdir)/test_environment.sh

tests_init \
create_from_bin \
create_from_binbase

genmanifest() {
local PKG_NAME="$1"
shift
local PKG_FLATSIZE=0
local PKG_FILES=""
local PKG_SHA256=""
local NL="
"

bin_meta "$1"
while [ -n "$1" ]; do
local file1="${1%#*}"
local file1_base=$(basename ${file1})
local file1_size=$(wc -c < ${file1})
local file1_sha=$(openssl dgst -sha256 -hex ${file1} | sed -nE 's/.*=[[:space:]]*([[:xdigit:]]+)/\1/p')
cp -a ${file1} ${TMPDIR}/${file1_base}

PKG_FILES="${PKG_FILES}/${file1_base}: {perm: 0644}${NL}"
PKG_SHA256="${PKG_SHA256}${NL} /${file1_base} = \"1\$${file1_sha}\";"

PKG_FLATSIZE=$((${PKG_FLATSIZE}+${file1_size}))
shift
done

cat << EOF > ${PKG_NAME}.manifest
name: ${PKG_NAME}
origin: ${PKG_NAME}
version: 1
maintainer: test
categories: [test]
comment: a test
www: http://test
prefix: /
desc: <<EOD
Yet another test
EOD
files: {
${PKG_FILES}
}
EOF

cat << EOF > ${PKG_NAME}.expected
name = "${PKG_NAME}";
origin = "${PKG_NAME}";
version = "1";
comment = "a test";
maintainer = "test";
www = "http://test";
abi = "${XABI}";
arch = "${XALTABI}";
prefix = "/";
flatsize = ${PKG_FLATSIZE};
desc = "Yet another test";
categories [
"test",
]
EOF
if [ x"${ALLOW_BASE_SHLIBS}" = xyes -a -n "${Xshlibs_required_base}" ]; then
echo "shlibs_required: [" >> ${PKG_NAME}.expected
for i in ${Xshlibs_required_base}; do
echo ${NL}" "\"$i\" >> ${PKG_NAME}.expected
done
echo "]" >> ${PKG_NAME}.expected
fi

if [ -n "${XFreeBSD_version}" ]; then
cat << EOF >> ${PKG_NAME}.expected
annotations {
FreeBSD_version = "${XFreeBSD_version}";
}
EOF
fi

cat << EOF >> ${PKG_NAME}.expected
files {${PKG_SHA256}
}
EOF
}


create_from_bin_body() {
local PKG_NAME=testbin

for bin in \
freebsd-aarch64.bin freebsd-amd64.bin freebsd-armv6.bin freebsd-armv7.bin \
freebsd-i386.bin freebsd-powerpc.bin freebsd-powerpc64.bin freebsd-powerpc64le.bin \
freebsd-riscv64.bin dfly.bin linux.bin \
macos.bin macos106.bin macos150.bin macosfat.bin \
"macosfat.bin#amd64" "macosfat.bin#aarch64"
do
local file1=$(atf_get_srcdir)/$bin

ALLOW_BASE_SHLIBS=no
genmanifest ${PKG_NAME} ${file1}

atf_check \
-o inline:"${ALLOW_BASE_SHLIBS}\n" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=${file1} -o ALLOW_BASE_SHLIBS=${ALLOW_BASE_SHLIBS} config allow_base_shlibs

# cat ${PKG_NAME}.manifest
atf_check \
-o empty \
-e empty \
-s exit:0 \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=${file1} -o ALLOW_BASE_SHLIBS=${ALLOW_BASE_SHLIBS} create -M ./${PKG_NAME}.manifest -r ${TMPDIR}

# cat ${PKG_NAME}.expected
atf_check \
-o file:${PKG_NAME}.expected \
-e empty \
-s exit:0 \
pkg info -R --raw-format=ucl -F ${PKG_NAME}-1.pkg
done
}

create_from_binbase_body() {
local PKG_NAME=testbinbase

for bin in \
freebsd-aarch64.bin freebsd-amd64.bin freebsd-armv6.bin freebsd-armv7.bin \
freebsd-i386.bin freebsd-powerpc.bin freebsd-powerpc64.bin freebsd-powerpc64le.bin \
freebsd-riscv64.bin dfly.bin linux.bin \
macos.bin macos106.bin macos150.bin macosfat.bin \
"macosfat.bin#amd64" "macosfat.bin#aarch64"
do
local file1=$(atf_get_srcdir)/$bin

ALLOW_BASE_SHLIBS=yes
genmanifest ${PKG_NAME} ${file1}

atf_check \
-o inline:"${ALLOW_BASE_SHLIBS}\n" \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=${file1} -o ALLOW_BASE_SHLIBS=${ALLOW_BASE_SHLIBS} config allow_base_shlibs

# cat ${PKG_NAME}.manifest
atf_check \
-o empty \
-e empty \
-s exit:0 \
pkg -o IGNORE_OSMAJOR=1 -o ABI_FILE=${file1} -o ALLOW_BASE_SHLIBS=${ALLOW_BASE_SHLIBS} create -M ./${PKG_NAME}.manifest -r ${TMPDIR}

# cat ${PKG_NAME}.expected
atf_check \
-o file:${PKG_NAME}.expected \
-e empty \
-s exit:0 \
pkg info -R --raw-format=ucl -F ${PKG_NAME}-1.pkg
done
}
Loading

0 comments on commit cf7a4ed

Please sign in to comment.