Prechádzať zdrojové kódy

feat: Add robust mnemonic hashing and debug tests for Info struct

thesimplekid 1 týždeň pred
rodič
commit
607cdf23d4
1 zmenil súbory, kde vykonal 80 pridanie a 3 odobranie
  1. 80 3
      crates/cdk-mintd/src/config.rs

+ 80 - 3
crates/cdk-mintd/src/config.rs

@@ -26,14 +26,18 @@ pub struct Info {
 
 impl std::fmt::Debug for Info {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        let mnemonic_hash = sha256::Hash::from_slice(&self.mnemonic.clone().into_bytes())
-            .map_err(|_| std::fmt::Error)?;
+        // Use a fallback approach that won't panic
+        let mnemonic_display = match sha256::Hash::hash(self.mnemonic.clone().into_bytes().as_ref())
+        {
+            Ok(hash) => format!("<hashed: {}>", hash),
+            Err(_) => String::from("<protected>"), // Fallback if hashing fails
+        };
 
         f.debug_struct("Info")
             .field("url", &self.url)
             .field("listen_host", &self.listen_host)
             .field("listen_port", &self.listen_port)
-            .field("mnemonic", &format!("<hashed: {}>", mnemonic_hash))
+            .field("mnemonic", &mnemonic_display)
             .field("input_fee_ppk", &self.input_fee_ppk)
             .field("http_cache", &self.http_cache)
             .field("enable_swagger_ui", &self.enable_swagger_ui)
@@ -361,3 +365,76 @@ impl Settings {
         Ok(settings)
     }
 }
+
+#[cfg(test)]
+mod tests {
+
+    use super::*;
+
+    #[test]
+    fn test_info_debug_impl() {
+        // Create a sample Info struct with test data
+        let info = Info {
+            url: "http://example.com".to_string(),
+            listen_host: "127.0.0.1".to_string(),
+            listen_port: 8080,
+            mnemonic: "test secret mnemonic phrase".to_string(),
+            input_fee_ppk: Some(100),
+            ..Default::default()
+        };
+
+        // Convert the Info struct to a debug string
+        let debug_output = format!("{:?}", info);
+
+        // Verify the debug output contains expected fields
+        assert!(debug_output.contains("url: \"http://example.com\""));
+        assert!(debug_output.contains("listen_host: \"127.0.0.1\""));
+        assert!(debug_output.contains("listen_port: 8080"));
+
+        // The mnemonic should be hashed, not displayed in plaintext
+        assert!(!debug_output.contains("test secret mnemonic phrase"));
+        assert!(debug_output.contains("<hashed: "));
+
+        assert!(debug_output.contains("input_fee_ppk: 100"));
+        assert!(debug_output.contains("http_cache: true"));
+        assert!(debug_output.contains("enable_swagger_ui: false"));
+    }
+
+    #[test]
+    fn test_info_debug_with_empty_mnemonic() {
+        // Test with an empty mnemonic to ensure it doesn't panic
+        let info = Info {
+            url: "http://example.com".to_string(),
+            listen_host: "127.0.0.1".to_string(),
+            listen_port: 8080,
+            mnemonic: "".to_string(), // Empty mnemonic
+            enable_swagger_ui: Some(false),
+            ..Default::default()
+        };
+
+        // This should not panic
+        let debug_output = format!("{:?}", info);
+
+        // The empty mnemonic should still be hashed
+        assert!(debug_output.contains("<hashed: "));
+    }
+
+    #[test]
+    fn test_info_debug_with_special_chars() {
+        // Test with a mnemonic containing special characters
+        let info = Info {
+            url: "http://example.com".to_string(),
+            listen_host: "127.0.0.1".to_string(),
+            listen_port: 8080,
+            mnemonic: "特殊字符 !@#$%^&*()".to_string(), // Special characters
+            ..Default::default()
+        };
+
+        // This should not panic
+        let debug_output = format!("{:?}", info);
+
+        // The mnemonic with special chars should be hashed
+        assert!(!debug_output.contains("特殊字符 !@#$%^&*()"));
+        assert!(debug_output.contains("<hashed: "));
+    }
+}