213 lines
7.2 KiB
Go
213 lines
7.2 KiB
Go
package judge
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"os"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"lincodex/runner/internal/checker"
|
|
"lincodex/runner/internal/sandbox"
|
|
)
|
|
|
|
// scriptedExecutor answers runs from a script keyed by the stdin of a test;
|
|
// the compile step has no stdin.
|
|
type scriptedExecutor struct {
|
|
root string
|
|
results map[string]sandbox.Result
|
|
errs map[string]error
|
|
runs []string
|
|
boxes []string
|
|
}
|
|
|
|
func (e *scriptedExecutor) NewBox(fileName string, content []byte) (*sandbox.Box, error) {
|
|
box, err := sandbox.CreateBox(e.root, fileName, content)
|
|
if err == nil {
|
|
e.boxes = append(e.boxes, box.Dir())
|
|
}
|
|
return box, err
|
|
}
|
|
|
|
func (e *scriptedExecutor) Run(_ context.Context, spec sandbox.Spec, _ sandbox.Limits) (sandbox.Result, error) {
|
|
key := string(spec.Stdin)
|
|
e.runs = append(e.runs, key)
|
|
if err := e.errs[key]; err != nil {
|
|
return sandbox.Result{}, err
|
|
}
|
|
if result, ok := e.results[key]; ok {
|
|
return result, nil
|
|
}
|
|
return sandbox.Result{}, nil
|
|
}
|
|
|
|
func newScripted(t *testing.T) *scriptedExecutor {
|
|
t.Helper()
|
|
return &scriptedExecutor{root: t.TempDir(), results: map[string]sandbox.Result{}, errs: map[string]error{}}
|
|
}
|
|
|
|
func output(text string) sandbox.Result {
|
|
return sandbox.Result{Stdout: []byte(text), CPUTime: 20 * time.Millisecond, WallTime: 30 * time.Millisecond, PeakMemoryKB: 9000}
|
|
}
|
|
|
|
func discardLogger() *slog.Logger {
|
|
return slog.New(slog.DiscardHandler)
|
|
}
|
|
|
|
func twoGroupRequest() Request {
|
|
return Request{
|
|
Language: "python3",
|
|
Source: "print(sum(map(int, input().split())))",
|
|
Limits: Limits{TimeLimit: time.Second, MemoryBytes: 64 << 20, OutputBytes: 64 << 10},
|
|
Groups: []Group{
|
|
{ID: "public", Visibility: Public, Checker: checker.Spec{Kind: checker.Exact}, Tests: []Test{
|
|
{ID: "t1", Input: "1 2", ExpectedOutput: "3\n"},
|
|
{ID: "t2", Input: "2 2", ExpectedOutput: "4\n"},
|
|
}},
|
|
{ID: "hidden-1", Visibility: Hidden, Checker: checker.Spec{Kind: checker.Tokens}, Tests: []Test{
|
|
{ID: "t1", Input: "5 5", ExpectedOutput: "10"},
|
|
{ID: "t2", Input: "6 6", ExpectedOutput: "12"},
|
|
}},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestRunAcceptsCorrectOutputAndRecordsUsage(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
executor := newScripted(t)
|
|
executor.results["1 2"] = output("3\n")
|
|
executor.results["2 2"] = output("4")
|
|
executor.results["5 5"] = output("10\n")
|
|
executor.results["6 6"] = output(" 12 ")
|
|
result := New(executor, discardLogger(), Python3("/usr/local/bin/python3")).Run(t.Context(), twoGroupRequest())
|
|
|
|
if result.Outcome != Accepted || result.Compile == nil || result.Compile.Outcome != Accepted {
|
|
t.Fatalf("result = %+v, want accepted with a passed compile step", result)
|
|
}
|
|
for _, group := range result.Groups {
|
|
if group.Outcome != Accepted || group.Passed != 2 || group.Total != 2 {
|
|
t.Fatalf("group %+v, want 2 of 2 accepted", group)
|
|
}
|
|
if group.MaxCPUTime != 20*time.Millisecond || group.MaxMemoryKB != 9000 {
|
|
t.Fatalf("group usage %v %d, want the recorded usage", group.MaxCPUTime, group.MaxMemoryKB)
|
|
}
|
|
}
|
|
assertBoxesRemoved(t, executor)
|
|
}
|
|
|
|
func TestRunStopsAGroupAtItsFirstFailureAndKeepsDiagnosticsPublicOnly(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
executor := newScripted(t)
|
|
crash := sandbox.Result{ExitCode: 1, Stderr: []byte("Traceback: ValueError")}
|
|
executor.results["1 2"] = crash
|
|
executor.results["5 5"] = crash
|
|
result := New(executor, discardLogger(), Python3("/usr/local/bin/python3")).Run(t.Context(), twoGroupRequest())
|
|
|
|
if result.Outcome != RuntimeError {
|
|
t.Fatalf("outcome = %s, want runtime_error", result.Outcome)
|
|
}
|
|
public, hidden := result.Groups[0], result.Groups[1]
|
|
if len(public.Tests) != 1 || public.Passed != 0 || public.Tests[0].Diagnostic != "Traceback: ValueError" {
|
|
t.Fatalf("public group %+v, want one failed test with its diagnostic", public)
|
|
}
|
|
if len(hidden.Tests) != 1 || hidden.Tests[0].Diagnostic != "" {
|
|
t.Fatalf("hidden group %+v, want one failed test without a diagnostic", hidden)
|
|
}
|
|
if slices.Contains(executor.runs, "2 2") || slices.Contains(executor.runs, "6 6") {
|
|
t.Fatalf("runs %q include tests after a failure", executor.runs)
|
|
}
|
|
}
|
|
|
|
func TestRunMapsSandboxTerminationsToOutcomes(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
result sandbox.Result
|
|
outcome Outcome
|
|
}{
|
|
{"cpu time", sandbox.Result{Termination: sandbox.TerminationCPUTime, Signal: 9}, TimeLimitExceeded},
|
|
{"wall time", sandbox.Result{Termination: sandbox.TerminationWallTime, Signal: 9}, TimeLimitExceeded},
|
|
{"memory", sandbox.Result{Termination: sandbox.TerminationMemory, Signal: 9}, MemoryLimitExceeded},
|
|
{"output", sandbox.Result{Termination: sandbox.TerminationOutput, Signal: 9, Stdout: []byte("3\n")}, OutputLimitExceeded},
|
|
{"processes", sandbox.Result{Termination: sandbox.TerminationProcesses, ExitCode: 1}, RuntimeError},
|
|
{"signal", sandbox.Result{Signal: 31, ExitCode: -1}, RuntimeError},
|
|
{"wrong answer", output("4\n"), WrongAnswer},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
executor := newScripted(t)
|
|
executor.results["1 2"] = tc.result
|
|
result := New(executor, discardLogger(), Python3("/usr/local/bin/python3")).Run(t.Context(), twoGroupRequest())
|
|
got := result.Groups[0].Tests[0]
|
|
if got.Outcome != tc.outcome || got.Limit != tc.result.Termination {
|
|
t.Fatalf("test result %+v, want %s with limit %q", got, tc.outcome, tc.result.Termination)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRunReportsCompileErrorsWithoutRunningTests(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
executor := newScripted(t)
|
|
executor.results[""] = sandbox.Result{ExitCode: 1, Stderr: []byte("SyntaxError: invalid syntax\n")}
|
|
result := New(executor, discardLogger(), Python3("/usr/local/bin/python3")).Run(t.Context(), twoGroupRequest())
|
|
|
|
if result.Outcome != CompileError || result.Compile.Diagnostic != "SyntaxError: invalid syntax\n" {
|
|
t.Fatalf("result = %+v, want compile_error with the diagnostic", result)
|
|
}
|
|
if len(executor.runs) != 1 {
|
|
t.Fatalf("runs = %q, want only the compile step", executor.runs)
|
|
}
|
|
for _, group := range result.Groups {
|
|
if group.Outcome != CompileError || len(group.Tests) != 0 || group.Total != 2 {
|
|
t.Fatalf("group %+v, want compile_error without tests", group)
|
|
}
|
|
}
|
|
assertBoxesRemoved(t, executor)
|
|
}
|
|
|
|
func TestRunTurnsSandboxFailuresIntoInfrastructureFailure(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
executor := newScripted(t)
|
|
executor.results["1 2"] = output("3\n")
|
|
executor.errs["2 2"] = errors.New("nsjail failed")
|
|
result := New(executor, discardLogger(), Python3("/usr/local/bin/python3")).Run(t.Context(), twoGroupRequest())
|
|
|
|
if result.Outcome != InfrastructureFailure {
|
|
t.Fatalf("outcome = %s, want infrastructure_failure", result.Outcome)
|
|
}
|
|
for _, group := range result.Groups {
|
|
if group.Outcome != InfrastructureFailure || len(group.Tests) != 0 {
|
|
t.Fatalf("group %+v, want infrastructure_failure without partial tests", group)
|
|
}
|
|
}
|
|
assertBoxesRemoved(t, executor)
|
|
}
|
|
|
|
func TestExcerptKeepsTheTailAsValidUTF8(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
text := append(bytes.Repeat([]byte("ж"), 10), 0xff, 'x')
|
|
if got := excerpt(text, 7); got != "ж\uFFFDx" {
|
|
t.Fatalf("excerpt = %q, want the last whole runes within 7 bytes", got)
|
|
}
|
|
}
|
|
|
|
func assertBoxesRemoved(t *testing.T, executor *scriptedExecutor) {
|
|
t.Helper()
|
|
for _, dir := range executor.boxes {
|
|
if _, err := os.Stat(dir); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("box %s still exists: %v", dir, err)
|
|
}
|
|
}
|
|
}
|