Skip to content

Commit

Permalink
update CBOR tests per @BurningEnlightenment's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
oconnor663 committed Jul 10, 2024
1 parent 15e21c9 commit 8416b16
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,12 +809,14 @@ fn test_mmap_rayon() -> Result<(), std::io::Error> {
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
fn test_serde() {
let hash: crate::Hash = [255; 32].into();
// Henrik suggested that we use 0xfe / 254 for byte test data instead of 0xff / 255, due to the
// fact that 0xfe is not a well formed CBOR item.
let hash: crate::Hash = [0xfe; 32].into();

let json = serde_json::to_string(&hash).unwrap();
assert_eq!(
json,
"[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]",
"[254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254,254]",
);
let hash2: crate::Hash = serde_json::from_str(&json).unwrap();
assert_eq!(hash, hash2);
Expand All @@ -824,20 +826,22 @@ fn test_serde() {
assert_eq!(
cbor,
[
88, 32, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
0x58, 0x20, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe,
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe,
0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe
]
);
let hash_from_cbor: crate::Hash = ciborium::from_reader(&cbor[..]).unwrap();
assert_eq!(hash_from_cbor, hash);

// Before we used serde_bytes, the hash [255; 32] would serialize as an array instead of a
// Before we used serde_bytes, the hash [254; 32] would serialize as an array instead of a
// byte string, like this. Make sure we can still deserialize this representation.
let old_cbor: &[u8] = &[
152, 32, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255,
24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255,
24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255, 24, 255,
24, 255, 24, 255, 24, 255,
0x98, 0x20, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18,
0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe,
0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18,
0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe,
0x18, 0xfe, 0x18, 0xfe, 0x18, 0xfe,
];
let hash_from_old_cbor: crate::Hash = ciborium::from_reader(old_cbor).unwrap();
assert_eq!(hash_from_old_cbor, hash);
Expand Down

0 comments on commit 8416b16

Please sign in to comment.