Files
2026-08-31 23:01:34 +01:00

93 lines
3.6 KiB
Go

package testkit
import (
"errors"
"testing"
"time"
"github.com/cosmic-clash/cosmic-clash/server/domain"
)
func TestFakeSteamVerifierIsDeterministicAndOffline(t *testing.T) {
now := time.Unix(1000, 0)
fake, err := NewFakeSteamVerifier(480)
if err != nil {
t.Fatal(err)
}
fake.Identities["steam-1"] = "player-1"
ticket := domain.SteamTicket{TicketID: "ticket-1", SteamID: "steam-1", AppID: 480, ExpiresAt: now.Add(time.Minute)}
identity, err := fake.Verify(ticket, now)
if err != nil || identity.PlayerID != "player-1" {
t.Fatalf("identity = %+v err=%v", identity, err)
}
if _, err := fake.Verify(ticket, now); err == nil {
t.Fatal("fake accepted ticket replay")
}
}
func TestFakeAllocatorCanForceFailureWithoutCloudState(t *testing.T) {
fake, err := NewFakeAllocator([]domain.ReadyServer{{ServerID: "server-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet", State: domain.ServerReady}})
if err != nil {
t.Fatal(err)
}
fake.ForcedError = errors.New("forced allocation failure")
_, err = fake.Allocate(domain.AllocationRequest{AllocationID: "allocation-1", MatchID: "match-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}, time.Unix(1000, 0))
if err == nil || err.Error() != "forced allocation failure" {
t.Fatalf("forced failure = %v", err)
}
}
func TestOfflineFakesCoverVerificationAndAllocationFailureMatrix(t *testing.T) {
now := time.Unix(1000, 0)
fakeSteam, err := NewFakeSteamVerifier(480)
if err != nil {
t.Fatal(err)
}
fakeSteam.Identities["steam-good"] = "player-good"
tests := []struct {
name string
ticket domain.SteamTicket
wantOK bool
}{
{name: "unknown identity", ticket: domain.SteamTicket{TicketID: "ticket-unknown", SteamID: "steam-unknown", AppID: 480, ExpiresAt: now.Add(time.Minute)}},
{name: "wrong app", ticket: domain.SteamTicket{TicketID: "ticket-wrong-app", SteamID: "steam-good", AppID: 481, ExpiresAt: now.Add(time.Minute)}},
{name: "expired", ticket: domain.SteamTicket{TicketID: "ticket-expired", SteamID: "steam-good", AppID: 480, ExpiresAt: now}},
{name: "valid", ticket: domain.SteamTicket{TicketID: "ticket-valid", SteamID: "steam-good", AppID: 480, ExpiresAt: now.Add(time.Minute)}, wantOK: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
identity, verifyErr := fakeSteam.Verify(test.ticket, now)
if (verifyErr == nil) != test.wantOK {
t.Fatalf("identity = %+v err = %v", identity, verifyErr)
}
})
}
if _, err := fakeSteam.Verify(tests[3].ticket, now); err == nil {
t.Fatal("valid Steam ticket replay was accepted")
}
fakeAllocator, err := NewFakeAllocator([]domain.ReadyServer{{ServerID: "server-eu", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet", State: domain.ServerReady}})
if err != nil {
t.Fatal(err)
}
base := domain.AllocationRequest{AllocationID: "allocation-1234567890123456", MatchID: "match-1234567890123456", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}
if _, err := fakeAllocator.Allocate(base, now); err != nil {
t.Fatal(err)
}
if _, err := fakeAllocator.Allocate(base, now); err != nil {
t.Fatalf("identical allocation replay failed: %v", err)
}
conflict := base
conflict.Transport = "steam_sdr"
if _, err := fakeAllocator.Allocate(conflict, now); err == nil {
t.Fatal("allocation key reuse with changed compatibility was accepted")
}
noCapacity := base
noCapacity.AllocationID = "allocation-no-capacity-123456"
noCapacity.MatchID = "match-no-capacity-123456"
noCapacity.Region = "NA"
if _, err := fakeAllocator.Allocate(noCapacity, now); err != domain.ErrNoCapacity {
t.Fatalf("no-capacity error = %v", err)
}
}