From 3fa3e8b9d016ee09a7bed154b751eff144e96565 Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Mon, 27 Jan 2025 11:03:24 +0100 Subject: [PATCH 1/4] Add report_duplicated_key and report_missing_keys functions to catalyst-types --- rust/catalyst-types/src/cbor_utils.rs | 34 +++++++++++++++++++++++++++ rust/catalyst-types/src/lib.rs | 1 + rust/cbork-utils/Cargo.toml | 5 +--- rust/cbork-utils/src/decode_helper.rs | 12 +++++----- 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 rust/catalyst-types/src/cbor_utils.rs diff --git a/rust/catalyst-types/src/cbor_utils.rs b/rust/catalyst-types/src/cbor_utils.rs new file mode 100644 index 0000000000..a648959205 --- /dev/null +++ b/rust/catalyst-types/src/cbor_utils.rs @@ -0,0 +1,34 @@ +//! CBOR utilities that are highly tied to Catalyst types, so they don't belong to the +//! `cbork-utils` crate. + +use std::fmt::Debug; + +use crate::problem_report::ProblemReport; + +/// Adds a "duplicated field" entry to the report and returns true if the field is already +/// present in the given found keys list. +pub fn report_duplicated_key( + found_keys: &[T], key: &T, index: u64, context: &str, report: &ProblemReport, +) -> bool { + if found_keys.contains(key) { + report.duplicate_field( + format!("{key:?}").as_str(), + format!("Redundant key found in item {} in RBAC map", index + 1).as_str(), + context, + ); + return true; + } + false +} + +/// Adds a "missing field" entry to the report for every required key that isn't present +/// in the found keys list. +pub fn report_missing_keys( + found_keys: &[T], required_keys: &[T], context: &str, report: &ProblemReport, +) { + for key in required_keys { + if !found_keys.contains(key) { + report.missing_field(&format!("{key:?}"), context); + } + } +} diff --git a/rust/catalyst-types/src/lib.rs b/rust/catalyst-types/src/lib.rs index 4802ce378a..e3862bfd62 100644 --- a/rust/catalyst-types/src/lib.rs +++ b/rust/catalyst-types/src/lib.rs @@ -1,5 +1,6 @@ //! Catalyst Generic Types +pub mod cbor_utils; pub mod conversion; pub mod hashes; pub mod id_uri; diff --git a/rust/cbork-utils/Cargo.toml b/rust/cbork-utils/Cargo.toml index 87bf9aced8..052f131924 100644 --- a/rust/cbork-utils/Cargo.toml +++ b/rust/cbork-utils/Cargo.toml @@ -14,7 +14,4 @@ workspace = true minicbor = { version = "0.25.1", features = ["std"] } [dev-dependencies] -proptest = { version = "1.5.0" } -# Potentially it could be replaced with using `proptest::property_test` attribute macro, -# after this PR will be merged https://github.com/proptest-rs/proptest/pull/523 -test-strategy = "0.4.0" +proptest = { version = "1.6.0", features = ["attr-macro"] } diff --git a/rust/cbork-utils/src/decode_helper.rs b/rust/cbork-utils/src/decode_helper.rs index db7462de44..1a8ab480f1 100644 --- a/rust/cbork-utils/src/decode_helper.rs +++ b/rust/cbork-utils/src/decode_helper.rs @@ -95,11 +95,11 @@ pub fn decode_any<'d>(d: &mut Decoder<'d>, from: &str) -> Result<&'d [u8], decod #[cfg(test)] mod tests { use minicbor::Encoder; - use test_strategy::proptest; + use proptest::property_test; use super::*; - #[proptest] + #[property_test] fn test_decode_any_bytes(random_bytes: Vec) { let mut buf = Vec::new(); let mut e = Encoder::new(&mut buf); @@ -112,7 +112,7 @@ mod tests { assert_eq!(result, random_bytes); } - #[proptest] + #[property_test] fn test_decode_any_string(random_string: String) { let mut buf = Vec::new(); let mut e = Encoder::new(&mut buf); @@ -126,7 +126,7 @@ mod tests { assert_eq!(result, random_string); } - #[proptest] + #[property_test] fn test_decode_any_array(random_array: Vec) { // The array should contain a supported type let mut buf = Vec::new(); @@ -143,7 +143,7 @@ mod tests { assert_eq!(result, random_array.len() as u64); } - #[proptest] + #[property_test] fn test_decode_any_u32(random_u32: u32) { let mut buf = Vec::new(); let mut e = Encoder::new(&mut buf); @@ -157,7 +157,7 @@ mod tests { assert_eq!(result, random_u32); } - #[proptest] + #[property_test] fn test_decode_any_i32(random_i32: i32) { let mut buf = Vec::new(); let mut e = Encoder::new(&mut buf); From 371037f794348c789c5ed45f44a5f21c649bba1a Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Mon, 27 Jan 2025 12:24:53 +0100 Subject: [PATCH 2/4] Update rust/catalyst-types/src/cbor_utils.rs Co-authored-by: bkioshn <35752733+bkioshn@users.noreply.github.com> --- rust/catalyst-types/src/cbor_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/catalyst-types/src/cbor_utils.rs b/rust/catalyst-types/src/cbor_utils.rs index a648959205..f3d5862c9b 100644 --- a/rust/catalyst-types/src/cbor_utils.rs +++ b/rust/catalyst-types/src/cbor_utils.rs @@ -13,7 +13,7 @@ pub fn report_duplicated_key( if found_keys.contains(key) { report.duplicate_field( format!("{key:?}").as_str(), - format!("Redundant key found in item {} in RBAC map", index + 1).as_str(), + format!("Redundant key found in item {}, index + 1).as_str(), context, ); return true; From 6819aeb213c38279d303d9f31fd9a7ff9ed16e82 Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Mon, 27 Jan 2025 12:44:06 +0100 Subject: [PATCH 3/4] Revert changes --- rust/catalyst-types/src/cbor_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/catalyst-types/src/cbor_utils.rs b/rust/catalyst-types/src/cbor_utils.rs index f3d5862c9b..a648959205 100644 --- a/rust/catalyst-types/src/cbor_utils.rs +++ b/rust/catalyst-types/src/cbor_utils.rs @@ -13,7 +13,7 @@ pub fn report_duplicated_key( if found_keys.contains(key) { report.duplicate_field( format!("{key:?}").as_str(), - format!("Redundant key found in item {}, index + 1).as_str(), + format!("Redundant key found in item {} in RBAC map", index + 1).as_str(), context, ); return true; From 48678debc09855e41fc95cfe710e9b887497462b Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Mon, 27 Jan 2025 12:45:55 +0100 Subject: [PATCH 4/4] fix --- rust/catalyst-types/src/cbor_utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/catalyst-types/src/cbor_utils.rs b/rust/catalyst-types/src/cbor_utils.rs index a648959205..22d0dd9872 100644 --- a/rust/catalyst-types/src/cbor_utils.rs +++ b/rust/catalyst-types/src/cbor_utils.rs @@ -13,7 +13,7 @@ pub fn report_duplicated_key( if found_keys.contains(key) { report.duplicate_field( format!("{key:?}").as_str(), - format!("Redundant key found in item {} in RBAC map", index + 1).as_str(), + format!("Redundant key found in item {}", index + 1).as_str(), context, ); return true;