diff --git a/CMakeLists.txt b/CMakeLists.txt index a4fb37c..a205a35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,8 +31,32 @@ target_compile_definitions("${PACKAGE_NAME}" PRIVATE "PACKAGE_NAME=\"${PACKAGE_NAME}\"" ) + enable_testing() -add_test( - NAME "tests" - COMMAND bash -c "${CMAKE_CURRENT_SOURCE_DIR}/tests/test.sh ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME} ${CMAKE_CURRENT_SOURCE_DIR}" - ) + +# Dynamic section tests +foreach(arch ${ARCHES}) + foreach(api ${APIS}) + add_test( + NAME "dynamic-section-${arch}-api${api}" + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/test-dynamic-section.sh + ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME} + ${CMAKE_CURRENT_SOURCE_DIR} + curl-7.83.1 + ${arch} + ${api} + ) + endforeach() +endforeach() + +# TLS alignment tests +foreach(arch ${ARCHES}) + add_test( + NAME "tls-alignment-${arch}-api${api}" + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/test-tls-alignment.sh + ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME} + ${CMAKE_CURRENT_SOURCE_DIR} + valgrind-3.19.0 + ${arch} + ) +endforeach() diff --git a/tests/test-dynamic-section.sh b/tests/test-dynamic-section.sh new file mode 100755 index 0000000..61627eb --- /dev/null +++ b/tests/test-dynamic-section.sh @@ -0,0 +1,45 @@ +#!/usr/bin/bash +set -e + +if [ $# != 5 ]; then + echo "Usage path/to/test-dynamic-section.sh " + exit 1 +fi + +elf_cleaner="$1" +source_dir="$2" +binary_name="$3" +arch="$4" +api="$5" + +progname="$(basename "$elf_cleaner")" +basefile="$source_dir/tests/$binary_name-$arch" +origfile="$basefile-original" +testfile="$basefile-api$api.test" +expectedfile="$basefile-api$api-cleaned" + +if [ "$api" = "21" ]; then + expected_logs="$progname: Removing version section from '$testfile' +$progname: Removing version section from '$testfile' +$progname: Removing the DT_RUNPATH dynamic section entry from '$testfile' +$progname: Removing the DT_VERNEEDNUM dynamic section entry from '$testfile' +$progname: Removing the DT_VERNEEDED dynamic section entry from '$testfile' +$progname: Removing the DT_VERSYM dynamic section entry from '$testfile' +$progname: Replacing unsupported DF_1_* flags 134217737 with 1 in '$testfile' +$progname: Removing the DT_GNU_HASH dynamic section entry from '$testfile'" +elif [ "$api" = "24" ]; then + expected_logs="$progname: Replacing unsupported DF_1_* flags 134217737 with 9 in '$testfile'" +else + echo "Unknown API level $api" + exit 1 +fi + +cp "$origfile" "$testfile" +if [ "$("$elf_cleaner" --api-level "$api" "$testfile")" != "$expected_logs" ]; then + echo "Logs do not match for $testfile" + exit 1 +fi +if not cmp -s "$testfile" "$expectedfile"; then + echo "Expected and actual files differ for $testfile" + exit 1 +fi diff --git a/tests/test-tls-alignment.sh b/tests/test-tls-alignment.sh new file mode 100755 index 0000000..5bc74f7 --- /dev/null +++ b/tests/test-tls-alignment.sh @@ -0,0 +1,37 @@ +#!/usr/bin/bash +set -e + +if [ $# != 4 ]; then + echo "Usage path/to/test-dynamic-section.sh " + exit 1 +fi + +elf_cleaner="$1" +source_dir="$2" +binary_name="$3" +arch="$4" + +progname="$(basename "$elf_cleaner")" +basefile="$source_dir/tests/$binary_name-$arch" +origfile="$basefile-original" +testfile="$basefile.test" +expectedfile="$basefile-tls-aligned" + +if [ "$arch" = "aarch64" ] || [ "$arch" = "x86_64" ]; then + expected_logs="$progname: Changing TLS alignment for '$testfile' to 64, instead of 8" +elif [ "$arch" = "arm" ] || [ "$arch" = "i686" ]; then + expected_logs="$progname: Changing TLS alignment for '$testfile' to 32, instead of 8" +else + echo "Unknown architecture $arch" + exit 1 +fi + +cp "$origfile" "$testfile" +if [ "$("$elf_cleaner" "$testfile")" != "$expected_logs" ]; then + echo "Logs do not match for $testfile" + exit 1 +fi +if not cmp -s "$testfile" "$expectedfile"; then + echo "Expected and actual files differ for $testfile" + exit 1 +fi diff --git a/tests/test.sh b/tests/test.sh deleted file mode 100755 index 462f2bd..0000000 --- a/tests/test.sh +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/bash -set -e -if [ $# != 2 ]; then - echo "Usage path/to/test.sh " - exit 1 -fi - -archs=('arm' 'aarch64' 'i686' 'x86_64') -apis=('21' '24') -progname="$(basename "$1")" - -for arch in "${archs[@]}"; do - for api in "${apis[@]}"; do - basefile="$2/tests/curl-7.83.1-${arch}" - if [ "${api}" = "21" ]; then - expected_logs="${progname}: Removing version section from '${basefile}-api${api}.test' -${progname}: Removing version section from '${basefile}-api${api}.test' -${progname}: Removing the DT_RUNPATH dynamic section entry from '${basefile}-api${api}.test' -${progname}: Removing the DT_VERNEEDNUM dynamic section entry from '${basefile}-api${api}.test' -${progname}: Removing the DT_VERNEEDED dynamic section entry from '${basefile}-api${api}.test' -${progname}: Removing the DT_VERSYM dynamic section entry from '${basefile}-api${api}.test' -${progname}: Replacing unsupported DF_1_* flags 134217737 with 1 in '${basefile}-api${api}.test' -${progname}: Removing the DT_GNU_HASH dynamic section entry from '${basefile}-api${api}.test'" - elif [ "${api}" = "24" ]; then - expected_logs="${progname}: Replacing unsupported DF_1_* flags 134217737 with 9 in '${basefile}-api${api}.test'" - else - echo "Unknown API level ${api}" - exit 1 - fi - cp "${basefile}"{-original,"-api${api}".test} - if [ "$("$1" --api-level "${api}" "${basefile}-api${api}.test")" != "${expected_logs}" ]; then - echo "Failed to remove version section from ${basefile}-api${api}.test" - exit 1 - fi - done -done - -for arch in "${archs[@]}"; do - basefile="$2/tests/valgrind-3.19.0-${arch}" - if [ "${arch}" = "aarch64" ] || [ "${arch}" = "x86_64" ]; then - expected_logs="${progname}: Changing TLS alignment for '${basefile}.test' to 64, instead of 8" - elif [ "${arch}" = "arm" ] || [ "${arch}" = "i686" ]; then - expected_logs="${progname}: Changing TLS alignment for '${basefile}.test' to 32, instead of 8" - else - echo "Unknown architecture ${arch}" - exit 1 - fi - cp "${basefile}"{-original,.test} - if [ "$("$1" "${basefile}.test")" != "${expected_logs}" ]; then - echo "Failed to remove version section from ${basefile}.test" - exit 1 - fi - diff "${basefile}"{-tls-aligned,.test} -done