Add shorthand methods for creating ByteBufs from ByteArrays and Strings

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent 5036eb3da8
commit 76977303b2
  1. 10
      buffer/src/main/kotlin/org/openrs2/buffer/ByteBufExtensions.kt
  2. 143
      buffer/src/test/kotlin/org/openrs2/buffer/ByteBufExtensionsTest.kt
  3. 50
      cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt
  4. 94
      cache/src/test/kotlin/org/openrs2/cache/BufferedFileChannelTest.kt
  5. 103
      cache/src/test/kotlin/org/openrs2/cache/DiskStoreTest.kt
  6. 9
      cache/src/test/kotlin/org/openrs2/cache/FlatFileStoreTest.kt
  7. 39
      cache/src/test/kotlin/org/openrs2/cache/Js5CompressionTest.kt
  8. 5
      cache/src/test/kotlin/org/openrs2/cache/Js5IndexTest.kt
  9. 11
      cache/src/test/kotlin/org/openrs2/cache/VersionTrailerTest.kt
  10. 10
      crypto/src/test/kotlin/org/openrs2/crypto/RsaTest.kt
  11. 7
      protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5RequestDecoderTest.kt
  12. 41
      protocol/src/test/kotlin/org/openrs2/protocol/js5/Js5ResponseDecoderTest.kt
  13. 8
      protocol/src/test/kotlin/org/openrs2/protocol/js5/XorDecoderTest.kt
  14. 8
      protocol/src/test/kotlin/org/openrs2/protocol/js5/XorEncoderTest.kt

