packet testing

This commit is contained in:
Bixilon 2022-11-15 09:30:01 +01:00
parent 581a1cb18b
commit 8ed96d758e
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 146 additions and 12 deletions

View File

@ -13,45 +13,62 @@
package de.bixilon.minosoft.data.container.click
import de.bixilon.minosoft.data.container.click.ContainerTestUtil.createContainer
import de.bixilon.minosoft.data.container.stack.ItemStack
import de.bixilon.minosoft.data.registries.items.AppleTestO
import de.bixilon.minosoft.data.registries.items.EggTestO
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnectionUtil.assertNoPacket
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnectionUtil.assertOnlyPacket
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnectionUtil.createConnection
import de.bixilon.minosoft.protocol.packets.c2s.play.container.ContainerClickC2SP
import org.testng.Assert.*
import org.testng.annotations.Test
@Test(groups = ["container"])
@Deprecated("Verify with minecraft")
class CloneContainerActionTest {
fun testEmpty() {
val container = ContainerTestUtil.createContainer()
val connection = createConnection()
val container = createContainer(connection)
container.invokeAction(CloneContainerAction(0))
assertNull(container.floatingItem)
connection.assertNoPacket()
}
fun testAlready() {
val container = ContainerTestUtil.createContainer()
container.floatingItem = ItemStack(EggTest0.item, count = 7)
val connection = createConnection()
val container = createContainer(connection)
container.floatingItem = ItemStack(EggTestO.item, count = 7)
container.invokeAction(CloneContainerAction(6))
assertEquals(container.floatingItem, ItemStack(EggTest0.item, count = 7))
assertEquals(container.floatingItem, ItemStack(EggTestO.item, count = 7))
connection.assertNoPacket()
}
fun testTaking() {
val container = ContainerTestUtil.createContainer()
container[1] = ItemStack(AppleTest0.item)
val connection = createConnection()
val container = createContainer(connection)
container[1] = ItemStack(AppleTestO.item)
container.invokeAction(CloneContainerAction(1))
assertEquals(container.floatingItem, ItemStack(AppleTest0.item, count = 64))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 64))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 1, 3, 0, 0, emptyMap(), ItemStack(AppleTestO.item)))
}
fun taskTalking2() {
val container = ContainerTestUtil.createContainer()
container[3] = ItemStack(AppleTest0.item, count = 8)
val connection = createConnection()
val container = createContainer(connection)
container[3] = ItemStack(AppleTestO.item, count = 8)
container.invokeAction(CloneContainerAction(3))
assertEquals(container.floatingItem, ItemStack(AppleTest0.item, count = 64))
assertEquals(container.floatingItem, ItemStack(AppleTestO.item, count = 64))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 3, 3, 0, 0, emptyMap(), ItemStack(AppleTestO.item, count = 8)))
}
fun testStackLimit() {
val container = ContainerTestUtil.createContainer()
val connection = createConnection()
val container = createContainer(connection)
container[8] = ItemStack(EggTestO.item, count = 9)
container.invokeAction(CloneContainerAction(8))
assertEquals(container.floatingItem, ItemStack(EggTestO.item, count = 16))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 8, 3, 0, 0, emptyMap(), ItemStack(EggTestO.item, count = 9)))
}
}

View File

@ -14,11 +14,14 @@
package de.bixilon.minosoft.data.container.click
import de.bixilon.minosoft.data.container.Container
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnectionUtil.createConnection
object ContainerTestUtil {
fun createContainer(): Container {
fun createContainer(connection: PlayConnection = createConnection()): Container {
// TODO: set id to 9
TODO()
}
}

View File

@ -0,0 +1,57 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.network.connection.play
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.minosoft.IT.reference
import de.bixilon.minosoft.protocol.network.network.client.test.TestNetwork
import de.bixilon.minosoft.protocol.packets.c2s.C2SPacket
import org.objenesis.ObjenesisStd
import org.testng.Assert
import org.testng.Assert.assertNull
object PlayConnectionUtil {
private val OBJENESIS = ObjenesisStd()
init {
reference()
}
fun createConnection(): PlayConnection {
val connection = OBJENESIS.newInstance(PlayConnection::class.java)
TODO()
connection::network.forceSet(TestNetwork())
return connection
}
fun PlayConnection.test(): TestNetwork {
return network.unsafeCast()
}
fun PlayConnection.assertPacket(packet: C2SPacket) {
Assert.assertEquals(test().take(), packet)
}
fun PlayConnection.assertNoPacket() {
assertNull(test().take())
}
fun PlayConnection.assertOnlyPacket(packet: C2SPacket) {
assertPacket(packet)
assertNoPacket()
}
}

View File

@ -0,0 +1,57 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.network.network.client.test
import de.bixilon.minosoft.protocol.address.ServerAddress
import de.bixilon.minosoft.protocol.network.network.client.ClientNetwork
import de.bixilon.minosoft.protocol.packets.c2s.C2SPacket
import de.bixilon.minosoft.protocol.protocol.ProtocolStates
import java.util.concurrent.ConcurrentLinkedQueue
import javax.crypto.Cipher
class TestNetwork : ClientNetwork {
override var connected: Boolean = false
override var state: ProtocolStates = ProtocolStates.PLAY
override var compressionThreshold: Int = -1
override var encrypted: Boolean = false
private var queue = ConcurrentLinkedQueue<C2SPacket>()
override fun connect(address: ServerAddress, native: Boolean) {
connected = true
}
override fun disconnect() {
connected = false
}
override fun setupEncryption(encrypt: Cipher, decrypt: Cipher) {
encrypted = true
}
override fun pauseSending(pause: Boolean) = Unit
override fun pauseReceiving(pause: Boolean) = Unit
override fun send(packet: C2SPacket) {
if (queue.size > 15) {
// leaking
return
}
queue += packet
}
fun take(): C2SPacket? {
return queue.remove()
}
}