package eu.friedcutlet.yokan.services.backup import kotlin.io.encoding.Base64 import kotlin.io.encoding.ExperimentalEncodingApi import kotlin.test.Test import kotlin.test.assertContentEquals import kotlin.test.assertFailsWith import kotlin.test.assertNotNull import kotlin.test.assertTrue import kotlinx.coroutines.test.runTest @OptIn(ExperimentalEncodingApi::class) class WasmBackupCompressionTest { /** Mangayomi-shaped ZIP: one raw-deflate *.backup.db entry, no data descriptor. */ private val deflatedBackup = Base64.decode( "UEsDBBQAAAgIAByl/1yyOASaVwAAAH0AAAAuAAAAbWFuZ2F5b21pXzIwMjYtMDctMzFfMTJfMDBf" + "MDAuMDAwMDAwLmJhY2t1cC5kYjXNuw6AIAyF4XfpzOTIqxiHBhogKiWl3mJ8d6Pidr7l/CesJDVx" + "BgsdGJgxBwTbDwYcKgWWRLU5YlGSJs9bnhh9owq6se2YqrIcH5biUf8L2pXyU3t93VBLAQIUABQA" + "AAgIAByl/1yyOASaVwAAAH0AAAAuAAAAAAAAAAAAAACkgQAAAABtYW5nYXlvbWlfMjAyNi0wNy0z" + "MV8xMl8wMF8wMC4wMDAwMDAuYmFja3VwLmRiUEsFBgAAAAABAAEAXAAAAKMAAAAAAA==", ) private val storedBackup = Base64.decode( "UEsDBBQAAAAAAAFhH10WbRMSGgAAABoAAAAYAAAAbWFuZ2F5b21pX3Rlc3QuYmFja3VwLmRieyJ2ZXJzaW9uIjoiMiIsIm1hbmdhIjpbXX1QSwECFAMUAAAAAAABYR9dFm0TEhoAAAAaAAAAGAAAAAAAAAAAAAAAgAEAAAAAbWFuZ2F5b21pX3Rlc3QuYmFja3VwLmRiUEsFBgAAAAABAAEARgAAAFAAAAAAAA==", ) @Test fun decodesMangayomiRawDeflateEntry() = runTest { val entries = assertNotNull(createPlatformBackupCompression().unzipEntriesAsync(deflatedBackup)) assertTrue(entries.keys.single().endsWith(".backup.db")) assertTrue(entries.values.single().decodeToString().contains("\"version\":\"2\"")) } @Test fun decodesStoredEntry() = runTest { val entry = assertNotNull(createPlatformBackupCompression().unzipFirstEntryAsync(storedBackup)) assertContentEquals("{\"version\":\"2\",\"manga\":[]}".encodeToByteArray(), entry) } @Test fun rejectsCorruptPayloadCrc() = runTest { val corrupted = storedBackup.copyOf().also { it[storedDataOffset(it)] = (it[storedDataOffset(it)].toInt() xor 1).toByte() } val error = assertFailsWith { createPlatformBackupCompression().unzipFirstEntryAsync(corrupted) } assertTrue(error.message.orEmpty().contains("CRC-32 mismatch")) } @Test fun rejectsMalformedEocdCommentLength() = runTest { val corrupted = storedBackup.copyOf().also { it[it.size - 2] = 1 } val error = assertFailsWith { createPlatformBackupCompression().unzipEntriesAsync(corrupted) } assertTrue(error.message.orEmpty().contains("end-of-central-directory")) } @Test fun rejectsEntryOverOutputLimitBeforeReadingIt() = runTest { val oversized = storedBackup.copyOf() val central = centralDirectoryOffset(oversized) writeLe32(oversized, central + 24, 64L * 1024L * 1024L + 1L) val error = assertFailsWith { createPlatformBackupCompression().unzipFirstEntryAsync(oversized) } assertTrue(error.message.orEmpty().contains("64 MiB")) } private fun storedDataOffset(zip: ByteArray): Int = 30 + readLe16(zip, 26) + readLe16(zip, 28) private fun centralDirectoryOffset(zip: ByteArray): Int = readLe32(zip, zip.size - 22 + 16).toInt() private fun readLe16(data: ByteArray, offset: Int): Int = (data[offset].toInt() and 0xFF) or ((data[offset + 1].toInt() and 0xFF) shl 8) private fun readLe32(data: ByteArray, offset: Int): Long = (data[offset].toLong() and 0xFFL) or ((data[offset + 1].toLong() and 0xFFL) shl 8) or ((data[offset + 2].toLong() and 0xFFL) shl 16) or ((data[offset + 3].toLong() and 0xFFL) shl 24) private fun writeLe32(data: ByteArray, offset: Int, value: Long) { repeat(4) { index -> data[offset + index] = (value ushr (index * 8)).toByte() } } }