Convert into a multi-module project.
For now, the API module only contains a few assorted utilities. More will be added later.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.velocitypowered.proxy.protocol;
|
||||
|
||||
import com.velocitypowered.proxy.protocol.packets.Handshake;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12;
|
||||
import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12_1;
|
||||
import static com.velocitypowered.proxy.protocol.ProtocolConstants.MINECRAFT_1_12_2;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PacketRegistryTest {
|
||||
private StateRegistry.PacketRegistry setupRegistry() {
|
||||
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND, StateRegistry.HANDSHAKE);
|
||||
registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12));
|
||||
return registry;
|
||||
}
|
||||
|
||||
@Test
|
||||
void packetRegistryWorks() {
|
||||
StateRegistry.PacketRegistry registry = setupRegistry();
|
||||
MinecraftPacket packet = registry.getVersion(MINECRAFT_1_12).createPacket(0);
|
||||
assertNotNull(packet, "Packet was not found in registry");
|
||||
assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class");
|
||||
|
||||
assertEquals(0, registry.getVersion(MINECRAFT_1_12).getPacketId(packet), "Registry did not return the correct packet ID");
|
||||
}
|
||||
|
||||
@Test
|
||||
void packetRegistryLinkingWorks() {
|
||||
StateRegistry.PacketRegistry registry = setupRegistry();
|
||||
MinecraftPacket packet = registry.getVersion(MINECRAFT_1_12_1).createPacket(0);
|
||||
assertNotNull(packet, "Packet was not found in registry");
|
||||
assertEquals(Handshake.class, packet.getClass(), "Registry returned wrong class");
|
||||
assertEquals(0, registry.getVersion(MINECRAFT_1_12_1).getPacketId(packet), "Registry did not return the correct packet ID");
|
||||
}
|
||||
|
||||
@Test
|
||||
void failOnNoMappings() {
|
||||
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND, StateRegistry.HANDSHAKE);
|
||||
assertThrows(IllegalArgumentException.class, () -> registry.register(Handshake.class, Handshake::new));
|
||||
assertThrows(IllegalArgumentException.class, () -> registry.getVersion(0).getPacketId(new Handshake()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registrySuppliesCorrectPacketsByProtocol() {
|
||||
StateRegistry.PacketRegistry registry = new StateRegistry.PacketRegistry(ProtocolConstants.Direction.CLIENTBOUND, StateRegistry.HANDSHAKE);
|
||||
registry.register(Handshake.class, Handshake::new, new StateRegistry.PacketMapping(0x00, MINECRAFT_1_12),
|
||||
new StateRegistry.PacketMapping(0x01, MINECRAFT_1_12_1));
|
||||
assertEquals(Handshake.class, registry.getVersion(MINECRAFT_1_12).createPacket(0x00).getClass());
|
||||
assertEquals(Handshake.class, registry.getVersion(MINECRAFT_1_12_1).createPacket(0x01).getClass());
|
||||
assertEquals(Handshake.class, registry.getVersion(MINECRAFT_1_12_2).createPacket(0x01).getClass());
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package com.velocitypowered.proxy.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class EncryptionUtilsTest {
|
||||
@Test
|
||||
void twosComplementHexdigest() throws Exception {
|
||||
String notchHash = mojangLoginSha1("Notch");
|
||||
assertEquals("4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48", notchHash);
|
||||
|
||||
String jebHash = mojangLoginSha1("jeb_");
|
||||
assertEquals("-7c9d5b0044c130109a5d7b5fb5c317c02b4e28c1", jebHash);
|
||||
}
|
||||
|
||||
private String mojangLoginSha1(String str) throws Exception {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
||||
digest.update(str.getBytes(StandardCharsets.UTF_8));
|
||||
byte[] digested = digest.digest();
|
||||
return EncryptionUtils.twosComplementHexdigest(digested);
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
package com.velocitypowered.proxy.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class UuidUtilsTest {
|
||||
private static final UUID EXPECTED_DASHED_UUID = UUID.fromString("6b501978-d3be-4f33-bcf6-6e7808f37a0d");
|
||||
private static final String ACTUAL_UNDASHED_UUID = EXPECTED_DASHED_UUID.toString().replace("-", "");
|
||||
|
||||
private static final UUID TEST_OFFLINE_PLAYER_UUID = UUID.fromString("708f6260-183d-3912-bbde-5e279a5e739a");
|
||||
private static final String TEST_OFFLINE_PLAYER = "tuxed";
|
||||
|
||||
@Test
|
||||
void generateOfflinePlayerUuid() {
|
||||
assertEquals(TEST_OFFLINE_PLAYER_UUID, UuidUtils.generateOfflinePlayerUuid(TEST_OFFLINE_PLAYER), "UUIDs do not match");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromUndashed() {
|
||||
assertEquals(EXPECTED_DASHED_UUID, UuidUtils.fromUndashed(ACTUAL_UNDASHED_UUID), "UUIDs do not match");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toUndashed() {
|
||||
assertEquals(ACTUAL_UNDASHED_UUID, UuidUtils.toUndashed(EXPECTED_DASHED_UUID), "UUIDs do not match");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user