From 26a3884aa5bce561f5f40d135ab4ddc7039f56bf Mon Sep 17 00:00:00 2001 From: Bogdan Opanchuk Date: Wed, 21 Jun 2023 14:52:27 -0700 Subject: [PATCH] Add MessagePack tests --- Cargo.lock | 29 +++++++++++++++++ serdect/Cargo.toml | 1 + serdect/tests/messagepack.rs | 61 ++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 serdect/tests/messagepack.rs diff --git a/Cargo.lock b/Cargo.lock index cf8f260ab..680032007 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -960,6 +960,12 @@ dependencies = [ "sha2", ] +[[package]] +name = "paste" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" + [[package]] name = "pbkdf2" version = "0.12.1" @@ -1258,6 +1264,28 @@ dependencies = [ "subtle", ] +[[package]] +name = "rmp" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44519172358fd6d58656c86ab8e7fbc9e1490c3e8f14d35ed78ca0dd07403c9f" +dependencies = [ + "byteorder", + "num-traits", + "paste", +] + +[[package]] +name = "rmp-serde" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5b13be192e0220b8afb7222aa5813cb62cc269ebb5cac346ca6487681d2913e" +dependencies = [ + "byteorder", + "rmp", + "serde", +] + [[package]] name = "rsa" version = "0.9.2" @@ -1463,6 +1491,7 @@ dependencies = [ "ciborium", "hex-literal", "proptest", + "rmp-serde", "serde", "serde-json-core", "serde_json", diff --git a/serdect/Cargo.toml b/serdect/Cargo.toml index 8ef577676..96752139f 100644 --- a/serdect/Cargo.toml +++ b/serdect/Cargo.toml @@ -30,6 +30,7 @@ serde = { version = "1.0.119", default-features = false, features = ["derive"] } serde_json = "1" serde-json-core = { version = "0.5", default-features = false, features = ["std"] } toml = "0.7" +rmp-serde = "1" [features] default = ["alloc"] diff --git a/serdect/tests/messagepack.rs b/serdect/tests/messagepack.rs new file mode 100644 index 000000000..21abf27cd --- /dev/null +++ b/serdect/tests/messagepack.rs @@ -0,0 +1,61 @@ +//! messagepack-specific tests. + +#![cfg(feature = "alloc")] + +use hex_literal::hex; +use proptest::{array::*, collection::vec, prelude::*}; +use serdect::{array, slice}; + +/// Example input to be serialized. +/// Last byte is `0xFF` to test that no packing is performed for values under 128. +const EXAMPLE_BYTES: [u8; 16] = hex!("000102030405060708090A0B0C0D0EFF"); + +/// messagepack serialization of [`EXAMPLE_BYTES`] as a slice. +const MESSAGEPACK_SLICE: [u8; 18] = hex!("C410000102030405060708090A0B0C0D0EFF"); + +/// messagepack serialization of [`EXAMPLE_BYTES`] as an array. +/// Note the 0xCC marker before 0xFF, denoting that the integers are dynamically sized. +const MESSAGEPACK_ARRAY: [u8; 20] = hex!("DC0010000102030405060708090A0B0C0D0ECCFF"); + +#[test] +fn deserialize_slice() { + let deserialized = + rmp_serde::decode::from_slice::(&MESSAGEPACK_SLICE).unwrap(); + assert_eq!(deserialized.0, EXAMPLE_BYTES); +} + +#[test] +fn deserialize_array() { + let deserialized = + rmp_serde::decode::from_slice::>(&MESSAGEPACK_ARRAY).unwrap(); + assert_eq!(deserialized.0, EXAMPLE_BYTES); +} + +#[test] +fn serialize_slice() { + let serialized = + rmp_serde::encode::to_vec(&slice::HexUpperOrBin::from(EXAMPLE_BYTES.as_ref())).unwrap(); + assert_eq!(&serialized, &MESSAGEPACK_SLICE); +} + +#[test] +fn serialize_array() { + let serialized = rmp_serde::encode::to_vec(&array::HexUpperOrBin::from(EXAMPLE_BYTES)).unwrap(); + assert_eq!(&serialized, &MESSAGEPACK_ARRAY); +} + +proptest! { + #[test] + fn round_trip_slice(bytes in vec(any::(), 0..1024)) { + let serialized = rmp_serde::encode::to_vec(&slice::HexUpperOrBin::from(bytes.as_ref())).unwrap(); + let deserialized = rmp_serde::decode::from_slice::(&serialized).unwrap(); + prop_assert_eq!(bytes, deserialized.0); + } + + #[test] + fn round_trip_array(bytes in uniform32(0u8..)) { + let serialized = rmp_serde::encode::to_vec(&array::HexUpperOrBin::from(bytes)).unwrap(); + let deserialized = rmp_serde::decode::from_slice::>(&serialized).unwrap(); + prop_assert_eq!(bytes, deserialized.0); + } +}