Skip to main content

unityrs_testkit/
fuzz.rs

1//! The fuzz targets' input layouts (`fuzz/`, the fuzz plan of
2//! `docs/plans/core.md` section 6), in one place: the targets read their
3//! inputs through these, and the seed command (`examples/fuzz_seeds.rs`)
4//! writes the builders' files, trees, objects and blocks through them, so
5//! the two cannot drift.
6//!
7//! - [`TreeCase`]: a typetree and the bytes of one value, for the targets
8//!   that read and write values (`boost_read`, `python_read`,
9//!   `write_after_read`). The rows are what the readers and the tree
10//!   compiler read of a node (level, meta flag, type, name), so a mutation
11//!   of a type name or a level is one byte away, not an offset into a
12//!   string buffer.
13//! - [`blob_case`]: a stored typetree, the bytes `TypeTreeNode.parse_blob`
14//!   or `TypeTreeNode.parse` read, for the `typetree` target.
15//! - [`lz4_case`]: one LZ4 block and its declared size, for the `lz4`
16//!   target.
17//! - [`TextureCase`]: a `Texture2D`'s fields, the meter its decode runs
18//!   under and its data, for the `texture` target.
19//! - [`MeshCase`]: a Mesh's or a Sprite's value, its file's version and
20//!   byte order, its resource and the meter, for the `meshdecode` target.
21//! - [`ShaderCase`]: a shader program blob, its Unity version and the meter,
22//!   for the `shader` target.
23//! - [`anim_humanoid_cases`]: [`SceneCase`] humanoid rigs, for the
24//!   `anim_humanoid` target.
25//!
26//! Every decoder here takes any bytes: a field the input does not reach is
27//! empty (a row, a reference, the data), never an error.
28
29mod anim;
30mod humanoid;
31mod scene;
32pub use anim::{anim_clip_cases, anim_gltf_cases};
33pub use humanoid::anim_humanoid_cases;
34pub use scene::scene_cases;
35
36/// A synthetic serialized scene and strict export profile for the 3D targets.
37/// Layout: meter and flags (one byte each), a little-endian u16 profile
38/// length, that many profile bytes, then the serialized file to the end.
39/// Flag 1 selects a prefab graph, flag 2 the `CorePbr` mapper. Nonzero meters
40/// also run the export under `meter² × 64` bytes.
41#[derive(Clone, Debug, Default, PartialEq, Eq)]
42pub struct SceneCase {
43    /// The operation's additional small-meter selector; zero runs only full.
44    pub meter: u8,
45    /// Graph and material policy bits.
46    pub flags: u8,
47    /// Strict profile JSON; empty means the default profile.
48    pub profile: Vec<u8>,
49    /// A serialized file, with embedded hand-built trees in the seeds.
50    pub data: Vec<u8>,
51}
52impl SceneCase {
53    /// Encode the profile before the mutable scene bytes.
54    #[must_use]
55    pub fn encode(&self) -> Vec<u8> {
56        let len = self.profile.len().min(usize::from(u16::MAX));
57        let mut out = vec![self.meter, self.flags];
58        out.extend_from_slice(&u16::try_from(len).unwrap_or(u16::MAX).to_le_bytes());
59        out.extend_from_slice(&self.profile[..len]);
60        out.extend_from_slice(&self.data);
61        out
62    }
63    /// Decode arbitrary bytes; a truncated profile retains only its prefix.
64    #[must_use]
65    pub fn decode(input: &[u8]) -> Self {
66        let mut case = Self {
67            meter: input.first().copied().unwrap_or(0),
68            flags: input.get(1).copied().unwrap_or(0),
69            ..Self::default()
70        };
71        if let Some(header) = input.get(2..4) {
72            let len = usize::from(u16::from_le_bytes([header[0], header[1]]));
73            let end = 4usize.saturating_add(len).min(input.len());
74            case.profile = input[4..end].to_vec();
75            case.data = input[end..].to_vec();
76        }
77        case
78    }
79}
80
81/// A typetree row: what the readers and the compiler read of a node.
82#[derive(Clone, Debug, Default, PartialEq, Eq)]
83pub struct Row {
84    /// `m_Level`.
85    pub level: u8,
86    /// `m_MetaFlag` (`0x4000` aligns).
87    pub meta_flag: i32,
88    /// `m_Type`.
89    pub ty: Vec<u8>,
90    /// `m_Name`.
91    pub name: Vec<u8>,
92}
93
94impl Row {
95    /// A row of `level`, `ty` and `name`, with meta flag `meta_flag`.
96    #[must_use]
97    pub fn new(level: u8, ty: &[u8], name: &[u8], meta_flag: i32) -> Self {
98        Self {
99            level,
100            meta_flag,
101            ty: ty.to_vec(),
102            name: name.to_vec(),
103        }
104    }
105}
106
107/// A ref type: its class, namespace and assembly, and its tree, if stored.
108#[derive(Clone, Debug, Default, PartialEq, Eq)]
109pub struct RefType {
110    /// `m_ClassName`.
111    pub class: Vec<u8>,
112    /// `m_NameSpace`.
113    pub namespace: Vec<u8>,
114    /// `m_AssemblyName`.
115    pub assembly: Vec<u8>,
116    /// The stored tree; `None` is a ref type with none.
117    pub rows: Option<Vec<Row>>,
118}
119
120/// A tree, the ref types it may reach, and the bytes of one value.
121///
122/// Layout: `flags` (a byte of [`TreeCase`]'s bits), `start` (an `i8`), the
123/// rows, the count of ref types (a byte) and each ref type, then the value's
124/// bytes, which run to the end. Rows are a `u16` count (little-endian) and
125/// each row's level (a byte), meta flag (an `i32`, little-endian), type and
126/// name; a string is a byte of length and its bytes. A ref type is its
127/// class, namespace and assembly, a byte whose low bit says a tree follows,
128/// and that tree's rows.
129#[derive(Clone, Debug, Default, PartialEq, Eq)]
130pub struct TreeCase {
131    /// The bits below.
132    pub flags: u8,
133    /// Where the pure-Python reader starts, from the start of the data.
134    pub start: i8,
135    /// The value's tree.
136    pub rows: Vec<Row>,
137    /// The file's ref types, in order.
138    pub refs: Vec<RefType>,
139    /// The value's bytes.
140    pub data: Vec<u8>,
141}
142
143impl TreeCase {
144    /// The data is big-endian.
145    pub const BIG_ENDIAN: u8 = 1;
146    /// The file has no `ref_types` (format below 20), whatever `refs` holds.
147    pub const REFS_ABSENT: u8 = 2;
148    /// There is no file at all, whatever `refs` holds (wins over
149    /// [`Self::REFS_ABSENT`]).
150    pub const REFS_NO_FILE: u8 = 4;
151    /// The pure-Python reader reads by the stream reader's rules (a file
152    /// loaded from a path).
153    pub const STREAM: u8 = 8;
154    /// Float32 signalling NaNs keep their bits.
155    pub const LOSSLESS: u8 = 16;
156    /// `write_after_read` reads with the pure-Python reader, not
157    /// UnityPyBoost's.
158    pub const PYTHON: u8 = 32;
159    /// The run bounds memory at the fuzz crate's small bound
160    /// (`SMALL_MEMORY` in `fuzz/src/lib.rs`), which a read of many small
161    /// values reaches before the work bound, not at the large one, which
162    /// no value read reaches. Without it the code that grows a charged
163    /// buffer at the memory bound never runs in the value targets.
164    pub const SMALL_MEMORY: u8 = 64;
165    /// With [`Self::STREAM`], the stream reader is backed by `io.BytesIO`,
166    /// where a negative read size reads to the end.
167    pub const BYTES_IO: u8 = 128;
168
169    /// Whether `flags` holds `bit`.
170    #[must_use]
171    pub fn has(&self, bit: u8) -> bool {
172        self.flags & bit != 0
173    }
174
175    /// The case's bytes.
176    #[must_use]
177    pub fn encode(&self) -> Vec<u8> {
178        let mut out = vec![self.flags, self.start.to_le_bytes()[0]];
179        put_rows(&mut out, &self.rows);
180        let refs = &self.refs[..self.refs.len().min(usize::from(u8::MAX))];
181        out.push(u8::try_from(refs.len()).unwrap_or(u8::MAX));
182        for r in refs {
183            put_str(&mut out, &r.class);
184            put_str(&mut out, &r.namespace);
185            put_str(&mut out, &r.assembly);
186            match &r.rows {
187                Some(rows) => {
188                    out.push(1);
189                    put_rows(&mut out, rows);
190                }
191                None => out.push(0),
192            }
193        }
194        out.extend_from_slice(&self.data);
195        out
196    }
197
198    /// The case `input` holds; what it does not reach is empty.
199    #[must_use]
200    pub fn decode(input: &[u8]) -> Self {
201        let mut c = Cursor(input);
202        let mut case = Self {
203            flags: c.u8().unwrap_or(0),
204            start: c.u8().map_or(0, |b| i8::from_le_bytes([b])),
205            ..Self::default()
206        };
207        let Some(rows) = c.rows() else {
208            return case;
209        };
210        case.rows = rows;
211        let Some(n) = c.u8() else {
212            return case;
213        };
214        for _ in 0..n {
215            let Some(r) = c.ref_type() else {
216                return case;
217            };
218            case.refs.push(r);
219        }
220        case.data = c.0.to_vec();
221        case
222    }
223}
224
225/// The `typetree` target's input: the format (a byte), a byte whose low bit
226/// says big-endian, then the stored tree.
227#[must_use]
228pub fn blob_case(format: u8, big_endian: bool, tree: &[u8]) -> Vec<u8> {
229    let mut out = vec![format, u8::from(big_endian)];
230    out.extend_from_slice(tree);
231    out
232}
233
234/// The `lz4` target's input: the declared size (a `u32`, little-endian),
235/// then the block.
236#[must_use]
237pub fn lz4_case(declared: u32, block: &[u8]) -> Vec<u8> {
238    let mut out = declared.to_le_bytes().to_vec();
239    out.extend_from_slice(block);
240    out
241}
242
243/// A `Texture2D`'s fields, the meter its decode runs under and its data,
244/// for the `texture` target (`unityrs-texture`'s `select` and `decode`).
245///
246/// Layout: `m_TextureFormat`, `m_Width`, `m_Height` and the platform, each
247/// an `i32`, little-endian; the meter (a byte); the platform blob, a byte of
248/// length ([`Self::NO_BLOB`] for none) and its bytes; then the data, which
249/// runs to the end. An input that stops inside the header is the default
250/// case, and one that stops inside the blob keeps the header alone.
251#[derive(Clone, Debug, Default, PartialEq, Eq)]
252pub struct TextureCase {
253    /// `m_TextureFormat`.
254    pub format: i32,
255    /// `m_Width`.
256    pub width: i32,
257    /// `m_Height`.
258    pub height: i32,
259    /// The platform of the file that holds it (a `BuildTarget`).
260    pub platform: i32,
261    /// The meter, by its low two bits: 0 is the fuzz memory bound; 1, 2
262    /// and 3 the decode's charge less 1, the charge, and the charge plus 1.
263    pub meter: u8,
264    /// `m_PlatformBlob`, at most 254 bytes, or none.
265    pub blob: Option<Vec<u8>>,
266    /// The image data, mip 0 first.
267    pub data: Vec<u8>,
268}
269
270impl TextureCase {
271    /// The blob length that says there is no blob.
272    pub const NO_BLOB: u8 = u8::MAX;
273
274    /// The case's bytes.
275    #[must_use]
276    pub fn encode(&self) -> Vec<u8> {
277        let mut out = Vec::new();
278        for v in [self.format, self.width, self.height, self.platform] {
279            out.extend_from_slice(&v.to_le_bytes());
280        }
281        out.push(self.meter);
282        match &self.blob {
283            Some(b) => {
284                let b = &b[..b.len().min(usize::from(Self::NO_BLOB - 1))];
285                out.push(u8::try_from(b.len()).unwrap_or(0));
286                out.extend_from_slice(b);
287            }
288            None => out.push(Self::NO_BLOB),
289        }
290        out.extend_from_slice(&self.data);
291        out
292    }
293
294    /// The case `input` holds; what it does not reach is empty.
295    #[must_use]
296    pub fn decode(input: &[u8]) -> Self {
297        let mut c = Cursor(input);
298        let (Some(format), Some(width), Some(height), Some(platform), Some(meter)) =
299            (c.i32(), c.i32(), c.i32(), c.i32(), c.u8())
300        else {
301            return Self::default();
302        };
303        let mut case = Self {
304            format,
305            width,
306            height,
307            platform,
308            meter,
309            ..Self::default()
310        };
311        case.blob = match c.u8() {
312            None => return case,
313            Some(Self::NO_BLOB) => None,
314            Some(n) => match c.take(usize::from(n)) {
315                Some(b) => Some(b.to_vec()),
316                None => return case,
317            },
318        };
319        case.data = c.0.to_vec();
320        case
321    }
322}
323
324fn put_str(out: &mut Vec<u8>, s: &[u8]) {
325    let s = &s[..s.len().min(usize::from(u8::MAX))];
326    out.push(u8::try_from(s.len()).unwrap_or(u8::MAX));
327    out.extend_from_slice(s);
328}
329
330fn put_rows(out: &mut Vec<u8>, rows: &[Row]) {
331    let rows = &rows[..rows.len().min(usize::from(u16::MAX))];
332    out.extend_from_slice(&u16::try_from(rows.len()).unwrap_or(u16::MAX).to_le_bytes());
333    for r in rows {
334        out.push(r.level);
335        out.extend_from_slice(&r.meta_flag.to_le_bytes());
336        put_str(out, &r.ty);
337        put_str(out, &r.name);
338    }
339}
340
341/// A Mesh's or a Sprite's value, the version and byte order its file has,
342/// the bytes its `m_StreamData` reads and the meter, for the `meshdecode`
343/// target (`unityrs-meshdecode`'s `MeshHandler`).
344///
345/// Layout: the flags (a byte of [`MeshCase`]'s bits), the meter (a byte),
346/// the version (major, minor and build, each a `u16`, little-endian, then
347/// the type and its number, a byte each), the resource (a `u16` of length,
348/// little-endian, and its bytes), then the value, a [`TreeCase`], which runs
349/// to the end. An input that stops inside the header is the default case,
350/// and one that stops inside the resource keeps the header alone.
351#[derive(Clone, Debug, Default, PartialEq, Eq)]
352pub struct MeshCase {
353    /// The bits below.
354    pub flags: u8,
355    /// The meter: 0 runs the handler under the case's limits alone; any
356    /// other value runs it again under a meter of `meter² × 64` bytes.
357    pub meter: u8,
358    /// The version's major, minor and build.
359    pub version: [u16; 3],
360    /// The version's type (a `UnityVersionType` byte, any value).
361    pub version_type: u8,
362    /// The number after the type.
363    pub version_number: u8,
364    /// What a resource read returns, at most 65,535 bytes.
365    pub resource: Vec<u8>,
366    /// The value's tree and bytes.
367    pub value: TreeCase,
368}
369
370impl MeshCase {
371    /// The value is a Sprite's (its `m_RD` is the source), not a Mesh's.
372    pub const SPRITE: u8 = 1;
373    /// The file is big-endian (the handler swaps the vertex data's bytes).
374    pub const BIG_ENDIAN: u8 = 2;
375    /// The handler has a resource reader (`MeshHandler::process`'s
376    /// `Resources`), returning [`Self::resource`].
377    pub const RESOURCES: u8 = 4;
378
379    /// Whether `flags` holds `bit`.
380    #[must_use]
381    pub fn has(&self, bit: u8) -> bool {
382        self.flags & bit != 0
383    }
384
385    /// The case's bytes.
386    #[must_use]
387    pub fn encode(&self) -> Vec<u8> {
388        let mut out = vec![self.flags, self.meter];
389        for v in self.version {
390            out.extend_from_slice(&v.to_le_bytes());
391        }
392        out.extend_from_slice(&[self.version_type, self.version_number]);
393        let r = &self.resource[..self.resource.len().min(usize::from(u16::MAX))];
394        out.extend_from_slice(&u16::try_from(r.len()).unwrap_or(u16::MAX).to_le_bytes());
395        out.extend_from_slice(r);
396        out.extend_from_slice(&self.value.encode());
397        out
398    }
399
400    /// The case `input` holds; what it does not reach is empty.
401    #[must_use]
402    pub fn decode(input: &[u8]) -> Self {
403        let mut c = Cursor(input);
404        let (Some(flags), Some(meter), Some(major), Some(minor), Some(build)) =
405            (c.u8(), c.u8(), c.u16(), c.u16(), c.u16())
406        else {
407            return Self::default();
408        };
409        let (Some(version_type), Some(version_number)) = (c.u8(), c.u8()) else {
410            return Self::default();
411        };
412        let mut case = Self {
413            flags,
414            meter,
415            version: [major, minor, build],
416            version_type,
417            version_number,
418            ..Self::default()
419        };
420        let Some(resource) = c.u16().and_then(|n| c.take(usize::from(n))) else {
421            return case;
422        };
423        case.resource = resource.to_vec();
424        case.value = TreeCase::decode(c.0);
425        case
426    }
427}
428
429/// A shader program blob and the Unity version selecting its entry table.
430///
431/// Layout: major and minor version, each a little-endian `u32`, the meter
432/// selector (one byte), then the blob through the end. A header cut short
433/// decodes to the default case.
434#[derive(Clone, Debug, Default, PartialEq, Eq)]
435pub struct ShaderCase {
436    /// Unity's major and minor version (the table changes at 2019.3).
437    pub version: (u32, u32),
438    /// 0 uses the fuzz memory bound; another value uses `meter² × 64` bytes.
439    pub meter: u8,
440    /// The little-endian shader program blob.
441    pub blob: Vec<u8>,
442}
443
444impl ShaderCase {
445    /// The case's bytes.
446    #[must_use]
447    pub fn encode(&self) -> Vec<u8> {
448        let mut out = self.version.0.to_le_bytes().to_vec();
449        out.extend_from_slice(&self.version.1.to_le_bytes());
450        out.push(self.meter);
451        out.extend_from_slice(&self.blob);
452        out
453    }
454
455    /// The case `input` holds; a missing header is the default case.
456    #[must_use]
457    pub fn decode(input: &[u8]) -> Self {
458        let mut c = Cursor(input);
459        let (Some(major), Some(minor), Some(meter)) = (c.u32(), c.u32(), c.u8()) else {
460            return Self::default();
461        };
462        Self {
463            version: (major, minor),
464            meter,
465            blob: c.0.to_vec(),
466        }
467    }
468}
469
470/// The bytes not yet read.
471struct Cursor<'a>(&'a [u8]);
472
473impl Cursor<'_> {
474    fn take(&mut self, n: usize) -> Option<&[u8]> {
475        let (head, rest) = self.0.split_at_checked(n)?;
476        self.0 = rest;
477        Some(head)
478    }
479
480    fn u8(&mut self) -> Option<u8> {
481        self.take(1)?.first().copied()
482    }
483
484    fn u16(&mut self) -> Option<u16> {
485        Some(u16::from_le_bytes(self.take(2)?.try_into().ok()?))
486    }
487
488    fn i32(&mut self) -> Option<i32> {
489        Some(i32::from_le_bytes(self.take(4)?.try_into().ok()?))
490    }
491
492    fn u32(&mut self) -> Option<u32> {
493        Some(u32::from_le_bytes(self.take(4)?.try_into().ok()?))
494    }
495
496    fn str(&mut self) -> Option<Vec<u8>> {
497        let n = self.u8()?;
498        Some(self.take(usize::from(n))?.to_vec())
499    }
500
501    fn rows(&mut self) -> Option<Vec<Row>> {
502        let n = u16::from_le_bytes(self.take(2)?.try_into().ok()?);
503        let mut rows = Vec::new();
504        for _ in 0..n {
505            let level = self.u8()?;
506            let meta_flag = i32::from_le_bytes(self.take(4)?.try_into().ok()?);
507            let ty = self.str()?;
508            let name = self.str()?;
509            rows.push(Row {
510                level,
511                meta_flag,
512                ty,
513                name,
514            });
515        }
516        Some(rows)
517    }
518
519    fn ref_type(&mut self) -> Option<RefType> {
520        let class = self.str()?;
521        let namespace = self.str()?;
522        let assembly = self.str()?;
523        let rows = if self.u8()? & 1 == 1 {
524            Some(self.rows()?)
525        } else {
526            None
527        };
528        Some(RefType {
529            class,
530            namespace,
531            assembly,
532            rows,
533        })
534    }
535}
536
537#[cfg(test)]
538mod tests {
539    use super::{MeshCase, RefType, Row, TextureCase, TreeCase, blob_case, lz4_case};
540
541    fn case() -> TreeCase {
542        TreeCase {
543            flags: TreeCase::BIG_ENDIAN | TreeCase::PYTHON,
544            start: -3,
545            rows: vec![
546                Row::new(0, b"Base", b"Base", 0),
547                Row::new(1, b"int", b"x", 0x4000),
548            ],
549            refs: vec![
550                RefType {
551                    class: b"A".to_vec(),
552                    namespace: b"n".to_vec(),
553                    assembly: b"a".to_vec(),
554                    rows: Some(vec![Row::new(0, b"A", b"Base", 0)]),
555                },
556                RefType::default(),
557            ],
558            data: vec![1, 2, 3, 4],
559        }
560    }
561
562    #[test]
563    fn a_case_decodes_to_what_was_encoded() {
564        let c = case();
565        assert_eq!(TreeCase::decode(&c.encode()), c);
566        assert!(c.has(TreeCase::PYTHON) && !c.has(TreeCase::STREAM));
567    }
568
569    #[test]
570    fn a_cut_case_keeps_what_it_reaches_and_empties_the_rest() {
571        let c = case();
572        let bytes = c.encode();
573        // Every cut decodes, and past the rows and the ref types only the
574        // data is cut.
575        for n in 0..bytes.len() {
576            let d = TreeCase::decode(&bytes[..n]);
577            assert!(d.data.len() <= c.data.len());
578        }
579        let cut = TreeCase::decode(&bytes[..bytes.len() - 1]);
580        assert_eq!((cut.rows, cut.refs), (c.rows.clone(), c.refs.clone()));
581        assert_eq!(cut.data, [1, 2, 3]);
582        let rows_only = TreeCase::decode(&bytes[..bytes.len() - 20]);
583        assert_eq!(rows_only.rows, c.rows);
584        assert!(rows_only.refs.is_empty() && rows_only.data.is_empty());
585        assert_eq!(TreeCase::decode(&[]), TreeCase::default());
586    }
587
588    #[test]
589    fn long_strings_and_lists_are_cut_to_what_their_lengths_hold() {
590        let c = TreeCase {
591            rows: vec![Row::new(0, &[b'x'; 300], b"Base", 0)],
592            ..TreeCase::default()
593        };
594        let d = TreeCase::decode(&c.encode());
595        assert_eq!(d.rows[0].ty.len(), 255);
596        assert_eq!(d.rows[0].name, b"Base");
597    }
598
599    #[test]
600    fn a_texture_case_decodes_to_what_was_encoded_and_a_cut_one_to_less() {
601        let c = TextureCase {
602            format: 25,
603            width: -3,
604            height: 16384,
605            platform: 38,
606            meter: 2,
607            blob: Some(vec![0; 12]),
608            data: vec![1, 2, 3],
609        };
610        let bytes = c.encode();
611        assert_eq!(bytes.len(), 17 + 1 + 12 + 3);
612        assert_eq!(TextureCase::decode(&bytes), c);
613        let none = TextureCase {
614            blob: None,
615            ..c.clone()
616        };
617        assert_eq!(TextureCase::decode(&none.encode()), none);
618        // Cut in the header: the default; in the blob: the header alone.
619        assert_eq!(TextureCase::decode(&bytes[..16]), TextureCase::default());
620        let header = TextureCase::decode(&bytes[..20]);
621        assert_eq!(
622            (header.width, header.blob, header.data),
623            (-3, None, Vec::new())
624        );
625        assert_eq!(TextureCase::decode(&bytes[..31]).data, [1]);
626    }
627
628    #[test]
629    fn a_mesh_case_decodes_to_what_was_encoded_and_a_cut_one_to_less() {
630        let c = MeshCase {
631            flags: MeshCase::SPRITE | MeshCase::RESOURCES,
632            meter: 9,
633            version: [2019, 4, 40],
634            version_type: 3,
635            version_number: 1,
636            resource: vec![5; 3],
637            value: case(),
638        };
639        let bytes = c.encode();
640        assert_eq!(bytes.len(), 10 + 2 + 3 + c.value.encode().len());
641        assert_eq!(MeshCase::decode(&bytes), c);
642        assert!(c.has(MeshCase::SPRITE) && !c.has(MeshCase::BIG_ENDIAN));
643        // Cut in the header: the default; in the resource: the header alone;
644        // in the value: the value as far as it reaches.
645        assert_eq!(MeshCase::decode(&bytes[..9]), MeshCase::default());
646        let header = MeshCase::decode(&bytes[..13]);
647        assert_eq!(
648            (header.version, header.resource, header.value),
649            ([2019, 4, 40], Vec::new(), TreeCase::default())
650        );
651        let cut = MeshCase::decode(&bytes[..bytes.len() - 1]);
652        assert_eq!((cut.resource, cut.value.data), (vec![5; 3], vec![1, 2, 3]));
653    }
654
655    #[test]
656    fn the_blob_and_lz4_cases_put_their_header_first() {
657        assert_eq!(blob_case(19, true, &[9]), [19, 1, 9]);
658        assert_eq!(lz4_case(0x0102_0304, &[7]), [4, 3, 2, 1, 7]);
659    }
660}