mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
feat: add assignment readiness gate
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func testAllocation() Allocation {
|
||||
return Allocation{AllocationID: "allocation-1", MatchID: "match-1", ServerID: "server-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet", State: ServerAllocated, AllocatedAt: time.Unix(1000, 0)}
|
||||
}
|
||||
|
||||
func testManifest() AllocationManifest {
|
||||
return AllocationManifest{AllocationID: "allocation-1", MatchID: "match-1", ServerID: "server-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet", RosterDigest: "roster-digest"}
|
||||
}
|
||||
|
||||
func TestAssignmentReadyRequiresBoundSignedManifestAndEndpoint(t *testing.T) {
|
||||
manifest := testManifest()
|
||||
digest := ManifestDigest(manifest)
|
||||
sign := func(payload, signature []byte) bool {
|
||||
return string(payload) == string(manifestBytes(manifest)) && string(signature) == string(digest[:])
|
||||
}
|
||||
assignment, err := VerifyAssignment(testAllocation(), manifest, "203.0.113.9:31001", digest[:], sign)
|
||||
if err != nil || assignment.Endpoint == "" {
|
||||
t.Fatalf("assignment = %+v err=%v", assignment, err)
|
||||
}
|
||||
if _, err := VerifyAssignment(testAllocation(), manifest, "", digest[:], sign); !errors.Is(err, ErrManifestRejected) {
|
||||
t.Fatalf("empty endpoint accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignmentReadyRejectsTamperedOrPrematureManifest(t *testing.T) {
|
||||
manifest := testManifest()
|
||||
digest := ManifestDigest(manifest)
|
||||
verify := func(payload, signature []byte) bool {
|
||||
return string(payload) == string(manifestBytes(manifest)) && string(signature) == string(digest[:])
|
||||
}
|
||||
tampered := manifest
|
||||
tampered.ServerID = "server-2"
|
||||
if _, err := VerifyAssignment(testAllocation(), tampered, "127.0.0.1:1", digest[:], verify); !errors.Is(err, ErrManifestRejected) {
|
||||
t.Fatalf("tampered manifest accepted: %v", err)
|
||||
}
|
||||
ready := testAllocation()
|
||||
ready.State = ServerReady
|
||||
if _, err := VerifyAssignment(ready, manifest, "127.0.0.1:1", digest[:], verify); !errors.Is(err, ErrManifestRejected) {
|
||||
t.Fatalf("Ready process exposed assignment: %v", err)
|
||||
}
|
||||
wrongBuild := manifest
|
||||
wrongBuild.Build = "build-2"
|
||||
if _, err := VerifyAssignment(testAllocation(), wrongBuild, "127.0.0.1:1", digest[:], verify); !errors.Is(err, ErrManifestRejected) {
|
||||
t.Fatalf("incompatible build accepted: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user