diff --git a/cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt b/cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt index d2d21ab7..706f1553 100644 --- a/cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt +++ b/cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt @@ -10,6 +10,7 @@ import java.nio.file.Path import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith +import kotlin.test.assertNull class Js5MasterIndexTest { @Test @@ -123,6 +124,35 @@ class Js5MasterIndexTest { ) } + @Test + fun testCreateCorruptIndex() { + val index = Store.open(ROOT.resolve("corrupt")).use { store -> + Js5MasterIndex.create(store) + } + + assertEquals( + Js5MasterIndex( + MasterIndexFormat.ORIGINAL, + mutableListOf( + Js5MasterIndex.Entry( + 0, 609698396, 0, 0, ByteBufUtil.decodeHexDump( + "0e1a2b93c80a41c7ad2a985dff707a6a8ff82e229cbc468f04191198920955a1" + + "4b3d7eab77a17faf99208dee5b44afb789962ad79f230b3b59106a0af892219c" + ) + ), + Js5MasterIndex.Entry(0, 0, 0, 0, null), + Js5MasterIndex.Entry( + 0, 609698396, 0, 0, ByteBufUtil.decodeHexDump( + "0e1a2b93c80a41c7ad2a985dff707a6a8ff82e229cbc468f04191198920955a1" + + "4b3d7eab77a17faf99208dee5b44afb789962ad79f230b3b59106a0af892219c" + ) + ) + ) + ), + index + ) + } + @Test fun testReadOriginal() { Unpooled.wrappedBuffer(encodedOriginal).use { buf -> @@ -283,6 +313,26 @@ class Js5MasterIndexTest { } } + @Test + fun testSetDigest() { + val entry = Js5MasterIndex.Entry(0, 0, 0, 0, null) + + val digest = ByteArray(Whirlpool.DIGESTBYTES) { it.toByte() } + entry.digest = digest + assertEquals(digest, entry.digest) + + entry.digest = null + assertNull(entry.digest) + + assertFailsWith { + entry.digest = ByteArray(Whirlpool.DIGESTBYTES - 1) + } + + assertFailsWith { + entry.digest = ByteArray(Whirlpool.DIGESTBYTES + 1) + } + } + private companion object { private val ROOT = Path.of(FlatFileStoreTest::class.java.getResource("master-index").toURI()) private val PRIVATE_KEY = Rsa.readPrivateKey(ROOT.resolve("private.key")) diff --git a/cache/src/test/resources/org/openrs2/cache/master-index/corrupt/main_file_cache.dat2 b/cache/src/test/resources/org/openrs2/cache/master-index/corrupt/main_file_cache.dat2 new file mode 100644 index 00000000..9b035c92 Binary files /dev/null and b/cache/src/test/resources/org/openrs2/cache/master-index/corrupt/main_file_cache.dat2 differ diff --git a/cache/src/test/resources/org/openrs2/cache/master-index/corrupt/main_file_cache.idx255 b/cache/src/test/resources/org/openrs2/cache/master-index/corrupt/main_file_cache.idx255 new file mode 100644 index 00000000..196f68d1 Binary files /dev/null and b/cache/src/test/resources/org/openrs2/cache/master-index/corrupt/main_file_cache.idx255 differ