Add tests for zero-copy deserialization

This commit is contained in:
Sosthène Guédon
2023-11-24 13:55:53 +01:00
committed by sosthene-nitrokey
parent 813c25b503
commit 1d0c99e8a1
2 changed files with 90 additions and 2 deletions
+2
View File
@@ -22,4 +22,6 @@ syn = "1.0"
heapless = { version = "0.7.16", default-features = false, features = ["serde"] }
hex-literal = "0.4.1"
serde = { version = "1", default-features = false }
serde-byte-array = "0.1.2"
serde_bytes = { version = "0.11.12", default-features = false }
serde_cbor = { version = "0.11.0", default-features = false }
+88 -2
View File
@@ -38,6 +38,8 @@ mod some_keys {
use super::*;
use hex_literal::hex;
use serde_byte_array::ByteArray;
use serde_bytes::Bytes;
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
#[serde_indexed(offset = 1)]
@@ -50,6 +52,17 @@ mod some_keys {
pub vector: heapless::Vec<u8, 16>,
}
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
#[serde_indexed(offset = 1)]
pub struct SomeRefKeys<'a> {
pub number: i32,
pub bytes: &'a ByteArray<7>,
pub string: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub option: Option<u8>,
pub vector: &'a Bytes,
}
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
// #[serde_indexed(offset = 1)]
pub struct NakedOption {
@@ -58,6 +71,14 @@ mod some_keys {
pub key: bool,
}
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
// #[serde_indexed(offset = 1)]
pub struct NakedRefOption<'a> {
pub option: Option<SomeRefKeys<'a>>,
pub num: usize,
pub key: bool,
}
#[derive(Clone, Debug, PartialEq, SerializeIndexed, DeserializeIndexed)]
// #[serde_indexed(offset = 1)]
pub struct EmptyStruct {}
@@ -76,21 +97,43 @@ mod some_keys {
option: None,
vector,
};
// in Python: cbor.dumps({1: -7, 2: [37]*7, 3: "so serde", 5: [42]*1})
// in Python: cbor2.dumps({1: -7, 2: [37]*7, 3: "so serde", 5: [42]*1})
let serialized: &[u8] =
&hex!("a40126028718251825182518251825182518250368736f2073657264650581182a");
(serialized, value)
}
fn a_ref_example() -> (&'static [u8], SomeRefKeys<'static>) {
const BYTE_ARRAY: ByteArray<7> = ByteArray::new([37u8; 7]);
let value = SomeRefKeys {
number: -7,
bytes: &BYTE_ARRAY,
string: "so serde",
option: None,
vector: Bytes::new(&[42]),
};
// in Python: cbor2.dumps({1: -7, 2: bytes([37]*7), 3: "so serde", 5: bytes([42]*1)}).
let serialized: &[u8] = &hex!("a401260247252525252525250368736f20736572646505412a");
(serialized, value)
}
fn another_example() -> (&'static [u8], SomeKeys) {
let (_, mut an_example) = an_example();
an_example.option = Some(0xff);
// in Python: cbor.dumps({1: -7, 2: [37]*7, 3: "so serde", 4: 0xff, 5: [42]*1})
// in Python: cbor2.dumps({1: -7, 2: [37]*7, 3: "so serde", 4: 0xff, 5: [42]*1})
let serialized: &[u8] =
&hex!("a50126028718251825182518251825182518250368736f2073657264650418ff0581182a");
(serialized, an_example)
}
fn another_ref_example() -> (&'static [u8], SomeRefKeys<'static>) {
let (_, mut an_example) = a_ref_example();
an_example.option = Some(0xff);
// in Python: cbor2.dumps({1: -7, 2: bytes([37]*7), 3: "so serde", 4: 0xff, 5: bytes([42]*1)}).hex()
let serialized: &[u8] = &hex!("a501260247252525252525250368736f2073657264650418ff05412a");
(serialized, an_example)
}
#[test]
fn serialize() {
let (serialized_value, example) = an_example();
@@ -112,6 +155,27 @@ mod some_keys {
assert_eq!(maybe_example, example);
}
#[test]
fn serialize_ref() {
let (serialized_value, example) = a_ref_example();
let mut buffer = [0u8; 64];
let size = cbor_serialize(&example, &mut buffer).unwrap();
assert_eq!(&buffer[..size], serialized_value);
}
#[test]
fn deserialize_ref() {
let (serialized_value, example) = a_ref_example();
// no allocations need in this case.
let maybe_example: SomeRefKeys =
cbor_deserialize_with_scratch(serialized_value, &mut []).unwrap();
assert_eq!(maybe_example, example);
}
#[test]
fn another_serialize() {
let (serialized_value, example) = another_example();
@@ -133,4 +197,26 @@ mod some_keys {
assert_eq!(maybe_example, example);
}
#[test]
fn another_ref_serialize() {
let (serialized_value, example) = another_ref_example();
let mut buffer = [0u8; 64];
let size = cbor_serialize(&example, &mut buffer).unwrap();
assert_eq!(&buffer[..size], serialized_value);
}
#[test]
fn another_ref_deserialize() {
let (serialized_value, example) = another_ref_example();
// could also use `cbor_deserialize_with_scratch` in this case,
// demonstrating the `cbor_deserialize` function.
let mut buffer = serialized_value.to_owned();
let maybe_example: SomeRefKeys = cbor_deserialize(&mut buffer).unwrap();
assert_eq!(maybe_example, example);
}
}