| 1 | //! #14 robustness gate: corrupt, truncated and mutated inputs must give a |
| 2 | //! recoverable `Err` (or a still-valid `Ok`), never a panic, hang or NaN body. |
| 3 | use paperdoll_renderer::{atlas::AtlasSet, equipment::Preset, preview, project}; |
| 4 | use std::panic::{catch_unwind, AssertUnwindSafe}; |
| 5 | use std::path::{Path, PathBuf}; |
| 6 | |
| 7 | fn fixture(rel: &str) -> PathBuf { |
| 8 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 9 | .join("tests/fixtures") |
| 10 | .join(rel) |
| 11 | } |
| 12 | |
| 13 | struct Temp(PathBuf); |
| 14 | impl Temp { |
| 15 | fn new(tag: &str) -> Self { |
| 16 | let p = std::env::temp_dir().join(format!("ppd-robust-{tag}-{}", std::process::id())); |
| 17 | let _ = std::fs::remove_dir_all(&p); |
| 18 | std::fs::create_dir_all(&p).unwrap(); |
| 19 | Temp(p) |
| 20 | } |
| 21 | } |
| 22 | impl Drop for Temp { |
| 23 | fn drop(&mut self) { |
| 24 | let _ = std::fs::remove_dir_all(&self.0); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /// Deterministic mutations: truncations, byte flips, zeroed and 0xFF runs. |
| 29 | fn mutations(bytes: &[u8], count: usize) -> Vec<Vec<u8>> { |
| 30 | let mut out = vec![Vec::new(), bytes[..bytes.len() / 2].to_vec()]; |
| 31 | let mut seed = 0x9e37_79b9_u64; |
| 32 | let mut next = || { |
| 33 | seed = seed |
| 34 | .wrapping_mul(6364136223846793005) |
| 35 | .wrapping_add(1442695040888963407); |
| 36 | (seed >> 33) as usize |
| 37 | }; |
| 38 | for i in 0..count { |
| 39 | let mut m = bytes.to_vec(); |
| 40 | match i % 4 { |
| 41 | 0 => m.truncate(next() % bytes.len()), |
| 42 | 1 => { |
| 43 | for _ in 0..1 + next() % 8 { |
| 44 | let at = next() % m.len(); |
| 45 | m[at] ^= 1 << (next() % 8); |
| 46 | } |
| 47 | } |
| 48 | 2 => { |
| 49 | let at = next() % m.len(); |
| 50 | let end = (at + 1 + next() % 64).min(m.len()); |
| 51 | m[at..end].fill(0); |
| 52 | } |
| 53 | _ => { |
| 54 | let at = next() % m.len(); |
| 55 | let end = (at + 1 + next() % 64).min(m.len()); |
| 56 | m[at..end].fill(0xff); |
| 57 | } |
| 58 | } |
| 59 | out.push(m); |
| 60 | } |
| 61 | out |
| 62 | } |
| 63 | |
| 64 | fn body_ok(body: &preview::PreviewCharacter) { |
| 65 | assert!(body.positions.iter().flatten().all(|v| v.is_finite())); |
| 66 | let n = body.positions.len() as u32; |
| 67 | assert!(body.indices.iter().all(|i| *i < n)); |
| 68 | } |
| 69 | |
| 70 | fn fuzz_body(src: &str, ext: &str, count: usize) { |
| 71 | let bytes = std::fs::read(fixture(src)).unwrap(); |
| 72 | let temp = Temp::new(&src.replace(['/', '.'], "-")); |
| 73 | for (i, m) in mutations(&bytes, count).into_iter().enumerate() { |
| 74 | let path = temp.0.join(format!("m{i}.{ext}")); |
| 75 | std::fs::write(&path, &m).unwrap(); |
| 76 | let result = catch_unwind(AssertUnwindSafe(|| preview::load_body_file(&path))); |
| 77 | match result { |
| 78 | Ok(Ok(body)) => body_ok(&body), |
| 79 | Ok(Err(e)) => assert!(!e.is_empty()), |
| 80 | Err(_) => panic!("{src} mutation {i} panicked"), |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn mutated_glb_bodies_never_panic() { |
| 87 | fuzz_body("bodies/test-body-male.glb", "glb", 800); |
| 88 | fuzz_body("bodies/static/static-body-male.glb", "glb", 80); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn mutated_fbx_body_never_panics() { |
| 93 | fuzz_body("bodies/test-body-male.fbx", "fbx", 120); |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn mutated_obj_body_never_panics() { |
| 98 | let temp = Temp::new("obj"); |
| 99 | for f in ["static-body-albedo.png", "static-body-male.mtl"] { |
| 100 | std::fs::copy(fixture(&format!("bodies/static/{f}")), temp.0.join(f)).unwrap(); |
| 101 | } |
| 102 | let bytes = std::fs::read(fixture("bodies/static/static-body-male.obj")).unwrap(); |
| 103 | for (i, m) in mutations(&bytes, 120).into_iter().enumerate() { |
| 104 | let path = temp.0.join(format!("m{i}.obj")); |
| 105 | std::fs::write(&path, &m).unwrap(); |
| 106 | match catch_unwind(AssertUnwindSafe(|| preview::load_body_file(&path))) { |
| 107 | Ok(Ok(body)) => body_ok(&body), |
| 108 | Ok(Err(_)) => {} |
| 109 | Err(_) => panic!("obj mutation {i} panicked"), |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn mutated_equipment_preset_never_panics() { |
| 116 | let bytes = std::fs::read(fixture("equipment/sword.ppe")).unwrap(); |
| 117 | for (i, m) in mutations(&bytes, 200).into_iter().enumerate() { |
| 118 | if catch_unwind(AssertUnwindSafe(|| Preset::decode(&m))).is_err() { |
| 119 | panic!("ppe mutation {i} panicked"); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn mutated_project_manifest_is_a_recoverable_error() { |
| 126 | let temp = Temp::new("ppd"); |
| 127 | let opened = project::create_project(&temp.0, "robust").unwrap(); |
| 128 | let text = std::fs::read(&opened.manifest_path).unwrap(); |
| 129 | for (i, m) in mutations(&text, 200).into_iter().enumerate() { |
| 130 | let s = String::from_utf8_lossy(&m); |
| 131 | if catch_unwind(AssertUnwindSafe(|| project::parse_manifest(&s))).is_err() { |
| 132 | panic!("manifest mutation {i} panicked"); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn garbage_ppda_is_an_error() { |
| 139 | let temp = Temp::new("ppda"); |
| 140 | let cases: [&[u8]; 4] = [b"", b"{", b"{\"format\":3}", &[0xff; 64]]; |
| 141 | for (i, c) in cases.iter().enumerate() { |
| 142 | let path = temp.0.join(format!("x{i}.ppda")); |
| 143 | std::fs::write(&path, c).unwrap(); |
| 144 | assert!(AtlasSet::open(&path).is_err()); |
| 145 | } |
| 146 | assert!(AtlasSet::open(&temp.0.join("missing.ppda")).is_err()); |
| 147 | } |
| 148 | |
| 149 | #[test] |
| 150 | fn non_finite_and_singular_gltf_transforms_are_errors() { |
| 151 | let source = std::fs::read(fixture("bodies/static/static-body-male.glb")).unwrap(); |
| 152 | let json_len = u32::from_le_bytes(source[12..16].try_into().unwrap()) as usize; |
| 153 | let doc: serde_json::Value = serde_json::from_slice(&source[20..20 + json_len]).unwrap(); |
| 154 | let temp = Temp::new("nan"); |
| 155 | let edits: [(&str, serde_json::Value); 2] = [ |
| 156 | ("scale0", serde_json::json!([0.0, 0.0, 0.0])), |
| 157 | ("huge", serde_json::json!([3.0e38, 3.0e38, 3.0e38])), |
| 158 | ]; |
| 159 | for (name, scale) in edits { |
| 160 | let mut d = doc.clone(); |
| 161 | for node in d["nodes"].as_array_mut().unwrap() { |
| 162 | if node.get("mesh").is_some() { |
| 163 | node["scale"] = scale.clone(); |
| 164 | } |
| 165 | } |
| 166 | let mut json = serde_json::to_vec(&d).unwrap(); |
| 167 | while !json.len().is_multiple_of(4) { |
| 168 | json.push(b' '); |
| 169 | } |
| 170 | let rest = &source[20 + json_len..]; |
| 171 | let mut out = source[0..8].to_vec(); |
| 172 | out.extend(((20 + json.len() + rest.len()) as u32).to_le_bytes()); |
| 173 | out.extend((json.len() as u32).to_le_bytes()); |
| 174 | out.extend(b"JSON"); |
| 175 | out.extend(&json); |
| 176 | out.extend(rest); |
| 177 | let path = temp.0.join(format!("{name}.glb")); |
| 178 | std::fs::write(&path, out).unwrap(); |
| 179 | match preview::load_body_file(&path) { |
| 180 | Err(_) => {} |
| 181 | Ok(body) => panic!("{name}: loaded {} vertices", body.positions.len()), |
| 182 | } |
| 183 | } |
| 184 | } |