A new admin-only solve page, /p/<name>/solve, edits a problem's code in
C++ or Python (a dropdown switches between them), runs it on custom
input or against the saved tests with per-test verdicts (AC, WA, RE,
TLE, unchecked) and compile errors, marks lint diagnostics in the
editor, and saves the code back as a commit ("Edit <name>"). Drafts
stay in the browser per problem and language until saved. LeetCode
problems are C++ only, since their tests run through driver.h.
Code never runs in the site, which holds the Claude, Gitea and S3
secrets. A separate runner service (cmd/usacorunner, Dockerfile.runner)
does it in a container meant to have no environment, an internal-only
network, a read-only root apart from /tmp, and memory, CPU and pids
limits, with tini to reap orphans. Runs copy the CLI: the same g++
flags with -DLOCAL and the repo's headers, <io>.in on disk and on
stdin, <io>.out or stdout as the output, and usaco test's comparison.
Unlike the CLI they have CPU, memory, file and wall-clock limits.
C++ lint is g++'s warnings plus clang-tidy with a new .clang-tidy
(bugprone, performance and a few readability checks; template.cpp is
clean). Python lint is a syntax check plus ruff. template.py is the
Python starter, with a stdio() helper like template.rs's.
CI builds, smoke-tests and rolls out the runner image before the site.
The web_server stack needs a usaco-runner service before that rollout
can succeed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
274 lines
8.3 KiB
Go
274 lines
8.3 KiB
Go
package runner
|
|
|
|
import (
|
|
"context"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func need(t *testing.T, bins ...string) {
|
|
t.Helper()
|
|
for _, b := range bins {
|
|
if _, err := exec.LookPath(b); err != nil {
|
|
t.Skipf("%s is not installed", b)
|
|
}
|
|
}
|
|
}
|
|
|
|
func str(s string) *string { return &s }
|
|
|
|
func runOK(t *testing.T, req RunRequest) RunResponse {
|
|
t.Helper()
|
|
resp, err := run(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !resp.Compile.OK {
|
|
t.Fatalf("compile failed:\n%s", resp.Compile.Output)
|
|
}
|
|
if len(resp.Results) != len(req.Tests) {
|
|
t.Fatalf("%d results for %d tests", len(resp.Results), len(req.Tests))
|
|
}
|
|
return resp
|
|
}
|
|
|
|
func verdict(t *testing.T, r Result, want string) {
|
|
t.Helper()
|
|
if r.Verdict != want {
|
|
t.Errorf("%s: verdict %s (%s), want %s; output %q, stderr %q", r.Name, r.Verdict, r.Message, want, r.Output, r.Stderr)
|
|
}
|
|
}
|
|
|
|
// A sum over stdin, small headers so the compile is quick.
|
|
const sumCpp = `#include <iostream>
|
|
int main() { long long a, b; std::cin >> a >> b; std::cout << a + b << "\n"; }
|
|
`
|
|
|
|
func TestCppVerdicts(t *testing.T) {
|
|
need(t, "g++")
|
|
t.Parallel()
|
|
resp := runOK(t, RunRequest{Lang: Cpp, Source: sumCpp, IO: "sum", Tests: []Test{
|
|
{Name: "ac", Input: "1 2\n", Output: str("3\n")},
|
|
{Name: "wa", Input: "1 2\n", Output: str("4\n")},
|
|
// norm(): trailing spaces and blank lines, and a missing newline, are ignored
|
|
{Name: "norm", Input: "2 2", Output: str("4 \n\n\n")},
|
|
{Name: "unchecked", Input: "5 5\n"},
|
|
}})
|
|
verdict(t, resp.Results[0], Accepted)
|
|
verdict(t, resp.Results[1], WrongAnswer)
|
|
verdict(t, resp.Results[2], Accepted)
|
|
verdict(t, resp.Results[3], Unchecked)
|
|
if got := resp.Results[3].Output; got != "10\n" {
|
|
t.Errorf("unchecked output %q", got)
|
|
}
|
|
if resp.Results[0].Input != "1 2\n" || *resp.Results[1].Expected != "4\n" {
|
|
t.Errorf("input/expected not echoed: %+v", resp.Results[0])
|
|
}
|
|
}
|
|
|
|
func TestCppFileIO(t *testing.T) {
|
|
need(t, "g++")
|
|
t.Parallel()
|
|
// Writes its answer to <io>.out, as USACO's STDIO does, and something
|
|
// else to stdout, which must then be ignored.
|
|
src := `#include <cstdio>
|
|
int main() {
|
|
std::puts("noise");
|
|
std::freopen("paint.in", "r", stdin);
|
|
std::freopen("paint.out", "w", stdout);
|
|
int a; std::scanf("%d", &a); std::printf("%d\n", a * 2);
|
|
}
|
|
`
|
|
resp := runOK(t, RunRequest{Lang: Cpp, Source: src, IO: "paint", Tests: []Test{
|
|
{Name: "1", Input: "21\n", Output: str("42\n")},
|
|
{Name: "2", Input: "5\n", Output: str("10\n")},
|
|
}})
|
|
verdict(t, resp.Results[0], Accepted)
|
|
verdict(t, resp.Results[1], Accepted)
|
|
}
|
|
|
|
func TestCppCrashes(t *testing.T) {
|
|
need(t, "g++")
|
|
t.Parallel()
|
|
src := `#include <cstdio>
|
|
#include <cstdlib>
|
|
int main() {
|
|
int k; std::scanf("%d", &k);
|
|
if (k == 1) { volatile int *p = nullptr; *p = 1; }
|
|
if (k == 2) { std::fprintf(stderr, "bad\n"); return 3; }
|
|
std::puts("ok");
|
|
}
|
|
`
|
|
resp := runOK(t, RunRequest{Lang: Cpp, Source: src, Tests: []Test{
|
|
{Name: "segv", Input: "1"},
|
|
{Name: "exit", Input: "2"},
|
|
}})
|
|
segv, exit := resp.Results[0], resp.Results[1]
|
|
verdict(t, segv, RuntimeError)
|
|
if !strings.Contains(segv.Message, "signal 11") || segv.ExitCode != 139 {
|
|
t.Errorf("segv: %q, exit %d", segv.Message, segv.ExitCode)
|
|
}
|
|
verdict(t, exit, RuntimeError)
|
|
if exit.Message != "exit status 3" || exit.ExitCode != 3 || exit.Stderr != "bad\n" {
|
|
t.Errorf("exit: %+v", exit)
|
|
}
|
|
}
|
|
|
|
func TestCppTimeLimit(t *testing.T) {
|
|
need(t, "g++")
|
|
t.Parallel()
|
|
src := `int main() { volatile unsigned long x = 0; for (;;) x++; }`
|
|
start := time.Now()
|
|
resp := runOK(t, RunRequest{Lang: Cpp, Source: src, TimeLimitMs: 200, Tests: []Test{{Name: "loop", Output: str("")}}})
|
|
verdict(t, resp.Results[0], TimeLimit)
|
|
if resp.Results[0].Message == "" {
|
|
t.Error("TLE without a message")
|
|
}
|
|
if d := time.Since(start); d > 10*time.Second {
|
|
t.Errorf("took %s", d)
|
|
}
|
|
}
|
|
|
|
func TestCppMeasuredCPUOverLimit(t *testing.T) {
|
|
need(t, "g++")
|
|
t.Parallel()
|
|
// Finishes on its own, after the 100ms limit: TLE by measurement, the
|
|
// rlimit being a second above it.
|
|
src := `#include <ctime>
|
|
int main() { std::clock_t s = std::clock(); while (std::clock() - s < CLOCKS_PER_SEC / 2) {} }`
|
|
resp := runOK(t, RunRequest{Lang: Cpp, Source: src, TimeLimitMs: 100, Tests: []Test{{Name: "slow"}}})
|
|
verdict(t, resp.Results[0], TimeLimit)
|
|
if !strings.Contains(resp.Results[0].Message, "CPU time") {
|
|
t.Errorf("message %q", resp.Results[0].Message)
|
|
}
|
|
}
|
|
|
|
func TestBudget(t *testing.T) {
|
|
need(t, "g++")
|
|
t.Parallel()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1500*time.Millisecond)
|
|
defer cancel()
|
|
src := `int main() { volatile unsigned long x = 0; for (;;) x++; }`
|
|
// Each test may take 2*2s+1s; the budget runs out during the first.
|
|
resp, err := run(ctx, RunRequest{Lang: Cpp, Source: src, Tests: []Test{{Name: "1"}, {Name: "2"}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !resp.Compile.OK {
|
|
t.Skipf("compile did not finish inside the budget:\n%s", resp.Compile.Output)
|
|
}
|
|
verdict(t, resp.Results[0], TimeLimit)
|
|
verdict(t, resp.Results[1], TimeLimit)
|
|
if resp.Results[0].Message != budgetMessage || !strings.HasPrefix(resp.Results[1].Message, "not run") {
|
|
t.Errorf("messages %q, %q", resp.Results[0].Message, resp.Results[1].Message)
|
|
}
|
|
}
|
|
|
|
func TestCppCompileError(t *testing.T) {
|
|
need(t, "g++")
|
|
t.Parallel()
|
|
resp, err := run(context.Background(), RunRequest{Lang: Cpp, Source: "int main() { return x; }", Tests: []Test{{Name: "1"}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.Compile.OK || !strings.Contains(resp.Compile.Output, "main.cpp:1") || len(resp.Results) != 0 {
|
|
t.Errorf("%+v", resp)
|
|
}
|
|
}
|
|
|
|
func TestCppFilesAndTemplate(t *testing.T) {
|
|
need(t, "g++")
|
|
t.Parallel()
|
|
// The LeetCode layout: a class in main.cpp, and a driver.h beside it
|
|
// (from Files) that holds main(), included only in LOCAL builds.
|
|
src := `#include <bits/stdc++.h>
|
|
using namespace std;
|
|
class Solution { public: int twice(int x) { return 2 * x; } };
|
|
#ifdef LOCAL
|
|
#include "driver.h"
|
|
#endif
|
|
`
|
|
driver := `int main() {
|
|
freopen("twice.in", "r", stdin);
|
|
freopen("twice.out", "w", stdout);
|
|
for (int x; cin >> x;) cout << Solution().twice(x) << '\n';
|
|
}
|
|
`
|
|
resp := runOK(t, RunRequest{Lang: Cpp, Source: src, IO: "twice", Files: map[string]string{"driver.h": driver},
|
|
Tests: []Test{{Name: "1", Input: "1\n2\n", Output: str("2\n4\n")}}})
|
|
verdict(t, resp.Results[0], Accepted)
|
|
}
|
|
|
|
func TestOutputCap(t *testing.T) {
|
|
need(t, "g++")
|
|
t.Parallel()
|
|
src := `#include <cstdio>
|
|
int main() { for (int i = 0; i < 200000; i++) std::puts("x"); }`
|
|
resp := runOK(t, RunRequest{Lang: Cpp, Source: src, Tests: []Test{{Name: "big", Output: str("x\n")}}})
|
|
r := resp.Results[0]
|
|
verdict(t, r, WrongAnswer)
|
|
if !r.Truncated || len(r.Output) != MaxOutput {
|
|
t.Errorf("truncated %v, %d bytes", r.Truncated, len(r.Output))
|
|
}
|
|
}
|
|
|
|
func TestPython(t *testing.T) {
|
|
need(t, "python3")
|
|
t.Parallel()
|
|
resp := runOK(t, RunRequest{Lang: Python, Source: "a, b = map(int, input().split())\nprint(a + b)\n", Tests: []Test{
|
|
{Name: "ac", Input: "1 2\n", Output: str("3")},
|
|
{Name: "wa", Input: "1 2\n", Output: str("4")},
|
|
{Name: "re", Input: "x\n", Output: str("4")},
|
|
}})
|
|
verdict(t, resp.Results[0], Accepted)
|
|
verdict(t, resp.Results[1], WrongAnswer)
|
|
verdict(t, resp.Results[2], RuntimeError)
|
|
if !strings.Contains(resp.Results[2].Stderr, "ValueError") {
|
|
t.Errorf("stderr %q", resp.Results[2].Stderr)
|
|
}
|
|
}
|
|
|
|
func TestPythonSyntaxError(t *testing.T) {
|
|
need(t, "python3")
|
|
t.Parallel()
|
|
resp, err := run(context.Background(), RunRequest{Lang: Python, Source: "def f(:\n pass\n", Tests: []Test{{Name: "1"}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp.Compile.OK || !strings.Contains(resp.Compile.Output, "SyntaxError") {
|
|
t.Errorf("%+v", resp.Compile)
|
|
}
|
|
}
|
|
|
|
func TestNormalize(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"a \nb\t\n\n\n": "a\nb",
|
|
"a\r\nb": "a\nb",
|
|
"\n\na": "\n\na",
|
|
"a\n\nb\n": "a\n\nb",
|
|
"": "",
|
|
" \n \n": "",
|
|
} {
|
|
if got := normalize(in); got != want {
|
|
t.Errorf("normalize(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCapText(t *testing.T) {
|
|
s, cut := capText("aé", 2) // é is two bytes: cutting at 2 would split it
|
|
if s != "a" || !cut {
|
|
t.Errorf("%q %v", s, cut)
|
|
}
|
|
}
|
|
|
|
func TestLimitsScript(t *testing.T) {
|
|
got := testLimits(1500 * time.Millisecond).script()
|
|
want := `ulimit -t 3 && ulimit -v 524288 && ulimit -f 131072 || exit 126; ulimit -s 524288 2>/dev/null; exec "$@"`
|
|
if got != want {
|
|
t.Errorf("got %s\nwant %s", got, want)
|
|
}
|
|
}
|