|
| 1 | +package pl.syntaxdevteam.syntaxlauncher.core.mods |
| 2 | + |
| 3 | +import io.ktor.client.HttpClient |
| 4 | +import io.ktor.client.engine.mock.MockEngine |
| 5 | +import io.ktor.client.engine.mock.respond |
| 6 | +import java.io.ByteArrayOutputStream |
| 7 | +import java.nio.file.Files |
| 8 | +import java.nio.file.Path |
| 9 | +import java.security.MessageDigest |
| 10 | +import java.util.zip.ZipEntry |
| 11 | +import java.util.zip.ZipOutputStream |
| 12 | +import kotlin.test.Test |
| 13 | +import kotlin.test.assertEquals |
| 14 | +import kotlin.test.assertFailsWith |
| 15 | +import kotlin.test.assertTrue |
| 16 | +import kotlinx.coroutines.runBlocking |
| 17 | +import pl.syntaxdevteam.syntaxlauncher.core.instance.MinecraftInstanceRepository |
| 18 | +import pl.syntaxdevteam.syntaxlauncher.core.loader.ModLoaderSelection |
| 19 | +import pl.syntaxdevteam.syntaxlauncher.core.network.DownloadManager |
| 20 | +import pl.syntaxdevteam.syntaxlauncher.core.network.DownloadRetryPolicy |
| 21 | +import pl.syntaxdevteam.syntaxlauncher.core.storage.LauncherPaths |
| 22 | +import pl.syntaxdevteam.syntaxlauncher.core.storage.MinecraftInstallationLayout |
| 23 | + |
| 24 | +class ModrinthInstallerTest { |
| 25 | + private fun jar(modId: String): ByteArray { |
| 26 | + val bytes = ByteArrayOutputStream() |
| 27 | + ZipOutputStream(bytes).use { zip -> |
| 28 | + zip.putNextEntry(ZipEntry("fabric.mod.json")) |
| 29 | + zip.write("""{"schemaVersion":1,"id":"$modId","name":"$modId","version":"1.0"}""".toByteArray()) |
| 30 | + zip.closeEntry() |
| 31 | + } |
| 32 | + return bytes.toByteArray() |
| 33 | + } |
| 34 | + |
| 35 | + private fun sha1(bytes: ByteArray): String = |
| 36 | + MessageDigest.getInstance("SHA-1").digest(bytes).joinToString("") { "%02x".format(it) } |
| 37 | + |
| 38 | + private fun setup(): Pair<MinecraftInstanceRepository, String> { |
| 39 | + val root = Files.createTempDirectory("modrinth-install-test") |
| 40 | + val paths = LauncherPaths(root.resolve("data"), root.resolve("config"), |
| 41 | + root.resolve("cache"), root.resolve("logs"), root.resolve("runtimes")) |
| 42 | + val instances = MinecraftInstanceRepository(paths.dataDir.resolve("instances"), |
| 43 | + paths.dataDir.resolve("trash"), MinecraftInstallationLayout.from(paths)) |
| 44 | + return instances to "26.3" |
| 45 | + } |
| 46 | + |
| 47 | + @Test fun installsReviewedDependenciesAsAnInstanceScopedBatch() = runBlocking { |
| 48 | + val (instances, game) = setup() |
| 49 | + val profile = instances.create("A", game, loader = ModLoaderSelection("fabric", "0.18.0")) |
| 50 | + val other = instances.create("B", game, loader = ModLoaderSelection("fabric", "0.18.0")) |
| 51 | + val first = jar("dependency") |
| 52 | + val second = jar("mainmod") |
| 53 | + val client = HttpClient(MockEngine { req -> |
| 54 | + respond(if (req.url.encodedPath.endsWith("/dependency.jar")) first else second) |
| 55 | + }) |
| 56 | + try { |
| 57 | + val catalog = LocalModManager(instances) |
| 58 | + val installer = ModrinthInstaller(instances, catalog, |
| 59 | + DownloadManager(client, retryPolicy = DownloadRetryPolicy(maxAttempts = 1))) |
| 60 | + val plan = ModrinthInstallPlan(game, "fabric", "ABCDEFGH", listOf( |
| 61 | + ModrinthInstallEntry("BCDEFGHI", "RELEASEB", "dep", "1.0", |
| 62 | + ModrinthFile("dependency.jar", "https://cdn.modrinth.com/data/dep/dependency.jar", |
| 63 | + sha1(first), first.size.toLong()), true), |
| 64 | + ModrinthInstallEntry("ABCDEFGH", "RELEASEA", "main", "1.0", |
| 65 | + ModrinthFile("main.jar", "https://cdn.modrinth.com/data/main/main.jar", |
| 66 | + sha1(second), second.size.toLong()), false), |
| 67 | + ), emptyList()) |
| 68 | + val installed = installer.install(profile.id, plan) |
| 69 | + assertEquals(setOf("mainmod", "dependency"), installed.mods.mapNotNull { it.metadata?.id }.toSet()) |
| 70 | + assertTrue(installed.mods.all { it.enabled }) |
| 71 | + assertTrue(catalog.list(other.id).mods.isEmpty()) |
| 72 | + assertFailsWith<IllegalStateException> { installer.install(profile.id, plan) } |
| 73 | + assertEquals(2, catalog.list(profile.id).mods.size) |
| 74 | + } finally { client.close() } |
| 75 | + } |
| 76 | + |
| 77 | + @Test fun badChecksumRejectsEntireInstallationWithoutPublishingJars() = runBlocking { |
| 78 | + val (instances, game) = setup() |
| 79 | + val profile = instances.create("A", game, loader = ModLoaderSelection("fabric", "0.18.0")) |
| 80 | + val bytes = jar("mainmod") |
| 81 | + val client = HttpClient(MockEngine { respond(bytes) }) |
| 82 | + try { |
| 83 | + val mods = LocalModManager(instances) |
| 84 | + val installer = ModrinthInstaller(instances, mods, |
| 85 | + DownloadManager(client, retryPolicy = DownloadRetryPolicy(maxAttempts = 1))) |
| 86 | + val plan = ModrinthInstallPlan(game, "fabric", "ABCDEFGH", |
| 87 | + listOf(ModrinthInstallEntry("ABCDEFGH", "RELEASEA", "main", "1.0", |
| 88 | + ModrinthFile("main.jar", "https://cdn.modrinth.com/data/main/main.jar", |
| 89 | + "0000000000000000000000000000000000000000", bytes.size.toLong()), false)), |
| 90 | + emptyList()) |
| 91 | + assertFailsWith<Exception> { installer.install(profile.id, plan) } |
| 92 | + assertTrue(mods.list(profile.id).mods.isEmpty()) |
| 93 | + assertTrue(Files.newDirectoryStream(instances.gameDirectory(profile)).use { |
| 94 | + it.none { entry -> entry.fileName.toString().startsWith(".modrinth-download-") } |
| 95 | + }) |
| 96 | + } finally { client.close() } |
| 97 | + } |
| 98 | +} |
0 commit comments