From 53801afd953682ab416883d84c8cb326c8ac631e Mon Sep 17 00:00:00 2001 From: karim Date: Tue, 27 Oct 2026 19:38:25 +0100 Subject: [PATCH] [16] Test: alle Befehle mit feindlichen Eingaben; revisionCloud gegen Speicher-Explosion abgesichert --- src/commands/hostile.test.ts | 93 +++++++++++++++++++++++++++++++++++ src/geometry/revisionCloud.ts | 9 ++-- 2 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 src/commands/hostile.test.ts diff --git a/src/commands/hostile.test.ts b/src/commands/hostile.test.ts new file mode 100644 index 00000000..cd93a110 --- /dev/null +++ b/src/commands/hostile.test.ts @@ -0,0 +1,93 @@ +// [16] „Befehle mit Unsinn füttern": jeder Befehl der Registratur bekommt feindliche Eingaben +// (NaN, ∞, riesige Zahlen, leere/lange Texte, fremde ids). Erwartung: kein Wurf, und was am Ende +// committet wird, darf das Projekt nie ungültig machen — oder wird vom Op-Wächter in setProject +// abgelehnt (core/ops.ts `validateOps`). Der Test prüft beides. +import { describe, expect, it } from "vitest"; +import { COMMANDS } from "./registry"; +import type { CmdInput, CommandContext } from "./types"; +import { sampleProject } from "../model/sampleProject"; +import { applyOps, diffToOps, validateOps } from "../core/ops"; + +const level = sampleProject.drawingLevels.find((l) => l.kind === "floor")!; +const ctx: CommandContext = { + project: sampleProject, + level, + defaultCategoryCode: sampleProject.layers[0]?.code ?? "20", + activeLineStyleId: "", + activeWallTypeId: sampleProject.wallTypes[0]?.id ?? "", + lastPoint: null, + selection: { + wallIds: sampleProject.walls.slice(0, 2).map((w) => w.id), + drawingId: null, + drawingIds: [], + } as unknown as CommandContext["selection"], +}; + +const NUMS = [NaN, Infinity, -Infinity, 0, -1, 1e300, -1e300, 1e-300, 0.5, 3]; +const TEXTS = ["", " ", "x".repeat(10_000), "0,0", "r3<45", "\u0000", "-", "NaN"]; +let seed = 12345; +const rnd = () => ((seed = (seed * 1664525 + 1013904223) % 4294967296) / 4294967296); +const pick = (a: T[]): T => a[Math.floor(rnd() * a.length)]; + +function hostile(optionIds: string[]): CmdInput { + switch (Math.floor(rnd() * 5)) { + case 0: + return { kind: "point", point: { x: pick(NUMS), y: pick(NUMS) } }; + case 1: + return { kind: "number", value: pick(NUMS) }; + case 2: + return { kind: "option", id: pick([...optionIds, "", "gibt-es-nicht", "__proto__"]) }; + case 3: + return { kind: "text", text: pick(TEXTS) }; + default: + return { kind: "selection", wallIds: ["nope", "", ...sampleProject.walls.map((w) => w.id).slice(0, 1)], drawingIds: ["nope"] }; + } +} + +describe("Befehle mit feindlichen Eingaben", () => { + const names = Object.keys(COMMANDS); + it("die Registratur ist nicht leer", () => expect(names.length).toBeGreaterThan(50)); + + for (const name of names) { + it(`${name}: kein Wurf, Projekt bleibt gültig oder wird abgelehnt`, () => { + const cmd = COMMANDS[name]; + const failures: string[] = []; + for (let round = 0; round < 25; round++) { + let state: ReturnType; + try { + state = cmd.init(); + } catch (e) { + failures.push(`init: ${String(e)}`); + break; + } + for (let step = 0; step < 14; step++) { + let opts: string[] = []; + try { + opts = (cmd.options(state) ?? []).map((o) => o.id); + } catch (e) { + failures.push(`options: ${String(e)}`); + } + const kind = Math.floor(rnd() * 6); + try { + let res; + if (kind === 0) [state, res] = cmd.onMove(state, { x: pick(NUMS), y: pick(NUMS) }, null, ctx); + else if (kind === 1) [state, res] = cmd.onConfirm(state, ctx); + else if (kind === 2 && rnd() < 0.2) [state, res] = cmd.onCancel(state); + else [state, res] = cmd.onInput(state, hostile(opts), ctx); + if (res?.commit) { + const next = res.commit(sampleProject); + const ops = diffToOps(sampleProject, next); + const applied = applyOps(sampleProject, ops); + validateOps(applied, ops, sampleProject); // darf melden, aber nicht werfen + } + if (res?.done) break; + } catch (e) { + failures.push(`${kind}: ${e instanceof Error ? e.message : String(e)}`); + break; + } + } + } + expect([...new Set(failures)].slice(0, 5)).toEqual([]); + }); + } +}); diff --git a/src/geometry/revisionCloud.ts b/src/geometry/revisionCloud.ts index 9d1c35ea..78d8a89e 100644 --- a/src/geometry/revisionCloud.ts +++ b/src/geometry/revisionCloud.ts @@ -22,6 +22,8 @@ import type { Vec2 } from "../model/types"; const EPS = 1e-9; +/** Höchstzahl Beulen je Segment (praktische Wolken haben < 100). */ +const MAX_BUMPS_PER_SEGMENT = 2000; /** * `bulge`-Wert EINER Beule — `tan(Δ/4)` für einen Öffnungswinkel Δ ≈ 100° @@ -41,8 +43,9 @@ const lerp = (a: Vec2, b: Vec2, t: number): Vec2 => ({ x: a.x + (b.x - a.x) * t, */ function expandSegment(a: Vec2, b: Vec2, targetArcLen: number): { pts: Vec2[]; bulge: number[] } { const len = dist(a, b); - if (len < EPS) return { pts: [], bulge: [] }; - const n = Math.max(1, Math.round(len / Math.max(targetArcLen, EPS))); + if (!Number.isFinite(len) || len < EPS) return { pts: [], bulge: [] }; + // Obergrenze gegen Unsinn-Eingaben (winzige Bogenlänge, riesige Strecke): sonst Speicher-Explosion. + const n = Math.min(MAX_BUMPS_PER_SEGMENT, Math.max(1, Math.round(len / Math.max(targetArcLen, EPS)))); const pts: Vec2[] = []; const bulge: number[] = []; for (let i = 1; i <= n; i++) { @@ -63,7 +66,7 @@ export function revisionCloudPts( closed: boolean, targetArcLen: number, ): { pts: Vec2[]; bulge: number[] } | null { - if (points.length < 2) return null; + if (points.length < 2 || !Number.isFinite(targetArcLen) || targetArcLen <= 0) return null; const segCount = closed ? points.length : points.length - 1; const outPts: Vec2[] = [points[0]]; const outBulge: number[] = [];