1
0
mirror of https://github.com/upx/upx.git synced 2025-08-11 22:52:30 +08:00
Files
upx/misc/testsuite/mimic_ctest.sh

184 lines
8.1 KiB
Bash
Raw Normal View History

2023-12-28 21:44:53 +01:00
#! /usr/bin/env bash
## vim:set ts=4 sw=4 et:
set -e; set -o pipefail
argv0=$0; argv0abs=$(readlink -fn "$argv0"); argv0dir=$(dirname "$argv0abs")
#
# Copyright (C) Markus Franz Xaver Johannes Oberhumer
#
2024-07-23 12:24:09 +02:00
# mimic running "ctest", i.e. the "test" section of CMakeLists.txt
# - does not redirect stdout
# - allows freely setting $upx_exe_runner, while CMake is restricted to configure-time settings
2023-12-28 21:44:53 +01:00
#
# requires:
# $upx_exe (required, but with convenience fallback "./upx")
# optional settings:
# $upx_exe_runner (e.g. "qemu-x86_64 -cpu Nehalem" or "valgrind")
#
# Debugging aid for locating failing commands. Depends on 'bash' shell.
# (BASH_LINENO is relative to current FUNCTION only; non-function ==> 0)
# Notice single-quoting of entire first argument.
trap 'echo ERROR: pwd=\"$PWD\" file=\"$BASH_SOURCE\" line=${BASH_LINENO[0]} cmd=\"$BASH_COMMAND\"' ERR
# Example: "false a b c" ==> ERROR: pwd="path/misc/testsuite" file="./mimic_ctest.sh" line=0 cmd="false a b c"
2023-12-28 21:44:53 +01:00
#***********************************************************************
# init & checks
#***********************************************************************
# upx_exe
[[ -z $upx_exe && -f ./upx && -x ./upx ]] && upx_exe=./upx # convenience fallback
if [[ -z $upx_exe ]]; then echo "UPX-ERROR: please set \$upx_exe"; exit 1; fi
if [[ ! -f $upx_exe ]]; then echo "UPX-ERROR: file '$upx_exe' does not exist"; exit 1; fi
upx_exe=$(readlink -fn "$upx_exe") # make absolute
[[ -f $upx_exe ]] || exit 1
2024-04-16 15:20:56 +02:00
# set emu and run_upx
emu=()
2023-12-28 21:44:53 +01:00
if [[ -n $upx_exe_runner ]]; then
# usage examples:
# export upx_exe_runner="qemu-x86_64 -cpu Nehalem"
# export upx_exe_runner="valgrind --leak-check=no --error-exitcode=1 --quiet"
# export upx_exe_runner="wine"
2024-04-16 15:20:56 +02:00
IFS=' ' read -r -a emu <<< "$upx_exe_runner" # split at spaces into array
2023-12-28 21:44:53 +01:00
elif [[ -n $CMAKE_CROSSCOMPILING_EMULATOR ]]; then
2024-04-16 15:20:56 +02:00
IFS=';' read -r -a emu <<< "$CMAKE_CROSSCOMPILING_EMULATOR" # split at semicolons into array
2023-12-28 21:44:53 +01:00
fi
2024-04-16 15:20:56 +02:00
run_upx=( "${emu[@]}" "$upx_exe" )
echo "run_upx='${run_upx[*]}'"
2023-12-28 21:44:53 +01:00
2024-04-16 15:20:56 +02:00
# run_upx sanity check
if ! "${run_upx[@]}" --version-short >/dev/null; then echo "UPX-ERROR: FATAL: upx --version-short FAILED"; exit 1; fi
if ! "${run_upx[@]}" -L >/dev/null 2>&1; then echo "UPX-ERROR: FATAL: upx -L FAILED"; exit 1; fi
if ! "${run_upx[@]}" --help >/dev/null; then echo "UPX-ERROR: FATAL: upx --help FAILED"; exit 1; fi
2023-12-28 21:44:53 +01:00
#***********************************************************************
# see CMakeLists.txt
2024-04-28 22:18:47 +02:00
#
2024-07-23 12:24:09 +02:00
# IDEA: create a Makefile and use "make -j8" so that these tests can
2024-04-28 22:18:47 +02:00
# run in parallel much like "ctest --parallel 8"
2023-12-28 21:44:53 +01:00
#***********************************************************************
2024-04-28 22:18:47 +02:00
# similar to cmake function upx_cache_bool_vars()
set_cmake_bool_vars() {
local default_value="$1"; shift
2024-04-10 10:52:48 +02:00
local var_name
for var_name do
case "${!var_name}" in
2024-04-28 22:18:47 +02:00
0 | FALSE | OFF | false | off) eval "export $var_name=OFF" ;;
1 | TRUE | ON | true | on) eval "export $var_name=ON" ;;
*) eval "export $var_name=$default_value" ;;
2024-04-10 10:52:48 +02:00
esac
done
}
set -x
2024-04-28 22:18:47 +02:00
set_cmake_bool_vars OFF UPX_CONFIG_DISABLE_SELF_PACK_TEST
set_cmake_bool_vars OFF UPX_CONFIG_DISABLE_RUN_UNPACKED_TEST
set_cmake_bool_vars OFF UPX_CONFIG_DISABLE_RUN_PACKED_TEST
if [[ "${emu[0]}" == *valgrind* ]]; then # valgrind is SLOW
set_cmake_bool_vars ON UPX_CONFIG_DISABLE_EXHAUSTIVE_TESTS
else
set_cmake_bool_vars OFF UPX_CONFIG_DISABLE_EXHAUSTIVE_TESTS
fi
2024-04-10 10:52:48 +02:00
2024-04-25 03:19:59 +02:00
export UPX="--prefer-ucl --no-color --no-progress"
export UPX_DEBUG_DISABLE_GITREV_WARNING=1
export UPX_DEBUG_DOCTEST_DISABLE=1 # already checked above
2023-12-28 21:44:53 +01:00
2024-04-16 15:20:56 +02:00
"${run_upx[@]}" --version
"${run_upx[@]}" --version-short
"${run_upx[@]}" --license
"${run_upx[@]}" --help
"${run_upx[@]}" --help-short
"${run_upx[@]}" --help-verbose
"${run_upx[@]}" --sysinfo
2024-04-16 15:20:56 +02:00
"${run_upx[@]}" --sysinfo -v
"${run_upx[@]}" --sysinfo -vv
2023-12-28 21:44:53 +01:00
2024-04-10 10:52:48 +02:00
if [[ $UPX_CONFIG_DISABLE_SELF_PACK_TEST == ON ]]; then
echo "Self-pack test disabled. All done."; exit 0
fi
2024-03-16 17:46:40 +01:00
2023-12-28 21:44:53 +01:00
exe=".out"
upx_self_exe=$upx_exe
fo="--force-overwrite"
2024-04-16 15:20:56 +02:00
"${run_upx[@]}" -3 "${upx_self_exe}" ${fo} -o upx-packed${exe}
"${run_upx[@]}" -3 --all-filters "${upx_self_exe}" ${fo} -o upx-packed-fa${exe}
"${run_upx[@]}" -3 --no-filter "${upx_self_exe}" ${fo} -o upx-packed-fn${exe}
"${run_upx[@]}" -3 --all-filters --debug-use-random-filter "${upx_self_exe}" ${fo} -o upx-packed-fr${exe}
2024-04-16 15:20:56 +02:00
"${run_upx[@]}" -3 --nrv2b "${upx_self_exe}" ${fo} -o upx-packed-nrv2b${exe}
"${run_upx[@]}" -3 --nrv2d "${upx_self_exe}" ${fo} -o upx-packed-nrv2d${exe}
"${run_upx[@]}" -3 --nrv2e "${upx_self_exe}" ${fo} -o upx-packed-nrv2e${exe}
"${run_upx[@]}" -1 --lzma "${upx_self_exe}" ${fo} -o upx-packed-lzma${exe}
2024-04-21 12:17:38 +02:00
"${run_upx[@]}" -l upx-packed${exe} upx-packed-fa${exe} upx-packed-fn${exe} upx-packed-fr${exe} upx-packed-nrv2b${exe} upx-packed-nrv2d${exe} upx-packed-nrv2e${exe} upx-packed-lzma${exe}
"${run_upx[@]}" --fileinfo upx-packed${exe} upx-packed-fa${exe} upx-packed-fn${exe} upx-packed-fr${exe} upx-packed-nrv2b${exe} upx-packed-nrv2d${exe} upx-packed-nrv2e${exe} upx-packed-lzma${exe}
"${run_upx[@]}" -t upx-packed${exe} upx-packed-fa${exe} upx-packed-fn${exe} upx-packed-fr${exe} upx-packed-nrv2b${exe} upx-packed-nrv2d${exe} upx-packed-nrv2e${exe} upx-packed-lzma${exe}
2024-04-16 15:20:56 +02:00
"${run_upx[@]}" -d upx-packed${exe} ${fo} -o upx-unpacked${exe}
"${run_upx[@]}" -d upx-packed-fa${exe} ${fo} -o upx-unpacked-fa${exe}
"${run_upx[@]}" -d upx-packed-fn${exe} ${fo} -o upx-unpacked-fn${exe}
2024-04-21 12:17:38 +02:00
"${run_upx[@]}" -d upx-packed-fr${exe} ${fo} -o upx-unpacked-fr${exe}
2024-04-16 15:20:56 +02:00
"${run_upx[@]}" -d upx-packed-nrv2b${exe} ${fo} -o upx-unpacked-nrv2b${exe}
"${run_upx[@]}" -d upx-packed-nrv2d${exe} ${fo} -o upx-unpacked-nrv2d${exe}
"${run_upx[@]}" -d upx-packed-nrv2e${exe} ${fo} -o upx-unpacked-nrv2e${exe}
"${run_upx[@]}" -d upx-packed-lzma${exe} ${fo} -o upx-unpacked-lzma${exe}
2023-12-28 21:44:53 +01:00
2024-04-21 12:17:38 +02:00
# all unpacked files must be identical
cmp -s upx-unpacked${exe} upx-unpacked-fa${exe}
cmp -s upx-unpacked${exe} upx-unpacked-fn${exe}
cmp -s upx-unpacked${exe} upx-unpacked-fr${exe}
cmp -s upx-unpacked${exe} upx-unpacked-nrv2b${exe}
cmp -s upx-unpacked${exe} upx-unpacked-nrv2d${exe}
cmp -s upx-unpacked${exe} upx-unpacked-nrv2e${exe}
cmp -s upx-unpacked${exe} upx-unpacked-lzma${exe}
2024-04-28 22:18:47 +02:00
if [[ $UPX_CONFIG_DISABLE_RUN_UNPACKED_TEST != ON ]]; then
2024-04-25 03:19:59 +02:00
"${emu[@]}" ./upx-unpacked${exe} --version-short
2024-04-10 10:52:48 +02:00
fi
2024-04-28 22:18:47 +02:00
if [[ $UPX_CONFIG_DISABLE_RUN_PACKED_TEST != ON ]]; then
2024-04-25 03:19:59 +02:00
"${emu[@]}" ./upx-packed${exe} --version-short
"${emu[@]}" ./upx-packed-fa${exe} --version-short
"${emu[@]}" ./upx-packed-fn${exe} --version-short
"${emu[@]}" ./upx-packed-fr${exe} --version-short
"${emu[@]}" ./upx-packed-nrv2b${exe} --version-short
"${emu[@]}" ./upx-packed-nrv2d${exe} --version-short
"${emu[@]}" ./upx-packed-nrv2e${exe} --version-short
"${emu[@]}" ./upx-packed-lzma${exe} --version-short
fi
2024-04-28 22:18:47 +02:00
if [[ $UPX_CONFIG_DISABLE_EXHAUSTIVE_TESTS != ON ]]; then
2024-04-25 03:19:59 +02:00
set +x
for method in nrv2b nrv2d nrv2e lzma; do
for level in 1 2 3 4 5 6 7; do
2025-05-07 19:11:57 +02:00
for small in normal small; do
s="${method}-${level}"
ss=
if [[ $small == "small" ]]; then
s="${method}-${level}-${small}"
ss="--small"
fi
echo "========== $s =========="
"${run_upx[@]}" -qq --${method} -${level} ${ss} --all-filters --debug-use-random-filter "${upx_self_exe}" ${fo} -o upx-packed-${s}${exe}
"${run_upx[@]}" -qq -l upx-packed-${s}${exe}
"${run_upx[@]}" -qq --fileinfo upx-packed-${s}${exe}
"${run_upx[@]}" -qq -t upx-packed-${s}${exe}
"${run_upx[@]}" -qq -d upx-packed-${s}${exe} ${fo} -o upx-unpacked-${s}${exe}
cmp -s upx-unpacked${exe} upx-unpacked-${s}${exe}
if [[ $UPX_CONFIG_DISABLE_RUN_PACKED_TEST != ON ]]; then
"${emu[@]}" ./upx-packed-${s}${exe} --version-short
2025-07-21 15:57:03 +02:00
for ((i = 0; i < 10; i++)); do
"${emu[@]}" ./upx-packed-${s}${exe} --version-short >/dev/null
done
2025-05-07 19:11:57 +02:00
fi
done
2024-04-25 03:19:59 +02:00
done
done
2024-04-10 10:52:48 +02:00
fi
2023-12-28 21:44:53 +01:00
2024-04-28 22:18:47 +02:00
echo "run_upx='${run_upx[*]}'"
2023-12-28 21:44:53 +01:00
echo "All done."