mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-22 15:43:49 +01:00
Add comprehensive tests for: - network: CryptConn encryption connection tests - signserver: character and member struct validation - entranceserver: encryption roundtrip, server config tests - channelserver: stage creation, object IDs, quest membership All tests pass with race detector enabled.
180 lines
4.7 KiB
Go
180 lines
4.7 KiB
Go
package network
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestNewCryptConn(t *testing.T) {
|
|
// NewCryptConn with nil should not panic
|
|
cc := NewCryptConn(nil)
|
|
|
|
if cc == nil {
|
|
t.Fatal("NewCryptConn() returned nil")
|
|
}
|
|
|
|
// Verify default key rotation values
|
|
if cc.readKeyRot != 995117 {
|
|
t.Errorf("readKeyRot = %d, want 995117", cc.readKeyRot)
|
|
}
|
|
if cc.sendKeyRot != 995117 {
|
|
t.Errorf("sendKeyRot = %d, want 995117", cc.sendKeyRot)
|
|
}
|
|
if cc.sentPackets != 0 {
|
|
t.Errorf("sentPackets = %d, want 0", cc.sentPackets)
|
|
}
|
|
if cc.prevRecvPacketCombinedCheck != 0 {
|
|
t.Errorf("prevRecvPacketCombinedCheck = %d, want 0", cc.prevRecvPacketCombinedCheck)
|
|
}
|
|
if cc.prevSendPacketCombinedCheck != 0 {
|
|
t.Errorf("prevSendPacketCombinedCheck = %d, want 0", cc.prevSendPacketCombinedCheck)
|
|
}
|
|
}
|
|
|
|
func TestCryptConnInitialState(t *testing.T) {
|
|
cc := &CryptConn{}
|
|
|
|
// Zero value should have all zeros
|
|
if cc.readKeyRot != 0 {
|
|
t.Errorf("zero value readKeyRot = %d, want 0", cc.readKeyRot)
|
|
}
|
|
if cc.sendKeyRot != 0 {
|
|
t.Errorf("zero value sendKeyRot = %d, want 0", cc.sendKeyRot)
|
|
}
|
|
if cc.conn != nil {
|
|
t.Error("zero value conn should be nil")
|
|
}
|
|
}
|
|
|
|
func TestCryptConnDefaultKeyRotation(t *testing.T) {
|
|
// The magic number 995117 is the default key rotation value
|
|
const defaultKeyRot = 995117
|
|
|
|
cc := NewCryptConn(nil)
|
|
|
|
if cc.readKeyRot != defaultKeyRot {
|
|
t.Errorf("default readKeyRot = %d, want %d", cc.readKeyRot, defaultKeyRot)
|
|
}
|
|
if cc.sendKeyRot != defaultKeyRot {
|
|
t.Errorf("default sendKeyRot = %d, want %d", cc.sendKeyRot, defaultKeyRot)
|
|
}
|
|
}
|
|
|
|
func TestCryptConnStructFields(t *testing.T) {
|
|
cc := &CryptConn{
|
|
readKeyRot: 123456,
|
|
sendKeyRot: 654321,
|
|
sentPackets: 10,
|
|
prevRecvPacketCombinedCheck: 0x1234,
|
|
prevSendPacketCombinedCheck: 0x5678,
|
|
}
|
|
|
|
if cc.readKeyRot != 123456 {
|
|
t.Errorf("readKeyRot = %d, want 123456", cc.readKeyRot)
|
|
}
|
|
if cc.sendKeyRot != 654321 {
|
|
t.Errorf("sendKeyRot = %d, want 654321", cc.sendKeyRot)
|
|
}
|
|
if cc.sentPackets != 10 {
|
|
t.Errorf("sentPackets = %d, want 10", cc.sentPackets)
|
|
}
|
|
if cc.prevRecvPacketCombinedCheck != 0x1234 {
|
|
t.Errorf("prevRecvPacketCombinedCheck = 0x%X, want 0x1234", cc.prevRecvPacketCombinedCheck)
|
|
}
|
|
if cc.prevSendPacketCombinedCheck != 0x5678 {
|
|
t.Errorf("prevSendPacketCombinedCheck = 0x%X, want 0x5678", cc.prevSendPacketCombinedCheck)
|
|
}
|
|
}
|
|
|
|
func TestCryptConnKeyRotationType(t *testing.T) {
|
|
// Verify key rotation uses uint32
|
|
cc := NewCryptConn(nil)
|
|
|
|
// Simulate key rotation
|
|
keyRotDelta := byte(3)
|
|
cc.sendKeyRot = (uint32(keyRotDelta) * (cc.sendKeyRot + 1))
|
|
|
|
// Should not overflow or behave unexpectedly
|
|
if cc.sendKeyRot == 0 {
|
|
t.Error("sendKeyRot should not be 0 after rotation")
|
|
}
|
|
}
|
|
|
|
func TestCryptConnSentPacketsCounter(t *testing.T) {
|
|
cc := NewCryptConn(nil)
|
|
|
|
if cc.sentPackets != 0 {
|
|
t.Errorf("initial sentPackets = %d, want 0", cc.sentPackets)
|
|
}
|
|
|
|
// Simulate incrementing sent packets
|
|
cc.sentPackets++
|
|
if cc.sentPackets != 1 {
|
|
t.Errorf("sentPackets after increment = %d, want 1", cc.sentPackets)
|
|
}
|
|
|
|
// Verify it's int32
|
|
cc.sentPackets = 0x7FFFFFFF // Max int32
|
|
if cc.sentPackets != 0x7FFFFFFF {
|
|
t.Errorf("sentPackets max value = %d, want %d", cc.sentPackets, 0x7FFFFFFF)
|
|
}
|
|
}
|
|
|
|
func TestCryptConnCombinedCheckStorage(t *testing.T) {
|
|
cc := NewCryptConn(nil)
|
|
|
|
// Test combined check storage
|
|
cc.prevRecvPacketCombinedCheck = 0xABCD
|
|
cc.prevSendPacketCombinedCheck = 0xDCBA
|
|
|
|
if cc.prevRecvPacketCombinedCheck != 0xABCD {
|
|
t.Errorf("prevRecvPacketCombinedCheck = 0x%X, want 0xABCD", cc.prevRecvPacketCombinedCheck)
|
|
}
|
|
if cc.prevSendPacketCombinedCheck != 0xDCBA {
|
|
t.Errorf("prevSendPacketCombinedCheck = 0x%X, want 0xDCBA", cc.prevSendPacketCombinedCheck)
|
|
}
|
|
}
|
|
|
|
func TestCryptConnKeyRotationFormula(t *testing.T) {
|
|
// Test the key rotation formula: (keyRotDelta * (keyRot + 1))
|
|
tests := []struct {
|
|
name string
|
|
initialKey uint32
|
|
keyRotDelta byte
|
|
expectedKey uint32
|
|
}{
|
|
{"delta 1", 995117, 1, 995118},
|
|
{"delta 3 default", 995117, 3, 2985354},
|
|
{"delta 0", 995117, 0, 0},
|
|
{"zero initial", 0, 3, 3},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
newKey := uint32(tt.keyRotDelta) * (tt.initialKey + 1)
|
|
if newKey != tt.expectedKey {
|
|
t.Errorf("key rotation = %d, want %d", newKey, tt.expectedKey)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCryptPacketHeaderLengthConstant(t *testing.T) {
|
|
// CryptPacketHeaderLength should always be 14
|
|
if CryptPacketHeaderLength != 14 {
|
|
t.Errorf("CryptPacketHeaderLength = %d, want 14", CryptPacketHeaderLength)
|
|
}
|
|
}
|
|
|
|
func TestMultipleCryptConnInstances(t *testing.T) {
|
|
// Multiple instances should be independent
|
|
cc1 := NewCryptConn(nil)
|
|
cc2 := NewCryptConn(nil)
|
|
|
|
cc1.sendKeyRot = 12345
|
|
cc2.sendKeyRot = 54321
|
|
|
|
if cc1.sendKeyRot == cc2.sendKeyRot {
|
|
t.Error("CryptConn instances should be independent")
|
|
}
|
|
}
|