diff --git a/app/core/src/main/java/com/topjohnwu/magisk/test/AppTest.kt b/app/core/src/main/java/com/topjohnwu/magisk/test/AppTest.kt new file mode 100644 index 000000000..b31e94012 --- /dev/null +++ b/app/core/src/main/java/com/topjohnwu/magisk/test/AppTest.kt @@ -0,0 +1,81 @@ +package com.topjohnwu.magisk.test + +import androidx.annotation.Keep +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.topjohnwu.magisk.core.di.ServiceLocator +import com.topjohnwu.magisk.core.tasks.ZipExtractor +import com.topjohnwu.magisk.core.utils.DataChannel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import java.io.File +import java.security.MessageDigest + +private const val FACTORY_IMAGE_URL = + "https://dl.google.com/dl/android/aosp/yogi-cd1a.261005.003.b1-factory-90f36052.zip" +private const val OTA_IMAGE_URL = + "https://dl.google.com/dl/android/aosp/yogi-ota-cd1a.261005.003.b1-7d7d9988.zip" +private const val INIT_BOOT_SIZE = 8388608L +private const val INIT_BOOT_SHA256 = + "01ef3679b997989281309e1c0ad96c433da206b822b8db8408f8b474f71405b8" + +@Keep +@RunWith(AndroidJUnit4::class) +class AppTest : TestCommon { + + private lateinit var outFile: File + + @Before + fun setup() { + outFile = File(appContext.cacheDir, "test_init_boot.img") + outFile.delete() + } + + @After + fun teardown() { + outFile.delete() + } + + private fun testZipExtraction(url: String) = runBlocking(Dispatchers.IO) { + val console = mutableListOf() + val logs = mutableListOf() + + try { + val channel = DataChannel.Http(ServiceLocator.okhttp, url) + ZipExtractor(outFile, console, logs).extract(channel) + } catch (e: Exception) { + System.err.println("Console:\n" + console.joinToString("\n")) + System.err.println("Logs:\n" + logs.joinToString("\n")) + throw e + } + + assertTrue("Extracted init_boot.img should exist", outFile.exists()) + assertEquals("Extracted file size should be $INIT_BOOT_SIZE bytes", INIT_BOOT_SIZE, outFile.length()) + + val actualSha256 = calculateSha256(outFile) + assertEquals("Checksum mismatch for extracted init_boot.img", INIT_BOOT_SHA256, actualSha256) + } + + @Test + fun testFactoryZipExtraction() = testZipExtraction(FACTORY_IMAGE_URL) + + @Test + fun testOtaZipExtraction() = testZipExtraction(OTA_IMAGE_URL) + + private fun calculateSha256(file: File): String { + val digest = MessageDigest.getInstance("SHA-256") + file.inputStream().use { input -> + val buffer = ByteArray(64 * 1024) + var bytesRead: Int + while (input.read(buffer).also { bytesRead = it } != -1) { + digest.update(buffer, 0, bytesRead) + } + } + return digest.digest().joinToString("") { "%02x".format(it) } + } +} diff --git a/scripts/avd.sh b/scripts/avd.sh index f996cdc01..0f8ee1d8b 100755 --- a/scripts/avd.sh +++ b/scripts/avd.sh @@ -315,6 +315,20 @@ live_test_main() { done } +app_test_main() { + local apks=($(print_apks "$@")) + local pkg='com.topjohnwu.magisk.test' + local app="$pkg/$pkg.AppTestRunner" + for apk in "${apks[@]}"; do + # Cleanup + adb shell pm uninstall com.topjohnwu.magisk || true + adb install -r -g $apk + adb install -r -g out/test.apk + am_instrument '.AppTest' $app + done +} + + case "$1" in test ) shift @@ -327,6 +341,11 @@ case "$1" in set -x live_test_main "$@" ;; + app-test ) + shift + set -x + app_test_main "$@" + ;; run ) shift trap cleanup EXIT