diff --git a/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/ByteConstraint.kt b/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/ByteConstraint.kt deleted file mode 100644 index 3955b596..00000000 --- a/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/ByteConstraint.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.walkertribe.ian.protocol.udp - -internal sealed interface ByteConstraint { - @JvmInline - value class Equals(val value: Byte) : ByteConstraint { - override fun check(byte: Byte): Boolean = byte == value - } - - class Range(val range: IntRange) : ByteConstraint { - override fun check(byte: Byte): Boolean = byte in range - } - - fun check(byte: Byte): Boolean -} diff --git a/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/IntConstraint.kt b/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/IntConstraint.kt new file mode 100644 index 00000000..1c8e237d --- /dev/null +++ b/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/IntConstraint.kt @@ -0,0 +1,14 @@ +package com.walkertribe.ian.protocol.udp + +internal sealed interface IntConstraint { + @JvmInline + value class Equals(val value: Int) : IntConstraint { + override fun check(int: Int): Boolean = int == value + } + + class Range(val range: IntRange) : IntConstraint { + override fun check(int: Int): Boolean = int in range + } + + fun check(int: Int): Boolean +} diff --git a/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/PrivateNetworkType.kt b/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/PrivateNetworkType.kt index a2940229..29175e5f 100644 --- a/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/PrivateNetworkType.kt +++ b/IAN/udp/src/main/kotlin/com/walkertribe/ian/protocol/udp/PrivateNetworkType.kt @@ -8,24 +8,24 @@ enum class PrivateNetworkType { TWENTY_FOUR_BIT_BLOCK { // 10.x.x.x override val broadcastAddress: String get() = TWENTY_FOUR_BIT_BROADCAST - override val constraints: Array get() = TWENTY_FOUR_BIT_CONSTRAINTS + override val constraints: Array get() = TWENTY_FOUR_BIT_CONSTRAINTS }, TWENTY_BIT_BLOCK { // 172.16.x.x - 172.31.x.x override val broadcastAddress: String get() = TWENTY_BIT_BROADCAST - override val constraints: Array get() = TWENTY_BIT_CONSTRAINTS + override val constraints: Array get() = TWENTY_BIT_CONSTRAINTS }, SIXTEEN_BIT_BLOCK { // 192.168.x.x override val broadcastAddress: String get() = SIXTEEN_BIT_BROADCAST - override val constraints: Array get() = SIXTEEN_BIT_CONSTRAINTS + override val constraints: Array get() = SIXTEEN_BIT_CONSTRAINTS }; abstract val broadcastAddress: String - internal abstract val constraints: Array + internal abstract val constraints: Array internal fun match(address: String): Boolean = address.split('.').run { - size == Int.SIZE_BYTES && zip(constraints).all { (byte, cons) -> cons.check(byte.toByte()) } + size == Int.SIZE_BYTES && zip(constraints).all { (byte, cons) -> cons.check(byte.toInt()) } } companion object { @@ -33,18 +33,18 @@ enum class PrivateNetworkType { private const val TWENTY_BIT_BROADCAST = "172.31.255.255" private const val SIXTEEN_BIT_BROADCAST = "192.168.255.255" - private val TWENTY_FOUR_BIT_CONSTRAINTS = arrayOf( - ByteConstraint.Equals(10), + private val TWENTY_FOUR_BIT_CONSTRAINTS = arrayOf( + IntConstraint.Equals(10), ) private val TWENTY_BIT_CONSTRAINTS = arrayOf( - ByteConstraint.Equals(-44), - ByteConstraint.Range(16 ..31), + IntConstraint.Equals(172), + IntConstraint.Range(16 ..31), ) - private val SIXTEEN_BIT_CONSTRAINTS = arrayOf( - ByteConstraint.Equals(-64), - ByteConstraint.Equals(-88), + private val SIXTEEN_BIT_CONSTRAINTS = arrayOf( + IntConstraint.Equals(192), + IntConstraint.Equals(168), ) /** diff --git a/IAN/udp/src/test/kotlin/com/walkertribe/ian/protocol/udp/PrivateNetworkTypeTest.kt b/IAN/udp/src/test/kotlin/com/walkertribe/ian/protocol/udp/PrivateNetworkTypeTest.kt index a89f17d7..79a6fee3 100644 --- a/IAN/udp/src/test/kotlin/com/walkertribe/ian/protocol/udp/PrivateNetworkTypeTest.kt +++ b/IAN/udp/src/test/kotlin/com/walkertribe/ian/protocol/udp/PrivateNetworkTypeTest.kt @@ -9,10 +9,9 @@ import io.kotest.matchers.nulls.shouldBeNull import io.kotest.matchers.nulls.shouldNotBeNull import io.kotest.property.Arb import io.kotest.property.arbitrary.bind -import io.kotest.property.arbitrary.byte -import io.kotest.property.arbitrary.byteArray import io.kotest.property.arbitrary.filter import io.kotest.property.arbitrary.int +import io.kotest.property.arbitrary.intArray import io.kotest.property.arbitrary.nonNegativeInt import io.kotest.property.arbitrary.of import io.kotest.property.arbitrary.pair @@ -21,12 +20,13 @@ import io.kotest.property.checkAll class PrivateNetworkTypeTest : DescribeSpec({ describe("PrivateNetworkType") { + val arbByte = Arb.nonNegativeInt(max = 0xFF) + data class TestSuite( val testName: String, - val firstByte: Byte, - val arbBytes: Arb>, + val firstByte: Int, + val arbBytes: Arb>, val expectedType: PrivateNetworkType, - val expectedMatches: Triple, val expectedBroadcastAddress: String, ) @@ -34,25 +34,22 @@ class PrivateNetworkTypeTest : DescribeSpec({ TestSuite( "24-bit block", 10, - Arb.triple(Arb.byte(), Arb.byte(), Arb.byte()), + Arb.triple(arbByte, arbByte, arbByte), PrivateNetworkType.TWENTY_FOUR_BIT_BLOCK, - Triple(true, false, false), "10.255.255.255", ), TestSuite( "20-bit block", - -44, - Arb.triple(Arb.byte(min = 16, max = 31), Arb.byte(), Arb.byte()), + 172, + Arb.triple(Arb.int(16..31), arbByte, arbByte), PrivateNetworkType.TWENTY_BIT_BLOCK, - Triple(false, true, false), "172.31.255.255", ), TestSuite( "16-bit block", - -64, - Arb.triple(Arb.of(-88), Arb.byte(), Arb.byte()), + 192, + Arb.triple(Arb.of(168), arbByte, arbByte), PrivateNetworkType.SIXTEEN_BIT_BLOCK, - Triple(false, false, true), "192.168.255.255", ), ) @@ -61,36 +58,36 @@ class PrivateNetworkTypeTest : DescribeSpec({ } shouldContainExactlyInAnyOrder PrivateNetworkType.entries val invalidTestSuites = listOf( - "Too few bytes" to Arb.byteArray(Arb.nonNegativeInt(3), Arb.byte()), - "Too many bytes" to Arb.byteArray(Arb.int(5..UShort.MAX_VALUE.toInt()), Arb.byte()), + "Too few bytes" to Arb.intArray(Arb.nonNegativeInt(3), arbByte), + "Too many bytes" to Arb.intArray(Arb.int(5..UShort.MAX_VALUE.toInt()), arbByte), "Not a private address" to Arb.bind( - Arb.pair(Arb.byte(), Arb.byte()).filter { (a, b) -> - a.toInt() != 10 && - (a.toInt() != -44 || b !in 16..31) && - (a.toInt() != -64 || b.toInt() != -88) + Arb.pair(arbByte, arbByte).filter { (a, b) -> + a != 10 && (a != 172 || b !in 16..31) && (a != 192 || b != 168) }, - Arb.pair(Arb.byte(), Arb.byte()), - ) { (a, b), (c, d) -> byteArrayOf(a, b, c, d) }, + Arb.pair(arbByte, arbByte), + ) { (a, b), (c, d) -> intArrayOf(a, b, c, d) }, ) - allTestSuites.forEach { suite -> + allTestSuites.forEachIndexed { suiteIndex, suite -> val first = suite.firstByte describe(suite.testName) { withData( nameFn = { "${if (it.second) "Matches" else "Does not match"} ${it.first.testName}" }, - allTestSuites.zip(suite.expectedMatches.toList()), + allTestSuites.mapIndexed { index, otherSuite -> + otherSuite to (index == suiteIndex) + }, ) { (testSuite, shouldMatch) -> suite.arbBytes.checkAll { (second, third, fourth) -> - val address = byteArrayOf(first, second, third, fourth).joinToString(".") + val address = intArrayOf(first, second, third, fourth).joinToString(".") testSuite.expectedType.match(address) shouldBeEqual shouldMatch } } it("Defines correct network type") { suite.arbBytes.checkAll { (second, third, fourth) -> - val address = byteArrayOf(first, second, third, fourth).joinToString(".") + val address = intArrayOf(first, second, third, fourth).joinToString(".") PrivateNetworkType.of(address).shouldNotBeNull() shouldBeEqual suite.expectedType }