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

@ -1,4 +1,3 @@
# Blokk
## To Do
- Stop Using Spek
- Support packet compression

View file

@ -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 {

View file

@ -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<Packet>()
val codecs = codecs.toSet()
private val codecsByPacketType = codecs.map { it.dataType to it }.toMap()
val incomingPacketCodecsByID = codecs.filterIsInstance<IncomingPacketCodec<*>>().map { it.id to it }.toMap()

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

View file

@ -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