diff options
author | Neal H. Walfield <neal@pep.foundation> | 2020-03-03 23:20:33 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2020-03-03 23:52:03 +0100 |
commit | a61bfdab687e9c0b6c5e263ed304f48183059a45 (patch) | |
tree | 545d516b6b2fe513f63caf3bcc81024338459583 /openpgp/tests/for-each-artifact.rs | |
parent | 93dbeb3160262726d4c60b4811cf352c08dfc5b3 (diff) |
openpgp: Only impl Serialize for objects that are normally exported.
- Add two new traits: `Marshal` and `MarshalInto`.
- Implement them instead of `Serialize` and `SerializeInto`.
- Only implement `Serialize` and `SerializeInto` for data structures
that are normally exported.
- This should prevent users from accidentally serializing a bare
signature (`Signature`) when they meant to serialize a signature
packet (`Packet`), for instance.
- Fixes #368.
Diffstat (limited to 'openpgp/tests/for-each-artifact.rs')
-rw-r--r-- | openpgp/tests/for-each-artifact.rs | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/openpgp/tests/for-each-artifact.rs b/openpgp/tests/for-each-artifact.rs index 5678bd79..3a1c8fb4 100644 --- a/openpgp/tests/for-each-artifact.rs +++ b/openpgp/tests/for-each-artifact.rs @@ -15,13 +15,13 @@ mod for_each_artifact { for_all_files(&test_data_dir(), |src| { for_all_packets(src, |p| { let mut v = Vec::new(); - p.serialize(&mut v)?; + (p as &Marshal).serialize(&mut v)?; let q = openpgp::Packet::from_bytes(&v)?; if p != &q { return Err(failure::format_err!( "assertion failed: p == q\np = {:?}\nq = {:?}", p, q)); } - let w = p.to_vec()?; + let w = (p as &MarshalInto).to_vec()?; if v != w { return Err(failure::format_err!( "assertion failed: v == w\nv = {:?}\nw = {:?}", v, w)); @@ -42,7 +42,7 @@ mod for_each_artifact { }; let mut v = Vec::new(); - p.as_tsk().serialize(&mut v)?; + (&p.as_tsk() as &Marshal).serialize(&mut v)?; let q = openpgp::Cert::from_bytes(&v)?; if p != q { eprintln!("roundtripping {:?} failed", src); @@ -62,20 +62,21 @@ mod for_each_artifact { eprintln!("This is the recovered cert:\n{}", String::from_utf8_lossy( - &q.armored().to_vec().unwrap())); + &(&q.armored() as &MarshalInto).to_vec() + .unwrap())); } assert_eq!(p, q, "roundtripping {:?} failed", src); - let w = p.as_tsk().to_vec().unwrap(); + let w = (&p.as_tsk() as &MarshalInto).to_vec().unwrap(); assert_eq!(v, w, "Serialize and SerializeInto disagree on {:?}", p); // Check that Cert::into_packets() and Cert::to_vec() // agree. - let v = p.to_vec()?; + let v = (&p as &MarshalInto).to_vec()?; let mut buf = Vec::new(); for p in p.clone().into_packets() { - p.serialize(&mut buf)?; + (&p as &Marshal).serialize(&mut buf)?; } assert_eq!(buf, v); Ok(()) @@ -93,11 +94,11 @@ mod for_each_artifact { }; let mut v = Vec::new(); - p.serialize(&mut v)?; + (&p as &Marshal).serialize(&mut v)?; let q = openpgp::Message::from_bytes(&v)?; assert_eq!(p, q, "roundtripping {:?} failed", src); - let w = p.to_vec().unwrap(); + let w = (&p as &MarshalInto).to_vec().unwrap(); assert_eq!(v, w, "Serialize and SerializeInto disagree on {:?}", p); Ok(()) @@ -189,7 +190,7 @@ fn for_all_packets<F>(src: &Path, mut fun: F) -> openpgp::Result<()> &mut sink, openpgp::armor::Kind::File, &[])?; - packet.serialize(&mut w)?; + (&packet as &Marshal).serialize(&mut w)?; return Err(e); }, } |