Yokan / rebuild
Course 1 8624561

WasmBackupCompressionTest.kt

composeApp/src/wasmJsTest/kotlin/eu/friedcutlet/yokan/services/backup/WasmBackupCompressionTest.kt

test / fixture86 lines
1package eu.friedcutlet.yokan.services.backup23import kotlin.io.encoding.Base644import kotlin.io.encoding.ExperimentalEncodingApi5import kotlin.test.Test6import kotlin.test.assertContentEquals7import kotlin.test.assertFailsWith8import kotlin.test.assertNotNull9import kotlin.test.assertTrue10import kotlinx.coroutines.test.runTest1112@OptIn(ExperimentalEncodingApi::class)13class WasmBackupCompressionTest {14    /** Mangayomi-shaped ZIP: one raw-deflate *.backup.db entry, no data descriptor. */15    private val deflatedBackup = Base64.decode(16        "UEsDBBQAAAgIAByl/1yyOASaVwAAAH0AAAAuAAAAbWFuZ2F5b21pXzIwMjYtMDctMzFfMTJfMDBf" +17            "MDAuMDAwMDAwLmJhY2t1cC5kYjXNuw6AIAyF4XfpzOTIqxiHBhogKiWl3mJ8d6Pidr7l/CesJDVx" +18            "BgsdGJgxBwTbDwYcKgWWRLU5YlGSJs9bnhh9owq6se2YqrIcH5biUf8L2pXyU3t93VBLAQIUABQA" +19            "AAgIAByl/1yyOASaVwAAAH0AAAAuAAAAAAAAAAAAAACkgQAAAABtYW5nYXlvbWlfMjAyNi0wNy0z" +20            "MV8xMl8wMF8wMC4wMDAwMDAuYmFja3VwLmRiUEsFBgAAAAABAAEAXAAAAKMAAAAAAA==",21    )2223    private val storedBackup = Base64.decode(24        "UEsDBBQAAAAAAAFhH10WbRMSGgAAABoAAAAYAAAAbWFuZ2F5b21pX3Rlc3QuYmFja3VwLmRieyJ2ZXJzaW9uIjoiMiIsIm1hbmdhIjpbXX1QSwECFAMUAAAAAAABYR9dFm0TEhoAAAAaAAAAGAAAAAAAAAAAAAAAgAEAAAAAbWFuZ2F5b21pX3Rlc3QuYmFja3VwLmRiUEsFBgAAAAABAAEARgAAAFAAAAAAAA==",25    )2627    @Test28    fun decodesMangayomiRawDeflateEntry() = runTest {29        val entries = assertNotNull(createPlatformBackupCompression().unzipEntriesAsync(deflatedBackup))30        assertTrue(entries.keys.single().endsWith(".backup.db"))31        assertTrue(entries.values.single().decodeToString().contains("\"version\":\"2\""))32    }3334    @Test35    fun decodesStoredEntry() = runTest {36        val entry = assertNotNull(createPlatformBackupCompression().unzipFirstEntryAsync(storedBackup))37        assertContentEquals("{\"version\":\"2\",\"manga\":[]}".encodeToByteArray(), entry)38    }3940    @Test41    fun rejectsCorruptPayloadCrc() = runTest {42        val corrupted = storedBackup.copyOf().also { it[storedDataOffset(it)] = (it[storedDataOffset(it)].toInt() xor 1).toByte() }43        val error = assertFailsWith<IllegalStateException> {44            createPlatformBackupCompression().unzipFirstEntryAsync(corrupted)45        }46        assertTrue(error.message.orEmpty().contains("CRC-32 mismatch"))47    }4849    @Test50    fun rejectsMalformedEocdCommentLength() = runTest {51        val corrupted = storedBackup.copyOf().also { it[it.size - 2] = 1 }52        val error = assertFailsWith<IllegalStateException> {53            createPlatformBackupCompression().unzipEntriesAsync(corrupted)54        }55        assertTrue(error.message.orEmpty().contains("end-of-central-directory"))56    }5758    @Test59    fun rejectsEntryOverOutputLimitBeforeReadingIt() = runTest {60        val oversized = storedBackup.copyOf()61        val central = centralDirectoryOffset(oversized)62        writeLe32(oversized, central + 24, 64L * 1024L * 1024L + 1L)63        val error = assertFailsWith<IllegalStateException> {64            createPlatformBackupCompression().unzipFirstEntryAsync(oversized)65        }66        assertTrue(error.message.orEmpty().contains("64 MiB"))67    }6869    private fun storedDataOffset(zip: ByteArray): Int =70        30 + readLe16(zip, 26) + readLe16(zip, 28)7172    private fun centralDirectoryOffset(zip: ByteArray): Int = readLe32(zip, zip.size - 22 + 16).toInt()7374    private fun readLe16(data: ByteArray, offset: Int): Int =75        (data[offset].toInt() and 0xFF) or ((data[offset + 1].toInt() and 0xFF) shl 8)7677    private fun readLe32(data: ByteArray, offset: Int): Long =78        (data[offset].toLong() and 0xFFL) or79            ((data[offset + 1].toLong() and 0xFFL) shl 8) or80            ((data[offset + 2].toLong() and 0xFFL) shl 16) or81            ((data[offset + 3].toLong() and 0xFFL) shl 24)8283    private fun writeLe32(data: ByteArray, offset: Int, value: Long) {84        repeat(4) { index -> data[offset + index] = (value ushr (index * 8)).toByte() }85    }86}87

Search chapters, reference and source filenames.