dash ble: add tests for payload splitting and joining

This commit is contained in:
Andrei Vereha 2021-03-13 13:16:46 +01:00
parent 2892d385ab
commit 5f71e1b27f
3 changed files with 72 additions and 0 deletions

View file

@ -0,0 +1,22 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message
import com.google.crypto.tink.subtle.Hex
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.PayloadJoiner
import info.nightscout.androidaps.utils.extensions.toHex
import org.junit.Assert.assertEquals
import org.junit.Test
class PayloadJoinerTest {
@Test fun testJoiner() {
val f1 = Hex.decode("00,01,54,57,10,23,03,00,00,c0,ff,ff,ff,fe,08,20,2e,a8,50,30".replace(",", ""))
val f2 = Hex.decode("01,04,bc,20,1f,f6,3d,00,01,a5,ff,ff,ff,fe,08,20,2e,a8,50,30".replace(",", ""))
val payload = "54,57,10,23,03,00,00,c0,ff,ff,ff,fe,08,20,2e,a8,50,30,3d,00,01,a5".replace(",", "")
val joiner = PayloadJoiner(f1)
joiner.accumulate(f2)
val actual = joiner.finalize()
assertEquals(payload, actual.toHex())
}
}

View file

@ -0,0 +1,26 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io.PayloadJoiner
import info.nightscout.androidaps.utils.extensions.toHex
import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.*
class PayloadSplitJoinTest {
val random = Random(42)
@Test fun testSplitAndJoinBack() {
for (s in 0..250) {
val payload = ByteArray(s)
random.nextBytes(payload)
val splitter = PayloadSplitter(payload)
val packets = splitter.splitInPackets()
val joiner = PayloadJoiner(packets.get(0).asByteArray())
for (p in packets.subList(1, packets.size)) {
joiner.accumulate(p.asByteArray())
}
val got = joiner.finalize()
assertEquals(got.toHex(), payload.toHex())
}
}
}

View file

@ -0,0 +1,24 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message
import com.google.crypto.tink.subtle.Hex
import info.nightscout.androidaps.utils.extensions.toHex
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
class PayloadSplitterTest {
@Test fun testSplitter() {
val f1 = "00,01,54,57,10,23,03,00,00,c0,ff,ff,ff,fe,08,20,2e,a8,50,30".replace(",", "")
val f2 = "01,04,bc,20,1f,f6,3d,00,01,a5,ff,ff,ff,fe,08,20,2e,a8,50,30".replace(",", "")
val payload = Hex.decode("54,57,10,23,03,00,00,c0,ff,ff,ff,fe,08,20,2e,a8,50,30,3d,00,01,a5".replace(",",""))
val splitter = PayloadSplitter(payload)
val packets = splitter.splitInPackets()
assertEquals(packets.size, 2)
assertEquals(f1, packets.get(0).asByteArray().toHex())
val p2 = packets.get(1).asByteArray()
assertTrue(p2.size >= 10)
assertEquals(f2.subSequence(0, 20),p2.copyOfRange(0, 10).toHex())
}
}