From 5f71e1b27fa27489208fe878810ad4c2aca2dd09 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Sat, 13 Mar 2021 13:16:46 +0100 Subject: [PATCH] dash ble: add tests for payload splitting and joining --- .../driver/comm/message/PayloadJoinerTest.kt | 22 ++++++++++++++++ .../comm/message/PayloadSplitJoinTest.kt | 26 +++++++++++++++++++ .../comm/message/PayloadSplitterTest.kt | 24 +++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadJoinerTest.kt create mode 100644 omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitJoinTest.kt create mode 100644 omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitterTest.kt diff --git a/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadJoinerTest.kt b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadJoinerTest.kt new file mode 100644 index 0000000000..8989791512 --- /dev/null +++ b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadJoinerTest.kt @@ -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()) + } +} + + diff --git a/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitJoinTest.kt b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitJoinTest.kt new file mode 100644 index 0000000000..9d7ba06d0c --- /dev/null +++ b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitJoinTest.kt @@ -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()) + } + } +} \ No newline at end of file diff --git a/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitterTest.kt b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitterTest.kt new file mode 100644 index 0000000000..de6e570134 --- /dev/null +++ b/omnipod-dash/src/test/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/message/PayloadSplitterTest.kt @@ -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()) + } +} \ No newline at end of file