Yokan / rebuild
Course 1 8624561

DesktopExtensionServerInstallerTest.kt

vendor/yokan-ext/src/desktopTest/kotlin/eu/friedcutlet/yokan/ext/DesktopExtensionServerInstallerTest.kt

test / fixture103 lines
1package eu.friedcutlet.yokan.ext23import com.sun.net.httpserver.HttpServer4import java.net.InetSocketAddress5import java.nio.file.Files6import java.security.MessageDigest7import java.util.zip.ZipEntry8import java.util.zip.ZipOutputStream9import kotlinx.coroutines.test.runTest10import kotlin.io.path.exists11import kotlin.test.Test12import kotlin.test.assertEquals13import kotlin.test.assertFailsWith14import kotlin.test.assertTrue1516class DesktopExtensionServerInstallerTest {17    @Test18    fun verifiesExtractsAndResolvesBundle() = runTest {19        val root = Files.createTempDirectory("yokan-ext-installer-")20        val archive = createBundleArchive(root)21        val server = serveFile(archive)22        try {23            val asset = ExtensionServerReleaseAsset(24                name = "test-bundle.zip",25                downloadUrl = "http://127.0.0.1:${server.address.port}/bundle.zip",26                size = Files.size(archive),27                sha256 = sha256(archive),28            )29            val release = ExtensionServerRelease("9.9.9", listOf(asset))30            val progress = mutableListOf<Long>()31            val bundle = DesktopExtensionServerInstaller(root.resolve("installed"))32                .install(release, asset) { downloaded, _ -> progress += downloaded }3334            assertEquals("9.9.9", bundle.version)35            assertTrue(Files.isRegularFile(java.nio.file.Path.of(bundle.serverJarPath)))36            assertTrue(Files.isRegularFile(java.nio.file.Path.of(bundle.jvmPath)))37            assertTrue(progress.last() == asset.size)38        } finally {39            server.stop(0)40            deleteRecursively(root)41        }42    }4344    @Test45    fun rejectsDigestMismatchWithoutInstallingVersion() = runTest {46        val root = Files.createTempDirectory("yokan-ext-digest-")47        val archive = createBundleArchive(root)48        val server = serveFile(archive)49        try {50            val asset = ExtensionServerReleaseAsset(51                "test-bundle.zip",52                "http://127.0.0.1:${server.address.port}/bundle.zip",53                Files.size(archive),54                "0".repeat(64),55            )56            val installer = DesktopExtensionServerInstaller(root.resolve("installed"))57            val error = assertFailsWith<ExtensionServerException> {58                installer.install(ExtensionServerRelease("1.0.0", listOf(asset)), asset)59            }60            assertEquals("DIGEST_MISMATCH", error.code)61            assertTrue(!root.resolve("installed/1.0.0").exists())62        } finally {63            server.stop(0)64            deleteRecursively(root)65        }66    }6768    private fun createBundleArchive(root: java.nio.file.Path): java.nio.file.Path {69        val archive = root.resolve("bundle.zip")70        ZipOutputStream(Files.newOutputStream(archive)).use { zip ->71            mapOf(72                "bundle/jre/bin/${if (System.getProperty("os.name").contains("win", true)) "java.exe" else "java"}" to "fake java",73                "bundle/MExtensionServer.jar" to "fake jar",74            ).forEach { (name, content) ->75                zip.putNextEntry(ZipEntry(name))76                zip.write(content.encodeToByteArray())77                zip.closeEntry()78            }79        }80        return archive81    }8283    private fun serveFile(path: java.nio.file.Path): HttpServer {84        val server = HttpServer.create(InetSocketAddress("127.0.0.1", 0), 0)85        server.createContext("/bundle.zip") { exchange ->86            val bytes = Files.readAllBytes(path)87            exchange.sendResponseHeaders(200, bytes.size.toLong())88            exchange.responseBody.use { it.write(bytes) }89        }90        server.start()91        return server92    }9394    private fun sha256(path: java.nio.file.Path): String {95        val digest = MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(path))96        return digest.joinToString("") { "%02x".format(it) }97    }9899    private fun deleteRecursively(path: java.nio.file.Path) {100        if (!Files.exists(path)) return101        Files.walk(path).use { paths -> paths.sorted(Comparator.reverseOrder()).forEach(Files::deleteIfExists) }102    }103}104

Search chapters, reference and source filenames.