fix: add metaint validation and mid-metadata truncation test
Made-with: Cursor
This commit is contained in:
@@ -27,6 +27,7 @@ class IcyParser(
|
||||
}
|
||||
|
||||
private fun parseWithMetadata(metaint: Int) {
|
||||
require(metaint > 0) { "metaint must be positive, got $metaint" }
|
||||
val audioBuf = ByteArray(metaint)
|
||||
while (true) {
|
||||
val audioRead = readFully(audioBuf, 0, metaint)
|
||||
|
||||
@@ -267,4 +267,28 @@ class IcyParserTest {
|
||||
assertTrue(audioCollected.all { it == 0x42.toByte() })
|
||||
assertEquals(0, metadataEvents.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun handlesTruncationMidMetadata_noCrash_noIncompleteMetadataEmitted() {
|
||||
val metaint = 8
|
||||
val audioChunk = ByteArray(8) { 0x42 }
|
||||
val fullStream = buildIcyStream(metaint, IcyBlock(audioChunk, "StreamTitle='Test';"))
|
||||
// Full audio (8) + length byte (2 = 32 bytes) + only 16 bytes of metadata (truncated)
|
||||
val truncated =
|
||||
fullStream.copyOf(8 + 1 + 16)
|
||||
|
||||
val audioCollected = mutableListOf<Byte>()
|
||||
val metadataEvents = mutableListOf<IcyMetadata>()
|
||||
|
||||
IcyParser(
|
||||
input = ByteArrayInputStream(truncated),
|
||||
metaint = metaint,
|
||||
onAudioData = { buf, off, len -> (0 until len).forEach { audioCollected.add(buf[off + it]) } },
|
||||
onMetadata = { metadataEvents.add(it) }
|
||||
).readAll()
|
||||
|
||||
assertEquals(8, audioCollected.size)
|
||||
assertTrue(audioCollected.all { it == 0x42.toByte() })
|
||||
assertEquals(0, metadataEvents.size)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user