From b0f163210565bb8067ff39c8a529ad2131361c1e Mon Sep 17 00:00:00 2001 From: Graham Date: Fri, 9 Apr 2021 14:29:12 +0100 Subject: [PATCH] Improve Js5MasterIndex code coverage Signed-off-by: Graham --- .../org/openrs2/cache/Js5MasterIndexTest.kt | 50 ++++++++++++++++++ .../master-index/corrupt/main_file_cache.dat2 | Bin 0 -> 1057 bytes .../corrupt/main_file_cache.idx255 | Bin 0 -> 18 bytes 3 files changed, 50 insertions(+) create mode 100644 cache/src/test/resources/org/openrs2/cache/master-index/corrupt/main_file_cache.dat2 create mode 100644 cache/src/test/resources/org/openrs2/cache/master-index/corrupt/main_file_cache.idx255 diff --git a/cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt b/cache/src/test/kotlin/org/openrs2/cache/Js5MasterIndexTest.kt index d2d21ab703..706f15536c 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 0000000000000000000000000000000000000000..9b035c92f13cb217abd2ce803da147a27f932c11 GIT binary patch literal 1057 hcmZQz7zLvt01tuxAQlTNoIlDKX(7M__AnwH001!C0uulL literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..196f68d147966cd9f84850b98b275c7dfb661ad9 GIT binary patch literal 18 YcmZQz;ACK6WMJU@|NlRbWME(d02vnp8~^|S literal 0 HcmV?d00001