From 8d0de82d68ecb289100732ad9d9b38bfbbc8fafe Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Wed, 27 Dec 2023 11:27:45 -0500 Subject: [PATCH] More test cases for plain groups (#221) Specifically tests for support for #121 for plain groups. For multi arrays covered by #120 For single ones covered by #210 We are keeping #121 open for now as the case for single non-plain-groups e.g. `[uint]` is not covered. We've yet to see this used anywhere in Cardano so it's low priority to fix. --- static/serialization_preserve.rs | 4 +++ tests/core/input.cddl | 6 ++-- tests/core/tests.rs | 35 ++++++++++++++++++++- tests/deser_test | 2 +- tests/preserve-encodings/input.cddl | 8 ++++- tests/preserve-encodings/tests.rs | 49 +++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 6 deletions(-) diff --git a/static/serialization_preserve.rs b/static/serialization_preserve.rs index c90a77a..0e18d4a 100644 --- a/static/serialization_preserve.rs +++ b/static/serialization_preserve.rs @@ -11,6 +11,10 @@ impl CBORReadLen { } } + pub fn read(&self) -> u64 { + self.read + } + // Marks {n} values as being read, and if we go past the available definite length // given by the CBOR, we return an error. pub fn read_elems(&mut self, count: usize) -> Result<(), DeserializeFailure> { diff --git a/tests/core/input.cddl b/tests/core/input.cddl index eb9ed93..8374895 100644 --- a/tests/core/input.cddl +++ b/tests/core/input.cddl @@ -20,9 +20,9 @@ bar = { plain = (d: #6.23(uint), e: tagged_text) outer = [a: uint, b: plain, c: "some text"] ; @used_as_key plain_arrays = [ -; this is not supported right now. When single-element arrays are supported remove this. -; single: [plain], - multi: [*plain], + embedded: plain, + single: [plain], + multi: [*plain], ] table = { * uint => text } diff --git a/tests/core/tests.rs b/tests/core/tests.rs index 256dbae..d528e0a 100644 --- a/tests/core/tests.rs +++ b/tests/core/tests.rs @@ -53,7 +53,40 @@ mod tests { #[test] fn plain_arrays() { let plain = Plain::new(7576, String::from("wiorurri34h").into()); - deser_test(&PlainArrays::new(vec![plain.clone(), plain.clone()])); + let plain_arrays = PlainArrays::new( + plain.clone(), + plain.clone(), + vec![plain.clone(), plain.clone()] + ); + deser_test(&plain_arrays); + // need to make sure they are actually inlined! + let bytes = vec![ + arr_def(4), + // embedded + cbor_tag(23), + cbor_int(7576, cbor_event::Sz::Two), + cbor_tag_sz(42, cbor_event::Sz::One), + cbor_string("wiorurri34h"), + // single + arr_def(2), + cbor_tag(23), + cbor_int(7576, cbor_event::Sz::Two), + cbor_tag_sz(42, cbor_event::Sz::One), + cbor_string("wiorurri34h"), + // multiple + arr_def(4), + cbor_tag(23), + cbor_int(7576, cbor_event::Sz::Two), + cbor_tag_sz(42, cbor_event::Sz::One), + cbor_string("wiorurri34h"), + cbor_tag(23), + cbor_int(7576, cbor_event::Sz::Two), + cbor_tag_sz(42, cbor_event::Sz::One), + cbor_string("wiorurri34h"), + ].into_iter().flatten().clone().collect::>(); + let from_bytes = PlainArrays::from_cbor_bytes(&bytes).unwrap(); + assert_eq!(from_bytes.to_cbor_bytes(), bytes); + assert_eq!(plain_arrays.to_cbor_bytes(), bytes); } #[test] diff --git a/tests/deser_test b/tests/deser_test index 9ebc23d..950518d 100644 --- a/tests/deser_test +++ b/tests/deser_test @@ -35,7 +35,7 @@ fn cbor_string(s: &str) -> Vec { } fn cbor_tag(t: u8) -> Vec { - assert!(t <= 0xd4 - 0xc0); + assert!(t <= 0xd7 - 0xc0); vec![0xc0u8 + t] } diff --git a/tests/preserve-encodings/input.cddl b/tests/preserve-encodings/input.cddl index 4d16d53..dfac09d 100644 --- a/tests/preserve-encodings/input.cddl +++ b/tests/preserve-encodings/input.cddl @@ -43,6 +43,12 @@ enums = [ plain = (d: #6.13(uint), e: tagged_text) +plain_arrays = [ + embedded: plain, + single: [plain], + multi: [*plain], +] + group_choice = [ 3 // #6.10(2) // foo // 0, x: uint // plain ] foo_bytes = bytes .cbor foo @@ -139,4 +145,4 @@ enum_opt_embed_fields = [ 1, ? non_overlapping_type_choice_some, #6.11(11) // ; @name eg 1, ? overlapping_inlined, #6.13(13) -] \ No newline at end of file +] diff --git a/tests/preserve-encodings/tests.rs b/tests/preserve-encodings/tests.rs index fb74116..db76713 100644 --- a/tests/preserve-encodings/tests.rs +++ b/tests/preserve-encodings/tests.rs @@ -997,4 +997,53 @@ mod tests { } } } + + #[test] + fn plain_arrays() { + let plain = Plain::new(10, String::from("wiorurri34h").into()); + let plain_arrays = PlainArrays::new( + plain.clone(), + plain.clone(), + vec![plain.clone(), plain.clone()] + ); + deser_test(&plain_arrays); + let def_encodings = vec![Sz::Inline, Sz::One, Sz::Two, Sz::Four, Sz::Eight]; + let str_11_encodings = vec![ + StringLenSz::Len(Sz::One), + StringLenSz::Len(Sz::Inline), + StringLenSz::Indefinite(vec![(5, Sz::Two), (6, Sz::One)]), + StringLenSz::Indefinite(vec![(2, Sz::Inline), (0, Sz::Inline), (9, Sz::Four)]), + ]; + for str_enc in &str_11_encodings { + for def_enc in &def_encodings { + // need to make sure they are actually inlined! + let irregular_bytes = vec![ + arr_sz(4, *def_enc), + // embedded + cbor_tag_sz(13, *def_enc), + cbor_int(10, *def_enc), + cbor_tag_sz(9, *def_enc), + cbor_str_sz("wiorurri34h", str_enc.clone()), + // single + arr_def(2), + cbor_tag(13), + cbor_int(10, *def_enc), + cbor_tag_sz(9, *def_enc), + cbor_str_sz("wiorurri34h", str_enc.clone()), + // multiple + arr_def(4), + cbor_tag_sz(13, *def_enc), + cbor_int(10, *def_enc), + cbor_tag_sz(9, *def_enc), + cbor_str_sz("wiorurri34h", str_enc.clone()), + cbor_tag_sz(13, *def_enc), + cbor_int(10, *def_enc), + cbor_tag_sz(9, *def_enc), + cbor_str_sz("wiorurri34h", str_enc.clone()), + ].into_iter().flatten().clone().collect::>(); + let from_bytes = PlainArrays::from_cbor_bytes(&irregular_bytes).unwrap(); + assert_eq!(from_bytes.to_cbor_bytes(), irregular_bytes); + } + } + } }