207 lines
5.2 KiB
Go
207 lines
5.2 KiB
Go
package eq2net
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestEQPacket(t *testing.T) {
|
|
data := []byte("Hello, World!")
|
|
packet := NewEQPacket(OPPacket, data)
|
|
|
|
if packet.Opcode != OPPacket {
|
|
t.Errorf("Expected opcode %04x, got %04x", OPPacket, packet.Opcode)
|
|
}
|
|
|
|
if packet.Size != uint32(len(data)) {
|
|
t.Errorf("Expected size %d, got %d", len(data), packet.Size)
|
|
}
|
|
|
|
if !bytes.Equal(packet.Buffer, data) {
|
|
t.Errorf("Buffer mismatch")
|
|
}
|
|
}
|
|
|
|
func TestProtocolPacketSerialization(t *testing.T) {
|
|
data := []byte("Test Data")
|
|
packet := NewEQProtocolPacket(OPPacket, data)
|
|
|
|
serialized := packet.Serialize(0)
|
|
|
|
// Check opcode (first 2 bytes)
|
|
if len(serialized) < 2 {
|
|
t.Fatal("Serialized packet too small")
|
|
}
|
|
|
|
// OPPacket = 0x0009, should be [0x00, 0x09] in big-endian
|
|
if serialized[0] != 0x00 || serialized[1] != 0x09 {
|
|
t.Errorf("Opcode not serialized correctly: %02x %02x", serialized[0], serialized[1])
|
|
}
|
|
|
|
// Check data
|
|
if !bytes.Equal(serialized[2:], data) {
|
|
t.Error("Data not serialized correctly")
|
|
}
|
|
}
|
|
|
|
func TestCRC16(t *testing.T) {
|
|
tests := []struct {
|
|
data []byte
|
|
key uint32
|
|
}{
|
|
{[]byte{0x00, 0x09, 0x00, 0x00, 0x00}, 0x12345678},
|
|
{[]byte{0x00, 0x01}, 0},
|
|
{[]byte("Hello"), 0},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
// Just test that CRC16 produces consistent results
|
|
got1 := CRC16(tt.data, len(tt.data), tt.key)
|
|
got2 := CRC16(tt.data, len(tt.data), tt.key)
|
|
if got1 != got2 {
|
|
t.Errorf("CRC16 not consistent: %04x != %04x", got1, got2)
|
|
}
|
|
// Test that different keys produce different CRCs
|
|
if tt.key != 0 {
|
|
got3 := CRC16(tt.data, len(tt.data), 0)
|
|
if got1 == got3 {
|
|
t.Errorf("Different keys should produce different CRCs")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateCRC(t *testing.T) {
|
|
// Test session packet (CRC exempt)
|
|
sessionPacket := []byte{0x00, byte(OPSessionRequest), 0x00, 0x00}
|
|
if !ValidateCRC(sessionPacket, 0) {
|
|
t.Error("Session packet should be CRC exempt")
|
|
}
|
|
|
|
// Test packet with valid CRC
|
|
data := []byte{0x00, 0x09, 0x48, 0x65, 0x6C, 0x6C, 0x6F} // "Hello"
|
|
crc := CRC16(data, len(data), 0x1234)
|
|
dataWithCRC := append(data, byte(crc>>8), byte(crc))
|
|
|
|
if !ValidateCRC(dataWithCRC, 0x1234) {
|
|
t.Error("Packet with valid CRC should validate")
|
|
}
|
|
|
|
// Test packet with invalid CRC
|
|
dataWithCRC[len(dataWithCRC)-1] ^= 0xFF // Corrupt CRC
|
|
if ValidateCRC(dataWithCRC, 0x1234) {
|
|
t.Error("Packet with invalid CRC should not validate")
|
|
}
|
|
}
|
|
|
|
func TestCompression(t *testing.T) {
|
|
// Test simple encoding (small packet)
|
|
smallData := []byte{0x00, 0x09, 0x01, 0x02, 0x03}
|
|
compressed, err := CompressPacket(smallData)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if compressed[2] != CompressionFlagSimple {
|
|
t.Errorf("Small packet should use simple encoding, got flag %02x", compressed[2])
|
|
}
|
|
|
|
decompressed, err := DecompressPacket(compressed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !bytes.Equal(decompressed, smallData) {
|
|
t.Error("Decompressed data doesn't match original")
|
|
}
|
|
|
|
// Test zlib compression (large packet)
|
|
largeData := make([]byte, 100)
|
|
largeData[0] = 0x00
|
|
largeData[1] = 0x09
|
|
for i := 2; i < len(largeData); i++ {
|
|
largeData[i] = byte(i)
|
|
}
|
|
|
|
compressed, err = CompressPacket(largeData)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if compressed[2] != CompressionFlagZlib {
|
|
t.Errorf("Large packet should use zlib compression, got flag %02x", compressed[2])
|
|
}
|
|
|
|
decompressed, err = DecompressPacket(compressed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !bytes.Equal(decompressed, largeData) {
|
|
t.Error("Decompressed large data doesn't match original")
|
|
}
|
|
}
|
|
|
|
func TestChatEncryption(t *testing.T) {
|
|
// Test chat encoding/decoding
|
|
original := []byte{0x00, 0x09, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21} // "Hello!"
|
|
key := uint32(0x12345678)
|
|
|
|
encoded := ChatEncode(original, key)
|
|
if bytes.Equal(encoded[2:], original[2:]) {
|
|
t.Error("Encoded data should differ from original")
|
|
}
|
|
|
|
decoded := ChatDecode(encoded, key)
|
|
if !bytes.Equal(decoded, original) {
|
|
t.Errorf("Decoded data doesn't match original\nOriginal: %v\nDecoded: %v", original, decoded)
|
|
}
|
|
|
|
// Test exempt packet types
|
|
exemptPacket := []byte{0x00, 0x01, 0x12, 0x34}
|
|
encoded = ChatEncode(exemptPacket, key)
|
|
if !bytes.Equal(encoded, exemptPacket) {
|
|
t.Error("Exempt packet should not be encoded")
|
|
}
|
|
}
|
|
|
|
func TestPacketCombiner(t *testing.T) {
|
|
combiner := NewPacketCombiner(256)
|
|
|
|
p1 := NewEQProtocolPacket(OPPacket, []byte{0x01, 0x02})
|
|
p2 := NewEQProtocolPacket(OPAck, []byte{0x03, 0x04})
|
|
p3 := NewEQProtocolPacket(OPKeepAlive, []byte{0x05})
|
|
|
|
combined := combiner.CombineProtocolPackets([]*EQProtocolPacket{p1, p2, p3})
|
|
|
|
if combined == nil {
|
|
t.Fatal("Failed to combine packets")
|
|
}
|
|
|
|
if combined.Opcode != OPCombined {
|
|
t.Errorf("Combined packet should have opcode %04x, got %04x", OPCombined, combined.Opcode)
|
|
}
|
|
|
|
// Verify combined packet structure
|
|
buffer := combined.Buffer
|
|
offset := 0
|
|
|
|
// First packet
|
|
if buffer[offset] != byte(p1.TotalSize()) {
|
|
t.Errorf("First packet size incorrect: %d", buffer[offset])
|
|
}
|
|
offset++
|
|
offset += int(p1.TotalSize())
|
|
|
|
// Second packet
|
|
if buffer[offset] != byte(p2.TotalSize()) {
|
|
t.Errorf("Second packet size incorrect: %d", buffer[offset])
|
|
}
|
|
offset++
|
|
offset += int(p2.TotalSize())
|
|
|
|
// Third packet
|
|
if buffer[offset] != byte(p3.TotalSize()) {
|
|
t.Errorf("Third packet size incorrect: %d", buffer[offset])
|
|
}
|
|
} |