libsoliton/soliton/fuzz/fuzz_targets/fuzz_ratchet_roundtrip.rs
Kamal Tufekcic 1d99048c95
Some checks failed
CI / lint (push) Successful in 1m37s
CI / test-python (push) Successful in 1m49s
CI / test-zig (push) Successful in 1m39s
CI / test-wasm (push) Successful in 1m54s
CI / test (push) Successful in 14m44s
CI / miri (push) Successful in 14m18s
CI / build (push) Successful in 1m9s
CI / fuzz-regression (push) Successful in 9m9s
CI / publish (push) Failing after 1m10s
CI / publish-python (push) Failing after 1m46s
CI / publish-wasm (push) Has been cancelled
initial commit
Signed-off-by: Kamal Tufekcic <kamal@lo.sh>
2026-04-02 23:48:10 +03:00

32 lines
1.5 KiB
Rust

#![no_main]
#![allow(deprecated)] // Fuzz targets exercise from_bytes directly.
use libfuzzer_sys::fuzz_target;
use soliton::ratchet::RatchetState;
fuzz_target!(|data: &[u8]| {
// from_bytes → to_bytes → from_bytes round-trip:
// any serializable state must re-serialize to identical bytes
// (except the epoch field at bytes 1..9, which increments on each to_bytes).
//
// States near the epoch ceiling are legitimately non-serializable
// (can_serialize() returns false). These are usable for encrypt/decrypt
// but not persistable — skip them.
let Ok(state1) = RatchetState::from_bytes(data) else { return; };
if !state1.can_serialize() { return; }
let Ok((bytes1, epoch1)) = state1.to_bytes() else {
panic!("to_bytes failed on a state where can_serialize() returned true");
};
let Ok(state2) = RatchetState::from_bytes(&bytes1) else {
panic!("from_bytes rejected bytes produced by to_bytes");
};
if !state2.can_serialize() { return; }
let Ok((bytes2, epoch2)) = state2.to_bytes() else {
panic!("second to_bytes failed on a serializable round-tripped state");
};
// Version byte must match.
assert_eq!(bytes1[0], bytes2[0], "version byte mismatch after round-trip");
// All fields after epoch must match.
assert_eq!(bytes1[9..], bytes2[9..], "fields after epoch diverged after round-trip");
// Epoch must advance by exactly 1 per to_bytes call.
assert_eq!(epoch2, epoch1 + 1, "epoch did not advance by 1");
});