2023-12-29 04:44:53 +08: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
|
|
|
|
#
|
|
|
|
# mimic running "ctest", i.e. the "test" section of CMakeLists.txt; does not redirect stdout
|
2024-04-10 16:52:48 +08:00
|
|
|
# (allows freely setting $upx_exe_runner, while CMake is restricted to configure-time settings)
|
2023-12-29 04:44:53 +08:00
|
|
|
#
|
|
|
|
# requires:
|
|
|
|
# $upx_exe (required, but with convenience fallback "./upx")
|
|
|
|
# optional settings:
|
|
|
|
# $upx_exe_runner (e.g. "qemu-x86_64 -cpu Nehalem" or "valgrind")
|
|
|
|
#
|
|
|
|
|
|
|
|
#***********************************************************************
|
|
|
|
# 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-21 20:20:18 +08:00
|
|
|
|
2024-04-16 21:20:56 +08:00
|
|
|
# set emu and run_upx
|
|
|
|
emu=()
|
2023-12-29 04:44:53 +08: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 21:20:56 +08:00
|
|
|
IFS=' ' read -r -a emu <<< "$upx_exe_runner" # split at spaces into array
|
2023-12-29 04:44:53 +08:00
|
|
|
elif [[ -n $CMAKE_CROSSCOMPILING_EMULATOR ]]; then
|
2024-04-16 21:20:56 +08:00
|
|
|
IFS=';' read -r -a emu <<< "$CMAKE_CROSSCOMPILING_EMULATOR" # split at semicolons into array
|
2023-12-29 04:44:53 +08:00
|
|
|
fi
|
2024-04-16 21:20:56 +08:00
|
|
|
run_upx=( "${emu[@]}" "$upx_exe" )
|
|
|
|
echo "run_upx='${run_upx[*]}'"
|
2023-12-29 04:44:53 +08:00
|
|
|
|
2024-04-16 21:20:56 +08: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-29 04:44:53 +08:00
|
|
|
|
|
|
|
#***********************************************************************
|
|
|
|
# see CMakeLists.txt
|
|
|
|
#***********************************************************************
|
|
|
|
|
2024-04-10 16:52:48 +08:00
|
|
|
set_on_off() {
|
|
|
|
local var_name
|
|
|
|
for var_name do
|
|
|
|
case "${!var_name}" in
|
|
|
|
"" | "0" | "FALSE" | "OFF") eval $var_name=OFF ;;
|
|
|
|
*) eval $var_name=ON ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
set -x
|
2024-04-25 16:56:24 +08:00
|
|
|
if [[ "${emu[0]}" == *valgrind* ]]; then true; # valgrind is SLOW
|
|
|
|
[[ -n $UPX_CONFIG_DISABLE_EXHAUSTIVE_TEST ]] || UPX_CONFIG_DISABLE_EXHAUSTIVE_TEST=ON
|
|
|
|
fi
|
2024-04-10 16:52:48 +08:00
|
|
|
set_on_off UPX_CONFIG_DISABLE_SELF_PACK_TEST
|
2024-04-25 09:19:59 +08:00
|
|
|
set_on_off UPX_CONFIG_DISABLE_EXHAUSTIVE_TEST
|
2024-04-10 16:52:48 +08:00
|
|
|
set_on_off UPX_CONFIG_DISABLE_RUN_UNPACKED_TEST
|
|
|
|
set_on_off UPX_CONFIG_DISABLE_RUN_PACKED_TEST
|
|
|
|
|
2024-04-25 09:19:59 +08: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-29 04:44:53 +08:00
|
|
|
|
2024-04-16 21:20:56 +08:00
|
|
|
"${run_upx[@]}" --version
|
|
|
|
"${run_upx[@]}" --version-short
|
|
|
|
"${run_upx[@]}" --license
|
2024-04-21 20:20:18 +08:00
|
|
|
"${run_upx[@]}" --help
|
|
|
|
"${run_upx[@]}" --help-short
|
|
|
|
"${run_upx[@]}" --help-verbose
|
|
|
|
"${run_upx[@]}" --sysinfo
|
2024-04-16 21:20:56 +08:00
|
|
|
"${run_upx[@]}" --sysinfo -v
|
2024-04-21 20:20:18 +08:00
|
|
|
"${run_upx[@]}" --sysinfo -vv
|
2023-12-29 04:44:53 +08:00
|
|
|
|
2024-04-10 16:52:48 +08:00
|
|
|
if [[ $UPX_CONFIG_DISABLE_SELF_PACK_TEST == ON ]]; then
|
|
|
|
echo "Self-pack test disabled. All done."; exit 0
|
|
|
|
fi
|
2024-03-17 00:46:40 +08:00
|
|
|
|
2023-12-29 04:44:53 +08:00
|
|
|
exe=".out"
|
|
|
|
upx_self_exe=$upx_exe
|
|
|
|
fo="--force-overwrite"
|
|
|
|
|
2024-04-16 21:20:56 +08: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}
|
2024-04-26 16:36:01 +08:00
|
|
|
"${run_upx[@]}" -3 --all-filters --debug-use-random-filter "${upx_self_exe}" ${fo} -o upx-packed-fr${exe}
|
2024-04-16 21:20:56 +08: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 18:17:38 +08: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 21:20:56 +08: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 18:17:38 +08:00
|
|
|
"${run_upx[@]}" -d upx-packed-fr${exe} ${fo} -o upx-unpacked-fr${exe}
|
2024-04-16 21:20:56 +08: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-29 04:44:53 +08:00
|
|
|
|
2024-04-21 18:17:38 +08: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-10 16:52:48 +08:00
|
|
|
if [[ $UPX_CONFIG_DISABLE_RUN_UNPACKED_TEST == OFF ]]; then
|
2024-04-25 09:19:59 +08:00
|
|
|
"${emu[@]}" ./upx-unpacked${exe} --version-short
|
2024-04-10 16:52:48 +08:00
|
|
|
fi
|
2024-04-06 21:47:46 +08:00
|
|
|
|
2024-04-10 16:52:48 +08:00
|
|
|
if [[ $UPX_CONFIG_DISABLE_RUN_PACKED_TEST == OFF ]]; then
|
2024-04-25 09:19:59 +08: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
|
|
|
|
|
|
|
|
if [[ $UPX_CONFIG_DISABLE_EXHAUSTIVE_TEST == OFF ]]; then
|
|
|
|
set +x
|
|
|
|
for method in nrv2b nrv2d nrv2e lzma; do
|
|
|
|
for level in 1 2 3 4 5 6 7; do
|
|
|
|
s="${method}-${level}"
|
|
|
|
echo "========== $s =========="
|
2024-04-26 16:36:01 +08:00
|
|
|
"${run_upx[@]}" -qq --${method} -${level} --all-filters --debug-use-random-filter "${upx_self_exe}" ${fo} -o upx-packed-${s}${exe}
|
2024-04-25 09:19:59 +08:00
|
|
|
"${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 == OFF ]]; then
|
|
|
|
"${emu[@]}" ./upx-packed-${s}${exe} --version-short
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
2024-04-10 16:52:48 +08:00
|
|
|
fi
|
2023-12-29 04:44:53 +08:00
|
|
|
|
|
|
|
echo "All done."
|