Răsfoiți Sursa

Added another roundtrip test and consolidated the impls in main.

Matthew Carr 3 ani în urmă
părinte
comite
a818c2ae38
3 a modificat fișierele cu 82 adăugiri și 56 ștergeri
  1. 1 0
      crates/node/.vscode/settings.json
  2. 69 55
      crates/node/src/main.rs
  3. 12 1
      crates/node/src/serde_tests.rs

+ 1 - 0
crates/node/.vscode/settings.json

@@ -2,6 +2,7 @@
     "cSpell.words": [
         "Asym",
         "blocktree",
+        "bodhi",
         "Hashable",
         "newtype",
         "Xsalsa"

+ 69 - 55
crates/node/src/main.rs

@@ -98,18 +98,6 @@ struct FragmentRecord {
     stored_by: Principal,
 }
 
-impl FragmentRecord {
-    /// Creates a new `FragmentRecord` whose `serial` and `stored_by` fields are set to
-    /// the given values.
-    #[allow(dead_code)]
-    fn new(serial: u32, stored_by: Hash) -> FragmentRecord {
-        FragmentRecord {
-            serial: FragmentSerial(serial),
-            stored_by: Principal(stored_by),
-        }
-    }
-}
-
 /// An identifier for a security principal, which is any entity that can be authenticated.
 #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable)]
 struct Principal(Hash);
@@ -122,13 +110,6 @@ struct Ciphertext<T>(T);
 #[derive(Debug, PartialEq, Serialize, Deserialize)]
 struct Path(Vec<String>);
 
-impl Path {
-    /// The character that is used to separate path components.
-    const SEP: char = '/';
-    /// The limit, in bytes, of a `Path`'s length.
-    const BYTE_LIMIT: usize = 4096;
-}
-
 /// Errors which can occur when converting a string to a `Path`.
 #[derive(Debug, PartialEq)]
 enum PathError {
@@ -140,6 +121,75 @@ enum PathError {
     EmptyComponent,
 }
 
+/// An instant in time represented by the number of seconds since January 1st 1970, 00:00:00 UTC.
+#[derive(Debug, PartialEq, Serialize, Deserialize)]
+struct Epoch(i64);
+
+/// The serial number of a block fragment.
+#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable)]
+struct FragmentSerial(u32);
+
+/// A cryptographic hash.
+#[allow(dead_code)]
+#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable)]
+enum Hash {
+    Sha2_256([u8; 32]),
+    #[serde(with = "BigArray")]
+    Sha2_512([u8; 64]),
+}
+
+/// A cryptographic signature.
+#[allow(dead_code)]
+#[derive(Debug, PartialEq, Serialize, Deserialize)]
+enum Signature {
+    #[serde(with = "BigArray")]
+    Ed25519([u8; 64]),
+}
+
+/// A cryptographic key.
+#[allow(dead_code)]
+#[derive(Debug, PartialEq, Serialize, Deserialize)]
+enum Key {
+    Xsalsa20Poly1305([u8; 32]),
+}
+
+fn main() {
+    println!("Hello, world!");
+}
+
+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`.
+    #[allow(dead_code)]
+    fn new(path_str: &str, serial_num: u32, body: Vec<u8>) -> Result<Fragment, PathError> {
+        let result = Path::try_from(path_str);
+        Ok(Fragment {
+            path: result?,
+            serial: FragmentSerial(serial_num),
+            body
+        })
+    }
+}
+
+impl FragmentRecord {
+    /// Creates a new `FragmentRecord` whose `serial` and `stored_by` fields are set to
+    /// the given values.
+    #[allow(dead_code)]
+    fn new(serial: u32, stored_by: Hash) -> FragmentRecord {
+        FragmentRecord {
+            serial: FragmentSerial(serial),
+            stored_by: Principal(stored_by),
+        }
+    }
+}
+
+impl Path {
+    /// The character that is used to separate path components.
+    const SEP: char = '/';
+    /// The limit, in bytes, of a `Path`'s length.
+    const BYTE_LIMIT: usize = 4096;
+}
+
 impl Display for PathError {
     fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        match self {
@@ -234,42 +284,6 @@ impl<'s> TryFrom<&'s str> for Path {
     }
 }
 
-/// An instant in time represented by the number of seconds since January 1st 1970, 00:00:00 UTC.
-#[derive(Debug, PartialEq, Serialize, Deserialize)]
-struct Epoch(u64);
-
-/// The serial number of a block fragment.
-#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable)]
-struct FragmentSerial(u32);
-
-/// A cryptographic hash.
-#[allow(dead_code)]
-#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable)]
-enum Hash {
-    Sha2_256([u8; 32]),
-    #[serde(with = "BigArray")]
-    Sha2_512([u8; 64]),
-}
-
-/// A cryptographic signature.
-#[allow(dead_code)]
-#[derive(Debug, PartialEq, Serialize, Deserialize)]
-enum Signature {
-    #[serde(with = "BigArray")]
-    Ed25519([u8; 64]),
-}
-
-/// A cryptographic key.
-#[allow(dead_code)]
-#[derive(Debug, PartialEq, Serialize, Deserialize)]
-enum Key {
-    Xsalsa20Poly1305([u8; 32]),
-}
-
-fn main() {
-    println!("Hello, world!");
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;

+ 12 - 1
crates/node/src/serde_tests.rs

@@ -1,7 +1,7 @@
 /// Tests which ensure that the main data structures can be round-tripped faithfully.
 
 use super::*;
-use serde_block_tree::{Result, from_vec, to_vec};
+use serde_block_tree::{Error, Result, from_vec, to_vec};
 
 static RANDOM_32ARRAY: [u8; 32] = [
     0x75, 0x28, 0xA9, 0xE0, 0x9D, 0x24, 0xBA, 0xB3, 0x79, 0x56, 0x15, 0x68, 0xFD, 0xA4, 0xE2, 0xA4,
@@ -32,4 +32,15 @@ fn roundtrip_directory() -> Result<()> {
     let de_result = from_vec(&ser_result?);
     assert_eq!(expected, de_result?);
     Ok(())
+}
+
+#[test]
+fn roundtrip_fragment() -> Result<()> {
+    let body = Vec::from(RANDOM_32ARRAY);
+    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(())
 }