From 752f393f3b59bde98c2bb05aef8b6361ae78c105 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sun, 14 Mar 2021 13:57:48 +0100 Subject: [PATCH] dash ble: add test for MessagePacket --- .../pump/omnipod/dash/driver/comm/Id.kt | 15 ++++++ .../dash/driver/comm/session/EapMessage.kt | 2 +- .../driver/comm/session/SessionEstablisher.kt | 4 +- .../driver/comm/message/MessagePacketTest.kt | 52 ++++++++++++++++++- 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Id.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Id.kt index 91a9238352..11c32853e7 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Id.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/Id.kt @@ -29,6 +29,21 @@ data class Id(val address: ByteArray) { return ByteBuffer.wrap(address).int.toLong() and 0xffffffffL } + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Id + + if (!address.contentEquals(other.address)) return false + + return true + } + + override fun hashCode(): Int { + return address.contentHashCode() + } + companion object { private const val PERIPHERAL_NODE_INDEX = 1 // TODO: understand the meaning of this value. It comes from preferences diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapMessage.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapMessage.kt index de5dd8c45d..e66684a770 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapMessage.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/EapMessage.kt @@ -23,7 +23,7 @@ enum class EapCode(val code: Byte) { data class EapMessage( val code: EapCode, val identifier: Byte, - val attributes: Array, + val attributes: Array ) { fun toByteArray(): ByteArray { diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt index da37dcfa71..41c7e9855b 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/SessionEstablisher.kt @@ -37,7 +37,7 @@ class SessionEstablisher( fun negotiateSessionKeys(): SessionKeys { // send EAP-AKA challenge - msgSeq++ // TODO: get from pod state. This only works for activating a new pod + msgSeq++ var challenge = eapAkaChallenge() msgIO.sendMessage(challenge) @@ -80,7 +80,7 @@ class SessionEstablisher( } private fun processChallengeResponse(challengeResponse: MessagePacket) { - // TODO verify that identifier matches identifer from the Challenge + // TODO verify that identifier matches identifier from the Challenge val eapMsg = EapMessage.parse(aapsLogger, challengeResponse.payload) if (eapMsg.attributes.size != 2) { aapsLogger.debug(LTag.PUMPBTCOMM, "EAP-AKA: got message: $eapMsg") diff --git a/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessagePacketTest.kt b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessagePacketTest.kt index e5802502d5..8e1afae115 100644 --- a/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessagePacketTest.kt +++ b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/MessagePacketTest.kt @@ -1,5 +1,55 @@ package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message +import android.app.Notification +import com.google.crypto.tink.subtle.Hex +import info.nightscout.androidaps.logging.AAPSLoggerTest +import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id +import info.nightscout.androidaps.utils.extensions.toHex import org.junit.Assert.* +import org.junit.Test -class MessagePacketTest \ No newline at end of file +class MessagePacketTest { + + val payload = + "54,57,11,01,07,00,03,40,08,20,2e,a8,08,20,2e,a9,ab,35,d8,31,60,9b,b8,fe,3a,3b,de,5b,18,37,24,9a,16,db,f8,e4,d3,05,e9,75,dc,81,7c,37,07,cc,41,5f,af,8a".replace( + ",", + "" + ) + + @Test fun testParseMessagePacket() { + val aapsLogger = AAPSLoggerTest() + val msg = MessagePacket.parse(Hex.decode(payload)) + assertEquals(msg.type, MessageType.ENCRYPTED) + assertEquals(msg.source, Id.fromLong(136326824)) + assertEquals(msg.destination, Id.fromLong(136326825)) + assertEquals(msg.sequenceNumber, 7.toByte()) + assertEquals(msg.ackNumber, 0.toByte()) + assertEquals(msg.eqos, 1.toShort()) + assertEquals(msg.priority, false) + assertEquals(msg.lastMessage, false) + assertEquals(msg.gateway, false) + assertEquals(msg.sas, true) + assertEquals(msg.tfs, false) + assertEquals(msg.version, 0.toShort()) + assertEquals(msg.payload.toHex(), payload.substring(32, payload.length) ) + } + + @Test fun testSerializeMessagePacket() { + val msg = MessagePacket( + type = MessageType.ENCRYPTED, + source = Id.fromLong(136326824), + destination = Id.fromLong(136326825), + sequenceNumber = 7.toByte(), + ackNumber = 0.toByte(), + eqos = 1.toShort(), + priority = false, + lastMessage = false, + gateway = false, + sas = true, + tfs = false, + payload = Hex.decode(payload.substring(32, payload.length)) + ) + assertEquals(msg.asByteArray().toHex(), payload) + } +} \ No newline at end of file