Reject invalid library snapshot magic
This commit is contained in:
@@ -20,6 +20,7 @@ package org.oxycblt.musikr.snapshot
|
||||
|
||||
import java.io.DataInputStream
|
||||
import java.io.DataOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
@@ -41,6 +42,9 @@ object LibrarySnapshotCodec {
|
||||
val dataInput = DataInputStream(input)
|
||||
val magic = ByteArray(MAGIC_BYTES.size)
|
||||
dataInput.readFully(magic)
|
||||
if (!magic.contentEquals(MAGIC_BYTES)) {
|
||||
throw IOException("Invalid snapshot magic")
|
||||
}
|
||||
return Header(String(magic, Charsets.US_ASCII), dataInput.readInt())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,9 @@ package org.oxycblt.musikr.snapshot
|
||||
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Test
|
||||
|
||||
class LibrarySnapshotCodecTest {
|
||||
@@ -34,4 +36,16 @@ class LibrarySnapshotCodecTest {
|
||||
assertEquals(LibrarySnapshotCodec.MAGIC, header.magic)
|
||||
assertEquals(1, header.schemaVersion)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readHeader_unknownMagic_throwsIOException() {
|
||||
val output = ByteArrayOutputStream()
|
||||
LibrarySnapshotCodec.writeHeader(output)
|
||||
val bytes = output.toByteArray()
|
||||
bytes[0] = 'X'.code.toByte()
|
||||
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readHeader(ByteArrayInputStream(bytes))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user