aboutsummaryrefslogtreecommitdiff
path: root/src/test.rs
diff options
context:
space:
mode:
authorJack O'Connor <[email protected]>2024-07-09 19:13:32 -0700
committerJack O'Connor <[email protected]>2024-07-09 19:20:48 -0700
commitdd0afd640ad97b5ebcf887107162009a23ffdca0 (patch)
tree9c5207791e710d24163a808f742400993ec1022d /src/test.rs
parent7ff69443d018f46e52f7678ed42c2b812c002805 (diff)
serialize Hash with serde_bytes
Closes #412.
Diffstat (limited to 'src/test.rs')
-rw-r--r--src/test.rs28
1 files changed, 26 insertions, 2 deletions
diff --git a/src/test.rs b/src/test.rs
index c76cbbc..b5bd4dc 100644
--- a/src/test.rs
+++ b/src/test.rs
@@ -809,14 +809,38 @@ fn test_mmap_rayon() -> Result<(), std::io::Error> {
#[cfg(feature = "std")]
#[cfg(feature = "serde")]
fn test_serde() {
- let hash: crate::Hash = [7; 32].into();
+ let hash: crate::Hash = [255; 32].into();
+
let json = serde_json::to_string(&hash).unwrap();
assert_eq!(
json,
- "[7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7]",
+ "[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]",
);
let hash2: crate::Hash = serde_json::from_str(&json).unwrap();
assert_eq!(hash, hash2);
+
+ let mut cbor = Vec::<u8>::new();
+ ciborium::into_writer(&hash, &mut cbor).unwrap();
+ 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
+ ]
+ );
+ 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
+ // 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,
+ ];
+ let hash_from_old_cbor: crate::Hash = ciborium::from_reader(old_cbor).unwrap();
+ assert_eq!(hash_from_old_cbor, hash);
}
// `cargo +nightly miri test` currently works, but it takes forever, because some of our test