From 3c17d6df4901904f71c35a38a88adfacf43daf5c Mon Sep 17 00:00:00 2001 From: Noah Hellman Date: Mon, 10 Apr 2023 13:23:44 +0200 Subject: [PATCH] afl: add target parse_balance verify all containers are balanced --- .github/workflows/ci.yml | 1 + tests/afl/Cargo.toml | 4 ++++ tests/afl/src/lib.rs | 15 +++++++++++++++ tests/afl/src/main.rs | 1 + tests/afl/src/parse_balance.rs | 3 +++ 5 files changed, 24 insertions(+) create mode 100644 tests/afl/src/parse_balance.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2ae1c5a..d02c622 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,6 +72,7 @@ jobs: matrix: target: - parse + - parse_balance - html runs-on: ubuntu-latest steps: diff --git a/tests/afl/Cargo.toml b/tests/afl/Cargo.toml index 70e156b..66b25e1 100644 --- a/tests/afl/Cargo.toml +++ b/tests/afl/Cargo.toml @@ -17,6 +17,10 @@ path = "src/main.rs" name = "parse" path = "src/parse.rs" +[[bin]] +name = "parse_balance" +path = "src/parse_balance.rs" + [[bin]] name = "html" path = "src/html.rs" diff --git a/tests/afl/src/lib.rs b/tests/afl/src/lib.rs index adbca14..0591238 100644 --- a/tests/afl/src/lib.rs +++ b/tests/afl/src/lib.rs @@ -11,6 +11,21 @@ pub fn parse(data: &[u8]) { } } +/// Ensure containers are always balanced, i.e. opened and closed in correct order. +pub fn parse_balance(data: &[u8]) { + if let Ok(s) = std::str::from_utf8(data) { + let mut open = Vec::new(); + for event in jotdown::Parser::new(s) { + match event { + jotdown::Event::Start(c, ..) => open.push(c.clone()), + jotdown::Event::End(c) => assert_eq!(open.pop().unwrap(), c), + _ => {} + } + } + assert_eq!(open, &[]); + } +} + pub fn html(data: &[u8]) { if data.iter().any(|i| *i == 0) { return; diff --git a/tests/afl/src/main.rs b/tests/afl/src/main.rs index b0a66c8..ad09a2d 100644 --- a/tests/afl/src/main.rs +++ b/tests/afl/src/main.rs @@ -8,6 +8,7 @@ fn main() { let f = match target.as_str() { "parse" => jotdown_afl::parse, + "parse_balance" => jotdown_afl::parse_balance, "html" => jotdown_afl::html, _ => panic!("unknown target '{}'", target), }; diff --git a/tests/afl/src/parse_balance.rs b/tests/afl/src/parse_balance.rs new file mode 100644 index 0000000..9118fb2 --- /dev/null +++ b/tests/afl/src/parse_balance.rs @@ -0,0 +1,3 @@ +fn main() { + afl::fuzz!(|data: &[u8]| { jotdown_afl::parse_balance(data) }); +}