[16] Test: alle Befehle mit feindlichen Eingaben; revisionCloud gegen Speicher-Explosion abgesichert
This commit is contained in:
1 parent
b069599a25
commit
53801afd95
2 files changed
+99
-3
No files matched your search
@@ -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 = <T,>(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<typeof cmd.init>;
|
||||
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([]);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -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[] = [];
|
||||
|
||||
Reference in new issue
Block a user