Skip to content

Commit

Permalink
Add MessagePack tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Jun 21, 2023
1 parent 56c88f9 commit 0a2138a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions serdect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
61 changes: 61 additions & 0 deletions serdect/tests/messagepack.rs
Original file line number Diff line number Diff line change
@@ -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");

/// bincode serialization of [`EXAMPLE_BYTES`] as a slice.
const MESSAGEPACK_SLICE: [u8; 18] = hex!("C410000102030405060708090A0B0C0D0EFF");

/// bincode 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::<slice::HexUpperOrBin>(&MESSAGEPACK_SLICE).unwrap();
assert_eq!(deserialized.0, EXAMPLE_BYTES);
}

#[test]
fn deserialize_array() {
let deserialized =
rmp_serde::decode::from_slice::<array::HexUpperOrBin<16>>(&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::<u8>(), 0..1024)) {
let serialized = rmp_serde::encode::to_vec(&slice::HexUpperOrBin::from(bytes.as_ref())).unwrap();
let deserialized = rmp_serde::decode::from_slice::<slice::HexUpperOrBin>(&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::<array::HexUpperOrBin<32>>(&serialized).unwrap();
prop_assert_eq!(bytes, deserialized.0);
}
}

0 comments on commit 0a2138a

Please sign in to comment.