dash ble: add test for MessagePacket

This commit is contained in:
Andrei Vereha 2021-03-14 13:57:48 +01:00
parent eff6247f77
commit 752f393f3b
4 changed files with 69 additions and 4 deletions

View file

@ -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

View file

@ -23,7 +23,7 @@ enum class EapCode(val code: Byte) {
data class EapMessage(
val code: EapCode,
val identifier: Byte,
val attributes: Array<EapAkaAttribute>,
val attributes: Array<EapAkaAttribute>
) {
fun toByteArray(): ByteArray {

View file

@ -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")

View file

@ -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
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)
}
}