Files
browser/plugins/examples/scum-server-plugin/companion/adapters_test.go
T

100 lines
5.1 KiB
Go

package companion
import (
"context"
"testing"
"time"
)
type configPortFixture struct {
fields map[string]string
patches []ConfigFieldPatch
}
func (fixture *configPortFixture) ReadConfig(context.Context) (map[string]string, error) {
return fixture.fields, nil
}
func (fixture *configPortFixture) ApplyConfigPatch(_ context.Context, _ string, fields []ConfigFieldPatch) (map[string]string, error) {
fixture.patches = fields
return map[string]string{"ServerName": "Moon", "hostPath": "C:/secret"}, nil
}
type notificationPortFixture struct {
deliveries []ue4SSPlayerNotification
accepted bool
}
func (fixture *notificationPortFixture) SendPlayerNotification(_ context.Context, notification ue4SSPlayerNotification) (UE4SSNotificationReceipt, error) {
fixture.deliveries = append(fixture.deliveries, notification)
return UE4SSNotificationReceipt{Accepted: fixture.accepted}, nil
}
func TestVersionedAdapterUsesOnlyLogicalConfigValuesAndRedactsDiagnostics(t *testing.T) {
port := &configPortFixture{fields: map[string]string{"ServerName": "Moon", "hostPath": "C:/secret", "Password": "nope"}}
adapter := VersionedAdapter{ServerVersion: "0.9.700.90357", Config: port, DiagnosticsState: map[string]string{"status": "healthy", "hostPath": "C:/secret"}}
read, err := adapter.ReadConfiguration(context.Background())
if err != nil {
t.Fatalf("read config: %v", err)
}
fields := read["fields"].(map[string]string)
if fields["ServerName"] != "Moon" || len(fields) != 1 {
t.Fatalf("unsafe config was exposed: %+v", fields)
}
patched, err := adapter.PatchConfiguration(context.Background(), map[string]any{"revision": "r1", "fields": []any{map[string]any{"key": "ServerName", "value": "Moon"}}})
if err != nil {
t.Fatalf("patch config: %v", err)
}
if len(port.patches) != 1 || patched["appliedFields"].(map[string]string)["hostPath"] != "" {
t.Fatalf("patch leaked unsafe details: %+v", patched)
}
diagnostics, _ := adapter.Diagnostics(context.Background())
if diagnostics["hostPath"] != nil || diagnostics["status"] != "healthy" {
t.Fatalf("diagnostics leaked unsafe details: %+v", diagnostics)
}
}
func TestVersionedUE4SSNotificationIsFixedTypedAndRedacted(t *testing.T) {
port := &notificationPortFixture{accepted: true}
adapter := VersionedAdapter{BoundServerID: "server-1", ServerVersion: "0.9.700.90357", UE4SSBuild: UE4SSReferenceBuild, UE4SSReferenceRevision: UE4SSReferenceRevision, Notification: port}
result, err := adapter.NotifyPlayer(context.Background(), map[string]any{"playerId": "76561198000000001", "message": "Moon \"gift\""})
if err != nil || !result["accepted"].(bool) || len(port.deliveries) != 1 {
t.Fatalf("typed notification was not delivered: result=%+v err=%v deliveries=%+v", result, err, port.deliveries)
}
delivery := port.deliveries[0]
if delivery.ServerID != "server-1" || delivery.chatType != fixedNotificationType || delivery.protectedAuditCommand != "SendChat 4 \"Moon \\\"gift\\\"\" 76561198000000001" {
t.Fatalf("notification did not use the fixed UE4SS contract: %+v", delivery)
}
if result["message"] == delivery.protectedAuditCommand || result["command"] != nil || result["rcon"] != nil {
t.Fatalf("notification leaked protected transport details: %+v", result)
}
}
func TestVersionedUE4SSNotificationFailsClosedForUnpinnedBuildOrInvalidRecipient(t *testing.T) {
port := &notificationPortFixture{accepted: true}
adapter := VersionedAdapter{BoundServerID: "server-1", ServerVersion: "0.9.700.90357", UE4SSBuild: "3.0.2", UE4SSReferenceRevision: UE4SSReferenceRevision, Notification: port}
if _, err := adapter.NotifyPlayer(context.Background(), map[string]any{"playerId": "76561198000000001", "message": "Moonlight"}); err == nil {
t.Fatal("unpinned UE4SS build must be unavailable")
}
adapter.UE4SSBuild = UE4SSReferenceBuild
if _, err := adapter.NotifyPlayer(context.Background(), map[string]any{"playerId": "not-a-steam-id", "message": "Moonlight"}); err == nil {
t.Fatal("unverified recipient identity must be rejected")
}
}
func TestNotificationFailureIsCachedWithoutInvokingRewardDelivery(t *testing.T) {
stamp := time.Now().UTC()
port := &notificationPortFixture{accepted: false}
adapter := VersionedAdapter{BoundServerID: "server-1", ServerVersion: "0.9.700.90357", UE4SSBuild: UE4SSReferenceBuild, UE4SSReferenceRevision: UE4SSReferenceRevision, Notification: port}
registry := NewHandlerRegistry(HandlerAvailability{BoundServerID: "server-1", ServerVersion: "0.9.700.90357", Approved: true, Capabilities: map[string]bool{"player.notify": true}}, adapter)
command := ClaimedCommand{ID: "notification-1", ProfileKey: ProfileKey, CommandType: "player.notify", Payload: map[string]any{"playerId": "76561198000000001", "message": "Moonlight"}, FencingToken: 1, LeaseExpiresAt: stamp.Add(time.Minute), ExpiresAt: stamp.Add(time.Minute)}
for range 2 {
result, err := registry.Execute(context.Background(), command)
if err != nil || result.Payload["accepted"] != false {
t.Fatalf("notification failure was not typed: result=%+v err=%v", result, err)
}
}
if len(port.deliveries) != 1 {
t.Fatalf("duplicate notification attempted transport %d times", len(port.deliveries))
}
}