@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) package eu.friedcutlet.yokan.ext import kotlinx.coroutines.test.runTest import okio.FileSystem import okio.ByteString.Companion.decodeBase64 import okio.Path import okio.Path.Companion.toPath import okio.buffer import platform.Foundation.NSTemporaryDirectory import platform.Foundation.NSUUID import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertTrue class IosExtensionServerInstallerTest { private val fileSystem = FileSystem.SYSTEM @Test fun importsJarAtomicallyAndUsesBundledJvm() = withTemporaryRoot { root -> val source = root / "MExtensionServer-v1.2.3.jar" writeText(source, "server") val installer = IosExtensionServerInstaller( installRoot = (root / "installed").toString(), bundledJvmPath = "/app/java-21-openjdk", ) val installed = installer.importJar(source.toString()) assertEquals("1.2.3", installed.version) assertEquals("/app/java-21-openjdk", installed.jvmPath) assertTrue(fileSystem.exists(installed.serverJarPath.toPath())) assertEquals("server", readText(installed.serverJarPath.toPath())) } @Test fun rejectsNonJarImports() = withTemporaryRoot { root -> val source = root / "MExtensionServer.txt" writeText(source, "not a jar") val installer = IosExtensionServerInstaller( installRoot = (root / "installed").toString(), bundledJvmPath = "/app/java-21-openjdk", ) val error = assertFailsWith { installer.importJar(source.toString()) } assertEquals("MISSING_JAR", error.code) } @Test fun rejectsUntrustedReleaseBeforeDownload() = runTest { withTemporaryRootSuspend { root -> val installRoot = root / "installed" val asset = releaseAsset() var downloadCalled = false val installer = IosExtensionServerInstaller.createForTesting( installRoot = installRoot.toString(), bundledJvmPath = "/app/java-21-openjdk", fileSystem = fileSystem, archiveDownloader = { _, _, _ -> downloadCalled = true }, trustedDigest = { release, selected -> release.trustedSha256(selected) }, ) val error = assertFailsWith { installer.install(ExtensionServerRelease("9.9.9", listOf(asset)), asset) } assertEquals("UNTRUSTED_RELEASE", error.code) assertTrue(!downloadCalled) assertTrue(!fileSystem.exists(installRoot)) } } @Test fun verifiesChecksumAndSelectsExtensionServerJar() = runTest { withTemporaryRootSuspend { root -> val installRoot = root / "installed" val asset = releaseAsset() val progress = mutableListOf() val installer = testInstaller(installRoot, FixtureSha256) { _, target, onProgress -> writeBytes(target, FixtureArchive) onProgress(FixtureArchive.size.toLong(), FixtureArchive.size.toLong()) } val installed = installer.install(ExtensionServerRelease("1.0.4", listOf(asset)), asset) { downloaded, _, -> progress += downloaded } assertEquals("MExtensionServer.jar", installed.serverJarPath.toPath().name) assertEquals("server", readText(installed.serverJarPath.toPath())) assertEquals(FixtureArchive.size.toLong(), progress.last()) } } @Test fun rejectsMissingTrustedDigest() = withTemporaryRoot { root -> val archive = root / "bundle.zip" writeBytes(archive, FixtureArchive) val error = assertFailsWith { verifyIosExtensionServerArchiveSha256(fileSystem, archive, null) } assertEquals("MISSING_DIGEST", error.code) } @Test fun removesArchiveAndStagingAfterChecksumMismatch() = runTest { withTemporaryRootSuspend { root -> val installRoot = root / "installed" val asset = releaseAsset() val installer = testInstaller(installRoot, "0".repeat(64)) { _, target, _ -> writeBytes(target, FixtureArchive) } val error = assertFailsWith { installer.install(ExtensionServerRelease("1.0.4", listOf(asset)), asset) } assertEquals("CHECKSUM_MISMATCH", error.code) assertTrue(!fileSystem.exists(installRoot / ".download-1.0.4.zip")) assertTrue(!fileSystem.exists(installRoot / ".install-1.0.4")) assertTrue(!fileSystem.exists(installRoot / "1.0.4")) } } private fun releaseAsset() = ExtensionServerReleaseAsset( name = "macOS-arm64-bundle.zip", downloadUrl = "https://example.test/bundle.zip", size = FixtureArchive.size.toLong(), sha256 = null, ) private fun testInstaller( installRoot: Path, digest: String, downloader: suspend (ExtensionServerReleaseAsset, Path, (Long, Long) -> Unit) -> Unit, ) = IosExtensionServerInstaller.createForTesting( installRoot = installRoot.toString(), bundledJvmPath = "/app/java-21-openjdk", fileSystem = fileSystem, archiveDownloader = downloader, trustedDigest = { _, _ -> digest }, ) private fun writeBytes(path: Path, value: ByteArray) { val sink = fileSystem.sink(path).buffer() try { sink.write(value) } finally { sink.close() } } private fun writeText(path: Path, value: String) { val sink = fileSystem.sink(path).buffer() try { sink.writeUtf8(value) } finally { sink.close() } } private fun readText(path: Path): String { val source = fileSystem.source(path).buffer() return try { source.readUtf8() } finally { source.close() } } private fun withTemporaryRoot(block: (Path) -> Unit) { val root = "${NSTemporaryDirectory().trimEnd('/')}/yokan-ext-${NSUUID.UUID().UUIDString}".toPath() fileSystem.createDirectories(root) try { block(root) } finally { fileSystem.deleteRecursively(root, mustExist = false) } } private suspend fun withTemporaryRootSuspend(block: suspend (Path) -> Unit) { val root = "${NSTemporaryDirectory().trimEnd('/')}/yokan-ext-${NSUUID.UUID().UUIDString}".toPath() fileSystem.createDirectories(root) try { block(root) } finally { fileSystem.deleteRecursively(root, mustExist = false) } } private companion object { const val FixtureSha256 = "ad0b8a48643602e5baf16bdc621000450254ea2280606a5b590a94ba245145b6" val FixtureArchive = checkNotNull( "UEsDBBQAAAAAAAAAIQAo6kUnBQAAAAUAAAAQAAAAYnVuZGxlL290aGVyLmphcmRlY295UEsDBBQAAAAAAAAAIQD21W1aBgAAAAYAAAAbAAAAYnVuZGxlL01FeHRlbnNpb25TZXJ2ZXIuamFyc2VydmVyUEsBAhQDFAAAAAAAAAAhACjqRScFAAAABQAAABAAAAAAAAAAAAAAAIABAAAAAGJ1bmRsZS9vdGhlci5qYXJQSwECFAMUAAAAAAAAACEA9tVtWgYAAAAGAAAAGwAAAAAAAAAAAAAAgAEzAAAAYnVuZGxlL01FeHRlbnNpb25TZXJ2ZXIuamFyUEsFBgAAAAACAAIAhwAAAHIAAAAAAA==" .decodeBase64(), ).toByteArray() } }