Bladeren bron

Finished writing roundtrip tests.

Matthew Carr 3 jaren geleden
bovenliggende
commit
59325669d4
2 gewijzigde bestanden met toevoegingen van 55 en 5 verwijderingen
  1. 8 1
      crates/node/src/main.rs
  2. 47 4
      crates/node/src/serde_tests.rs

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

@@ -26,7 +26,7 @@ struct Block {
     path: Path,
     /// This field contains a collection of `ReadCap`s indexed by the principal who holds them.
     /// `ReadCap`s are envelopments of the key used to encrypt this block.
-    read_caps: HashMap<Principal, ReadCap>,
+    read_caps: HashMap<Principal, Ciphertext<Key>>,
     /// This field is used to verify that the signer of this block had permission to write it.
     /// It contains a certificate chain that must lead back to the root key for the tree this block
     /// is part of.
@@ -157,6 +157,13 @@ fn main() {
     println!("Hello, world!");
 }
 
+impl ReadCap {
+    #[allow(dead_code)]
+    fn new(issued_to: Hash, key: Ciphertext<Key>) -> ReadCap {
+        ReadCap { issued_to: Principal(issued_to), key }
+    }
+}
+
 impl Fragment {
     /// Create a new fragment with the given fields. If `path_str` cannot be parsed then a failed
     /// `Result` is returned containing a `PathError`.

+ 47 - 4
crates/node/src/serde_tests.rs

@@ -26,6 +26,11 @@ static SIGNATURE: [u8; 64] = [
     0x1F, 0x5C, 0x50, 0xE9, 0xFA, 0x8A, 0xDB, 0xB2, 0x4C, 0xC6, 0x9B, 0x06, 0x5F, 0xFB, 0xE3, 0xDA,
 ];
 
+static KEY: [u8; 32] = [
+    0xB2, 0xB3, 0xDA, 0x5A, 0x1A, 0xF6, 0xB3, 0x78, 0x30, 0xAB, 0x1D, 0x33, 0x33, 0xE7, 0xE3, 0x5B,
+    0xBB, 0xF9, 0xFE, 0xD0, 0xC1, 0xF7, 0x90, 0x34, 0x69, 0xB7, 0xE7, 0xC6, 0x1C, 0x46, 0x85, 0x48,
+];
+
 #[test]
 fn roundtrip_fragment_record() -> Result<()> {
     let expected = FragmentRecord::new(229, Hash::Sha2_256(PRINCIPAL));
@@ -63,9 +68,8 @@ fn roundtrip_fragment() -> Result<()> {
     Ok(())
 }
 
-#[test]
-fn roundtrip_write_cap() -> Result<()> {
-    let expected = WriteCap {
+fn make_write_cap() -> Result<WriteCap> {
+    Ok(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()))?,
@@ -79,7 +83,46 @@ fn roundtrip_write_cap() -> Result<()> {
             signature: Signature::Ed25519(SIGNATURE),
             next: None,
         }))
-    };
+    })
+}
+
+#[test]
+fn roundtrip_write_cap() -> Result<()> {
+    let expected = make_write_cap()?;
+    let ser_result = to_vec(&expected);
+    let de_result = from_vec(&ser_result?);
+    assert_eq!(expected, de_result?);
+    Ok(())
+}
+
+fn make_read_cap() -> ReadCap {
+    ReadCap::new(Hash::Sha2_256(PRINCIPAL), Ciphertext(Key::Xsalsa20Poly1305(KEY)))
+}
+
+#[test]
+fn roundtrip_read_cap() -> Result<()> {
+    let expected = make_read_cap();
+    let ser_result = to_vec(&expected);
+    let de_result = from_vec(&ser_result?);
+    assert_eq!(expected, de_result?);
+    Ok(())
+}
+
+#[test]
+fn roundtrip_versioned_block() -> Result<()> {
+    let mut read_caps = HashMap::new();
+    {
+        let read_cap = make_read_cap();
+        read_caps.insert(read_cap.issued_to, read_cap.key);
+    }
+    let write_cap = make_write_cap()?;
+    let expected = VersionedBlock::V0(Block {
+        path: Path::try_from("apps/verse").map_err(|err| Error::Message(err.to_string()))?,
+        read_caps,
+        write_cap,
+        body: Ciphertext(Vec::from(PAYLOAD)),
+        signature: Signature::Ed25519(SIGNATURE)
+    });
     let ser_result = to_vec(&expected);
     let de_result = from_vec(&ser_result?);
     assert_eq!(expected, de_result?);