diff options
author | Neal H. Walfield <neal@gnu.org> | 2017-11-01 20:50:05 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@gnu.org> | 2017-11-01 21:01:40 +0100 |
commit | ad77229afac758802a45f00be9d050785f9af100 (patch) | |
tree | d2ec612798106b446d66f0fc88a30b55bd0c6533 /src/openpgp | |
parent | 1d9172f63878efe7e698456881c10a0803bd30b2 (diff) |
Provide better Debug trait implementations for the OpenPGP packets
- The default implementations print everything, as is. Instead,
only show a summary of very long content.
- Since text is not guaranteed to be valid UTF-8, it is not stored
as a String, but as a &[u8]. When displaying these fields, try to
display them as strings.
Diffstat (limited to 'src/openpgp')
-rw-r--r-- | src/openpgp/openpgp.rs | 90 | ||||
-rw-r--r-- | src/openpgp/parse/parse.rs | 1 |
2 files changed, 86 insertions, 5 deletions
diff --git a/src/openpgp/openpgp.rs b/src/openpgp/openpgp.rs index 90b2efbb..3c9e8bbc 100644 --- a/src/openpgp/openpgp.rs +++ b/src/openpgp/openpgp.rs @@ -1,5 +1,6 @@ // Machinery for parsing and serializing OpenPGP packet headers. +use std; use std::ops::Deref; /// The OpenPGP packet types. The values correspond to the serialized @@ -185,7 +186,6 @@ pub struct Header { length: BodyLength, } -#[derive(Debug)] pub struct Signature { common: PacketCommon, version: u8, @@ -198,6 +198,25 @@ pub struct Signature { mpis: Box<[u8]>, } +impl std::fmt::Debug for Signature { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let hashed_area = format!("{} bytes", self.hashed_area.len()); + let unhashed_area = format!("{} bytes", self.unhashed_area.len()); + let mpis = format!("{} bytes", self.mpis.len()); + + f.debug_struct("Signature") + .field("version", &self.version) + .field("sigtype", &self.sigtype) + .field("pk_algo", &self.pk_algo) + .field("hash_algo", &self.hash_algo) + .field("hashed_area", &hashed_area) + .field("unhashed_area", &unhashed_area) + .field("hash_prefix", &self.hash_prefix) + .field("mpis", &mpis) + .finish() + } +} + // Allow transparent access of common fields. impl<'a> Deref for Signature { type Target = PacketCommon; @@ -207,7 +226,6 @@ impl<'a> Deref for Signature { } } -#[derive(Debug)] pub struct Key { common: PacketCommon, version: u8, @@ -217,6 +235,20 @@ pub struct Key { mpis: Box<[u8]>, } +impl std::fmt::Debug for Key { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let mpis = format!("{} bytes", self.mpis.len()); + + f.debug_struct("Key") + .field("tag", &self.common.tag) + .field("version", &self.version) + .field("creation_time", &self.creation_time) + .field("pk_algo", &self.pk_algo) + .field("mpis", &mpis) + .finish() + } +} + // Allow transparent access of common fields. impl<'a> Deref for Key { type Target = PacketCommon; @@ -226,12 +258,21 @@ impl<'a> Deref for Key { } } -#[derive(Debug)] pub struct UserID { common: PacketCommon, value: Box<[u8]>, } +impl std::fmt::Debug for UserID { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let userid = String::from_utf8_lossy(&self.value[..]); + + f.debug_struct("UserID") + .field("value", &userid) + .finish() + } +} + // Allow transparent access of common fields. impl<'a> Deref for UserID { type Target = PacketCommon; @@ -241,7 +282,6 @@ impl<'a> Deref for UserID { } } -#[derive(Debug)] pub struct Literal { common: PacketCommon, format: u8, @@ -255,6 +295,31 @@ pub struct Literal { content: Box<[u8]>, } +impl std::fmt::Debug for Literal { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let filename = if let Some(ref filename) = self.filename { + Some(String::from_utf8_lossy(filename)) + } else { + None + }; + + let threshold = 36; + let prefix = &self.content[..std::cmp::min(threshold, self.content.len())]; + let mut prefix_fmt = String::from_utf8_lossy(prefix).into_owned(); + if self.content.len() > threshold { + prefix_fmt.push_str("..."); + } + prefix_fmt.push_str(&format!(" ({} bytes)", self.content.len())[..]); + + f.debug_struct("Literal") + .field("format", &(self.format as char)) + .field("filename", &filename) + .field("date", &self.date) + .field("content", &prefix_fmt) + .finish() + } +} + // Allow transparent access of common fields. impl<'a> Deref for Literal { type Target = PacketCommon; @@ -264,7 +329,6 @@ impl<'a> Deref for Literal { } } -#[derive(Debug)] pub struct CompressedData { common: PacketCommon, algo: u8, @@ -280,6 +344,22 @@ impl<'a> Deref for CompressedData { } } +impl std::fmt::Debug for CompressedData { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let l = std::cmp::min(16, self.content.len()); + let mut content_fmt = format!("{:?}", &self.content[..l]); + if l < self.content.len() { + content_fmt.push_str("..."); + } + content_fmt.push_str(&format!(" ({} bytes)", self.content.len())[..]); + + f.debug_struct("CompressedData") + .field("algo", &self.algo) + .field("content (decompressed)", &content_fmt) + .finish() + } +} + #[derive(Debug)] pub enum Packet { Signature(Signature), diff --git a/src/openpgp/parse/parse.rs b/src/openpgp/parse/parse.rs index 91f2626e..9f95dd5b 100644 --- a/src/openpgp/parse/parse.rs +++ b/src/openpgp/parse/parse.rs @@ -388,6 +388,7 @@ fn compressed_data_body_test () { assert_eq!(h.length, BodyLength::Indeterminate); let p = compressed_data_body(&mut bio).unwrap(); + println!("{:?}", p); let mut bio2 = BufferedReaderMemory::new(&p.content); let h = header(&mut bio2).unwrap(); |