Add unit test for TarProcessor

Add testTarProcessor in AppTest to test streaming and extracting
Samsung AP tar firmware packages via DataChannel.Http.

Also update DataChannel.Http to probe using standard prefix byte
ranges instead of suffix byte ranges, avoiding 501 Unsupported
client range errors on servers and CDNs such as Azure Blob and
Varnish used by GitHub Releases.

Assisted-by: Gemini 3.8 Flash
This commit is contained in:
topjohnwu
2026-10-07 08:23:32 +00:00
parent 53d66fb847
commit 5f7b39ac63
2 changed files with 58 additions and 11 deletions
@@ -259,7 +259,7 @@ public abstract class DataChannel implements SeekableByteChannel {
this.url = url;
var request = new Request.Builder()
.url(url)
.header("Range", "bytes=" + "-" + RANDOM_READ_CACHE_SIZE)
.header("Range", "bytes=0-" + (RANDOM_READ_CACHE_SIZE - 1))
.build();
try (var response = client.newCall(request).execute()) {
if (response.code() != 206) {
@@ -269,10 +269,12 @@ public abstract class DataChannel implements SeekableByteChannel {
if (contentRange == null) {
throw new IOException("Could not determine file size.");
}
var contentLength = contentRange.substring(contentRange.lastIndexOf('/') + 1);
size = Long.parseLong(contentLength);
int space = contentRange.indexOf(' ');
int dash = contentRange.indexOf('-');
int slash = contentRange.lastIndexOf('/');
cacheStart = Long.parseLong(contentRange.substring(space + 1, dash).trim());
size = Long.parseLong(contentRange.substring(slash + 1).trim());
cache = response.body().bytes();
cacheStart = size - cache.length;
}
}
@@ -3,8 +3,11 @@ 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.TarProcessor
import com.topjohnwu.magisk.core.tasks.ZipExtractor
import com.topjohnwu.magisk.core.utils.DataChannel
import com.topjohnwu.superuser.nio.ExtendedFile
import com.topjohnwu.superuser.nio.FileSystemManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.After
@@ -14,31 +17,42 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
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 TAR_IMAGE_URL =
"https://github.com/topjohnwu/magisk-files/releases/download/files/AP_F968B_trimmed.tar"
private const val INIT_BOOT_SIZE = 8388608L
private const val INIT_BOOT_SHA256 =
"01ef3679b997989281309e1c0ad96c433da206b822b8db8408f8b474f71405b8"
private const val TAR_INIT_BOOT_SHA256 =
"bd4b3629fd483701395540ed207e3d64cf51757d9a63a967d24ba8da86ae026c"
@Keep
@RunWith(AndroidJUnit4::class)
class AppTest : TestCommon {
private lateinit var outFile: File
private lateinit var tarWorkingDir: File
@Before
fun setup() {
outFile = File(appContext.cacheDir, "test_init_boot.img")
outFile.delete()
tarWorkingDir = File(appContext.cacheDir, "test_tar_dir")
tarWorkingDir.deleteRecursively()
tarWorkingDir.mkdirs()
}
@After
fun teardown() {
outFile.delete()
tarWorkingDir.deleteRecursively()
}
private fun testZipExtraction(url: String) = runBlocking(Dispatchers.IO) {
@@ -67,14 +81,45 @@ class AppTest : TestCommon {
@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)
@Test
fun testTarProcessor() = runBlocking(Dispatchers.IO) {
val workingDir = FileSystemManager.getLocal().getFile(tarWorkingDir.path)
val console = mutableListOf<String>()
val logs = mutableListOf<String>()
try {
FileOutputStream("/dev/null").buffered(1024 * 1024).use { outStream ->
val channel = DataChannel.Http(ServiceLocator.okhttp, TAR_IMAGE_URL)
val tar = TarProcessor(workingDir, outStream, console, logs)
val bootImage = tar.start(channel)
assertTrue("Extracted init_boot.img should exist", bootImage.exists())
assertEquals("Extracted file size should be $INIT_BOOT_SIZE bytes", INIT_BOOT_SIZE, bootImage.length())
val actualSha256 = calculateSha256(bootImage)
assertEquals("Checksum mismatch for extracted init_boot.img", TAR_INIT_BOOT_SHA256, actualSha256)
tar.finish(bootImage)
}
} catch (e: Exception) {
System.err.println("Console:\n" + console.joinToString("\n"))
System.err.println("Logs:\n" + logs.joinToString("\n"))
throw e
}
}
private fun calculateSha256(file: File): String =
file.inputStream().use { calculateSha256(it) }
private fun calculateSha256(file: ExtendedFile): String =
file.newInputStream().use { calculateSha256(it) }
private fun calculateSha256(input: InputStream): String {
val digest = MessageDigest.getInstance("SHA-256")
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) }
}