Skip to content

Commit

Permalink
Add encoding parsing fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Nov 15, 2022
1 parent bc93934 commit f67d42b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
17 changes: 17 additions & 0 deletions crates/objc2-encode/src/encoding_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,21 @@ mod tests {
assert!(ENC3C.equivalent_to_box(&enc1));
assert!(ENC3C.equivalent_to_box(&enc2), "now they're equivalent");
}

#[test]
fn parse_atomic_struct() {
let expected = EncodingBox::Atomic(Box::new(EncodingBox::Atomic(Box::new(
EncodingBox::Struct("a".into(), Some(Vec::new())),
))));
let actual = EncodingBox::from_str("AA{a=}").unwrap();
assert_eq!(expected, actual);
assert_eq!(expected.to_string(), "AA{a}");

let expected = EncodingBox::Atomic(Box::new(EncodingBox::Atomic(Box::new(
EncodingBox::Struct("a".into(), None),
))));
let actual = EncodingBox::from_str("AA{a}").unwrap();
assert_eq!(expected, actual);
assert_eq!(expected.to_string(), "AA{a}");
}
}
2 changes: 2 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target/
artifacts/
# This grows very quickly, and doesn't really need to be in-tree
corpus/encoding_parse/
10 changes: 8 additions & 2 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ gnustep-1-9 = ["gnustep-1-8", "objc2/gnustep-1-9"]
gnustep-2-0 = ["gnustep-1-9", "objc2/gnustep-2-0"]
gnustep-2-1 = ["gnustep-2-0", "objc2/gnustep-2-1"]

[workspace]
members = ["."]

[[bin]]
name = "class"
path = "fuzz_targets/class.rs"
Expand All @@ -34,5 +37,8 @@ path = "fuzz_targets/sel.rs"
test = false
doc = false

[workspace]
members = ["."]
[[bin]]
name = "encoding_parse"
path = "fuzz_targets/encoding_parse.rs"
test = false
doc = false
24 changes: 24 additions & 0 deletions fuzz/fuzz_targets/encoding_parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![no_main]
use std::str::FromStr;

use libfuzzer_sys::fuzz_target;
use objc2::encode::{Encoding, EncodingBox};

fuzz_target!(|s: &str| {
// Check that parsing encodings doesn't panic
if let Ok(enc) = EncodingBox::from_str(s) {
// Check a "negative" case of `equivalent_to_box`
if enc != EncodingBox::Char {
assert_ne!(EncodingBox::Char, enc, "not equal to char");
assert!(!Encoding::Char.equivalent_to_box(&enc), "not equivalent to char");
}

let s2 = enc.to_string();
// Note: s and s2 may not be equal!

// Test roundtrip
let enc2 = EncodingBox::from_str(&s2).expect("parsing valid encoding string");
let s3 = enc2.to_string();
assert_eq!(s2, s3);
}
});

0 comments on commit f67d42b

Please sign in to comment.