maauto: fail TLS wrap on zero progress, fix tests
- Commit
41bef7f655aad70c8fb33bfbfa577b06d3d2297c- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-12 16:09 UTC (2 months ago)
- Parents
b3f6371
Diff
auto/protocol/src/main/java/com/vayunmathur/auto/protocol/TlsCodec.kt
diff --git a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/TlsCodec.kt b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/TlsCodec.ktindex e5082e1baa..5ecb6dd9b4 100644--- a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/TlsCodec.kt+++ b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/TlsCodec.kt@@ -24,6 +24,14 @@ class TlsCodec(private val engine: SSLEngine) { while (source.hasRemaining()) { val result = engine.wrap(source, record) check(result.status == Status.OK) { "TLS wrap failed: ${result.status}" }+ // A pre-handshake engine returns OK with zero bytes consumed AND+ // produced forever, which spins this loop at 100% CPU (production+ // ANR; hung the unit-test worker at GalConnectionTest:138). Real+ // multi-record wraps always make progress per iteration, so+ // zero/zero is only ever the stuck case: fail loudly instead.+ check(result.bytesConsumed() != 0 || result.bytesProduced() != 0) {+ "TLS wrap made no progress -- engine handshake incomplete?"+ } } record.flip() return ByteArray(record.remaining()).also { record.get(it) }
auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.kt
diff --git a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.kt b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.ktindex 43eee374e5..052824cc76 100644--- a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.kt+++ b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/GalConnectionTest.kt@@ -5,6 +5,7 @@ import java.nio.ByteBuffer import kotlin.test.Test import kotlin.test.assertContentEquals import kotlin.test.assertEquals+import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -113,39 +114,62 @@ class GalConnectionTest { // an empty 0xff and no 0x8 (Run 5), while the CONTROL-less 0x5 is // accepted -- so the bit goes on the open, nothing else. Dalvik ground // truth (`Ljbe.i`): gearhead's 0x7 goes out as 0x0F.- val transport = FakeTransport()- val connection = connection(transport)- connection.send(- OutboundMessage(- type = GalMessage.Control.CHANNEL_OPEN_REQUEST,- payload = byteArrayOf(0x08, 0x00, 0x10, 0x02),- encrypted = true,- isControl = true,- ),+ //+ // Framed at the FrameWriter level with a handshook engine: sending+ // encrypted data through a full connection needs a completed handshake+ // (a fresh engine's wrap() makes no progress -- see TlsCodec.wrap).+ val context = TestTls.context()+ val server = GalCredential.serverEngine(context)+ TestTls.handshake(server, TestTls.carEngine(context))+ val writer = FrameWriter(encrypt = TlsCodec(server)::wrap)+ val message = OutboundMessage(+ type = GalMessage.Control.CHANNEL_OPEN_REQUEST,+ payload = byteArrayOf(0x08, 0x00, 0x10, 0x02),+ encrypted = true,+ isControl = true, ) - val written = transport.written.toByteArray()- assertEquals(0x00, written[0])- assertEquals(0x0F, written[1].toInt() and 0xFF)+ val frame = writer.frame(+ channelId = 0,+ payload = MessageCodec.encode(message.type, message.payload),+ isControl = message.isControl,+ encrypted = message.encrypted,+ ).single()++ assertEquals(0x00, frame[0])+ assertEquals(0x0F, frame[1].toInt() and 0xFF) } @Test fun `other control messages leave the CONTROL bit clear`() { // Discovery (0x5), ping responses and byebye ride CONTROL-less: the 0x5 // is live-accepted that way, and nothing else has shown a need.+ // Plaintext here exercises the full connection send path end to end. val transport = FakeTransport() val connection = connection(transport) connection.send( OutboundMessage( type = GalMessage.Control.SERVICE_DISCOVERY_REQUEST, payload = byteArrayOf(0x2A, 0x0E) + "Google Pixel 8".toByteArray(),- encrypted = true,+ encrypted = false, ), ) val written = transport.written.toByteArray() assertEquals(0x00, written[0])- assertEquals(0x0B, written[1].toInt() and 0xFF)+ assertEquals(0x03, written[1].toInt() and 0xFF)+ }++ @Test+ fun `wrapping without a handshake fails instead of spinning`() {+ // Pre-handshake wrap() returns OK with zero bytes consumed/produced+ // forever; the codec must fail loudly (production ANR + hung test+ // worker at GalConnectionTest:138) rather than loop.+ val codec = TlsCodec(GalCredential.serverEngine(TestTls.context()))++ assertFailsWith<IllegalStateException> {+ codec.wrap(byteArrayOf(0x08, 0x00, 0x10, 0x02))+ } } @Test