diff --git a/README.md b/README.md index b1d7fde..e9cd6ea 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ # Blokk ## To Do -- Stop Using Spek - Support packet compression diff --git a/blokk-api/build.gradle.kts b/blokk-api/build.gradle.kts index ff428fa..afc7054 100644 --- a/blokk-api/build.gradle.kts +++ b/blokk-api/build.gradle.kts @@ -16,6 +16,8 @@ val moshiVersion = properties["version.moshi"].toString() val coroutinesVersion = properties["version.kotlinx-coroutines"].toString() val nettyVersion = properties["version.netty"].toString() val slf4jVersion = properties["version.slf4j"].toString() +val junitVersion = properties["version.junit"].toString() +val striktVersion = properties["version.strikt"].toString() dependencies { // Kotlin @@ -33,9 +35,9 @@ dependencies { api("io.netty:netty-buffer:${nettyVersion}") // Testing - testImplementation("io.strikt:strikt-core:0.26.1") - testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.2") - testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.2") + testImplementation("io.strikt:strikt-core:${striktVersion}") + testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") } tasks { diff --git a/blokk-api/src/main/kotlin/space/blokk/net/packets/Protocol.kt b/blokk-api/src/main/kotlin/space/blokk/net/packets/Protocol.kt index 0c11342..fb7051e 100644 --- a/blokk-api/src/main/kotlin/space/blokk/net/packets/Protocol.kt +++ b/blokk-api/src/main/kotlin/space/blokk/net/packets/Protocol.kt @@ -3,7 +3,7 @@ package space.blokk.net.packets import kotlin.reflect.KClass abstract class Protocol constructor(val name: String, vararg codecs: PacketCodec<*>) { - val test = emptyList() + val codecs = codecs.toSet() private val codecsByPacketType = codecs.map { it.dataType to it }.toMap() val incomingPacketCodecsByID = codecs.filterIsInstance>().map { it.id to it }.toMap() diff --git a/blokk-packet-codecs/build.gradle.kts b/blokk-packet-codecs/build.gradle.kts index 2073a52..301b44e 100644 --- a/blokk-packet-codecs/build.gradle.kts +++ b/blokk-packet-codecs/build.gradle.kts @@ -7,19 +7,35 @@ version = rootProject.version repositories { mavenCentral() + jcenter() } val nettyVersion = properties["version.netty"].toString() +val junitVersion = properties["version.junit"].toString() +val striktVersion = properties["version.strikt"].toString() dependencies { api(project(":blokk-packets")) // Netty api("io.netty:netty-buffer:${nettyVersion}") + + // Testing + testImplementation("io.strikt:strikt-core:${striktVersion}") + testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") } tasks { compileKotlin { kotlinOptions.jvmTarget = "1.8" } + + compileTestKotlin { + kotlinOptions.jvmTarget = "1.8" + } + + test { + useJUnitPlatform() + } } diff --git a/blokk-packet-codecs/src/test/kotlin/space/blokk/net/packets/ProtocolValidationTest.kt b/blokk-packet-codecs/src/test/kotlin/space/blokk/net/packets/ProtocolValidationTest.kt new file mode 100644 index 0000000..04e829c --- /dev/null +++ b/blokk-packet-codecs/src/test/kotlin/space/blokk/net/packets/ProtocolValidationTest.kt @@ -0,0 +1,33 @@ +package space.blokk.net.packets + +import org.junit.jupiter.api.Test +import space.blokk.net.packets.handshaking.HandshakingProtocol +import space.blokk.net.packets.login.LoginProtocol +import space.blokk.net.packets.play.PlayProtocol +import space.blokk.net.packets.status.StatusProtocol + +class ProtocolValidationTest { + private fun ensureIDUniqueness(codecs: List>, type: String, protocol: Protocol) { + codecs.groupBy { it.id }.forEach { (id, c) -> + if (c.count() > 1) { + val packetsString = c.joinToString(", ", limit = 4) { it.dataType.simpleName.toString() } + throw AssertionError( + "Multiple $type packets of the ${protocol.name} protocol use the same ID " + + "(0x${id.toString(16)}): $packetsString" + ) + } + } + } + + @Test + fun `all packets have unique IDs in their protocol`() { + for (protocol in PROTOCOLS) { + ensureIDUniqueness(protocol.codecs.filterIsInstance>(), "incoming", protocol) + ensureIDUniqueness(protocol.codecs.filterIsInstance>(), "outgoing", protocol) + } + } + + companion object { + val PROTOCOLS = setOf(HandshakingProtocol, LoginProtocol, StatusProtocol, PlayProtocol) + } +} diff --git a/gradle.properties b/gradle.properties index 9906446..a5d7b1b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,3 +4,5 @@ version.netty=4.1.50.Final version.moshi=1.9.3 version.kotlinx-coroutines=1.3.8 version.slf4j=1.7.30 +version.junit=5.6.2 +version.strikt=0.26.1