diff --git a/kotlin/carlife/src/main/kotlin/org/openxplay/stream/carlife/CarlifeSession.kt b/kotlin/carlife/src/main/kotlin/org/openxplay/stream/carlife/CarlifeSession.kt index 13e8b3d..2fa7349 100644 --- a/kotlin/carlife/src/main/kotlin/org/openxplay/stream/carlife/CarlifeSession.kt +++ b/kotlin/carlife/src/main/kotlin/org/openxplay/stream/carlife/CarlifeSession.kt @@ -64,6 +64,12 @@ internal class CarlifeSession( diagnosticSink("session=${origin.clientId} phase=$phase elapsedMs=${(System.nanoTime() - createdAt) / 1_000_000} $event") } + private fun diagnosticVersion(value: Long?): String = when (value) { + null -> "absent" + in 0L..999L -> value.toString() + else -> "out_of_range" + } + private fun stage(next: String) { phase = next diagnostic("phase_changed") @@ -101,7 +107,11 @@ internal class CarlifeSession( when (message.id) { 98305 -> { protocolRequire(!version, "unexpected_phase") - protocolRequire(payload.number(1) in 3L..4L, "version_mismatch") + val major = payload.number(1) + if (major !in 3L..4L) { + diagnostic("version_rejected major=${diagnosticVersion(major)} minor=${diagnosticVersion(payload.number(2))}") + throw CarlifeProtocolException("version_mismatch") + } version = true stage("version_matched") send(1, 65538, ProtoWriter().number(1, 1).number(2, 4).build()) diff --git a/kotlin/carlife/src/test/kotlin/org/openxplay/stream/carlife/CarlifeSessionTest.kt b/kotlin/carlife/src/test/kotlin/org/openxplay/stream/carlife/CarlifeSessionTest.kt index c9519af..7c0c45b 100644 --- a/kotlin/carlife/src/test/kotlin/org/openxplay/stream/carlife/CarlifeSessionTest.kt +++ b/kotlin/carlife/src/test/kotlin/org/openxplay/stream/carlife/CarlifeSessionTest.kt @@ -85,6 +85,21 @@ class CarlifeSessionTest { assertFalse(fixture.session.authenticated) } + @Test fun rejectedVersionReportsOnlyBoundedProtocolFields() { + val fixture = Fixture() + val unsupported = ProtoWriter().number(1, 2).number(2, 11).build() + val missingMajor = ProtoWriter().number(2, 1_000_000_000).build() + + for (request in listOf(unsupported, missingMajor)) { + val error = assertFailsWith { fixture.receive(98305, request) } + assertEquals("version_mismatch", error.code) + } + assertTrue(fixture.diagnostics.any { it.endsWith("version_rejected major=2 minor=11") }) + assertTrue(fixture.diagnostics.any { it.endsWith("version_rejected major=absent minor=out_of_range") }) + assertTrue(fixture.messages().isEmpty()) + assertTrue(fixture.states.isEmpty()) + } + @Test fun displayNegotiationIsH264OnlyWithinPanelAnd1080pBounds() { val fixture = Fixture() fixture.authenticate()