Parcourir la source

Wrote a roundtrip test for WriteCap.

Matthew Carr il y a 3 ans
Parent
commit
1fe0889ecb
2 fichiers modifiés avec 47 ajouts et 6 suppressions
  1. 1 1
      crates/node/src/main.rs
  2. 46 5
      crates/node/src/serde_tests.rs

+ 1 - 1
crates/node/src/main.rs

@@ -62,7 +62,7 @@ struct WriteCap {
     /// A digital signature which covers all of the fields in the write cap except for next.
     signature: Signature,
     /// The next write cap in the chain leading back to the root.
-    next: Box<Option<WriteCap>>,
+    next: Option<Box<WriteCap>>,
 }
 
 /// Fragments are created from blocks using Erasure Encoding and stored with other nodes in the

+ 46 - 5
crates/node/src/serde_tests.rs

@@ -3,14 +3,32 @@
 use super::*;
 use serde_block_tree::{Error, Result, from_vec, to_vec};
 
-static RANDOM_32ARRAY: [u8; 32] = [
+static PRINCIPAL: [u8; 32] = [
     0x75, 0x28, 0xA9, 0xE0, 0x9D, 0x24, 0xBA, 0xB3, 0x79, 0x56, 0x15, 0x68, 0xFD, 0xA4, 0xE2, 0xA4,
     0xCF, 0xB2, 0xC0, 0xE3, 0x96, 0xAE, 0xA2, 0x6E, 0x45, 0x15, 0x50, 0xED, 0xA6, 0xBE, 0x6D, 0xEC,
 ];
 
+static PAYLOAD: [u8; 128] = [
+    0x39, 0x79, 0x1A, 0x0D, 0x8E, 0x6C, 0xF5, 0x4B, 0xF3, 0xA4, 0x75, 0xC4, 0x44, 0x73, 0x58, 0x58,
+    0x97, 0x14, 0x64, 0xE0, 0xC6, 0xFE, 0xCB, 0xCF, 0xBE, 0x67, 0x49, 0x49, 0x40, 0xAE, 0x71, 0x5A,
+    0x94, 0x7E, 0x6C, 0x4B, 0xDE, 0x33, 0x22, 0x75, 0xD8, 0x54, 0x23, 0x37, 0xFD, 0x1A, 0x68, 0x4A,
+    0x5F, 0xB5, 0xB3, 0xC9, 0x9A, 0x72, 0x7C, 0xF4, 0x3C, 0xAB, 0xED, 0x97, 0x87, 0x63, 0xBB, 0xD9,
+    0x8B, 0x11, 0xD3, 0xC1, 0x4C, 0x9A, 0x09, 0x0E, 0x7C, 0x10, 0x65, 0x9B, 0x8F, 0x35, 0xEB, 0x51,
+    0x19, 0xD7, 0x6E, 0xA3, 0xC9, 0x64, 0xE2, 0x54, 0x84, 0x5F, 0xA1, 0x8B, 0x63, 0x0C, 0xC3, 0x9D,
+    0xBE, 0xBB, 0x7F, 0x31, 0x1D, 0x59, 0xE2, 0x68, 0xC4, 0x5B, 0x37, 0x77, 0x04, 0xAD, 0x44, 0x75,
+    0xEE, 0x1F, 0x84, 0x17, 0xA2, 0x74, 0xC3, 0xC3, 0xD5, 0x2F, 0x70, 0x74, 0xFE, 0xD8, 0x2C, 0x29,
+];
+
+static SIGNATURE: [u8; 64] = [
+    0x2E, 0x19, 0x8E, 0xCC, 0x2D, 0xE1, 0x0E, 0x42, 0x3F, 0xBB, 0x89, 0x3D, 0x07, 0xAB, 0x57, 0xBF,
+    0xEB, 0xB1, 0xA7, 0x23, 0xF9, 0xD2, 0xC3, 0x3F, 0x7A, 0x3C, 0xD0, 0x31, 0x38, 0x01, 0x33, 0x1F,
+    0x07, 0x8D, 0x68, 0x0C, 0x6B, 0xF1, 0xBA, 0xD3, 0xD4, 0xAE, 0xAD, 0x1A, 0xF7, 0x5D, 0x2C, 0xEF,
+    0x1F, 0x5C, 0x50, 0xE9, 0xFA, 0x8A, 0xDB, 0xB2, 0x4C, 0xC6, 0x9B, 0x06, 0x5F, 0xFB, 0xE3, 0xDA,
+];
+
 #[test]
 fn roundtrip_fragment_record() -> Result<()> {
-    let expected = FragmentRecord::new(229, Hash::Sha2_256(RANDOM_32ARRAY));
+    let expected = FragmentRecord::new(229, Hash::Sha2_256(PRINCIPAL));
     let ser_result = to_vec(&expected);
     let de_result = from_vec(&ser_result?);
     assert_eq!(expected, de_result?);
@@ -19,13 +37,13 @@ fn roundtrip_fragment_record() -> Result<()> {
 
 #[test]
 fn roundtrip_directory() -> Result<()> {
-    let principal = Principal(Hash::Sha2_256(RANDOM_32ARRAY));
+    let principal = Principal(Hash::Sha2_256(PRINCIPAL));
     let mut child = HashMap::new();
     child.insert(FragmentSerial(0), FragmentRecord::new(0, principal.0));
     let mut children = HashMap::new();
     children.insert(".metadata".to_string(), child);
     let expected = Directory {
-        nodes: vec![Principal(Hash::Sha2_256(RANDOM_32ARRAY))],
+        nodes: vec![Principal(Hash::Sha2_256(PRINCIPAL))],
         children
     };
     let ser_result = to_vec(&expected);
@@ -36,11 +54,34 @@ fn roundtrip_directory() -> Result<()> {
 
 #[test]
 fn roundtrip_fragment() -> Result<()> {
-    let body = Vec::from(RANDOM_32ARRAY);
+    let body = Vec::from(PAYLOAD);
     let expected = Fragment::new("apps/bodhi", 42, body)
         .map_err(|err| Error::Message(err.to_string()))?;
     let ser_result = to_vec(&expected);
     let de_result = from_vec(&ser_result?);
     assert_eq!(expected, de_result?);
     Ok(())
+}
+
+#[test]
+fn roundtrip_write_cap() -> Result<()> {
+    let expected = WriteCap {
+        issued_to: Principal(Hash::Sha2_256(PRINCIPAL)),
+        issued_by: Principal(Hash::Sha2_256(PRINCIPAL)),
+        path: Path::try_from("contacts/emergency").map_err(|err| Error::Message(err.to_string()))?,
+        expires: Epoch(1649904316),
+        signature: Signature::Ed25519(SIGNATURE),
+        next: Some(Box::from(WriteCap {
+            issued_to: Principal(Hash::Sha2_256(PRINCIPAL)),
+            issued_by: Principal(Hash::Sha2_256(PRINCIPAL)),
+            path: Path::try_from("contacts").map_err(|err| Error::Message(err.to_string()))?,
+            expires: Epoch(1649994316),
+            signature: Signature::Ed25519(SIGNATURE),
+            next: None,
+        }))
+    };
+    let ser_result = to_vec(&expected);
+    let de_result = from_vec(&ser_result?);
+    assert_eq!(expected, de_result?);
+    Ok(())
 }