Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Tests: io's byte mutator moves to a testkit crate, a dev-dependency only (#5184) (#7682)

CamWight303236e52475
diff --git a/Cargo.lock b/Cargo.lock
index 2d6fe07de1..9a63ec35de 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4355,6 +4355,7 @@ dependencies = [
"solidcraft-doc",
"solidcraft-geom",
"solidcraft-kernel",
+ "solidcraft-testkit",
"thiserror 2.0.21",
"zip",
]
@@ -4424,6 +4425,10 @@ dependencies = [
"serde",
]
+[[package]]
+name = "solidcraft-testkit"
+version = "0.29.0"
+
[[package]]
name = "solidcraft-ui-egui"
version = "0.29.0"
diff --git a/Cargo.toml b/Cargo.toml
index 13a3f37788..a37b2b65e1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -28,6 +28,8 @@ solidcraft-space-mouse = { path = "crates/space-mouse" }
solidcraft-mcp = { path = "crates/mcp" }
solidcraft-derive = { path = "crates/derive" }
solidcraft-3dx-client = { path = "crates/threedx-client" }
+# test tooling: a dev-dependency only (xtask/src/layers.rs)
+solidcraft-testkit = { path = "crates/testkit" }
# foundation
thiserror = "2"
diff --git a/crates/io/Cargo.toml b/crates/io/Cargo.toml
index e5f7c4dee8..8b75ac98fa 100644
--- a/crates/io/Cargo.toml
+++ b/crates/io/Cargo.toml
@@ -43,3 +43,6 @@ rustix = { version = "1", default-features = false, features = ["std", "fs"] }
# The computer's name for a design's lock file (src/design_lock.rs; the browser keeps no lock files).
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
gethostname = { workspace = true }
+
+[dev-dependencies]
+solidcraft-testkit = { workspace = true }
diff --git a/crates/io/src/mutate.rs b/crates/io/src/mutate.rs
index 10e27a12b6..c5dcd68b1e 100644
--- a/crates/io/src/mutate.rs
+++ b/crates/io/src/mutate.rs
@@ -1,323 +1,40 @@
-//! Deterministic mutations of valid files, and the readers run over them (#469). Every reader of
-//! bytes the user didn't write gets the same treatment: truncation, flipped and overwritten
-//! bytes, dropped and repeated runs, appended garbage and, for text formats, dropped and
-//! repeated lines and numbers that lie (`nan`, `inf`, `1e400`, …). Each mutated file must give an
-//! error or a result, never a panic, within a time budget and the reader's own caps. A panic a
-//! reader catches itself (`solidcraft_kernel::guard`) counts too: the browser build aborts on any
-//! panic, so a caught one on the desktop is a crash there.
-//!
-//! The mutations come from a fixed seed (xorshift), so a failure names a case that reproduces.
-//! No fuzzing dependency, nothing nightly, nothing native: it runs in the normal test suite and
-//! builds for wasm. The readers' own hand-written cases stay; they cover particular lies (a
-//! length in a container header) that random changes rarely hit. STEP, IGES and FreeCAD/Rhino
-//! files are read by the kernel, which is moving to solidcore (#1597), and keep their own tests.
+//! Every reader of bytes the user didn't write, run over mutations of a valid file (#469): each
+//! case gives an error or a result, never a panic, within the time budget and the reader's own
+//! caps. The mutations and the check are `solidcraft_testkit`'s.
-use std::panic::{AssertUnwindSafe, catch_unwind};
-use std::time::{Duration, Instant};
+use solidcraft_geom::{Mesh, Vec2, Vec3};
+use solidcraft_testkit::{mutations, survives};
-/// A deterministic xorshift64 stream.
-pub(crate) struct Rng(u64);
-
-impl Rng {
- pub(crate) fn new(seed: u64) -> Rng {
- Rng(seed | 1)
- }
-
- pub(crate) fn next(&mut self) -> u64 {
- self.0 ^= self.0 << 13;
- self.0 ^= self.0 >> 7;
- self.0 ^= self.0 << 17;
- self.0
- }
-
- /// A value in `0..n` (0 when `n` is 0).
- pub(crate) fn below(&mut self, n: usize) -> usize {
- if n == 0 { 0 } else { (self.next() % n as u64) as usize }
- }
+fn cube() -> Mesh {
+ let positions = (0..8).map(|i| Vec3::new(f64::from(i & 1) * 10.0, f64::from((i >> 1) & 1) * 10.0, f64::from((i >> 2) & 1) * 10.0)).collect();
+ let t = [[0, 2, 1], [1, 2, 3], [4, 5, 6], [5, 7, 6], [0, 1, 4], [1, 5, 4], [2, 6, 3], [3, 6, 7], [0, 4, 2], [2, 4, 6], [1, 3, 5], [3, 7, 5]];
+ Mesh { positions, triangles: t.to_vec(), ..Default::default() }
}
-/// Numbers a hostile file might hold in place of a real one.
-const LIARS: [&str; 10] = ["nan", "inf", "-inf", "1e400", "-1e400", "-0", "4294967296", "-1", "99999999999999999999", "0x7fffffff"];
-
-/// Most truncations of one file (every length up to this, evenly spaced beyond).
-const MAX_TRUNCATIONS: usize = 1024;
-
-/// Mutations of `good`, the same for the same `good` and `seed`. `text`: also line and number
-/// mutations (the file is UTF-8 text).
-pub(crate) fn mutations(good: &[u8], text: bool, seed: u64) -> Vec<Vec<u8>> {
- let mut out: Vec<Vec<u8>> = Vec::new();
- let n = good.len();
- let mut rng = Rng::new(seed);
- // Truncations: every length of a short file; of a long one, its first and last 64 lengths
- // (headers, final records) and evenly spaced ones between.
- if n <= MAX_TRUNCATIONS {
- out.extend((0..n).map(|k| good[..k].to_vec()));
- } else {
- let mut cuts: Vec<usize> = (0..64).chain(n - 64..n).collect();
- cuts.extend((0..MAX_TRUNCATIONS - 128).map(|i| 64 + i * (n - 128) / (MAX_TRUNCATIONS - 128)));
- cuts.sort_unstable();
- cuts.dedup();
- out.extend(cuts.into_iter().map(|k| good[..k].to_vec()));
- }
- if n == 0 {
- return out;
- }
- // Bytes flipped, zeroed and saturated at seeded places.
- for i in 0..384 {
- let at = rng.below(n);
- let mut b = good.to_vec();
- b[at] = match i % 3 {
- 0 => b[at] ^ ((rng.next() >> 32) as u8 | 1),
- 1 => 0x00,
- _ => 0xff,
- };
- out.push(b);
- }
- // Runs of bytes dropped, repeated and swapped with their neighbour.
- for _ in 0..96 {
- let at = rng.below(n);
- let len = 1 + rng.below(64.min(n - at));
- let end = at + len;
- let mut dropped = good[..at].to_vec();
- dropped.extend_from_slice(&good[end..]);
- out.push(dropped);
- let mut repeated = good[..end].to_vec();
- repeated.extend_from_slice(&good[at..]);
- out.push(repeated);
- if end + len <= n {
- let mut swapped = good[..at].to_vec();
- swapped.extend_from_slice(&good[end..end + len]);
- swapped.extend_from_slice(&good[at..end]);
- swapped.extend_from_slice(&good[end + len..]);
- out.push(swapped);
- }
- }
- // The whole file: with garbage after it, twice over, reversed.
- let mut tail = good.to_vec();
- tail.extend((0..4096).map(|_| (rng.next() >> 40) as u8));
- out.push(tail);
- out.push([good, good].concat());
- out.push(good.iter().rev().copied().collect());
- if text && let Ok(s) = std::str::from_utf8(good) {
- out.extend(text_mutations(s, &mut rng));
- }
- out
+#[test]
+fn stl_survives_mutation() {
+ let mesh = cube();
+ let binary = crate::stl_binary(std::slice::from_ref(&mesh), "fuzz");
+ let ascii = crate::stl_ascii(std::slice::from_ref(&mesh), "fuzz").into_bytes();
+ assert_eq!(crate::read_stl(&binary).map(|t| t.len()).ok(), Some(12));
+ assert_eq!(crate::read_stl(&ascii).map(|t| t.len()).ok(), Some(12));
+ let read = |b: &[u8]| crate::read_stl(b).ok().map(|t| t.len());
+ survives("binary STL", &mutations(&binary, false, 0x5717), crate::MAX_STL_TRIANGLES, read);
+ survives("ASCII STL", &mutations(&ascii, true, 0x5718), crate::MAX_STL_TRIANGLES, read);
}
-/// A text file's lines dropped, repeated and swapped, and its numbers replaced by lies.
-fn text_mutations(s: &str, rng: &mut Rng) -> Vec<Vec<u8>> {
- let mut out = Vec::new();
- let lines: Vec<&str> = s.split_inclusive('\n').collect();
- let join = |v: &[&str]| v.concat().into_bytes();
- let picked: Vec<usize> = if lines.len() <= 200 { (0..lines.len()).collect() } else { (0..200).map(|_| rng.below(lines.len())).collect() };
- for &i in &picked {
- let mut v = lines.clone();
- v.remove(i);
- out.push(join(&v));
- let mut v = lines.clone();
- v.insert(i, lines[i]);
- out.push(join(&v));
- if i + 1 < lines.len() {
- let mut v = lines.clone();
- v.swap(i, i + 1);
- out.push(join(&v));
- }
- }
- // A line in place of another (a block that inserts itself, a count where a coordinate was).
- for _ in 0..200.min(lines.len() * lines.len()) {
- let (i, j) = (rng.below(lines.len()), rng.below(lines.len()));
- let mut v = lines.clone();
- v[i] = lines[j];
- out.push(join(&v));
- }
- // A word in place of another of the file's own words: a renamed block, a swapped keyword, a
- // block that now inserts itself. Every pair in a small file, a sample in a large one.
- let words = word_spans(s);
- let mut vocab: Vec<&str> = words.iter().map(|&(a, b)| &s[a..b]).collect();
- vocab.sort_unstable();
- vocab.dedup();
- let pairs = words.len().saturating_mul(vocab.len());
- let swap = |(a, b): (usize, usize), w: &str| format!("{}{w}{}", &s[..a], &s[b..]).into_bytes();
- if pairs <= 3000 {
- for &span in &words {
- for &w in vocab.iter().filter(|w| **w != &s[span.0..span.1]) {
- out.push(swap(span, w));
- }
- }
- } else {
- for _ in 0..400 {
- out.push(swap(words[rng.below(words.len())], vocab[rng.below(vocab.len())]));
- }
- }
- // Numbers: each of up to 200 of them, in turn, becomes one of the liars.
- let numbers = number_spans(s);
- let chosen: Vec<(usize, usize)> = if numbers.len() <= 200 { numbers } else { (0..200).map(|_| numbers[rng.below(numbers.len())]).collect() };
- for (k, (a, b)) in chosen.into_iter().enumerate() {
- let liar = LIARS[k % LIARS.len()];
- out.push(format!("{}{liar}{}", &s[..a], &s[b..]).into_bytes());
- }
- // Every number lying at once (two counts that are only harmless one at a time).
- let all = number_spans(s);
- for liar in LIARS {
- let mut t = String::with_capacity(s.len());
- let mut at = 0;
- for &(a, b) in &all {
- t.push_str(&s[at..a]);
- t.push_str(liar);
- at = b;
- }
- t.push_str(&s[at..]);
- out.push(t.into_bytes());
- }
- out
-}
-
-/// Byte spans of the words in `s` (runs of ASCII letters, digits and `_` that start with a
-/// letter).
-fn word_spans(s: &str) -> Vec<(usize, usize)> {
- let b = s.as_bytes();
- let mut out = Vec::new();
- let mut i = 0;
- while i < b.len() {
- if !b[i].is_ascii_alphabetic() || (i > 0 && (b[i - 1].is_ascii_alphanumeric() || b[i - 1] == b'_')) {
- i += 1;
- continue;
- }
- let a = i;
- while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_') {
- i += 1;
- }
- out.push((a, i));
- }
- out
-}
-
-/// Byte spans of the decimal numbers in `s` (a sign, digits, a point, an exponent).
-fn number_spans(s: &str) -> Vec<(usize, usize)> {
- let b = s.as_bytes();
- let mut out = Vec::new();
- let mut i = 0;
- while i < b.len() {
- let starts = b[i].is_ascii_digit() || ((b[i] == b'-' || b[i] == b'.') && b.get(i + 1).is_some_and(u8::is_ascii_digit));
- let after_word = i > 0 && (b[i - 1].is_ascii_alphanumeric() || b[i - 1] == b'_');
- if !starts || after_word {
- i += 1;
- continue;
- }
- let a = i;
- i += 1;
- while i < b.len() && (b[i].is_ascii_digit() || b[i] == b'.' || ((b[i] == b'e' || b[i] == b'E') && i + 1 < b.len())) {
- if (b[i] == b'e' || b[i] == b'E') && (b[i + 1] == b'-' || b[i + 1] == b'+') {
- i += 1;
- }
- i += 1;
- }
- out.push((a, i));
- }
- out
-}
-
-thread_local! {
- /// Panics on this thread so far, caught or not ([`count_panics`]).
- static PANICS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
-}
-
-/// Count every panic per thread (tests run on several), then run the hook that was there.
-fn count_panics() {
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| {
- let previous = std::panic::take_hook();
- std::panic::set_hook(Box::new(move |info| {
- PANICS.with(|p| p.set(p.get() + 1));
- previous(info);
- }));
+#[test]
+fn obj_survives_mutation() {
+ let good = crate::obj(&[("Cube".into(), cube())]).into_bytes();
+ assert!(crate::read_obj(&good, "fuzz").is_ok());
+ survives("OBJ", &mutations(&good, true, 0x0b1), usize::MAX, |b| {
+ crate::read_obj(b, "fuzz").ok().map(|(objects, _)| objects.iter().map(|o| o.triangles.len()).sum())
});
}
-/// The time any one reader call may take on a mutated file (a debug build on a loaded machine:
-/// generous, but far below what unbounded work on a lying count would take).
-pub(crate) const BUDGET: Duration = Duration::from_secs(2);
-
-/// Run `read` over `inputs`: no panic, within [`BUDGET`], and `read`'s count (triangles,
-/// entities, …) at most `cap`. `read` returns `None` for an error. Panics (the test fails) naming
-/// the first case that breaks a rule.
-pub(crate) fn survives(what: &str, inputs: &[Vec<u8>], cap: usize, mut read: impl FnMut(&[u8]) -> Option<usize>) {
- count_panics();
- let mut slowest = (Duration::ZERO, 0);
- let mut read_ok = 0;
- for (i, input) in inputs.iter().enumerate() {
- let before = PANICS.with(std::cell::Cell::get);
- let t = Instant::now();
- let r = catch_unwind(AssertUnwindSafe(|| read(input)));
- let took = t.elapsed();
- let head = String::from_utf8_lossy(&input[..input.len().min(120)]).into_owned();
- let Ok(r) = r else { panic!("{what}: case {i} ({} bytes) panicked; it starts {head:?}", input.len()) };
- let caught = PANICS.with(std::cell::Cell::get) - before;
- assert!(
- caught == 0,
- "{what}: case {i} ({} bytes) panicked inside the reader (caught there, but the browser aborts); it starts {head:?}",
- input.len()
- );
- assert!(took <= BUDGET, "{what}: case {i} ({} bytes) took {took:?} (budget {BUDGET:?}); it starts {head:?}", input.len());
- if let Some(count) = r {
- assert!(count <= cap, "{what}: case {i} gave {count} (cap {cap})");
- read_ok += 1;
- }
- slowest = slowest.max((took, i));
- }
- eprintln!("{what}: {} mutations, {read_ok} read, slowest case {} in {:?}", inputs.len(), slowest.1, slowest.0);
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use solidcraft_geom::{Mesh, Vec2, Vec3};
-
- fn cube() -> Mesh {
- let positions = (0..8).map(|i| Vec3::new(f64::from(i & 1) * 10.0, f64::from((i >> 1) & 1) * 10.0, f64::from((i >> 2) & 1) * 10.0)).collect();
- let t = [[0, 2, 1], [1, 2, 3], [4, 5, 6], [5, 7, 6], [0, 1, 4], [1, 5, 4], [2, 6, 3], [3, 6, 7], [0, 4, 2], [2, 4, 6], [1, 3, 5], [3, 7, 5]];
- Mesh { positions, triangles: t.to_vec(), ..Default::default() }
- }
-
- #[test]
- fn mutations_are_deterministic_and_cover_the_file() {
- let good = b"solid x\n facet normal 0 0 1\n vertex 1.5 -2 3e2\n endfacet\nendsolid x\n";
- let a = mutations(good, true, 7);
- assert_eq!(a, mutations(good, true, 7), "the same seed gives the same cases");
- assert_ne!(a, mutations(good, true, 8));
- assert!(a.iter().any(Vec::is_empty) && a.iter().any(|m| m.len() == good.len() - 1), "truncations down to nothing");
- assert!(a.iter().any(|m| m.windows(3).any(|w| w == b"nan")), "a number replaced by a liar");
- assert_eq!(number_spans("v 1.5 -2 3e2 x1 .5e-3"), vec![(2, 5), (6, 8), (9, 12), (16, 21)]);
- // A long file: bounded truncations, kept to the first and last bytes and spaced between.
- let long = vec![b'a'; 100_000];
- let m = mutations(&long, false, 1);
- assert!(m.len() < 3000, "{} cases", m.len());
- }
-
- #[test]
- fn stl_survives_mutation() {
- let mesh = cube();
- let binary = crate::stl_binary(std::slice::from_ref(&mesh), "fuzz");
- let ascii = crate::stl_ascii(std::slice::from_ref(&mesh), "fuzz").into_bytes();
- assert_eq!(crate::read_stl(&binary).map(|t| t.len()).ok(), Some(12));
- assert_eq!(crate::read_stl(&ascii).map(|t| t.len()).ok(), Some(12));
- let read = |b: &[u8]| crate::read_stl(b).ok().map(|t| t.len());
- survives("binary STL", &mutations(&binary, false, 0x5717), crate::MAX_STL_TRIANGLES, read);
- survives("ASCII STL", &mutations(&ascii, true, 0x5718), crate::MAX_STL_TRIANGLES, read);
- }
-
- #[test]
- fn obj_survives_mutation() {
- let good = crate::obj(&[("Cube".into(), cube())]).into_bytes();
- assert!(crate::read_obj(&good, "fuzz").is_ok());
- survives("OBJ", &mutations(&good, true, 0x0b1), usize::MAX, |b| {
- crate::read_obj(b, "fuzz").ok().map(|(objects, _)| objects.iter().map(|o| o.triangles.len()).sum())
- });
- }
-
- #[test]
- fn svg_survives_mutation() {
- let good = br##"<svg xmlns="http://www.w3.org/2000/svg" width="100mm" height="50mm" viewBox="0 0 100 50">
+#[test]
+fn svg_survives_mutation() {
+ let good = br##"<svg xmlns="http://www.w3.org/2000/svg" width="100mm" height="50mm" viewBox="0 0 100 50">
<g transform="translate(10 5) rotate(15) scale(1.5)">
<rect x="1" y="2" width="30" height="4" rx="1"/>
<circle cx="20" cy="20" r="5"/>
@@ -328,266 +45,265 @@ mod tests {
<use href="#a"/><g id="a"><line x1="0" y1="0" x2="5" y2="5"/></g>
</svg>
"##;
- assert!(crate::read_svg(std::str::from_utf8(good).unwrap()).is_ok());
- survives("SVG", &mutations(good, true, 0x5f6), usize::MAX, |b| crate::read_svg(&String::from_utf8_lossy(b)).ok().map(|g| g.len()));
- }
+ assert!(crate::read_svg(std::str::from_utf8(good).unwrap()).is_ok());
+ survives("SVG", &mutations(good, true, 0x5f6), usize::MAX, |b| crate::read_svg(&String::from_utf8_lossy(b)).ok().map(|g| g.len()));
+}
- #[test]
- fn dxf_survives_mutation() {
- let geom = [
- crate::Geom2::Line(Vec2::new(0.0, 0.0), Vec2::new(10.0, 0.0)),
- crate::Geom2::Circle(Vec2::new(5.0, 5.0), 2.5),
- crate::Geom2::Arc { c: Vec2::new(0.0, 0.0), a: Vec2::new(3.0, 0.0), b: Vec2::new(0.0, 3.0) },
- crate::Geom2::Point(Vec2::new(1.0, 1.0)),
- ];
- let written = crate::write_dxf(&geom).into_bytes();
- assert!(crate::read_dxf(std::str::from_utf8(&written).unwrap()).is_ok_and(|g| g.len() >= 3));
- survives("DXF (written)", &mutations(&written, true, 0xdf), usize::MAX, |b| {
- crate::read_dxf(&String::from_utf8_lossy(b)).ok().map(|g| g.len())
- });
- // Blocks inside blocks, an array insert and a bulged polyline: what the block recursion,
- // the array cells and the work budget guard.
- let pairs: &[(&str, &str)] = &[
- ("0", "SECTION"),
- ("2", "BLOCKS"),
- ("0", "BLOCK"),
- ("2", "inner"),
- ("10", "0"),
- ("20", "0"),
- ("0", "LINE"),
- ("10", "0"),
- ("20", "0"),
- ("11", "1"),
- ("21", "1"),
- ("0", "ENDBLK"),
- ("0", "BLOCK"),
- ("2", "outer"),
- ("10", "0"),
- ("20", "0"),
- ("0", "INSERT"),
- ("2", "inner"),
- ("10", "2"),
- ("20", "2"),
- ("0", "CIRCLE"),
- ("10", "0"),
- ("20", "0"),
- ("40", "1"),
- ("0", "ENDBLK"),
- ("0", "ENDSEC"),
- ("0", "SECTION"),
- ("2", "ENTITIES"),
- ("0", "INSERT"),
- ("2", "outer"),
- ("10", "5"),
- ("20", "5"),
- ("41", "2"),
- ("42", "2"),
- ("50", "30"),
- ("70", "3"),
- ("71", "2"),
- ("44", "10"),
- ("45", "10"),
- ("0", "LWPOLYLINE"),
- ("90", "3"),
- ("70", "1"),
- ("10", "0"),
- ("20", "0"),
- ("42", "0.5"),
- ("10", "5"),
- ("20", "0"),
- ("10", "5"),
- ("20", "5"),
- ("0", "ENDSEC"),
- ("0", "EOF"),
- ];
- let blocks: String = pairs
- .iter()
- .map(|(c, v)| {
- format!(
- "{c}
+#[test]
+fn dxf_survives_mutation() {
+ let geom = [
+ crate::Geom2::Line(Vec2::new(0.0, 0.0), Vec2::new(10.0, 0.0)),
+ crate::Geom2::Circle(Vec2::new(5.0, 5.0), 2.5),
+ crate::Geom2::Arc { c: Vec2::new(0.0, 0.0), a: Vec2::new(3.0, 0.0), b: Vec2::new(0.0, 3.0) },
+ crate::Geom2::Point(Vec2::new(1.0, 1.0)),
+ ];
+ let written = crate::write_dxf(&geom).into_bytes();
+ assert!(crate::read_dxf(std::str::from_utf8(&written).unwrap()).is_ok_and(|g| g.len() >= 3));
+ survives("DXF (written)", &mutations(&written, true, 0xdf), usize::MAX, |b| crate::read_dxf(&String::from_utf8_lossy(b)).ok().map(|g| g.len()));
+ // Blocks inside blocks, an array insert and a bulged polyline: what the block recursion,
+ // the array cells and the work budget guard.
+ let pairs: &[(&str, &str)] = &[
+ ("0", "SECTION"),
+ ("2", "BLOCKS"),
+ ("0", "BLOCK"),
+ ("2", "inner"),
+ ("10", "0"),
+ ("20", "0"),
+ ("0", "LINE"),
+ ("10", "0"),
+ ("20", "0"),
+ ("11", "1"),
+ ("21", "1"),
+ ("0", "ENDBLK"),
+ ("0", "BLOCK"),
+ ("2", "outer"),
+ ("10", "0"),
+ ("20", "0"),
+ ("0", "INSERT"),
+ ("2", "inner"),
+ ("10", "2"),
+ ("20", "2"),
+ ("0", "CIRCLE"),
+ ("10", "0"),
+ ("20", "0"),
+ ("40", "1"),
+ ("0", "ENDBLK"),
+ ("0", "ENDSEC"),
+ ("0", "SECTION"),
+ ("2", "ENTITIES"),
+ ("0", "INSERT"),
+ ("2", "outer"),
+ ("10", "5"),
+ ("20", "5"),
+ ("41", "2"),
+ ("42", "2"),
+ ("50", "30"),
+ ("70", "3"),
+ ("71", "2"),
+ ("44", "10"),
+ ("45", "10"),
+ ("0", "LWPOLYLINE"),
+ ("90", "3"),
+ ("70", "1"),
+ ("10", "0"),
+ ("20", "0"),
+ ("42", "0.5"),
+ ("10", "5"),
+ ("20", "0"),
+ ("10", "5"),
+ ("20", "5"),
+ ("0", "ENDSEC"),
+ ("0", "EOF"),
+ ];
+ let blocks: String = pairs
+ .iter()
+ .map(|(c, v)| {
+ format!(
+ "{c}
{v}
"
- )
- })
- .collect();
- let n = crate::read_dxf(&blocks).map(|g| g.len()).unwrap_or(0);
- assert!(n >= 12, "the array insert expands its nested blocks: {n} entities");
- survives("DXF (blocks)", &mutations(blocks.as_bytes(), true, 0xdb), usize::MAX, |b| {
- crate::read_dxf(&String::from_utf8_lossy(b)).ok().map(|g| g.len())
- });
- // The two lies that need each other, written out: a block that inserts itself (the
- // depth limit), and an array of 10,000 by 10,000 copies (the cell and work budgets).
- let looped = blocks.replacen(
- "2
+ )
+ })
+ .collect();
+ let n = crate::read_dxf(&blocks).map(|g| g.len()).unwrap_or(0);
+ assert!(n >= 12, "the array insert expands its nested blocks: {n} entities");
+ survives("DXF (blocks)", &mutations(blocks.as_bytes(), true, 0xdb), usize::MAX, |b| {
+ crate::read_dxf(&String::from_utf8_lossy(b)).ok().map(|g| g.len())
+ });
+ // The two lies that need each other, written out: a block that inserts itself (the
+ // depth limit), and an array of 10,000 by 10,000 copies (the cell and work budgets).
+ let looped = blocks.replacen(
+ "2
inner
10
2",
- "2
+ "2
outer
10
2",
- 1,
- );
- assert_ne!(looped, blocks, "the outer block now inserts itself");
- let huge = blocks.replacen(
- "70
+ 1,
+ );
+ assert_ne!(looped, blocks, "the outer block now inserts itself");
+ let huge = blocks.replacen(
+ "70
3
71
2",
- "70
+ "70
10000
71
10000",
- 1,
- );
- assert_ne!(huge, blocks);
- survives("DXF (self-insert, huge array)", &[looped.into_bytes(), huge.into_bytes()], usize::MAX, |b| {
- crate::read_dxf(&String::from_utf8_lossy(b)).ok().map(|g| g.len())
- });
- }
+ 1,
+ );
+ assert_ne!(huge, blocks);
+ survives("DXF (self-insert, huge array)", &[looped.into_bytes(), huge.into_bytes()], usize::MAX, |b| {
+ crate::read_dxf(&String::from_utf8_lossy(b)).ok().map(|g| g.len())
+ });
+}
- #[test]
- fn threemf_survives_mutation() {
- let mesh = cube();
- let object = crate::MeshObject {
- name: "Cube".into(),
- positions: mesh.positions,
- triangles: mesh.triangles,
- color: Some([0.8, 0.2, 0.1]),
- ..Default::default()
- };
- let good = crate::write_3mf(&[object]).unwrap();
- assert!(crate::read_3mf(&good).is_ok());
- survives("3MF", &mutations(&good, false, 0x3f), usize::MAX, |b| {
- crate::read_3mf(b).ok().map(|(objects, _)| objects.iter().map(|o| o.triangles.len()).sum())
- });
- // Objects in another model part (production extension), as slicers save projects.
- let production = crate::threemf::tests::production_package();
- assert!(crate::read_3mf(&production).is_ok());
- survives("3MF (production extension)", &mutations(&production, false, 0x2089), usize::MAX, |b| {
+#[test]
+fn threemf_survives_mutation() {
+ let mesh = cube();
+ let object = crate::MeshObject {
+ name: "Cube".into(),
+ positions: mesh.positions,
+ triangles: mesh.triangles,
+ color: Some([0.8, 0.2, 0.1]),
+ ..Default::default()
+ };
+ let good = crate::write_3mf(&[object]).unwrap();
+ assert!(crate::read_3mf(&good).is_ok());
+ survives("3MF", &mutations(&good, false, 0x3f), usize::MAX, |b| {
+ crate::read_3mf(b).ok().map(|(objects, _)| objects.iter().map(|o| o.triangles.len()).sum())
+ });
+ // Objects in another model part (production extension), as slicers save projects.
+ let production = crate::threemf::tests::production_package();
+ assert!(crate::read_3mf(&production).is_ok());
+ survives("3MF (production extension)", &mutations(&production, false, 0x2089), usize::MAX, |b| {
+ crate::read_3mf(b).ok().map(|(objects, _)| objects.iter().map(|o| o.triangles.len()).sum())
+ });
+ // Its model parts' XML mutated and zipped again: mutations of the zip mostly stop at its
+ // checksums, these reach the `p:path` and `objectid` handling.
+ let parts = crate::threemf::tests::production_parts();
+ for (k, (name, xml)) in parts.iter().enumerate() {
+ let rezipped: Vec<Vec<u8>> = mutations(xml.as_bytes(), true, 0x2089 + k as u64)
+ .iter()
+ .map(|m| {
+ let entries: Vec<(&str, &[u8])> = parts.iter().map(|(n, x)| (*n, if n == name { m.as_slice() } else { x.as_bytes() })).collect();
+ crate::threemf::tests::package_bytes(&entries)
+ })
+ .collect();
+ survives(&format!("3MF (production extension, {name} mutated)"), &rezipped, usize::MAX, |b| {
crate::read_3mf(b).ok().map(|(objects, _)| objects.iter().map(|o| o.triangles.len()).sum())
});
- // Its model parts' XML mutated and zipped again: mutations of the zip mostly stop at its
- // checksums, these reach the `p:path` and `objectid` handling.
- let parts = crate::threemf::tests::production_parts();
- for (k, (name, xml)) in parts.iter().enumerate() {
- let rezipped: Vec<Vec<u8>> = mutations(xml.as_bytes(), true, 0x2089 + k as u64)
- .iter()
- .map(|m| {
- let entries: Vec<(&str, &[u8])> = parts.iter().map(|(n, x)| (*n, if n == name { m.as_slice() } else { x.as_bytes() })).collect();
- crate::threemf::tests::package_bytes(&entries)
- })
- .collect();
- survives(&format!("3MF (production extension, {name} mutated)"), &rezipped, usize::MAX, |b| {
- crate::read_3mf(b).ok().map(|(objects, _)| objects.iter().map(|o| o.triangles.len()).sum())
- });
- }
}
+}
- #[test]
- fn gltf_survives_mutation() {
- let mut f = crate::gltf_in::tests::Fixture::default();
- let m = f.cube_mesh("Cube", 0.01);
- let child = f.node(serde_json::json!({"name": "C", "mesh": m, "translation": [0.0, 0.1, 0.0]}));
- let n = f.node(serde_json::json!({"mesh": m, "children": [child], "rotation": [0.0, 0.0, std::f64::consts::FRAC_1_SQRT_2, std::f64::consts::FRAC_1_SQRT_2]}));
- f.roots = vec![n];
- let mut no_files = |name: &str| -> std::result::Result<Vec<u8>, String> { Err(format!("no file `{name}`")) };
- let glb = f.glb();
- assert!(crate::read_gltf(&glb, "fuzz.glb", &mut no_files).is_ok());
- survives("glTF (.glb)", &mutations(&glb, false, 0x91b), usize::MAX, |b| crate::read_gltf(b, "fuzz.glb", &mut no_files).ok().map(|_| 1));
- // The JSON form, its buffer embedded as a data URI.
- let uri = format!("data:application/octet-stream;base64,{}", base64(&f.bin));
- let gltf = f.json(Some(&uri)).to_string().into_bytes();
- assert!(crate::read_gltf(&gltf, "fuzz.gltf", &mut no_files).is_ok());
- survives("glTF (.gltf)", &mutations(&gltf, true, 0x917), usize::MAX, |b| crate::read_gltf(b, "fuzz.gltf", &mut no_files).ok().map(|_| 1));
- }
+#[test]
+fn gltf_survives_mutation() {
+ let mut f = crate::gltf_in::tests::Fixture::default();
+ let m = f.cube_mesh("Cube", 0.01);
+ let child = f.node(serde_json::json!({"name": "C", "mesh": m, "translation": [0.0, 0.1, 0.0]}));
+ let n = f.node(
+ serde_json::json!({"mesh": m, "children": [child], "rotation": [0.0, 0.0, std::f64::consts::FRAC_1_SQRT_2, std::f64::consts::FRAC_1_SQRT_2]}),
+ );
+ f.roots = vec![n];
+ let mut no_files = |name: &str| -> std::result::Result<Vec<u8>, String> { Err(format!("no file `{name}`")) };
+ let glb = f.glb();
+ assert!(crate::read_gltf(&glb, "fuzz.glb", &mut no_files).is_ok());
+ survives("glTF (.glb)", &mutations(&glb, false, 0x91b), usize::MAX, |b| crate::read_gltf(b, "fuzz.glb", &mut no_files).ok().map(|_| 1));
+ // The JSON form, its buffer embedded as a data URI.
+ let uri = format!("data:application/octet-stream;base64,{}", base64(&f.bin));
+ let gltf = f.json(Some(&uri)).to_string().into_bytes();
+ assert!(crate::read_gltf(&gltf, "fuzz.gltf", &mut no_files).is_ok());
+ survives("glTF (.gltf)", &mutations(&gltf, true, 0x917), usize::MAX, |b| crate::read_gltf(b, "fuzz.gltf", &mut no_files).ok().map(|_| 1));
+}
- #[test]
- fn designs_survive_mutation() {
- let mut doc = solidcraft_doc::Document::new("Fuzz");
- doc.set_param("width", "40", Some("mm"), None).unwrap();
- let mesh = solidcraft_doc::MeshData {
- name: "Preview".into(),
- positions: vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0].into(),
- triangles: vec![0, 1, 2].into(),
- color: None,
- };
- let preview = crate::DesignPreview { meshes: vec![mesh] };
- let container = crate::write_design_container_with_preview(&doc, crate::DesignCompression::Fast, Some(&preview)).unwrap();
- assert!(crate::read_design(&container).is_ok() && crate::read_design_preview(&container).is_some());
- survives("design container", &mutations(&container, false, 0xde5), usize::MAX, |b| {
- let _ = crate::read_design_preview(b);
- let _ = crate::read_design_thumbnail(b);
- crate::read_design(b).ok().map(|d| d.features.len())
- });
- let json = crate::write_design(&doc);
- assert!(crate::read_design(&json).is_ok());
- survives("design JSON", &mutations(&json, true, 0xde6), usize::MAX, |b| crate::read_design(b).ok().map(|d| d.features.len()));
- }
+#[test]
+fn designs_survive_mutation() {
+ let mut doc = solidcraft_doc::Document::new("Fuzz");
+ doc.set_param("width", "40", Some("mm"), None).unwrap();
+ let mesh = solidcraft_doc::MeshData {
+ name: "Preview".into(),
+ positions: vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0].into(),
+ triangles: vec![0, 1, 2].into(),
+ color: None,
+ };
+ let preview = crate::DesignPreview { meshes: vec![mesh] };
+ let container = crate::write_design_container_with_preview(&doc, crate::DesignCompression::Fast, Some(&preview)).unwrap();
+ assert!(crate::read_design(&container).is_ok() && crate::read_design_preview(&container).is_some());
+ survives("design container", &mutations(&container, false, 0xde5), usize::MAX, |b| {
+ let _ = crate::read_design_preview(b);
+ let _ = crate::read_design_thumbnail(b);
+ crate::read_design(b).ok().map(|d| d.features.len())
+ });
+ let json = crate::write_design(&doc);
+ assert!(crate::read_design(&json).is_ok());
+ survives("design JSON", &mutations(&json, true, 0xde6), usize::MAX, |b| crate::read_design(b).ok().map(|d| d.features.len()));
+}
- /// A drawing, as a container and as plain JSON: every mutation gives an error or a drawing,
- /// never a panic, and `file_kind` always answers.
- #[test]
- fn drawings_survive_mutation() {
- use solidcraft_geom::drawing_sheet::{DrawingBorderProfile, Orientation, PaperSize, Sheet};
- let mut ids = solidcraft_doc::ids::IdSource::seeded(0xd4a);
- let a3 = Sheet::standard(PaperSize::A3, Orientation::Landscape);
- let mut drawing = solidcraft_doc::drawing::Drawing::new("Fuzz", "en", "Sheet 1", a3, Some(DrawingBorderProfile::iso()), &mut ids).unwrap();
- let id = drawing.allocate_id(&mut ids).unwrap();
- drawing.sources.push(solidcraft_doc::drawing::Source { id, path: "plate.sc".into() });
- let container = crate::write_drawing_container(&drawing).unwrap();
- assert!(crate::read_drawing(&container).is_ok());
- survives("drawing container", &mutations(&container, false, 0xd4b), usize::MAX, |b| {
- let _ = crate::file_kind(b);
- let _ = crate::read_design(b);
- crate::read_drawing(b).ok().map(|d| d.sheets.len())
- });
- let json = drawing.to_json().unwrap().into_bytes();
- assert!(crate::read_drawing(&json).is_ok());
- survives("drawing JSON", &mutations(&json, true, 0xd4c), usize::MAX, |b| {
- let _ = crate::file_kind(b);
- crate::read_drawing(b).ok().map(|d| d.sheets.len())
- });
- }
+/// A drawing, as a container and as plain JSON: every mutation gives an error or a drawing,
+/// never a panic, and `file_kind` always answers.
+#[test]
+fn drawings_survive_mutation() {
+ use solidcraft_geom::drawing_sheet::{DrawingBorderProfile, Orientation, PaperSize, Sheet};
+ let mut ids = solidcraft_doc::ids::IdSource::seeded(0xd4a);
+ let a3 = Sheet::standard(PaperSize::A3, Orientation::Landscape);
+ let mut drawing = solidcraft_doc::drawing::Drawing::new("Fuzz", "en", "Sheet 1", a3, Some(DrawingBorderProfile::iso()), &mut ids).unwrap();
+ let id = drawing.allocate_id(&mut ids).unwrap();
+ drawing.sources.push(solidcraft_doc::drawing::Source { id, path: "plate.sc".into() });
+ let container = crate::write_drawing_container(&drawing).unwrap();
+ assert!(crate::read_drawing(&container).is_ok());
+ survives("drawing container", &mutations(&container, false, 0xd4b), usize::MAX, |b| {
+ let _ = crate::file_kind(b);
+ let _ = crate::read_design(b);
+ crate::read_drawing(b).ok().map(|d| d.sheets.len())
+ });
+ let json = drawing.to_json().unwrap().into_bytes();
+ assert!(crate::read_drawing(&json).is_ok());
+ survives("drawing JSON", &mutations(&json, true, 0xd4c), usize::MAX, |b| {
+ let _ = crate::file_kind(b);
+ crate::read_drawing(b).ok().map(|d| d.sheets.len())
+ });
+}
- /// A paper scene's JSON, mutated, read, prepared and written as SVG: an error or a document,
- /// never a panic.
- #[test]
- fn paper_scenes_survive_mutation_into_svg() {
- use solidcraft_geom::paper::{Group, Item, ObjectKind, Scene};
- use solidcraft_geom::{Loop2, Region2, Seg2, Vec2};
- let p = Vec2::new;
- let border = Loop2::polygon(&[p(20.0, 10.0), p(410.0, 10.0), p(410.0, 287.0), p(20.0, 287.0)]);
- let items = vec![
- Item::Stroke { path: border.segs.clone(), closed: true, width_mm: 0.7 },
- Item::Stroke { path: vec![Seg2::Arc { center: p(50.0, 50.0), radius: 10.0, start: 0.0, sweep: 4.0 }], closed: false, width_mm: 0.35 },
- Item::Fill { region: Region2 { outer: border, holes: vec![] } },
- ];
- let scene = Scene { size_mm: p(420.0, 297.0), groups: vec![Group { object: 7, kind: ObjectKind::DrawingBorder, items }] };
- let json = serde_json::to_vec(&scene).unwrap();
- let write = |b: &[u8]| -> Option<usize> {
- let scene: Scene = serde_json::from_slice(b).ok()?;
- let prepared = scene.prepare().ok()?;
- crate::paper::paper_svg(&prepared).ok().map(|(bytes, _)| bytes.len())
- };
- assert!(write(&json).is_some());
- survives("paper scene to SVG", &mutations(&json, true, 0x5a3), usize::MAX, write);
- }
+/// A paper scene's JSON, mutated, read, prepared and written as SVG: an error or a document,
+/// never a panic.
+#[test]
+fn paper_scenes_survive_mutation_into_svg() {
+ use solidcraft_geom::paper::{Group, Item, ObjectKind, Scene};
+ use solidcraft_geom::{Loop2, Region2, Seg2, Vec2};
+ let p = Vec2::new;
+ let border = Loop2::polygon(&[p(20.0, 10.0), p(410.0, 10.0), p(410.0, 287.0), p(20.0, 287.0)]);
+ let items = vec![
+ Item::Stroke { path: border.segs.clone(), closed: true, width_mm: 0.7 },
+ Item::Stroke { path: vec![Seg2::Arc { center: p(50.0, 50.0), radius: 10.0, start: 0.0, sweep: 4.0 }], closed: false, width_mm: 0.35 },
+ Item::Fill { region: Region2 { outer: border, holes: vec![] } },
+ ];
+ let scene = Scene { size_mm: p(420.0, 297.0), groups: vec![Group { object: 7, kind: ObjectKind::DrawingBorder, items }] };
+ let json = serde_json::to_vec(&scene).unwrap();
+ let write = |b: &[u8]| -> Option<usize> {
+ let scene: Scene = serde_json::from_slice(b).ok()?;
+ let prepared = scene.prepare().ok()?;
+ crate::paper::paper_svg(&prepared).ok().map(|(bytes, _)| bytes.len())
+ };
+ assert!(write(&json).is_some());
+ survives("paper scene to SVG", &mutations(&json, true, 0x5a3), usize::MAX, write);
+}
- /// Standard base64 (RFC 4648) of `bytes`, for a data URI.
- fn base64(bytes: &[u8]) -> String {
- const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- let mut s = String::new();
- for c in bytes.chunks(3) {
- let v = (u32::from(c[0]) << 16) | (u32::from(*c.get(1).unwrap_or(&0)) << 8) | u32::from(*c.get(2).unwrap_or(&0));
- for k in 0..4 {
- if k <= c.len() {
- s.push(char::from(A[(v >> (18 - 6 * k)) as usize & 63]));
- } else {
- s.push('=');
- }
+/// Standard base64 (RFC 4648) of `bytes`, for a data URI.
+fn base64(bytes: &[u8]) -> String {
+ const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ let mut s = String::new();
+ for c in bytes.chunks(3) {
+ let v = (u32::from(c[0]) << 16) | (u32::from(*c.get(1).unwrap_or(&0)) << 8) | u32::from(*c.get(2).unwrap_or(&0));
+ for k in 0..4 {
+ if k <= c.len() {
+ s.push(char::from(A[(v >> (18 - 6 * k)) as usize & 63]));
+ } else {
+ s.push('=');
}
}
- s
}
+ s
}
diff --git a/crates/testkit/Cargo.toml b/crates/testkit/Cargo.toml
new file mode 100644
index 0000000000..118c436d2d
--- /dev/null
+++ b/crates/testkit/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "solidcraft-testkit"
+description = "SolidCraft test tooling: deterministic mutations of valid files and the no-panic check run over them (a dev-dependency only)"
+version.workspace = true
+edition.workspace = true
+license.workspace = true
+
+[lints]
+workspace = true
diff --git a/crates/testkit/src/lib.rs b/crates/testkit/src/lib.rs
new file mode 100644
index 0000000000..60ba9eccc6
--- /dev/null
+++ b/crates/testkit/src/lib.rs
@@ -0,0 +1,294 @@
+//! Deterministic mutations of valid files, and the readers run over them (#469). Every reader of
+//! bytes the user didn't write gets the same treatment: truncation, flipped and overwritten
+//! bytes, dropped and repeated runs, appended garbage and, for text formats, dropped and
+//! repeated lines and numbers that lie (`nan`, `inf`, `1e400`, …). Each mutated file must give an
+//! error or a result, never a panic, within a time budget and the reader's own caps. A panic a
+//! reader catches itself (`solidcraft_kernel::guard`) counts too: the browser build aborts on any
+//! panic, so a caught one on the desktop is a crash there.
+//!
+//! The mutations come from a fixed seed (xorshift), so a failure names a case that reproduces.
+//! No fuzzing dependency, nothing nightly, nothing native: it runs in the normal test suite and
+//! builds for wasm. The readers' own hand-written cases stay; they cover particular lies (a
+//! length in a container header) that random changes rarely hit. STEP, IGES and FreeCAD/Rhino
+//! files are read by the kernel, which is moving to solidcore (#1597), and keep their own tests.
+
+#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
+#![forbid(unsafe_code)]
+
+use std::panic::{AssertUnwindSafe, catch_unwind};
+use std::time::{Duration, Instant};
+
+/// A deterministic xorshift64 stream.
+struct Rng(u64);
+
+impl Rng {
+ fn new(seed: u64) -> Rng {
+ Rng(seed | 1)
+ }
+
+ fn next(&mut self) -> u64 {
+ self.0 ^= self.0 << 13;
+ self.0 ^= self.0 >> 7;
+ self.0 ^= self.0 << 17;
+ self.0
+ }
+
+ /// A value in `0..n` (0 when `n` is 0).
+ fn below(&mut self, n: usize) -> usize {
+ if n == 0 { 0 } else { (self.next() % n as u64) as usize }
+ }
+}
+
+/// Numbers a hostile file might hold in place of a real one.
+const LIARS: [&str; 10] = ["nan", "inf", "-inf", "1e400", "-1e400", "-0", "4294967296", "-1", "99999999999999999999", "0x7fffffff"];
+
+/// Most truncations of one file (every length up to this, evenly spaced beyond).
+const MAX_TRUNCATIONS: usize = 1024;
+
+/// Mutations of `good`, the same for the same `good` and `seed`. `text`: also line and number
+/// mutations (the file is UTF-8 text).
+pub fn mutations(good: &[u8], text: bool, seed: u64) -> Vec<Vec<u8>> {
+ let mut out: Vec<Vec<u8>> = Vec::new();
+ let n = good.len();
+ let mut rng = Rng::new(seed);
+ // Truncations: every length of a short file; of a long one, its first and last 64 lengths
+ // (headers, final records) and evenly spaced ones between.
+ if n <= MAX_TRUNCATIONS {
+ out.extend((0..n).map(|k| good[..k].to_vec()));
+ } else {
+ let mut cuts: Vec<usize> = (0..64).chain(n - 64..n).collect();
+ cuts.extend((0..MAX_TRUNCATIONS - 128).map(|i| 64 + i * (n - 128) / (MAX_TRUNCATIONS - 128)));
+ cuts.sort_unstable();
+ cuts.dedup();
+ out.extend(cuts.into_iter().map(|k| good[..k].to_vec()));
+ }
+ if n == 0 {
+ return out;
+ }
+ // Bytes flipped, zeroed and saturated at seeded places.
+ for i in 0..384 {
+ let at = rng.below(n);
+ let mut b = good.to_vec();
+ b[at] = match i % 3 {
+ 0 => b[at] ^ ((rng.next() >> 32) as u8 | 1),
+ 1 => 0x00,
+ _ => 0xff,
+ };
+ out.push(b);
+ }
+ // Runs of bytes dropped, repeated and swapped with their neighbour.
+ for _ in 0..96 {
+ let at = rng.below(n);
+ let len = 1 + rng.below(64.min(n - at));
+ let end = at + len;
+ let mut dropped = good[..at].to_vec();
+ dropped.extend_from_slice(&good[end..]);
+ out.push(dropped);
+ let mut repeated = good[..end].to_vec();
+ repeated.extend_from_slice(&good[at..]);
+ out.push(repeated);
+ if end + len <= n {
+ let mut swapped = good[..at].to_vec();
+ swapped.extend_from_slice(&good[end..end + len]);
+ swapped.extend_from_slice(&good[at..end]);
+ swapped.extend_from_slice(&good[end + len..]);
+ out.push(swapped);
+ }
+ }
+ // The whole file: with garbage after it, twice over, reversed.
+ let mut tail = good.to_vec();
+ tail.extend((0..4096).map(|_| (rng.next() >> 40) as u8));
+ out.push(tail);
+ out.push([good, good].concat());
+ out.push(good.iter().rev().copied().collect());
+ if text && let Ok(s) = std::str::from_utf8(good) {
+ out.extend(text_mutations(s, &mut rng));
+ }
+ out
+}
+
+/// A text file's lines dropped, repeated and swapped, and its numbers replaced by lies.
+fn text_mutations(s: &str, rng: &mut Rng) -> Vec<Vec<u8>> {
+ let mut out = Vec::new();
+ let lines: Vec<&str> = s.split_inclusive('\n').collect();
+ let join = |v: &[&str]| v.concat().into_bytes();
+ let picked: Vec<usize> = if lines.len() <= 200 { (0..lines.len()).collect() } else { (0..200).map(|_| rng.below(lines.len())).collect() };
+ for &i in &picked {
+ let mut v = lines.clone();
+ v.remove(i);
+ out.push(join(&v));
+ let mut v = lines.clone();
+ v.insert(i, lines[i]);
+ out.push(join(&v));
+ if i + 1 < lines.len() {
+ let mut v = lines.clone();
+ v.swap(i, i + 1);
+ out.push(join(&v));
+ }
+ }
+ // A line in place of another (a block that inserts itself, a count where a coordinate was).
+ for _ in 0..200.min(lines.len() * lines.len()) {
+ let (i, j) = (rng.below(lines.len()), rng.below(lines.len()));
+ let mut v = lines.clone();
+ v[i] = lines[j];
+ out.push(join(&v));
+ }
+ // A word in place of another of the file's own words: a renamed block, a swapped keyword, a
+ // block that now inserts itself. Every pair in a small file, a sample in a large one.
+ let words = word_spans(s);
+ let mut vocab: Vec<&str> = words.iter().map(|&(a, b)| &s[a..b]).collect();
+ vocab.sort_unstable();
+ vocab.dedup();
+ let pairs = words.len().saturating_mul(vocab.len());
+ let swap = |(a, b): (usize, usize), w: &str| format!("{}{w}{}", &s[..a], &s[b..]).into_bytes();
+ if pairs <= 3000 {
+ for &span in &words {
+ for &w in vocab.iter().filter(|w| **w != &s[span.0..span.1]) {
+ out.push(swap(span, w));
+ }
+ }
+ } else {
+ for _ in 0..400 {
+ out.push(swap(words[rng.below(words.len())], vocab[rng.below(vocab.len())]));
+ }
+ }
+ // Numbers: each of up to 200 of them, in turn, becomes one of the liars.
+ let numbers = number_spans(s);
+ let chosen: Vec<(usize, usize)> = if numbers.len() <= 200 { numbers } else { (0..200).map(|_| numbers[rng.below(numbers.len())]).collect() };
+ for (k, (a, b)) in chosen.into_iter().enumerate() {
+ let liar = LIARS[k % LIARS.len()];
+ out.push(format!("{}{liar}{}", &s[..a], &s[b..]).into_bytes());
+ }
+ // Every number lying at once (two counts that are only harmless one at a time).
+ let all = number_spans(s);
+ for liar in LIARS {
+ let mut t = String::with_capacity(s.len());
+ let mut at = 0;
+ for &(a, b) in &all {
+ t.push_str(&s[at..a]);
+ t.push_str(liar);
+ at = b;
+ }
+ t.push_str(&s[at..]);
+ out.push(t.into_bytes());
+ }
+ out
+}
+
+/// Byte spans of the words in `s` (runs of ASCII letters, digits and `_` that start with a
+/// letter).
+fn word_spans(s: &str) -> Vec<(usize, usize)> {
+ let b = s.as_bytes();
+ let mut out = Vec::new();
+ let mut i = 0;
+ while i < b.len() {
+ if !b[i].is_ascii_alphabetic() || (i > 0 && (b[i - 1].is_ascii_alphanumeric() || b[i - 1] == b'_')) {
+ i += 1;
+ continue;
+ }
+ let a = i;
+ while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_') {
+ i += 1;
+ }
+ out.push((a, i));
+ }
+ out
+}
+
+/// Byte spans of the decimal numbers in `s` (a sign, digits, a point, an exponent).
+fn number_spans(s: &str) -> Vec<(usize, usize)> {
+ let b = s.as_bytes();
+ let mut out = Vec::new();
+ let mut i = 0;
+ while i < b.len() {
+ let starts = b[i].is_ascii_digit() || ((b[i] == b'-' || b[i] == b'.') && b.get(i + 1).is_some_and(u8::is_ascii_digit));
+ let after_word = i > 0 && (b[i - 1].is_ascii_alphanumeric() || b[i - 1] == b'_');
+ if !starts || after_word {
+ i += 1;
+ continue;
+ }
+ let a = i;
+ i += 1;
+ while i < b.len() && (b[i].is_ascii_digit() || b[i] == b'.' || ((b[i] == b'e' || b[i] == b'E') && i + 1 < b.len())) {
+ if (b[i] == b'e' || b[i] == b'E') && (b[i + 1] == b'-' || b[i + 1] == b'+') {
+ i += 1;
+ }
+ i += 1;
+ }
+ out.push((a, i));
+ }
+ out
+}
+
+thread_local! {
+ /// Panics on this thread so far, caught or not ([`count_panics`]).
+ static PANICS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
+}
+
+/// Count every panic per thread (tests run on several), then run the hook that was there.
+fn count_panics() {
+ static ONCE: std::sync::Once = std::sync::Once::new();
+ ONCE.call_once(|| {
+ let previous = std::panic::take_hook();
+ std::panic::set_hook(Box::new(move |info| {
+ PANICS.with(|p| p.set(p.get() + 1));
+ previous(info);
+ }));
+ });
+}
+
+/// The time any one reader call may take on a mutated file (a debug build on a loaded machine:
+/// generous, but far below what unbounded work on a lying count would take).
+pub const BUDGET: Duration = Duration::from_secs(2);
+
+/// Run `read` over `inputs`: no panic, within [`BUDGET`], and `read`'s count (triangles,
+/// entities, …) at most `cap`. `read` returns `None` for an error. Panics (the test fails) naming
+/// the first case that breaks a rule.
+// Failing the calling test is this function's job; it only ever runs inside tests.
+#[allow(clippy::panic)]
+pub fn survives(what: &str, inputs: &[Vec<u8>], cap: usize, mut read: impl FnMut(&[u8]) -> Option<usize>) {
+ count_panics();
+ let mut slowest = (Duration::ZERO, 0);
+ let mut read_ok = 0;
+ for (i, input) in inputs.iter().enumerate() {
+ let before = PANICS.with(std::cell::Cell::get);
+ let t = Instant::now();
+ let r = catch_unwind(AssertUnwindSafe(|| read(input)));
+ let took = t.elapsed();
+ let head = String::from_utf8_lossy(&input[..input.len().min(120)]).into_owned();
+ let Ok(r) = r else { panic!("{what}: case {i} ({} bytes) panicked; it starts {head:?}", input.len()) };
+ let caught = PANICS.with(std::cell::Cell::get) - before;
+ assert!(
+ caught == 0,
+ "{what}: case {i} ({} bytes) panicked inside the reader (caught there, but the browser aborts); it starts {head:?}",
+ input.len()
+ );
+ assert!(took <= BUDGET, "{what}: case {i} ({} bytes) took {took:?} (budget {BUDGET:?}); it starts {head:?}", input.len());
+ if let Some(count) = r {
+ assert!(count <= cap, "{what}: case {i} gave {count} (cap {cap})");
+ read_ok += 1;
+ }
+ slowest = slowest.max((took, i));
+ }
+ eprintln!("{what}: {} mutations, {read_ok} read, slowest case {} in {:?}", inputs.len(), slowest.1, slowest.0);
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn mutations_are_deterministic_and_cover_the_file() {
+ let good = b"solid x\n facet normal 0 0 1\n vertex 1.5 -2 3e2\n endfacet\nendsolid x\n";
+ let a = mutations(good, true, 7);
+ assert_eq!(a, mutations(good, true, 7), "the same seed gives the same cases");
+ assert_ne!(a, mutations(good, true, 8));
+ assert!(a.iter().any(Vec::is_empty) && a.iter().any(|m| m.len() == good.len() - 1), "truncations down to nothing");
+ assert!(a.iter().any(|m| m.windows(3).any(|w| w == b"nan")), "a number replaced by a liar");
+ assert_eq!(number_spans("v 1.5 -2 3e2 x1 .5e-3"), vec![(2, 5), (6, 8), (9, 12), (16, 21)]);
+ // A long file: bounded truncations, kept to the first and last bytes and spaced between.
+ let long = vec![b'a'; 100_000];
+ let m = mutations(&long, false, 1);
+ assert!(m.len() < 3000, "{} cases", m.len());
+ }
+}