@ -3,8 +3,18 @@ package org.openrs2.buffer
import com.google.common.base.Preconditions import com.google.common.base.Preconditions
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufUtil import io.netty.buffer.ByteBufUtil
import io.netty.buffer.Unpooled
import java.nio.charset.Charset
import java.util.zip.CRC32 import java.util.zip.CRC32
public fun wrappedBuffer(vararg bytes: Byte): ByteBuf {
return Unpooled.wrappedBuffer(bytes)
}
public fun copiedBuffer(s: String, charset: Charset = Charsets.UTF_8): ByteBuf {
return Unpooled.copiedBuffer(s, charset)
}
public fun ByteBuf.readShortSmart(): Int { public fun ByteBuf.readShortSmart(): Int {
val peek = getUnsignedByte(readerIndex()).toInt() val peek = getUnsignedByte(readerIndex()).toInt()
return if ((peek and 0x80) == 0) { return if ((peek and 0x80) == 0) {

@ -8,39 +8,60 @@ import kotlin.test.assertEquals
import kotlin.test.assertFalse import kotlin.test.assertFalse
object ByteBufExtensionsTest { object ByteBufExtensionsTest {
@Test
fun testWrappedBuffer() {
wrappedBuffer(1, 2, 3).use { actual ->
Unpooled.wrappedBuffer(byteArrayOf(1, 2, 3)).use { expected ->
assertEquals(expected, actual)
}
}
copiedBuffer("ØpenRS2").use { actual ->
Unpooled.wrappedBuffer("ØpenRS2".toByteArray()).use { expected ->
assertEquals(expected, actual)
}
}
copiedBuffer("ØpenRS2", Charsets.UTF_16BE).use { actual ->
Unpooled.wrappedBuffer("ØpenRS2".toByteArray(Charsets.UTF_16BE)).use { expected ->
assertEquals(expected, actual)
}
}
}
@Test @Test
fun testReadShortSmart() { fun testReadShortSmart() {
Unpooled.wrappedBuffer(byteArrayOf(0x00)).use { buf -> wrappedBuffer(0x00).use { buf ->
assertEquals(-0x40, buf.readShortSmart()) assertEquals(-0x40, buf.readShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x40)).use { buf -> wrappedBuffer(0x40).use { buf ->
assertEquals(0, buf.readShortSmart()) assertEquals(0, buf.readShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x7F)).use { buf -> wrappedBuffer(0x7F).use { buf ->
assertEquals(0x3F, buf.readShortSmart()) assertEquals(0x3F, buf.readShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x80.toByte(), 0x00)).use { buf -> wrappedBuffer(0x80.toByte(), 0x00).use { buf ->
assertEquals(-0x4000, buf.readShortSmart()) assertEquals(-0x4000, buf.readShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xBF.toByte(), 0xBF.toByte())).use { buf -> wrappedBuffer(0xBF.toByte(), 0xBF.toByte()).use { buf ->
assertEquals(-0x41, buf.readShortSmart()) assertEquals(-0x41, buf.readShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xC0.toByte(), 0x40.toByte())).use { buf -> wrappedBuffer(0xC0.toByte(), 0x40.toByte()).use { buf ->
assertEquals(0x40, buf.readShortSmart()) assertEquals(0x40, buf.readShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xFF.toByte(), 0xFF.toByte())).use { buf -> wrappedBuffer(0xFF.toByte(), 0xFF.toByte()).use { buf ->
assertEquals(0x3FFF, buf.readShortSmart()) assertEquals(0x3FFF, buf.readShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
@ -48,49 +69,49 @@ object ByteBufExtensionsTest {
@Test @Test
fun testWriteShortSmart() { fun testWriteShortSmart() {
Unpooled.wrappedBuffer(byteArrayOf(0x00)).use { expected -> wrappedBuffer(0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeShortSmart(-0x40) actual.writeShortSmart(-0x40)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x40)).use { expected -> wrappedBuffer(0x40).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeShortSmart(0) actual.writeShortSmart(0)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x7F)).use { expected -> wrappedBuffer(0x7F).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeShortSmart(0x3F) actual.writeShortSmart(0x3F)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x80.toByte(), 0x00)).use { expected -> wrappedBuffer(0x80.toByte(), 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeShortSmart(-0x4000) actual.writeShortSmart(-0x4000)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0xBF.toByte(), 0xBF.toByte())).use { expected -> wrappedBuffer(0xBF.toByte(), 0xBF.toByte()).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeShortSmart(-0x41) actual.writeShortSmart(-0x41)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0xC0.toByte(), 0x40.toByte())).use { expected -> wrappedBuffer(0xC0.toByte(), 0x40.toByte()).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeShortSmart(0x40) actual.writeShortSmart(0x40)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0xFF.toByte(), 0xFF.toByte())).use { expected -> wrappedBuffer(0xFF.toByte(), 0xFF.toByte()).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeShortSmart(0x3FFF) actual.writeShortSmart(0x3FFF)
assertEquals(expected, actual) assertEquals(expected, actual)
@ -112,32 +133,32 @@ object ByteBufExtensionsTest {
@Test @Test
fun testReadUnsignedShortSmart() { fun testReadUnsignedShortSmart() {
Unpooled.wrappedBuffer(byteArrayOf(0x00)).use { buf -> wrappedBuffer(0x00).use { buf ->
assertEquals(0, buf.readUnsignedShortSmart()) assertEquals(0, buf.readUnsignedShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x40)).use { buf -> wrappedBuffer(0x40).use { buf ->
assertEquals(0x40, buf.readUnsignedShortSmart()) assertEquals(0x40, buf.readUnsignedShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x7F)).use { buf -> wrappedBuffer(0x7F).use { buf ->
assertEquals(0x7F, buf.readUnsignedShortSmart()) assertEquals(0x7F, buf.readUnsignedShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x80.toByte(), 0x80.toByte())).use { buf -> wrappedBuffer(0x80.toByte(), 0x80.toByte()).use { buf ->
assertEquals(0x80, buf.readUnsignedShortSmart()) assertEquals(0x80, buf.readUnsignedShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xC0.toByte(), 0x00)).use { buf -> wrappedBuffer(0xC0.toByte(), 0x00).use { buf ->
assertEquals(0x4000, buf.readUnsignedShortSmart()) assertEquals(0x4000, buf.readUnsignedShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xFF.toByte(), 0xFF.toByte())).use { buf -> wrappedBuffer(0xFF.toByte(), 0xFF.toByte()).use { buf ->
assertEquals(0x7FFF, buf.readUnsignedShortSmart()) assertEquals(0x7FFF, buf.readUnsignedShortSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
@ -145,42 +166,42 @@ object ByteBufExtensionsTest {
@Test @Test
fun testWriteUnsignedShortSmart() { fun testWriteUnsignedShortSmart() {
Unpooled.wrappedBuffer(byteArrayOf(0x00)).use { expected -> wrappedBuffer(0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedShortSmart(0) actual.writeUnsignedShortSmart(0)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x40)).use { expected -> wrappedBuffer(0x40).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedShortSmart(0x40) actual.writeUnsignedShortSmart(0x40)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x7F)).use { expected -> wrappedBuffer(0x7F).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedShortSmart(0x7F) actual.writeUnsignedShortSmart(0x7F)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x80.toByte(), 0x80.toByte())).use { expected -> wrappedBuffer(0x80.toByte(), 0x80.toByte()).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedShortSmart(0x80) actual.writeUnsignedShortSmart(0x80)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0xC0.toByte(), 0x00)).use { expected -> wrappedBuffer(0xC0.toByte(), 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedShortSmart(0x4000) actual.writeUnsignedShortSmart(0x4000)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0xFF.toByte(), 0xFF.toByte())).use { expected -> wrappedBuffer(0xFF.toByte(), 0xFF.toByte()).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedShortSmart(0x7FFF) actual.writeUnsignedShortSmart(0x7FFF)
assertEquals(expected, actual) assertEquals(expected, actual)
@ -202,37 +223,37 @@ object ByteBufExtensionsTest {
@Test @Test
fun testReadIntSmart() { fun testReadIntSmart() {
Unpooled.wrappedBuffer(byteArrayOf(0x00, 0x00)).use { buf -> wrappedBuffer(0x00, 0x00).use { buf ->
assertEquals(-0x4000, buf.readIntSmart()) assertEquals(-0x4000, buf.readIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x40, 0x00)).use { buf -> wrappedBuffer(0x40, 0x00).use { buf ->
assertEquals(0, buf.readIntSmart()) assertEquals(0, buf.readIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x7F, 0xFF.toByte())).use { buf -> wrappedBuffer(0x7F, 0xFF.toByte()).use { buf ->
assertEquals(0x3FFF, buf.readIntSmart()) assertEquals(0x3FFF, buf.readIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x80.toByte(), 0x00, 0x00, 0x00)).use { buf -> wrappedBuffer(0x80.toByte(), 0x00, 0x00, 0x00).use { buf ->
assertEquals(-0x40000000, buf.readIntSmart()) assertEquals(-0x40000000, buf.readIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xBF.toByte(), 0xFF.toByte(), 0xBF.toByte(), 0xFF.toByte())).use { buf -> wrappedBuffer(0xBF.toByte(), 0xFF.toByte(), 0xBF.toByte(), 0xFF.toByte()).use { buf ->
assertEquals(-0x4001, buf.readIntSmart()) assertEquals(-0x4001, buf.readIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xC0.toByte(), 0x00, 0x40.toByte(), 0x00)).use { buf -> wrappedBuffer(0xC0.toByte(), 0x00, 0x40.toByte(), 0x00).use { buf ->
assertEquals(0x4000, buf.readIntSmart()) assertEquals(0x4000, buf.readIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte())).use { buf -> wrappedBuffer(0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte()).use { buf ->
assertEquals(0x3FFFFFFF, buf.readIntSmart()) assertEquals(0x3FFFFFFF, buf.readIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
@ -240,53 +261,49 @@ object ByteBufExtensionsTest {
@Test @Test
fun testWriteIntSmart() { fun testWriteIntSmart() {
Unpooled.wrappedBuffer(byteArrayOf(0x00, 0x00)).use { expected -> wrappedBuffer(0x00, 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeIntSmart(-0x4000) actual.writeIntSmart(-0x4000)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x40, 0x00)).use { expected -> wrappedBuffer(0x40, 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeIntSmart(0) actual.writeIntSmart(0)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x7F, 0xFF.toByte())).use { expected -> wrappedBuffer(0x7F, 0xFF.toByte()).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeIntSmart(0x3FFF) actual.writeIntSmart(0x3FFF)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x80.toByte(), 0x00, 0x00, 0x00)).use { expected -> wrappedBuffer(0x80.toByte(), 0x00, 0x00, 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeIntSmart(-0x40000000) actual.writeIntSmart(-0x40000000)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer( wrappedBuffer(0xBF.toByte(), 0xFF.toByte(), 0xBF.toByte(), 0xFF.toByte()).use { expected ->
byteArrayOf(0xBF.toByte(), 0xFF.toByte(), 0xBF.toByte(), 0xFF.toByte())
).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeIntSmart(-0x4001) actual.writeIntSmart(-0x4001)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0xC0.toByte(), 0x00, 0x40.toByte(), 0x00)).use { expected -> wrappedBuffer(0xC0.toByte(), 0x00, 0x40.toByte(), 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeIntSmart(0x4000) actual.writeIntSmart(0x4000)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer( wrappedBuffer(0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte()).use { expected ->
byteArrayOf(0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte())
).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeIntSmart(0x3FFFFFFF) actual.writeIntSmart(0x3FFFFFFF)
assertEquals(expected, actual) assertEquals(expected, actual)
@ -308,32 +325,32 @@ object ByteBufExtensionsTest {
@Test @Test
fun testReadUnsignedIntSmart() { fun testReadUnsignedIntSmart() {
Unpooled.wrappedBuffer(byteArrayOf(0x00, 0x00)).use { buf -> wrappedBuffer(0x00, 0x00).use { buf ->
assertEquals(0, buf.readUnsignedIntSmart()) assertEquals(0, buf.readUnsignedIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x40, 0x00)).use { buf -> wrappedBuffer(0x40, 0x00).use { buf ->
assertEquals(0x4000, buf.readUnsignedIntSmart()) assertEquals(0x4000, buf.readUnsignedIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x7F, 0xFF.toByte())).use { buf -> wrappedBuffer(0x7F, 0xFF.toByte()).use { buf ->
assertEquals(0x7FFF, buf.readUnsignedIntSmart()) assertEquals(0x7FFF, buf.readUnsignedIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x80.toByte(), 0x00, 0x80.toByte(), 0x00)).use { buf -> wrappedBuffer(0x80.toByte(), 0x00, 0x80.toByte(), 0x00).use { buf ->
assertEquals(0x8000, buf.readUnsignedIntSmart()) assertEquals(0x8000, buf.readUnsignedIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xC0.toByte(), 0x00, 0x00, 0x00)).use { buf -> wrappedBuffer(0xC0.toByte(), 0x00, 0x00, 0x00).use { buf ->
assertEquals(0x40000000, buf.readUnsignedIntSmart()) assertEquals(0x40000000, buf.readUnsignedIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
Unpooled.wrappedBuffer(byteArrayOf(0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte())).use { buf -> wrappedBuffer(0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte()).use { buf ->
assertEquals(0x7FFFFFFF, buf.readUnsignedIntSmart()) assertEquals(0x7FFFFFFF, buf.readUnsignedIntSmart())
assertFalse(buf.isReadable) assertFalse(buf.isReadable)
} }
@ -341,44 +358,42 @@ object ByteBufExtensionsTest {
@Test @Test
fun testWriteUnsignedIntSmart() { fun testWriteUnsignedIntSmart() {
Unpooled.wrappedBuffer(byteArrayOf(0x00, 0x00)).use { expected -> wrappedBuffer(0x00, 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedIntSmart(0) actual.writeUnsignedIntSmart(0)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x40, 0x00)).use { expected -> wrappedBuffer(0x40, 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedIntSmart(0x4000) actual.writeUnsignedIntSmart(0x4000)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x7F, 0xFF.toByte())).use { expected -> wrappedBuffer(0x7F, 0xFF.toByte()).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedIntSmart(0x7FFF) actual.writeUnsignedIntSmart(0x7FFF)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x80.toByte(), 0x00, 0x80.toByte(), 0x00)).use { expected -> wrappedBuffer(0x80.toByte(), 0x00, 0x80.toByte(), 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedIntSmart(0x8000) actual.writeUnsignedIntSmart(0x8000)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0xC0.toByte(), 0x00, 0x00, 0x00)).use { expected -> wrappedBuffer(0xC0.toByte(), 0x00, 0x00, 0x00).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedIntSmart(0x40000000) actual.writeUnsignedIntSmart(0x40000000)
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer( wrappedBuffer(0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte()).use { expected ->
byteArrayOf(0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte())
).use { expected ->
ByteBufAllocator.DEFAULT.buffer().use { actual -> ByteBufAllocator.DEFAULT.buffer().use { actual ->
actual.writeUnsignedIntSmart(0x7FFFFFFF) actual.writeUnsignedIntSmart(0x7FFFFFFF)
assertEquals(expected, actual) assertEquals(expected, actual)
@ -400,15 +415,15 @@ object ByteBufExtensionsTest {
* Tests the hasArray() case. The slicedBuf trickery is to allow us * Tests the hasArray() case. The slicedBuf trickery is to allow us
* to test a non-zero arrayOffset(). * to test a non-zero arrayOffset().
*/ */
Unpooled.wrappedBuffer(s.toByteArray()).use { buf -> copiedBuffer(s).use { buf ->
val slicedBuf = buf.slice(1, buf.readableBytes() - 1) val slicedBuf = buf.slice(1, buf.readableBytes() - 1)
assertEquals(0x414FA339, slicedBuf.crc32(1, slicedBuf.writerIndex() - 2)) assertEquals(0x414FA339, slicedBuf.crc32(1, slicedBuf.writerIndex() - 2))
} }
// Tests the nioBufferCount() == 1 case. // Tests the nioBufferCount() == 1 case.
Unpooled.wrappedBuffer(s.toByteArray()).use { buf -> copiedBuffer(s).use { buf ->
ByteBufAllocator.DEFAULT.directBuffer().use { directBuf -> ByteBufAllocator.DEFAULT.directBuffer().use { directBuf ->
directBuf.writeBytes(buf, 0, buf.capacity()) directBuf.writeBytes(buf)
assertEquals(0x414FA339, directBuf.crc32(2, directBuf.writerIndex() - 3)) assertEquals(0x414FA339, directBuf.crc32(2, directBuf.writerIndex() - 3))
} }
@ -416,8 +431,8 @@ object ByteBufExtensionsTest {
// Tests the nioBufferCount() > 1 case. // Tests the nioBufferCount() > 1 case.
Unpooled.wrappedBuffer( Unpooled.wrappedBuffer(
Unpooled.wrappedBuffer("AAThe quick brown fox ".toByteArray()), copiedBuffer("AAThe quick brown fox "),
Unpooled.wrappedBuffer("jumps over the lazy dogA".toByteArray()) copiedBuffer("jumps over the lazy dogA")
).use { buf -> ).use { buf ->
assertEquals(0x414FA339, buf.crc32(2, buf.writerIndex() - 3)) assertEquals(0x414FA339, buf.crc32(2, buf.writerIndex() - 3))
} }
@ -426,7 +441,7 @@ object ByteBufExtensionsTest {
* Check the crc32() method (with no arguments) sets the index/length * Check the crc32() method (with no arguments) sets the index/length
* correctly. * correctly.
*/ */
Unpooled.wrappedBuffer(s.toByteArray()).use { buf -> copiedBuffer(s).use { buf ->
buf.readerIndex(2) buf.readerIndex(2)
buf.writerIndex(buf.writerIndex() - 1) buf.writerIndex(buf.writerIndex() - 1)
assertEquals(0x414FA339, buf.crc32()) assertEquals(0x414FA339, buf.crc32())

@ -0,0 +1,50 @@
package org.openrs2.cache
import io.netty.buffer.ByteBuf
public inline class Js5MasterIndex(public val entries: MutableList<Entry> = mutableListOf()) {
public class Entry(public var version: Int, public var checksum: Int) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Entry
if (version != other.version) return false
if (checksum != other.checksum) return false
return true
}
override fun hashCode(): Int {
var result = version
result = 31 * result + checksum
return result
}
override fun toString(): String {
return "Entry{version=$version, checksum=$checksum}"
}
}
public fun write(buf: ByteBuf) {
for (entry in entries) {
buf.writeInt(entry.checksum)
buf.writeInt(entry.version)
}
}
public companion object {
public fun read(buf: ByteBuf): Js5MasterIndex {
check(buf.readableBytes() % 8 == 0)
val index = Js5MasterIndex()
while (buf.isReadable) {
val checksum = buf.readInt()
val version = buf.readInt()
index.entries += Entry(version, checksum)
}
return index
}
}
}

@ -6,7 +6,9 @@ import io.netty.buffer.ByteBufAllocator
import io.netty.buffer.Unpooled import io.netty.buffer.Unpooled
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.openrs2.buffer.copiedBuffer
import org.openrs2.buffer.use import org.openrs2.buffer.use
import org.openrs2.buffer.wrappedBuffer
import java.io.EOFException import java.io.EOFException
import java.nio.channels.FileChannel import java.nio.channels.FileChannel
import java.nio.file.Files import java.nio.file.Files
@ -33,7 +35,7 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
channel.write(0, buf, buf.readableBytes()) channel.write(0, buf, buf.readableBytes())
} }
@ -41,7 +43,7 @@ object BufferedFileChannelTest {
} }
Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual -> Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -54,14 +56,14 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
channel.write(1, buf.slice(), buf.readableBytes()) channel.write(1, buf.slice(), buf.readableBytes())
channel.write(0, buf.slice(), buf.readableBytes()) channel.write(0, buf.slice(), buf.readableBytes())
} }
} }
Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual -> Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual ->
Unpooled.wrappedBuffer("OpenRS22".toByteArray()).use { expected -> copiedBuffer("OpenRS22").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -74,14 +76,14 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
channel.write(0, buf.slice(), buf.readableBytes()) channel.write(0, buf.slice(), buf.readableBytes())
channel.write(1, buf.slice(), buf.readableBytes()) channel.write(1, buf.slice(), buf.readableBytes())
} }
} }
Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual -> Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual ->
Unpooled.wrappedBuffer("OOpenRS2".toByteArray()).use { expected -> copiedBuffer("OOpenRS2").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -94,14 +96,14 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
channel.write(0, buf.slice(), buf.readableBytes()) channel.write(0, buf.slice(), buf.readableBytes())
channel.write(7, buf.slice(), buf.readableBytes()) channel.write(7, buf.slice(), buf.readableBytes())
} }
} }
Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual -> Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual ->
Unpooled.wrappedBuffer("OpenRS2OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2OpenRS2").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -114,14 +116,14 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
channel.write(0, buf.slice(), buf.readableBytes()) channel.write(0, buf.slice(), buf.readableBytes())
channel.write(8, buf.slice(), buf.readableBytes()) channel.write(8, buf.slice(), buf.readableBytes())
} }
} }
Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual -> Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual ->
Unpooled.wrappedBuffer("OpenRS2\u0000OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2\u0000OpenRS2").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -134,7 +136,7 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("Hello, world!".toByteArray()).use { buf -> copiedBuffer("Hello, world!").use { buf ->
channel.write(0, buf, buf.readableBytes()) channel.write(0, buf, buf.readableBytes())
} }
@ -142,7 +144,7 @@ object BufferedFileChannelTest {
} }
Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual -> Unpooled.wrappedBuffer(Files.readAllBytes(path)).use { actual ->
Unpooled.wrappedBuffer("Hello, world!".toByteArray()).use { expected -> copiedBuffer("Hello, world!").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -157,7 +159,7 @@ object BufferedFileChannelTest {
BufferedFileChannel(FileChannel.open(path, READ), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, READ), 8, 8).use { channel ->
ByteBufAllocator.DEFAULT.buffer(7, 7).use { actual -> ByteBufAllocator.DEFAULT.buffer(7, 7).use { actual ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
channel.read(0, actual, actual.writableBytes()) channel.read(0, actual, actual.writableBytes())
assertEquals(expected, actual) assertEquals(expected, actual)
@ -179,7 +181,7 @@ object BufferedFileChannelTest {
BufferedFileChannel(FileChannel.open(path, READ), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, READ), 8, 8).use { channel ->
ByteBufAllocator.DEFAULT.buffer(14, 14).use { actual -> ByteBufAllocator.DEFAULT.buffer(14, 14).use { actual ->
Unpooled.wrappedBuffer("OpenRS2OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2OpenRS2").use { expected ->
channel.read(0, actual, actual.writableBytes()) channel.read(0, actual, actual.writableBytes())
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -225,14 +227,14 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer(byteArrayOf(1)).use { buf -> wrappedBuffer(1).use { buf ->
channel.write(7, buf, buf.readableBytes()) channel.write(7, buf, buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(8, 8).use { actual -> ByteBufAllocator.DEFAULT.buffer(8, 8).use { actual ->
channel.read(0, actual, actual.writableBytes()) channel.read(0, actual, actual.writableBytes())
Unpooled.wrappedBuffer(byteArrayOf(0, 0, 0, 0, 0, 0, 0, 1)).use { expected -> wrappedBuffer(0, 0, 0, 0, 0, 0, 0, 1).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -246,14 +248,14 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
channel.write(0, buf, buf.readableBytes()) channel.write(0, buf, buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(7, 7).use { actual -> ByteBufAllocator.DEFAULT.buffer(7, 7).use { actual ->
channel.read(0, actual, actual.writableBytes()) channel.read(0, actual, actual.writableBytes())
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -267,14 +269,14 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
channel.write(0, buf, buf.readableBytes()) channel.write(0, buf, buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(6, 6).use { actual -> ByteBufAllocator.DEFAULT.buffer(6, 6).use { actual ->
channel.read(0, actual, actual.writableBytes()) channel.read(0, actual, actual.writableBytes())
Unpooled.wrappedBuffer("OpenRS".toByteArray()).use { expected -> copiedBuffer("OpenRS").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -288,14 +290,14 @@ object BufferedFileChannelTest {
val path = fs.getPath("/test.dat") val path = fs.getPath("/test.dat")
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
channel.write(0, buf, buf.readableBytes()) channel.write(0, buf, buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(6, 6).use { actual -> ByteBufAllocator.DEFAULT.buffer(6, 6).use { actual ->
channel.read(1, actual, actual.writableBytes()) channel.read(1, actual, actual.writableBytes())
Unpooled.wrappedBuffer("penRS2".toByteArray()).use { expected -> copiedBuffer("penRS2").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -311,14 +313,14 @@ object BufferedFileChannelTest {
Files.write(path, "OpenRS2OpenRS2".toByteArray()) Files.write(path, "OpenRS2OpenRS2".toByteArray())
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { buf -> copiedBuffer("Hello").use { buf ->
channel.write(4, buf, buf.readableBytes()) channel.write(4, buf, buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(14, 14).use { actual -> ByteBufAllocator.DEFAULT.buffer(14, 14).use { actual ->
channel.read(0, actual, actual.writableBytes()) channel.read(0, actual, actual.writableBytes())
Unpooled.wrappedBuffer("OpenHelloenRS2".toByteArray()).use { expected -> copiedBuffer("OpenHelloenRS2").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -334,14 +336,14 @@ object BufferedFileChannelTest {
Files.write(path, "OpenRS2OpenRS2".toByteArray()) Files.write(path, "OpenRS2OpenRS2".toByteArray())
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { buf -> copiedBuffer("Hello").use { buf ->
channel.write(4, buf, buf.readableBytes()) channel.write(4, buf, buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(7, 7).use { actual -> ByteBufAllocator.DEFAULT.buffer(7, 7).use { actual ->
channel.read(0, actual, actual.writableBytes()) channel.read(0, actual, actual.writableBytes())
Unpooled.wrappedBuffer("OpenHel".toByteArray()).use { expected -> copiedBuffer("OpenHel").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -357,14 +359,14 @@ object BufferedFileChannelTest {
Files.write(path, "OpenRS2OpenRS2".toByteArray()) Files.write(path, "OpenRS2OpenRS2".toByteArray())
BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, CREATE, READ, WRITE), 8, 8).use { channel ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { buf -> copiedBuffer("Hello").use { buf ->
channel.write(4, buf, buf.readableBytes()) channel.write(4, buf, buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(7, 7).use { actual -> ByteBufAllocator.DEFAULT.buffer(7, 7).use { actual ->
channel.read(7, actual, actual.writableBytes()) channel.read(7, actual, actual.writableBytes())
Unpooled.wrappedBuffer("loenRS2".toByteArray()).use { expected -> copiedBuffer("loenRS2").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -380,14 +382,14 @@ object BufferedFileChannelTest {
Files.write(path, "OpenRS2OpenRS2".toByteArray()) Files.write(path, "OpenRS2OpenRS2".toByteArray())
BufferedFileChannel(FileChannel.open(path, READ, WRITE), 4, 8).use { channel -> BufferedFileChannel(FileChannel.open(path, READ, WRITE), 4, 8).use { channel ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { buf -> copiedBuffer("Hello").use { buf ->
channel.write(4, buf, buf.readableBytes()) channel.write(4, buf, buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(0, actual, actual.writableBytes()) channel.read(0, actual, actual.writableBytes())
Unpooled.wrappedBuffer("Open".toByteArray()).use { expected -> copiedBuffer("Open").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -395,7 +397,7 @@ object BufferedFileChannelTest {
ByteBufAllocator.DEFAULT.buffer(5, 5).use { actual -> ByteBufAllocator.DEFAULT.buffer(5, 5).use { actual ->
channel.read(9, actual, actual.writableBytes()) channel.read(9, actual, actual.writableBytes())
Unpooled.wrappedBuffer("enRS2".toByteArray()).use { expected -> copiedBuffer("enRS2").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -414,19 +416,19 @@ object BufferedFileChannelTest {
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("RS2O".toByteArray()).use { expected -> copiedBuffer("RS2O").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer("ABCD".toByteArray()).use { buf -> copiedBuffer("ABCD").use { buf ->
channel.write(4, buf.slice(), buf.readableBytes()) channel.write(4, buf.slice(), buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("ABCD".toByteArray()).use { expected -> copiedBuffer("ABCD").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -445,19 +447,19 @@ object BufferedFileChannelTest {
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("RS2O".toByteArray()).use { expected -> copiedBuffer("RS2O").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer("ABC".toByteArray()).use { buf -> copiedBuffer("ABC").use { buf ->
channel.write(4, buf.slice(), buf.readableBytes()) channel.write(4, buf.slice(), buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("ABCO".toByteArray()).use { expected -> copiedBuffer("ABCO").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -476,19 +478,19 @@ object BufferedFileChannelTest {
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("RS2O".toByteArray()).use { expected -> copiedBuffer("RS2O").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer("BCD".toByteArray()).use { buf -> copiedBuffer("BCD").use { buf ->
channel.write(5, buf.slice(), buf.readableBytes()) channel.write(5, buf.slice(), buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("RBCD".toByteArray()).use { expected -> copiedBuffer("RBCD").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -507,19 +509,19 @@ object BufferedFileChannelTest {
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("RS2O".toByteArray()).use { expected -> copiedBuffer("RS2O").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer("ZABCD".toByteArray()).use { buf -> copiedBuffer("ZABCD").use { buf ->
channel.write(3, buf.slice(), buf.readableBytes()) channel.write(3, buf.slice(), buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("ABCD".toByteArray()).use { expected -> copiedBuffer("ABCD").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -538,19 +540,19 @@ object BufferedFileChannelTest {
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("RS2O".toByteArray()).use { expected -> copiedBuffer("RS2O").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
Unpooled.wrappedBuffer("ABCDZ".toByteArray()).use { buf -> copiedBuffer("ABCDZ").use { buf ->
channel.write(4, buf.slice(), buf.readableBytes()) channel.write(4, buf.slice(), buf.readableBytes())
} }
ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual -> ByteBufAllocator.DEFAULT.buffer(4, 4).use { actual ->
channel.read(4, actual, actual.writableBytes()) channel.read(4, actual, actual.writableBytes())
Unpooled.wrappedBuffer("ABCD".toByteArray()).use { expected -> copiedBuffer("ABCD").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }

@ -4,6 +4,7 @@ import com.google.common.jimfs.Configuration
import com.google.common.jimfs.Jimfs import com.google.common.jimfs.Jimfs
import io.netty.buffer.Unpooled import io.netty.buffer.Unpooled
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.openrs2.buffer.copiedBuffer
import org.openrs2.buffer.use import org.openrs2.buffer.use
import org.openrs2.util.io.recursiveCopy import org.openrs2.util.io.recursiveCopy
import org.openrs2.util.io.recursiveEquals import org.openrs2.util.io.recursiveEquals
@ -208,7 +209,7 @@ object DiskStoreTest {
@Test @Test
fun testReadSingleBlock() { fun testReadSingleBlock() {
readTest("single-block") { store -> readTest("single-block") { store ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -219,7 +220,7 @@ object DiskStoreTest {
@Test @Test
fun testReadSingleBlockExtended() { fun testReadSingleBlockExtended() {
readTest("single-block-extended") { store -> readTest("single-block-extended") { store ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
store.read(255, 65536).use { actual -> store.read(255, 65536).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -230,7 +231,7 @@ object DiskStoreTest {
@Test @Test
fun testReadTwoBlocks() { fun testReadTwoBlocks() {
readTest("two-blocks") { store -> readTest("two-blocks") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(100).toByteArray()).use { expected -> copiedBuffer("OpenRS2".repeat(100)).use { expected ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -241,7 +242,7 @@ object DiskStoreTest {
@Test @Test
fun testReadTwoBlocksExtended() { fun testReadTwoBlocksExtended() {
readTest("two-blocks-extended") { store -> readTest("two-blocks-extended") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(100).toByteArray()).use { expected -> copiedBuffer("OpenRS2".repeat(100)).use { expected ->
store.read(255, 65536).use { actual -> store.read(255, 65536).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -252,7 +253,7 @@ object DiskStoreTest {
@Test @Test
fun testReadMultipleBlocks() { fun testReadMultipleBlocks() {
readTest("multiple-blocks") { store -> readTest("multiple-blocks") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1000).toByteArray()).use { expected -> copiedBuffer("OpenRS2".repeat(1000)).use { expected ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -263,7 +264,7 @@ object DiskStoreTest {
@Test @Test
fun testReadMultipleBlocksExtended() { fun testReadMultipleBlocksExtended() {
readTest("multiple-blocks-extended") { store -> readTest("multiple-blocks-extended") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1000).toByteArray()).use { expected -> copiedBuffer("OpenRS2".repeat(1000)).use { expected ->
store.read(255, 65536).use { actual -> store.read(255, 65536).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -292,7 +293,7 @@ object DiskStoreTest {
fun testReadOverwritten() { fun testReadOverwritten() {
readTest("single-block-overwritten") { store -> readTest("single-block-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { expected -> copiedBuffer("Hello").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -300,7 +301,7 @@ object DiskStoreTest {
readTest("two-blocks-overwritten") { store -> readTest("two-blocks-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { expected -> copiedBuffer("Hello").use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -308,7 +309,7 @@ object DiskStoreTest {
readTest("multiple-blocks-overwritten") { store -> readTest("multiple-blocks-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(200).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(200)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -318,7 +319,7 @@ object DiskStoreTest {
@Test @Test
fun testReadFragmented() { fun testReadFragmented() {
readTest("fragmented") { store -> readTest("fragmented") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(100).toByteArray()).use { expected -> copiedBuffer("OpenRS2".repeat(100)).use { expected ->
for (j in 0 until 2) { for (j in 0 until 2) {
store.read(255, j).use { actual -> store.read(255, j).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -331,7 +332,7 @@ object DiskStoreTest {
@Test @Test
fun testWriteSingleBlock() { fun testWriteSingleBlock() {
writeTest("single-block") { store -> writeTest("single-block") { store ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
@ -340,7 +341,7 @@ object DiskStoreTest {
@Test @Test
fun testWriteSingleBlockExtended() { fun testWriteSingleBlockExtended() {
writeTest("single-block-extended") { store -> writeTest("single-block-extended") { store ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
store.write(255, 65536, buf) store.write(255, 65536, buf)
} }
} }
@ -349,7 +350,7 @@ object DiskStoreTest {
@Test @Test
fun testWriteTwoBlocks() { fun testWriteTwoBlocks() {
writeTest("two-blocks") { store -> writeTest("two-blocks") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(100).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(100)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
@ -358,7 +359,7 @@ object DiskStoreTest {
@Test @Test
fun testWriteTwoBlocksExtended() { fun testWriteTwoBlocksExtended() {
writeTest("two-blocks-extended") { store -> writeTest("two-blocks-extended") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(100).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(100)).use { buf ->
store.write(255, 65536, buf) store.write(255, 65536, buf)
} }
} }
@ -367,7 +368,7 @@ object DiskStoreTest {
@Test @Test
fun testWriteMultipleBlocks() { fun testWriteMultipleBlocks() {
writeTest("multiple-blocks") { store -> writeTest("multiple-blocks") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1000).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(1000)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
@ -376,7 +377,7 @@ object DiskStoreTest {
@Test @Test
fun testWriteMultipleBlocksExtended() { fun testWriteMultipleBlocksExtended() {
writeTest("multiple-blocks-extended") { store -> writeTest("multiple-blocks-extended") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1000).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(1000)).use { buf ->
store.write(255, 65536, buf) store.write(255, 65536, buf)
} }
} }
@ -386,7 +387,7 @@ object DiskStoreTest {
fun testWriteFragmented() { fun testWriteFragmented() {
writeTest("fragmented") { store -> writeTest("fragmented") { store ->
for (i in 1..2) { for (i in 1..2) {
Unpooled.wrappedBuffer("OpenRS2".repeat(i * 50).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(i * 50)).use { buf ->
for (j in 0 until 2) { for (j in 0 until 2) {
store.write(255, j, buf.slice()) store.write(255, j, buf.slice())
} }
@ -398,19 +399,19 @@ object DiskStoreTest {
@Test @Test
fun testOverwriteShorter() { fun testOverwriteShorter() {
overwriteTest("single-block", "single-block-overwritten") { store -> overwriteTest("single-block", "single-block-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { buf -> copiedBuffer("Hello").use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("two-blocks", "two-blocks-overwritten") { store -> overwriteTest("two-blocks", "two-blocks-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { buf -> copiedBuffer("Hello").use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("multiple-blocks", "multiple-blocks-overwritten") { store -> overwriteTest("multiple-blocks", "multiple-blocks-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(200).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(200)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
@ -419,19 +420,19 @@ object DiskStoreTest {
@Test @Test
fun testOverwriteShorterExtended() { fun testOverwriteShorterExtended() {
overwriteTest("single-block-extended", "single-block-extended-overwritten") { store -> overwriteTest("single-block-extended", "single-block-extended-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { buf -> copiedBuffer("Hello").use { buf ->
store.write(255, 65536, buf) store.write(255, 65536, buf)
} }
} }
overwriteTest("two-blocks-extended", "two-blocks-extended-overwritten") { store -> overwriteTest("two-blocks-extended", "two-blocks-extended-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".toByteArray()).use { buf -> copiedBuffer("Hello").use { buf ->
store.write(255, 65536, buf) store.write(255, 65536, buf)
} }
} }
overwriteTest("multiple-blocks-extended", "multiple-blocks-extended-overwritten") { store -> overwriteTest("multiple-blocks-extended", "multiple-blocks-extended-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(200).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(200)).use { buf ->
store.write(255, 65536, buf) store.write(255, 65536, buf)
} }
} }
@ -440,19 +441,19 @@ object DiskStoreTest {
@Test @Test
fun testOverwriteLonger() { fun testOverwriteLonger() {
overwriteTest("single-block", "two-blocks") { store -> overwriteTest("single-block", "two-blocks") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(100).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(100)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("single-block", "multiple-blocks") { store -> overwriteTest("single-block", "multiple-blocks") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1000).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(1000)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("two-blocks", "multiple-blocks") { store -> overwriteTest("two-blocks", "multiple-blocks") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1000).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(1000)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
@ -461,19 +462,19 @@ object DiskStoreTest {
@Test @Test
fun testOverwriteLongerExtended() { fun testOverwriteLongerExtended() {
overwriteTest("single-block-extended", "two-blocks-extended") { store -> overwriteTest("single-block-extended", "two-blocks-extended") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(100).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(100)).use { buf ->
store.write(255, 65536, buf) store.write(255, 65536, buf)
} }
} }
overwriteTest("single-block-extended", "multiple-blocks-extended") { store -> overwriteTest("single-block-extended", "multiple-blocks-extended") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1000).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(1000)).use { buf ->
store.write(255, 65536, buf) store.write(255, 65536, buf)
} }
} }
overwriteTest("two-blocks-extended", "multiple-blocks-extended") { store -> overwriteTest("two-blocks-extended", "multiple-blocks-extended") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1000).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(1000)).use { buf ->
store.write(255, 65536, buf) store.write(255, 65536, buf)
} }
} }
@ -566,49 +567,49 @@ object DiskStoreTest {
@Test @Test
fun testOverwriteCorrupt() { fun testOverwriteCorrupt() {
overwriteTest("corrupt-eof-late", "corrupt-eof-late-overwritten") { store -> overwriteTest("corrupt-eof-late", "corrupt-eof-late-overwritten") { store ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1050).toByteArray()).use { buf -> copiedBuffer("OpenRS2".repeat(1050)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("corrupt-first-eof-early", "corrupt-first-eof-early-overwritten") { store -> overwriteTest("corrupt-first-eof-early", "corrupt-first-eof-early-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("corrupt-first-invalid-archive", "corrupt-first-invalid-archive-overwritten") { store -> overwriteTest("corrupt-first-invalid-archive", "corrupt-first-invalid-archive-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("corrupt-first-invalid-block-number", "corrupt-first-invalid-block-number-overwritten") { store -> overwriteTest("corrupt-first-invalid-block-number", "corrupt-first-invalid-block-number-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("corrupt-first-invalid-group", "corrupt-first-invalid-group-overwritten") { store -> overwriteTest("corrupt-first-invalid-group", "corrupt-first-invalid-group-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("corrupt-first-outside-data-file", "corrupt-first-outside-data-file-overwritten") { store -> overwriteTest("corrupt-first-outside-data-file", "corrupt-first-outside-data-file-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("corrupt-second-eof-early", "corrupt-second-eof-early-overwritten") { store -> overwriteTest("corrupt-second-eof-early", "corrupt-second-eof-early-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("corrupt-second-invalid-archive", "corrupt-second-invalid-archive-overwritten") { store -> overwriteTest("corrupt-second-invalid-archive", "corrupt-second-invalid-archive-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
@ -617,19 +618,19 @@ object DiskStoreTest {
"corrupt-second-invalid-block-number", "corrupt-second-invalid-block-number",
"corrupt-second-invalid-block-number-overwritten" "corrupt-second-invalid-block-number-overwritten"
) { store -> ) { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("corrupt-second-invalid-group", "corrupt-second-invalid-group-overwritten") { store -> overwriteTest("corrupt-second-invalid-group", "corrupt-second-invalid-group-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
overwriteTest("corrupt-second-outside-data-file", "corrupt-second-outside-data-file-overwritten") { store -> overwriteTest("corrupt-second-outside-data-file", "corrupt-second-outside-data-file-overwritten") { store ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { buf -> copiedBuffer("Hello".repeat(300)).use { buf ->
store.write(255, 1, buf) store.write(255, 1, buf)
} }
} }
@ -639,7 +640,7 @@ object DiskStoreTest {
fun testReadOverwrittenCorrupt() { fun testReadOverwrittenCorrupt() {
readTest("corrupt-eof-late-overwritten") { store -> readTest("corrupt-eof-late-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("OpenRS2".repeat(1050).toByteArray()).use { expected -> copiedBuffer("OpenRS2".repeat(1050)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -647,7 +648,7 @@ object DiskStoreTest {
readTest("corrupt-first-eof-early-overwritten") { store -> readTest("corrupt-first-eof-early-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -655,7 +656,7 @@ object DiskStoreTest {
readTest("corrupt-first-invalid-archive-overwritten") { store -> readTest("corrupt-first-invalid-archive-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -663,7 +664,7 @@ object DiskStoreTest {
readTest("corrupt-first-invalid-block-number-overwritten") { store -> readTest("corrupt-first-invalid-block-number-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -671,7 +672,7 @@ object DiskStoreTest {
readTest("corrupt-first-invalid-group-overwritten") { store -> readTest("corrupt-first-invalid-group-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -679,7 +680,7 @@ object DiskStoreTest {
readTest("corrupt-first-outside-data-file-overwritten") { store -> readTest("corrupt-first-outside-data-file-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -687,7 +688,7 @@ object DiskStoreTest {
readTest("corrupt-second-eof-early-overwritten") { store -> readTest("corrupt-second-eof-early-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -695,7 +696,7 @@ object DiskStoreTest {
readTest("corrupt-second-invalid-archive-overwritten") { store -> readTest("corrupt-second-invalid-archive-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -703,7 +704,7 @@ object DiskStoreTest {
readTest("corrupt-second-invalid-block-number-overwritten") { store -> readTest("corrupt-second-invalid-block-number-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -711,7 +712,7 @@ object DiskStoreTest {
readTest("corrupt-second-invalid-group-overwritten") { store -> readTest("corrupt-second-invalid-group-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }
@ -719,7 +720,7 @@ object DiskStoreTest {
readTest("corrupt-second-outside-data-file-overwritten") { store -> readTest("corrupt-second-outside-data-file-overwritten") { store ->
store.read(255, 1).use { actual -> store.read(255, 1).use { actual ->
Unpooled.wrappedBuffer("Hello".repeat(300).toByteArray()).use { expected -> copiedBuffer("Hello".repeat(300)).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }

@ -5,6 +5,7 @@ import com.google.common.jimfs.Jimfs
import io.netty.buffer.Unpooled import io.netty.buffer.Unpooled
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.openrs2.buffer.copiedBuffer
import org.openrs2.buffer.use import org.openrs2.buffer.use
import org.openrs2.util.io.recursiveCopy import org.openrs2.util.io.recursiveCopy
import org.openrs2.util.io.recursiveEquals import org.openrs2.util.io.recursiveEquals
@ -181,7 +182,7 @@ object FlatFileStoreTest {
store.read(1, 0).release() store.read(1, 0).release()
} }
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
store.read(255, 0).use { actual -> store.read(255, 0).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -196,19 +197,19 @@ object FlatFileStoreTest {
@Test @Test
fun testWrite() { fun testWrite() {
writeTest("single-group") { store -> writeTest("single-group") { store ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
store.write(255, 0, buf) store.write(255, 0, buf)
} }
} }
overwriteTest("empty-archive", "single-group") { store -> overwriteTest("empty-archive", "single-group") { store ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
store.write(255, 0, buf) store.write(255, 0, buf)
} }
} }
overwriteTest("single-group", "single-group-overwritten") { store -> overwriteTest("single-group", "single-group-overwritten") { store ->
Unpooled.wrappedBuffer("Hello, world!".toByteArray()).use { buf -> copiedBuffer("Hello, world!").use { buf ->
store.write(255, 0, buf) store.write(255, 0, buf)
} }
} }

@ -3,6 +3,7 @@ package org.openrs2.cache
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import io.netty.buffer.Unpooled import io.netty.buffer.Unpooled
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.openrs2.buffer.copiedBuffer
import org.openrs2.buffer.use import org.openrs2.buffer.use
import org.openrs2.crypto.XteaKey import org.openrs2.crypto.XteaKey
import java.io.IOException import java.io.IOException
@ -19,7 +20,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressNone() { fun testCompressNone() {
read("none.dat").use { expected -> read("none.dat").use { expected ->
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { input -> copiedBuffer("OpenRS2").use { input ->
Js5Compression.compress(input, Js5CompressionType.UNCOMPRESSED).use { actual -> Js5Compression.compress(input, Js5CompressionType.UNCOMPRESSED).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -29,7 +30,7 @@ object Js5CompressionTest {
@Test @Test
fun testUncompressNone() { fun testUncompressNone() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
read("none.dat").use { input -> read("none.dat").use { input ->
Js5Compression.uncompress(input).use { actual -> Js5Compression.uncompress(input).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -40,7 +41,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressGzip() { fun testCompressGzip() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
Js5Compression.compress(expected.slice(), Js5CompressionType.GZIP).use { compressed -> Js5Compression.compress(expected.slice(), Js5CompressionType.GZIP).use { compressed ->
Js5Compression.uncompress(compressed).use { actual -> Js5Compression.uncompress(compressed).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -51,7 +52,7 @@ object Js5CompressionTest {
@Test @Test
fun testUncompressGzip() { fun testUncompressGzip() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
read("gzip.dat").use { input -> read("gzip.dat").use { input ->
Js5Compression.uncompress(input).use { actual -> Js5Compression.uncompress(input).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -73,7 +74,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressBzip2() { fun testCompressBzip2() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
Js5Compression.compress(expected.slice(), Js5CompressionType.BZIP2).use { compressed -> Js5Compression.compress(expected.slice(), Js5CompressionType.BZIP2).use { compressed ->
Js5Compression.uncompress(compressed).use { actual -> Js5Compression.uncompress(compressed).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -84,7 +85,7 @@ object Js5CompressionTest {
@Test @Test
fun testUncompressBzip2() { fun testUncompressBzip2() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
read("bzip2.dat").use { input -> read("bzip2.dat").use { input ->
Js5Compression.uncompress(input).use { actual -> Js5Compression.uncompress(input).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -95,7 +96,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressLzma() { fun testCompressLzma() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
Js5Compression.compress(expected.slice(), Js5CompressionType.LZMA).use { compressed -> Js5Compression.compress(expected.slice(), Js5CompressionType.LZMA).use { compressed ->
Js5Compression.uncompress(compressed).use { actual -> Js5Compression.uncompress(compressed).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -106,7 +107,7 @@ object Js5CompressionTest {
@Test @Test
fun testUncompressLzma() { fun testUncompressLzma() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
read("lzma.dat").use { input -> read("lzma.dat").use { input ->
Js5Compression.uncompress(input).use { actual -> Js5Compression.uncompress(input).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -118,7 +119,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressNoneEncrypted() { fun testCompressNoneEncrypted() {
read("none-encrypted.dat").use { expected -> read("none-encrypted.dat").use { expected ->
Unpooled.wrappedBuffer("OpenRS2".repeat(3).toByteArray()).use { input -> copiedBuffer("OpenRS2".repeat(3)).use { input ->
Js5Compression.compress(input, Js5CompressionType.UNCOMPRESSED, KEY).use { actual -> Js5Compression.compress(input, Js5CompressionType.UNCOMPRESSED, KEY).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
@ -128,7 +129,7 @@ object Js5CompressionTest {
@Test @Test
fun testUncompressNoneEncrypted() { fun testUncompressNoneEncrypted() {
Unpooled.wrappedBuffer("OpenRS2".repeat(3).toByteArray()).use { expected -> copiedBuffer("OpenRS2".repeat(3)).use { expected ->
read("none-encrypted.dat").use { input -> read("none-encrypted.dat").use { input ->
Js5Compression.uncompress(input, KEY).use { actual -> Js5Compression.uncompress(input, KEY).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -139,7 +140,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressGzipEncrypted() { fun testCompressGzipEncrypted() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
Js5Compression.compress(expected.slice(), Js5CompressionType.GZIP, KEY).use { compressed -> Js5Compression.compress(expected.slice(), Js5CompressionType.GZIP, KEY).use { compressed ->
Js5Compression.uncompress(compressed, KEY).use { actual -> Js5Compression.uncompress(compressed, KEY).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -150,7 +151,7 @@ object Js5CompressionTest {
@Test @Test
fun testUncompressGzipEncrypted() { fun testUncompressGzipEncrypted() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
read("gzip-encrypted.dat").use { input -> read("gzip-encrypted.dat").use { input ->
Js5Compression.uncompress(input, KEY).use { actual -> Js5Compression.uncompress(input, KEY).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -161,7 +162,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressBzip2Encrypted() { fun testCompressBzip2Encrypted() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
Js5Compression.compress(expected.slice(), Js5CompressionType.BZIP2, KEY).use { compressed -> Js5Compression.compress(expected.slice(), Js5CompressionType.BZIP2, KEY).use { compressed ->
Js5Compression.uncompress(compressed, KEY).use { actual -> Js5Compression.uncompress(compressed, KEY).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -172,7 +173,7 @@ object Js5CompressionTest {
@Test @Test
fun testUncompressBzip2Encrypted() { fun testUncompressBzip2Encrypted() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
read("bzip2-encrypted.dat").use { input -> read("bzip2-encrypted.dat").use { input ->
Js5Compression.uncompress(input, KEY).use { actual -> Js5Compression.uncompress(input, KEY).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -183,7 +184,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressLzmaEncrypted() { fun testCompressLzmaEncrypted() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
Js5Compression.compress(expected.slice(), Js5CompressionType.LZMA, KEY).use { compressed -> Js5Compression.compress(expected.slice(), Js5CompressionType.LZMA, KEY).use { compressed ->
Js5Compression.uncompress(compressed, KEY).use { actual -> Js5Compression.uncompress(compressed, KEY).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -194,7 +195,7 @@ object Js5CompressionTest {
@Test @Test
fun testUncompressLzmaEncrypted() { fun testUncompressLzmaEncrypted() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { expected -> copiedBuffer("OpenRS2").use { expected ->
read("lzma-encrypted.dat").use { input -> read("lzma-encrypted.dat").use { input ->
Js5Compression.uncompress(input, KEY).use { actual -> Js5Compression.uncompress(input, KEY).use { actual ->
assertEquals(expected, actual) assertEquals(expected, actual)
@ -205,7 +206,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressBest() { fun testCompressBest() {
Unpooled.wrappedBuffer("OpenRS2".repeat(100).toByteArray()).use { expected -> copiedBuffer("OpenRS2".repeat(100)).use { expected ->
val noneLen = Js5Compression.compress(expected.slice(), Js5CompressionType.UNCOMPRESSED).use { compressed -> val noneLen = Js5Compression.compress(expected.slice(), Js5CompressionType.UNCOMPRESSED).use { compressed ->
compressed.readableBytes() compressed.readableBytes()
} }
@ -223,7 +224,7 @@ object Js5CompressionTest {
@Test @Test
fun testCompressBestEncrypted() { fun testCompressBestEncrypted() {
Unpooled.wrappedBuffer("OpenRS2".repeat(100).toByteArray()).use { expected -> copiedBuffer("OpenRS2".repeat(100)).use { expected ->
val noneLen = Js5Compression.compress(expected.slice(), Js5CompressionType.UNCOMPRESSED).use { compressed -> val noneLen = Js5Compression.compress(expected.slice(), Js5CompressionType.UNCOMPRESSED).use { compressed ->
compressed.readableBytes() compressed.readableBytes()
} }
@ -241,7 +242,7 @@ object Js5CompressionTest {
@Test @Test
fun testUncompressedEncryption() { fun testUncompressedEncryption() {
Unpooled.wrappedBuffer("OpenRS2".toByteArray()).use { buf -> copiedBuffer("OpenRS2").use { buf ->
Js5Compression.compressBest(buf.slice()).use { compressed -> Js5Compression.compressBest(buf.slice()).use { compressed ->
assertEquals(Js5CompressionType.UNCOMPRESSED.ordinal, compressed.getUnsignedByte(0).toInt()) assertEquals(Js5CompressionType.UNCOMPRESSED.ordinal, compressed.getUnsignedByte(0).toInt())
} }

@ -6,6 +6,7 @@ import io.netty.buffer.ByteBufUtil
import io.netty.buffer.Unpooled import io.netty.buffer.Unpooled
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.openrs2.buffer.use import org.openrs2.buffer.use
import org.openrs2.buffer.wrappedBuffer
import org.openrs2.util.krHashCode import org.openrs2.util.krHashCode
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -128,13 +129,13 @@ object Js5IndexTest {
@Test @Test
fun testReadUnsupportedProtocol() { fun testReadUnsupportedProtocol() {
Unpooled.wrappedBuffer(byteArrayOf(4)).use { buf -> wrappedBuffer(4).use { buf ->
assertThrows<IllegalArgumentException> { assertThrows<IllegalArgumentException> {
Js5Index.read(buf) Js5Index.read(buf)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(8)).use { buf -> wrappedBuffer(8).use { buf ->
assertThrows<IllegalArgumentException> { assertThrows<IllegalArgumentException> {
Js5Index.read(buf) Js5Index.read(buf)
} }

@ -2,6 +2,7 @@ package org.openrs2.cache
import io.netty.buffer.Unpooled import io.netty.buffer.Unpooled
import org.openrs2.buffer.use import org.openrs2.buffer.use
import org.openrs2.buffer.wrappedBuffer
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNull import kotlin.test.assertNull
@ -11,23 +12,23 @@ object VersionTrailerTest {
public fun testStrip() { public fun testStrip() {
assertNull(VersionTrailer.strip(Unpooled.EMPTY_BUFFER)) assertNull(VersionTrailer.strip(Unpooled.EMPTY_BUFFER))
Unpooled.wrappedBuffer(byteArrayOf(0)).use { buf -> wrappedBuffer(0).use { buf ->
assertNull(VersionTrailer.strip(buf)) assertNull(VersionTrailer.strip(buf))
Unpooled.wrappedBuffer(byteArrayOf(0)).use { expected -> wrappedBuffer(0).use { expected ->
assertEquals(expected, buf) assertEquals(expected, buf)
} }
} }
Unpooled.wrappedBuffer(byteArrayOf(0x12, 0x34)).use { buf -> wrappedBuffer(0x12, 0x34).use { buf ->
assertEquals(0x1234, VersionTrailer.strip(buf)) assertEquals(0x1234, VersionTrailer.strip(buf))
assertEquals(Unpooled.EMPTY_BUFFER, buf) assertEquals(Unpooled.EMPTY_BUFFER, buf)
} }
Unpooled.wrappedBuffer(byteArrayOf(0x12, 0x34, 0x56)).use { buf -> wrappedBuffer(0x12, 0x34, 0x56).use { buf ->
assertEquals(0x3456, VersionTrailer.strip(buf)) assertEquals(0x3456, VersionTrailer.strip(buf))
Unpooled.wrappedBuffer(byteArrayOf(0x012)).use { expected -> wrappedBuffer(0x012).use { expected ->
assertEquals(expected, buf) assertEquals(expected, buf)
} }
} }

@ -2,11 +2,11 @@ package org.openrs2.crypto
import com.google.common.jimfs.Configuration import com.google.common.jimfs.Configuration
import com.google.common.jimfs.Jimfs import com.google.common.jimfs.Jimfs
import io.netty.buffer.Unpooled
import org.bouncycastle.crypto.params.RSAKeyParameters import org.bouncycastle.crypto.params.RSAKeyParameters
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters
import org.bouncycastle.util.Properties import org.bouncycastle.util.Properties
import org.openrs2.buffer.use import org.openrs2.buffer.use
import org.openrs2.buffer.wrappedBuffer
import org.openrs2.util.io.useTempFile import org.openrs2.util.io.useTempFile
import java.math.BigInteger import java.math.BigInteger
import java.nio.file.Files import java.nio.file.Files
@ -77,9 +77,9 @@ object RsaTest {
@Test @Test
fun testEncryptByteBuf() { fun testEncryptByteBuf() {
Unpooled.wrappedBuffer(byteArrayOf(65)).use { plaintext -> wrappedBuffer(65).use { plaintext ->
plaintext.rsaEncrypt(PUBLIC_KEY).use { ciphertext -> plaintext.rsaEncrypt(PUBLIC_KEY).use { ciphertext ->
Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte())).use { expectedCiphertext -> wrappedBuffer(10, 230.toByte()).use { expectedCiphertext ->
assertEquals(expectedCiphertext, ciphertext) assertEquals(expectedCiphertext, ciphertext)
} }
} }
@ -88,9 +88,9 @@ object RsaTest {
@Test @Test
fun testDecryptByteBuf() { fun testDecryptByteBuf() {
Unpooled.wrappedBuffer(byteArrayOf(10, 230.toByte())).use { ciphertext -> wrappedBuffer(10, 230.toByte()).use { ciphertext ->
ciphertext.rsaDecrypt(PRIVATE_KEY).use { plaintext -> ciphertext.rsaDecrypt(PRIVATE_KEY).use { plaintext ->
Unpooled.wrappedBuffer(byteArrayOf(65)).use { expectedPlaintext -> wrappedBuffer(65).use { expectedPlaintext ->
assertEquals(expectedPlaintext, plaintext) assertEquals(expectedPlaintext, plaintext)
} }
} }

@ -4,6 +4,7 @@ import io.netty.buffer.Unpooled
import io.netty.channel.embedded.EmbeddedChannel import io.netty.channel.embedded.EmbeddedChannel
import io.netty.handler.codec.DecoderException import io.netty.handler.codec.DecoderException
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.openrs2.buffer.wrappedBuffer
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -22,8 +23,8 @@ object Js5RequestDecoderTest {
@Test @Test
fun testFragmented() { fun testFragmented() {
val channel = EmbeddedChannel(Js5RequestDecoder()) val channel = EmbeddedChannel(Js5RequestDecoder())
channel.writeInbound(Unpooled.wrappedBuffer(byteArrayOf(0, 2))) channel.writeInbound(wrappedBuffer(0, 2))
channel.writeInbound(Unpooled.wrappedBuffer(byteArrayOf(0, 3))) channel.writeInbound(wrappedBuffer(0, 3))
assertEquals(Js5Request.Group(true, 2, 3), channel.readInbound()) assertEquals(Js5Request.Group(true, 2, 3), channel.readInbound())
} }
@ -32,7 +33,7 @@ object Js5RequestDecoderTest {
val channel = EmbeddedChannel(Js5RequestDecoder()) val channel = EmbeddedChannel(Js5RequestDecoder())
assertThrows<DecoderException> { assertThrows<DecoderException> {
channel.writeInbound(Unpooled.wrappedBuffer(byteArrayOf(8, 0, 0, 0))) channel.writeInbound(wrappedBuffer(8, 0, 0, 0))
} }
} }

@ -6,6 +6,7 @@ import io.netty.channel.embedded.EmbeddedChannel
import io.netty.handler.codec.DecoderException import io.netty.handler.codec.DecoderException
import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.assertThrows
import org.openrs2.buffer.use import org.openrs2.buffer.use
import org.openrs2.buffer.wrappedBuffer
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -35,19 +36,9 @@ object Js5ResponseDecoderTest {
fun testDecodeFragmented() { fun testDecodeFragmented() {
val channel = EmbeddedChannel(Js5ResponseDecoder()) val channel = EmbeddedChannel(Js5ResponseDecoder())
channel.writeInbound(Unpooled.wrappedBuffer(byteArrayOf(2, 0, 3, 0, 0, 0, 0))) channel.writeInbound(wrappedBuffer(2, 0, 3, 0, 0, 0, 0))
channel.writeInbound( channel.writeInbound(wrappedBuffer(7, 'O'.toByte(), 'p'.toByte(), 'e'.toByte(), 'n'.toByte()))
Unpooled.wrappedBuffer( channel.writeInbound(wrappedBuffer('R'.toByte(), 'S'.toByte(), '2'.toByte()))
byteArrayOf(
7,
'O'.toByte(),
'p'.toByte(),
'e'.toByte(),
'n'.toByte()
)
)
)
channel.writeInbound(Unpooled.wrappedBuffer(byteArrayOf('R'.toByte(), 'S'.toByte(), '2'.toByte())))
Unpooled.buffer().use { buf -> Unpooled.buffer().use { buf ->
buf.writeByte(0) buf.writeByte(0)
@ -67,13 +58,7 @@ object Js5ResponseDecoderTest {
val channel = EmbeddedChannel(Js5ResponseDecoder()) val channel = EmbeddedChannel(Js5ResponseDecoder())
assertThrows<DecoderException> { assertThrows<DecoderException> {
channel.writeInbound( channel.writeInbound(wrappedBuffer(2, 0, 3, 0, 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte()))
Unpooled.wrappedBuffer(
byteArrayOf(
2, 0, 3, 0, 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte()
)
)
)
} }
} }
@ -82,13 +67,7 @@ object Js5ResponseDecoderTest {
val channel = EmbeddedChannel(Js5ResponseDecoder()) val channel = EmbeddedChannel(Js5ResponseDecoder())
assertThrows<DecoderException> { assertThrows<DecoderException> {
channel.writeInbound( channel.writeInbound(wrappedBuffer(2, 0, 3, 0, 0x7F, 0xFF.toByte(), 0xFF.toByte(), 0xFB.toByte()))
Unpooled.wrappedBuffer(
byteArrayOf(
2, 0, 3, 0, 0x7F, 0xFF.toByte(), 0xFF.toByte(), 0xFB.toByte()
)
)
)
} }
} }
@ -97,13 +76,7 @@ object Js5ResponseDecoderTest {
val channel = EmbeddedChannel(Js5ResponseDecoder()) val channel = EmbeddedChannel(Js5ResponseDecoder())
assertThrows<DecoderException> { assertThrows<DecoderException> {
channel.writeInbound( channel.writeInbound(wrappedBuffer(2, 0, 3, 1, 0x7F, 0xFF.toByte(), 0xFF.toByte(), 0xF7.toByte()))
Unpooled.wrappedBuffer(
byteArrayOf(
2, 0, 3, 1, 0x7F, 0xFF.toByte(), 0xFF.toByte(), 0xF7.toByte()
)
)
)
} }
} }

@ -2,8 +2,8 @@ package org.openrs2.protocol.js5
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import io.netty.buffer.PooledByteBufAllocator import io.netty.buffer.PooledByteBufAllocator
import io.netty.buffer.Unpooled
import io.netty.channel.embedded.EmbeddedChannel import io.netty.channel.embedded.EmbeddedChannel
import org.openrs2.buffer.copiedBuffer
import org.openrs2.buffer.use import org.openrs2.buffer.use
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -24,15 +24,15 @@ object XorDecoderTest {
val channel = EmbeddedChannel(decoder) val channel = EmbeddedChannel(decoder)
if (direct) { if (direct) {
PooledByteBufAllocator.DEFAULT.ioBuffer().use { buf -> PooledByteBufAllocator.DEFAULT.ioBuffer().use { buf ->
buf.writeBytes("OpenRS2".toByteArray()) buf.writeCharSequence("OpenRS2", Charsets.UTF_8)
channel.writeInbound(buf.retain()) channel.writeInbound(buf.retain())
} }
} else { } else {
channel.writeInbound(Unpooled.wrappedBuffer("OpenRS2".toByteArray())) channel.writeInbound(copiedBuffer("OpenRS2"))
} }
channel.readInbound<ByteBuf>().use { actual -> channel.readInbound<ByteBuf>().use { actual ->
Unpooled.wrappedBuffer(expected.toByteArray()).use { expected -> copiedBuffer(expected).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }

@ -2,8 +2,8 @@ package org.openrs2.protocol.js5
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import io.netty.buffer.PooledByteBufAllocator import io.netty.buffer.PooledByteBufAllocator
import io.netty.buffer.Unpooled
import io.netty.channel.embedded.EmbeddedChannel import io.netty.channel.embedded.EmbeddedChannel
import org.openrs2.buffer.copiedBuffer
import org.openrs2.buffer.use import org.openrs2.buffer.use
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -24,15 +24,15 @@ object XorEncoderTest {
val channel = EmbeddedChannel(encoder) val channel = EmbeddedChannel(encoder)
if (direct) { if (direct) {
PooledByteBufAllocator.DEFAULT.ioBuffer().use { buf -> PooledByteBufAllocator.DEFAULT.ioBuffer().use { buf ->
buf.writeBytes("OpenRS2".toByteArray()) buf.writeCharSequence("OpenRS2", Charsets.UTF_8)
channel.writeOutbound(buf.retain()) channel.writeOutbound(buf.retain())
} }
} else { } else {
channel.writeOutbound(Unpooled.wrappedBuffer("OpenRS2".toByteArray())) channel.writeOutbound(copiedBuffer("OpenRS2"))
} }
channel.readOutbound<ByteBuf>().use { actual -> channel.readOutbound<ByteBuf>().use { actual ->
Unpooled.wrappedBuffer(expected.toByteArray()).use { expected -> copiedBuffer(expected).use { expected ->
assertEquals(expected, actual) assertEquals(expected, actual)
} }
} }

Loading…
Cancel
Save