Skip to content

Commit

Permalink
add fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Sep 16, 2024
1 parent 8a6e812 commit c80b8e3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ debug = true

[workspace.dependencies]
jiter = { path = "crates/jiter", version = "0.5.0" }
batson = { path = "crates/batson", version = "0.5.0" }
pyo3 = { version = "0.22.0" }
pyo3-build-config = { version = "0.22.0" }
bencher = "0.1.5"
Expand Down
9 changes: 8 additions & 1 deletion crates/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ serde = "1.0.190"
indexmap = "2.0.0"
num-bigint = "0.4.4"
num-traits = "0.2.17"
jiter = {path = "../jiter"}
jiter = {workspace = true}
batson = {workspace = true}

[[bin]]
name = "compare_to_serde"
Expand All @@ -28,3 +29,9 @@ name = "compare_skip"
path = "fuzz_targets/compare_skip.rs"
test = false
doc = false

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

use batson::{batson_to_json_string, encode_from_json};
use jiter::JsonValue;

use libfuzzer_sys::fuzz_target;

fn round_trip(json: String) {
let Ok(jiter_value1) = JsonValue::parse(json.as_bytes(), false) else {
return;
};
let bytes1 = encode_from_json(&jiter_value1).unwrap();
let json1 = batson_to_json_string(&bytes1).unwrap();

let jiter_value2 = JsonValue::parse(json1.as_bytes(), false).unwrap();
let bytes2 = encode_from_json(&jiter_value2).unwrap();
let json2 = batson_to_json_string(&bytes2).unwrap();

assert_eq!(json1, json2);
}

fuzz_target!(|json: String| {
round_trip(json);
});

0 comments on commit c80b8e3

Please sign in to comment.