mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-22 15:43:49 +01:00
test: improve test coverage from 11% to 20%
Add comprehensive tests across multiple packages: - mhfpacket: Add tests for 300+ packet opcodes, system packets, MHF packets, and detailed parsing tests (6.4% -> 38.8%) - timeserver: Add tests for all time functions (0% -> 97.4%) - deltacomp: Add edge case tests for compression functions - entranceserver: Add server creation tests - binpacket: Add mail notify panic test - config: Add Mode.String() tests - signserver: Expand server tests
This commit is contained in:
@@ -111,3 +111,87 @@ func TestDeltaPatch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyDataDiffEmptyDiff(t *testing.T) {
|
||||
baseData := []byte{1, 2, 3, 4, 5}
|
||||
diff := []byte{}
|
||||
|
||||
result := ApplyDataDiff(diff, baseData)
|
||||
if !bytes.Equal(result, baseData) {
|
||||
t.Errorf("ApplyDataDiff with empty diff should return base data")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyDataDiffEmptyBase(t *testing.T) {
|
||||
baseData := []byte{}
|
||||
diff := []byte{}
|
||||
|
||||
result := ApplyDataDiff(diff, baseData)
|
||||
if len(result) != 0 {
|
||||
t.Errorf("ApplyDataDiff with empty base and diff should return empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckReadUint8Error(t *testing.T) {
|
||||
r := bytes.NewReader([]byte{})
|
||||
_, err := checkReadUint8(r)
|
||||
if err == nil {
|
||||
t.Error("checkReadUint8 on empty reader should return error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckReadUint16Error(t *testing.T) {
|
||||
r := bytes.NewReader([]byte{0x01}) // Only 1 byte, need 2
|
||||
_, err := checkReadUint16(r)
|
||||
if err == nil {
|
||||
t.Error("checkReadUint16 with insufficient data should return error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckReadUint16Success(t *testing.T) {
|
||||
r := bytes.NewReader([]byte{0x12, 0x34})
|
||||
val, err := checkReadUint16(r)
|
||||
if err != nil {
|
||||
t.Errorf("checkReadUint16 error = %v", err)
|
||||
}
|
||||
if val != 0x1234 {
|
||||
t.Errorf("checkReadUint16 = 0x%04X, want 0x1234", val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadCountError(t *testing.T) {
|
||||
r := bytes.NewReader([]byte{})
|
||||
_, err := readCount(r)
|
||||
if err == nil {
|
||||
t.Error("readCount on empty reader should return error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadCountExtended(t *testing.T) {
|
||||
// When count8 is 0, read count16
|
||||
r := bytes.NewReader([]byte{0x00, 0x01, 0x00}) // count8=0, count16=256
|
||||
count, err := readCount(r)
|
||||
if err != nil {
|
||||
t.Errorf("readCount error = %v", err)
|
||||
}
|
||||
if count != 256 {
|
||||
t.Errorf("readCount = %d, want 256", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadCountExtendedError(t *testing.T) {
|
||||
// count8 is 0 but not enough bytes for count16
|
||||
r := bytes.NewReader([]byte{0x00, 0x01})
|
||||
_, err := readCount(r)
|
||||
if err == nil {
|
||||
t.Error("readCount with insufficient data for count16 should return error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckReadUint16EmptyReader(t *testing.T) {
|
||||
r := bytes.NewReader([]byte{})
|
||||
_, err := checkReadUint16(r)
|
||||
if err == nil {
|
||||
t.Error("checkReadUint16 on empty reader should return error")
|
||||
}
|
||||
}
|
||||
|
||||
150
server/channelserver/timeserver/time_mode_test.go
Normal file
150
server/channelserver/timeserver/time_mode_test.go
Normal file
@@ -0,0 +1,150 @@
|
||||
package timeserver
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPFaddTime(t *testing.T) {
|
||||
// Save original state
|
||||
originalPnewtime := Pnewtime
|
||||
defer func() { Pnewtime = originalPnewtime }()
|
||||
|
||||
Pnewtime = 0
|
||||
|
||||
// First call should return 24
|
||||
result := PFadd_time()
|
||||
if result != time.Duration(24) {
|
||||
t.Errorf("PFadd_time() = %v, want 24", result)
|
||||
}
|
||||
|
||||
// Second call should return 48
|
||||
result = PFadd_time()
|
||||
if result != time.Duration(48) {
|
||||
t.Errorf("PFadd_time() second call = %v, want 48", result)
|
||||
}
|
||||
|
||||
// Check Pfixtimer is updated
|
||||
if Pfixtimer != time.Duration(48) {
|
||||
t.Errorf("Pfixtimer = %v, want 48", Pfixtimer)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeCurrent(t *testing.T) {
|
||||
result := TimeCurrent()
|
||||
if result.IsZero() {
|
||||
t.Error("TimeCurrent() returned zero time")
|
||||
}
|
||||
|
||||
// Result should be in the past (7 years ago)
|
||||
now := time.Now()
|
||||
diff := now.Year() - result.Year()
|
||||
if diff != 7 {
|
||||
t.Errorf("TimeCurrent() year diff = %d, want 7", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeMidnight(t *testing.T) {
|
||||
result := Time_midnight()
|
||||
if result.IsZero() {
|
||||
t.Error("Time_midnight() returned zero time")
|
||||
}
|
||||
|
||||
// Result should be midnight (with hour added, so 1:00 AM)
|
||||
if result.Minute() != 0 {
|
||||
t.Errorf("Time_midnight() minute = %d, want 0", result.Minute())
|
||||
}
|
||||
if result.Second() != 0 {
|
||||
t.Errorf("Time_midnight() second = %d, want 0", result.Second())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeStatic(t *testing.T) {
|
||||
// Reset state for testing
|
||||
DoOnce_t = false
|
||||
Fix_t = time.Time{}
|
||||
|
||||
result := Time_static()
|
||||
if result.IsZero() {
|
||||
t.Error("Time_static() returned zero time")
|
||||
}
|
||||
|
||||
// Calling again should return same time (static)
|
||||
result2 := Time_static()
|
||||
if !result.Equal(result2) {
|
||||
t.Error("Time_static() should return same time on second call")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTstaticMidnight(t *testing.T) {
|
||||
// Reset state for testing
|
||||
DoOnce_midnight = false
|
||||
Fix_midnight = time.Time{}
|
||||
|
||||
result := Tstatic_midnight()
|
||||
if result.IsZero() {
|
||||
t.Error("Tstatic_midnight() returned zero time")
|
||||
}
|
||||
|
||||
// Calling again should return same time (static)
|
||||
result2 := Tstatic_midnight()
|
||||
if !result.Equal(result2) {
|
||||
t.Error("Tstatic_midnight() should return same time on second call")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeCurrentWeekUint8(t *testing.T) {
|
||||
result := Time_Current_Week_uint8()
|
||||
|
||||
// Week of month should be 1-5
|
||||
if result < 1 || result > 5 {
|
||||
t.Errorf("Time_Current_Week_uint8() = %d, expected 1-5", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeCurrentWeekUint32(t *testing.T) {
|
||||
result := Time_Current_Week_uint32()
|
||||
|
||||
// Week of month should be 1-5
|
||||
if result < 1 || result > 5 {
|
||||
t.Errorf("Time_Current_Week_uint32() = %d, expected 1-5", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectDay(t *testing.T) {
|
||||
result := Detect_Day()
|
||||
|
||||
// Result should be bool, and true only on Wednesday
|
||||
isWednesday := time.Now().Weekday() == time.Wednesday
|
||||
if result != isWednesday {
|
||||
t.Errorf("Detect_Day() = %v, expected %v (today is %v)", result, isWednesday, time.Now().Weekday())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobalVariables(t *testing.T) {
|
||||
// Test that global variables exist and have expected default types
|
||||
_ = DoOnce_midnight
|
||||
_ = DoOnce_t2
|
||||
_ = DoOnce_t
|
||||
_ = Fix_midnight
|
||||
_ = Fix_t2
|
||||
_ = Fix_t
|
||||
_ = Pfixtimer
|
||||
_ = Pnewtime
|
||||
}
|
||||
|
||||
func TestTimeConsistency(t *testing.T) {
|
||||
// Test that TimeCurrent and Time_midnight are on the same day
|
||||
current := TimeCurrent()
|
||||
midnight := Time_midnight()
|
||||
|
||||
if current.Year() != midnight.Year() {
|
||||
t.Errorf("Year mismatch: current=%d, midnight=%d", current.Year(), midnight.Year())
|
||||
}
|
||||
if current.Month() != midnight.Month() {
|
||||
t.Errorf("Month mismatch: current=%v, midnight=%v", current.Month(), midnight.Month())
|
||||
}
|
||||
if current.Day() != midnight.Day() {
|
||||
t.Errorf("Day mismatch: current=%d, midnight=%d", current.Day(), midnight.Day())
|
||||
}
|
||||
}
|
||||
62
server/entranceserver/entrance_server_test.go
Normal file
62
server/entranceserver/entrance_server_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package entranceserver
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"erupe-ce/config"
|
||||
)
|
||||
|
||||
func TestNewServer(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Logger: nil,
|
||||
DB: nil,
|
||||
ErupeConfig: &config.Config{},
|
||||
}
|
||||
|
||||
s := NewServer(cfg)
|
||||
if s == nil {
|
||||
t.Fatal("NewServer() returned nil")
|
||||
}
|
||||
if s.isShuttingDown {
|
||||
t.Error("New server should not be shutting down")
|
||||
}
|
||||
if s.erupeConfig == nil {
|
||||
t.Error("erupeConfig should not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewServerWithNilConfig(t *testing.T) {
|
||||
cfg := &Config{}
|
||||
s := NewServer(cfg)
|
||||
if s == nil {
|
||||
t.Fatal("NewServer() returned nil for empty config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerType(t *testing.T) {
|
||||
s := &Server{}
|
||||
if s.isShuttingDown {
|
||||
t.Error("Zero value server should not be shutting down")
|
||||
}
|
||||
if s.listener != nil {
|
||||
t.Error("Zero value server should have nil listener")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigFields(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Logger: nil,
|
||||
DB: nil,
|
||||
ErupeConfig: nil,
|
||||
}
|
||||
|
||||
if cfg.Logger != nil {
|
||||
t.Error("Config Logger should be nil")
|
||||
}
|
||||
if cfg.DB != nil {
|
||||
t.Error("Config DB should be nil")
|
||||
}
|
||||
if cfg.ErupeConfig != nil {
|
||||
t.Error("Config ErupeConfig should be nil")
|
||||
}
|
||||
}
|
||||
@@ -210,3 +210,59 @@ func TestFailureRespIsMinimal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewServer(t *testing.T) {
|
||||
// Test that NewServer creates a valid server
|
||||
cfg := &Config{
|
||||
Logger: nil,
|
||||
DB: nil,
|
||||
ErupeConfig: nil,
|
||||
}
|
||||
|
||||
s := NewServer(cfg)
|
||||
if s == nil {
|
||||
t.Fatal("NewServer() returned nil")
|
||||
}
|
||||
if s.isShuttingDown {
|
||||
t.Error("New server should not be shutting down")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewServerWithNilConfig(t *testing.T) {
|
||||
// Testing with nil fields in config
|
||||
cfg := &Config{}
|
||||
s := NewServer(cfg)
|
||||
if s == nil {
|
||||
t.Fatal("NewServer() returned nil for empty config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerType(t *testing.T) {
|
||||
// Test Server struct fields
|
||||
s := &Server{}
|
||||
if s.isShuttingDown {
|
||||
t.Error("Zero value server should not be shutting down")
|
||||
}
|
||||
if s.sessions != nil {
|
||||
t.Error("Zero value server should have nil sessions map")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigFields(t *testing.T) {
|
||||
// Test Config struct fields
|
||||
cfg := &Config{
|
||||
Logger: nil,
|
||||
DB: nil,
|
||||
ErupeConfig: nil,
|
||||
}
|
||||
|
||||
if cfg.Logger != nil {
|
||||
t.Error("Config Logger should be nil")
|
||||
}
|
||||
if cfg.DB != nil {
|
||||
t.Error("Config DB should be nil")
|
||||
}
|
||||
if cfg.ErupeConfig != nil {
|
||||
t.Error("Config ErupeConfig should be nil")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user