From 0f59f1f3457b6bc13b51c0ca0c5a5439b68e7ab1 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 28 Jul 2026 17:23:35 -0700 Subject: [PATCH] Setup userspace Magisk tests The existing test infrastructure relies on the full Magisk setup before it can test any functionality. However for most of the time, we just want to test the core Magisk functionality without the need of involving MagiskInit. --- .../com/topjohnwu/magisk/test/Environment.kt | 12 ++++++-- scripts/avd.sh | 28 +++++++++++++++++++ scripts/live_setup.sh | 2 ++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/core/src/main/java/com/topjohnwu/magisk/test/Environment.kt b/app/core/src/main/java/com/topjohnwu/magisk/test/Environment.kt index adb781a46..79efcdadf 100644 --- a/app/core/src/main/java/com/topjohnwu/magisk/test/Environment.kt +++ b/app/core/src/main/java/com/topjohnwu/magisk/test/Environment.kt @@ -17,6 +17,7 @@ import com.topjohnwu.magisk.core.tasks.MagiskInstaller import com.topjohnwu.magisk.core.utils.RootUtils import com.topjohnwu.superuser.CallbackList import com.topjohnwu.superuser.Shell +import com.topjohnwu.superuser.ShellUtils import com.topjohnwu.superuser.nio.ExtendedFile import kotlinx.coroutines.runBlocking import org.apache.commons.compress.archivers.zip.ZipFile @@ -39,15 +40,22 @@ class Environment : BaseTest { @JvmStatic fun before() = BaseTest.prerequisite() + // Whether we're running with live setup (patches through live_setup.sh) + fun isLiveSetup(): Boolean { + val liveMarker = ShellUtils.fastCmd("echo \$MAGISKTMP/.magisk/live") + return RootUtils.fs.getFile(liveMarker).exists() + } + // The kernel running on emulators < API 26 does not play well with // magic mount. Skip mount_test on those legacy platforms. fun mount(): Boolean { return Build.VERSION.SDK_INT >= 26 } - // It is possible that there are no suitable preinit partition to use + // It is possible that there are no suitable preinit partition to use. + // We also skip this test when running within a live setup. fun preinit(): Boolean { - return Shell.cmd("magisk --preinit-device").exec().isSuccess + return !isLiveSetup() && Shell.cmd("magisk --preinit-device").exec().isSuccess } fun lsposed(): Boolean { diff --git a/scripts/avd.sh b/scripts/avd.sh index c54b96874..41d2b692b 100755 --- a/scripts/avd.sh +++ b/scripts/avd.sh @@ -259,6 +259,28 @@ dl_main() { dl_emu "$avd_pkg" } +live_test_main() { + local apks=($(print_apks "$@")) + for apk in "${apks[@]}"; do + # Cleanup + adb shell pm uninstall com.topjohnwu.magisk || true + adb shell pm uninstall repackaged.com.topjohnwu.magisk.test || true + adb shell /system/xbin/su 0 rm -rf /data/adb/modules + + # "Install" Magisk + ./build.py -v emulator $apk + timeout $boot_timeout bash -c wait_for_boot + + run_setup $apk + + # Trigger Magisk soft reboot + ./build.py -v emulator $apk + timeout $boot_timeout bash -c wait_for_boot + + run_tests + done +} + case "$1" in test ) shift @@ -267,6 +289,12 @@ case "$1" in set -x test_main "$@" ;; + live-test ) + shift + export -f wait_for_boot + set -x + live_test_main "$@" + ;; run ) shift trap cleanup EXIT diff --git a/scripts/live_setup.sh b/scripts/live_setup.sh index 6e242aa45..206e5363b 100755 --- a/scripts/live_setup.sh +++ b/scripts/live_setup.sh @@ -149,6 +149,8 @@ mkdir -p $MAGISKTMP/.magisk/worker mount_tmpfs $MAGISKTMP/.magisk/worker mount --make-private $MAGISKTMP/.magisk/worker touch $MAGISKTMP/.magisk/config +# Create a marker file to understand it is a live setup +touch $MAGISKTMP/.magisk/live export MAGISKTMP MAKEDEV=1 $MAGISKTMP/magisk --preinit-device 2>&1