Skip to content

Commit

Permalink
Add parse_duration fuzzing (via Casper Meijn)
Browse files Browse the repository at this point in the history
  • Loading branch information
mumbleskates committed Oct 23, 2024
1 parent b23b807 commit c4a0c43
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
26 changes: 26 additions & 0 deletions fuzz/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ pub fn test_parse_date(data: &[u8]) {
);
}

pub fn test_parse_duration(data: &[u8]) {
use std::str::from_utf8;
use std::str::FromStr;

// input must be text
let Ok(original_text) = from_utf8(data) else {
return;
};

// parse input as a duration
let Ok(duration) = bilrost_types::Duration::from_str(original_text) else {
if original_text.ends_with("s") {
assert!(
original_text.parse::<f64>().is_err(),
"bilrost failed to parse duration, but it seems to be a valid number: {}",
original_text
);
}
return;
};

// roundtrip to and from string
let roundtrip_text = format!("{duration}");
assert_eq!(Ok(&duration), roundtrip_text.parse().as_ref());
}

enum RoundtripResult {
/// The roundtrip succeeded.
Ok(Vec<u8>),
Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzzers/parse_date_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![no_main]

use common::test_parse_date;
use common::{test_parse_date, test_parse_duration};
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
test_parse_date(data);
test_parse_duration(data);
});

0 comments on commit c4a0c43

Please sign in to comment.