Skip to content

Commit

Permalink
More test cases for plain groups (#221)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rooooooooob authored Dec 27, 2023
1 parent e3ba977 commit 8d0de82
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 6 deletions.
4 changes: 4 additions & 0 deletions static/serialization_preserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
6 changes: 3 additions & 3 deletions tests/core/input.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
35 changes: 34 additions & 1 deletion tests/core/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<u8>>();
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]
Expand Down
2 changes: 1 addition & 1 deletion tests/deser_test
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn cbor_string(s: &str) -> Vec<u8> {
}

fn cbor_tag(t: u8) -> Vec<u8> {
assert!(t <= 0xd4 - 0xc0);
assert!(t <= 0xd7 - 0xc0);
vec![0xc0u8 + t]
}

Expand Down
8 changes: 7 additions & 1 deletion tests/preserve-encodings/input.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
]
]
49 changes: 49 additions & 0 deletions tests/preserve-encodings/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<u8>>();
let from_bytes = PlainArrays::from_cbor_bytes(&irregular_bytes).unwrap();
assert_eq!(from_bytes.to_cbor_bytes(), irregular_bytes);
}
}
}
}

0 comments on commit 8d0de82

Please sign in to comment.