Magisk/scripts/avd_test.sh

248 lines
5.2 KiB
Bash
Raw Permalink Normal View History

2022-03-18 12:56:19 +01:00
#!/usr/bin/env bash
emu="$ANDROID_SDK_ROOT/emulator/emulator"
2022-04-16 16:21:02 +02:00
avd="$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/avdmanager"
sdk="$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager"
2024-01-28 09:46:03 +01:00
emu_args_base='-no-window -no-audio -no-boot-anim -gpu swiftshader_indirect -read-only -no-snapshot -show-kernel -memory $memory'
2023-11-16 23:29:45 +01:00
lsposed_url='https://github.com/LSPosed/LSPosed/releases/download/v1.9.2/LSPosed-v1.9.2-7024-zygisk-release.zip'
2023-10-14 08:29:08 +02:00
boot_timeout=600
2023-10-12 07:42:45 +02:00
emu_pid=
2022-03-18 12:56:19 +01:00
2024-01-23 03:10:26 +01:00
export PATH="$PATH:$ANDROID_SDK_ROOT/platform-tools"
2024-04-10 11:56:23 +02:00
min_api=23
max_api=34
2023-11-16 23:29:45 +01:00
atd_min_api=30
2023-12-08 10:59:24 +01:00
atd_max_api=34
2023-11-16 23:29:45 +01:00
lsposed_min_api=27
2024-01-28 09:46:03 +01:00
huge_ram_min_api=26
2024-04-10 11:56:23 +02:00
i386_max_api=30
2022-09-16 04:45:58 +02:00
print_title() {
echo -e "\n\033[44;39m${1}\033[0m\n"
}
print_error() {
echo -e "\n\033[41;39m${1}\033[0m\n"
}
2022-09-16 04:45:58 +02:00
cleanup() {
print_error "! An error occurred when testing $pkg"
2022-09-16 04:45:58 +02:00
for api in $api_list; do
set_api_env $api
restore_avd
done
"$avd" delete avd -n test
2023-05-10 08:11:11 +02:00
pkill -INT -P $$
wait
2023-10-12 07:42:45 +02:00
trap - EXIT
exit 1
}
wait_for_bootanim() {
adb wait-for-device
while true; do
local result="$(adb exec-out getprop init.svc.bootanim)"
if [ $? -ne 0 ]; then
exit 1
elif [ "$result" = "stopped" ]; then
break
fi
sleep 2
done
2022-09-16 04:45:58 +02:00
}
2022-03-18 12:56:19 +01:00
wait_for_boot() {
2023-05-10 08:11:11 +02:00
adb wait-for-device
2022-03-18 12:56:19 +01:00
while true; do
2023-10-12 07:42:45 +02:00
local result="$(adb exec-out getprop sys.boot_completed)"
if [ $? -ne 0 ]; then
exit 1
elif [ "$result" = "1" ]; then
2022-03-18 12:56:19 +01:00
break
fi
sleep 2
done
}
2022-09-16 04:45:58 +02:00
set_api_env() {
2024-01-28 09:46:03 +01:00
local memory
2023-11-16 23:29:45 +01:00
local type='default'
if [ $1 -ge $atd_min_api -a $1 -le $atd_max_api ]; then
# Use the lightweight ATD images if possible
type='aosp_atd'
fi
2024-01-28 09:46:03 +01:00
# Old Linux kernels will not boot with memory larger than 3GB
if [ $1 -lt $huge_ram_min_api ]; then
memory=3072
else
memory=8192
fi
eval emu_args=\"$emu_args_base\"
pkg="system-images;android-$1;$type;$arch"
2022-09-16 04:45:58 +02:00
local img_dir="$ANDROID_SDK_ROOT/system-images/android-$1/$type/$arch"
2022-04-16 16:21:02 +02:00
ramdisk="$img_dir/ramdisk.img"
features="$img_dir/advancedFeatures.ini"
2022-09-16 04:45:58 +02:00
}
restore_avd() {
2022-03-18 12:56:19 +01:00
if [ -f "${ramdisk}.bak" ]; then
cp "${ramdisk}.bak" "$ramdisk"
fi
2022-04-16 16:21:02 +02:00
if [ -f "${features}.bak" ]; then
cp "${features}.bak" "$features"
fi
2022-09-16 04:45:58 +02:00
}
wait_emu() {
local wait_fn=$1
local which_pid
timeout $boot_timeout bash -c $wait_fn &
local wait_pid=$!
2023-10-25 01:45:24 +02:00
# Handle the case when emulator dies earlier than timeout
wait -p which_pid -n $emu_pid $wait_pid
[ $which_pid -eq $wait_pid ]
}
2023-10-25 01:45:24 +02:00
run_content_cmd() {
while true; do
local out=$(adb shell echo "'content call --uri content://com.topjohnwu.magisk.provider --method $1'" \| /system/xbin/su | tee /dev/fd/2)
if ! grep -q 'Bundle\[' <<< "$out"; then
# The call failed, wait a while and retry later
sleep 30
else
grep -q 'result=true' <<< "$out"
return $?
fi
done
}
2023-10-12 07:42:45 +02:00
test_emu() {
local variant=$1
2023-11-16 23:29:45 +01:00
local api=$2
print_title "* Testing $pkg ($variant)"
2023-10-12 07:42:45 +02:00
"$emu" @test $emu_args &
emu_pid=$!
wait_emu wait_for_boot
2023-10-12 07:42:45 +02:00
2024-01-05 11:39:59 +01:00
adb shell 'PATH=$PATH:/debug_ramdisk magisk -v'
# Install the Magisk app
adb install -r -g out/app-${variant}.apk
# Use the app to run setup and reboot
2023-10-25 01:45:24 +02:00
run_content_cmd setup
2023-11-16 23:29:45 +01:00
# Install LSPosed
if [ $api -ge $lsposed_min_api -a $api -le $atd_max_api ]; then
adb push out/lsposed.zip /data/local/tmp/lsposed.zip
2024-01-05 11:39:59 +01:00
adb shell echo 'PATH=$PATH:/debug_ramdisk magisk --install-module /data/local/tmp/lsposed.zip' \| /system/xbin/su
2023-11-16 23:29:45 +01:00
fi
adb reboot
wait_emu wait_for_boot
# Run app tests
2023-10-25 01:45:24 +02:00
run_content_cmd test
2023-11-16 23:29:45 +01:00
adb shell echo 'su -c id' \| /system/xbin/su 2000 | tee /dev/fd/2 | grep -q 'uid=0'
# Try to launch LSPosed
if [ $api -ge $lsposed_min_api -a $api -le $atd_max_api ]; then
2023-12-11 13:32:33 +01:00
adb shell rm -f /data/local/tmp/window_dump.xml
2023-11-16 23:29:45 +01:00
adb shell am start -c org.lsposed.manager.LAUNCH_MANAGER com.android.shell/.BugreportWarningActivity
2023-12-11 13:32:33 +01:00
while adb shell '[ ! -f /data/local/tmp/window_dump.xml ]'; do
sleep 10
adb shell uiautomator dump /data/local/tmp/window_dump.xml
done
adb shell grep -q org.lsposed.manager /data/local/tmp/window_dump.xml
2023-11-16 23:29:45 +01:00
fi
2023-10-12 07:42:45 +02:00
}
2022-09-16 04:45:58 +02:00
run_test() {
2023-05-10 08:11:11 +02:00
local api=$1
set_api_env $api
2022-09-16 04:45:58 +02:00
# Setup emulator
2023-11-16 23:29:45 +01:00
"$sdk" --channel=3 $pkg
2022-09-16 04:45:58 +02:00
echo no | "$avd" create avd -f -n test -k $pkg
2023-10-12 07:42:45 +02:00
# Launch stock emulator
print_title "* Launching $pkg"
2022-09-16 04:45:58 +02:00
restore_avd
2022-04-16 16:21:02 +02:00
"$emu" @test $emu_args &
2023-10-12 07:42:45 +02:00
emu_pid=$!
wait_emu wait_for_bootanim
2023-04-24 22:54:46 +02:00
2023-10-12 07:42:45 +02:00
# Patch and test debug build
2022-04-16 16:21:02 +02:00
./build.py avd_patch -s "$ramdisk"
2023-10-12 07:42:45 +02:00
kill -INT $emu_pid
wait $emu_pid
2023-11-16 23:29:45 +01:00
test_emu debug $api
2022-03-18 12:56:19 +01:00
2023-10-12 07:42:45 +02:00
# Re-patch and test release build
2023-10-09 14:47:50 +02:00
./build.py -r avd_patch -s "$ramdisk"
2023-10-12 07:42:45 +02:00
kill -INT $emu_pid
wait $emu_pid
2023-11-16 23:29:45 +01:00
test_emu release $api
2023-10-09 14:47:50 +02:00
2023-10-12 07:42:45 +02:00
# Cleanup
kill -INT $emu_pid
wait $emu_pid
2022-09-16 04:45:58 +02:00
restore_avd
2022-03-18 12:56:19 +01:00
}
2022-09-16 04:45:58 +02:00
trap cleanup EXIT
2022-03-18 12:56:19 +01:00
2022-09-16 04:45:58 +02:00
export -f wait_for_boot
2023-10-12 07:42:45 +02:00
export -f wait_for_bootanim
2022-09-16 04:45:58 +02:00
set -xe
case $(uname -m) in
'arm64'|'aarch64')
arch=arm64-v8a
;;
*)
arch=x86_64
;;
esac
2024-04-10 11:56:23 +02:00
if [ -n "$FORCE_32_BIT" ]; then
case $arch in
'arm64-v8a')
echo "! ARM32 is not supported"
exit 1
;;
'x86_64')
arch=x86
max_api=$i386_max_api
;;
esac
fi
api_list=$(seq $min_api $max_api)
2023-11-16 23:29:45 +01:00
yes | "$sdk" --licenses > /dev/null
curl -L $lsposed_url -o out/lsposed.zip
2024-01-23 03:10:26 +01:00
"$sdk" --channel=3 tools platform-tools emulator
2023-05-10 08:11:11 +02:00
if [ -n "$1" ]; then
run_test $1
else
for api in $api_list; do
run_test $api
done
fi
2022-03-18 12:56:19 +01:00
2022-04-16 16:21:02 +02:00
"$avd" delete avd -n test
2022-03-18 12:56:19 +01:00
trap - EXIT