|
| 1 | +// Robustness / smoke-fuzz suite that runs under plain `cargo test` (no nightly). |
| 2 | +// It hammers the decoders with arbitrary, mostly-malformed input to prove they |
| 3 | +// are *total* (never panic, only Ok/Err) and that the decode variants agree. |
| 4 | +// Deeper coverage lives in `fuzz/` (cargo-fuzz). |
| 5 | +#![allow(clippy::pedantic, missing_docs)] |
| 6 | + |
| 7 | +use cobs_codec_rs::{cobs, cobsr}; |
| 8 | + |
| 9 | +/// Tiny deterministic xorshift PRNG (keeps the crate dependency-free). |
| 10 | +struct Rng(u64); |
| 11 | +impl Rng { |
| 12 | + fn new(seed: u64) -> Self { |
| 13 | + Self(seed) |
| 14 | + } |
| 15 | + fn next(&mut self) -> u64 { |
| 16 | + let mut x = self.0; |
| 17 | + x ^= x << 13; |
| 18 | + x ^= x >> 7; |
| 19 | + x ^= x << 17; |
| 20 | + self.0 = x; |
| 21 | + x |
| 22 | + } |
| 23 | + fn byte(&mut self) -> u8 { |
| 24 | + (self.next() & 0xFF) as u8 |
| 25 | + } |
| 26 | + // A byte biased toward the values that stress COBS code bytes: 0x00, 0xFF, |
| 27 | + // and small counts, with the occasional fully-random byte. |
| 28 | + fn stress_byte(&mut self) -> u8 { |
| 29 | + match self.next() % 4 { |
| 30 | + 0 => 0x00, |
| 31 | + 1 => 0xFF, |
| 32 | + 2 => (self.next() % 6) as u8, |
| 33 | + _ => self.byte(), |
| 34 | + } |
| 35 | + } |
| 36 | + fn stress_bytes(&mut self, len: usize) -> Vec<u8> { |
| 37 | + (0..len).map(|_| self.stress_byte()).collect() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[test] |
| 42 | +fn decode_is_total_on_arbitrary_input() { |
| 43 | + // Arbitrary (mostly invalid) bytes must never panic, and basic-COBS decode |
| 44 | + // must agree with in-place decode on both the accept/reject decision and the |
| 45 | + // decoded bytes. |
| 46 | + let mut rng = Rng::new(0xF0FA_D00D_1111_2222); |
| 47 | + let mut scratch = vec![0u8; 1024]; |
| 48 | + for _ in 0..200_000 { |
| 49 | + let len = (rng.next() as usize) % 512; |
| 50 | + let input = rng.stress_bytes(len); |
| 51 | + |
| 52 | + // COBS/R decode must also never panic. |
| 53 | + let _ = cobsr::decode(&input, &mut scratch); |
| 54 | + // A custom sentinel decode path must never panic either. |
| 55 | + let s = rng.byte(); |
| 56 | + let _ = cobs::decode_with_sentinel(&input, &mut scratch, s); |
| 57 | + |
| 58 | + // Slice decode vs in-place decode must always agree. |
| 59 | + let slice = cobs::decode(&input, &mut scratch); |
| 60 | + let mut buf = input.clone(); |
| 61 | + let in_place = cobs::decode_in_place(&mut buf); |
| 62 | + match (slice, in_place) { |
| 63 | + (Ok(m), Ok(n)) => { |
| 64 | + assert_eq!(m, n, "length mismatch on {input:02x?}"); |
| 65 | + assert_eq!(&scratch[..m], &buf[..n], "byte mismatch on {input:02x?}"); |
| 66 | + } |
| 67 | + (Err(_), Err(_)) => {} |
| 68 | + (a, b) => panic!("slice/in-place disagree on {input:02x?}: {a:?} vs {b:?}"), |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn encode_decode_round_trips_arbitrary_payloads() { |
| 75 | + // Any payload round-trips through encode -> decode for COBS, COBS/R, and the |
| 76 | + // sentinel variants; the encoding never contains the (sentinel) delimiter. |
| 77 | + let mut rng = Rng::new(0xC0FF_EE00_3333_4444); |
| 78 | + let mut enc = vec![0u8; 8192]; |
| 79 | + let mut dec = vec![0u8; 8192]; |
| 80 | + for _ in 0..100_000 { |
| 81 | + let len = (rng.next() as usize) % 700; |
| 82 | + let src = rng.stress_bytes(len); |
| 83 | + |
| 84 | + let n = cobs::encode(&src, &mut enc); |
| 85 | + assert!(!enc[..n].contains(&0), "COBS output contains 0x00"); |
| 86 | + let m = cobs::decode(&enc[..n], &mut dec).unwrap(); |
| 87 | + assert_eq!(&dec[..m], &src[..]); |
| 88 | + |
| 89 | + let n = cobsr::encode(&src, &mut enc); |
| 90 | + assert!(!enc[..n].contains(&0), "COBS/R output contains 0x00"); |
| 91 | + let m = cobsr::decode(&enc[..n], &mut dec).unwrap(); |
| 92 | + assert_eq!(&dec[..m], &src[..]); |
| 93 | + |
| 94 | + let s = rng.byte(); |
| 95 | + let n = cobs::encode_with_sentinel(&src, &mut enc, s); |
| 96 | + if s != 0 { |
| 97 | + assert!(!enc[..n].contains(&s), "sentinel byte {s:#04x} in output"); |
| 98 | + } |
| 99 | + let m = cobs::decode_with_sentinel(&enc[..n], &mut dec, s).unwrap(); |
| 100 | + assert_eq!(&dec[..m], &src[..]); |
| 101 | + } |
| 102 | +} |
0 commit comments