Skip to content
Merged
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
1 change: 1 addition & 0 deletions cedar-drt/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ rayon = { version = "1.5", optional = true }
rand = { version = "0.8", optional = true }
clap = { version = "4.0", features = ["derive"], optional = true }
rand_chacha = { version = "0.3", optional = true }
similar-asserts = "1.5.0"

[dependencies.uuid]
version = "1.3.1"
Expand Down
26 changes: 23 additions & 3 deletions cedar-drt/fuzz/fuzz_targets/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use cedar_policy_generators::{
use libfuzzer_sys::arbitrary::{self, Arbitrary, Unstructured};
use log::debug;
use serde::Serialize;
use similar_asserts::SimpleDiff;
use smol_str::SmolStr;
use std::collections::HashMap;
use uuid::Uuid;
Expand Down Expand Up @@ -104,12 +105,31 @@ fn round_trip(p: &StaticPolicy) -> Result<StaticPolicy, parser::err::ParseErrors
line_width: 80,
};
let mut uuids = Vec::new();
let commented = attach_comment(&p.to_string(), &mut uuids);
let formatted_policy_str =
&policies_str_to_pretty(&attach_comment(&p.to_string(), &mut uuids), &config)
.expect("pretty-printing should not fail");
&policies_str_to_pretty(&commented, &config).expect("pretty-printing should not fail");
// check if pretty-printing drops any comment
let mut formatted_policy_tail: &str = formatted_policy_str.as_str();
for u in &uuids {
assert!(formatted_policy_str.contains(u), "missing comment: {}\n", u);
match formatted_policy_tail.split_once(u) {
Some((_, after_uuid)) => formatted_policy_tail = after_uuid,
None => {
Copy link
Contributor

@shaobo-he-aws shaobo-he-aws May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove the println?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's printed only once we know the test case is an error, so no reason to remove it, and it makes debugging failures much easier

println!(
"{}",
SimpleDiff::from_str(
&commented,
formatted_policy_str,
"Commented Policy",
"Commented and formatted"
)
);
if formatted_policy_str.contains(u) {
panic!("It looks like we skipped over comment `{u}`. The formatter might have reordered the comment")
} else {
panic!("Failed to find comment `{u}` anywhere. The formatter might have dropped the comment")
}
}
}
}
parse_policy(None, formatted_policy_str)
}
Expand Down