Skip to content

Commit

Permalink
tests: deserialize_byte with data greater than 4k
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Oct 20, 2023
1 parent 6c618fa commit 187988c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ciborium/tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>);

impl Serialize for Wrap {
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_bytes(&self.0[..])
}
}

impl<'de> Deserialize<'de> for Wrap {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Wrap, D::Error> {
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<E: serde::de::Error>(self, v: &[u8]) -> Result<Self::Value, E> {
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);
}

0 comments on commit 187988c

Please sign in to comment.