Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add round-trip fuzz test #55

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ clippy: ## run clippy
journeytests: ## run journey tests
./tests/cat.sh

fuzz: ## run fuzz tests for 30 seconds
cargo install cargo-fuzz
cargo fuzz run round-trip -- -only_ascii=1 -max_total_time=30

tests: docs clippy unittests journeytests ## run all tests
tests: docs clippy unittests journeytests fuzz ## run all tests
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
104 changes: 104 additions & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "pulldown-cmark-to-cmark-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
pulldown-cmark = { version = "0.9.0", default-features = false }

[dependencies.pulldown-cmark-to-cmark]
path = ".."

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[profile.release]
debug = 1

[[bin]]
name = "round-trip"
path = "fuzz_targets/round-trip.rs"
test = false
doc = false
42 changes: 42 additions & 0 deletions fuzz/fuzz_targets/round-trip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use pulldown_cmark::Parser;
use pulldown_cmark_to_cmark::cmark;

fn round_trip(text: &str) -> String {
let mut result = String::with_capacity(text.len());
cmark(Parser::new(&text), &mut result).unwrap();
result
}

fn print_events(text: &str) {
eprintln!("{text:?} -> [");
for event in Parser::new(&text) {
eprintln!(" {event:?}");
}
eprintln!("]");
}

fuzz_target!(|text: String| {
let round_trip_1 = round_trip(&text);
let round_trip_2 = round_trip(&round_trip_1);
let round_trip_3 = round_trip(&round_trip_2);
let round_trip_4 = round_trip(&round_trip_3);
if round_trip_3 != round_trip_4 {
print_events(&text);
print_events(&round_trip_1);
print_events(&round_trip_2);
print_events(&round_trip_3);
print_events(&round_trip_4);

panic!(
"round-trip failed:\n\
-- {text:?}\n\
-> {round_trip_1:?}\n\
-> {round_trip_2:?}\n\
-> {round_trip_3:?}\n\
-> {round_trip_4:?}"
);
}
});