package outbound_test import ( "errors" "fmt" "log/slog" "math" "strings" "testing" "github.com/google/uuid" "lincodex/backend/internal/port/outbound" ) const sourceMarker = "source-marker-7f3a" func validCodeRunRequest() outbound.CodeRunRequest { return outbound.CodeRunRequest{ Key: outbound.CodeRunKey{AttemptItemID: uuid.New(), SubmissionID: uuid.New()}, Language: outbound.CodeLanguageGo, Source: "package main // " + sourceMarker, Limits: outbound.CodeRunLimits{TimeLimitMs: 1000, MemoryLimitMB: 128, OutputLimitKB: 64}, Groups: []outbound.CodeTestGroup{ { ID: "public", Visibility: outbound.CodeTestPublic, Checker: outbound.CodeChecker{Kind: outbound.CodeCheckerExact}, Tests: []outbound.CodeTest{{ID: "1", Input: "1\n", ExpectedOutput: "2\n"}}, }, { ID: "hidden-1", Visibility: outbound.CodeTestHidden, Checker: outbound.CodeChecker{Kind: outbound.CodeCheckerNumbers, AbsTolerance: 1e-6}, Tests: []outbound.CodeTest{{ID: "1", Input: "hidden-input-marker\n", ExpectedOutput: "hidden-output-marker\n"}}, }, }, } } func TestCodeRunRequestValidateAcceptsAContractRequest(t *testing.T) { if err := validCodeRunRequest().Validate(); err != nil { t.Fatalf("Validate() error = %v", err) } } func TestCodeRunRequestValidateRejectsWhatTheRunnerRejects(t *testing.T) { tests := []struct { name string change func(*outbound.CodeRunRequest) }{ {"no attempt item", func(r *outbound.CodeRunRequest) { r.Key.AttemptItemID = uuid.Nil }}, {"no submission", func(r *outbound.CodeRunRequest) { r.Key.SubmissionID = uuid.Nil }}, {"unknown language", func(r *outbound.CodeRunRequest) { r.Language = "java" }}, {"empty source", func(r *outbound.CodeRunRequest) { r.Source = " \n" }}, {"source above 64 KiB", func(r *outbound.CodeRunRequest) { r.Source = strings.Repeat("a", 65537) }}, {"source not UTF-8", func(r *outbound.CodeRunRequest) { r.Source = "\xff\xfe" }}, {"source with NUL", func(r *outbound.CodeRunRequest) { r.Source = "a\x00b" }}, {"time limit below range", func(r *outbound.CodeRunRequest) { r.Limits.TimeLimitMs = 99 }}, {"time limit above range", func(r *outbound.CodeRunRequest) { r.Limits.TimeLimitMs = 10001 }}, {"memory below range", func(r *outbound.CodeRunRequest) { r.Limits.MemoryLimitMB = 31 }}, {"memory above range", func(r *outbound.CodeRunRequest) { r.Limits.MemoryLimitMB = 513 }}, {"output below range", func(r *outbound.CodeRunRequest) { r.Limits.OutputLimitKB = 0 }}, {"output above range", func(r *outbound.CodeRunRequest) { r.Limits.OutputLimitKB = 1025 }}, {"no groups", func(r *outbound.CodeRunRequest) { r.Groups = nil }}, {"five groups", func(r *outbound.CodeRunRequest) { for index := range 3 { group := r.Groups[0] group.ID = fmt.Sprintf("extra-%d", index) r.Groups = append(r.Groups, group) } }}, {"duplicate group ID", func(r *outbound.CodeRunRequest) { r.Groups[1].ID = r.Groups[0].ID }}, {"group ID with capitals", func(r *outbound.CodeRunRequest) { r.Groups[0].ID = "Public" }}, {"unknown visibility", func(r *outbound.CodeRunRequest) { r.Groups[0].Visibility = "secret" }}, {"group without tests", func(r *outbound.CodeRunRequest) { r.Groups[0].Tests = nil }}, {"fifty one tests", func(r *outbound.CodeRunRequest) { r.Limits.TimeLimitMs = 100 r.Groups[0].Tests = nil for index := range 51 { r.Groups[0].Tests = append(r.Groups[0].Tests, outbound.CodeTest{ID: fmt.Sprintf("t%d", index)}) } }}, {"duplicate test ID", func(r *outbound.CodeRunRequest) { r.Groups[0].Tests = append(r.Groups[0].Tests, r.Groups[0].Tests[0]) }}, {"test input too large", func(r *outbound.CodeRunRequest) { r.Groups[0].Tests[0].Input = strings.Repeat("a", 262145) }}, {"expected output too large", func(r *outbound.CodeRunRequest) { r.Groups[0].Tests[0].ExpectedOutput = strings.Repeat("a", 262145) }}, {"CPU budget above 300 s", func(r *outbound.CodeRunRequest) { r.Limits.TimeLimitMs = 10000 for index := range 30 { r.Groups[0].Tests = append(r.Groups[0].Tests, outbound.CodeTest{ID: fmt.Sprintf("t%d", index+2)}) } }}, {"unknown checker", func(r *outbound.CodeRunRequest) { r.Groups[0].Checker.Kind = "regex" }}, {"exact with tolerance", func(r *outbound.CodeRunRequest) { r.Groups[0].Checker.AbsTolerance = 1 }}, {"numbers without tolerance", func(r *outbound.CodeRunRequest) { r.Groups[1].Checker.AbsTolerance = 0 }}, {"numbers with NaN tolerance", func(r *outbound.CodeRunRequest) { r.Groups[1].Checker.AbsTolerance = math.NaN() }}, {"numbers with negative tolerance", func(r *outbound.CodeRunRequest) { r.Groups[1].Checker.RelTolerance = -1 }}, {"numbers with relative tolerance above 1", func(r *outbound.CodeRunRequest) { r.Groups[1].Checker.RelTolerance = 2 }}, {"numbers with a program", func(r *outbound.CodeRunRequest) { r.Groups[1].Checker.Program = &outbound.CodeCheckerProgram{Language: outbound.CodeLanguageGo, Source: "package main"} }}, {"custom without program", func(r *outbound.CodeRunRequest) { r.Groups[0].Checker = outbound.CodeChecker{Kind: outbound.CodeCheckerCustom} }}, {"custom with tolerance", func(r *outbound.CodeRunRequest) { r.Groups[0].Checker = outbound.CodeChecker{ Kind: outbound.CodeCheckerCustom, AbsTolerance: 1, Program: &outbound.CodeCheckerProgram{Language: outbound.CodeLanguageGo, Source: "package main"}, } }}, {"custom with empty program", func(r *outbound.CodeRunRequest) { r.Groups[0].Checker = outbound.CodeChecker{Kind: outbound.CodeCheckerCustom, Program: &outbound.CodeCheckerProgram{Language: outbound.CodeLanguageGo}} }}, {"payload above the runner limit", func(r *outbound.CodeRunRequest) { r.Limits.TimeLimitMs = 100 r.Groups = r.Groups[:1] r.Groups[0].Tests = nil for index := range 50 { r.Groups[0].Tests = append(r.Groups[0].Tests, outbound.CodeTest{ ID: fmt.Sprintf("t%d", index), Input: strings.Repeat("a", 262144), ExpectedOutput: strings.Repeat("b", 262144), }) } }}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { request := validCodeRunRequest() test.change(&request) err := request.Validate() if !errors.Is(err, outbound.ErrInvalidCodeRunRequest) { t.Fatalf("Validate() error = %v, want ErrInvalidCodeRunRequest", err) } for _, marker := range []string{sourceMarker, "hidden-input-marker", "hidden-output-marker"} { if strings.Contains(err.Error(), marker) { t.Fatalf("Validate() error exposes %q: %v", marker, err) } } }) } } func TestCodeRunRequestIsRedactedInEveryRendering(t *testing.T) { request := validCodeRunRequest() renderings := []string{ request.String(), request.GoString(), request.LogValue().String(), fmt.Sprintf("%v", request), fmt.Sprintf("%+v", request), fmt.Sprintf("%#v", request), fmt.Sprint(request), slog.Group("run", slog.Any("request", request)).Value.String(), } for _, rendered := range renderings { for _, marker := range []string{sourceMarker, "hidden-input-marker", "hidden-output-marker", request.Key.AttemptItemID.String()} { if strings.Contains(rendered, marker) { t.Fatalf("rendering %q exposes %q", rendered, marker) } } } } func TestCodeRunnerFailuresShareTheUnavailableUmbrella(t *testing.T) { for name, err := range map[string]error{ "disabled": outbound.ErrCodeRunnerDisabled, "unauthorized": outbound.ErrCodeRunnerUnauthorized, "bad response": outbound.ErrCodeRunnerBadResponse, "infrastructure failure": outbound.ErrCodeRunnerInfrastructureFailure, } { if !errors.Is(err, outbound.ErrCodeRunnerUnavailable) { t.Fatalf("%s is not under ErrCodeRunnerUnavailable", name) } } for name, err := range map[string]error{ "invalid request": outbound.ErrInvalidCodeRunRequest, "not found": outbound.ErrCodeRunNotFound, "key reused": outbound.ErrCodeRunKeyReused, } { if errors.Is(err, outbound.ErrCodeRunnerUnavailable) { t.Fatalf("%s must not read as an unavailable runner", name) } } }