mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
Add unit tests for ZipExtractor
Implement AppTest with unit tests for ZipExtractor verifying remote boot image extraction from both factory image packages and OTA update packages using HTTP range requests. Also add an app-test sub-command to scripts/avd.sh to run AppTest via instrumentation on connected emulators. Assisted-by: Gemini 3.8 Flash
This commit is contained in:
@@ -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<String>()
|
||||
val logs = mutableListOf<String>()
|
||||
|
||||
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) }
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user