Files
Marcos Paulo cd43ea488e
ci / ci (macos-latest) (push) Has been cancelled
ci / ci (ubuntu-latest) (push) Has been cancelled
ci / ci (windows-latest) (push) Has been cancelled
feat: notescraft-lottie player crate, egui lottie_icon painter, icon_sheet review example, first icon animation set
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-16 19:42:57 -03:00

251 lines
8.3 KiB
Rust

//! Unsupported features, bounded input, and deterministic parser mutations.
use std::panic::catch_unwind;
use notescraft_lottie::{Animation, Error, Frame, Item};
use serde_json::{Value, json};
const STROKE: &str = include_str!("fixtures/stroke.json");
fn fixture() -> Value {
serde_json::from_str(STROKE).expect("fixture")
}
fn group(mut shapes: Value) -> Value {
shapes.as_array_mut().expect("shape array").push(json!({"ty": "tr"}));
json!([{"ty": "gr", "it": shapes}])
}
fn rejected(value: &Value, reason: &str) {
assert!(Animation::parse(&value.to_string()).is_err(), "accepted {reason}");
}
fn unsupported(value: &Value, reason: &str) {
assert!(
matches!(Animation::parse(&value.to_string()),
Err(Error::Unsupported { what, at }) if !what.is_empty() && !at.is_empty()),
"expected contextual Unsupported for {reason}"
);
}
fn finite_bounded(frame: &Frame) {
assert!(frame.width.is_finite());
assert!(frame.height.is_finite());
let mut count = 0;
for item in &frame.items {
let (lines, opacity) = match item {
Item::Stroke(path) => {
assert!(path.width.is_finite());
(&path.polylines, path.opacity)
}
Item::Fill(path) => (&path.polylines, path.opacity),
};
assert!(opacity.is_finite());
for line in lines {
count += line.points.len();
for point in &line.points {
assert!(point.iter().all(|value| value.is_finite()));
}
}
}
assert!(count <= 20_000, "sample budget exceeded: {count}");
}
#[test]
fn rejects_precomp_solid_image_and_text_layers() {
for layer_type in [0, 1, 2, 5] {
let mut value = fixture();
value["layers"][0]["ty"] = json!(layer_type);
unsupported(&value, "unsupported layer type");
}
}
#[test]
fn rejects_masks_mattes_effects_three_d_and_time_stretch() {
for (key, content) in
[("masksProperties", json!([{"mode": "a"}])), ("tt", json!(1)), ("td", json!(1)), ("ef", json!([{"ty": 0}])), ("ddd", json!(1)), ("sr", json!(2))]
{
let mut value = fixture();
value["layers"][0][key] = content;
unsupported(&value, key);
}
let mut value = fixture();
value["ddd"] = json!(1);
unsupported(&value, "3D composition");
}
#[test]
fn rejects_expressions_at_property_boundaries() {
for expression in [json!("time * 10"), json!(""), json!(true)] {
let mut value = fixture();
value["layers"][0]["ks"]["p"]["x"] = expression;
unsupported(&value, "expression property");
}
}
#[test]
fn rejects_unsupported_shape_operators_including_nested_groups() {
for shape_type in ["gf", "gs", "rp", "mm", "rd", "pb", "zz"] {
let mut value = fixture();
value["layers"][0]["shapes"].as_array_mut().expect("shapes").push(json!({"ty": shape_type}));
unsupported(&value, shape_type);
let shapes = value["layers"][0]["shapes"].take();
value["layers"][0]["shapes"] = group(shapes);
unsupported(&value, "nested unsupported shape");
}
}
#[test]
fn rejects_invalid_composition_numbers_without_silent_repair() {
for (key, number) in
[("fr", json!(0)), ("fr", json!(-1)), ("w", json!(0)), ("w", json!(-1)), ("h", json!(0)), ("op", json!(-1)), ("fr", json!(1e100)), ("w", json!(1e100))]
{
let mut value = fixture();
value[key] = number;
rejected(&value, key);
}
for invalid in ["NaN", "Infinity", "-Infinity", "1e999"] {
let source = STROKE.replacen("\"fr\": 10", &format!("\"fr\": {invalid}"), 1);
assert!(Animation::parse(&source).is_err());
}
}
#[test]
fn rejects_nonfinite_or_overflowing_path_and_transform_values() {
for pointer in ["/layers/0/ks/p/k/0", "/layers/0/ks/s/k/0", "/layers/0/ks/r/k", "/layers/0/shapes/1/w/k", "/layers/0/shapes/0/ks/k/v/0/0"] {
let mut value = fixture();
*value.pointer_mut(pointer).expect("fixture field") = json!(1e100);
rejected(&value, pointer);
}
}
#[test]
fn rejects_mismatched_vertex_and_tangent_counts() {
for field in ["v", "i", "o"] {
let mut value = fixture();
value["layers"][0]["shapes"][0]["ks"]["k"][field] = json!([[0, 0]]);
rejected(&value, "mismatched path arrays");
}
for point in [json!([]), json!([0]), json!([0, 0, 0]), json!(["x", 0])] {
let mut value = fixture();
value["layers"][0]["shapes"][0]["ks"]["k"]["v"][0] = point;
rejected(&value, "invalid vertex dimensions");
}
}
#[test]
fn rejects_parent_cycles_and_unknown_parent_ids() {
let mut value = fixture();
value["layers"][0]["parent"] = json!(1);
rejected(&value, "self parent");
value["layers"][0]["parent"] = json!(999);
rejected(&value, "unknown parent");
let mut parent = value["layers"][0].clone();
value["layers"][0]["parent"] = json!(2);
parent["ind"] = json!(2);
parent["parent"] = json!(1);
value["layers"].as_array_mut().expect("layers").push(parent);
rejected(&value, "parent cycle");
}
#[test]
fn rejects_layer_budget_and_ten_thousand_layer_attack() {
for count in [65, 10_000] {
let mut value = fixture();
value["layers"] = json!(
(0..count)
.map(|index| {
let mut layer = value["layers"][0].clone();
layer["ind"] = json!(index + 1);
layer
})
.collect::<Vec<_>>()
);
if count == 65 {
unsupported(&value, "layer cap");
} else {
rejected(&value, "ten thousand layer attack");
}
}
}
#[test]
fn rejects_group_nesting_beyond_eight() {
let mut value = fixture();
let mut shapes = value["layers"][0]["shapes"].take();
for _ in 0..9 {
shapes = group(shapes);
}
value["layers"][0]["shapes"] = shapes;
unsupported(&value, "group depth cap");
}
#[test]
fn rejects_more_than_four_thousand_ninety_six_vertices() {
let mut value = fixture();
let points = vec![[0, 0]; 4097];
value["layers"][0]["shapes"][0]["ks"]["k"] = json!({
"v": points, "i": points, "o": points, "c": false
});
unsupported(&value, "vertex cap");
}
#[test]
fn rejects_cubic_subdivision_requiring_more_than_twenty_thousand_points() {
let mut value = fixture();
value["layers"][0]["shapes"][0]["ks"]["k"] = json!({
"v": [[0, 0], [10, 0]], "i": [[0, 0], [0, 1_000_000]],
"o": [[0, 1_000_000], [0, 0]], "c": false
});
value["layers"][0]["ks"]["s"]["k"] = json!([10_000, 10_000]);
unsupported(&value, "flattened point budget");
}
#[test]
fn oversized_source_is_rejected_before_json_parsing() {
let source = " ".repeat(512 * 1024 + 1);
assert!(matches!(Animation::parse(&source), Err(Error::TooLarge)));
}
#[test]
fn truncation_and_wrong_json_types_return_errors() {
for end in [0, 1, 10, STROKE.len() / 2, STROKE.len() - 3] {
assert!(Animation::parse(&STROKE[..end]).is_err(), "prefix {end}");
}
for source in ["null", "[]", "{}", "true", "42", "\"lottie\""] {
assert!(Animation::parse(source).is_err(), "accepted {source}");
}
}
#[test]
fn nonfinite_and_extreme_sample_frames_never_emit_nonfinite_geometry() {
let animation = Animation::parse(STROKE).expect("fixture");
for frame in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, f32::MAX, -f32::MAX] {
finite_bounded(&animation.frame_at(frame));
}
}
#[test]
fn deterministic_byte_mutations_never_panic_or_emit_invalid_geometry() {
let sources = [STROKE, include_str!("fixtures/shackle.json"), include_str!("fixtures/keyframes.json")];
let mut state = 0x9e37_79b9_u32;
for iteration in 0..2000 {
let source = sources[iteration % sources.len()];
let mut bytes = source.as_bytes().to_vec();
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
let index = usize::try_from(state).expect("u32 fits usize") % bytes.len();
bytes[index] ^= state.to_le_bytes()[1];
let input = String::from_utf8_lossy(&bytes);
let outcome = catch_unwind(|| {
if let Ok(animation) = Animation::parse(&input) {
for frame in [0.0, 2.5, 5.0, 10.0] {
finite_bounded(&animation.frame_at(frame));
}
}
});
assert!(outcome.is_ok(), "mutation {iteration} at byte {index} panicked");
}
}