-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(fuzz): Add
parse_duration
fuzzing target
Add a fuzzing target for parsing into a Duration.
- Loading branch information
1 parent
e773f5f
commit a43c30e
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#![no_main] | ||
|
||
use libfuzzer_sys::fuzz_target; | ||
|
||
fuzz_target!(|data: &[u8]| test_parse_duration(data)); | ||
|
||
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) = prost_types::Duration::from_str(original_text) else { | ||
if original_text.ends_with("s") { | ||
assert!( | ||
original_text.parse::<f64>().is_err(), | ||
"prost 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()); | ||
} |