From 187988c80a04b0da47579c2683f42b16795e6f14 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 20 Oct 2023 10:23:45 +0200 Subject: [PATCH] tests: deserialize_byte with data greater than 4k --- ciborium/tests/codec.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/ciborium/tests/codec.rs b/ciborium/tests/codec.rs index d737059..d601ba0 100644 --- a/ciborium/tests/codec.rs +++ b/ciborium/tests/codec.rs @@ -436,3 +436,43 @@ fn handle_struct_field_names(input: &str, expected: Foo) { let read = from_reader(&buf[..]).unwrap(); assert_eq!(expected, read); } + +#[test] +fn deserialize_bytes_greater_than_4k() { + #[derive(PartialEq, Eq, Debug)] + pub struct Wrap(Vec); + + impl Serialize for Wrap { + fn serialize(&self, s: S) -> Result { + s.serialize_bytes(&self.0[..]) + } + } + + impl<'de> Deserialize<'de> for Wrap { + fn deserialize>(d: D) -> Result { + d.deserialize_bytes(WrapVisitor {}) + } + } + + pub struct WrapVisitor; + + impl<'de> serde::de::Visitor<'de> for WrapVisitor { + type Value = Wrap; + + fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { + formatter.write_str("oh no!") + } + + fn visit_bytes(self, v: &[u8]) -> Result { + Ok(Wrap(v.to_vec())) + } + } + + let a = Wrap(vec![0u8; 5000]); + + let mut bytes = vec![]; + ciborium::ser::into_writer(&a, &mut bytes).unwrap(); + + let b: Wrap = ciborium::from_reader(&bytes[..]).unwrap(); + assert_eq!(a, b); +}