Skip to content

Commit

Permalink
Implement RFCs
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Aug 6, 2023
1 parent dbd314b commit d7cccf4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
11 changes: 5 additions & 6 deletions serdect/tests/bincode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ const EXAMPLE_BYTES: [u8; 16] = hex!("000102030405060708090A0B0C0D0EFF");
/// bincode serialization of [`EXAMPLE_BYTES`] as a slice.
const BINCODE_SLICE: [u8; 24] = hex!("1000000000000000000102030405060708090A0B0C0D0EFF");

/// bincode serialization of [`EXAMPLE_BYTES`] as an array.
const BINCODE_ARRAY: [u8; 24] = BINCODE_SLICE;

#[test]
fn deserialize_slice() {
let deserialized = bincode::deserialize::<slice::HexUpperOrBin>(&BINCODE_SLICE).unwrap();
Expand All @@ -31,14 +28,14 @@ fn deserialize_slice_owned() {

#[test]
fn deserialize_array() {
let deserialized = bincode::deserialize::<array::HexUpperOrBin<16>>(&BINCODE_ARRAY).unwrap();
let deserialized = bincode::deserialize::<array::HexUpperOrBin<16>>(&BINCODE_SLICE).unwrap();
assert_eq!(deserialized.0, EXAMPLE_BYTES);
}

#[test]
fn deserialize_array_owned() {
let deserialized =
bincode::deserialize_from::<_, array::HexUpperOrBin<16>>(BINCODE_ARRAY.as_ref()).unwrap();
bincode::deserialize_from::<_, array::HexUpperOrBin<16>>(BINCODE_SLICE.as_ref()).unwrap();
assert_eq!(deserialized.0, EXAMPLE_BYTES);
}

Expand All @@ -52,7 +49,7 @@ fn serialize_slice() {
#[test]
fn serialize_array() {
let serialized = bincode::serialize(&array::HexUpperOrBin::from(EXAMPLE_BYTES)).unwrap();
assert_eq!(&serialized, &BINCODE_ARRAY);
assert_eq!(&serialized, &BINCODE_SLICE);
}

proptest! {
Expand All @@ -68,5 +65,7 @@ proptest! {
let serialized = bincode::serialize(&array::HexUpperOrBin::from(bytes)).unwrap();
let deserialized = bincode::deserialize::<array::HexUpperOrBin<32>>(&serialized).unwrap();
prop_assert_eq!(bytes, deserialized.0);
// 8 bytes for the length tag + 32 bytes of data
prop_assert_eq!(serialized.len(), 8 + 32);
}
}
11 changes: 4 additions & 7 deletions serdect/tests/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ const EXAMPLE_BYTES: [u8; 16] = hex!("000102030405060708090A0B0C0D0EFF");
/// (first three bits, `0b010` denote a byte string, and the last five, `0b10000` denote the length)
const CBOR_SLICE: [u8; 17] = hex!("50000102030405060708090A0B0C0D0EFF");

/// CBOR serialization of [`EXAMPLE_BYTES`] as an array.
/// (first three bits, `0b100` denote an array, and the last five, `0b10000` denote the length)
/// Note the 0x18 marker before 0xFF, denoting that the integers are dynamically sized.
const CBOR_ARRAY: [u8; 17] = CBOR_SLICE;

#[test]
fn deserialize_slice() {
let deserialized = de::from_reader::<slice::HexUpperOrBin, _>(CBOR_SLICE.as_ref()).unwrap();
Expand All @@ -29,7 +24,7 @@ fn deserialize_slice() {

#[test]
fn deserialize_array() {
let deserialized = de::from_reader::<array::HexUpperOrBin<16>, _>(CBOR_ARRAY.as_ref()).unwrap();
let deserialized = de::from_reader::<array::HexUpperOrBin<16>, _>(CBOR_SLICE.as_ref()).unwrap();
assert_eq!(deserialized.0, EXAMPLE_BYTES);
}

Expand All @@ -51,7 +46,7 @@ fn serialize_slice() {
#[test]
fn serialize_array() {
let serialized = serialize(&array::HexUpperOrBin::from(EXAMPLE_BYTES));
assert_eq!(&serialized, &CBOR_ARRAY);
assert_eq!(&serialized, &CBOR_SLICE);
}

proptest! {
Expand All @@ -67,5 +62,7 @@ proptest! {
let serialized = serialize(&array::HexUpperOrBin::from(bytes));
let deserialized = de::from_reader::<array::HexUpperOrBin<32>, _>(serialized.as_slice()).unwrap();
prop_assert_eq!(bytes, deserialized.0);
// 1 byte slice tag + 1 byte length tag + 32 bytes of data
prop_assert_eq!(serialized.len(), 2 + 32);
}
}
10 changes: 4 additions & 6 deletions serdect/tests/messagepack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ 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; 18] = MESSAGEPACK_SLICE;

#[test]
fn deserialize_slice() {
let deserialized =
Expand All @@ -27,7 +23,7 @@ fn deserialize_slice() {
#[test]
fn deserialize_array() {
let deserialized =
rmp_serde::decode::from_slice::<array::HexUpperOrBin<16>>(&MESSAGEPACK_ARRAY).unwrap();
rmp_serde::decode::from_slice::<array::HexUpperOrBin<16>>(&MESSAGEPACK_SLICE).unwrap();
assert_eq!(deserialized.0, EXAMPLE_BYTES);
}

Expand All @@ -41,7 +37,7 @@ fn serialize_slice() {
#[test]
fn serialize_array() {
let serialized = rmp_serde::encode::to_vec(&array::HexUpperOrBin::from(EXAMPLE_BYTES)).unwrap();
assert_eq!(&serialized, &MESSAGEPACK_ARRAY);
assert_eq!(&serialized, &MESSAGEPACK_SLICE);
}

proptest! {
Expand All @@ -57,5 +53,7 @@ proptest! {
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);
// 1 byte slice tag + 1 byte length tag + 32 bytes of data
prop_assert_eq!(serialized.len(), 2 + 32);
}
}

0 comments on commit d7cccf4

Please sign in to comment.