Yokan / rebuild
Course 1 8624561

IosExtensionServerInstallerTest.kt

vendor/yokan-ext/src/iosTest/kotlin/eu/friedcutlet/yokan/ext/IosExtensionServerInstallerTest.kt

test / fixture207 lines
1@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)23package eu.friedcutlet.yokan.ext45import kotlinx.coroutines.test.runTest6import okio.FileSystem7import okio.ByteString.Companion.decodeBase648import okio.Path9import okio.Path.Companion.toPath10import okio.buffer11import platform.Foundation.NSTemporaryDirectory12import platform.Foundation.NSUUID13import kotlin.test.Test14import kotlin.test.assertEquals15import kotlin.test.assertFailsWith16import kotlin.test.assertTrue1718class IosExtensionServerInstallerTest {19    private val fileSystem = FileSystem.SYSTEM2021    @Test22    fun importsJarAtomicallyAndUsesBundledJvm() = withTemporaryRoot { root ->23        val source = root / "MExtensionServer-v1.2.3.jar"24        writeText(source, "server")25        val installer = IosExtensionServerInstaller(26            installRoot = (root / "installed").toString(),27            bundledJvmPath = "/app/java-21-openjdk",28        )2930        val installed = installer.importJar(source.toString())3132        assertEquals("1.2.3", installed.version)33        assertEquals("/app/java-21-openjdk", installed.jvmPath)34        assertTrue(fileSystem.exists(installed.serverJarPath.toPath()))35        assertEquals("server", readText(installed.serverJarPath.toPath()))36    }3738    @Test39    fun rejectsNonJarImports() = withTemporaryRoot { root ->40        val source = root / "MExtensionServer.txt"41        writeText(source, "not a jar")42        val installer = IosExtensionServerInstaller(43            installRoot = (root / "installed").toString(),44            bundledJvmPath = "/app/java-21-openjdk",45        )4647        val error = assertFailsWith<ExtensionServerException> {48            installer.importJar(source.toString())49        }5051        assertEquals("MISSING_JAR", error.code)52    }5354    @Test55    fun rejectsUntrustedReleaseBeforeDownload() = runTest {56        withTemporaryRootSuspend { root ->57            val installRoot = root / "installed"58            val asset = releaseAsset()59            var downloadCalled = false60            val installer = IosExtensionServerInstaller.createForTesting(61                installRoot = installRoot.toString(),62                bundledJvmPath = "/app/java-21-openjdk",63                fileSystem = fileSystem,64                archiveDownloader = { _, _, _ -> downloadCalled = true },65                trustedDigest = { release, selected -> release.trustedSha256(selected) },66            )6768            val error = assertFailsWith<ExtensionServerException> {69                installer.install(ExtensionServerRelease("9.9.9", listOf(asset)), asset)70            }7172            assertEquals("UNTRUSTED_RELEASE", error.code)73            assertTrue(!downloadCalled)74            assertTrue(!fileSystem.exists(installRoot))75        }76    }7778    @Test79    fun verifiesChecksumAndSelectsExtensionServerJar() = runTest {80        withTemporaryRootSuspend { root ->81            val installRoot = root / "installed"82            val asset = releaseAsset()83            val progress = mutableListOf<Long>()84            val installer = testInstaller(installRoot, FixtureSha256) { _, target, onProgress ->85                writeBytes(target, FixtureArchive)86                onProgress(FixtureArchive.size.toLong(), FixtureArchive.size.toLong())87            }8889            val installed = installer.install(ExtensionServerRelease("1.0.4", listOf(asset)), asset) {90                    downloaded,91                    _,92                ->93                progress += downloaded94            }9596            assertEquals("MExtensionServer.jar", installed.serverJarPath.toPath().name)97            assertEquals("server", readText(installed.serverJarPath.toPath()))98            assertEquals(FixtureArchive.size.toLong(), progress.last())99        }100    }101102    @Test103    fun rejectsMissingTrustedDigest() = withTemporaryRoot { root ->104        val archive = root / "bundle.zip"105        writeBytes(archive, FixtureArchive)106107        val error = assertFailsWith<ExtensionServerException> {108            verifyIosExtensionServerArchiveSha256(fileSystem, archive, null)109        }110111        assertEquals("MISSING_DIGEST", error.code)112    }113114    @Test115    fun removesArchiveAndStagingAfterChecksumMismatch() = runTest {116        withTemporaryRootSuspend { root ->117            val installRoot = root / "installed"118            val asset = releaseAsset()119            val installer = testInstaller(installRoot, "0".repeat(64)) { _, target, _ ->120                writeBytes(target, FixtureArchive)121            }122123            val error = assertFailsWith<ExtensionServerException> {124                installer.install(ExtensionServerRelease("1.0.4", listOf(asset)), asset)125            }126127            assertEquals("CHECKSUM_MISMATCH", error.code)128            assertTrue(!fileSystem.exists(installRoot / ".download-1.0.4.zip"))129            assertTrue(!fileSystem.exists(installRoot / ".install-1.0.4"))130            assertTrue(!fileSystem.exists(installRoot / "1.0.4"))131        }132    }133134    private fun releaseAsset() = ExtensionServerReleaseAsset(135        name = "macOS-arm64-bundle.zip",136        downloadUrl = "https://example.test/bundle.zip",137        size = FixtureArchive.size.toLong(),138        sha256 = null,139    )140141    private fun testInstaller(142        installRoot: Path,143        digest: String,144        downloader: suspend (ExtensionServerReleaseAsset, Path, (Long, Long) -> Unit) -> Unit,145    ) = IosExtensionServerInstaller.createForTesting(146        installRoot = installRoot.toString(),147        bundledJvmPath = "/app/java-21-openjdk",148        fileSystem = fileSystem,149        archiveDownloader = downloader,150        trustedDigest = { _, _ -> digest },151    )152153    private fun writeBytes(path: Path, value: ByteArray) {154        val sink = fileSystem.sink(path).buffer()155        try {156            sink.write(value)157        } finally {158            sink.close()159        }160    }161162    private fun writeText(path: Path, value: String) {163        val sink = fileSystem.sink(path).buffer()164        try {165            sink.writeUtf8(value)166        } finally {167            sink.close()168        }169    }170171    private fun readText(path: Path): String {172        val source = fileSystem.source(path).buffer()173        return try {174            source.readUtf8()175        } finally {176            source.close()177        }178    }179180    private fun withTemporaryRoot(block: (Path) -> Unit) {181        val root = "${NSTemporaryDirectory().trimEnd('/')}/yokan-ext-${NSUUID.UUID().UUIDString}".toPath()182        fileSystem.createDirectories(root)183        try {184            block(root)185        } finally {186            fileSystem.deleteRecursively(root, mustExist = false)187        }188    }189190    private suspend fun withTemporaryRootSuspend(block: suspend (Path) -> Unit) {191        val root = "${NSTemporaryDirectory().trimEnd('/')}/yokan-ext-${NSUUID.UUID().UUIDString}".toPath()192        fileSystem.createDirectories(root)193        try {194            block(root)195        } finally {196            fileSystem.deleteRecursively(root, mustExist = false)197        }198    }199200    private companion object {201        const val FixtureSha256 = "ad0b8a48643602e5baf16bdc621000450254ea2280606a5b590a94ba245145b6"202        val FixtureArchive = checkNotNull(203            "UEsDBBQAAAAAAAAAIQAo6kUnBQAAAAUAAAAQAAAAYnVuZGxlL290aGVyLmphcmRlY295UEsDBBQAAAAAAAAAIQD21W1aBgAAAAYAAAAbAAAAYnVuZGxlL01FeHRlbnNpb25TZXJ2ZXIuamFyc2VydmVyUEsBAhQDFAAAAAAAAAAhACjqRScFAAAABQAAABAAAAAAAAAAAAAAAIABAAAAAGJ1bmRsZS9vdGhlci5qYXJQSwECFAMUAAAAAAAAACEA9tVtWgYAAAAGAAAAGwAAAAAAAAAAAAAAgAEzAAAAYnVuZGxlL01FeHRlbnNpb25TZXJ2ZXIuamFyUEsFBgAAAAACAAIAhwAAAHIAAAAAAA=="204                .decodeBase64(),205        ).toByteArray()206    }207}208

Search chapters, reference and source filenames.