Archived
1
0
Fork 0

Add test for unique packet IDs

This commit is contained in:
Moritz Ruth 2020-09-09 23:49:08 +02:00
parent 4be30418fb
commit 2b522d0d43
6 changed files with 57 additions and 5 deletions

View file

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

View file

@ -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<PacketCodec<*>>, 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<IncomingPacketCodec<*>>(), "incoming", protocol)
ensureIDUniqueness(protocol.codecs.filterIsInstance<OutgoingPacketCodec<*>>(), "outgoing", protocol)
}
}
companion object {
val PROTOCOLS = setOf(HandshakingProtocol, LoginProtocol, StatusProtocol, PlayProtocol)
}
}